diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-08-06 17:10:50 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-08-06 17:10:50 +0200 |
commit | ec6c8b5883eccdf304b932e25f847a2546ec46f5 (patch) | |
tree | cb99f714dac2f9642153dbed177a1be863b035de /ber | |
parent | f83e5f62aebf4924a393f5c131b32e0843a735c9 (diff) |
fix UnmarshalOID
Diffstat (limited to 'ber')
-rw-r--r-- | ber/marshal.go | 2 | ||||
-rw-r--r-- | ber/obj.go | 42 | ||||
-rw-r--r-- | ber/obj_test.go | 25 |
3 files changed, 45 insertions, 24 deletions
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) } @@ -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) + } } } |