-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.go
More file actions
667 lines (568 loc) · 17.8 KB
/
encoder.go
File metadata and controls
667 lines (568 loc) · 17.8 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package msgpack
// https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family
import (
"reflect"
"strings"
"unsafe"
"bytes"
"log"
"fmt"
"io"
)
type Marshaler interface {
MsgPackMarshaler() ([]byte, error)
}
type Encoder struct {
wtr io.Writer
}
// Convineince function to write out a byte onto a writer
func writeByte(wtr io.Writer, b byte) error {
if bwtr, ok := wtr.(io.ByteWriter); ok {
return bwtr.WriteByte(b)
}
_, err := wtr.Write([]byte{ b })
return err
}
// Function reverses a byte array (BE to LE and LE to BE convesion)
func reverseByte(b []byte) {
blen := len(b)
for i:=0; i<blen/2; i++ {
b[i], b[blen-i-1] = b[blen-i-1], b[i]
}
}
var errFixNumOutOfBounds = fmt.Errorf("Numerical value out of bounds for fixint")
// Function takes in an integer and translates the number to a fixnum format
// Fixnum format consists of an 8bit (1byte) integer with positive numbers:
// 0XXXXXXX where MSB is the control bit set to 0 and the next 7bits are the
// positive number
func encodeFixNumInt(wtr io.Writer, val int8) error {
// Integer signed
// For positive integers we can store 0XXXXXXX
// where 0 is postive int indicator and XXXXXXX is
// actual number
if val <= 0x7F && val >= 0 {
nval := byte(0x00 | (uint8(val) & 0x7F))
if bwtr, ok := wtr.(io.ByteWriter); ok {
bwtr.WriteByte(nval)
} else {
wtr.Write([]byte{ nval })
}
return nil
// Negative number representations are 111YYYYY
// Control bits are 111 and YYYYY is the value of
// the negative integer
// We convert the negative integer two's compliment
// to postive to ensure it fits in 5 bits (YYYYY)
} else if val < 0 && ((uint8(val) ^ 0xFF)+1) < 0x1F {
// Ones compliment the number
// 111b YYYYY (5 bit)
nval := 0xE0 | byte((uint8(val) ^ 0xFF)+1)
if bwtr, ok := wtr.(io.ByteWriter); ok {
bwtr.WriteByte(nval)
} else {
wtr.Write([]byte{ nval })
}
return nil
}
return errFixNumOutOfBounds
}
// Function encodes the integer based on size
func EncodeInt(wtr io.Writer, val int64, bsize int) error {
bsize /= 8
//Check if we do FixNum int encoding
if (val <= 0x7f && val >= 0) || (val < 0 && ((val ^ 0xff)+1) < 0x1f) {
return encodeFixNumInt(wtr, int8(val))
}
// All representations are in big endian format. Msgpack
// representation is sizeof(val) + 1 where the first byte
// is the control byte that specifies size of the value.
// 0xd0 - 1 byte value
// 0xd1 - 2 byte value
// 0xd2 - 4 byte value
// 0xd3 - 8 byte value
var ctlbyte byte
var bval []byte
if bsize == 1 {
ctlbyte = 0xd0
bval = (*[1]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 2 {
ctlbyte = 0xd1
bval = (*[2]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 4 {
ctlbyte = 0xd2
bval = (*[4]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 8 {
ctlbyte = 0xd3
bval = (*[8]byte)(unsafe.Pointer(&val))[:]
}
//Write byte
if err := writeByte(wtr, ctlbyte); err != nil {
return err
}
//TODO: Check endianess of machine, we are assuming we are running on a little endian arch
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
return nil
}
// Function encodes the unsigned integer based on size
func EncodeUint(wtr io.Writer, val uint64, bsize int) error {
bsize /= 8
//Check if we do FixNum int encoding
if val <= 0x7f && val >= 0 {
return encodeFixNumInt(wtr, int8(val))
}
// All representations are in big endian format. Msgpack
// representation is sizeof(val) + 1 where the first byte
// is the control byte that specifies size of the value.
// 0xcc - 1 byte value
// 0xcd - 2 byte value
// 0xce - 4 byte value
// 0xcf - 8 byte value
var ctlbyte byte
var bval []byte
if bsize == 1 {
ctlbyte = 0xcc
bval = (*[1]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 2 {
ctlbyte = 0xcd
bval = (*[2]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 4 {
ctlbyte = 0xce
bval = (*[4]byte)(unsafe.Pointer(&val))[:]
} else if bsize == 8 {
ctlbyte = 0xcf
bval = (*[8]byte)(unsafe.Pointer(&val))[:]
}
//Write byte
if err := writeByte(wtr, ctlbyte); err != nil {
return err
}
//TODO: Check endianess of machine, we are assuming we are running on a little endian arch
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
return nil
}
// Function encodes a boolean into msgpack
func EncodeBool(wtr io.Writer, val bool) error {
var bval byte
if val {
bval = 0xc3
} else {
bval = 0xc2
}
//Write out
if bwtr, ok := wtr.(io.ByteWriter); ok {
bwtr.WriteByte(bval)
} else {
wtr.Write([]byte{ bval })
}
return nil
}
// Function encodes the string into msgpack format
// The first few bytes are a control byte + length
// definition of the string. Strings are not NULL
// terminated.
// | 101XXXXX | data - [fixstr] For data that is <=31 bytes long
// | 0xd9 | YYYYYYYY | data - [str8] For data with lengths that are 255 or less
// | 0xda | ZZZZZZZZ | ZZZZZZZZ | data - [str16] For data with lengths that are 65535 or less
// | 0xdb | AAAAAAAA * 4 | data - [str32] For data with lengths that are 4294967295 or less
func EncodeString(wtr io.Writer, s string) error {
//Write control byte
l := len(s)
switch {
case l<=31:
var err error
lval := byte(0xa0 | l)
if bwtr, ok := wtr.(io.ByteWriter); ok {
err = bwtr.WriteByte(lval)
} else {
_, err = wtr.Write([]byte{ lval })
}
//Check if we errored out
if err != nil {
return err
}
case l <= 255:
//Control byte
if err := writeByte(wtr, 0xd9); err != nil {
return err
}
//Length of string
if err := writeByte(wtr, byte(l)); err != nil {
return err
}
case l <= 65535:
//Control byte
if err := writeByte(wtr, 0xda); err != nil {
return err
}
//Length of string
var bval []byte
tlen := uint16(l)
bval = (*[2]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
case l <= 4294967295:
//Control byte
if err := writeByte(wtr, 0xdb); err != nil {
return err
}
//Length of string 4 bytes
var bval []byte
tlen := uint32(l)
bval = (*[4]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
default:
panic("String larger than 4 gigs!")
}
//Write out the string
_, err := io.WriteString(wtr, s)
return err
}
// Function encodes a byte array to a binary msgpack type
// bin format has three control bytes we can use:
// | 0xc4 | XXXXXXXX | data - 255 length binary data
// | 0xc5 | XXXXXXXX * 2 | data - 65535 length binary data
// | 0xc6 | XXXXXXXX * 4 | data - 4294967295 length binary data
func EncodeBin(wtr io.Writer, b []byte) error {
l := len(b)
switch {
case l<=255:
//Control byte
if err := writeByte(wtr, 0xc4); err != nil {
return err
}
//Lenght of binary data 1 byte
if err := writeByte(wtr, byte(l)); err != nil {
return err
}
case l <= 65535:
//Control Byte
if err := writeByte(wtr, 0xc5); err != nil {
return err
}
//Length of binary data 2 bytes
var bval []byte
tlen := uint16(l)
bval = (*[2]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
case l <= 4294967295:
//Control byte
if err := writeByte(wtr, 0xc6); err != nil {
return err
}
//Length of string 4 bytes
var bval []byte
tlen := uint32(l)
bval = (*[4]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := wtr.Write(bval); err != nil {
return err
}
default:
panic("Binary larger than 4 gigs!")
}
//Write the binary data
_, err := wtr.Write(b)
return err
}
// Encode float 64
// Float64 is a 9 byte binary (1 byte control + 8 byte float)
// The data portion must be big endian format
// | 0xca | XXXXXXXX * 8 |
func EncodeFloat64(wtr io.Writer, f float64) error {
//Write control byte
if err := writeByte(wtr, 0xca); err != nil {
return err
}
//Convert float to byte order and reverse (LE to BE)
b := (*[unsafe.Sizeof(f)]byte)(unsafe.Pointer(&f))[:]
reverseByte(b)
if _, err := wtr.Write(b); err != nil {
return err
}
return nil
}
// Encode float 32
// Float64 is a 5 byte binary (1 byte control + 4 byte float)
// The data portion must be big endian format
// | 0xcb | XXXXXXXX * 4 |
func EncodeFloat32(wtr io.Writer, f float32) error {
//Write control byte
if err := writeByte(wtr, 0xca); err != nil {
return err
}
//Convert float to byte order and reverse (LE to BE)
b := (*[unsafe.Sizeof(f)]byte)(unsafe.Pointer(&f))[:]
reverseByte(b)
if _, err := wtr.Write(b); err != nil {
return err
}
return nil
}
// Function encodes the nil value
func EncodeNil(wtr io.Writer) error {
return writeByte(wtr, 0xc0)
}
/* Start Encoder **/
// Function creates a new encoder
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{ wtr: w }
}
// Function encodes an array into the writer
// msgpack defines three array encoding types
// | 1001XXXX | data - [fixarray] up to 15 elements
// | 0xdc | YYYYYYYY * 2 | data - [array16] stores up to 65535 elements
// | 0xdd | ZZZZZZZZ * 4 | data - [array32] stores up to 4294967295 elements
func (e *Encoder) encodeArray(typ reflect.Type, val reflect.Value) error {
l := val.Len()
switch {
case l <= 15:
//Control byte + len
if err := writeByte(e.wtr, byte(0x90 | l)); err != nil {
return err
}
case l <= 65535:
//Control byte
if err := writeByte(e.wtr, 0xdc); err != nil {
return err
}
//Big Endian the length
var bval []byte
tlen := uint16(l)
bval = (*[2]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := e.wtr.Write(bval); err != nil {
return err
}
case l <= 4294967295:
//Control byte
if err := writeByte(e.wtr, 0xdd); err != nil {
return err
}
//Big Endian the length
var bval []byte
tlen := uint32(l)
bval = (*[4]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := e.wtr.Write(bval); err != nil {
return err
}
default:
panic("Array size larger than 4294967295!")
}
//Actual data
for i:=0; i<l; i++ {
ed := val.Index(i).Interface()
if err := e.Encode(ed); err != nil {
return err
}
}
return nil
}
// Function encodes the header for a Map (hash)
func (e *Encoder) encodeMapHeader(l int) error {
switch {
case l <= 15:
//Control byte + len
if err := writeByte(e.wtr, byte(0x80 | l)); err != nil {
return err
}
case l <= 65535:
//Control byte
if err := writeByte(e.wtr, 0xde); err != nil {
return err
}
//Big Endian the length
var bval []byte
tlen := uint16(l)
bval = (*[2]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := e.wtr.Write(bval); err != nil {
return err
}
case l <= 4294967295:
//Control byte
if err := writeByte(e.wtr, 0xdf); err != nil {
return err
}
//Big Endian the length
var bval []byte
tlen := uint32(l)
bval = (*[4]byte)(unsafe.Pointer(&tlen))[:]
reverseByte(bval)
if _, err := e.wtr.Write(bval); err != nil {
return err
}
default:
panic("Array size larger than 4294967295!")
}
return nil
}
// Function encodes the map entity
func (e *Encoder) encodeMapEntity(key interface{}, val interface{}) error {
//Encode key
if err := e.Encode(key); err != nil {
return err
}
//Encode value
if err := e.Encode(val); err != nil {
return err
}
return nil
}
// Function encodes a map into the writer
// msgpack defines three map encoding types
// | 1000XXXX | data - [fixmap] up to 15 elements
// | 0xde | YYYYYYYY * 2 | data - [map16] up to 65535 elements
// | 0xdf | YYYYYYYY * 4 | data - [map32] up to 4294967295 elements
func (e *Encoder) encodeMap(typ reflect.Type, val reflect.Value) error {
l := val.Len()
if err := e.encodeMapHeader(l); err != nil {
return err
}
//Actual data
keys := val.MapKeys()
for i:=0; i<len(keys); i++ {
if err := e.encodeMapEntity(keys[i].Interface(), val.MapIndex(keys[i]).Interface()); err != nil {
return err
}
}
return nil
}
// Function encodes a structs public entities.
// msgpack uses the "msgpack" tag for renaming
// of keys
// Structs are just maps:
// | 1000XXXX | data - [fixmap] up to 15 elements
// | 0xde | YYYYYYYY * 2 | data - [map16] up to 65535 elements
// | 0xdf | YYYYYYYY * 4 | data - [map32] up to 4294967295 elements
func (e *Encoder) encodeStruct(t reflect.Type, v reflect.Value) error {
slen := v.NumField()
if err := e.encodeMapHeader(slen); err != nil {
return err
}
//Go through the struct
for i:=0; i<slen; i++ {
var tname *string
stval := t.Field(i)
//Get any msgpack tags
if tval, ok := stval.Tag.Lookup("msgpack"); ok {
if stmp, omit := parseMsgPackTag(tval); omit && stmp == "" {
continue
} else if stmp != "" {
tname = &stmp
}
}
//Struct type name
if tname == nil {
tname = &stval.Name
}
//Field value
if err := e.encodeMapEntity(*tname, v.Field(i).Interface()); err != nil {
return err
}
}
return nil
}
// Function parses out the struct tag contents.
// First return is the value name of the tag.
// Second return is if we omit if empty (omitempty)
func parseMsgPackTag(t string) (fieldname string, omit bool) {
sp := strings.Split(t, ",")
if len(sp) >= 1 {
fieldname = strings.TrimSpace(sp[0])
}
if len(sp) >= 2 && strings.TrimSpace(sp[1]) == "omitempty" {
omit = true
}
return
}
// Function encodes the interface
func (e *Encoder) Encode(v interface{}) error {
//Check for base type encoding
switch val := v.(type) {
//Integer
case int64:
return EncodeInt(e.wtr, val, 64)
case int32:
return EncodeInt(e.wtr, int64(val), 32)
case int16:
return EncodeInt(e.wtr, int64(val), 16)
case int8:
return EncodeInt(e.wtr, int64(val), 8)
case int:
return EncodeInt(e.wtr, int64(val), int(reflect.TypeOf(v).Size())*8)
//Unsigned integer
case uint64:
return EncodeUint(e.wtr, val, 64)
case uint32:
return EncodeUint(e.wtr, uint64(val), 32)
case uint16:
return EncodeUint(e.wtr, uint64(val), 16)
case uint8:
return EncodeUint(e.wtr, uint64(val), 8)
case uint:
return EncodeUint(e.wtr, uint64(val), int(reflect.TypeOf(v).Size())*8)
//Float
case float64:
return EncodeFloat64(e.wtr, float64(val))
case float32:
return EncodeFloat32(e.wtr, float32(val))
//Boolean
case bool:
return EncodeBool(e.wtr, val)
//String
case string:
return EncodeString(e.wtr, val)
//Byte (binary)
case []byte:
return EncodeBin(e.wtr, val)
//Nil case
case nil:
return EncodeNil(e.wtr)
}
//More complicated type discovery
typ := reflect.TypeOf(v)
switch typ.Kind() {
//Array/slice type
case reflect.Slice:
fallthrough
case reflect.Array:
return e.encodeArray(typ, reflect.ValueOf(v))
case reflect.Map:
return e.encodeMap(typ, reflect.ValueOf(v))
case reflect.Struct:
return e.encodeStruct(typ, reflect.ValueOf(v))
case reflect.Ptr:
vptr := reflect.ValueOf(v)
if vptr.IsNil() {
return e.Encode(nil)
}
return e.Encode(reflect.Indirect(vptr).Interface())
}
//TODO: Encode ext
log.Panicf("Unhandled type %T", v)
return nil
}
// Marshal function
func Marshal(v interface{}) ([]byte, error) {
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}