aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-08-08 13:05:41 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-08-08 13:05:41 +0200
commit4c4272628632cc881eb52f675e1219b4fbd491aa (patch)
treec3b5d0d5468a70c9fe251766bcb8c4aaf29e5f33
parent0eb96c8c2494cfa093dd289efa35be9b0db6727c (diff)
Parse len
-rw-r--r--ber/class.go10
-rw-r--r--ber/int.go7
-rw-r--r--ber/len.go12
-rw-r--r--ber/len_test.go25
-rw-r--r--ber/packet.go2
5 files changed, 38 insertions, 18 deletions
diff --git a/ber/class.go b/ber/class.go
index aca8220..23d31cd 100644
--- a/ber/class.go
+++ b/ber/class.go
@@ -117,16 +117,6 @@ func Ident(b []byte) (Class, Kind, Tag, int) {
return c, k, t, n + 1
}
-func Length(b []byte) (int, int) {
- if b[0]&0x80 != 0 {
- l := int(b[0] & 0x7f)
- i := UnmarshalUint(b[1 : l+1])
- return int(i), int(l + 1)
- } else {
- return int(b[0]), 1
- }
-}
-
func Unversal(b []byte) (int, bool) {
c, _, t, _ := Ident(b)
return int(t), c == classUniversal
diff --git a/ber/int.go b/ber/int.go
index ee28b4f..bcdc96f 100644
--- a/ber/int.go
+++ b/ber/int.go
@@ -14,13 +14,6 @@ func UnmarshalInt(b []byte) (i int) {
return
}
-func UnmarshalUint(b []byte) (i uint) {
- for n, v := range b {
- i += uint(v) << uint((len(b)-n-1)*8)
- }
- return
-}
-
func intLen(i int) (n int) {
for i > 255 {
n++
diff --git a/ber/len.go b/ber/len.go
new file mode 100644
index 0000000..aea9965
--- /dev/null
+++ b/ber/len.go
@@ -0,0 +1,12 @@
+package ber
+
+func UnmarshalLen(b []byte) (i, n int) {
+ if b[0]&0x80 == 0 {
+ return int(b[0]), 1
+ }
+ n = int(b[0]&0x7f)
+ for k := n; k > 0; k-- {
+ i += int(b[k]) << uint((k-1)*8)
+ }
+ return i, n
+}
diff --git a/ber/len_test.go b/ber/len_test.go
new file mode 100644
index 0000000..aeda5f9
--- /dev/null
+++ b/ber/len_test.go
@@ -0,0 +1,25 @@
+package ber
+
+import "testing"
+
+type lenTest struct {
+ in int
+ out []byte
+}
+
+var lenTestData = []lenTest{
+ {0, []byte{0x00}},
+ {23, []byte{0x17}},
+ {193, []byte{0x81, 0xc1}},
+ {240, []byte{0x81, 0xf0}},
+ {257, []byte{0x82, 0x01, 0x01}},
+}
+
+func TestLen(t *testing.T) {
+ for _, test := range lenTestData {
+ n, _ := UnmarshalLen(test.out)
+ if n != test.in {
+ t.Error(test.out, "expected", test.in, "got", n)
+ }
+ }
+}
diff --git a/ber/packet.go b/ber/packet.go
index 1a9465f..f244c4e 100644
--- a/ber/packet.go
+++ b/ber/packet.go
@@ -4,7 +4,7 @@ func Split(b []byte) (c Class, k Kind, t Tag, l int, v []byte, r []byte) {
var n int
c, k, t, n = Ident(b)
b = b[n:]
- l, n = Length(b)
+ l, n = UnmarshalLen(b)
b = b[n:]
v = b[:l]
r = b[l:]