cbor.go 21 KB

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