/
decode.go
241 lines (208 loc) · 4.79 KB
/
decode.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package protowire
import (
"math"
)
const (
_ = -iota
errCodeTruncated
errCodeFieldNumber
errCodeOverflow
errCodeReserved
errCodeEndGroup
errCodeRecursionDepth
)
type BinaryDecoder struct{}
// ConsumeVarint parses b as a varint-encoded uint64, reporting its length.
// This returns a negative length upon an error (see ParseError).
func ConsumeVarint(b []byte) (v uint64, n int) {
var y uint64
if len(b) <= 0 {
return 0, errCodeTruncated
}
v = uint64(b[0])
if v < 0x80 {
return v, 1
}
v -= 0x80
if len(b) <= 1 {
return 0, errCodeTruncated
}
y = uint64(b[1])
v += y << 7
if y < 0x80 {
return v, 2
}
v -= 0x80 << 7
if len(b) <= 2 {
return 0, errCodeTruncated
}
y = uint64(b[2])
v += y << 14
if y < 0x80 {
return v, 3
}
v -= 0x80 << 14
if len(b) <= 3 {
return 0, errCodeTruncated
}
y = uint64(b[3])
v += y << 21
if y < 0x80 {
return v, 4
}
v -= 0x80 << 21
if len(b) <= 4 {
return 0, errCodeTruncated
}
y = uint64(b[4])
v += y << 28
if y < 0x80 {
return v, 5
}
v -= 0x80 << 28
if len(b) <= 5 {
return 0, errCodeTruncated
}
y = uint64(b[5])
v += y << 35
if y < 0x80 {
return v, 6
}
v -= 0x80 << 35
if len(b) <= 6 {
return 0, errCodeTruncated
}
y = uint64(b[6])
v += y << 42
if y < 0x80 {
return v, 7
}
v -= 0x80 << 42
if len(b) <= 7 {
return 0, errCodeTruncated
}
y = uint64(b[7])
v += y << 49
if y < 0x80 {
return v, 8
}
v -= 0x80 << 49
if len(b) <= 8 {
return 0, errCodeTruncated
}
y = uint64(b[8])
v += y << 56
if y < 0x80 {
return v, 9
}
v -= 0x80 << 56
if len(b) <= 9 {
return 0, errCodeTruncated
}
y = uint64(b[9])
v += y << 63
if y < 2 {
return v, 10
}
return 0, errCodeOverflow
}
// ConsumeFixed32 parses b as a little-endian uint32, reporting its length.
// This returns a negative length upon an error (see ParseError).
func ConsumeFixed32(b []byte) (v uint32, n int) {
if len(b) < 4 {
return 0, errCodeTruncated
}
v = uint32(b[0])<<0 | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
return v, 4
}
// ConsumeFixed64 parses b as a little-endian uint64, reporting its length.
// This returns a negative length upon an error (see ParseError).
func ConsumeFixed64(b []byte) (v uint64, n int) {
if len(b) < 8 {
return 0, errCodeTruncated
}
v = uint64(b[0])<<0 | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
return v, 8
}
// ConsumeBytes parses b as a length-prefixed bytes value, reporting its length.
// This returns a negative length upon an error (see ParseError).
func ConsumeBytes(b []byte) (v []byte, n int, all int) {
m, n := ConsumeVarint(b)
if n < 0 {
return nil, n, n
}
if m > uint64(len(b[n:])) {
return nil, errCodeTruncated, errCodeTruncated
}
return b[n:][:m], n, n + int(m) // V's bytes, L's bytelen, (L+V)'s bytelen
}
// DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.
//
// Input: {…, 5, 3, 1, 0, 2, 4, 6, …}
// Output: {…, -3, -2, -1, 0, +1, +2, +3, …}
func DecodeZigZag(x uint64) int64 {
return int64(x>>1) ^ int64(x)<<63>>63
}
func (BinaryDecoder) DecodeBool(b []byte) (bool, int) {
v, n := ConsumeVarint(b)
return int8(v) == 1, n
}
func (BinaryDecoder) DecodeByte(b []byte) byte {
return byte(b[0])
}
func (BinaryDecoder) DecodeInt32(b []byte) (int32, int) {
v, n := ConsumeVarint(b)
return int32(v), n
}
func (BinaryDecoder) DecodeSint32(b []byte) (int32, int) {
v, n := ConsumeVarint(b)
return int32(DecodeZigZag(v & math.MaxUint32)), n
}
func (BinaryDecoder) DecodeUint32(b []byte) (uint32, int) {
v, n := ConsumeVarint(b)
return uint32(v), n
}
func (BinaryDecoder) DecodeInt64(b []byte) (int64, int) {
v, n := ConsumeVarint(b)
return int64(v), n
}
func (BinaryDecoder) DecodeSint64(b []byte) (int64, int) {
v, n := ConsumeVarint(b)
return DecodeZigZag(v), n
}
func (BinaryDecoder) DecodeUint64(b []byte) (uint64, int) {
v, n := ConsumeVarint(b)
return v, n
}
func (BinaryDecoder) DecodeSfixed32(b []byte) (int32, int) {
v, n := ConsumeFixed32(b)
return int32(v), n
}
func (BinaryDecoder) DecodeFixed32(b []byte) (uint32, int) {
v, n := ConsumeFixed32(b)
return v, n
}
func (BinaryDecoder) DecodeFloat32(b []byte) (float32, int) {
v, n := ConsumeFixed32(b)
return math.Float32frombits(v), n
}
func (BinaryDecoder) DecodeSfixed64(b []byte) (int64, int) {
v, n := ConsumeFixed64(b)
return int64(v), n
}
func (BinaryDecoder) DecodeFixed64(b []byte) (uint64, int) {
v, n := ConsumeFixed64(b)
return v, n
}
func (BinaryDecoder) DecodeDouble(b []byte) (float64, int) {
v, n := ConsumeFixed64(b)
return math.Float64frombits(v), n
}
func (BinaryDecoder) DecodeString(b []byte) (string, int, int) {
v, n, all := ConsumeBytes(b)
return string(v), n, all
}
func (BinaryDecoder) DecodeBytes(b []byte) ([]byte, int, int) {
v, n, all := ConsumeBytes(b)
return v, n, all
}