aboutsummaryrefslogtreecommitdiff
path: root/ber
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-06-26 20:12:33 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-06-26 20:12:33 +0200
commit2864729d325aa13bd8591536185bf09bc0505bfa (patch)
treecbd0cb4da664b341428b6d761bae5ce969956c44 /ber
parent55a24dc100ec773be11ed0dc03ce8af05923b6ee (diff)
Obj marshaler
Diffstat (limited to 'ber')
-rw-r--r--ber/obj.go14
-rw-r--r--ber/obj_test.go14
2 files changed, 24 insertions, 4 deletions
diff --git a/ber/obj.go b/ber/obj.go
index 735ef97..f2e76d1 100644
--- a/ber/obj.go
+++ b/ber/obj.go
@@ -1,12 +1,13 @@
package ber
func base128(n int) (b []byte) {
+ if n == 0 {
+ return []byte{0x00}
+ }
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
@@ -22,8 +23,13 @@ func debase128(b []byte) int {
return 0
}
-func marshalObj(obj []int) []byte {
- return []byte{}
+func marshalObj(obj []int) (b []byte) {
+ first := obj[0]*40 + obj[1]
+ b = append(b, base128(first)...)
+ for _, o := range obj[2:] {
+ b = append(b, base128(o)...)
+ }
+ return b
}
func unmarshalObj(b []byte) []int {
diff --git a/ber/obj_test.go b/ber/obj_test.go
index e874d17..d8299bb 100644
--- a/ber/obj_test.go
+++ b/ber/obj_test.go
@@ -17,3 +17,17 @@ func TestBase128(t *testing.T) {
testBase128(t, 113549, []byte{0x86, 0xF7, 0x0D})
testBase128(t, 49152, []byte{0x83, 0x80, 0x00})
}
+
+func testObj(t *testing.T, o []int, e []byte) {
+ a := marshalObj(o)
+ if !bytes.Equal(a, e) {
+ t.Error("Obj", o, "expexted", e, "got", a)
+ }
+}
+
+func TestObj(t *testing.T) {
+ testObj(t, []int{1, 3, 12, 0, 218},
+ []byte{0x2B, 0x0C, 0x00, 0x81, 0x5A})
+ testObj(t, []int{1, 3, 12, 0, 285, 200},
+ []byte{0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48})
+}