From 5a1439dcf94bfcd9eac4ae8b9d9ba60a3f7a3419 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 27 Jun 2015 22:16:24 +0200 Subject: Add class decector --- ber/class.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ber/class_test.go | 20 +++++++++ 2 files changed, 141 insertions(+) create mode 100644 ber/class.go create mode 100644 ber/class_test.go (limited to 'ber') diff --git a/ber/class.go b/ber/class.go new file mode 100644 index 0000000..c2e31b6 --- /dev/null +++ b/ber/class.go @@ -0,0 +1,121 @@ +package ber + +type Class byte + +const ( + Universal Class = iota << 6 + Application + ContextSpecific + Private +) + +// 1100 0000 +// C 0 +const ClassMask Class = 3 << 6 + +var classNames = map[Class]string{ + Universal: "Universal", + Application: "Application", + ContextSpecific: "Context-specific", + Private: "Private", +} + +func (c Class) String() string { return classNames[c] } + +type Identifier byte + +const ( + Primitive Identifier = iota << 5 + Constructed +) + +// 0010 0000 +// 2 0 +const IdentifierMask Identifier = 1 << 5 + +var identifierNames = map[Identifier]string{ + Primitive: "Primitive", + Constructed: "Constructed", +} + +func (i Identifier) String() string { return identifierNames[i] } + +type Tag byte + +const ( + EOT Tag = iota + Boolean + Integer + BitString + OctetString + Null + ObjectIdentifier + ObjectDescriptor + InstanceOf + Real + Enumerated + EmbeddedPDV + UTF8String + RelativeOID + _ + _ + Sequence // SequenceOf + Set // SetOf + NumericString + PrintableString + TeletexString // T61String + VideotexString + IA5String + UTCTime + GeneralizedTime + GraphicString + VisibleString // ISO646String + GeneralString + UniversalString + CharacterString + BMPString +) + +// 0001 1111 +// 1 F +const TagMask Tag = 0x20 - 1 + +var tagNames = map[Tag]string{ + EOT: "End-of-Content", + Boolean: "Boolean", + Integer: "Integer", + BitString: "Bit String", + OctetString: "Octet String", + Null: "Null", + ObjectIdentifier: "Object Identifier", + ObjectDescriptor: "Object Descriptor", + InstanceOf: "Instance Of", + Real: "Real", + Enumerated: "Enumerated", + EmbeddedPDV: "Embedded PDV", + UTF8String: "UTF8 String", + RelativeOID: "Relative OID", + Sequence: "Sequence / Of", + Set: "Set / Of", + NumericString: "Numeric String", + PrintableString: "Printable String", + TeletexString: "Teletext String", + VideotexString: "Videotex String", + IA5String: "IA5 String", + UTCTime: "UTC Time", + GeneralizedTime: "Generalized Time", + GraphicString: "Graphic String", + VisibleString: "Visible String", + GeneralString: "General String", + UniversalString: "Universal String", + CharacterString: "Character String", + BMPString: "BMP String", +} + +func (t Tag) String() string { return tagNames[t] } + +func Ident(b byte) (Class, Identifier, Tag) { + return Class(b) & ClassMask, + Identifier(b) & IdentifierMask, + Tag(b) & TagMask +} diff --git a/ber/class_test.go b/ber/class_test.go new file mode 100644 index 0000000..8e9a49a --- /dev/null +++ b/ber/class_test.go @@ -0,0 +1,20 @@ +package ber + +import "fmt" + +func ExampleIdent() { + b := []byte{0x60, 0xA1, 0x02, 0x30, 0x0A, 0x80, 0x7E, 0x04} + for _, v := range b { + c, i, t := Ident(v) + fmt.Printf("0x%.2X: %v %v %v - 0x%.2X\n", v, c, i, t, byte(t)) + } + // Output: + // 0x60: Application Constructed End-of-Content - 0x00 + // 0xA1: Context-specific Constructed Boolean - 0x01 + // 0x02: Universal Primitive Integer - 0x02 + // 0x30: Universal Constructed Sequence / Of - 0x10 + // 0x0A: Universal Primitive Enumerated - 0x0A + // 0x80: Context-specific Primitive End-of-Content - 0x00 + // 0x7E: Application Constructed BMP String - 0x1E + // 0x04: Universal Primitive Octet String - 0x04 +} -- cgit v1.2.3