open-vault/vendor/github.com/ugorji/go/codec/simple.go

653 lines
16 KiB
Go
Raw Normal View History

2018-01-26 23:51:00 +00:00
// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
2015-08-19 01:12:51 +00:00
// Use of this source code is governed by a MIT license found in the LICENSE file.
2015-06-29 21:50:55 +00:00
package codec
2015-10-07 20:10:00 +00:00
import (
"math"
"reflect"
2018-01-26 23:51:00 +00:00
"time"
2015-10-07 20:10:00 +00:00
)
2015-06-29 21:50:55 +00:00
const (
_ uint8 = iota
simpleVdNil = 1
simpleVdFalse = 2
simpleVdTrue = 3
simpleVdFloat32 = 4
simpleVdFloat64 = 5
// each lasts for 4 (ie n, n+1, n+2, n+3)
simpleVdPosInt = 8
simpleVdNegInt = 12
2018-01-26 23:51:00 +00:00
simpleVdTime = 24
2015-06-29 21:50:55 +00:00
// containers: each lasts for 4 (ie n, n+1, n+2, ... n+7)
simpleVdString = 216
simpleVdByteArray = 224
simpleVdArray = 232
simpleVdMap = 240
simpleVdExt = 248
)
type simpleEncDriver struct {
2015-10-30 22:07:00 +00:00
noBuiltInTypes
2017-10-27 19:06:04 +00:00
// encNoSeparator
2015-06-29 21:50:55 +00:00
e *Encoder
h *SimpleHandle
w encWriter
b [8]byte
2018-01-26 23:51:00 +00:00
// c containerState
encDriverTrackContainerWriter
// encDriverNoopContainerWriter
_ [2]uint64 // padding
2015-06-29 21:50:55 +00:00
}
func (e *simpleEncDriver) EncodeNil() {
e.w.writen1(simpleVdNil)
}
func (e *simpleEncDriver) EncodeBool(b bool) {
2018-01-26 23:51:00 +00:00
if e.h.EncZeroValuesAsNil && e.c != containerMapKey && !b {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
if b {
e.w.writen1(simpleVdTrue)
} else {
e.w.writen1(simpleVdFalse)
}
}
func (e *simpleEncDriver) EncodeFloat32(f float32) {
2018-01-26 23:51:00 +00:00
if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
e.w.writen1(simpleVdFloat32)
bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
}
func (e *simpleEncDriver) EncodeFloat64(f float64) {
2018-01-26 23:51:00 +00:00
if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
e.w.writen1(simpleVdFloat64)
bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f))
}
func (e *simpleEncDriver) EncodeInt(v int64) {
if v < 0 {
e.encUint(uint64(-v), simpleVdNegInt)
} else {
e.encUint(uint64(v), simpleVdPosInt)
}
}
func (e *simpleEncDriver) EncodeUint(v uint64) {
e.encUint(v, simpleVdPosInt)
}
func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
2018-01-26 23:51:00 +00:00
if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == 0 {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
if v <= math.MaxUint8 {
e.w.writen2(bd, uint8(v))
} else if v <= math.MaxUint16 {
e.w.writen1(bd + 1)
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
} else if v <= math.MaxUint32 {
e.w.writen1(bd + 2)
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
2015-08-19 01:12:51 +00:00
} else { // if v <= math.MaxUint64 {
2015-06-29 21:50:55 +00:00
e.w.writen1(bd + 3)
bigenHelper{e.b[:8], e.w}.writeUint64(v)
}
}
func (e *simpleEncDriver) encLen(bd byte, length int) {
if length == 0 {
e.w.writen1(bd)
} else if length <= math.MaxUint8 {
e.w.writen1(bd + 1)
e.w.writen1(uint8(length))
} else if length <= math.MaxUint16 {
e.w.writen1(bd + 2)
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length))
} else if int64(length) <= math.MaxUint32 {
e.w.writen1(bd + 3)
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length))
} else {
e.w.writen1(bd + 4)
bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length))
}
}
func (e *simpleEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
bs := ext.WriteExt(rv)
if bs == nil {
e.EncodeNil()
return
}
e.encodeExtPreamble(uint8(xtag), len(bs))
e.w.writeb(bs)
}
func (e *simpleEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
e.w.writeb(re.Data)
}
func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
e.encLen(simpleVdExt, length)
e.w.writen1(xtag)
}
2017-10-27 19:06:04 +00:00
func (e *simpleEncDriver) WriteArrayStart(length int) {
2018-01-26 23:51:00 +00:00
e.c = containerArrayStart
2015-06-29 21:50:55 +00:00
e.encLen(simpleVdArray, length)
}
2017-10-27 19:06:04 +00:00
func (e *simpleEncDriver) WriteMapStart(length int) {
2018-01-26 23:51:00 +00:00
e.c = containerMapStart
2015-06-29 21:50:55 +00:00
e.encLen(simpleVdMap, length)
}
func (e *simpleEncDriver) EncodeString(c charEncoding, v string) {
2018-01-26 23:51:00 +00:00
if false && e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == "" {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
e.encLen(simpleVdString, len(v))
e.w.writestr(v)
}
2018-01-26 23:51:00 +00:00
// func (e *simpleEncDriver) EncodeSymbol(v string) {
// e.EncodeString(cUTF8, v)
// }
2015-06-29 21:50:55 +00:00
func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
2018-01-26 23:51:00 +00:00
// if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == nil {
if v == nil {
e.EncodeNil()
return
}
2015-06-29 21:50:55 +00:00
e.encLen(simpleVdByteArray, len(v))
e.w.writeb(v)
}
2018-01-26 23:51:00 +00:00
func (e *simpleEncDriver) EncodeTime(t time.Time) {
// if e.h.EncZeroValuesAsNil && e.c != containerMapKey && t.IsZero() {
if t.IsZero() {
e.EncodeNil()
return
}
v, err := t.MarshalBinary()
if err != nil {
e.e.errorv(err)
return
}
// time.Time marshalbinary takes about 14 bytes.
e.w.writen2(simpleVdTime, uint8(len(v)))
e.w.writeb(v)
}
2015-06-29 21:50:55 +00:00
//------------------------------------
type simpleDecDriver struct {
d *Decoder
h *SimpleHandle
r decReader
bdRead bool
bd byte
2018-01-26 23:51:00 +00:00
br bool // a bytes reader?
c containerState
// b [scratchByteArrayLen]byte
2015-06-29 21:50:55 +00:00
noBuiltInTypes
2017-10-27 19:06:04 +00:00
// noStreamingCodec
decDriverNoopContainerReader
2018-01-26 23:51:00 +00:00
_ [3]uint64 // padding
2015-06-29 21:50:55 +00:00
}
func (d *simpleDecDriver) readNextBd() {
d.bd = d.r.readn1()
d.bdRead = true
2015-10-30 22:07:00 +00:00
}
2016-09-12 19:16:55 +00:00
func (d *simpleDecDriver) uncacheRead() {
if d.bdRead {
d.r.unreadn1()
d.bdRead = false
}
}
2015-10-30 22:07:00 +00:00
func (d *simpleDecDriver) ContainerType() (vt valueType) {
2017-07-18 14:15:54 +00:00
if !d.bdRead {
d.readNextBd()
}
2018-01-26 23:51:00 +00:00
switch d.bd {
case simpleVdNil:
2015-10-30 22:07:00 +00:00
return valueTypeNil
2018-01-26 23:51:00 +00:00
case simpleVdByteArray, simpleVdByteArray + 1,
simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
2015-10-30 22:07:00 +00:00
return valueTypeBytes
2018-01-26 23:51:00 +00:00
case simpleVdString, simpleVdString + 1,
simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
2015-10-30 22:07:00 +00:00
return valueTypeString
2018-01-26 23:51:00 +00:00
case simpleVdArray, simpleVdArray + 1,
simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
2015-10-30 22:07:00 +00:00
return valueTypeArray
2018-01-26 23:51:00 +00:00
case simpleVdMap, simpleVdMap + 1,
simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
2015-10-30 22:07:00 +00:00
return valueTypeMap
2018-01-26 23:51:00 +00:00
// case simpleVdTime:
// return valueTypeTime
2015-06-29 21:50:55 +00:00
}
2018-01-26 23:51:00 +00:00
// else {
// d.d.errorf("isContainerType: unsupported parameter: %v", vt)
// }
2015-10-30 22:07:00 +00:00
return valueTypeUnset
2015-06-29 21:50:55 +00:00
}
func (d *simpleDecDriver) TryDecodeAsNil() bool {
if !d.bdRead {
d.readNextBd()
}
if d.bd == simpleVdNil {
d.bdRead = false
return true
}
return false
}
func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
if !d.bdRead {
d.readNextBd()
}
switch d.bd {
case simpleVdPosInt:
ui = uint64(d.r.readn1())
case simpleVdPosInt + 1:
ui = uint64(bigen.Uint16(d.r.readx(2)))
case simpleVdPosInt + 2:
ui = uint64(bigen.Uint32(d.r.readx(4)))
case simpleVdPosInt + 3:
ui = uint64(bigen.Uint64(d.r.readx(8)))
case simpleVdNegInt:
ui = uint64(d.r.readn1())
neg = true
case simpleVdNegInt + 1:
ui = uint64(bigen.Uint16(d.r.readx(2)))
neg = true
case simpleVdNegInt + 2:
ui = uint64(bigen.Uint32(d.r.readx(4)))
neg = true
case simpleVdNegInt + 3:
ui = uint64(bigen.Uint64(d.r.readx(8)))
neg = true
default:
2018-01-26 23:51:00 +00:00
d.d.errorf("Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
2015-06-29 21:50:55 +00:00
return
}
// don't do this check, because callers may only want the unsigned value.
// if ui > math.MaxInt64 {
// d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
// return
// }
return
}
2018-01-26 23:51:00 +00:00
func (d *simpleDecDriver) DecodeInt64() (i int64) {
2015-06-29 21:50:55 +00:00
ui, neg := d.decCheckInteger()
2018-01-26 23:51:00 +00:00
i = chkOvf.SignedIntV(ui)
2015-06-29 21:50:55 +00:00
if neg {
i = -i
}
d.bdRead = false
return
}
2018-01-26 23:51:00 +00:00
func (d *simpleDecDriver) DecodeUint64() (ui uint64) {
2015-06-29 21:50:55 +00:00
ui, neg := d.decCheckInteger()
if neg {
d.d.errorf("Assigning negative signed value to unsigned type")
return
}
d.bdRead = false
return
}
2018-01-26 23:51:00 +00:00
func (d *simpleDecDriver) DecodeFloat64() (f float64) {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
if d.bd == simpleVdFloat32 {
f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
} else if d.bd == simpleVdFloat64 {
f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
} else {
if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
2018-01-26 23:51:00 +00:00
f = float64(d.DecodeInt64())
2015-06-29 21:50:55 +00:00
} else {
d.d.errorf("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
return
}
}
d.bdRead = false
return
}
// bool can be decoded from bool only (single byte).
func (d *simpleDecDriver) DecodeBool() (b bool) {
if !d.bdRead {
d.readNextBd()
}
if d.bd == simpleVdTrue {
b = true
} else if d.bd == simpleVdFalse {
} else {
d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
return
}
d.bdRead = false
return
}
func (d *simpleDecDriver) ReadMapStart() (length int) {
2017-07-18 14:15:54 +00:00
if !d.bdRead {
d.readNextBd()
}
2015-06-29 21:50:55 +00:00
d.bdRead = false
2018-01-26 23:51:00 +00:00
d.c = containerMapStart
2015-06-29 21:50:55 +00:00
return d.decLen()
}
func (d *simpleDecDriver) ReadArrayStart() (length int) {
2017-07-18 14:15:54 +00:00
if !d.bdRead {
d.readNextBd()
}
2015-06-29 21:50:55 +00:00
d.bdRead = false
2018-01-26 23:51:00 +00:00
d.c = containerArrayStart
2015-06-29 21:50:55 +00:00
return d.decLen()
}
2018-01-26 23:51:00 +00:00
func (d *simpleDecDriver) ReadArrayElem() {
d.c = containerArrayElem
}
func (d *simpleDecDriver) ReadArrayEnd() {
d.c = containerArrayEnd
}
func (d *simpleDecDriver) ReadMapElemKey() {
d.c = containerMapKey
}
func (d *simpleDecDriver) ReadMapElemValue() {
d.c = containerMapValue
}
func (d *simpleDecDriver) ReadMapEnd() {
d.c = containerMapEnd
}
2015-06-29 21:50:55 +00:00
func (d *simpleDecDriver) decLen() int {
switch d.bd % 8 {
case 0:
return 0
case 1:
return int(d.r.readn1())
case 2:
return int(bigen.Uint16(d.r.readx(2)))
case 3:
ui := uint64(bigen.Uint32(d.r.readx(4)))
if chkOvf.Uint(ui, intBitsize) {
2018-01-26 23:51:00 +00:00
d.d.errorf("overflow integer: %v", ui)
2015-06-29 21:50:55 +00:00
return 0
}
return int(ui)
case 4:
ui := bigen.Uint64(d.r.readx(8))
if chkOvf.Uint(ui, intBitsize) {
2018-01-26 23:51:00 +00:00
d.d.errorf("overflow integer: %v", ui)
2015-06-29 21:50:55 +00:00
return 0
}
return int(ui)
}
2017-01-13 14:06:34 +00:00
d.d.errorf("decLen: Cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8)
2015-06-29 21:50:55 +00:00
return -1
}
func (d *simpleDecDriver) DecodeString() (s string) {
2018-01-26 23:51:00 +00:00
return string(d.DecodeBytes(d.d.b[:], true))
2015-06-29 21:50:55 +00:00
}
2017-10-27 19:06:04 +00:00
func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) {
2018-01-26 23:51:00 +00:00
return d.DecodeBytes(d.d.b[:], true)
2017-10-27 19:06:04 +00:00
}
func (d *simpleDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
if d.bd == simpleVdNil {
d.bdRead = false
return
}
2018-01-26 23:51:00 +00:00
// check if an "array" of uint8's (see ContainerType for how to infer if an array)
if d.bd >= simpleVdArray && d.bd <= simpleVdMap+4 {
if len(bs) == 0 && zerocopy {
bs = d.d.b[:]
}
bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
return
}
2015-06-29 21:50:55 +00:00
clen := d.decLen()
d.bdRead = false
if zerocopy {
if d.br {
return d.r.readx(clen)
} else if len(bs) == 0 {
2018-01-26 23:51:00 +00:00
bs = d.d.b[:]
2015-06-29 21:50:55 +00:00
}
}
2017-01-27 01:16:19 +00:00
return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs)
2015-06-29 21:50:55 +00:00
}
2018-01-26 23:51:00 +00:00
func (d *simpleDecDriver) DecodeTime() (t time.Time) {
if !d.bdRead {
d.readNextBd()
}
if d.bd == simpleVdNil {
d.bdRead = false
return
}
if d.bd != simpleVdTime {
d.d.errorf("invalid descriptor for time.Time - expect 0x%x, received 0x%x", simpleVdTime, d.bd)
return
}
d.bdRead = false
clen := int(d.r.readn1())
b := d.r.readx(clen)
if err := (&t).UnmarshalBinary(b); err != nil {
d.d.errorv(err)
}
return
}
2015-06-29 21:50:55 +00:00
func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
if xtag > 0xff {
d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
return
}
realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
realxtag = uint64(realxtag1)
if ext == nil {
re := rv.(*RawExt)
re.Tag = realxtag
re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
} else {
ext.ReadExt(rv, xbs)
}
return
}
func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
if !d.bdRead {
d.readNextBd()
}
switch d.bd {
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
l := d.decLen()
xtag = d.r.readn1()
if verifyTag && xtag != tag {
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
return
}
xbs = d.r.readx(l)
2018-01-26 23:51:00 +00:00
case simpleVdByteArray, simpleVdByteArray + 1,
simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
2017-10-27 19:06:04 +00:00
xbs = d.DecodeBytes(nil, true)
2015-06-29 21:50:55 +00:00
default:
2018-01-26 23:51:00 +00:00
d.d.errorf("Invalid descriptor - expecting extensions/bytearray, got: 0x%x", d.bd)
2015-06-29 21:50:55 +00:00
return
}
d.bdRead = false
return
}
2015-10-30 22:07:00 +00:00
func (d *simpleDecDriver) DecodeNaked() {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
2017-10-27 19:06:04 +00:00
n := d.d.n
2015-10-30 22:07:00 +00:00
var decodeFurther bool
2015-06-29 21:50:55 +00:00
switch d.bd {
case simpleVdNil:
2015-10-30 22:07:00 +00:00
n.v = valueTypeNil
2015-06-29 21:50:55 +00:00
case simpleVdFalse:
2015-10-30 22:07:00 +00:00
n.v = valueTypeBool
n.b = false
2015-06-29 21:50:55 +00:00
case simpleVdTrue:
2015-10-30 22:07:00 +00:00
n.v = valueTypeBool
n.b = true
2015-06-29 21:50:55 +00:00
case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
if d.h.SignedInteger {
2015-10-30 22:07:00 +00:00
n.v = valueTypeInt
2018-01-26 23:51:00 +00:00
n.i = d.DecodeInt64()
2015-06-29 21:50:55 +00:00
} else {
2015-10-30 22:07:00 +00:00
n.v = valueTypeUint
2018-01-26 23:51:00 +00:00
n.u = d.DecodeUint64()
2015-06-29 21:50:55 +00:00
}
case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
2015-10-30 22:07:00 +00:00
n.v = valueTypeInt
2018-01-26 23:51:00 +00:00
n.i = d.DecodeInt64()
2015-06-29 21:50:55 +00:00
case simpleVdFloat32:
2015-10-30 22:07:00 +00:00
n.v = valueTypeFloat
2018-01-26 23:51:00 +00:00
n.f = d.DecodeFloat64()
2015-06-29 21:50:55 +00:00
case simpleVdFloat64:
2015-10-30 22:07:00 +00:00
n.v = valueTypeFloat
2018-01-26 23:51:00 +00:00
n.f = d.DecodeFloat64()
case simpleVdTime:
n.v = valueTypeTime
n.t = d.DecodeTime()
case simpleVdString, simpleVdString + 1,
simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
2015-10-30 22:07:00 +00:00
n.v = valueTypeString
n.s = d.DecodeString()
2018-01-26 23:51:00 +00:00
case simpleVdByteArray, simpleVdByteArray + 1,
simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
2015-10-30 22:07:00 +00:00
n.v = valueTypeBytes
2017-10-27 19:06:04 +00:00
n.l = d.DecodeBytes(nil, false)
2015-06-29 21:50:55 +00:00
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
2015-10-30 22:07:00 +00:00
n.v = valueTypeExt
2015-06-29 21:50:55 +00:00
l := d.decLen()
2015-10-30 22:07:00 +00:00
n.u = uint64(d.r.readn1())
n.l = d.r.readx(l)
2018-01-26 23:51:00 +00:00
case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2,
simpleVdArray + 3, simpleVdArray + 4:
2015-10-30 22:07:00 +00:00
n.v = valueTypeArray
2015-06-29 21:50:55 +00:00
decodeFurther = true
case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
2015-10-30 22:07:00 +00:00
n.v = valueTypeMap
2015-06-29 21:50:55 +00:00
decodeFurther = true
default:
d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
}
if !decodeFurther {
d.bdRead = false
}
return
}
//------------------------------------
// SimpleHandle is a Handle for a very simple encoding format.
//
// simple is a simplistic codec similar to binc, but not as compact.
2016-09-02 22:05:09 +00:00
// - Encoding of a value is always preceded by the descriptor byte (bd)
2015-06-29 21:50:55 +00:00
// - True, false, nil are encoded fully in 1 byte (the descriptor)
// - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
// There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
// - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
2018-01-26 23:51:00 +00:00
// - Length of containers (strings, bytes, array, map, extensions)
2015-06-29 21:50:55 +00:00
// are encoded in 0, 1, 2, 4 or 8 bytes.
// Zero-length containers have no length encoded.
// For others, the number of bytes is given by pow(2, bd%3)
// - maps are encoded as [bd] [length] [[key][value]]...
// - arrays are encoded as [bd] [length] [value]...
// - extensions are encoded as [bd] [length] [tag] [byte]...
// - strings/bytearrays are encoded as [bd] [length] [byte]...
2018-01-26 23:51:00 +00:00
// - time.Time are encoded as [bd] [length] [byte]...
2015-06-29 21:50:55 +00:00
//
// The full spec will be published soon.
type SimpleHandle struct {
BasicHandle
binaryEncodingType
2017-10-27 19:06:04 +00:00
noElemSeparators
2018-01-26 23:51:00 +00:00
// EncZeroValuesAsNil says to encode zero values for numbers, bool, string, etc as nil
EncZeroValuesAsNil bool
_ [1]uint64 // padding
2015-06-29 21:50:55 +00:00
}
2018-01-26 23:51:00 +00:00
// Name returns the name of the handle: simple
func (h *SimpleHandle) Name() string { return "simple" }
// SetBytesExt sets an extension
2015-10-30 22:07:00 +00:00
func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
2018-01-26 23:51:00 +00:00
return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
2015-10-30 22:07:00 +00:00
}
2018-01-26 23:51:00 +00:00
func (h *SimpleHandle) hasElemSeparators() bool { return true } // as it implements Write(Map|Array)XXX
2015-06-29 21:50:55 +00:00
func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver {
return &simpleEncDriver{e: e, w: e.w, h: h}
}
func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver {
2017-03-31 00:03:13 +00:00
return &simpleDecDriver{d: d, h: h, r: d.r, br: d.bytes}
2015-06-29 21:50:55 +00:00
}
2015-10-30 22:07:00 +00:00
func (e *simpleEncDriver) reset() {
2018-01-26 23:51:00 +00:00
e.c = 0
2015-10-30 22:07:00 +00:00
e.w = e.e.w
}
func (d *simpleDecDriver) reset() {
2018-01-26 23:51:00 +00:00
d.c = 0
2017-03-31 00:03:13 +00:00
d.r, d.br = d.d.r, d.d.bytes
2016-04-26 00:18:04 +00:00
d.bd, d.bdRead = 0, false
2015-10-07 20:10:00 +00:00
}
2015-06-29 21:50:55 +00:00
var _ decDriver = (*simpleDecDriver)(nil)
var _ encDriver = (*simpleEncDriver)(nil)