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

770 lines
18 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 (
cborMajorUint byte = iota
cborMajorNegInt
cborMajorBytes
cborMajorText
cborMajorArray
cborMajorMap
cborMajorTag
cborMajorOther
)
const (
cborBdFalse byte = 0xf4 + iota
cborBdTrue
cborBdNil
cborBdUndefined
cborBdExt
cborBdFloat16
cborBdFloat32
cborBdFloat64
)
const (
cborBdIndefiniteBytes byte = 0x5f
cborBdIndefiniteString byte = 0x7f
cborBdIndefiniteArray byte = 0x9f
cborBdIndefiniteMap byte = 0xbf
cborBdBreak byte = 0xff
2015-06-29 21:50:55 +00:00
)
2018-01-26 23:51:00 +00:00
// These define some in-stream descriptors for
// manual encoding e.g. when doing explicit indefinite-length
2015-06-29 21:50:55 +00:00
const (
CborStreamBytes byte = 0x5f
CborStreamString byte = 0x7f
CborStreamArray byte = 0x9f
CborStreamMap byte = 0xbf
CborStreamBreak byte = 0xff
2015-06-29 21:50:55 +00:00
)
const (
cborBaseUint byte = 0x00
cborBaseNegInt byte = 0x20
cborBaseBytes byte = 0x40
cborBaseString byte = 0x60
cborBaseArray byte = 0x80
cborBaseMap byte = 0xa0
cborBaseTag byte = 0xc0
cborBaseSimple byte = 0xe0
2015-06-29 21:50:55 +00:00
)
2018-02-11 00:29:52 +00:00
func cbordesc(bd byte) string {
switch bd {
case cborBdNil:
return "nil"
case cborBdFalse:
return "false"
case cborBdTrue:
return "true"
case cborBdFloat16, cborBdFloat32, cborBdFloat64:
return "float"
case cborBdIndefiniteBytes:
return "bytes*"
case cborBdIndefiniteString:
return "string*"
case cborBdIndefiniteArray:
return "array*"
case cborBdIndefiniteMap:
return "map*"
default:
switch {
case bd >= cborBaseUint && bd < cborBaseNegInt:
return "(u)int"
case bd >= cborBaseNegInt && bd < cborBaseBytes:
return "int"
case bd >= cborBaseBytes && bd < cborBaseString:
return "bytes"
case bd >= cborBaseString && bd < cborBaseArray:
return "string"
case bd >= cborBaseArray && bd < cborBaseMap:
return "array"
case bd >= cborBaseMap && bd < cborBaseTag:
return "map"
case bd >= cborBaseTag && bd < cborBaseSimple:
return "ext"
default:
return "unknown"
}
}
}
2015-06-29 21:50:55 +00:00
// -------------------
type cborEncDriver struct {
2015-10-30 22:07:00 +00:00
noBuiltInTypes
2017-10-27 19:06:04 +00:00
encDriverNoopContainerWriter
2015-06-29 21:50:55 +00:00
e *Encoder
w *encWriterSwitch
2015-06-29 21:50:55 +00:00
h *CborHandle
x [8]byte
2018-10-03 16:55:26 +00:00
// _ [3]uint64 // padding
2015-06-29 21:50:55 +00:00
}
func (e *cborEncDriver) EncodeNil() {
e.w.writen1(cborBdNil)
}
func (e *cborEncDriver) EncodeBool(b bool) {
if b {
e.w.writen1(cborBdTrue)
} else {
e.w.writen1(cborBdFalse)
}
}
func (e *cborEncDriver) EncodeFloat32(f float32) {
e.w.writen1(cborBdFloat32)
bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f))
}
func (e *cborEncDriver) EncodeFloat64(f float64) {
e.w.writen1(cborBdFloat64)
bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f))
}
func (e *cborEncDriver) encUint(v uint64, bd byte) {
if v <= 0x17 {
e.w.writen1(byte(v) + bd)
} else if v <= math.MaxUint8 {
e.w.writen2(bd+0x18, uint8(v))
} else if v <= math.MaxUint16 {
e.w.writen1(bd + 0x19)
bigenHelper{e.x[:2], e.w}.writeUint16(uint16(v))
} else if v <= math.MaxUint32 {
e.w.writen1(bd + 0x1a)
bigenHelper{e.x[: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 + 0x1b)
bigenHelper{e.x[:8], e.w}.writeUint64(v)
}
}
func (e *cborEncDriver) EncodeInt(v int64) {
if v < 0 {
e.encUint(uint64(-1-v), cborBaseNegInt)
} else {
e.encUint(uint64(v), cborBaseUint)
}
}
func (e *cborEncDriver) EncodeUint(v uint64) {
e.encUint(v, cborBaseUint)
}
func (e *cborEncDriver) encLen(bd byte, length int) {
e.encUint(uint64(length), bd)
}
2018-01-26 23:51:00 +00:00
func (e *cborEncDriver) EncodeTime(t time.Time) {
if t.IsZero() {
e.EncodeNil()
} else if e.h.TimeRFC3339 {
e.encUint(0, cborBaseTag)
e.EncodeStringEnc(cUTF8, t.Format(time.RFC3339Nano))
2018-01-26 23:51:00 +00:00
} else {
e.encUint(1, cborBaseTag)
t = t.UTC().Round(time.Microsecond)
sec, nsec := t.Unix(), uint64(t.Nanosecond())
if nsec == 0 {
e.EncodeInt(sec)
} else {
e.EncodeFloat64(float64(sec) + float64(nsec)/1e9)
}
}
}
2015-06-29 21:50:55 +00:00
func (e *cborEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, en *Encoder) {
e.encUint(uint64(xtag), cborBaseTag)
if v := ext.ConvertExt(rv); v == nil {
e.EncodeNil()
} else {
en.encode(v)
}
}
func (e *cborEncDriver) EncodeRawExt(re *RawExt, en *Encoder) {
e.encUint(uint64(re.Tag), cborBaseTag)
2018-10-03 16:55:26 +00:00
// only encodes re.Value (never re.Data)
// if false && re.Data != nil {
// en.encode(re.Data)
// } else if re.Value != nil {
if re.Value != nil {
2017-10-27 19:06:04 +00:00
en.encode(re.Value)
} else {
2015-06-29 21:50:55 +00:00
e.EncodeNil()
2017-10-27 19:06:04 +00:00
}
}
func (e *cborEncDriver) WriteArrayStart(length int) {
if e.h.IndefiniteLength {
e.w.writen1(cborBdIndefiniteArray)
2015-06-29 21:50:55 +00:00
} else {
2017-10-27 19:06:04 +00:00
e.encLen(cborBaseArray, length)
2015-06-29 21:50:55 +00:00
}
}
2017-10-27 19:06:04 +00:00
func (e *cborEncDriver) WriteMapStart(length int) {
if e.h.IndefiniteLength {
e.w.writen1(cborBdIndefiniteMap)
} else {
e.encLen(cborBaseMap, length)
}
2015-06-29 21:50:55 +00:00
}
2017-10-27 19:06:04 +00:00
func (e *cborEncDriver) WriteMapEnd() {
if e.h.IndefiniteLength {
e.w.writen1(cborBdBreak)
}
2015-06-29 21:50:55 +00:00
}
2017-10-27 19:06:04 +00:00
func (e *cborEncDriver) WriteArrayEnd() {
if e.h.IndefiniteLength {
e.w.writen1(cborBdBreak)
}
2015-06-29 21:50:55 +00:00
}
2017-10-27 19:06:04 +00:00
func (e *cborEncDriver) EncodeString(c charEncoding, v string) {
e.encStringBytesS(cborBaseString, v)
2015-06-29 21:50:55 +00:00
}
func (e *cborEncDriver) EncodeStringEnc(c charEncoding, v string) {
e.encStringBytesS(cborBaseString, v)
}
2015-06-29 21:50:55 +00:00
func (e *cborEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
2018-01-26 23:51:00 +00:00
if v == nil {
e.EncodeNil()
} else if c == cRAW {
2017-10-27 19:06:04 +00:00
e.encStringBytesS(cborBaseBytes, stringView(v))
2015-10-07 20:10:00 +00:00
} else {
2017-10-27 19:06:04 +00:00
e.encStringBytesS(cborBaseString, stringView(v))
}
}
func (e *cborEncDriver) EncodeStringBytesRaw(v []byte) {
if v == nil {
e.EncodeNil()
} else {
e.encStringBytesS(cborBaseBytes, stringView(v))
}
}
2017-10-27 19:06:04 +00:00
func (e *cborEncDriver) encStringBytesS(bb byte, v string) {
if e.h.IndefiniteLength {
if bb == cborBaseBytes {
e.w.writen1(cborBdIndefiniteBytes)
} else {
e.w.writen1(cborBdIndefiniteString)
}
var vlen uint = uint(len(v))
blen := vlen / 4
2017-10-27 19:06:04 +00:00
if blen == 0 {
blen = 64
} else if blen > 1024 {
blen = 1024
}
for i := uint(0); i < vlen; {
2017-10-27 19:06:04 +00:00
var v2 string
i2 := i + blen
if i2 < vlen {
2017-10-27 19:06:04 +00:00
v2 = v[i:i2]
} else {
v2 = v[i:]
}
e.encLen(bb, len(v2))
e.w.writestr(v2)
i = i2
}
e.w.writen1(cborBdBreak)
} else {
e.encLen(bb, len(v))
e.w.writestr(v)
2015-10-07 20:10:00 +00:00
}
2015-06-29 21:50:55 +00:00
}
// ----------------------
type cborDecDriver struct {
2018-10-03 16:55:26 +00:00
d *Decoder
h *CborHandle
r *decReaderSwitch
2015-06-29 21:50:55 +00:00
br bool // bytes reader
bdRead bool
bd byte
noBuiltInTypes
2017-10-27 19:06:04 +00:00
// decNoSeparator
decDriverNoopContainerReader
2018-10-03 16:55:26 +00:00
// _ [3]uint64 // padding
2015-06-29 21:50:55 +00:00
}
func (d *cborDecDriver) readNextBd() {
d.bd = d.r.readn1()
d.bdRead = true
}
2016-09-12 19:16:55 +00:00
func (d *cborDecDriver) uncacheRead() {
if d.bdRead {
d.r.unreadn1()
d.bdRead = false
}
}
2015-10-30 22:07:00 +00:00
func (d *cborDecDriver) ContainerType() (vt valueType) {
2017-07-18 14:15:54 +00:00
if !d.bdRead {
d.readNextBd()
}
2015-10-30 22:07:00 +00:00
if d.bd == cborBdNil {
return valueTypeNil
} else if d.bd == cborBdIndefiniteBytes || (d.bd >= cborBaseBytes && d.bd < cborBaseString) {
return valueTypeBytes
} else if d.bd == cborBdIndefiniteString || (d.bd >= cborBaseString && d.bd < cborBaseArray) {
return valueTypeString
} else if d.bd == cborBdIndefiniteArray || (d.bd >= cborBaseArray && d.bd < cborBaseMap) {
return valueTypeArray
} else if d.bd == cborBdIndefiniteMap || (d.bd >= cborBaseMap && d.bd < cborBaseTag) {
return valueTypeMap
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 *cborDecDriver) TryDecodeAsNil() bool {
if !d.bdRead {
d.readNextBd()
}
// treat Nil and Undefined as nil values
if d.bd == cborBdNil || d.bd == cborBdUndefined {
d.bdRead = false
return true
}
return false
}
func (d *cborDecDriver) CheckBreak() bool {
if !d.bdRead {
d.readNextBd()
}
if d.bd == cborBdBreak {
d.bdRead = false
return true
}
return false
}
func (d *cborDecDriver) decUint() (ui uint64) {
v := d.bd & 0x1f
if v <= 0x17 {
ui = uint64(v)
} else {
if v == 0x18 {
ui = uint64(d.r.readn1())
} else if v == 0x19 {
ui = uint64(bigen.Uint16(d.r.readx(2)))
} else if v == 0x1a {
ui = uint64(bigen.Uint32(d.r.readx(4)))
} else if v == 0x1b {
ui = uint64(bigen.Uint64(d.r.readx(8)))
} else {
2018-02-11 00:29:52 +00:00
d.d.errorf("invalid descriptor decoding uint: %x/%s", d.bd, cbordesc(d.bd))
2015-06-29 21:50:55 +00:00
return
}
}
return
}
func (d *cborDecDriver) decCheckInteger() (neg bool) {
if !d.bdRead {
d.readNextBd()
}
major := d.bd >> 5
if major == cborMajorUint {
} else if major == cborMajorNegInt {
neg = true
} else {
d.d.errorf("not an integer - invalid major %v from descriptor %x/%s",
major, d.bd, cbordesc(d.bd))
2015-06-29 21:50:55 +00:00
return
}
return
}
2018-01-26 23:51:00 +00:00
func (d *cborDecDriver) DecodeInt64() (i int64) {
2015-06-29 21:50:55 +00:00
neg := d.decCheckInteger()
ui := d.decUint()
// check if this number can be converted to an int without overflow
if neg {
2018-01-26 23:51:00 +00:00
i = -(chkOvf.SignedIntV(ui + 1))
2015-06-29 21:50:55 +00:00
} else {
2018-01-26 23:51:00 +00:00
i = chkOvf.SignedIntV(ui)
2015-06-29 21:50:55 +00:00
}
d.bdRead = false
return
}
2018-01-26 23:51:00 +00:00
func (d *cborDecDriver) DecodeUint64() (ui uint64) {
2015-06-29 21:50:55 +00:00
if d.decCheckInteger() {
2018-02-11 00:29:52 +00:00
d.d.errorf("assigning negative signed value to unsigned type")
2015-06-29 21:50:55 +00:00
return
}
ui = d.decUint()
d.bdRead = false
return
}
2018-01-26 23:51:00 +00:00
func (d *cborDecDriver) DecodeFloat64() (f float64) {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
if bd := d.bd; bd == cborBdFloat16 {
f = float64(math.Float32frombits(halfFloatToFloatBits(bigen.Uint16(d.r.readx(2)))))
} else if bd == cborBdFloat32 {
f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
} else if bd == cborBdFloat64 {
f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
} else if bd >= cborBaseUint && bd < cborBaseBytes {
2018-01-26 23:51:00 +00:00
f = float64(d.DecodeInt64())
2015-06-29 21:50:55 +00:00
} else {
2018-02-11 00:29:52 +00:00
d.d.errorf("float only valid from float16/32/64 - invalid descriptor %x/%s", bd, cbordesc(bd))
2015-06-29 21:50:55 +00:00
return
}
d.bdRead = false
return
}
// bool can be decoded from bool only (single byte).
func (d *cborDecDriver) DecodeBool() (b bool) {
if !d.bdRead {
d.readNextBd()
}
if bd := d.bd; bd == cborBdTrue {
b = true
} else if bd == cborBdFalse {
} else {
2018-02-11 00:29:52 +00:00
d.d.errorf("not bool - %s %x/%s", msgBadDesc, d.bd, cbordesc(d.bd))
2015-06-29 21:50:55 +00:00
return
}
d.bdRead = false
return
}
func (d *cborDecDriver) 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
if d.bd == cborBdIndefiniteMap {
return -1
}
return d.decLen()
}
func (d *cborDecDriver) 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
if d.bd == cborBdIndefiniteArray {
return -1
}
return d.decLen()
}
func (d *cborDecDriver) decLen() int {
return int(d.decUint())
}
func (d *cborDecDriver) decAppendIndefiniteBytes(bs []byte) []byte {
d.bdRead = false
for {
if d.CheckBreak() {
break
}
if major := d.bd >> 5; major != cborMajorBytes && major != cborMajorText {
2018-01-26 23:51:00 +00:00
d.d.errorf("expect bytes/string major type in indefinite string/bytes;"+
2018-02-11 00:29:52 +00:00
" got major %v from descriptor %x/%x", major, d.bd, cbordesc(d.bd))
2015-06-29 21:50:55 +00:00
return nil
}
n := d.decLen()
oldLen := len(bs)
newLen := oldLen + n
if newLen > cap(bs) {
bs2 := make([]byte, newLen, 2*cap(bs)+n)
copy(bs2, bs)
bs = bs2
} else {
bs = bs[:newLen]
}
d.r.readb(bs[oldLen:newLen])
// bs = append(bs, d.r.readn()...)
d.bdRead = false
}
d.bdRead = false
return bs
}
2017-10-27 19:06:04 +00:00
func (d *cborDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
if d.bd == cborBdNil || d.bd == cborBdUndefined {
d.bdRead = false
return nil
}
if d.bd == cborBdIndefiniteBytes || d.bd == cborBdIndefiniteString {
2017-10-27 19:06:04 +00:00
d.bdRead = false
2015-06-29 21:50:55 +00:00
if bs == nil {
2018-01-26 23:51:00 +00:00
if zerocopy {
return d.decAppendIndefiniteBytes(d.d.b[:0])
}
2017-10-27 19:06:04 +00:00
return d.decAppendIndefiniteBytes(zeroByteSlice)
2015-06-29 21:50:55 +00:00
}
return d.decAppendIndefiniteBytes(bs[:0])
}
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 == cborBdIndefiniteArray || (d.bd >= cborBaseArray && d.bd < cborBaseMap) {
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(uint(clen))
2015-06-29 21:50:55 +00:00
} 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
}
}
2018-01-26 23:51:00 +00:00
return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
2015-06-29 21:50:55 +00:00
}
func (d *cborDecDriver) DecodeString() (s string) {
2018-01-26 23:51:00 +00:00
return string(d.DecodeBytes(d.d.b[:], true))
2017-10-27 19:06:04 +00:00
}
func (d *cborDecDriver) DecodeStringAsBytes() (s []byte) {
2018-01-26 23:51:00 +00:00
return d.DecodeBytes(d.d.b[:], true)
}
func (d *cborDecDriver) DecodeTime() (t time.Time) {
if !d.bdRead {
d.readNextBd()
}
if d.bd == cborBdNil || d.bd == cborBdUndefined {
d.bdRead = false
return
}
xtag := d.decUint()
d.bdRead = false
return d.decodeTime(xtag)
}
func (d *cborDecDriver) decodeTime(xtag uint64) (t time.Time) {
if !d.bdRead {
d.readNextBd()
}
switch xtag {
case 0:
var err error
if t, err = time.Parse(time.RFC3339, stringView(d.DecodeStringAsBytes())); err != nil {
d.d.errorv(err)
}
case 1:
// decode an int64 or a float, and infer time.Time from there.
// for floats, round to microseconds, as that is what is guaranteed to fit well.
switch {
case d.bd == cborBdFloat16, d.bd == cborBdFloat32:
f1, f2 := math.Modf(d.DecodeFloat64())
t = time.Unix(int64(f1), int64(f2*1e9))
case d.bd == cborBdFloat64:
f1, f2 := math.Modf(d.DecodeFloat64())
t = time.Unix(int64(f1), int64(f2*1e9))
case d.bd >= cborBaseUint && d.bd < cborBaseNegInt,
d.bd >= cborBaseNegInt && d.bd < cborBaseBytes:
t = time.Unix(d.DecodeInt64(), 0)
default:
d.d.errorf("time.Time can only be decoded from a number (or RFC3339 string)")
}
default:
d.d.errorf("invalid tag for time.Time - expecting 0 or 1, got 0x%x", xtag)
}
t = t.UTC().Round(time.Microsecond)
return
2015-06-29 21:50:55 +00:00
}
func (d *cborDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
if !d.bdRead {
d.readNextBd()
}
u := d.decUint()
d.bdRead = false
realxtag = u
if ext == nil {
re := rv.(*RawExt)
re.Tag = realxtag
d.d.decode(&re.Value)
} else if xtag != realxtag {
d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", realxtag, xtag)
return
} else {
var v interface{}
d.d.decode(&v)
ext.UpdateExt(rv, v)
}
d.bdRead = false
return
}
2015-10-30 22:07:00 +00:00
func (d *cborDecDriver) DecodeNaked() {
2015-06-29 21:50:55 +00:00
if !d.bdRead {
d.readNextBd()
}
n := d.d.naked()
2015-10-30 22:07:00 +00:00
var decodeFurther bool
2015-06-29 21:50:55 +00:00
switch d.bd {
case cborBdNil:
2015-10-30 22:07:00 +00:00
n.v = valueTypeNil
2015-06-29 21:50:55 +00:00
case cborBdFalse:
2015-10-30 22:07:00 +00:00
n.v = valueTypeBool
n.b = false
2015-06-29 21:50:55 +00:00
case cborBdTrue:
2015-10-30 22:07:00 +00:00
n.v = valueTypeBool
n.b = true
2018-01-26 23:51:00 +00:00
case cborBdFloat16, cborBdFloat32, cborBdFloat64:
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 cborBdIndefiniteBytes:
n.v = valueTypeBytes
n.l = d.DecodeBytes(nil, false)
2015-06-29 21:50:55 +00:00
case cborBdIndefiniteString:
2015-10-30 22:07:00 +00:00
n.v = valueTypeString
n.s = d.DecodeString()
2015-06-29 21:50:55 +00:00
case cborBdIndefiniteArray:
2015-10-30 22:07:00 +00:00
n.v = valueTypeArray
2015-06-29 21:50:55 +00:00
decodeFurther = true
case cborBdIndefiniteMap:
2015-10-30 22:07:00 +00:00
n.v = valueTypeMap
2015-06-29 21:50:55 +00:00
decodeFurther = true
default:
switch {
case d.bd >= cborBaseUint && d.bd < cborBaseNegInt:
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 d.bd >= cborBaseNegInt && d.bd < cborBaseBytes:
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 d.bd >= cborBaseBytes && d.bd < cborBaseString:
n.v = valueTypeBytes
n.l = d.DecodeBytes(nil, false)
2015-06-29 21:50:55 +00:00
case d.bd >= cborBaseString && d.bd < cborBaseArray:
2015-10-30 22:07:00 +00:00
n.v = valueTypeString
n.s = d.DecodeString()
2015-06-29 21:50:55 +00:00
case d.bd >= cborBaseArray && d.bd < cborBaseMap:
2015-10-30 22:07:00 +00:00
n.v = valueTypeArray
2015-06-29 21:50:55 +00:00
decodeFurther = true
case d.bd >= cborBaseMap && d.bd < cborBaseTag:
2015-10-30 22:07:00 +00:00
n.v = valueTypeMap
2015-06-29 21:50:55 +00:00
decodeFurther = true
case d.bd >= cborBaseTag && d.bd < cborBaseSimple:
2015-10-30 22:07:00 +00:00
n.v = valueTypeExt
n.u = d.decUint()
n.l = nil
2018-01-26 23:51:00 +00:00
if n.u == 0 || n.u == 1 {
d.bdRead = false
n.v = valueTypeTime
n.t = d.decodeTime(n.u)
}
2016-04-26 00:18:04 +00:00
// d.bdRead = false
2015-10-30 22:07:00 +00:00
// d.d.decode(&re.Value) // handled by decode itself.
2015-06-29 21:50:55 +00:00
// decodeFurther = true
default:
d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
return
}
}
if !decodeFurther {
d.bdRead = false
}
}
// -------------------------
// CborHandle is a Handle for the CBOR encoding format,
// defined at http://tools.ietf.org/html/rfc7049 and documented further at http://cbor.io .
//
// CBOR is comprehensively supported, including support for:
// - indefinite-length arrays/maps/bytes/strings
// - (extension) tags in range 0..0xffff (0 .. 65535)
// - half, single and double-precision floats
// - all numbers (1, 2, 4 and 8-byte signed and unsigned integers)
// - nil, true, false, ...
// - arrays and maps, bytes and text strings
//
// None of the optional extensions (with tags) defined in the spec are supported out-of-the-box.
// Users can implement them as needed (using SetExt), including spec-documented ones:
2018-01-26 23:51:00 +00:00
// - timestamp, BigNum, BigFloat, Decimals,
// - Encoded Text (e.g. URL, regexp, base64, MIME Message), etc.
2015-06-29 21:50:55 +00:00
type CborHandle struct {
binaryEncodingType
2017-10-27 19:06:04 +00:00
noElemSeparators
2015-10-30 22:07:00 +00:00
BasicHandle
2017-10-27 19:06:04 +00:00
// IndefiniteLength=true, means that we encode using indefinitelength
IndefiniteLength bool
2018-01-26 23:51:00 +00:00
// TimeRFC3339 says to encode time.Time using RFC3339 format.
// If unset, we encode time.Time using seconds past epoch.
TimeRFC3339 bool
// _ [1]uint64 // padding
2015-10-30 22:07:00 +00:00
}
2018-01-26 23:51:00 +00:00
// Name returns the name of the handle: cbor
func (h *CborHandle) Name() string { return "cbor" }
// SetInterfaceExt sets an extension
2015-10-30 22:07:00 +00:00
func (h *CborHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) {
2018-01-26 23:51:00 +00:00
return h.SetExt(rt, tag, &extWrapper{bytesExtFailer{}, ext})
2015-06-29 21:50:55 +00:00
}
func (h *CborHandle) newEncDriver(e *Encoder) encDriver {
return &cborEncDriver{e: e, w: e.w, h: h}
}
func (h *CborHandle) newDecDriver(d *Decoder) decDriver {
2017-03-31 00:03:13 +00:00
return &cborDecDriver{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 *cborEncDriver) reset() {
e.w = e.e.w
}
func (d *cborDecDriver) reset() {
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 = (*cborDecDriver)(nil)
var _ encDriver = (*cborEncDriver)(nil)