Import upstream version 1.4, md5 dd71b8edbcaeb59048eaebe806c0130e
Debian Janitor
4 years ago
0 | 0 | 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 | |
10 | 30 | go_import_path: gopkg.in/asn-ber.v1 |
11 | 31 | install: |
12 | 32 | - go list -f '{{range .Imports}}{{.}} {{end}}' ./... | xargs go get -v |
14 | 34 | - go get code.google.com/p/go.tools/cmd/cover || go get golang.org/x/tools/cmd/cover |
15 | 35 | - go build -v ./... |
16 | 36 | script: |
17 | - go test -v -cover ./... | |
37 | - go test -v -cover ./... || go test -v ./... |
4 | 4 | "errors" |
5 | 5 | "fmt" |
6 | 6 | "io" |
7 | "math" | |
7 | 8 | "os" |
8 | 9 | "reflect" |
9 | 10 | ) |
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 | |
10 | 15 | |
11 | 16 | type Packet struct { |
12 | 17 | Identifier |
154 | 159 | } |
155 | 160 | } |
156 | 161 | |
162 | func WritePacket(out io.Writer, p *Packet) { | |
163 | printPacket(out, p, 0, false) | |
164 | } | |
165 | ||
157 | 166 | func PrintPacket(p *Packet) { |
158 | 167 | printPacket(os.Stdout, p, 0, false) |
159 | 168 | } |
206 | 215 | return string(data) |
207 | 216 | } |
208 | 217 | |
209 | func parseInt64(bytes []byte) (ret int64, err error) { | |
218 | func ParseInt64(bytes []byte) (ret int64, err error) { | |
210 | 219 | if len(bytes) > 8 { |
211 | 220 | // We'll overflow an int64 in this case. |
212 | 221 | err = fmt.Errorf("integer too large") |
329 | 338 | } |
330 | 339 | |
331 | 340 | // 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 | } | |
332 | 344 | content := make([]byte, length, length) |
333 | 345 | if length > 0 { |
334 | 346 | _, err := io.ReadFull(reader, content) |
348 | 360 | switch p.Tag { |
349 | 361 | case TagEOC: |
350 | 362 | case TagBoolean: |
351 | val, _ := parseInt64(content) | |
363 | val, _ := ParseInt64(content) | |
352 | 364 | |
353 | 365 | p.Value = val != 0 |
354 | 366 | case TagInteger: |
355 | p.Value, _ = parseInt64(content) | |
367 | p.Value, _ = ParseInt64(content) | |
356 | 368 | case TagBitString: |
357 | 369 | case TagOctetString: |
358 | 370 | // the actual string encoding is not known here |
365 | 377 | case TagExternal: |
366 | 378 | case TagRealFloat: |
367 | 379 | case TagEnumerated: |
368 | p.Value, _ = parseInt64(content) | |
380 | p.Value, _ = ParseInt64(content) | |
369 | 381 | case TagEmbeddedPDV: |
370 | 382 | case TagUTF8String: |
371 | 383 | p.Value = DecodeString(content) |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "bytes" |
4 | "io" | |
4 | 5 | "math" |
5 | ||
6 | "io" | |
7 | 6 | "testing" |
8 | 7 | ) |
9 | 8 | |
10 | 9 | func TestEncodeDecodeInteger(t *testing.T) { |
11 | 10 | for _, v := range []int64{0, 10, 128, 1024, math.MaxInt64, -1, -100, -128, -1024, math.MinInt64} { |
12 | 11 | enc := encodeInteger(v) |
13 | dec, err := parseInt64(enc) | |
12 | dec, err := ParseInt64(enc) | |
14 | 13 | if err != nil { |
15 | 14 | t.Fatalf("Error decoding %d : %s", v, err) |
16 | 15 | } |
17 | 16 | 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) | |
19 | 18 | } |
20 | 19 | |
21 | 20 | } |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "errors" |
4 | "fmt" | |
4 | 5 | "io" |
5 | 6 | ) |
6 | 7 | |
24 | 25 | return Identifier{}, 0, read, errors.New("indefinite length used with primitive type") |
25 | 26 | } |
26 | 27 | |
28 | if length < LengthIndefinite { | |
29 | err = fmt.Errorf("length cannot be less than %d", LengthIndefinite) | |
30 | return | |
31 | } | |
32 | ||
27 | 33 | return identifier, length, read, nil |
28 | 34 | } |
3 | 3 | "errors" |
4 | 4 | "fmt" |
5 | 5 | "io" |
6 | "math" | |
7 | 6 | ) |
8 | 7 | |
9 | 8 | func readIdentifier(reader io.Reader) (Identifier, int, error) { |
79 | 78 | |
80 | 79 | tag := identifier.Tag |
81 | 80 | |
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)...) | |
100 | 82 | } |
101 | 83 | return b |
102 | 84 | } |
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 | } |
341 | 341 | } |
342 | 342 | } |
343 | 343 | } |
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 | } |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "bytes" |
4 | "fmt" | |
4 | 5 | "io" |
5 | 6 | "io/ioutil" |
7 | "math" | |
6 | 8 | "testing" |
7 | 9 | ) |
8 | 10 | |
77 | 79 | {File: "tests/tc46.ber", Error: "indefinite length used with primitive type"}, |
78 | 80 | {File: "tests/tc47.ber", Error: "eoc child not allowed with definite length"}, |
79 | 81 | {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 | |
80 | 94 | } |
81 | 95 | |
82 | 96 | func TestSuiteDecodePacket(t *testing.T) { |
113 | 127 | } |
114 | 128 | } else if !bytes.Equal(dataOut, dataIn) { |
115 | 129 | // 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) | |
117 | 131 | } |
118 | 132 | |
119 | 133 | packet, err = DecodePacketErr(dataOut) |
125 | 139 | // Make sure the re-serialized data matches our original serialization |
126 | 140 | dataOut2 := packet.Bytes() |
127 | 141 | 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) | |
129 | 143 | } |
130 | 144 | } |
131 | 145 | } |
163 | 177 | } |
164 | 178 | } else if !bytes.Equal(dataOut, dataIn) { |
165 | 179 | // 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) | |
167 | 181 | } |
168 | 182 | |
169 | 183 | packet, err = DecodePacketErr(dataOut) |
175 | 189 | // Make sure the re-serialized data matches our original serialization |
176 | 190 | dataOut2 := packet.Bytes() |
177 | 191 | 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) | |
179 | 193 | } |
180 | 194 | } |
181 | 195 | } |
Binary diff not shown
0 | ˆ›0000000⏎ |
0 | …00000⏎ |