| 1 | package berscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.13 errors/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 7 | ) |
| 8 | |
| 9 | func readIdentifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/readIdentifier().(reader ioscip-go gomod github.com/golang/go/src go1.13 io/.Readerscip-go gomod github.com/golang/go/src go1.13 io/Reader#) (Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#, int, error) { |
| 10 | identifier := Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#{} |
| 11 | read := 0 |
| 12 | |
| 13 | // identifier byte |
| 14 | b, err := readBytescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/readByte().(reader) |
| 15 | if err != nil { |
| 16 | if Debugscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Debug. { |
| 17 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printfscip-go gomod github.com/golang/go/src go1.13 fmt/Printf().("error reading identifier byte: %v\n", err) |
| 18 | } |
| 19 | return Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#{}, read, err |
| 20 | } |
| 21 | read++ |
| 22 | |
| 23 | identifier.ClassTypescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#ClassType. = Classscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Class#(b) & ClassBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/ClassBitmask. |
| 24 | identifier.TagTypescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#TagType. = Typescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Type#(b) & TypeBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/TypeBitmask. |
| 25 | |
| 26 | if tag := Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Tag#(b) & TagBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/TagBitmask.; tag != HighTagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTag. { |
| 27 | // short-form tag |
| 28 | identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. = tag |
| 29 | return identifier, read, nil |
| 30 | } |
| 31 | |
| 32 | // high-tag-number tag |
| 33 | tagBytes := 0 |
| 34 | for { |
| 35 | b, err := readBytescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/readByte().(reader) |
| 36 | if err != nil { |
| 37 | if Debugscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Debug. { |
| 38 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printfscip-go gomod github.com/golang/go/src go1.13 fmt/Printf().("error reading high-tag-number tag byte %d: %v\n", tagBytes, err) |
| 39 | } |
| 40 | return Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#{}, read, err |
| 41 | } |
| 42 | tagBytes++ |
| 43 | read++ |
| 44 | |
| 45 | // Lowest 7 bits get appended to the tag value (x.690, 8.1.2.4.2.b) |
| 46 | identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. <<= 7 |
| 47 | identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. |= Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Tag#(b) & HighTagValueBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTagValueBitmask. |
| 48 | |
| 49 | // First byte may not be all zeros (x.690, 8.1.2.4.2.c) |
| 50 | if tagBytes == 1 && identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. == 0 { |
| 51 | return Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#{}, read, errorsscip-go gomod github.com/golang/go/src go1.13 errors/.Newscip-go gomod github.com/golang/go/src go1.13 errors/New().("invalid first high-tag-number tag byte") |
| 52 | } |
| 53 | // Overflow of int64 |
| 54 | // TODO: support big int tags? |
| 55 | if tagBytes > 9 { |
| 56 | return Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#{}, read, errorsscip-go gomod github.com/golang/go/src go1.13 errors/.Newscip-go gomod github.com/golang/go/src go1.13 errors/New().("high-tag-number tag overflow") |
| 57 | } |
| 58 | |
| 59 | // Top bit of 0 means this is the last byte in the high-tag-number tag (x.690, 8.1.2.4.2.a) |
| 60 | if Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Tag#(b)&HighTagContinueBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTagContinueBitmask. == 0 { |
| 61 | break |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return identifier, read, nil |
| 66 | } |
| 67 | |
| 68 | func encodeIdentifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/encodeIdentifier().(identifier Identifierscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#) []byte { |
| 69 | b := []byte{0x0} |
| 70 | b[0] |= byte(identifier.ClassTypescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#ClassType.) |
| 71 | b[0] |= byte(identifier.TagTypescip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#TagType.) |
| 72 | |
| 73 | if identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. < HighTagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTag. { |
| 74 | // Short-form |
| 75 | b[0] |= byte(identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag.) |
| 76 | } else { |
| 77 | // high-tag-number |
| 78 | b[0] |= byte(HighTagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTag.) |
| 79 | |
| 80 | tag := identifier.Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Identifier#Tag. |
| 81 | |
| 82 | b = append(b, encodeHighTagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/encodeHighTag().(tag)...) |
| 83 | } |
| 84 | return b |
| 85 | } |
| 86 | |
| 87 | func encodeHighTagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/encodeHighTag().(tag Tagscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/Tag#) []byte { |
| 88 | // set cap=4 to hopefully avoid additional allocations |
| 89 | b := make([]byte, 0, 4) |
| 90 | for tag != 0 { |
| 91 | // t := last 7 bits of tag (HighTagValueBitmask = 0x7F) |
| 92 | t := tag & HighTagValueBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTagValueBitmask. |
| 93 | |
| 94 | // right shift tag 7 to remove what was just pulled off |
| 95 | tag >>= 7 |
| 96 | |
| 97 | // if b already has entries this entry needs a continuation bit (0x80) |
| 98 | if len(b) != 0 { |
| 99 | t |= HighTagContinueBitmaskscip-go gomod github.com/go-asn1-ber/asn1-ber . `github.com/go-asn1-ber/asn1-ber`/HighTagContinueBitmask. |
| 100 | } |
| 101 | |
| 102 | b = append(b, byte(t)) |
| 103 | } |
| 104 | // reverse |
| 105 | // since bits were pulled off 'tag' small to high the byte slice is in reverse order. |
| 106 | // example: tag = 0xFF results in {0x7F, 0x01 + 0x80 (continuation bit)} |
| 107 | // this needs to be reversed into 0x81 0x7F |
| 108 | for i, j := 0, len(b)-1; i < len(b)/2; i++ { |
| 109 | b[i], b[j-i] = b[j-i], b[i] |
| 110 | } |
| 111 | return b |
| 112 | } |