aboutsummaryrefslogtreecommitdiff
path: root/ber
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-26 13:16:07 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-26 13:16:07 +0200
commit0a6d80154a138f8240ba18fc670854021aeee648 (patch)
tree6cf34ae75aa26cac84cbd823c32090da002ebf84 /ber
parent54caa4e7e085e177ff05b719ae247d21fe8c257e (diff)
fix marshalClass
Diffstat (limited to 'ber')
-rw-r--r--ber/ber_test.go23
-rw-r--r--ber/marshal.go11
2 files changed, 29 insertions, 5 deletions
diff --git a/ber/ber_test.go b/ber/ber_test.go
index 8365987..46cab4e 100644
--- a/ber/ber_test.go
+++ b/ber/ber_test.go
@@ -194,3 +194,26 @@ func TestLen(t *testing.T) {
}
}
}
+
+var classTestData = map[Header][]byte{
+ {classUniversal, kindPrimitive, tagInteger}: {0x02},
+ {classUniversal, kindPrimitive, 32}: {0x1f, 0x20},
+ {classUniversal, kindPrimitive, 256}: {0x1f, 0x82, 0x00},
+ {classApplication, kindConstructed, 2}: {0x62},
+ {classContextSpecific, kindConstructed, 1}: {0xa1},
+}
+
+func TestClass(t *testing.T) {
+ for val, out := range classTestData {
+ s := state{}
+ s.marshalClass(val)
+ o := s.Bytes()
+ if !bytes.Equal(o, out) {
+ t.Error(val, "expected", out, "got", o)
+ }
+ v := s.unmarshalClass()
+ if v != val {
+ t.Error(out, "expected", val, "got", v)
+ }
+ }
+}
diff --git a/ber/marshal.go b/ber/marshal.go
index 041cb1b..831ee5b 100644
--- a/ber/marshal.go
+++ b/ber/marshal.go
@@ -107,9 +107,10 @@ func (s *state) marshalBitString(val BitString) {
}
func (s *state) marshalClass(h Header) {
- buf := &state{}
- buf.marshalBase128(int(h.Tag))
- b := buf.Bytes()
- b[0] |= byte(h.Class) | byte(h.Kind)
- s.Write(b)
+ if h.Tag >= tagMask {
+ s.WriteByte(byte(h.Class) | byte(h.Kind) | byte(tagMask))
+ s.marshalBase128(int(h.Tag))
+ } else {
+ s.WriteByte(byte(h.Class) | byte(h.Kind) | byte(h.Tag))
+ }
}