This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
encode.go
595 lines (542 loc) · 12.2 KB
/
encode.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package amqp
import (
"encoding/binary"
"math"
"time"
"unicode/utf8"
)
// writesFrame encodes fr into buf.
func writeFrame(buf *buffer, fr frame) error {
// write header
buf.write([]byte{
0, 0, 0, 0, // size, overwrite later
2, // doff, see frameHeader.DataOffset comment
fr.type_, // frame type
})
buf.writeUint16(fr.channel) // channel
// write AMQP frame body
err := marshal(buf, fr.body)
if err != nil {
return err
}
// validate size
if uint(buf.len()) > math.MaxUint32 {
return errorNew("frame too large")
}
// retrieve raw bytes
bufBytes := buf.bytes()
// write correct size
binary.BigEndian.PutUint32(bufBytes, uint32(len(bufBytes)))
return nil
}
type marshaler interface {
marshal(*buffer) error
}
func marshal(wr *buffer, i interface{}) error {
switch t := i.(type) {
case nil:
wr.writeByte(byte(typeCodeNull))
case bool:
if t {
wr.writeByte(byte(typeCodeBoolTrue))
} else {
wr.writeByte(byte(typeCodeBoolFalse))
}
case *bool:
if *t {
wr.writeByte(byte(typeCodeBoolTrue))
} else {
wr.writeByte(byte(typeCodeBoolFalse))
}
case uint:
writeUint64(wr, uint64(t))
case *uint:
writeUint64(wr, uint64(*t))
case uint64:
writeUint64(wr, t)
case *uint64:
writeUint64(wr, *t)
case uint32:
writeUint32(wr, t)
case *uint32:
writeUint32(wr, *t)
case uint16:
wr.writeByte(byte(typeCodeUshort))
wr.writeUint16(t)
case *uint16:
wr.writeByte(byte(typeCodeUshort))
wr.writeUint16(*t)
case uint8:
wr.write([]byte{
byte(typeCodeUbyte),
t,
})
case *uint8:
wr.write([]byte{
byte(typeCodeUbyte),
*t,
})
case int:
writeInt64(wr, int64(t))
case *int:
writeInt64(wr, int64(*t))
case int8:
wr.write([]byte{
byte(typeCodeByte),
uint8(t),
})
case *int8:
wr.write([]byte{
byte(typeCodeByte),
uint8(*t),
})
case int16:
wr.writeByte(byte(typeCodeShort))
wr.writeUint16(uint16(t))
case *int16:
wr.writeByte(byte(typeCodeShort))
wr.writeUint16(uint16(*t))
case int32:
writeInt32(wr, t)
case *int32:
writeInt32(wr, *t)
case int64:
writeInt64(wr, t)
case *int64:
writeInt64(wr, *t)
case float32:
writeFloat(wr, t)
case *float32:
writeFloat(wr, *t)
case float64:
writeDouble(wr, t)
case *float64:
writeDouble(wr, *t)
case string:
return writeString(wr, t)
case *string:
return writeString(wr, *t)
case []byte:
return writeBinary(wr, t)
case *[]byte:
return writeBinary(wr, *t)
case map[interface{}]interface{}:
return writeMap(wr, t)
case *map[interface{}]interface{}:
return writeMap(wr, *t)
case map[string]interface{}:
return writeMap(wr, t)
case *map[string]interface{}:
return writeMap(wr, *t)
case map[symbol]interface{}:
return writeMap(wr, t)
case *map[symbol]interface{}:
return writeMap(wr, *t)
case unsettled:
return writeMap(wr, t)
case *unsettled:
return writeMap(wr, *t)
case time.Time:
writeTimestamp(wr, t)
case *time.Time:
writeTimestamp(wr, *t)
case []int8:
return arrayInt8(t).marshal(wr)
case *[]int8:
return arrayInt8(*t).marshal(wr)
case []uint16:
return arrayUint16(t).marshal(wr)
case *[]uint16:
return arrayUint16(*t).marshal(wr)
case []int16:
return arrayInt16(t).marshal(wr)
case *[]int16:
return arrayInt16(*t).marshal(wr)
case []uint32:
return arrayUint32(t).marshal(wr)
case *[]uint32:
return arrayUint32(*t).marshal(wr)
case []int32:
return arrayInt32(t).marshal(wr)
case *[]int32:
return arrayInt32(*t).marshal(wr)
case []uint64:
return arrayUint64(t).marshal(wr)
case *[]uint64:
return arrayUint64(*t).marshal(wr)
case []int64:
return arrayInt64(t).marshal(wr)
case *[]int64:
return arrayInt64(*t).marshal(wr)
case []float32:
return arrayFloat(t).marshal(wr)
case *[]float32:
return arrayFloat(*t).marshal(wr)
case []float64:
return arrayDouble(t).marshal(wr)
case *[]float64:
return arrayDouble(*t).marshal(wr)
case []bool:
return arrayBool(t).marshal(wr)
case *[]bool:
return arrayBool(*t).marshal(wr)
case []string:
return arrayString(t).marshal(wr)
case *[]string:
return arrayString(*t).marshal(wr)
case []symbol:
return arraySymbol(t).marshal(wr)
case *[]symbol:
return arraySymbol(*t).marshal(wr)
case [][]byte:
return arrayBinary(t).marshal(wr)
case *[][]byte:
return arrayBinary(*t).marshal(wr)
case []time.Time:
return arrayTimestamp(t).marshal(wr)
case *[]time.Time:
return arrayTimestamp(*t).marshal(wr)
case []UUID:
return arrayUUID(t).marshal(wr)
case *[]UUID:
return arrayUUID(*t).marshal(wr)
case []interface{}:
return list(t).marshal(wr)
case *[]interface{}:
return list(*t).marshal(wr)
case marshaler:
return t.marshal(wr)
default:
return errorErrorf("marshal not implemented for %T", i)
}
return nil
}
func writeInt32(wr *buffer, n int32) {
if n < 128 && n >= -128 {
wr.write([]byte{
byte(typeCodeSmallint),
byte(n),
})
return
}
wr.writeByte(byte(typeCodeInt))
wr.writeUint32(uint32(n))
}
func writeInt64(wr *buffer, n int64) {
if n < 128 && n >= -128 {
wr.write([]byte{
byte(typeCodeSmalllong),
byte(n),
})
return
}
wr.writeByte(byte(typeCodeLong))
wr.writeUint64(uint64(n))
}
func writeUint32(wr *buffer, n uint32) {
if n == 0 {
wr.writeByte(byte(typeCodeUint0))
return
}
if n < 256 {
wr.write([]byte{
byte(typeCodeSmallUint),
byte(n),
})
return
}
wr.writeByte(byte(typeCodeUint))
wr.writeUint32(n)
}
func writeUint64(wr *buffer, n uint64) {
if n == 0 {
wr.writeByte(byte(typeCodeUlong0))
return
}
if n < 256 {
wr.write([]byte{
byte(typeCodeSmallUlong),
byte(n),
})
return
}
wr.writeByte(byte(typeCodeUlong))
wr.writeUint64(n)
}
func writeFloat(wr *buffer, f float32) {
wr.writeByte(byte(typeCodeFloat))
wr.writeUint32(math.Float32bits(f))
}
func writeDouble(wr *buffer, f float64) {
wr.writeByte(byte(typeCodeDouble))
wr.writeUint64(math.Float64bits(f))
}
func writeTimestamp(wr *buffer, t time.Time) {
wr.writeByte(byte(typeCodeTimestamp))
ms := t.UnixNano() / int64(time.Millisecond)
wr.writeUint64(uint64(ms))
}
// marshalField is a field to be marshaled
type marshalField struct {
value interface{} // value to be marshaled, use pointers to avoid interface conversion overhead
omit bool // indicates that this field should be omitted (set to null)
}
// marshalComposite is a helper for us in a composite's marshal() function.
//
// The returned bytes include the composite header and fields. Fields with
// omit set to true will be encoded as null or omitted altogether if there are
// no non-null fields after them.
func marshalComposite(wr *buffer, code amqpType, fields []marshalField) error {
// lastSetIdx is the last index to have a non-omitted field.
// start at -1 as it's possible to have no fields in a composite
lastSetIdx := -1
// marshal each field into it's index in rawFields,
// null fields are skipped, leaving the index nil.
for i, f := range fields {
if f.omit {
continue
}
lastSetIdx = i
}
// write header only
if lastSetIdx == -1 {
wr.write([]byte{
0x0,
byte(typeCodeSmallUlong),
byte(code),
byte(typeCodeList0),
})
return nil
}
// write header
writeDescriptor(wr, code)
// write fields
wr.writeByte(byte(typeCodeList32))
// write temp size, replace later
sizeIdx := wr.len()
wr.write([]byte{0, 0, 0, 0})
preFieldLen := wr.len()
// field count
wr.writeUint32(uint32(lastSetIdx + 1))
// write null to each index up to lastSetIdx
for _, f := range fields[:lastSetIdx+1] {
if f.omit {
wr.writeByte(byte(typeCodeNull))
continue
}
err := marshal(wr, f.value)
if err != nil {
return err
}
}
// fix size
size := uint32(wr.len() - preFieldLen)
buf := wr.bytes()
binary.BigEndian.PutUint32(buf[sizeIdx:], size)
return nil
}
func writeDescriptor(wr *buffer, code amqpType) {
wr.write([]byte{
0x0,
byte(typeCodeSmallUlong),
byte(code),
})
}
func writeString(wr *buffer, str string) error {
if !utf8.ValidString(str) {
return errorNew("not a valid UTF-8 string")
}
l := len(str)
switch {
// Str8
case l < 256:
wr.write([]byte{
byte(typeCodeStr8),
byte(l),
})
wr.writeString(str)
return nil
// Str32
case uint(l) < math.MaxUint32:
wr.writeByte(byte(typeCodeStr32))
wr.writeUint32(uint32(l))
wr.writeString(str)
return nil
default:
return errorNew("too long")
}
}
func writeBinary(wr *buffer, bin []byte) error {
l := len(bin)
switch {
// List8
case l < 256:
wr.write([]byte{
byte(typeCodeVbin8),
byte(l),
})
wr.write(bin)
return nil
// List32
case uint(l) < math.MaxUint32:
wr.writeByte(byte(typeCodeVbin32))
wr.writeUint32(uint32(l))
wr.write(bin)
return nil
default:
return errorNew("too long")
}
}
func writeMap(wr *buffer, m interface{}) error {
startIdx := wr.len()
wr.write([]byte{
byte(typeCodeMap32), // type
0, 0, 0, 0, // size placeholder
0, 0, 0, 0, // length placeholder
})
var pairs int
switch m := m.(type) {
case map[interface{}]interface{}:
pairs = len(m) * 2
for key, val := range m {
err := marshal(wr, key)
if err != nil {
return err
}
err = marshal(wr, val)
if err != nil {
return err
}
}
case map[string]interface{}:
pairs = len(m) * 2
for key, val := range m {
err := writeString(wr, key)
if err != nil {
return err
}
err = marshal(wr, val)
if err != nil {
return err
}
}
case map[symbol]interface{}:
pairs = len(m) * 2
for key, val := range m {
err := key.marshal(wr)
if err != nil {
return err
}
err = marshal(wr, val)
if err != nil {
return err
}
}
case unsettled:
pairs = len(m) * 2
for key, val := range m {
err := writeString(wr, key)
if err != nil {
return err
}
err = marshal(wr, val)
if err != nil {
return err
}
}
case filter:
pairs = len(m) * 2
for key, val := range m {
err := key.marshal(wr)
if err != nil {
return err
}
err = val.marshal(wr)
if err != nil {
return err
}
}
case Annotations:
pairs = len(m) * 2
for key, val := range m {
switch key := key.(type) {
case string:
err := symbol(key).marshal(wr)
if err != nil {
return err
}
case symbol:
err := key.marshal(wr)
if err != nil {
return err
}
case int64:
writeInt64(wr, key)
case int:
writeInt64(wr, int64(key))
default:
return errorErrorf("unsupported Annotations key type %T", key)
}
err := marshal(wr, val)
if err != nil {
return err
}
}
default:
return errorErrorf("unsupported map type %T", m)
}
if uint(pairs) > math.MaxUint32-4 {
return errorNew("map contains too many elements")
}
// overwrite placeholder size and length
bytes := wr.bytes()[startIdx+1 : startIdx+9]
_ = bytes[7] // bounds check hint
length := wr.len() - startIdx - 1 - 4 // -1 for type, -4 for length
binary.BigEndian.PutUint32(bytes[:4], uint32(length))
binary.BigEndian.PutUint32(bytes[4:8], uint32(pairs))
return nil
}
// type length sizes
const (
array8TLSize = 2
array32TLSize = 5
)
func writeArrayHeader(wr *buffer, length, typeSize int, type_ amqpType) {
size := length * typeSize
// array type
if size+array8TLSize <= math.MaxUint8 {
wr.write([]byte{
byte(typeCodeArray8), // type
byte(size + array8TLSize), // size
byte(length), // length
byte(type_), // element type
})
} else {
wr.writeByte(byte(typeCodeArray32)) //type
wr.writeUint32(uint32(size + array32TLSize)) // size
wr.writeUint32(uint32(length)) // length
wr.writeByte(byte(type_)) // element type
}
}
func writeVariableArrayHeader(wr *buffer, length, elementsSizeTotal int, type_ amqpType) {
// 0xA_ == 1, 0xB_ == 4
// http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-types-v1.0-os.html#doc-idp82960
elementTypeSize := 1
if type_&0xf0 == 0xb0 {
elementTypeSize = 4
}
size := elementsSizeTotal + (length * elementTypeSize) // size excluding array length
if size+array8TLSize <= math.MaxUint8 {
wr.write([]byte{
byte(typeCodeArray8), // type
byte(size + array8TLSize), // size
byte(length), // length
byte(type_), // element type
})
} else {
wr.writeByte(byte(typeCodeArray32)) // type
wr.writeUint32(uint32(size + array32TLSize)) // size
wr.writeUint32(uint32(length)) // length
wr.writeByte(byte(type_)) // element type
}
}