aboutsummaryrefslogtreecommitdiff
path: root/ber/new
diff options
context:
space:
mode:
Diffstat (limited to 'ber/new')
-rw-r--r--ber/new/ber_test.go196
-rw-r--r--ber/new/class.go135
-rw-r--r--ber/new/common.go77
-rw-r--r--ber/new/dump.go40
-rw-r--r--ber/new/marshal.go107
-rw-r--r--ber/new/unmarshal.go84
6 files changed, 0 insertions, 639 deletions
diff --git a/ber/new/ber_test.go b/ber/new/ber_test.go
deleted file mode 100644
index 8365987..0000000
--- a/ber/new/ber_test.go
+++ /dev/null
@@ -1,196 +0,0 @@
-package ber
-
-import (
- "bytes"
- "testing"
-)
-
-var boolTestData = map[bool][]byte{
- true: {0x01, 0xff},
- false: {0x01, 0x00},
-}
-
-func TestBool(t *testing.T) {
- for val, out := range boolTestData {
- s := state{}
- s.marshalBool(val)
- o := s.Bytes()
- if !bytes.Equal(o, out) {
- t.Error(val, "expeced", out, "got", o)
- }
- v := s.unmarshalBool()
- if v != val {
- t.Error(out, "expected", val, "got", v)
- }
- }
-}
-
-var intTestData = map[int][]byte{
- 0: {0x01, 0x00},
- 127: {0x01, 0x7f},
- 128: {0x02, 0x00, 0x80},
- 256: {0x02, 0x01, 0x00},
- -128: {0x01, 0x80},
- -129: {0x02, 0xff, 0x7f},
- 8388607: {0x03, 0x7f, 0xff, 0xff},
- -8388607: {0x03, 0x80, 0x00, 0x01},
- -136: {0x02, 0xff, 0x78},
-}
-
-func TestInt(t *testing.T) {
- for val, out := range intTestData {
- s := state{}
- s.marshalInt(val)
- o := s.Bytes()
- if !bytes.Equal(o, out) {
- t.Error(val, "expected", out, "got", o)
- }
- v := s.unmarshalInt()
- if v != val {
- t.Error(out, "expected", val, "got", v)
- }
- }
-}
-
-var stringTestData = map[string][]byte{
- "111": {0x03, 0x031, 0x031, 0x031},
- "0A16": {0x04, 0x30, 0x41, 0x31, 0x36},
-}
-
-func TestString(t *testing.T) {
- for val, out := range stringTestData {
- s := state{}
- s.marshalString(val)
- o := s.Bytes()
- if !bytes.Equal(o, out) {
- t.Error(val, "expected", out, "got", o)
- }
- v := s.unmarshalString()
- if v != val {
- t.Error(out, "expected", val, "got", v)
- }
- }
-}
-
-type oidTest struct {
- val OID
- out []byte
- valid bool
-}
-
-var oidTestData = []oidTest{
- {
- val: OID{1, 3, 12, 0, 218},
- out: []byte{0x05, 0x2B, 0x0C, 0x00, 0x81, 0x5A},
- valid: true,
- },
- {
- val: OID{1, 3, 12, 0, 285, 200},
- out: []byte{0x07, 0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48},
- valid: true,
- },
- {
- val: OID{0, 39},
- out: []byte{0x01, 0x27},
- valid: true,
- },
- {
- val: OID{1, 39},
- out: []byte{0x01, 0x4f},
- valid: true,
- },
- {
- val: OID{2, 40},
- out: []byte{0x01, 0x78},
- valid: true,
- },
- {
- val: OID{},
- out: []byte{},
- valid: true,
- },
- {
- val: OID{1, 40},
- out: []byte{},
- valid: false,
- },
-}
-
-func TestOID(t *testing.T) {
- for _, test := range oidTestData {
- s := state{}
- s.marshalOID(test.val)
- o := s.Bytes()
- if !bytes.Equal(o, test.out) {
- t.Error(test.val, "expected", test.out, "got", o)
- }
- v := s.unmarshalOID()
- if !v.Equal(test.val) && test.valid {
- t.Error(test.out, "expected", test.val, "got", v)
- }
- }
-}
-
-type bitStringTest struct {
- val BitString
- out []byte
-}
-
-var bitStringTestData = []bitStringTest{
- {
- val: BitString{
- false, false, false, false,
- true, false, false, false,
- false, false, false, false,
- false, false, false, false,
- },
- out: []byte{0x03, 0x00, 0x08, 0x00},
- },
- {
- val: BitString{true},
- out: []byte{0x02, 0x07, 0x80},
- },
- {
- val: BitString{false, false, true, false},
- out: []byte{0x02, 0x04, 0x20},
- },
-}
-
-func TestBitString(t *testing.T) {
- for _, test := range bitStringTestData {
- s := state{}
- s.marshalBitString(test.val)
- o := s.Bytes()
- if !bytes.Equal(o, test.out) {
- t.Error(test.val, "expected", test.out, "got", o)
- }
- v := s.unmarshalBitString()
- if !v.Equal(test.val) {
- t.Error(test.out, "expected", test.val, "got", v)
- }
- }
-}
-
-var lenTestData = map[int][]byte{
- 0: {0x00},
- 127: {0x7f},
- 128: {0x81, 0x80},
- 256: {0x82, 0x01, 0x00},
- 8388607: {0x83, 0x7f, 0xff, 0xff},
- //129: {0x80, 0x81, 0x00, 0x00}, // indefinite length
-}
-
-func TestLen(t *testing.T) {
- for val, out := range lenTestData {
- s := state{}
- s.marshalLen(val)
- o := s.Bytes()
- if !bytes.Equal(o, out) {
- t.Error(val, "expected", out, "got", o)
- }
- v := s.unmarshalLen()
- if v != val {
- t.Error(out, "expected", val, "got", v)
- }
- }
-}
diff --git a/ber/new/class.go b/ber/new/class.go
deleted file mode 100644
index 094c804..0000000
--- a/ber/new/class.go
+++ /dev/null
@@ -1,135 +0,0 @@
-package ber
-
-import "fmt"
-
-type Header struct {
- Class
- Kind
- Tag
-}
-
-type Class int
-
-const (
- classUniversal Class = iota << 6
- classApplication
- classContextSpecific
- classPrivate
- classMask Class = 3 << 6
-)
-
-var classNames = map[Class]string{
- classUniversal: "Universal",
- classApplication: "Application",
- classContextSpecific: "Context-specific",
- classPrivate: "Private",
-}
-
-func (c Class) String() string { return classNames[c] }
-
-type Kind int
-
-const (
- kindPrimitive Kind = iota << 5
- kindConstructed
- kindMask Kind = 1 << 5
-)
-
-var kindNames = map[Kind]string{
- kindPrimitive: "Primitive",
- kindConstructed: "Constructed",
-}
-
-func (k Kind) String() string { return kindNames[k] }
-
-type Tag int
-
-const (
- tagEOT Tag = iota
- tagBoolean
- tagInteger
- tagBitString
- tagOctetString
- tagNull
- tagObjectIdentifier
- tagObjectDescriptor
- tagInstanceOf
- tagReal
- tagEnumerated
- tagEmbeddedPDV
- tagUTF8String
- tagRelativeOID
- _
- _
- tagSequence // SequenceOf
- tagSet // SetOf
- tagNumericString
- tagPrintableString
- tagTeletexString // T61String
- tagVideotexString
- tagIA5String
- tagUTCTime
- tagGeneralizedTime
- tagGraphicString
- tagVisibleString // ISO646String
- tagGeneralString
- tagUniversalString
- tagCharacterString
- tagBMPString
- tagMask Tag = (1 << 5) - 1
-)
-
-var tagNames = map[Tag]string{
- tagEOT: "End-of-Content",
- tagBoolean: "Boolean",
- tagInteger: "Integer",
- tagBitString: "Bit String",
- tagOctetString: "Octet String",
- tagNull: "Null",
- tagObjectIdentifier: "Object Identifier",
- tagObjectDescriptor: "Object Descriptor",
- tagInstanceOf: "Instance Of",
- tagReal: "Real",
- tagEnumerated: "Enumerated",
- tagEmbeddedPDV: "Embedded PDV",
- tagUTF8String: "UTF8 String",
- tagRelativeOID: "Relative OID",
- tagSequence: "Sequence (Of)",
- tagSet: "Set (Of)",
- tagNumericString: "Numeric String",
- tagPrintableString: "Printable String",
- tagTeletexString: "Teletext String",
- tagVideotexString: "Videotex String",
- tagIA5String: "IA5 String",
- tagUTCTime: "UTC Time",
- tagGeneralizedTime: "Generalized Time",
- tagGraphicString: "Graphic String",
- tagVisibleString: "Visible String",
- tagGeneralString: "General String",
- tagUniversalString: "Universal String",
- tagCharacterString: "Character String",
- tagBMPString: "BMP String",
-}
-
-func (t Tag) String() string { return tagNames[t] }
-
-func (s *state) ident() Header {
- b, _ := s.ReadByte()
- tag := Tag(b) & tagMask
- if tag == tagMask {
- tag = Tag(s.unmarshalBase128())
- }
- return Header{
- Class: Class(b) & classMask,
- Kind: Kind(b) & kindMask,
- Tag: tag,
- }
-}
-
-func (h Header) String() string {
- f := "%v %v %v"
- if h.Class != classUniversal {
- f = "%v %v %d"
- }
- return fmt.Sprintf(f, h.Class, h.Kind, h.Tag)
-}
diff --git a/ber/new/common.go b/ber/new/common.go
deleted file mode 100644
index 7c4f496..0000000
--- a/ber/new/common.go
+++ /dev/null
@@ -1,77 +0,0 @@
-package ber
-
-import (
- "bytes"
- "strconv"
- "strings"
-)
-
-type state struct{ bytes.Buffer }
-
-func newState(b []byte) *state {
- s := &state{}
- s.Write(b)
- return s
-}
-
-func (s *state) subState() *state {
- ss := state{}
- ss.Write(s.next())
- return &ss
-}
-
-func (s *state) next() []byte {
- l := s.unmarshalLen()
- return s.Next(l)
-}
-
-type OID []int
-
-func (o OID) Equal(p OID) bool {
- if len(o) != len(p) {
- return false
- }
- for i := range o {
- if o[i] != p[i] {
- return false
- }
- }
- return true
-}
-
-func (o OID) String() string {
- s := make([]string, len(o))
- for i, v := range o {
- s[i] = strconv.Itoa(v)
- }
- return strings.Join(s, ".")
-}
-
-type BitString []bool
-
-func (o BitString) Equal(p BitString) bool {
- if len(o) != len(p) {
- return false
- }
- for i := range o {
- if o[i] != p[i] {
- return false
- }
- }
- return true
-}
-
-func (o BitString) String() string {
- bmap := map[bool]string{
- true: "1",
- false: "0",
- }
- var s string
- for i, bit := range o {
- if i != 0 && i%4 == 0 {
- s += " "
- }
- s += bmap[bit]
- }
- return s
-}
diff --git a/ber/new/dump.go b/ber/new/dump.go
deleted file mode 100644
index 330bc7a..0000000
--- a/ber/new/dump.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package ber
-
-import (
- "fmt"
-)
-
-func Dump(b []byte) string {
- return newState(b).dump(0)
-}
-
-func (s *state) dump(indent int) string {
- if s.Len() == 0 {
- return ""
- }
- //fmt.Printf("%*s%x %x\n", 2*indent, "", s.Len(), s.Bytes())
- for s.Len() > 0 {
- h := s.ident()
- fmt.Printf("%*s%v: ", 2*indent, "", h)
- if h.Class == classUniversal && h.Kind == kindPrimitive {
- switch h.Tag {
- case tagInteger, tagEnumerated:
- fmt.Println(s.unmarshalInt())
- case tagBoolean:
- fmt.Println(s.unmarshalBool())
- case tagObjectIdentifier:
- fmt.Println(s.unmarshalOID())
- case tagBitString:
- fmt.Println(s.unmarshalBitString())
- case tagIA5String, tagOctetString:
- fmt.Println(s.unmarshalString())
- default:
- fmt.Println(s.next())
- }
- } else {
- fmt.Println("→")
- s.subState().dump(indent + 1)
- }
- }
- return s.dump(indent)
-}
diff --git a/ber/new/marshal.go b/ber/new/marshal.go
deleted file mode 100644
index ed63be6..0000000
--- a/ber/new/marshal.go
+++ /dev/null
@@ -1,107 +0,0 @@
-package ber
-
-func lenLen(i int) int {
- n := 1
- for ; i > 255; i >>= 8 {
- n++
- }
- return n
-}
-
-func (s *state) marshalLen(val int) {
- if val < 0x80 {
- s.WriteByte(byte(val))
- return
- }
- n := lenLen(val)
- s.WriteByte(byte(n) | 0x80)
- for ; n > 0; n-- {
- s.WriteByte(byte(val >> uint((n-1)*8)))
- }
-}
-
-func (s *state) marshalBool(val bool) {
- s.marshalLen(1)
- if val {
- s.WriteByte(0xff)
- } else {
- s.WriteByte(0)
- }
-}
-
-func intLen(i int) int {
- n := 1
- for ; i > 127; i >>= 8 {
- n++
- }
- for ; i < -128; i >>= 8 {
- n++
- }
- return n
-}
-
-func (s *state) marshalInt(val int) {
- n := intLen(val)
- s.marshalLen(n)
- for ; n > 0; n-- {
- s.WriteByte(byte(val >> uint((n-1)*8)))
- }
-}
-
-func (s *state) marshalString(val string) {
- s.marshalLen(len(val))
- s.Write([]byte(val))
-}
-
-func (s *state) marshalBase128(val int) {
- if val == 0 {
- s.WriteByte(0)
- return
- }
- var l int
- for i := val; i > 0; i >>= 7 {
- l++
- }
- for i := l - 1; i >= 0; i-- {
- o := byte(val >> uint(i*7) & 0x7f)
- if i != 0 {
- o |= 0x80
- }
- s.WriteByte(o)
- }
-}
-
-func (s *state) marshalOID(val OID) {
- if len(val) < 2 || val[0] > 2 {
- return
- }
- if val[0] < 2 && val[1] > 39 {
- return
- }
- buf := &state{}
- buf.marshalBase128(val[0]*40 + val[1])
- for _, v := range val[2:] {
- buf.marshalBase128(v)
- }
- s.marshalLen(buf.Len())
- s.Write(buf.Bytes())
-}
-
-func (s *state) marshalBitString(val BitString) {
- pad := (8 - len(val)%8) % 8
- l := len(val) / 8
- if pad != 0 {
- l++
- }
- b := make([]byte, l)
- for i, v := range val {
- if v {
- x := i / 8
- y := 7 - uint(i%8)
- b[x] |= 1 << y
- }
- }
- s.marshalLen(l + 1)
- s.WriteByte(byte(pad))
- s.Write(b)
-}
diff --git a/ber/new/unmarshal.go b/ber/new/unmarshal.go
deleted file mode 100644
index b29eba5..0000000
--- a/ber/new/unmarshal.go
+++ /dev/null
@@ -1,84 +0,0 @@
-package ber
-
-func (s *state) unmarshalLen() int {
- b, _ := s.ReadByte()
- if b&0x80 == 0 {
- return int(b)
- }
- l := int(b) & 0x7f
- var n int
- for i, v := range s.Next(l) {
- n |= int(v) << uint((l-i-1)*8)
- }
- return n
-}
-
-func (s *state) unmarshalBase128() int {
- var i int
- for {
- b, err := s.ReadByte()
- if err != nil {
- return i
- }
- i = (i << 7) | int(b&0x7f)
- if b&0x80 == 0 {
- return i
- }
- }
-}
-
-func (s *state) unmarshalBool() bool {
- b := s.next()
- return b[0] != 0x00
-}
-
-func (s *state) unmarshalInt() int {
- b := s.next()
- neg := b[0]&0x80 != 0
- var i int
- for _, v := range b {
- if neg {
- v = ^v
- }
- i = (i << 8) | int(v)
- }
- if neg {
- i = ^i
- }
- return i
-}
-
-func (s *state) unmarshalString() string {
- return string(s.next())
-}
-
-func (s *state) unmarshalOID() OID {
- var o OID
- if s.Len() < 2 {
- return o
- }
- buf := s.subState()
- v := buf.unmarshalBase128()
- if v < 80 {
- o = OID{v / 40, v % 40}
- } else {
- o = OID{2, v - 80}
- }
- for buf.Len() > 0 {
- o = append(o, buf.unmarshalBase128())
- }
- return o
-}
-
-func (s *state) unmarshalBitString() BitString {
- var bs BitString
- b := s.next()
- pad := int(b[0])
- l := (len(b)-1)*8 - pad
- for i := 0; i < l; i++ {
- x := 1 + i/8
- y := 7 - uint(i%8)
- bs = append(bs, b[x]&(1<<y) != 0)
- }
- return bs
-}