From ec6c8b5883eccdf304b932e25f847a2546ec46f5 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 6 Aug 2015 17:10:50 +0200 Subject: fix UnmarshalOID --- ber/marshal.go | 2 +- ber/obj.go | 42 +++++++++++++++++++++++++++++------------- ber/obj_test.go | 25 +++++++++++++++---------- 3 files changed, 45 insertions(+), 24 deletions(-) (limited to 'ber') diff --git a/ber/marshal.go b/ber/marshal.go index c73c122..79c1879 100644 --- a/ber/marshal.go +++ b/ber/marshal.go @@ -84,6 +84,6 @@ func intEncoder(e *encodeState, v reflect.Value) { } func objEncoder(e *encodeState, v reflect.Value) { - b := marshalOID(v.Interface().(OID)) + b := MarshalOID(v.Interface().(OID)) e.Write(b) } diff --git a/ber/obj.go b/ber/obj.go index c92e657..fa19891 100644 --- a/ber/obj.go +++ b/ber/obj.go @@ -4,7 +4,7 @@ import "strconv" type OID []int -func marshalOID(obj OID) (b []byte) { +func MarshalOID(obj OID) (b []byte) { if len(obj) < 2 || obj[0] > 2 { return []byte{} } @@ -19,23 +19,27 @@ func marshalOID(obj OID) (b []byte) { return b } -func unmarshalOID(b []byte) OID { - return OID{} -} - func (o OID) Marshal() ([]byte, error) { - b := marshalOID(o) + b := MarshalOID(o) return b, nil } func UnmarshalOID(b []byte) (o OID) { - o = append(o, int(b[0])/40) - o = append(o, int(b[0])%40) - b = b[2:] - for i := 0; i < len(b); { - d, n := debase128(b[i:]) - o = append(o, d) - i += n + if len(b) < 1 { + return + } + v, n := debase128(b) + if v < 80 { + o = append(o, v/40) + o = append(o, v%40) + } else { + o = append(o, 2) + o = append(o, v-80) + } + + for i := n; i < len(b); i += n { + v, n = debase128(b[i:]) + o = append(o, v) } return } @@ -49,3 +53,15 @@ func (o OID) String() (s string) { } return } + +func (o OID) Equal(a OID) bool { + if len(o) != len(a) { + return false + } + for i := 0; i < len(o); i++ { + if o[i] != a[i] { + return false + } + } + return true +} diff --git a/ber/obj_test.go b/ber/obj_test.go index 7f1c4ab..53ea7f5 100644 --- a/ber/obj_test.go +++ b/ber/obj_test.go @@ -6,27 +6,32 @@ import ( ) type oidTest struct { - oid OID - out []byte + oid OID + out []byte + valid bool } var oidTestData = []oidTest{ {OID{1, 3, 12, 0, 218}, - []byte{0x2B, 0x0C, 0x00, 0x81, 0x5A}}, + []byte{0x2B, 0x0C, 0x00, 0x81, 0x5A}, true}, {OID{1, 3, 12, 0, 285, 200}, - []byte{0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48}}, - {OID{}, []byte{}}, - {OID{0, 39}, []byte{0x27}}, - {OID{1, 39}, []byte{0x4f}}, - {OID{1, 40}, []byte{}}, - {OID{2, 40}, []byte{0x78}}, + []byte{0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48}, true}, + {OID{}, []byte{}, false}, + {OID{0, 39}, []byte{0x27}, true}, + {OID{1, 39}, []byte{0x4f}, true}, + {OID{1, 40}, []byte{}, false}, + {OID{2, 40}, []byte{0x78}, true}, } func TestOID(t *testing.T) { for _, test := range oidTestData { - oid := marshalOID(test.oid) + oid := MarshalOID(test.oid) if !bytes.Equal(oid, test.out) { t.Error(test.oid, "expected", test.out, "got", oid) } + out := UnmarshalOID(test.out) + if test.valid && !out.Equal(test.oid) { + t.Error(test.out, "expected", test.oid, "got", out) + } } } -- cgit v1.2.3