-
Notifications
You must be signed in to change notification settings - Fork 13
/
encoder.go
443 lines (404 loc) · 14.9 KB
/
encoder.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package csproto
import (
"encoding/binary"
"math"
"unsafe"
)
// Encoder implements a binary Protobuf Encoder by sequentially writing to a wrapped []byte.
type Encoder struct {
p []byte
offset int
}
// NewEncoder initializes a new Protobuf encoder to write to the specified buffer, which must be
// pre-allocated by the caller with sufficient space to hold the message(s) being written.
func NewEncoder(p []byte) *Encoder {
return &Encoder{
p: p,
offset: 0,
}
}
// EncodeBool writes a varint-encoded boolean value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeBool(tag int, v bool) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
if v {
e.p[e.offset] = 1
} else {
e.p[e.offset] = 0
}
e.offset++
}
// EncodeString writes a length-delimited string value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeString(tag int, s string) {
b := e.stringToBytes(s)
e.EncodeBytes(tag, b)
}
// EncodeBytes writes a length-delimited byte slice to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeBytes(tag int, v []byte) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(v)))
copy(e.p[e.offset:], v)
e.offset += len(v)
}
// EncodeUInt32 writes a varint-encoded 32-bit unsigned integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeUInt32(tag int, v uint32) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
// EncodeUInt64 writes a varint-encoded 64-bit unsigned integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeUInt64(tag int, v uint64) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeVarint(e.p[e.offset:], v)
}
// EncodeInt32 writes a varint-encoded 32-bit signed integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeInt32(tag int, v int32) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
// EncodeInt64 writes a varint-encoded 64-bit signed integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeInt64(tag int, v int64) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
// EncodeSInt32 writes a zigzag-encoded 32-bit signed integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeSInt32(tag int, v int32) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeZigZag32(e.p[e.offset:], v)
}
// EncodeSInt64 writes a zigzag-encoded 64-bit signed integer value to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeSInt64(tag int, v int64) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeVarint)
e.offset += EncodeZigZag64(e.p[e.offset:], v)
}
// EncodeFixed32 writes a 32-bit unsigned integer value to the buffer using 4 bytes in little endian format,
// preceded by the varint-encoded tag key.
func (e *Encoder) EncodeFixed32(tag int, v uint32) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeFixed32)
e.offset += EncodeFixed32(e.p[e.offset:], v)
}
// EncodeFixed64 writes a 64-bit unsigned integer value to the buffer using 8 bytes in little endian format,
// preceded by the varint-encoded tag key.
func (e *Encoder) EncodeFixed64(tag int, v uint64) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeFixed64)
e.offset += EncodeFixed64(e.p[e.offset:], v)
}
// EncodeFloat32 writes a 32-bit IEEE 754 floating point value to the buffer using 4 bytes in little endian format,
// preceded by the varint-encoded tag key.
func (e *Encoder) EncodeFloat32(tag int, v float32) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeFixed32)
binary.LittleEndian.PutUint32(e.p[e.offset:], math.Float32bits(v))
e.offset += 4
}
// EncodeFloat64 writes a 64-bit IEEE 754 floating point value to the buffer using 8 bytes in little endian format,
// preceded by the varint-encoded tag key.
func (e *Encoder) EncodeFloat64(tag int, v float64) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeFixed64)
binary.LittleEndian.PutUint64(e.p[e.offset:], math.Float64bits(v))
e.offset += 8
}
// EncodePackedBool writes a list of booleans to the buffer using packed encoding, preceded by
// the varint-encoded tag key.
func (e *Encoder) EncodePackedBool(tag int, vs []bool) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)))
for _, v := range vs {
if v {
e.p[e.offset] = 1
} else {
e.p[e.offset] = 0
}
e.offset++
}
}
// EncodePackedInt32 writes a list of 32-bit integers to the buffer using packed encoding, preceded by
// the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedInt32(tag int, vs []int32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfVarint(uint64(v))
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
}
// EncodePackedInt64 writes a list of 64-bit integers to the buffer using packed encoding, preceded by
// the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedInt64(tag int, vs []int64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfVarint(uint64(v))
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
}
// EncodePackedUInt32 writes a list of 32-bit unsigned integers to the buffer using packed encoding,
// preceded by the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedUInt32(tag int, vs []uint32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfVarint(uint64(v))
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeVarint(e.p[e.offset:], uint64(v))
}
}
// EncodePackedUInt64 writes a list of 64-bit unsigned integers to the buffer using packed encoding,
// preceded by the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedUInt64(tag int, vs []uint64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfVarint(v)
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeVarint(e.p[e.offset:], v)
}
}
// EncodePackedSInt32 writes a list of 32-bit signed integers to the buffer using packed encoding,
// preceded by the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedSInt32(tag int, vs []int32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfZigZag(uint64(v))
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeZigZag32(e.p[e.offset:], v)
}
}
// EncodePackedSInt64 writes a list of 64-bit signed integers to the buffer using packed encoding,
// preceded by the varint-encoded tag key.
//
// This operation is O(n^2) because we have to traverse the list of values to calculate the total
// encoded size and write that size *before* the actual encoded values.
func (e *Encoder) EncodePackedSInt64(tag int, vs []int64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
sz := 0
for _, v := range vs {
sz += SizeOfZigZag(uint64(v))
}
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
for _, v := range vs {
e.offset += EncodeZigZag64(e.p[e.offset:], v)
}
}
// EncodePackedFixed32 writes a list of 32-bit fixed-width unsigned integers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedFixed32(tag int, vs []uint32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*4))
for _, v := range vs {
binary.LittleEndian.PutUint32(e.p[e.offset:], v)
e.offset += 4
}
}
// EncodePackedFixed64 writes a list of 64-bit fixed-width unsigned integers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedFixed64(tag int, vs []uint64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*8))
for _, v := range vs {
binary.LittleEndian.PutUint64(e.p[e.offset:], v)
e.offset += 8
}
}
// EncodePackedSFixed32 writes a list of 32-bit fixed-width signed integers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedSFixed32(tag int, vs []int32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*4))
for _, v := range vs {
binary.LittleEndian.PutUint32(e.p[e.offset:], uint32(v))
e.offset += 4
}
}
// EncodePackedSFixed64 writes a list of 64-bit fixed-width signed integers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedSFixed64(tag int, vs []int64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*8))
for _, v := range vs {
binary.LittleEndian.PutUint64(e.p[e.offset:], uint64(v))
e.offset += 8
}
}
// EncodePackedFloat32 writes a list of 32-bit floating point numbers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedFloat32(tag int, vs []float32) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*4))
for _, v := range vs {
binary.LittleEndian.PutUint32(e.p[e.offset:], math.Float32bits(v))
e.offset += 4
}
}
// EncodePackedFloat64 writes a list of 64-bit floating point numbers to the buffer using packed
// encoding, preceded by the varint-encoded tag key.
func (e *Encoder) EncodePackedFloat64(tag int, vs []float64) {
if len(vs) == 0 {
return
}
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(len(vs)*8))
for _, v := range vs {
binary.LittleEndian.PutUint64(e.p[e.offset:], math.Float64bits(v))
e.offset += 8
}
}
// EncodeNested writes a nested message to the buffer preceded by the varint-encoded tag key.
func (e *Encoder) EncodeNested(tag int, m interface{}) error {
sz := Size(m)
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(sz))
switch tv := m.(type) {
case MarshalerTo:
if err := tv.MarshalTo(e.p[e.offset:]); err != nil {
return err
}
e.offset += sz
return nil
case Marshaler:
buf, err := tv.Marshal()
if err != nil {
return err
}
copy(e.p[e.offset:], buf)
e.offset += sz
return nil
default:
buf, err := Marshal(tv)
if err != nil {
return err
}
copy(e.p[e.offset:], buf)
e.offset += sz
return nil
}
}
// EncodeRaw writes the raw bytes of d into the buffer at the current offset
func (e *Encoder) EncodeRaw(d []byte) {
if l := len(d); l > 0 {
copy(e.p[e.offset:], d)
e.offset += l
}
}
// EncodeMapEntryHeader writes a map entry header into the buffer, which consists of the specified
// tag with a wire type of WireTypeLengthDelimited followed by the varint encoded entry size.
func (e *Encoder) EncodeMapEntryHeader(tag int, size int) {
e.offset += EncodeTag(e.p[e.offset:], tag, WireTypeLengthDelimited)
e.offset += EncodeVarint(e.p[e.offset:], uint64(size))
}
// stringToBytes is an optimized convert from a string to a []byte using unsafe.Pointer
func (e *Encoder) stringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
// EncodeTag combines tag and wireType then encodes the result into dest using the Protobuf
// varint format and returns the number of bytes written.
func EncodeTag(dest []byte, tag int, wireType WireType) int {
// per the Protobuf spec, the field tag is: (tag << 3) | wireType
// ex:
// field #1, wire type varint (0) -> 8 (1000 in binary, 0x8 in hex)
// field #13, wire type fixed32 (5) -> 109 (1101101 in binary, 0x6d in hex)
k := (uint64(tag) << 3) | uint64(wireType)
return EncodeVarint(dest, k)
}
// EncodeVarint encodes v into dest using the Protobuf base-128 varint format and returns the number
// of bytes written
func EncodeVarint(dest []byte, v uint64) int {
n := 0
for v >= 1<<7 {
dest[n] = uint8(v&0x7f | 0x80)
v >>= 7
n++
}
dest[n] = uint8(v)
return n + 1
}
// EncodeFixed32 encodes v into dest using the Protobuf fixed 32-bit encoding, which is just the 4 bytes
// of the value in little-endian format, and returns the number of bytes written
func EncodeFixed32(dest []byte, v uint32) int {
binary.LittleEndian.PutUint32(dest, v)
return 4
}
// EncodeFixed64 encodes v into dest using the Protobuf fixed 64-bit encoding, which is just the 8 bytes
// of the value in little-endian format, and returns the number of bytes written
func EncodeFixed64(dest []byte, v uint64) int {
binary.LittleEndian.PutUint64(dest, v)
return 8
}
// EncodeZigZag32 encodes v into dest using the Protobuf zig/zag encoding for more efficient encoding
// of negative numbers, and returns the number of bytes written.
func EncodeZigZag32(dest []byte, v int32) int {
zz := uint64((uint32(v) << 1) ^ uint32((v >> 31)))
return EncodeVarint(dest, zz)
}
// EncodeZigZag64 encodes v into dest using the Protobuf zig/zag encoding for more efficient encoding
// of negative numbers, and returns the number of bytes written.
func EncodeZigZag64(dest []byte, v int64) int {
zz := uint64(v<<1) ^ uint64((v >> 63))
return EncodeVarint(dest, zz)
}