Codebase list golang-gopkg-asn1-ber.v1 / 8cd831e
Import upstream version 1.4, md5 dd71b8edbcaeb59048eaebe806c0130e Debian Janitor 4 years ago
11 changed file(s) with 125 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
00 language: go
1 go:
2 - 1.2
3 - 1.3
4 - 1.4
5 - 1.5
6 - 1.6
7 - 1.7
8 - 1.8
9 - tip
1 matrix:
2 include:
3 - go: 1.2.x
4 env: GOOS=linux GOARCH=amd64
5 - go: 1.2.x
6 env: GOOS=linux GOARCH=386
7 - go: 1.2.x
8 env: GOOS=windows GOARCH=amd64
9 - go: 1.2.x
10 env: GOOS=windows GOARCH=386
11 - go: 1.3.x
12 - go: 1.4.x
13 - go: 1.5.x
14 - go: 1.6.x
15 - go: 1.7.x
16 - go: 1.8.x
17 - go: 1.9.x
18 - go: 1.10.x
19 - go: 1.11.x
20 - go: 1.12.x
21 - go: 1.13.x
22 env: GOOS=linux GOARCH=amd64
23 - go: 1.13.x
24 env: GOOS=linux GOARCH=386
25 - go: 1.13.x
26 env: GOOS=windows GOARCH=amd64
27 - go: 1.13.x
28 env: GOOS=windows GOARCH=386
29 - go: tip
1030 go_import_path: gopkg.in/asn-ber.v1
1131 install:
1232 - go list -f '{{range .Imports}}{{.}} {{end}}' ./... | xargs go get -v
1434 - go get code.google.com/p/go.tools/cmd/cover || go get golang.org/x/tools/cmd/cover
1535 - go build -v ./...
1636 script:
17 - go test -v -cover ./...
37 - go test -v -cover ./... || go test -v ./...
44 "errors"
55 "fmt"
66 "io"
7 "math"
78 "os"
89 "reflect"
910 )
11
12 // MaxPacketLengthBytes specifies the maximum allowed packet size when calling ReadPacket or DecodePacket. Set to 0 for
13 // no limit.
14 var MaxPacketLengthBytes int64 = math.MaxInt32
1015
1116 type Packet struct {
1217 Identifier
154159 }
155160 }
156161
162 func WritePacket(out io.Writer, p *Packet) {
163 printPacket(out, p, 0, false)
164 }
165
157166 func PrintPacket(p *Packet) {
158167 printPacket(os.Stdout, p, 0, false)
159168 }
206215 return string(data)
207216 }
208217
209 func parseInt64(bytes []byte) (ret int64, err error) {
218 func ParseInt64(bytes []byte) (ret int64, err error) {
210219 if len(bytes) > 8 {
211220 // We'll overflow an int64 in this case.
212221 err = fmt.Errorf("integer too large")
329338 }
330339
331340 // Read definite-length content
341 if MaxPacketLengthBytes > 0 && int64(length) > MaxPacketLengthBytes {
342 return nil, read, fmt.Errorf("length %d greater than maximum %d", length, MaxPacketLengthBytes)
343 }
332344 content := make([]byte, length, length)
333345 if length > 0 {
334346 _, err := io.ReadFull(reader, content)
348360 switch p.Tag {
349361 case TagEOC:
350362 case TagBoolean:
351 val, _ := parseInt64(content)
363 val, _ := ParseInt64(content)
352364
353365 p.Value = val != 0
354366 case TagInteger:
355 p.Value, _ = parseInt64(content)
367 p.Value, _ = ParseInt64(content)
356368 case TagBitString:
357369 case TagOctetString:
358370 // the actual string encoding is not known here
365377 case TagExternal:
366378 case TagRealFloat:
367379 case TagEnumerated:
368 p.Value, _ = parseInt64(content)
380 p.Value, _ = ParseInt64(content)
369381 case TagEmbeddedPDV:
370382 case TagUTF8String:
371383 p.Value = DecodeString(content)
11
22 import (
33 "bytes"
4 "io"
45 "math"
5
6 "io"
76 "testing"
87 )
98
109 func TestEncodeDecodeInteger(t *testing.T) {
1110 for _, v := range []int64{0, 10, 128, 1024, math.MaxInt64, -1, -100, -128, -1024, math.MinInt64} {
1211 enc := encodeInteger(v)
13 dec, err := parseInt64(enc)
12 dec, err := ParseInt64(enc)
1413 if err != nil {
1514 t.Fatalf("Error decoding %d : %s", v, err)
1615 }
1716 if v != dec {
18 t.Error("TestEncodeDecodeInteger failed for %d (got %d)", v, dec)
17 t.Errorf("TestEncodeDecodeInteger failed for %d (got %d)", v, dec)
1918 }
2019
2120 }
0 module github.com/go-asn1-ber/asn1-ber
1
2 go 1.13
11
22 import (
33 "errors"
4 "fmt"
45 "io"
56 )
67
2425 return Identifier{}, 0, read, errors.New("indefinite length used with primitive type")
2526 }
2627
28 if length < LengthIndefinite {
29 err = fmt.Errorf("length cannot be less than %d", LengthIndefinite)
30 return
31 }
32
2733 return identifier, length, read, nil
2834 }
33 "errors"
44 "fmt"
55 "io"
6 "math"
76 )
87
98 func readIdentifier(reader io.Reader) (Identifier, int, error) {
7978
8079 tag := identifier.Tag
8180
82 highBit := uint(63)
83 for {
84 if tag&(1<<highBit) != 0 {
85 break
86 }
87 highBit--
88 }
89
90 tagBytes := int(math.Ceil(float64(highBit) / 7.0))
91 for i := tagBytes - 1; i >= 0; i-- {
92 offset := uint(i) * 7
93 mask := Tag(0x7f) << offset
94 tagByte := (tag & mask) >> offset
95 if i != 0 {
96 tagByte |= 0x80
97 }
98 b = append(b, byte(tagByte))
99 }
81 b = append(b, encodeHighTag(tag)...)
10082 }
10183 return b
10284 }
85
86 func encodeHighTag(tag Tag) []byte {
87 // set cap=4 to hopefully avoid additional allocations
88 b := make([]byte, 0, 4)
89 for tag != 0 {
90 // t := last 7 bits of tag (HighTagValueBitmask = 0x7F)
91 t := tag & HighTagValueBitmask
92
93 // right shift tag 7 to remove what was just pulled off
94 tag >>= 7
95
96 // if b already has entries this entry needs a continuation bit (0x80)
97 if len(b) != 0 {
98 t |= HighTagContinueBitmask
99 }
100
101 b = append(b, byte(t))
102 }
103 // reverse
104 // since bits were pulled off 'tag' small to high the byte slice is in reverse order.
105 // example: tag = 0xFF results in {0x7F, 0x01 + 0x80 (continuation bit)}
106 // this needs to be reversed into 0x81 0x7F
107 for i, j := 0, len(b)-1; i < len(b)/2; i++ {
108 b[i], b[j-i] = b[j-i], b[i]
109 }
110 return b
111 }
341341 }
342342 }
343343 }
344
345 func TestEncodeHighTag(t *testing.T) {
346 cases := []struct {
347 tag Tag
348 want []byte
349 }{
350 {134, []byte{0x80 + 0x01, 0x06}},
351 {123456, []byte{0x80 + 0x07, 0x80 + 0x44, 0x40}},
352 {0xFF, []byte{0x81, 0x7F}},
353 }
354
355 for _, c := range cases {
356 got := encodeHighTag(c.tag)
357
358 if !bytes.Equal(c.want, got) {
359 t.Errorf("tag: %d want: %#v got: %#v", c.tag, c.want, got)
360 }
361 }
362 }
11
22 import (
33 "bytes"
4 "fmt"
45 "io"
56 "io/ioutil"
7 "math"
68 "testing"
79 )
810
7779 {File: "tests/tc46.ber", Error: "indefinite length used with primitive type"},
7880 {File: "tests/tc47.ber", Error: "eoc child not allowed with definite length"},
7981 {File: "tests/tc48.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of more than 7 "unused bits" in BIT STRING with constrictive encoding form"
82 {File: "tests/tc49.ber", Error: ""},
83 {File: "tests/tc50.ber", Error: is64bit("length cannot be less than -1", "long-form length overflow")},
84 {File: "tests/tc51.ber", Error: is64bit(fmt.Sprintf("length 206966894640 greater than maximum %v", MaxPacketLengthBytes), "long-form length overflow")},
85 }
86
87 func is64bit(a, b string) string {
88 maxInt64 := int64(math.MaxInt64)
89 length := int(maxInt64)
90 if int64(length) != maxInt64 {
91 return b
92 }
93 return a
8094 }
8195
8296 func TestSuiteDecodePacket(t *testing.T) {
113127 }
114128 } else if !bytes.Equal(dataOut, dataIn) {
115129 // Make sure the serialized data matches the source
116 t.Errorf("%s: data should be the same", file)
130 t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataIn, dataOut)
117131 }
118132
119133 packet, err = DecodePacketErr(dataOut)
125139 // Make sure the re-serialized data matches our original serialization
126140 dataOut2 := packet.Bytes()
127141 if !bytes.Equal(dataOut, dataOut2) {
128 t.Errorf("%s: data should be the same", file)
142 t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataOut, dataOut2)
129143 }
130144 }
131145 }
163177 }
164178 } else if !bytes.Equal(dataOut, dataIn) {
165179 // Make sure the serialized data matches the source
166 t.Errorf("%s: data should be the same", file)
180 t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataIn, dataOut)
167181 }
168182
169183 packet, err = DecodePacketErr(dataOut)
175189 // Make sure the re-serialized data matches our original serialization
176190 dataOut2 := packet.Bytes()
177191 if !bytes.Equal(dataOut, dataOut2) {
178 t.Errorf("%s: data should be the same", file)
192 t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataOut, dataOut2)
179193 }
180194 }
181195 }
Binary diff not shown
0 ˆ›0000000
0 …00000