1
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
2
// Use of this source code is governed by a MIT license found in the LICENSE file.
14
// - symbols are stored in a symbol map during encoding and decoding.
15
// - the symbols persist until the (En|De)coder ResetXXX method is called.
17
const bincDoPrune = true
19
// vd as low 4 bits (there are 16 slots)
21
bincVdSpecial byte = iota
33
_ // bincVdUnicodeOther
39
bincVdCustomExt = 0x0f
55
_ byte = iota // bincFlBin16
60
// others not currently supported
63
const bincBdNil = 0 // bincVdSpecial<<4 | bincSpNil // staticcheck barfs on this (SA4016)
66
bincdescSpecialVsNames = map[byte]string{
71
bincSpPosInf: "float",
72
bincSpNegInf: "float",
73
bincSpZeroFloat: "float",
77
bincdescVdNames = map[byte]string{
78
bincVdSpecial: "special",
79
bincVdSmallInt: "uint",
82
bincVdSymbol: "string",
83
bincVdString: "string",
84
bincVdByteArray: "bytes",
85
bincVdTimestamp: "time",
86
bincVdCustomExt: "ext",
92
func bincdescbd(bd byte) (s string) {
93
return bincdesc(bd>>4, bd&0x0f)
96
func bincdesc(vd, vs byte) (s string) {
97
if vd == bincVdSpecial {
98
s = bincdescSpecialVsNames[vs]
100
s = bincdescVdNames[vd]
108
type bincEncState struct {
109
m map[string]uint16 // symbols
112
func (e bincEncState) captureState() interface{} { return e.m }
113
func (e *bincEncState) resetState() { e.m = nil }
114
func (e *bincEncState) reset() { e.resetState() }
115
func (e *bincEncState) restoreState(v interface{}) { e.m = v.(map[string]uint16) }
117
type bincEncDriver struct {
119
encDriverNoopContainerWriter
126
func (e *bincEncDriver) encoder() *Encoder {
130
func (e *bincEncDriver) EncodeNil() {
131
e.e.encWr.writen1(bincBdNil)
134
func (e *bincEncDriver) EncodeTime(t time.Time) {
138
bs := bincEncodeTime(t)
139
e.e.encWr.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
144
func (e *bincEncDriver) EncodeBool(b bool) {
146
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpTrue)
148
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpFalse)
152
func (e *bincEncDriver) encSpFloat(f float64) (done bool) {
154
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
155
} else if math.IsNaN(float64(f)) {
156
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNan)
157
} else if math.IsInf(float64(f), +1) {
158
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpPosInf)
159
} else if math.IsInf(float64(f), -1) {
160
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNegInf)
167
func (e *bincEncDriver) EncodeFloat32(f float32) {
168
if !e.encSpFloat(float64(f)) {
169
e.e.encWr.writen1(bincVdFloat<<4 | bincFlBin32)
170
bigen.writeUint32(e.e.w(), math.Float32bits(f))
174
func (e *bincEncDriver) EncodeFloat64(f float64) {
178
b := bigen.PutUint64(math.Float64bits(f))
181
for ; i >= 0 && (b[i] == 0); i-- {
185
e.e.encWr.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
186
e.e.encWr.writen1(byte(i))
187
e.e.encWr.writeb(b[:i])
191
e.e.encWr.writen1(bincVdFloat<<4 | bincFlBin64)
195
func (e *bincEncDriver) encIntegerPrune32(bd byte, pos bool, v uint64) {
196
b := bigen.PutUint32(uint32(v))
198
i := byte(pruneSignExt(b[:], pos))
199
e.e.encWr.writen1(bd | 3 - i)
200
e.e.encWr.writeb(b[i:])
202
e.e.encWr.writen1(bd | 3)
207
func (e *bincEncDriver) encIntegerPrune64(bd byte, pos bool, v uint64) {
208
b := bigen.PutUint64(v)
210
i := byte(pruneSignExt(b[:], pos))
211
e.e.encWr.writen1(bd | 7 - i)
212
e.e.encWr.writeb(b[i:])
214
e.e.encWr.writen1(bd | 7)
219
func (e *bincEncDriver) EncodeInt(v int64) {
221
e.encUint(bincVdPosInt<<4, true, uint64(v))
223
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpNegOne)
225
e.encUint(bincVdNegInt<<4, false, uint64(-v))
229
func (e *bincEncDriver) EncodeUint(v uint64) {
230
e.encUint(bincVdPosInt<<4, true, v)
233
func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) {
235
e.e.encWr.writen1(bincVdSpecial<<4 | bincSpZero)
236
} else if pos && v >= 1 && v <= 16 {
237
e.e.encWr.writen1(bincVdSmallInt<<4 | byte(v-1))
238
} else if v <= math.MaxUint8 {
239
e.e.encWr.writen2(bd|0x0, byte(v))
240
} else if v <= math.MaxUint16 {
241
e.e.encWr.writen1(bd | 0x01)
242
bigen.writeUint16(e.e.w(), uint16(v))
243
} else if v <= math.MaxUint32 {
244
e.encIntegerPrune32(bd, pos, v)
246
e.encIntegerPrune64(bd, pos, v)
250
func (e *bincEncDriver) EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
253
bs0 = e.e.blist.get(1024)
255
e.e.sideEncode(v, basetype, &bs)
263
e.encodeExtPreamble(uint8(xtag), len(bs))
268
if !byteSliceSameData(bs0, bs) {
274
func (e *bincEncDriver) EncodeRawExt(re *RawExt) {
275
e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
276
e.e.encWr.writeb(re.Data)
279
func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
280
e.encLen(bincVdCustomExt<<4, uint64(length))
281
e.e.encWr.writen1(xtag)
284
func (e *bincEncDriver) WriteArrayStart(length int) {
285
e.encLen(bincVdArray<<4, uint64(length))
288
func (e *bincEncDriver) WriteMapStart(length int) {
289
e.encLen(bincVdMap<<4, uint64(length))
292
func (e *bincEncDriver) EncodeSymbol(v string) {
293
//symbols only offer benefit when string length > 1.
294
//This is because strings with length 1 take only 2 bytes to store
295
//(bd with embedded length, and single byte for string val).
299
e.encBytesLen(cUTF8, 0)
302
e.encBytesLen(cUTF8, 1)
303
e.e.encWr.writen1(v[0])
307
e.m = make(map[string]uint16, 16)
311
if ui <= math.MaxUint8 {
312
e.e.encWr.writen2(bincVdSymbol<<4, byte(ui))
314
e.e.encWr.writen1(bincVdSymbol<<4 | 0x8)
315
bigen.writeUint16(e.e.w(), ui)
322
if l <= math.MaxUint8 {
324
} else if l <= math.MaxUint16 {
326
} else if int64(l) <= math.MaxUint32 {
331
if ui <= math.MaxUint8 {
332
e.e.encWr.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
334
e.e.encWr.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
335
bigen.writeUint16(e.e.w(), ui)
338
e.e.encWr.writen1(byte(l))
339
} else if lenprec == 1 {
340
bigen.writeUint16(e.e.w(), uint16(l))
341
} else if lenprec == 2 {
342
bigen.writeUint32(e.e.w(), uint32(l))
344
bigen.writeUint64(e.e.w(), uint64(l))
346
e.e.encWr.writestr(v)
350
func (e *bincEncDriver) EncodeString(v string) {
352
e.encLen(bincVdByteArray<<4, uint64(len(v)))
354
e.e.encWr.writestr(v)
358
e.EncodeStringEnc(cUTF8, v)
361
func (e *bincEncDriver) EncodeStringEnc(c charEncoding, v string) {
362
if e.e.c == containerMapKey && c == cUTF8 && (e.h.AsSymbols == 1) {
366
e.encLen(bincVdString<<4, uint64(len(v)))
368
e.e.encWr.writestr(v)
372
func (e *bincEncDriver) EncodeStringBytesRaw(v []byte) {
377
e.encLen(bincVdByteArray<<4, uint64(len(v)))
383
func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
384
// MARKER: we currently only support UTF-8 (string) and RAW (bytearray).
385
// We should consider supporting bincUnicodeOther.
388
e.encLen(bincVdByteArray<<4, length)
390
e.encLen(bincVdString<<4, length)
394
func (e *bincEncDriver) encLen(bd byte, l uint64) {
396
e.e.encWr.writen1(bd | uint8(l+4))
398
e.encLenNumber(bd, l)
402
func (e *bincEncDriver) encLenNumber(bd byte, v uint64) {
403
if v <= math.MaxUint8 {
404
e.e.encWr.writen2(bd, byte(v))
405
} else if v <= math.MaxUint16 {
406
e.e.encWr.writen1(bd | 0x01)
407
bigen.writeUint16(e.e.w(), uint16(v))
408
} else if v <= math.MaxUint32 {
409
e.e.encWr.writen1(bd | 0x02)
410
bigen.writeUint32(e.e.w(), uint32(v))
412
e.e.encWr.writen1(bd | 0x03)
413
bigen.writeUint64(e.e.w(), uint64(v))
417
//------------------------------------
419
type bincDecState struct {
426
// MARKER: consider using binary search here instead of a map (ie bincDecSymbol)
430
func (x bincDecState) captureState() interface{} { return x }
431
func (x *bincDecState) resetState() { *x = bincDecState{} }
432
func (x *bincDecState) reset() { x.resetState() }
433
func (x *bincDecState) restoreState(v interface{}) { *x = v.(bincDecState) }
435
type bincDecDriver struct {
436
decDriverNoopContainerReader
437
decDriverNoopNumberHelper
446
func (d *bincDecDriver) decoder() *Decoder {
450
func (d *bincDecDriver) descBd() string {
451
return sprintf("%v (%s)", d.bd, bincdescbd(d.bd))
454
func (d *bincDecDriver) readNextBd() {
455
d.bd = d.d.decRd.readn1()
461
func (d *bincDecDriver) advanceNil() (null bool) {
465
if d.bd == bincBdNil {
467
return true // null = true
472
func (d *bincDecDriver) TryNil() bool {
473
return d.advanceNil()
476
func (d *bincDecDriver) ContainerType() (vt valueType) {
480
if d.bd == bincBdNil {
483
} else if d.vd == bincVdByteArray {
484
return valueTypeBytes
485
} else if d.vd == bincVdString {
486
return valueTypeString
487
} else if d.vd == bincVdArray {
488
return valueTypeArray
489
} else if d.vd == bincVdMap {
492
return valueTypeUnset
495
func (d *bincDecDriver) DecodeTime() (t time.Time) {
499
if d.vd != bincVdTimestamp {
500
d.d.errorf("cannot decode time - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
502
t, err := bincDecodeTime(d.d.decRd.readx(uint(d.vs)))
508
func (d *bincDecDriver) decFloatPruned(maxlen uint8) {
509
l := d.d.decRd.readn1()
511
d.d.errorf("cannot read float - at most %v bytes used to represent float - received %v bytes", maxlen, l)
513
for i := l; i < maxlen; i++ {
516
d.d.decRd.readb(d.d.b[0:l])
519
func (d *bincDecDriver) decFloatPre32() (b [4]byte) {
521
b = d.d.decRd.readn4()
529
func (d *bincDecDriver) decFloatPre64() (b [8]byte) {
531
b = d.d.decRd.readn8()
539
func (d *bincDecDriver) decFloatVal() (f float64) {
542
f = float64(math.Float32frombits(bigen.Uint32(d.decFloatPre32())))
544
f = math.Float64frombits(bigen.Uint64(d.decFloatPre64()))
547
d.d.errorf("read float supports only float32/64 - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
552
func (d *bincDecDriver) decUint() (v uint64) {
555
v = uint64(d.d.decRd.readn1())
557
v = uint64(bigen.Uint16(d.d.decRd.readn2()))
559
b3 := d.d.decRd.readn3()
562
v = uint64(bigen.Uint32(b))
564
v = uint64(bigen.Uint32(d.d.decRd.readn4()))
573
v = bigen.Uint64(d.d.decRd.readn8())
575
d.d.errorf("unsigned integers with greater than 64 bits of precision not supported: d.vs: %v %x", d.vs, d.vs)
580
func (d *bincDecDriver) uintBytes() (bs []byte) {
584
bs[0] = d.d.decRd.readn1()
602
d.d.errorf("unsigned integers with greater than 64 bits of precision not supported: d.vs: %v %x", d.vs, d.vs)
607
func (d *bincDecDriver) decInteger() (ui uint64, neg, ok bool) {
610
if vd == bincVdPosInt {
612
} else if vd == bincVdNegInt {
615
} else if vd == bincVdSmallInt {
616
ui = uint64(d.vs) + 1
617
} else if vd == bincVdSpecial {
618
if vs == bincSpZero {
620
} else if vs == bincSpNegOne {
625
// d.d.errorf("integer decode has invalid special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs))
629
// d.d.errorf("integer can only be decoded from int/uint. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd)
634
func (d *bincDecDriver) decFloat() (f float64, ok bool) {
637
if vd == bincVdSpecial {
640
} else if vs == bincSpPosInf {
642
} else if vs == bincSpZeroFloat || vs == bincSpZero {
644
} else if vs == bincSpNegInf {
648
// d.d.errorf("float - invalid special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs))
650
} else if vd == bincVdFloat {
658
func (d *bincDecDriver) DecodeInt64() (i int64) {
662
i = decNegintPosintFloatNumberHelper{&d.d}.int64(d.decInteger())
667
func (d *bincDecDriver) DecodeUint64() (ui uint64) {
671
ui = decNegintPosintFloatNumberHelper{&d.d}.uint64(d.decInteger())
676
func (d *bincDecDriver) DecodeFloat64() (f float64) {
680
f = decNegintPosintFloatNumberHelper{&d.d}.float64(d.decFloat())
685
func (d *bincDecDriver) DecodeBool() (b bool) {
689
if d.bd == (bincVdSpecial | bincSpFalse) {
691
} else if d.bd == (bincVdSpecial | bincSpTrue) {
694
d.d.errorf("bool - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
700
func (d *bincDecDriver) ReadMapStart() (length int) {
702
return containerLenNil
704
if d.vd != bincVdMap {
705
d.d.errorf("map - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
712
func (d *bincDecDriver) ReadArrayStart() (length int) {
714
return containerLenNil
716
if d.vd != bincVdArray {
717
d.d.errorf("array - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
724
func (d *bincDecDriver) decLen() int {
728
return int(d.decLenNumber())
731
func (d *bincDecDriver) decLenNumber() (v uint64) {
732
if x := d.vs; x == 0 {
733
v = uint64(d.d.decRd.readn1())
735
v = uint64(bigen.Uint16(d.d.decRd.readn2()))
737
v = uint64(bigen.Uint32(d.d.decRd.readn4()))
739
v = bigen.Uint64(d.d.decRd.readn8())
744
// func (d *bincDecDriver) decStringBytes(bs []byte, zerocopy bool) (bs2 []byte) {
745
func (d *bincDecDriver) DecodeStringAsBytes() (bs2 []byte) {
746
d.d.decByteState = decByteStateNone
752
case bincVdString, bincVdByteArray:
755
d.d.decByteState = decByteStateZerocopy
756
bs2 = d.d.decRd.rb.readx(uint(slen))
758
d.d.decByteState = decByteStateReuseBuf
759
bs2 = decByteSlice(d.d.r(), slen, d.d.h.MaxInitLen, d.d.b[:])
762
// zerocopy doesn't apply for symbols,
763
// as the values must be stored in a table for later use.
767
symbol = uint16(d.d.decRd.readn1())
769
symbol = uint16(bigen.Uint16(d.d.decRd.readn2()))
772
d.s = make(map[uint16][]byte, 16)
780
slen = int(d.d.decRd.readn1())
782
slen = int(bigen.Uint16(d.d.decRd.readn2()))
784
slen = int(bigen.Uint32(d.d.decRd.readn4()))
786
slen = int(bigen.Uint64(d.d.decRd.readn8()))
788
// As we are using symbols, do not store any part of
789
// the parameter bs in the map, as it might be a shared buffer.
790
bs2 = decByteSlice(d.d.r(), slen, d.d.h.MaxInitLen, nil)
794
d.d.errorf("string/bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
797
if d.h.ValidateUnicode && !utf8.Valid(bs2) {
798
d.d.errorf("DecodeStringAsBytes: invalid UTF-8: %s", bs2)
805
func (d *bincDecDriver) DecodeBytes(bs []byte) (bsOut []byte) {
806
d.d.decByteState = decByteStateNone
810
if d.vd == bincVdArray {
813
d.d.decByteState = decByteStateReuseBuf
815
slen := d.ReadArrayStart()
817
if bs, changed = usableByteSlice(bs, slen); changed {
818
d.d.decByteState = decByteStateNone
820
for i := 0; i < slen; i++ {
821
bs[i] = uint8(chkOvf.UintV(d.DecodeUint64(), 8))
823
for i := len(bs); i < slen; i++ {
824
bs = append(bs, uint8(chkOvf.UintV(d.DecodeUint64(), 8)))
829
if d.vd == bincVdString || d.vd == bincVdByteArray {
832
d.d.errorf("bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
836
d.d.decByteState = decByteStateZerocopy
837
return d.d.decRd.rb.readx(uint(clen))
841
d.d.decByteState = decByteStateReuseBuf
843
return decByteSlice(d.d.r(), clen, d.d.h.MaxInitLen, bs)
846
func (d *bincDecDriver) DecodeExt(rv interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
848
d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
853
xbs, realxtag1, zerocopy := d.decodeExtV(ext != nil, uint8(xtag))
854
realxtag := uint64(realxtag1)
858
re.setData(xbs, zerocopy)
859
} else if ext == SelfExt {
860
d.d.sideDecode(rv, basetype, xbs)
866
func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xbs []byte, xtag byte, zerocopy bool) {
867
if d.vd == bincVdCustomExt {
869
xtag = d.d.decRd.readn1()
870
if verifyTag && xtag != tag {
871
d.d.errorf("wrong extension tag - got %b, expecting: %v", xtag, tag)
874
xbs = d.d.decRd.rb.readx(uint(l))
877
xbs = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:])
879
} else if d.vd == bincVdByteArray {
880
xbs = d.DecodeBytes(nil)
882
d.d.errorf("ext expects extensions or byte array - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
888
func (d *bincDecDriver) DecodeNaked() {
894
var decodeFurther bool
916
case bincSpZeroFloat:
921
n.u = uint64(0) // int8(0)
924
n.i = int64(-1) // int8(-1)
926
d.d.errorf("cannot infer value - unrecognized special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs))
930
n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
936
n.i = -(int64(d.decUint()))
939
n.f = d.decFloatVal()
941
n.v = valueTypeString
942
n.s = d.d.stringZC(d.DecodeStringAsBytes())
943
case bincVdByteArray:
944
d.d.fauxUnionReadRawBytes(false)
946
n.v = valueTypeSymbol
947
n.s = d.d.stringZC(d.DecodeStringAsBytes())
948
case bincVdTimestamp:
950
tt, err := bincDecodeTime(d.d.decRd.readx(uint(d.vs)))
953
case bincVdCustomExt:
956
n.u = uint64(d.d.decRd.readn1())
958
n.l = d.d.decRd.rb.readx(uint(l))
960
n.l = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:])
969
d.d.errorf("cannot infer value - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
975
if n.v == valueTypeUint && d.h.SignedInteger {
981
func (d *bincDecDriver) nextValueBytes(v0 []byte) (v []byte) {
986
var h = decNextValueBytesHelper{d: &d.d}
987
var cursor = d.d.rb.c - 1
989
v = d.nextValueBytesBdReadR(v)
991
h.bytesRdV(&v, cursor)
995
func (d *bincDecDriver) nextValueBytesR(v0 []byte) (v []byte) {
998
var h = decNextValueBytesHelper{d: &d.d}
1000
return d.nextValueBytesBdReadR(v)
1003
func (d *bincDecDriver) nextValueBytesBdReadR(v0 []byte) (v []byte) {
1005
var h = decNextValueBytesHelper{d: &d.d}
1007
fnLen := func(vs byte) uint {
1010
x := d.d.decRd.readn1()
1014
x := d.d.decRd.readn2()
1015
h.appendN(&v, x[:]...)
1016
return uint(bigen.Uint16(x))
1018
x := d.d.decRd.readn4()
1019
h.appendN(&v, x[:]...)
1020
return uint(bigen.Uint32(x))
1022
x := d.d.decRd.readn8()
1023
h.appendN(&v, x[:]...)
1024
return uint(bigen.Uint64(x))
1035
case bincSpNil, bincSpFalse, bincSpTrue, bincSpNan, bincSpPosInf: // pass
1036
case bincSpNegInf, bincSpZeroFloat, bincSpZero, bincSpNegOne: // pass
1038
d.d.errorf("cannot infer value - unrecognized special value %x-%x/%s", d.vd, d.vs, bincdesc(d.vd, d.vs))
1040
case bincVdSmallInt: // pass
1041
case bincVdPosInt, bincVdNegInt:
1043
h.appendN(&v, bs...)
1045
fn := func(xlen byte) {
1047
xlen = d.d.decRd.readn1()
1050
d.d.errorf("cannot read float - at most 8 bytes used to represent float - received %v bytes", xlen)
1053
d.d.decRd.readb(d.d.b[:xlen])
1054
h.appendN(&v, d.d.b[:xlen]...)
1062
d.d.errorf("read float supports only float32/64 - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
1064
case bincVdString, bincVdByteArray:
1066
h.appendN(&v, d.d.decRd.readx(clen)...)
1069
h.append1(&v, d.d.decRd.readn1())
1071
h.appendN(&v, d.d.decRd.rb.readx(2)...)
1074
clen = fnLen(d.vs & 0x3)
1075
h.appendN(&v, d.d.decRd.readx(clen)...)
1077
case bincVdTimestamp:
1078
h.appendN(&v, d.d.decRd.readx(uint(d.vs))...)
1079
case bincVdCustomExt:
1081
h.append1(&v, d.d.decRd.readn1()) // tag
1082
h.appendN(&v, d.d.decRd.readx(clen)...)
1085
for i := uint(0); i < clen; i++ {
1086
v = d.nextValueBytesR(v)
1090
for i := uint(0); i < clen; i++ {
1091
v = d.nextValueBytesR(v)
1092
v = d.nextValueBytesR(v)
1095
d.d.errorf("cannot infer value - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
1100
//------------------------------------
1102
// BincHandle is a Handle for the Binc Schema-Free Encoding Format
1103
// defined at https://github.com/ugorji/binc .
1105
// BincHandle currently supports all Binc features with the following EXCEPTIONS:
1106
// - only integers up to 64 bits of precision are supported.
1107
// big integers are unsupported.
1108
// - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
1109
// extended precision and decimal IEEE 754 floats are unsupported.
1110
// - Only UTF-8 strings supported.
1111
// Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
1113
// Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
1114
type BincHandle struct {
1119
// AsSymbols defines what should be encoded as symbols.
1121
// Encoding as symbols can reduce the encoded size significantly.
1123
// However, during decoding, each string to be encoded as a symbol must
1124
// be checked to see if it has been seen before. Consequently, encoding time
1125
// will increase if using symbols, because string comparisons has a clear cost.
1128
// - 0: default: library uses best judgement
1130
// - 2: do not use symbols
1133
// AsSymbols: may later on introduce more options ...
1135
// - s: struct fields
1137
// - a: all: same as m, s, ...
1139
// _ [7]uint64 // padding (cache-aligned)
1142
// Name returns the name of the handle: binc
1143
func (h *BincHandle) Name() string { return "binc" }
1145
func (h *BincHandle) desc(bd byte) string { return bincdesc(bd>>4, bd&0x0f) }
1147
func (h *BincHandle) newEncDriver() encDriver {
1148
var e = &bincEncDriver{h: h}
1155
func (h *BincHandle) newDecDriver() decDriver {
1156
d := &bincDecDriver{h: h}
1163
// var timeDigits = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
1165
// EncodeTime encodes a time.Time as a []byte, including
1166
// information on the instant in time and UTC offset.
1168
// Format Description
1170
// A timestamp is composed of 3 components:
1172
// - secs: signed integer representing seconds since unix epoch
1173
// - nsces: unsigned integer representing fractional seconds as a
1174
// nanosecond offset within secs, in the range 0 <= nsecs < 1e9
1175
// - tz: signed integer representing timezone offset in minutes east of UTC,
1176
// and a dst (daylight savings time) flag
1178
// When encoding a timestamp, the first byte is the descriptor, which
1179
// defines which components are encoded and how many bytes are used to
1180
// encode secs and nsecs components. *If secs/nsecs is 0 or tz is UTC, it
1181
// is not encoded in the byte array explicitly*.
1183
// Descriptor 8 bits are of the form `A B C DDD EE`:
1184
// A: Is secs component encoded? 1 = true
1185
// B: Is nsecs component encoded? 1 = true
1186
// C: Is tz component encoded? 1 = true
1187
// DDD: Number of extra bytes for secs (range 0-7).
1188
// If A = 1, secs encoded in DDD+1 bytes.
1189
// If A = 0, secs is not encoded, and is assumed to be 0.
1190
// If A = 1, then we need at least 1 byte to encode secs.
1191
// DDD says the number of extra bytes beyond that 1.
1192
// E.g. if DDD=0, then secs is represented in 1 byte.
1193
// if DDD=2, then secs is represented in 3 bytes.
1194
// EE: Number of extra bytes for nsecs (range 0-3).
1195
// If B = 1, nsecs encoded in EE+1 bytes (similar to secs/DDD above)
1197
// Following the descriptor bytes, subsequent bytes are:
1199
// secs component encoded in `DDD + 1` bytes (if A == 1)
1200
// nsecs component encoded in `EE + 1` bytes (if B == 1)
1201
// tz component encoded in 2 bytes (if C == 1)
1203
// secs and nsecs components are integers encoded in a BigEndian
1204
// 2-complement encoding format.
1206
// tz component is encoded as 2 bytes (16 bits). Most significant bit 15 to
1207
// Least significant bit 0 are described below:
1209
// Timezone offset has a range of -12:00 to +14:00 (ie -720 to +840 minutes).
1210
// Bit 15 = have\_dst: set to 1 if we set the dst flag.
1211
// Bit 14 = dst\_on: set to 1 if dst is in effect at the time, or 0 if not.
1212
// Bits 13..0 = timezone offset in minutes. It is a signed integer in Big Endian format.
1213
func bincEncodeTime(t time.Time) []byte {
1214
// t := rv2i(rv).(time.Time)
1215
tsecs, tnsecs := t.Unix(), t.Nanosecond()
1227
btmp := bigen.PutUint64(uint64(tsecs))
1228
f := pruneSignExt(btmp[:], tsecs >= 0)
1229
bd = bd | (byte(7-f) << 2)
1230
copy(bs[i:], btmp[f:])
1235
btmp := bigen.PutUint32(uint32(tnsecs))
1236
f := pruneSignExt(btmp[:4], true)
1238
copy(bs[i:], btmp[f:4])
1243
// Note that Go Libs do not give access to dst flag.
1244
_, zoneOffset := t.Zone()
1245
// zoneName, zoneOffset := t.Zone()
1247
z := uint16(zoneOffset)
1248
btmp := bigen.PutUint16(z)
1250
bs[i] = btmp[0] & 0x3f
1258
// bincDecodeTime decodes a []byte into a time.Time.
1259
func bincDecodeTime(bs []byte) (tt time.Time, err error) {
1271
n = ((bd >> 2) & 0x7) + 1
1273
copy(btmp[8-n:], bs[i:i2])
1274
// if first bit of bs[i] is set, then fill btmp[0..8-n] with 0xff (ie sign extend it)
1275
if bs[i]&(1<<7) != 0 {
1276
copy(btmp[0:8-n], bsAll0xff)
1279
tsec = int64(bigen.Uint64(btmp))
1285
copy(btmp[4-n:], bs[i:i2])
1287
tnsec = bigen.Uint32(btmp)
1290
tt = time.Unix(tsec, int64(tnsec)).UTC()
1293
// In stdlib time.Parse, when a date is parsed without a zone name, it uses "" as zone name.
1294
// However, we need name here, so it can be shown when time is printf.d.
1295
// Zone name is in form: UTC-08:00.
1296
// Note that Go Libs do not give access to dst flag, so we ignore dst bits
1298
tz = bigen.Uint16([2]byte{bs[i], bs[i+1]})
1299
// sign extend sign bit into top 2 MSB (which were dst bits):
1300
if tz&(1<<13) == 0 { // positive
1301
tz = tz & 0x3fff //clear 2 MSBs: dst bits
1302
} else { // negative
1303
tz = tz | 0xc000 //set 2 MSBs: dst bits
1307
tt = time.Unix(tsec, int64(tnsec)).UTC()
1309
// For Go Time, do not use a descriptive timezone.
1310
// It's unnecessary, and makes it harder to do a reflect.DeepEqual.
1311
// The Offset already tells what the offset should be, if not on UTC and unknown zone name.
1312
// var zoneName = timeLocUTCName(tzint)
1313
tt = time.Unix(tsec, int64(tnsec)).In(time.FixedZone("", int(tzint)*60))
1318
var _ decDriver = (*bincDecDriver)(nil)
1319
var _ encDriver = (*bincEncDriver)(nil)