github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/cbor.go (about)

     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.
     3  
     4  package codec
     5  
     6  import (
     7  	"math"
     8  	"reflect"
     9  	"time"
    10  	"unicode/utf8"
    11  )
    12  
    13  // major
    14  const (
    15  	cborMajorUint byte = iota
    16  	cborMajorNegInt
    17  	cborMajorBytes
    18  	cborMajorString
    19  	cborMajorArray
    20  	cborMajorMap
    21  	cborMajorTag
    22  	cborMajorSimpleOrFloat
    23  )
    24  
    25  // simple
    26  const (
    27  	cborBdFalse byte = 0xf4 + iota
    28  	cborBdTrue
    29  	cborBdNil
    30  	cborBdUndefined
    31  	cborBdExt
    32  	cborBdFloat16
    33  	cborBdFloat32
    34  	cborBdFloat64
    35  )
    36  
    37  // indefinite
    38  const (
    39  	cborBdIndefiniteBytes  byte = 0x5f
    40  	cborBdIndefiniteString byte = 0x7f
    41  	cborBdIndefiniteArray  byte = 0x9f
    42  	cborBdIndefiniteMap    byte = 0xbf
    43  	cborBdBreak            byte = 0xff
    44  )
    45  
    46  // These define some in-stream descriptors for
    47  // manual encoding e.g. when doing explicit indefinite-length
    48  const (
    49  	CborStreamBytes  byte = 0x5f
    50  	CborStreamString byte = 0x7f
    51  	CborStreamArray  byte = 0x9f
    52  	CborStreamMap    byte = 0xbf
    53  	CborStreamBreak  byte = 0xff
    54  )
    55  
    56  // base values
    57  const (
    58  	cborBaseUint   byte = 0x00
    59  	cborBaseNegInt byte = 0x20
    60  	cborBaseBytes  byte = 0x40
    61  	cborBaseString byte = 0x60
    62  	cborBaseArray  byte = 0x80
    63  	cborBaseMap    byte = 0xa0
    64  	cborBaseTag    byte = 0xc0
    65  	cborBaseSimple byte = 0xe0
    66  )
    67  
    68  // const (
    69  // 	cborSelfDesrTag  byte = 0xd9
    70  // 	cborSelfDesrTag2 byte = 0xd9
    71  // 	cborSelfDesrTag3 byte = 0xf7
    72  // )
    73  
    74  var (
    75  	cbordescSimpleNames = map[byte]string{
    76  		cborBdNil:     "nil",
    77  		cborBdFalse:   "false",
    78  		cborBdTrue:    "true",
    79  		cborBdFloat16: "float",
    80  		cborBdFloat32: "float",
    81  		cborBdFloat64: "float",
    82  		cborBdBreak:   "break",
    83  	}
    84  	cbordescIndefNames = map[byte]string{
    85  		cborBdIndefiniteBytes:  "bytes*",
    86  		cborBdIndefiniteString: "string*",
    87  		cborBdIndefiniteArray:  "array*",
    88  		cborBdIndefiniteMap:    "map*",
    89  	}
    90  	cbordescMajorNames = map[byte]string{
    91  		cborMajorUint:          "(u)int",
    92  		cborMajorNegInt:        "int",
    93  		cborMajorBytes:         "bytes",
    94  		cborMajorString:        "string",
    95  		cborMajorArray:         "array",
    96  		cborMajorMap:           "map",
    97  		cborMajorTag:           "tag",
    98  		cborMajorSimpleOrFloat: "simple",
    99  	}
   100  )
   101  
   102  func cbordesc(bd byte) (s string) {
   103  	bm := bd >> 5
   104  	if bm == cborMajorSimpleOrFloat {
   105  		s = cbordescSimpleNames[bd]
   106  	} else {
   107  		s = cbordescMajorNames[bm]
   108  		if s == "" {
   109  			s = cbordescIndefNames[bd]
   110  		}
   111  	}
   112  	if s == "" {
   113  		s = "unknown"
   114  	}
   115  	return
   116  }
   117  
   118  // -------------------
   119  
   120  type cborEncDriver struct {
   121  	noBuiltInTypes
   122  	encDriverNoState
   123  	encDriverNoopContainerWriter
   124  	h *CborHandle
   125  
   126  	// scratch buffer for: encode time, numbers, etc
   127  	//
   128  	// RFC3339Nano uses 35 chars: 2006-01-02T15:04:05.999999999Z07:00
   129  	b [40]byte
   130  
   131  	e Encoder
   132  }
   133  
   134  func (e *cborEncDriver) encoder() *Encoder {
   135  	return &e.e
   136  }
   137  
   138  func (e *cborEncDriver) EncodeNil() {
   139  	e.e.encWr.writen1(cborBdNil)
   140  }
   141  
   142  func (e *cborEncDriver) EncodeBool(b bool) {
   143  	if b {
   144  		e.e.encWr.writen1(cborBdTrue)
   145  	} else {
   146  		e.e.encWr.writen1(cborBdFalse)
   147  	}
   148  }
   149  
   150  func (e *cborEncDriver) EncodeFloat32(f float32) {
   151  	b := math.Float32bits(f)
   152  	if e.h.OptimumSize {
   153  		if h := floatToHalfFloatBits(b); halfFloatToFloatBits(h) == b {
   154  			e.e.encWr.writen1(cborBdFloat16)
   155  			bigen.writeUint16(e.e.w(), h)
   156  			return
   157  		}
   158  	}
   159  	e.e.encWr.writen1(cborBdFloat32)
   160  	bigen.writeUint32(e.e.w(), b)
   161  }
   162  
   163  func (e *cborEncDriver) EncodeFloat64(f float64) {
   164  	if e.h.OptimumSize {
   165  		if f32 := float32(f); float64(f32) == f {
   166  			e.EncodeFloat32(f32)
   167  			return
   168  		}
   169  	}
   170  	e.e.encWr.writen1(cborBdFloat64)
   171  	bigen.writeUint64(e.e.w(), math.Float64bits(f))
   172  }
   173  
   174  func (e *cborEncDriver) encUint(v uint64, bd byte) {
   175  	if v <= 0x17 {
   176  		e.e.encWr.writen1(byte(v) + bd)
   177  	} else if v <= math.MaxUint8 {
   178  		e.e.encWr.writen2(bd+0x18, uint8(v))
   179  	} else if v <= math.MaxUint16 {
   180  		e.e.encWr.writen1(bd + 0x19)
   181  		bigen.writeUint16(e.e.w(), uint16(v))
   182  	} else if v <= math.MaxUint32 {
   183  		e.e.encWr.writen1(bd + 0x1a)
   184  		bigen.writeUint32(e.e.w(), uint32(v))
   185  	} else { // if v <= math.MaxUint64 {
   186  		e.e.encWr.writen1(bd + 0x1b)
   187  		bigen.writeUint64(e.e.w(), v)
   188  	}
   189  }
   190  
   191  func (e *cborEncDriver) EncodeInt(v int64) {
   192  	if v < 0 {
   193  		e.encUint(uint64(-1-v), cborBaseNegInt)
   194  	} else {
   195  		e.encUint(uint64(v), cborBaseUint)
   196  	}
   197  }
   198  
   199  func (e *cborEncDriver) EncodeUint(v uint64) {
   200  	e.encUint(v, cborBaseUint)
   201  }
   202  
   203  func (e *cborEncDriver) encLen(bd byte, length int) {
   204  	e.encUint(uint64(length), bd)
   205  }
   206  
   207  func (e *cborEncDriver) EncodeTime(t time.Time) {
   208  	if t.IsZero() {
   209  		e.EncodeNil()
   210  	} else if e.h.TimeRFC3339 {
   211  		e.encUint(0, cborBaseTag)
   212  		e.encStringBytesS(cborBaseString, stringView(fmtTime(t, time.RFC3339Nano, e.b[:0])))
   213  	} else {
   214  		e.encUint(1, cborBaseTag)
   215  		t = t.UTC().Round(time.Microsecond)
   216  		sec, nsec := t.Unix(), uint64(t.Nanosecond())
   217  		if nsec == 0 {
   218  			e.EncodeInt(sec)
   219  		} else {
   220  			e.EncodeFloat64(float64(sec) + float64(nsec)/1e9)
   221  		}
   222  	}
   223  }
   224  
   225  func (e *cborEncDriver) EncodeExt(rv interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
   226  	e.encUint(uint64(xtag), cborBaseTag)
   227  	if ext == SelfExt {
   228  		e.e.encodeValue(baseRV(rv), e.h.fnNoExt(basetype))
   229  	} else if v := ext.ConvertExt(rv); v == nil {
   230  		e.EncodeNil()
   231  	} else {
   232  		e.e.encode(v)
   233  	}
   234  }
   235  
   236  func (e *cborEncDriver) EncodeRawExt(re *RawExt) {
   237  	e.encUint(uint64(re.Tag), cborBaseTag)
   238  	// only encodes re.Value (never re.Data)
   239  	if re.Value != nil {
   240  		e.e.encode(re.Value)
   241  	} else {
   242  		e.EncodeNil()
   243  	}
   244  }
   245  
   246  func (e *cborEncDriver) WriteArrayStart(length int) {
   247  	if e.h.IndefiniteLength {
   248  		e.e.encWr.writen1(cborBdIndefiniteArray)
   249  	} else {
   250  		e.encLen(cborBaseArray, length)
   251  	}
   252  }
   253  
   254  func (e *cborEncDriver) WriteMapStart(length int) {
   255  	if e.h.IndefiniteLength {
   256  		e.e.encWr.writen1(cborBdIndefiniteMap)
   257  	} else {
   258  		e.encLen(cborBaseMap, length)
   259  	}
   260  }
   261  
   262  func (e *cborEncDriver) WriteMapEnd() {
   263  	if e.h.IndefiniteLength {
   264  		e.e.encWr.writen1(cborBdBreak)
   265  	}
   266  }
   267  
   268  func (e *cborEncDriver) WriteArrayEnd() {
   269  	if e.h.IndefiniteLength {
   270  		e.e.encWr.writen1(cborBdBreak)
   271  	}
   272  }
   273  
   274  func (e *cborEncDriver) EncodeString(v string) {
   275  	bb := cborBaseString
   276  	if e.h.StringToRaw {
   277  		bb = cborBaseBytes
   278  	}
   279  	e.encStringBytesS(bb, v)
   280  }
   281  
   282  func (e *cborEncDriver) EncodeStringBytesRaw(v []byte) {
   283  	if v == nil {
   284  		e.EncodeNil()
   285  	} else {
   286  		e.encStringBytesS(cborBaseBytes, stringView(v))
   287  	}
   288  }
   289  
   290  func (e *cborEncDriver) encStringBytesS(bb byte, v string) {
   291  	if e.h.IndefiniteLength {
   292  		if bb == cborBaseBytes {
   293  			e.e.encWr.writen1(cborBdIndefiniteBytes)
   294  		} else {
   295  			e.e.encWr.writen1(cborBdIndefiniteString)
   296  		}
   297  		var vlen uint = uint(len(v))
   298  		blen := vlen / 4
   299  		if blen == 0 {
   300  			blen = 64
   301  		} else if blen > 1024 {
   302  			blen = 1024
   303  		}
   304  		for i := uint(0); i < vlen; {
   305  			var v2 string
   306  			i2 := i + blen
   307  			if i2 >= i && i2 < vlen {
   308  				v2 = v[i:i2]
   309  			} else {
   310  				v2 = v[i:]
   311  			}
   312  			e.encLen(bb, len(v2))
   313  			e.e.encWr.writestr(v2)
   314  			i = i2
   315  		}
   316  		e.e.encWr.writen1(cborBdBreak)
   317  	} else {
   318  		e.encLen(bb, len(v))
   319  		e.e.encWr.writestr(v)
   320  	}
   321  }
   322  
   323  // ----------------------
   324  
   325  type cborDecDriver struct {
   326  	decDriverNoopContainerReader
   327  	decDriverNoopNumberHelper
   328  	h *CborHandle
   329  	bdAndBdread
   330  	st bool // skip tags
   331  	_  bool // found nil
   332  	noBuiltInTypes
   333  	d Decoder
   334  }
   335  
   336  func (d *cborDecDriver) decoder() *Decoder {
   337  	return &d.d
   338  }
   339  
   340  func (d *cborDecDriver) descBd() string {
   341  	return sprintf("%v (%s)", d.bd, cbordesc(d.bd))
   342  }
   343  
   344  func (d *cborDecDriver) readNextBd() {
   345  	d.bd = d.d.decRd.readn1()
   346  	d.bdRead = true
   347  }
   348  
   349  func (d *cborDecDriver) advanceNil() (null bool) {
   350  	if !d.bdRead {
   351  		d.readNextBd()
   352  	}
   353  	if d.bd == cborBdNil || d.bd == cborBdUndefined {
   354  		d.bdRead = false
   355  		return true // null = true
   356  	}
   357  	return
   358  }
   359  
   360  func (d *cborDecDriver) TryNil() bool {
   361  	return d.advanceNil()
   362  }
   363  
   364  // skipTags is called to skip any tags in the stream.
   365  //
   366  // Since any value can be tagged, then we should call skipTags
   367  // before any value is decoded.
   368  //
   369  // By definition, skipTags should not be called before
   370  // checking for break, or nil or undefined.
   371  func (d *cborDecDriver) skipTags() {
   372  	for d.bd>>5 == cborMajorTag {
   373  		d.decUint()
   374  		d.bd = d.d.decRd.readn1()
   375  	}
   376  }
   377  
   378  func (d *cborDecDriver) ContainerType() (vt valueType) {
   379  	if !d.bdRead {
   380  		d.readNextBd()
   381  	}
   382  	if d.st {
   383  		d.skipTags()
   384  	}
   385  	if d.bd == cborBdNil {
   386  		d.bdRead = false // always consume nil after seeing it in container type
   387  		return valueTypeNil
   388  	}
   389  	major := d.bd >> 5
   390  	if major == cborMajorBytes {
   391  		return valueTypeBytes
   392  	} else if major == cborMajorString {
   393  		return valueTypeString
   394  	} else if major == cborMajorArray {
   395  		return valueTypeArray
   396  	} else if major == cborMajorMap {
   397  		return valueTypeMap
   398  	}
   399  	return valueTypeUnset
   400  }
   401  
   402  func (d *cborDecDriver) CheckBreak() (v bool) {
   403  	if !d.bdRead {
   404  		d.readNextBd()
   405  	}
   406  	if d.bd == cborBdBreak {
   407  		d.bdRead = false
   408  		v = true
   409  	}
   410  	return
   411  }
   412  
   413  func (d *cborDecDriver) decUint() (ui uint64) {
   414  	v := d.bd & 0x1f
   415  	if v <= 0x17 {
   416  		ui = uint64(v)
   417  	} else if v == 0x18 {
   418  		ui = uint64(d.d.decRd.readn1())
   419  	} else if v == 0x19 {
   420  		ui = uint64(bigen.Uint16(d.d.decRd.readn2()))
   421  	} else if v == 0x1a {
   422  		ui = uint64(bigen.Uint32(d.d.decRd.readn4()))
   423  	} else if v == 0x1b {
   424  		ui = uint64(bigen.Uint64(d.d.decRd.readn8()))
   425  	} else {
   426  		d.d.errorf("invalid descriptor decoding uint: %x/%s", d.bd, cbordesc(d.bd))
   427  	}
   428  	return
   429  }
   430  
   431  func (d *cborDecDriver) decLen() int {
   432  	return int(d.decUint())
   433  }
   434  
   435  func (d *cborDecDriver) decAppendIndefiniteBytes(bs []byte, major byte) []byte {
   436  	d.bdRead = false
   437  	for !d.CheckBreak() {
   438  		chunkMajor := d.bd >> 5
   439  		if chunkMajor != major {
   440  			d.d.errorf("malformed indefinite string/bytes %x (%s); contains chunk with major type %v, expected %v",
   441  				d.bd, cbordesc(d.bd), chunkMajor, major)
   442  		}
   443  		n := uint(d.decLen())
   444  		oldLen := uint(len(bs))
   445  		newLen := oldLen + n
   446  		if newLen > uint(cap(bs)) {
   447  			bs2 := make([]byte, newLen, 2*uint(cap(bs))+n)
   448  			copy(bs2, bs)
   449  			bs = bs2
   450  		} else {
   451  			bs = bs[:newLen]
   452  		}
   453  		d.d.decRd.readb(bs[oldLen:newLen])
   454  		if d.h.ValidateUnicode && major == cborMajorString && !utf8.Valid(bs[oldLen:newLen]) {
   455  			d.d.errorf("indefinite-length text string contains chunk that is not a valid utf-8 sequence: 0x%x", bs[oldLen:newLen])
   456  		}
   457  		d.bdRead = false
   458  	}
   459  	d.bdRead = false
   460  	return bs
   461  }
   462  
   463  func (d *cborDecDriver) decFloat() (f float64, ok bool) {
   464  	ok = true
   465  	switch d.bd {
   466  	case cborBdFloat16:
   467  		f = float64(math.Float32frombits(halfFloatToFloatBits(bigen.Uint16(d.d.decRd.readn2()))))
   468  	case cborBdFloat32:
   469  		f = float64(math.Float32frombits(bigen.Uint32(d.d.decRd.readn4())))
   470  	case cborBdFloat64:
   471  		f = math.Float64frombits(bigen.Uint64(d.d.decRd.readn8()))
   472  	default:
   473  		ok = false
   474  	}
   475  	return
   476  }
   477  
   478  func (d *cborDecDriver) decInteger() (ui uint64, neg, ok bool) {
   479  	ok = true
   480  	switch d.bd >> 5 {
   481  	case cborMajorUint:
   482  		ui = d.decUint()
   483  	case cborMajorNegInt:
   484  		ui = d.decUint()
   485  		neg = true
   486  	default:
   487  		ok = false
   488  	}
   489  	return
   490  }
   491  
   492  func (d *cborDecDriver) DecodeInt64() (i int64) {
   493  	if d.advanceNil() {
   494  		return
   495  	}
   496  	if d.st {
   497  		d.skipTags()
   498  	}
   499  	i = decNegintPosintFloatNumberHelper{&d.d}.int64(d.decInteger())
   500  	d.bdRead = false
   501  	return
   502  }
   503  
   504  func (d *cborDecDriver) DecodeUint64() (ui uint64) {
   505  	if d.advanceNil() {
   506  		return
   507  	}
   508  	if d.st {
   509  		d.skipTags()
   510  	}
   511  	ui = decNegintPosintFloatNumberHelper{&d.d}.uint64(d.decInteger())
   512  	d.bdRead = false
   513  	return
   514  }
   515  
   516  func (d *cborDecDriver) DecodeFloat64() (f float64) {
   517  	if d.advanceNil() {
   518  		return
   519  	}
   520  	if d.st {
   521  		d.skipTags()
   522  	}
   523  	f = decNegintPosintFloatNumberHelper{&d.d}.float64(d.decFloat())
   524  	d.bdRead = false
   525  	return
   526  }
   527  
   528  // bool can be decoded from bool only (single byte).
   529  func (d *cborDecDriver) DecodeBool() (b bool) {
   530  	if d.advanceNil() {
   531  		return
   532  	}
   533  	if d.st {
   534  		d.skipTags()
   535  	}
   536  	if d.bd == cborBdTrue {
   537  		b = true
   538  	} else if d.bd == cborBdFalse {
   539  	} else {
   540  		d.d.errorf("not bool - %s %x/%s", msgBadDesc, d.bd, cbordesc(d.bd))
   541  	}
   542  	d.bdRead = false
   543  	return
   544  }
   545  
   546  func (d *cborDecDriver) ReadMapStart() (length int) {
   547  	if d.advanceNil() {
   548  		return containerLenNil
   549  	}
   550  	if d.st {
   551  		d.skipTags()
   552  	}
   553  	d.bdRead = false
   554  	if d.bd == cborBdIndefiniteMap {
   555  		return containerLenUnknown
   556  	}
   557  	if d.bd>>5 != cborMajorMap {
   558  		d.d.errorf("error reading map; got major type: %x, expected %x/%s", d.bd>>5, cborMajorMap, cbordesc(d.bd))
   559  	}
   560  	return d.decLen()
   561  }
   562  
   563  func (d *cborDecDriver) ReadArrayStart() (length int) {
   564  	if d.advanceNil() {
   565  		return containerLenNil
   566  	}
   567  	if d.st {
   568  		d.skipTags()
   569  	}
   570  	d.bdRead = false
   571  	if d.bd == cborBdIndefiniteArray {
   572  		return containerLenUnknown
   573  	}
   574  	if d.bd>>5 != cborMajorArray {
   575  		d.d.errorf("invalid array; got major type: %x, expect: %x/%s", d.bd>>5, cborMajorArray, cbordesc(d.bd))
   576  	}
   577  	return d.decLen()
   578  }
   579  
   580  func (d *cborDecDriver) DecodeBytes(bs []byte) (bsOut []byte) {
   581  	d.d.decByteState = decByteStateNone
   582  	if d.advanceNil() {
   583  		return
   584  	}
   585  	if d.st {
   586  		d.skipTags()
   587  	}
   588  	if d.bd == cborBdIndefiniteBytes || d.bd == cborBdIndefiniteString {
   589  		d.bdRead = false
   590  		if bs == nil {
   591  			d.d.decByteState = decByteStateReuseBuf
   592  			return d.decAppendIndefiniteBytes(d.d.b[:0], d.bd>>5)
   593  		}
   594  		return d.decAppendIndefiniteBytes(bs[:0], d.bd>>5)
   595  	}
   596  	if d.bd == cborBdIndefiniteArray {
   597  		d.bdRead = false
   598  		if bs == nil {
   599  			d.d.decByteState = decByteStateReuseBuf
   600  			bs = d.d.b[:0]
   601  		} else {
   602  			bs = bs[:0]
   603  		}
   604  		for !d.CheckBreak() {
   605  			bs = append(bs, uint8(chkOvf.UintV(d.DecodeUint64(), 8)))
   606  		}
   607  		return bs
   608  	}
   609  	if d.bd>>5 == cborMajorArray {
   610  		d.bdRead = false
   611  		if bs == nil {
   612  			d.d.decByteState = decByteStateReuseBuf
   613  			bs = d.d.b[:]
   614  		}
   615  		slen := d.decLen()
   616  		var changed bool
   617  		if bs, changed = usableByteSlice(bs, slen); changed {
   618  			d.d.decByteState = decByteStateNone
   619  		}
   620  		for i := 0; i < len(bs); i++ {
   621  			bs[i] = uint8(chkOvf.UintV(d.DecodeUint64(), 8))
   622  		}
   623  		for i := len(bs); i < slen; i++ {
   624  			bs = append(bs, uint8(chkOvf.UintV(d.DecodeUint64(), 8)))
   625  		}
   626  		return bs
   627  	}
   628  	clen := d.decLen()
   629  	d.bdRead = false
   630  	if d.d.zerocopy() {
   631  		d.d.decByteState = decByteStateZerocopy
   632  		return d.d.decRd.rb.readx(uint(clen))
   633  	}
   634  	if bs == nil {
   635  		d.d.decByteState = decByteStateReuseBuf
   636  		bs = d.d.b[:]
   637  	}
   638  	return decByteSlice(d.d.r(), clen, d.h.MaxInitLen, bs)
   639  }
   640  
   641  func (d *cborDecDriver) DecodeStringAsBytes() (s []byte) {
   642  	s = d.DecodeBytes(nil)
   643  	if d.h.ValidateUnicode && !utf8.Valid(s) {
   644  		d.d.errorf("DecodeStringAsBytes: invalid UTF-8: %s", s)
   645  	}
   646  	return
   647  }
   648  
   649  func (d *cborDecDriver) DecodeTime() (t time.Time) {
   650  	if d.advanceNil() {
   651  		return
   652  	}
   653  	if d.bd>>5 != cborMajorTag {
   654  		d.d.errorf("error reading tag; expected major type: %x, got: %x", cborMajorTag, d.bd>>5)
   655  	}
   656  	xtag := d.decUint()
   657  	d.bdRead = false
   658  	return d.decodeTime(xtag)
   659  }
   660  
   661  func (d *cborDecDriver) decodeTime(xtag uint64) (t time.Time) {
   662  	switch xtag {
   663  	case 0:
   664  		var err error
   665  		t, err = time.Parse(time.RFC3339, stringView(d.DecodeStringAsBytes()))
   666  		d.d.onerror(err)
   667  	case 1:
   668  		f1, f2 := math.Modf(d.DecodeFloat64())
   669  		t = time.Unix(int64(f1), int64(f2*1e9))
   670  	default:
   671  		d.d.errorf("invalid tag for time.Time - expecting 0 or 1, got 0x%x", xtag)
   672  	}
   673  	t = t.UTC().Round(time.Microsecond)
   674  	return
   675  }
   676  
   677  func (d *cborDecDriver) DecodeExt(rv interface{}, basetype reflect.Type, xtag uint64, ext Ext) {
   678  	if d.advanceNil() {
   679  		return
   680  	}
   681  	if d.bd>>5 != cborMajorTag {
   682  		d.d.errorf("error reading tag; expected major type: %x, got: %x", cborMajorTag, d.bd>>5)
   683  	}
   684  	realxtag := d.decUint()
   685  	d.bdRead = false
   686  	if ext == nil {
   687  		re := rv.(*RawExt)
   688  		re.Tag = realxtag
   689  		d.d.decode(&re.Value)
   690  	} else if xtag != realxtag {
   691  		d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", realxtag, xtag)
   692  	} else if ext == SelfExt {
   693  		d.d.decodeValue(baseRV(rv), d.h.fnNoExt(basetype))
   694  	} else {
   695  		d.d.interfaceExtConvertAndDecode(rv, ext)
   696  	}
   697  	d.bdRead = false
   698  }
   699  
   700  func (d *cborDecDriver) DecodeNaked() {
   701  	if !d.bdRead {
   702  		d.readNextBd()
   703  	}
   704  
   705  	n := d.d.naked()
   706  	var decodeFurther bool
   707  
   708  	switch d.bd >> 5 {
   709  	case cborMajorUint:
   710  		if d.h.SignedInteger {
   711  			n.v = valueTypeInt
   712  			n.i = d.DecodeInt64()
   713  		} else {
   714  			n.v = valueTypeUint
   715  			n.u = d.DecodeUint64()
   716  		}
   717  	case cborMajorNegInt:
   718  		n.v = valueTypeInt
   719  		n.i = d.DecodeInt64()
   720  	case cborMajorBytes:
   721  		d.d.fauxUnionReadRawBytes(false)
   722  	case cborMajorString:
   723  		n.v = valueTypeString
   724  		n.s = d.d.stringZC(d.DecodeStringAsBytes())
   725  	case cborMajorArray:
   726  		n.v = valueTypeArray
   727  		decodeFurther = true
   728  	case cborMajorMap:
   729  		n.v = valueTypeMap
   730  		decodeFurther = true
   731  	case cborMajorTag:
   732  		n.v = valueTypeExt
   733  		n.u = d.decUint()
   734  		n.l = nil
   735  		if n.u == 0 || n.u == 1 {
   736  			d.bdRead = false
   737  			n.v = valueTypeTime
   738  			n.t = d.decodeTime(n.u)
   739  		} else if d.st && d.h.getExtForTag(n.u) == nil {
   740  			// d.skipTags() // no need to call this - tags already skipped
   741  			d.bdRead = false
   742  			d.DecodeNaked()
   743  			return // return when done (as true recursive function)
   744  		}
   745  	case cborMajorSimpleOrFloat:
   746  		switch d.bd {
   747  		case cborBdNil, cborBdUndefined:
   748  			n.v = valueTypeNil
   749  		case cborBdFalse:
   750  			n.v = valueTypeBool
   751  			n.b = false
   752  		case cborBdTrue:
   753  			n.v = valueTypeBool
   754  			n.b = true
   755  		case cborBdFloat16, cborBdFloat32, cborBdFloat64:
   756  			n.v = valueTypeFloat
   757  			n.f = d.DecodeFloat64()
   758  		default:
   759  			d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
   760  		}
   761  	default: // should never happen
   762  		d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
   763  	}
   764  	if !decodeFurther {
   765  		d.bdRead = false
   766  	}
   767  }
   768  
   769  func (d *cborDecDriver) uintBytes() (v []byte, ui uint64) {
   770  	// this is only used by nextValueBytes, so it's ok to
   771  	// use readx and bigenstd here.
   772  	switch vv := d.bd & 0x1f; vv {
   773  	case 0x18:
   774  		v = d.d.decRd.readx(1)
   775  		ui = uint64(v[0])
   776  	case 0x19:
   777  		v = d.d.decRd.readx(2)
   778  		ui = uint64(bigenstd.Uint16(v))
   779  	case 0x1a:
   780  		v = d.d.decRd.readx(4)
   781  		ui = uint64(bigenstd.Uint32(v))
   782  	case 0x1b:
   783  		v = d.d.decRd.readx(8)
   784  		ui = uint64(bigenstd.Uint64(v))
   785  	default:
   786  		if vv > 0x1b {
   787  			d.d.errorf("invalid descriptor decoding uint: %x/%s", d.bd, cbordesc(d.bd))
   788  		}
   789  		ui = uint64(vv)
   790  	}
   791  	return
   792  }
   793  
   794  func (d *cborDecDriver) nextValueBytes(v0 []byte) (v []byte) {
   795  	if !d.bdRead {
   796  		d.readNextBd()
   797  	}
   798  	v = v0
   799  	var h = decNextValueBytesHelper{d: &d.d}
   800  	var cursor = d.d.rb.c - 1
   801  	h.append1(&v, d.bd)
   802  	v = d.nextValueBytesBdReadR(v)
   803  	d.bdRead = false
   804  	h.bytesRdV(&v, cursor)
   805  	return
   806  }
   807  
   808  func (d *cborDecDriver) nextValueBytesR(v0 []byte) (v []byte) {
   809  	d.readNextBd()
   810  	v = v0
   811  	var h = decNextValueBytesHelper{d: &d.d}
   812  	h.append1(&v, d.bd)
   813  	return d.nextValueBytesBdReadR(v)
   814  }
   815  
   816  func (d *cborDecDriver) nextValueBytesBdReadR(v0 []byte) (v []byte) {
   817  	v = v0
   818  	var h = decNextValueBytesHelper{d: &d.d}
   819  
   820  	var bs []byte
   821  	var ui uint64
   822  
   823  	switch d.bd >> 5 {
   824  	case cborMajorUint, cborMajorNegInt:
   825  		bs, _ = d.uintBytes()
   826  		h.appendN(&v, bs...)
   827  	case cborMajorString, cborMajorBytes:
   828  		if d.bd == cborBdIndefiniteBytes || d.bd == cborBdIndefiniteString {
   829  			for {
   830  				d.readNextBd()
   831  				h.append1(&v, d.bd)
   832  				if d.bd == cborBdBreak {
   833  					break
   834  				}
   835  				bs, ui = d.uintBytes()
   836  				h.appendN(&v, bs...)
   837  				h.appendN(&v, d.d.decRd.readx(uint(ui))...)
   838  			}
   839  		} else {
   840  			bs, ui = d.uintBytes()
   841  			h.appendN(&v, bs...)
   842  			h.appendN(&v, d.d.decRd.readx(uint(ui))...)
   843  		}
   844  	case cborMajorArray:
   845  		if d.bd == cborBdIndefiniteArray {
   846  			for {
   847  				d.readNextBd()
   848  				h.append1(&v, d.bd)
   849  				if d.bd == cborBdBreak {
   850  					break
   851  				}
   852  				v = d.nextValueBytesBdReadR(v)
   853  			}
   854  		} else {
   855  			bs, ui = d.uintBytes()
   856  			h.appendN(&v, bs...)
   857  			for i := uint64(0); i < ui; i++ {
   858  				v = d.nextValueBytesR(v)
   859  			}
   860  		}
   861  	case cborMajorMap:
   862  		if d.bd == cborBdIndefiniteMap {
   863  			for {
   864  				d.readNextBd()
   865  				h.append1(&v, d.bd)
   866  				if d.bd == cborBdBreak {
   867  					break
   868  				}
   869  				v = d.nextValueBytesBdReadR(v)
   870  				v = d.nextValueBytesR(v)
   871  			}
   872  		} else {
   873  			bs, ui = d.uintBytes()
   874  			h.appendN(&v, bs...)
   875  			for i := uint64(0); i < ui; i++ {
   876  				v = d.nextValueBytesR(v)
   877  				v = d.nextValueBytesR(v)
   878  			}
   879  		}
   880  	case cborMajorTag:
   881  		bs, _ = d.uintBytes()
   882  		h.appendN(&v, bs...)
   883  		v = d.nextValueBytesR(v)
   884  	case cborMajorSimpleOrFloat:
   885  		switch d.bd {
   886  		case cborBdNil, cborBdUndefined, cborBdFalse, cborBdTrue: // pass
   887  		case cborBdFloat16:
   888  			h.appendN(&v, d.d.decRd.readx(2)...)
   889  		case cborBdFloat32:
   890  			h.appendN(&v, d.d.decRd.readx(4)...)
   891  		case cborBdFloat64:
   892  			h.appendN(&v, d.d.decRd.readx(8)...)
   893  		default:
   894  			d.d.errorf("nextValueBytes: Unrecognized d.bd: 0x%x", d.bd)
   895  		}
   896  	default: // should never happen
   897  		d.d.errorf("nextValueBytes: Unrecognized d.bd: 0x%x", d.bd)
   898  	}
   899  	return
   900  }
   901  
   902  // -------------------------
   903  
   904  // CborHandle is a Handle for the CBOR encoding format,
   905  // defined at http://tools.ietf.org/html/rfc7049 and documented further at http://cbor.io .
   906  //
   907  // CBOR is comprehensively supported, including support for:
   908  //   - indefinite-length arrays/maps/bytes/strings
   909  //   - (extension) tags in range 0..0xffff (0 .. 65535)
   910  //   - half, single and double-precision floats
   911  //   - all numbers (1, 2, 4 and 8-byte signed and unsigned integers)
   912  //   - nil, true, false, ...
   913  //   - arrays and maps, bytes and text strings
   914  //
   915  // None of the optional extensions (with tags) defined in the spec are supported out-of-the-box.
   916  // Users can implement them as needed (using SetExt), including spec-documented ones:
   917  //   - timestamp, BigNum, BigFloat, Decimals,
   918  //   - Encoded Text (e.g. URL, regexp, base64, MIME Message), etc.
   919  type CborHandle struct {
   920  	binaryEncodingType
   921  	// noElemSeparators
   922  	BasicHandle
   923  
   924  	// IndefiniteLength=true, means that we encode using indefinitelength
   925  	IndefiniteLength bool
   926  
   927  	// TimeRFC3339 says to encode time.Time using RFC3339 format.
   928  	// If unset, we encode time.Time using seconds past epoch.
   929  	TimeRFC3339 bool
   930  
   931  	// SkipUnexpectedTags says to skip over any tags for which extensions are
   932  	// not defined. This is in keeping with the cbor spec on "Optional Tagging of Items".
   933  	//
   934  	// Furthermore, this allows the skipping over of the Self Describing Tag 0xd9d9f7.
   935  	SkipUnexpectedTags bool
   936  }
   937  
   938  // Name returns the name of the handle: cbor
   939  func (h *CborHandle) Name() string { return "cbor" }
   940  
   941  func (h *CborHandle) desc(bd byte) string { return cbordesc(bd) }
   942  
   943  func (h *CborHandle) newEncDriver() encDriver {
   944  	var e = &cborEncDriver{h: h}
   945  	e.e.e = e
   946  	e.e.init(h)
   947  	e.reset()
   948  	return e
   949  }
   950  
   951  func (h *CborHandle) newDecDriver() decDriver {
   952  	d := &cborDecDriver{h: h, st: h.SkipUnexpectedTags}
   953  	d.d.d = d
   954  	d.d.cbor = true
   955  	d.d.init(h)
   956  	d.reset()
   957  	return d
   958  }
   959  
   960  func (d *cborDecDriver) reset() {
   961  	d.bdAndBdread.reset()
   962  	d.st = d.h.SkipUnexpectedTags
   963  }
   964  
   965  var _ decDriver = (*cborDecDriver)(nil)
   966  var _ encDriver = (*cborEncDriver)(nil)