2015-08-20 04:44:36 +03:00
|
|
|
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
2015-02-03 05:17:15 +03:00
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// 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-08-20 04:44:36 +03:00
|
|
|
e *Encoder
|
2015-02-03 05:17:15 +03:00
|
|
|
h *SimpleHandle
|
|
|
|
w encWriter
|
2015-08-20 04:44:36 +03:00
|
|
|
noBuiltInTypes
|
|
|
|
b [8]byte
|
|
|
|
encNoSeparator
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeNil() {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(simpleVdNil)
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeBool(b bool) {
|
2015-02-03 05:17:15 +03:00
|
|
|
if b {
|
|
|
|
e.w.writen1(simpleVdTrue)
|
|
|
|
} else {
|
|
|
|
e.w.writen1(simpleVdFalse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeFloat32(f float32) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(simpleVdFloat32)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeFloat64(f float64) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(simpleVdFloat64)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f))
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeInt(v int64) {
|
2015-02-03 05:17:15 +03:00
|
|
|
if v < 0 {
|
|
|
|
e.encUint(uint64(-v), simpleVdNegInt)
|
|
|
|
} else {
|
|
|
|
e.encUint(uint64(v), simpleVdPosInt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeUint(v uint64) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.encUint(v, simpleVdPosInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
|
2015-08-20 04:44:36 +03:00
|
|
|
if v <= math.MaxUint8 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen2(bd, uint8(v))
|
2015-08-20 04:44:36 +03:00
|
|
|
} else if v <= math.MaxUint16 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 1)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
|
|
|
|
} else if v <= math.MaxUint32 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 2)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
|
|
|
|
} else { // if v <= math.MaxUint64 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 3)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:8], e.w}.writeUint64(v)
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *simpleEncDriver) encLen(bd byte, length int) {
|
2015-08-20 04:44:36 +03:00
|
|
|
if length == 0 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd)
|
2015-08-20 04:44:36 +03:00
|
|
|
} else if length <= math.MaxUint8 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 1)
|
|
|
|
e.w.writen1(uint8(length))
|
2015-08-20 04:44:36 +03:00
|
|
|
} else if length <= math.MaxUint16 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 2)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length))
|
|
|
|
} else if int64(length) <= math.MaxUint32 {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 3)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length))
|
|
|
|
} else {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.w.writen1(bd + 4)
|
2015-08-20 04:44:36 +03:00
|
|
|
bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length))
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:17:15 +03:00
|
|
|
func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
|
|
|
|
e.encLen(simpleVdExt, length)
|
|
|
|
e.w.writen1(xtag)
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeArrayStart(length int) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.encLen(simpleVdArray, length)
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeMapStart(length int) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.encLen(simpleVdMap, length)
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeString(c charEncoding, v string) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.encLen(simpleVdString, len(v))
|
|
|
|
e.w.writestr(v)
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeSymbol(v string) {
|
|
|
|
e.EncodeString(c_UTF8, v)
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
|
2015-02-03 05:17:15 +03:00
|
|
|
e.encLen(simpleVdByteArray, len(v))
|
|
|
|
e.w.writeb(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
type simpleDecDriver struct {
|
2015-08-20 04:44:36 +03:00
|
|
|
d *Decoder
|
2015-02-03 05:17:15 +03:00
|
|
|
h *SimpleHandle
|
|
|
|
r decReader
|
|
|
|
bdRead bool
|
|
|
|
bdType valueType
|
|
|
|
bd byte
|
2015-08-20 04:44:36 +03:00
|
|
|
br bool // bytes reader
|
|
|
|
noBuiltInTypes
|
|
|
|
noStreamingCodec
|
|
|
|
decNoSeparator
|
|
|
|
b [scratchByteArrayLen]byte
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) readNextBd() {
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bd = d.r.readn1()
|
|
|
|
d.bdRead = true
|
|
|
|
d.bdType = valueTypeUnset
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) IsContainerType(vt valueType) bool {
|
|
|
|
switch vt {
|
|
|
|
case valueTypeNil:
|
|
|
|
return d.bd == simpleVdNil
|
|
|
|
case valueTypeBytes:
|
|
|
|
const x uint8 = simpleVdByteArray
|
|
|
|
return d.bd == x || d.bd == x+1 || d.bd == x+2 || d.bd == x+3 || d.bd == x+4
|
|
|
|
case valueTypeString:
|
|
|
|
const x uint8 = simpleVdString
|
|
|
|
return d.bd == x || d.bd == x+1 || d.bd == x+2 || d.bd == x+3 || d.bd == x+4
|
|
|
|
case valueTypeArray:
|
|
|
|
const x uint8 = simpleVdArray
|
|
|
|
return d.bd == x || d.bd == x+1 || d.bd == x+2 || d.bd == x+3 || d.bd == x+4
|
|
|
|
case valueTypeMap:
|
|
|
|
const x uint8 = simpleVdMap
|
|
|
|
return d.bd == x || d.bd == x+1 || d.bd == x+2 || d.bd == x+3 || d.bd == x+4
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("isContainerType: unsupported parameter: %v", vt)
|
|
|
|
return false // "unreachable"
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) TryDecodeAsNil() bool {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
if d.bd == simpleVdNil {
|
|
|
|
d.bdRead = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
switch d.bd {
|
|
|
|
case simpleVdPosInt:
|
|
|
|
ui = uint64(d.r.readn1())
|
|
|
|
case simpleVdPosInt + 1:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint16(d.r.readx(2)))
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdPosInt + 2:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint32(d.r.readx(4)))
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdPosInt + 3:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint64(d.r.readx(8)))
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdNegInt:
|
|
|
|
ui = uint64(d.r.readn1())
|
|
|
|
neg = true
|
|
|
|
case simpleVdNegInt + 1:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint16(d.r.readx(2)))
|
2015-02-03 05:17:15 +03:00
|
|
|
neg = true
|
|
|
|
case simpleVdNegInt + 2:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint32(d.r.readx(4)))
|
2015-02-03 05:17:15 +03:00
|
|
|
neg = true
|
|
|
|
case simpleVdNegInt + 3:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui = uint64(bigen.Uint64(d.r.readx(8)))
|
2015-02-03 05:17:15 +03:00
|
|
|
neg = true
|
|
|
|
default:
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("decIntAny: Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
// don't do this check, because callers may only want the unsigned value.
|
|
|
|
// if ui > math.MaxInt64 {
|
2015-08-20 04:44:36 +03:00
|
|
|
// d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
|
|
|
|
// return
|
2015-02-03 05:17:15 +03:00
|
|
|
// }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeInt(bitsize uint8) (i int64) {
|
|
|
|
ui, neg := d.decCheckInteger()
|
|
|
|
i, overflow := chkOvf.SignedInt(ui)
|
|
|
|
if overflow {
|
|
|
|
d.d.errorf("simple: overflow converting %v to signed integer", ui)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if neg {
|
|
|
|
i = -i
|
|
|
|
}
|
|
|
|
if chkOvf.Int(i, bitsize) {
|
|
|
|
d.d.errorf("simple: overflow integer: %v", i)
|
|
|
|
return
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bdRead = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
|
|
|
|
ui, neg := d.decCheckInteger()
|
2015-02-03 05:17:15 +03:00
|
|
|
if neg {
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("Assigning negative signed value to unsigned type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if chkOvf.Uint(ui, bitsize) {
|
|
|
|
d.d.errorf("simple: overflow integer: %v", ui)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
d.bdRead = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
|
|
|
|
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 {
|
2015-02-03 05:17:15 +03:00
|
|
|
if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
|
2015-08-20 04:44:36 +03:00
|
|
|
f = float64(d.DecodeInt(64))
|
2015-02-03 05:17:15 +03:00
|
|
|
} else {
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 04:44:36 +03:00
|
|
|
if chkOverflow32 && chkOvf.Float32(f) {
|
|
|
|
d.d.errorf("msgpack: float32 overflow: %v", f)
|
|
|
|
return
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bdRead = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool can be decoded from bool only (single byte).
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeBool() (b bool) {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
|
|
|
if d.bd == simpleVdTrue {
|
2015-02-03 05:17:15 +03:00
|
|
|
b = true
|
2015-08-20 04:44:36 +03:00
|
|
|
} else if d.bd == simpleVdFalse {
|
|
|
|
} else {
|
|
|
|
d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
d.bdRead = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) ReadMapStart() (length int) {
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bdRead = false
|
|
|
|
return d.decLen()
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) ReadArrayStart() (length int) {
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bdRead = false
|
|
|
|
return d.decLen()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *simpleDecDriver) decLen() int {
|
|
|
|
switch d.bd % 8 {
|
|
|
|
case 0:
|
|
|
|
return 0
|
|
|
|
case 1:
|
|
|
|
return int(d.r.readn1())
|
|
|
|
case 2:
|
2015-08-20 04:44:36 +03:00
|
|
|
return int(bigen.Uint16(d.r.readx(2)))
|
2015-02-03 05:17:15 +03:00
|
|
|
case 3:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui := uint64(bigen.Uint32(d.r.readx(4)))
|
|
|
|
if chkOvf.Uint(ui, intBitsize) {
|
|
|
|
d.d.errorf("simple: overflow integer: %v", ui)
|
|
|
|
return 0
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
return int(ui)
|
|
|
|
case 4:
|
2015-08-20 04:44:36 +03:00
|
|
|
ui := bigen.Uint64(d.r.readx(8))
|
|
|
|
if chkOvf.Uint(ui, intBitsize) {
|
|
|
|
d.d.errorf("simple: overflow integer: %v", ui)
|
|
|
|
return 0
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
return int(ui)
|
|
|
|
}
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("decLen: Cannot read length: bd%8 must be in range 0..4. Got: %d", d.bd%8)
|
2015-02-03 05:17:15 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeString() (s string) {
|
|
|
|
return string(d.DecodeBytes(d.b[:], true, true))
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
|
|
|
if d.bd == simpleVdNil {
|
|
|
|
d.bdRead = false
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
2015-08-20 04:44:36 +03:00
|
|
|
clen := d.decLen()
|
2015-02-03 05:17:15 +03:00
|
|
|
d.bdRead = false
|
2015-08-20 04:44:36 +03:00
|
|
|
if zerocopy {
|
|
|
|
if d.br {
|
|
|
|
return d.r.readx(clen)
|
|
|
|
} else if len(bs) == 0 {
|
|
|
|
bs = d.b[:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return decByteSlice(d.r, clen, bs)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
switch d.bd {
|
|
|
|
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
|
|
|
|
l := d.decLen()
|
|
|
|
xtag = d.r.readn1()
|
|
|
|
if verifyTag && xtag != tag {
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
2015-08-20 04:44:36 +03:00
|
|
|
xbs = d.r.readx(l)
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
|
2015-08-20 04:44:36 +03:00
|
|
|
xbs = d.DecodeBytes(nil, false, true)
|
2015-02-03 05:17:15 +03:00
|
|
|
default:
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("Invalid d.bd for extensions (Expecting extensions or byte array). Got: 0x%x", d.bd)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
d.bdRead = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (d *simpleDecDriver) DecodeNaked() (v interface{}, vt valueType, decodeFurther bool) {
|
|
|
|
if !d.bdRead {
|
|
|
|
d.readNextBd()
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
|
|
|
|
switch d.bd {
|
|
|
|
case simpleVdNil:
|
|
|
|
vt = valueTypeNil
|
|
|
|
case simpleVdFalse:
|
|
|
|
vt = valueTypeBool
|
|
|
|
v = false
|
|
|
|
case simpleVdTrue:
|
|
|
|
vt = valueTypeBool
|
|
|
|
v = true
|
|
|
|
case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
|
2015-08-20 04:44:36 +03:00
|
|
|
if d.h.SignedInteger {
|
|
|
|
vt = valueTypeInt
|
|
|
|
v = d.DecodeInt(64)
|
|
|
|
} else {
|
|
|
|
vt = valueTypeUint
|
|
|
|
v = d.DecodeUint(64)
|
|
|
|
}
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
|
|
|
|
vt = valueTypeInt
|
2015-08-20 04:44:36 +03:00
|
|
|
v = d.DecodeInt(64)
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdFloat32:
|
|
|
|
vt = valueTypeFloat
|
2015-08-20 04:44:36 +03:00
|
|
|
v = d.DecodeFloat(true)
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdFloat64:
|
|
|
|
vt = valueTypeFloat
|
2015-08-20 04:44:36 +03:00
|
|
|
v = d.DecodeFloat(false)
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
|
|
|
|
vt = valueTypeString
|
2015-08-20 04:44:36 +03:00
|
|
|
v = d.DecodeString()
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
|
|
|
|
vt = valueTypeBytes
|
2015-08-20 04:44:36 +03:00
|
|
|
v = d.DecodeBytes(nil, false, false)
|
2015-02-03 05:17:15 +03:00
|
|
|
case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
|
|
|
|
vt = valueTypeExt
|
|
|
|
l := d.decLen()
|
|
|
|
var re RawExt
|
2015-08-20 04:44:36 +03:00
|
|
|
re.Tag = uint64(d.r.readn1())
|
|
|
|
re.Data = d.r.readx(l)
|
2015-02-03 05:17:15 +03:00
|
|
|
v = &re
|
|
|
|
case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
|
|
|
|
vt = valueTypeArray
|
|
|
|
decodeFurther = true
|
|
|
|
case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
|
|
|
|
vt = valueTypeMap
|
|
|
|
decodeFurther = true
|
|
|
|
default:
|
2015-08-20 04:44:36 +03:00
|
|
|
d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
|
|
|
|
return
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
// - Encoding of a value is always preceeded by the descriptor byte (bd)
|
|
|
|
// - 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)
|
|
|
|
// - Lenght of containers (strings, bytes, array, map, extensions)
|
|
|
|
// 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]...
|
|
|
|
//
|
|
|
|
// The full spec will be published soon.
|
|
|
|
type SimpleHandle struct {
|
|
|
|
BasicHandle
|
2015-08-20 04:44:36 +03:00
|
|
|
binaryEncodingType
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver {
|
|
|
|
return &simpleEncDriver{e: e, w: e.w, h: h}
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 04:44:36 +03:00
|
|
|
func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver {
|
|
|
|
return &simpleDecDriver{d: d, r: d.r, h: h, br: d.bytes}
|
2015-02-03 05:17:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ decDriver = (*simpleDecDriver)(nil)
|
|
|
|
var _ encDriver = (*simpleEncDriver)(nil)
|