-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_any.go
172 lines (156 loc) · 3.84 KB
/
read_any.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//go:build !cbor_no_readany
package cbor
import (
"bytes"
"io"
)
// ReadAny returns the next object form [in] regardless of type.
// Outputs can be any of int64, uint64, bool, []byte, string, []any,
// map[any]any, float32, float64, nil.
func ReadAny(in io.Reader) (any, error) {
majorType, arg, value, err := readMajorType(in)
if err != nil {
return 0, err
}
return readAny(in, majorType, arg, value)
}
func readAny(in io.Reader, majorType MajorType, arg Arg, value uint64) (any, error) {
var err error
switch majorType {
case MajorTypeUInt:
switch {
case arg <= Arg8:
return readUnsigned[uint8](majorType, arg, value)
case arg == Arg16:
return readUnsigned[uint16](majorType, arg, value)
case arg == Arg32:
return readUnsigned[uint32](majorType, arg, value)
case arg == Arg64:
return readUnsigned[uint64](majorType, arg, value)
default:
return nil, ErrNotWellFormed
}
case MajorTypeNInt:
switch {
case arg <= Arg8:
return readSigned[int8](majorType, arg, value)
case arg == Arg16:
return readSigned[int16](majorType, arg, value)
case arg == Arg32:
return readSigned[int32](majorType, arg, value)
case arg == Arg64:
return readSigned[int64](majorType, arg, value)
default:
return nil, ErrNotWellFormed
}
case MajorTypeBstr:
b := bytes.NewBuffer(nil)
err = readBytes(in, majorType, arg, value,
func(indefinite bool, length uint64) error {
b.Grow(int(length))
return nil
},
b,
)
if err != nil {
return nil, err
}
return b.Bytes(), nil
case MajorTypeTstr:
b := bytes.NewBuffer(nil)
err = readBytes(in, majorType, arg, value,
func(indefinite bool, length uint64) error {
b.Grow(int(length))
return nil
},
b,
)
if err != nil {
return nil, err
}
return string(b.Bytes()), nil
case MajorTypeArray:
a := make([]any, 0, min(value, 16)) // limit pre allocate
if arg == ArgIndefinite {
for {
majorType, arg, value, err := readMajorType(in)
if err != nil {
return nil, err
}
if majorType == MajorTypeSimpleFloat && arg == SimpleBreak {
break
}
v, err := readAny(in, majorType, arg, value)
if err != nil {
return nil, err
}
a = append(a, v)
}
} else {
for i := uint64(0); i < value; i++ {
v, err := ReadAny(in)
if err != nil {
return nil, err
}
a = append(a, v)
}
}
return a, nil
case MajorTypeMap:
m := make(map[any]any, min(value, 16)) // limit pre allocate
if arg == ArgIndefinite {
for {
majorType, arg, value, err := readMajorType(in)
if err != nil {
return nil, err
}
if majorType == MajorTypeSimpleFloat && arg == SimpleBreak {
break
}
k, err := readAny(in, majorType, arg, value)
if err != nil {
return nil, err
}
v, err := ReadAny(in)
if err != nil {
return nil, err
}
m[k] = v
}
} else {
for i := uint64(0); i < value; i++ {
k, err := ReadAny(in)
if err != nil {
return nil, err
}
v, err := ReadAny(in)
if err != nil {
return nil, err
}
m[k] = v
}
}
return m, nil
case MajorTypeTagged:
return ReadAny(in)
default: // MajorTypeSimpleFloat:
switch {
case arg == 0 && Arg(value) < SimpleFalse:
return readUnsigned[uint8](majorType, arg, value)
case arg == 0 && (Arg(value) == SimpleFalse || Arg(value) == SimpleTrue):
return readBool(majorType, value)
case arg == 0 && (Arg(value) == SimpleNull || Arg(value) == SimpleUndefined):
return nil, nil
case arg == SimpleUint8:
return readUnsigned[uint8](majorType, arg, value)
case arg == SimpleFloat16:
return readFloat[float32](majorType, arg, value)
case arg == SimpleFloat32:
return readFloat[float32](majorType, arg, value)
case arg == SimpleFloat64:
return readFloat[float64](majorType, arg, value)
default: // SimpleBreak
return nil, ErrNotWellFormed
}
}
}