diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-06-30 19:04:10 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-06-30 19:04:10 +0200 |
commit | 50b0ac29b852743325bd9d57b1660f2f90b6d3c4 (patch) | |
tree | 75be314b7f6f3cfc1c5eb670bdac6dbc4468e8f5 /ber | |
parent | a5dba3e8cfd95b3bc5d98aa0f0ca8da81eafedb5 (diff) |
Split Base128
Diffstat (limited to 'ber')
-rw-r--r-- | ber/base128.go | 32 | ||||
-rw-r--r-- | ber/base128_test.go | 26 | ||||
-rw-r--r-- | ber/obj.go | 31 | ||||
-rw-r--r-- | ber/obj_test.go | 20 |
4 files changed, 58 insertions, 51 deletions
diff --git a/ber/base128.go b/ber/base128.go new file mode 100644 index 0000000..7e44d76 --- /dev/null +++ b/ber/base128.go @@ -0,0 +1,32 @@ +package ber + +func base128(n int) (b []byte) { + if n == 0 { + return []byte{0} + } + l := 0 + for i := n; i > 0; i >>= 7 { + l++ + } + for i := l - 1; i >= 0; i-- { + o := byte(n >> uint(i*7)) + o &= 0x7F + if i != 0 { + o |= 0x80 + } + b = append(b, o) + } + return +} + +func debase128(b []byte) (i, n int) { + for _, v := range b { + i <<= 7 + i |= int(v & 0x7f) + n++ + if v & 0x80 == 0 { + return + } + } + return +} diff --git a/ber/base128_test.go b/ber/base128_test.go new file mode 100644 index 0000000..c06bca3 --- /dev/null +++ b/ber/base128_test.go @@ -0,0 +1,26 @@ +package ber + +import ( + "bytes" + "testing" +) + +type base128Test struct { + in int + out []byte +} + +var base128TestData = []base128Test{ + {643, []byte{0x85, 0x03}}, + {113549, []byte{0x86, 0xF7, 0x0D}}, + {49152, []byte{0x83, 0x80, 0x00}}, +} + +func TestBase128(t *testing.T) { + for _, test := range base128TestData { + n := base128(test.in) + if !bytes.Equal(n, test.out) { + t.Error(test.in, "expected", test.out, "got", n) + } + } +} @@ -4,37 +4,6 @@ import "strconv" type OID []int -func base128(n int) (b []byte) { - if n == 0 { - return []byte{0} - } - l := 0 - for i := n; i > 0; i >>= 7 { - l++ - } - for i := l - 1; i >= 0; i-- { - o := byte(n >> uint(i*7)) - o &= 0x7F - if i != 0 { - o |= 0x80 - } - b = append(b, o) - } - return -} - -func debase128(b []byte) (i, n int) { - for _, v := range b { - i <<= 7 - i |= int(v & 0x7f) - n++ - if v & 0x80 == 0 { - return - } - } - return -} - func marshalOID(obj OID) (b []byte) { if len(obj) < 2 || obj[0] > 2 { return []byte{} diff --git a/ber/obj_test.go b/ber/obj_test.go index 27fd8e6..7f1c4ab 100644 --- a/ber/obj_test.go +++ b/ber/obj_test.go @@ -5,26 +5,6 @@ import ( "testing" ) -type base128Test struct { - in int - out []byte -} - -var base128TestData = []base128Test{ - {643, []byte{0x85, 0x03}}, - {113549, []byte{0x86, 0xF7, 0x0D}}, - {49152, []byte{0x83, 0x80, 0x00}}, -} - -func TestBase128(t *testing.T) { - for _, test := range base128TestData { - n := base128(test.in) - if !bytes.Equal(n, test.out) { - t.Error(test.in, "expected", test.out, "got", n) - } - } -} - type oidTest struct { oid OID out []byte |