-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvalidate.go
56 lines (50 loc) · 1.14 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package typegen
import (
"bytes"
"fmt"
"io"
)
// ValidateCBOR validates that a byte array is a single valid CBOR object.
func ValidateCBOR(b []byte) error {
// The code here is basically identical to the previous function, it
// just doesn't copy.
br := bytes.NewReader(b)
for remaining := uint64(1); remaining > 0; remaining-- {
maj, extra, err := CborReadHeader(br)
if err != nil {
return err
}
switch maj {
case MajUnsignedInt, MajNegativeInt, MajOther:
// nothing fancy to do
case MajByteString, MajTextString:
if extra > ByteArrayMaxLen {
return maxLengthError
}
if uint64(br.Len()) < extra {
return io.ErrUnexpectedEOF
}
if _, err := br.Seek(int64(extra), io.SeekCurrent); err != nil {
return err
}
case MajTag:
remaining++
case MajArray:
if extra > MaxLength {
return maxLengthError
}
remaining += extra
case MajMap:
if extra > MaxLength {
return maxLengthError
}
remaining += extra * 2
default:
return fmt.Errorf("unhandled deferred cbor type: %d", maj)
}
}
if br.Len() > 0 {
return fmt.Errorf("unexpected %d unread bytes", br.Len())
}
return nil
}