cbor.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "math"
  6. "reflect"
  7. "time"
  8. )
  9. const (
  10. cborMajorUint byte = iota
  11. cborMajorNegInt
  12. cborMajorBytes
  13. cborMajorText
  14. cborMajorArray
  15. cborMajorMap
  16. cborMajorTag
  17. cborMajorOther
  18. )
  19. const (
  20. cborBdFalse byte = 0xf4 + iota
  21. cborBdTrue
  22. cborBdNil
  23. cborBdUndefined
  24. cborBdExt
  25. cborBdFloat16
  26. cborBdFloat32
  27. cborBdFloat64
  28. )
  29. const (
  30. cborBdIndefiniteBytes byte = 0x5f
  31. cborBdIndefiniteString byte = 0x7f
  32. cborBdIndefiniteArray byte = 0x9f
  33. cborBdIndefiniteMap byte = 0xbf
  34. cborBdBreak byte = 0xff
  35. )
  36. // These define some in-stream descriptors for
  37. // manual encoding e.g. when doing explicit indefinite-length
  38. const (
  39. CborStreamBytes byte = 0x5f
  40. CborStreamString byte = 0x7f
  41. CborStreamArray byte = 0x9f
  42. CborStreamMap byte = 0xbf
  43. CborStreamBreak byte = 0xff
  44. )
  45. const (
  46. cborBaseUint byte = 0x00
  47. cborBaseNegInt byte = 0x20
  48. cborBaseBytes byte = 0x40
  49. cborBaseString byte = 0x60
  50. cborBaseArray byte = 0x80
  51. cborBaseMap byte = 0xa0
  52. cborBaseTag byte = 0xc0
  53. cborBaseSimple byte = 0xe0
  54. )
  55. func cbordesc(bd byte) string {
  56. switch bd {
  57. case cborBdNil:
  58. return "nil"
  59. case cborBdFalse:
  60. return "false"
  61. case cborBdTrue:
  62. return "true"
  63. case cborBdFloat16, cborBdFloat32, cborBdFloat64:
  64. return "float"
  65. case cborBdIndefiniteBytes:
  66. return "bytes*"
  67. case cborBdIndefiniteString:
  68. return "string*"
  69. case cborBdIndefiniteArray:
  70. return "array*"
  71. case cborBdIndefiniteMap:
  72. return "map*"
  73. default:
  74. switch {
  75. case bd >= cborBaseUint && bd < cborBaseNegInt:
  76. return "(u)int"
  77. case bd >= cborBaseNegInt && bd < cborBaseBytes:
  78. return "int"
  79. case bd >= cborBaseBytes && bd < cborBaseString:
  80. return "bytes"
  81. case bd >= cborBaseString && bd < cborBaseArray:
  82. return "string"
  83. case bd >= cborBaseArray && bd < cborBaseMap:
  84. return "array"
  85. case bd >= cborBaseMap && bd < cborBaseTag:
  86. return "map"
  87. case bd >= cborBaseTag && bd < cborBaseSimple:
  88. return "ext"
  89. default:
  90. return "unknown"
  91. }
  92. }
  93. }
  94. // -------------------
  95. type cborEncDriver struct {
  96. noBuiltInTypes
  97. encDriverNoopContainerWriter
  98. e *Encoder
  99. w *encWriterSwitch
  100. h *CborHandle
  101. x [8]byte
  102. // _ [3]uint64 // padding
  103. }
  104. func (e *cborEncDriver) EncodeNil() {
  105. e.w.writen1(cborBdNil)
  106. }
  107. func (e *cborEncDriver) EncodeBool(b bool) {
  108. if b {
  109. e.w.writen1(cborBdTrue)
  110. } else {
  111. e.w.writen1(cborBdFalse)
  112. }
  113. }
  114. func (e *cborEncDriver) EncodeFloat32(f float32) {
  115. e.w.writen1(cborBdFloat32)
  116. bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f))
  117. }
  118. func (e *cborEncDriver) EncodeFloat64(f float64) {
  119. e.w.writen1(cborBdFloat64)
  120. bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f))
  121. }
  122. func (e *cborEncDriver) encUint(v uint64, bd byte) {
  123. if v <= 0x17 {
  124. e.w.writen1(byte(v) + bd)
  125. } else if v <= math.MaxUint8 {
  126. e.w.writen2(bd+0x18, uint8(v))
  127. } else if v <= math.MaxUint16 {
  128. e.w.writen1(bd + 0x19)
  129. bigenHelper{e.x[:2], e.w}.writeUint16(uint16(v))
  130. } else if v <= math.MaxUint32 {
  131. e.w.writen1(bd + 0x1a)
  132. bigenHelper{e.x[:4], e.w}.writeUint32(uint32(v))
  133. } else { // if v <= math.MaxUint64 {
  134. e.w.writen1(bd + 0x1b)
  135. bigenHelper{e.x[:8], e.w}.writeUint64(v)
  136. }
  137. }
  138. func (e *cborEncDriver) EncodeInt(v int64) {
  139. if v < 0 {
  140. e.encUint(uint64(-1-v), cborBaseNegInt)
  141. } else {
  142. e.encUint(uint64(v), cborBaseUint)
  143. }
  144. }
  145. func (e *cborEncDriver) EncodeUint(v uint64) {
  146. e.encUint(v, cborBaseUint)
  147. }
  148. func (e *cborEncDriver) encLen(bd byte, length int) {
  149. e.encUint(uint64(length), bd)
  150. }
  151. func (e *cborEncDriver) EncodeTime(t time.Time) {
  152. if t.IsZero() {
  153. e.EncodeNil()
  154. } else if e.h.TimeRFC3339 {
  155. e.encUint(0, cborBaseTag)
  156. e.EncodeStringEnc(cUTF8, t.Format(time.RFC3339Nano))
  157. } else {
  158. e.encUint(1, cborBaseTag)
  159. t = t.UTC().Round(time.Microsecond)
  160. sec, nsec := t.Unix(), uint64(t.Nanosecond())
  161. if nsec == 0 {
  162. e.EncodeInt(sec)
  163. } else {
  164. e.EncodeFloat64(float64(sec) + float64(nsec)/1e9)
  165. }
  166. }
  167. }
  168. func (e *cborEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, en *Encoder) {
  169. e.encUint(uint64(xtag), cborBaseTag)
  170. if v := ext.ConvertExt(rv); v == nil {
  171. e.EncodeNil()
  172. } else {
  173. en.encode(v)
  174. }
  175. }
  176. func (e *cborEncDriver) EncodeRawExt(re *RawExt, en *Encoder) {
  177. e.encUint(uint64(re.Tag), cborBaseTag)
  178. // only encodes re.Value (never re.Data)
  179. // if false && re.Data != nil {
  180. // en.encode(re.Data)
  181. // } else if re.Value != nil {
  182. if re.Value != nil {
  183. en.encode(re.Value)
  184. } else {
  185. e.EncodeNil()
  186. }
  187. }
  188. func (e *cborEncDriver) WriteArrayStart(length int) {
  189. if e.h.IndefiniteLength {
  190. e.w.writen1(cborBdIndefiniteArray)
  191. } else {
  192. e.encLen(cborBaseArray, length)
  193. }
  194. }
  195. func (e *cborEncDriver) WriteMapStart(length int) {
  196. if e.h.IndefiniteLength {
  197. e.w.writen1(cborBdIndefiniteMap)
  198. } else {
  199. e.encLen(cborBaseMap, length)
  200. }
  201. }
  202. func (e *cborEncDriver) WriteMapEnd() {
  203. if e.h.IndefiniteLength {
  204. e.w.writen1(cborBdBreak)
  205. }
  206. }
  207. func (e *cborEncDriver) WriteArrayEnd() {
  208. if e.h.IndefiniteLength {
  209. e.w.writen1(cborBdBreak)
  210. }
  211. }
  212. func (e *cborEncDriver) EncodeStringEnc(c charEncoding, v string) {
  213. e.encStringBytesS(cborBaseString, v)
  214. }
  215. func (e *cborEncDriver) EncodeStringBytesRaw(v []byte) {
  216. if v == nil {
  217. e.EncodeNil()
  218. } else {
  219. e.encStringBytesS(cborBaseBytes, stringView(v))
  220. }
  221. }
  222. func (e *cborEncDriver) encStringBytesS(bb byte, v string) {
  223. if e.h.IndefiniteLength {
  224. if bb == cborBaseBytes {
  225. e.w.writen1(cborBdIndefiniteBytes)
  226. } else {
  227. e.w.writen1(cborBdIndefiniteString)
  228. }
  229. var vlen uint = uint(len(v))
  230. blen := vlen / 4
  231. if blen == 0 {
  232. blen = 64
  233. } else if blen > 1024 {
  234. blen = 1024
  235. }
  236. for i := uint(0); i < vlen; {
  237. var v2 string
  238. i2 := i + blen
  239. if i2 < vlen {
  240. v2 = v[i:i2]
  241. } else {
  242. v2 = v[i:]
  243. }
  244. e.encLen(bb, len(v2))
  245. e.w.writestr(v2)
  246. i = i2
  247. }
  248. e.w.writen1(cborBdBreak)
  249. } else {
  250. e.encLen(bb, len(v))
  251. e.w.writestr(v)
  252. }
  253. }
  254. // ----------------------
  255. type cborDecDriver struct {
  256. d *Decoder
  257. h *CborHandle
  258. r *decReaderSwitch
  259. br bool // bytes reader
  260. bdRead bool
  261. bd byte
  262. noBuiltInTypes
  263. // decNoSeparator
  264. decDriverNoopContainerReader
  265. // _ [3]uint64 // padding
  266. }
  267. func (d *cborDecDriver) readNextBd() {
  268. d.bd = d.r.readn1()
  269. d.bdRead = true
  270. }
  271. func (d *cborDecDriver) uncacheRead() {
  272. if d.bdRead {
  273. d.r.unreadn1()
  274. d.bdRead = false
  275. }
  276. }
  277. func (d *cborDecDriver) ContainerType() (vt valueType) {
  278. if !d.bdRead {
  279. d.readNextBd()
  280. }
  281. if d.bd == cborBdNil {
  282. return valueTypeNil
  283. } else if d.bd == cborBdIndefiniteBytes || (d.bd >= cborBaseBytes && d.bd < cborBaseString) {
  284. return valueTypeBytes
  285. } else if d.bd == cborBdIndefiniteString || (d.bd >= cborBaseString && d.bd < cborBaseArray) {
  286. return valueTypeString
  287. } else if d.bd == cborBdIndefiniteArray || (d.bd >= cborBaseArray && d.bd < cborBaseMap) {
  288. return valueTypeArray
  289. } else if d.bd == cborBdIndefiniteMap || (d.bd >= cborBaseMap && d.bd < cborBaseTag) {
  290. return valueTypeMap
  291. }
  292. // else {
  293. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  294. // }
  295. return valueTypeUnset
  296. }
  297. func (d *cborDecDriver) TryDecodeAsNil() bool {
  298. if !d.bdRead {
  299. d.readNextBd()
  300. }
  301. // treat Nil and Undefined as nil values
  302. if d.bd == cborBdNil || d.bd == cborBdUndefined {
  303. d.bdRead = false
  304. return true
  305. }
  306. return false
  307. }
  308. func (d *cborDecDriver) CheckBreak() bool {
  309. if !d.bdRead {
  310. d.readNextBd()
  311. }
  312. if d.bd == cborBdBreak {
  313. d.bdRead = false
  314. return true
  315. }
  316. return false
  317. }
  318. func (d *cborDecDriver) decUint() (ui uint64) {
  319. v := d.bd & 0x1f
  320. if v <= 0x17 {
  321. ui = uint64(v)
  322. } else {
  323. if v == 0x18 {
  324. ui = uint64(d.r.readn1())
  325. } else if v == 0x19 {
  326. ui = uint64(bigen.Uint16(d.r.readx(2)))
  327. } else if v == 0x1a {
  328. ui = uint64(bigen.Uint32(d.r.readx(4)))
  329. } else if v == 0x1b {
  330. ui = uint64(bigen.Uint64(d.r.readx(8)))
  331. } else {
  332. d.d.errorf("invalid descriptor decoding uint: %x/%s", d.bd, cbordesc(d.bd))
  333. return
  334. }
  335. }
  336. return
  337. }
  338. func (d *cborDecDriver) decCheckInteger() (neg bool) {
  339. if !d.bdRead {
  340. d.readNextBd()
  341. }
  342. major := d.bd >> 5
  343. if major == cborMajorUint {
  344. } else if major == cborMajorNegInt {
  345. neg = true
  346. } else {
  347. d.d.errorf("not an integer - invalid major %v from descriptor %x/%s",
  348. major, d.bd, cbordesc(d.bd))
  349. return
  350. }
  351. return
  352. }
  353. func (d *cborDecDriver) DecodeInt64() (i int64) {
  354. neg := d.decCheckInteger()
  355. ui := d.decUint()
  356. // check if this number can be converted to an int without overflow
  357. if neg {
  358. i = -(chkOvf.SignedIntV(ui + 1))
  359. } else {
  360. i = chkOvf.SignedIntV(ui)
  361. }
  362. d.bdRead = false
  363. return
  364. }
  365. func (d *cborDecDriver) DecodeUint64() (ui uint64) {
  366. if d.decCheckInteger() {
  367. d.d.errorf("assigning negative signed value to unsigned type")
  368. return
  369. }
  370. ui = d.decUint()
  371. d.bdRead = false
  372. return
  373. }
  374. func (d *cborDecDriver) DecodeFloat64() (f float64) {
  375. if !d.bdRead {
  376. d.readNextBd()
  377. }
  378. if bd := d.bd; bd == cborBdFloat16 {
  379. f = float64(math.Float32frombits(halfFloatToFloatBits(bigen.Uint16(d.r.readx(2)))))
  380. } else if bd == cborBdFloat32 {
  381. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  382. } else if bd == cborBdFloat64 {
  383. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  384. } else if bd >= cborBaseUint && bd < cborBaseBytes {
  385. f = float64(d.DecodeInt64())
  386. } else {
  387. d.d.errorf("float only valid from float16/32/64 - invalid descriptor %x/%s", bd, cbordesc(bd))
  388. return
  389. }
  390. d.bdRead = false
  391. return
  392. }
  393. // bool can be decoded from bool only (single byte).
  394. func (d *cborDecDriver) DecodeBool() (b bool) {
  395. if !d.bdRead {
  396. d.readNextBd()
  397. }
  398. if bd := d.bd; bd == cborBdTrue {
  399. b = true
  400. } else if bd == cborBdFalse {
  401. } else {
  402. d.d.errorf("not bool - %s %x/%s", msgBadDesc, d.bd, cbordesc(d.bd))
  403. return
  404. }
  405. d.bdRead = false
  406. return
  407. }
  408. func (d *cborDecDriver) ReadMapStart() (length int) {
  409. if !d.bdRead {
  410. d.readNextBd()
  411. }
  412. d.bdRead = false
  413. if d.bd == cborBdIndefiniteMap {
  414. return -1
  415. }
  416. return d.decLen()
  417. }
  418. func (d *cborDecDriver) ReadArrayStart() (length int) {
  419. if !d.bdRead {
  420. d.readNextBd()
  421. }
  422. d.bdRead = false
  423. if d.bd == cborBdIndefiniteArray {
  424. return -1
  425. }
  426. return d.decLen()
  427. }
  428. func (d *cborDecDriver) decLen() int {
  429. return int(d.decUint())
  430. }
  431. func (d *cborDecDriver) decAppendIndefiniteBytes(bs []byte) []byte {
  432. d.bdRead = false
  433. for {
  434. if d.CheckBreak() {
  435. break
  436. }
  437. if major := d.bd >> 5; major != cborMajorBytes && major != cborMajorText {
  438. d.d.errorf("expect bytes/string major type in indefinite string/bytes;"+
  439. " got major %v from descriptor %x/%x", major, d.bd, cbordesc(d.bd))
  440. return nil
  441. }
  442. n := d.decLen()
  443. oldLen := len(bs)
  444. newLen := oldLen + n
  445. if newLen > cap(bs) {
  446. bs2 := make([]byte, newLen, 2*cap(bs)+n)
  447. copy(bs2, bs)
  448. bs = bs2
  449. } else {
  450. bs = bs[:newLen]
  451. }
  452. d.r.readb(bs[oldLen:newLen])
  453. // bs = append(bs, d.r.readn()...)
  454. d.bdRead = false
  455. }
  456. d.bdRead = false
  457. return bs
  458. }
  459. func (d *cborDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  460. if !d.bdRead {
  461. d.readNextBd()
  462. }
  463. if d.bd == cborBdNil || d.bd == cborBdUndefined {
  464. d.bdRead = false
  465. return nil
  466. }
  467. if d.bd == cborBdIndefiniteBytes || d.bd == cborBdIndefiniteString {
  468. d.bdRead = false
  469. if bs == nil {
  470. if zerocopy {
  471. return d.decAppendIndefiniteBytes(d.d.b[:0])
  472. }
  473. return d.decAppendIndefiniteBytes(zeroByteSlice)
  474. }
  475. return d.decAppendIndefiniteBytes(bs[:0])
  476. }
  477. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  478. if d.bd == cborBdIndefiniteArray || (d.bd >= cborBaseArray && d.bd < cborBaseMap) {
  479. bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  480. return
  481. }
  482. clen := d.decLen()
  483. d.bdRead = false
  484. if zerocopy {
  485. if d.br {
  486. return d.r.readx(uint(clen))
  487. } else if len(bs) == 0 {
  488. bs = d.d.b[:]
  489. }
  490. }
  491. return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
  492. }
  493. func (d *cborDecDriver) DecodeString() (s string) {
  494. return string(d.DecodeBytes(d.d.b[:], true))
  495. }
  496. func (d *cborDecDriver) DecodeStringAsBytes() (s []byte) {
  497. return d.DecodeBytes(d.d.b[:], true)
  498. }
  499. func (d *cborDecDriver) DecodeTime() (t time.Time) {
  500. if !d.bdRead {
  501. d.readNextBd()
  502. }
  503. if d.bd == cborBdNil || d.bd == cborBdUndefined {
  504. d.bdRead = false
  505. return
  506. }
  507. xtag := d.decUint()
  508. d.bdRead = false
  509. return d.decodeTime(xtag)
  510. }
  511. func (d *cborDecDriver) decodeTime(xtag uint64) (t time.Time) {
  512. if !d.bdRead {
  513. d.readNextBd()
  514. }
  515. switch xtag {
  516. case 0:
  517. var err error
  518. if t, err = time.Parse(time.RFC3339, stringView(d.DecodeStringAsBytes())); err != nil {
  519. d.d.errorv(err)
  520. }
  521. case 1:
  522. // decode an int64 or a float, and infer time.Time from there.
  523. // for floats, round to microseconds, as that is what is guaranteed to fit well.
  524. switch {
  525. case d.bd == cborBdFloat16, d.bd == cborBdFloat32:
  526. f1, f2 := math.Modf(d.DecodeFloat64())
  527. t = time.Unix(int64(f1), int64(f2*1e9))
  528. case d.bd == cborBdFloat64:
  529. f1, f2 := math.Modf(d.DecodeFloat64())
  530. t = time.Unix(int64(f1), int64(f2*1e9))
  531. case d.bd >= cborBaseUint && d.bd < cborBaseNegInt,
  532. d.bd >= cborBaseNegInt && d.bd < cborBaseBytes:
  533. t = time.Unix(d.DecodeInt64(), 0)
  534. default:
  535. d.d.errorf("time.Time can only be decoded from a number (or RFC3339 string)")
  536. }
  537. default:
  538. d.d.errorf("invalid tag for time.Time - expecting 0 or 1, got 0x%x", xtag)
  539. }
  540. t = t.UTC().Round(time.Microsecond)
  541. return
  542. }
  543. func (d *cborDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  544. if !d.bdRead {
  545. d.readNextBd()
  546. }
  547. u := d.decUint()
  548. d.bdRead = false
  549. realxtag = u
  550. if ext == nil {
  551. re := rv.(*RawExt)
  552. re.Tag = realxtag
  553. d.d.decode(&re.Value)
  554. } else if xtag != realxtag {
  555. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", realxtag, xtag)
  556. return
  557. } else {
  558. var v interface{}
  559. d.d.decode(&v)
  560. ext.UpdateExt(rv, v)
  561. }
  562. d.bdRead = false
  563. return
  564. }
  565. func (d *cborDecDriver) DecodeNaked() {
  566. if !d.bdRead {
  567. d.readNextBd()
  568. }
  569. n := d.d.naked()
  570. var decodeFurther bool
  571. switch d.bd {
  572. case cborBdNil:
  573. n.v = valueTypeNil
  574. case cborBdFalse:
  575. n.v = valueTypeBool
  576. n.b = false
  577. case cborBdTrue:
  578. n.v = valueTypeBool
  579. n.b = true
  580. case cborBdFloat16, cborBdFloat32, cborBdFloat64:
  581. n.v = valueTypeFloat
  582. n.f = d.DecodeFloat64()
  583. case cborBdIndefiniteBytes:
  584. decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
  585. case cborBdIndefiniteString:
  586. n.v = valueTypeString
  587. n.s = d.DecodeString()
  588. case cborBdIndefiniteArray:
  589. n.v = valueTypeArray
  590. decodeFurther = true
  591. case cborBdIndefiniteMap:
  592. n.v = valueTypeMap
  593. decodeFurther = true
  594. default:
  595. switch {
  596. case d.bd >= cborBaseUint && d.bd < cborBaseNegInt:
  597. if d.h.SignedInteger {
  598. n.v = valueTypeInt
  599. n.i = d.DecodeInt64()
  600. } else {
  601. n.v = valueTypeUint
  602. n.u = d.DecodeUint64()
  603. }
  604. case d.bd >= cborBaseNegInt && d.bd < cborBaseBytes:
  605. n.v = valueTypeInt
  606. n.i = d.DecodeInt64()
  607. case d.bd >= cborBaseBytes && d.bd < cborBaseString:
  608. decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
  609. case d.bd >= cborBaseString && d.bd < cborBaseArray:
  610. n.v = valueTypeString
  611. n.s = d.DecodeString()
  612. case d.bd >= cborBaseArray && d.bd < cborBaseMap:
  613. n.v = valueTypeArray
  614. decodeFurther = true
  615. case d.bd >= cborBaseMap && d.bd < cborBaseTag:
  616. n.v = valueTypeMap
  617. decodeFurther = true
  618. case d.bd >= cborBaseTag && d.bd < cborBaseSimple:
  619. n.v = valueTypeExt
  620. n.u = d.decUint()
  621. n.l = nil
  622. if n.u == 0 || n.u == 1 {
  623. d.bdRead = false
  624. n.v = valueTypeTime
  625. n.t = d.decodeTime(n.u)
  626. }
  627. // d.bdRead = false
  628. // d.d.decode(&re.Value) // handled by decode itself.
  629. // decodeFurther = true
  630. default:
  631. d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
  632. return
  633. }
  634. }
  635. if !decodeFurther {
  636. d.bdRead = false
  637. }
  638. }
  639. // -------------------------
  640. // CborHandle is a Handle for the CBOR encoding format,
  641. // defined at http://tools.ietf.org/html/rfc7049 and documented further at http://cbor.io .
  642. //
  643. // CBOR is comprehensively supported, including support for:
  644. // - indefinite-length arrays/maps/bytes/strings
  645. // - (extension) tags in range 0..0xffff (0 .. 65535)
  646. // - half, single and double-precision floats
  647. // - all numbers (1, 2, 4 and 8-byte signed and unsigned integers)
  648. // - nil, true, false, ...
  649. // - arrays and maps, bytes and text strings
  650. //
  651. // None of the optional extensions (with tags) defined in the spec are supported out-of-the-box.
  652. // Users can implement them as needed (using SetExt), including spec-documented ones:
  653. // - timestamp, BigNum, BigFloat, Decimals,
  654. // - Encoded Text (e.g. URL, regexp, base64, MIME Message), etc.
  655. type CborHandle struct {
  656. binaryEncodingType
  657. noElemSeparators
  658. BasicHandle
  659. // IndefiniteLength=true, means that we encode using indefinitelength
  660. IndefiniteLength bool
  661. // TimeRFC3339 says to encode time.Time using RFC3339 format.
  662. // If unset, we encode time.Time using seconds past epoch.
  663. TimeRFC3339 bool
  664. _ [1]uint64 // padding (cache-aligned)
  665. }
  666. // Name returns the name of the handle: cbor
  667. func (h *CborHandle) Name() string { return "cbor" }
  668. // SetInterfaceExt sets an extension
  669. func (h *CborHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) {
  670. return h.SetExt(rt, tag, &interfaceExtWrapper{InterfaceExt: ext})
  671. }
  672. func (h *CborHandle) newEncDriver(e *Encoder) encDriver {
  673. return &cborEncDriver{e: e, w: e.w(), h: h}
  674. }
  675. func (h *CborHandle) newDecDriver(d *Decoder) decDriver {
  676. return &cborDecDriver{d: d, h: h, r: d.r(), br: d.bytes}
  677. }
  678. func (e *cborEncDriver) reset() {
  679. e.w = e.e.w()
  680. }
  681. func (d *cborDecDriver) reset() {
  682. d.r, d.br = d.d.r(), d.d.bytes
  683. d.bd, d.bdRead = 0, false
  684. }
  685. var _ decDriver = (*cborDecDriver)(nil)
  686. var _ encDriver = (*cborEncDriver)(nil)