binc.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. import (
  5. "math"
  6. "reflect"
  7. // "reflect"
  8. // "sync/atomic"
  9. "time"
  10. //"fmt"
  11. )
  12. const bincDoPrune = true // No longer needed. Needed before as C lib did not support pruning.
  13. //var _ = fmt.Printf
  14. // vd as low 4 bits (there are 16 slots)
  15. const (
  16. bincVdSpecial byte = iota
  17. bincVdPosInt
  18. bincVdNegInt
  19. bincVdFloat
  20. bincVdString
  21. bincVdByteArray
  22. bincVdArray
  23. bincVdMap
  24. bincVdTimestamp
  25. bincVdSmallInt
  26. bincVdUnicodeOther
  27. bincVdSymbol
  28. bincVdDecimal
  29. _ // open slot
  30. _ // open slot
  31. bincVdCustomExt = 0x0f
  32. )
  33. const (
  34. bincSpNil byte = iota
  35. bincSpFalse
  36. bincSpTrue
  37. bincSpNan
  38. bincSpPosInf
  39. bincSpNegInf
  40. bincSpZeroFloat
  41. bincSpZero
  42. bincSpNegOne
  43. )
  44. const (
  45. bincFlBin16 byte = iota
  46. bincFlBin32
  47. _ // bincFlBin32e
  48. bincFlBin64
  49. _ // bincFlBin64e
  50. // others not currently supported
  51. )
  52. type bincEncDriver struct {
  53. w encWriter
  54. m map[string]uint16 // symbols
  55. s uint32 // symbols sequencer
  56. b [8]byte
  57. }
  58. func (e *bincEncDriver) isBuiltinType(rt uintptr) bool {
  59. return rt == timeTypId
  60. }
  61. func (e *bincEncDriver) encodeBuiltin(rt uintptr, v interface{}) {
  62. switch rt {
  63. case timeTypId:
  64. bs := encodeTime(v.(time.Time))
  65. e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
  66. e.w.writeb(bs)
  67. }
  68. }
  69. func (e *bincEncDriver) encodeNil() {
  70. e.w.writen1(bincVdSpecial<<4 | bincSpNil)
  71. }
  72. func (e *bincEncDriver) encodeBool(b bool) {
  73. if b {
  74. e.w.writen1(bincVdSpecial<<4 | bincSpTrue)
  75. } else {
  76. e.w.writen1(bincVdSpecial<<4 | bincSpFalse)
  77. }
  78. }
  79. func (e *bincEncDriver) encodeFloat32(f float32) {
  80. if f == 0 {
  81. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  82. return
  83. }
  84. e.w.writen1(bincVdFloat<<4 | bincFlBin32)
  85. e.w.writeUint32(math.Float32bits(f))
  86. }
  87. func (e *bincEncDriver) encodeFloat64(f float64) {
  88. if f == 0 {
  89. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  90. return
  91. }
  92. bigen.PutUint64(e.b[:], math.Float64bits(f))
  93. if bincDoPrune {
  94. i := 7
  95. for ; i >= 0 && (e.b[i] == 0); i-- {
  96. }
  97. i++
  98. if i <= 6 {
  99. e.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
  100. e.w.writen1(byte(i))
  101. e.w.writeb(e.b[:i])
  102. return
  103. }
  104. }
  105. e.w.writen1(bincVdFloat<<4 | bincFlBin64)
  106. e.w.writeb(e.b[:])
  107. }
  108. func (e *bincEncDriver) encIntegerPrune(bd byte, pos bool, v uint64, lim uint8) {
  109. if lim == 4 {
  110. bigen.PutUint32(e.b[:lim], uint32(v))
  111. } else {
  112. bigen.PutUint64(e.b[:lim], v)
  113. }
  114. if bincDoPrune {
  115. i := pruneSignExt(e.b[:lim], pos)
  116. e.w.writen1(bd | lim - 1 - byte(i))
  117. e.w.writeb(e.b[i:lim])
  118. } else {
  119. e.w.writen1(bd | lim - 1)
  120. e.w.writeb(e.b[:lim])
  121. }
  122. }
  123. func (e *bincEncDriver) encodeInt(v int64) {
  124. const nbd byte = bincVdNegInt << 4
  125. switch {
  126. case v >= 0:
  127. e.encUint(bincVdPosInt<<4, true, uint64(v))
  128. case v == -1:
  129. e.w.writen1(bincVdSpecial<<4 | bincSpNegOne)
  130. default:
  131. e.encUint(bincVdNegInt<<4, false, uint64(-v))
  132. }
  133. }
  134. func (e *bincEncDriver) encodeUint(v uint64) {
  135. e.encUint(bincVdPosInt<<4, true, v)
  136. }
  137. func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) {
  138. switch {
  139. case v == 0:
  140. e.w.writen1(bincVdSpecial<<4 | bincSpZero)
  141. case pos && v >= 1 && v <= 16:
  142. e.w.writen1(bincVdSmallInt<<4 | byte(v-1))
  143. case v <= math.MaxUint8:
  144. e.w.writen2(bd|0x0, byte(v))
  145. case v <= math.MaxUint16:
  146. e.w.writen1(bd | 0x01)
  147. e.w.writeUint16(uint16(v))
  148. case v <= math.MaxUint32:
  149. e.encIntegerPrune(bd, pos, v, 4)
  150. default:
  151. e.encIntegerPrune(bd, pos, v, 8)
  152. }
  153. }
  154. func (e *bincEncDriver) encodeExt(rv reflect.Value, xtag uint64, ext Ext, _ *Encoder) {
  155. bs := ext.WriteExt(rv)
  156. if bs == nil {
  157. e.encodeNil()
  158. return
  159. }
  160. e.encodeExtPreamble(uint8(xtag), len(bs))
  161. e.w.writeb(bs)
  162. }
  163. func (e *bincEncDriver) encodeRawExt(re *RawExt, _ *Encoder) {
  164. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  165. e.w.writeb(re.Data)
  166. }
  167. func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
  168. e.encLen(bincVdCustomExt<<4, uint64(length))
  169. e.w.writen1(xtag)
  170. }
  171. func (e *bincEncDriver) encodeArrayPreamble(length int) {
  172. e.encLen(bincVdArray<<4, uint64(length))
  173. }
  174. func (e *bincEncDriver) encodeMapPreamble(length int) {
  175. e.encLen(bincVdMap<<4, uint64(length))
  176. }
  177. func (e *bincEncDriver) encodeString(c charEncoding, v string) {
  178. l := uint64(len(v))
  179. e.encBytesLen(c, l)
  180. if l > 0 {
  181. e.w.writestr(v)
  182. }
  183. }
  184. func (e *bincEncDriver) encodeSymbol(v string) {
  185. // if WriteSymbolsNoRefs {
  186. // e.encodeString(c_UTF8, v)
  187. // return
  188. // }
  189. //symbols only offer benefit when string length > 1.
  190. //This is because strings with length 1 take only 2 bytes to store
  191. //(bd with embedded length, and single byte for string val).
  192. l := len(v)
  193. switch l {
  194. case 0:
  195. e.encBytesLen(c_UTF8, 0)
  196. return
  197. case 1:
  198. e.encBytesLen(c_UTF8, 1)
  199. e.w.writen1(v[0])
  200. return
  201. }
  202. if e.m == nil {
  203. e.m = make(map[string]uint16, 16)
  204. }
  205. ui, ok := e.m[v]
  206. if ok {
  207. if ui <= math.MaxUint8 {
  208. e.w.writen2(bincVdSymbol<<4, byte(ui))
  209. } else {
  210. e.w.writen1(bincVdSymbol<<4 | 0x8)
  211. e.w.writeUint16(ui)
  212. }
  213. } else {
  214. e.s++
  215. ui = uint16(e.s)
  216. //ui = uint16(atomic.AddUint32(&e.s, 1))
  217. e.m[v] = ui
  218. var lenprec uint8
  219. switch {
  220. case l <= math.MaxUint8:
  221. // lenprec = 0
  222. case l <= math.MaxUint16:
  223. lenprec = 1
  224. case int64(l) <= math.MaxUint32:
  225. lenprec = 2
  226. default:
  227. lenprec = 3
  228. }
  229. if ui <= math.MaxUint8 {
  230. e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
  231. } else {
  232. e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
  233. e.w.writeUint16(ui)
  234. }
  235. switch lenprec {
  236. case 0:
  237. e.w.writen1(byte(l))
  238. case 1:
  239. e.w.writeUint16(uint16(l))
  240. case 2:
  241. e.w.writeUint32(uint32(l))
  242. default:
  243. e.w.writeUint64(uint64(l))
  244. }
  245. e.w.writestr(v)
  246. }
  247. }
  248. func (e *bincEncDriver) encodeStringBytes(c charEncoding, v []byte) {
  249. l := uint64(len(v))
  250. e.encBytesLen(c, l)
  251. if l > 0 {
  252. e.w.writeb(v)
  253. }
  254. }
  255. func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
  256. //TODO: support bincUnicodeOther (for now, just use string or bytearray)
  257. if c == c_RAW {
  258. e.encLen(bincVdByteArray<<4, length)
  259. } else {
  260. e.encLen(bincVdString<<4, length)
  261. }
  262. }
  263. func (e *bincEncDriver) encLen(bd byte, l uint64) {
  264. if l < 12 {
  265. e.w.writen1(bd | uint8(l+4))
  266. } else {
  267. e.encLenNumber(bd, l)
  268. }
  269. }
  270. func (e *bincEncDriver) encLenNumber(bd byte, v uint64) {
  271. switch {
  272. case v <= math.MaxUint8:
  273. e.w.writen2(bd, byte(v))
  274. case v <= math.MaxUint16:
  275. e.w.writen1(bd | 0x01)
  276. e.w.writeUint16(uint16(v))
  277. case v <= math.MaxUint32:
  278. e.w.writen1(bd | 0x02)
  279. e.w.writeUint32(uint32(v))
  280. default:
  281. e.w.writen1(bd | 0x03)
  282. e.w.writeUint64(uint64(v))
  283. }
  284. }
  285. //------------------------------------
  286. type bincDecDriver struct {
  287. h *BincHandle
  288. r decReader
  289. bdRead bool
  290. bdType valueType
  291. bd byte
  292. vd byte
  293. vs byte
  294. noStreamingCodec
  295. b [8]byte
  296. m map[uint32]string // symbols (use uint32 as key, as map optimizes for it)
  297. }
  298. func (d *bincDecDriver) initReadNext() {
  299. if d.bdRead {
  300. return
  301. }
  302. d.bd = d.r.readn1()
  303. d.vd = d.bd >> 4
  304. d.vs = d.bd & 0x0f
  305. d.bdRead = true
  306. d.bdType = valueTypeUnset
  307. }
  308. func (d *bincDecDriver) currentEncodedType() valueType {
  309. if d.bdType == valueTypeUnset {
  310. switch d.vd {
  311. case bincVdSpecial:
  312. switch d.vs {
  313. case bincSpNil:
  314. d.bdType = valueTypeNil
  315. case bincSpFalse, bincSpTrue:
  316. d.bdType = valueTypeBool
  317. case bincSpNan, bincSpNegInf, bincSpPosInf, bincSpZeroFloat:
  318. d.bdType = valueTypeFloat
  319. case bincSpZero:
  320. if d.h.SignedInteger {
  321. d.bdType = valueTypeInt
  322. } else {
  323. d.bdType = valueTypeUint
  324. }
  325. case bincSpNegOne:
  326. d.bdType = valueTypeInt
  327. default:
  328. decErr("currentEncodedType: Unrecognized special value 0x%x", d.vs)
  329. }
  330. case bincVdSmallInt, bincVdPosInt:
  331. if d.h.SignedInteger {
  332. d.bdType = valueTypeInt
  333. } else {
  334. d.bdType = valueTypeUint
  335. }
  336. case bincVdNegInt:
  337. d.bdType = valueTypeInt
  338. case bincVdFloat:
  339. d.bdType = valueTypeFloat
  340. case bincVdString:
  341. d.bdType = valueTypeString
  342. case bincVdSymbol:
  343. d.bdType = valueTypeSymbol
  344. case bincVdByteArray:
  345. d.bdType = valueTypeBytes
  346. case bincVdTimestamp:
  347. d.bdType = valueTypeTimestamp
  348. case bincVdCustomExt:
  349. d.bdType = valueTypeExt
  350. case bincVdArray:
  351. d.bdType = valueTypeArray
  352. case bincVdMap:
  353. d.bdType = valueTypeMap
  354. default:
  355. decErr("currentEncodedType: Unrecognized d.vd: 0x%x", d.vd)
  356. }
  357. }
  358. return d.bdType
  359. }
  360. func (d *bincDecDriver) tryDecodeAsNil() bool {
  361. if d.bd == bincVdSpecial<<4|bincSpNil {
  362. d.bdRead = false
  363. return true
  364. }
  365. return false
  366. }
  367. func (d *bincDecDriver) isBuiltinType(rt uintptr) bool {
  368. return rt == timeTypId
  369. }
  370. func (d *bincDecDriver) decodeBuiltin(rt uintptr, v interface{}) {
  371. switch rt {
  372. case timeTypId:
  373. if d.vd != bincVdTimestamp {
  374. decErr("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd)
  375. }
  376. tt, err := decodeTime(d.r.readn(int(d.vs)))
  377. if err != nil {
  378. panic(err)
  379. }
  380. var vt *time.Time = v.(*time.Time)
  381. *vt = tt
  382. d.bdRead = false
  383. }
  384. }
  385. func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
  386. if vs&0x8 == 0 {
  387. d.r.readb(d.b[0:defaultLen])
  388. } else {
  389. l := d.r.readn1()
  390. if l > 8 {
  391. decErr("At most 8 bytes used to represent float. Received: %v bytes", l)
  392. }
  393. for i := l; i < 8; i++ {
  394. d.b[i] = 0
  395. }
  396. d.r.readb(d.b[0:l])
  397. }
  398. }
  399. func (d *bincDecDriver) decFloat() (f float64) {
  400. //if true { f = math.Float64frombits(d.r.readUint64()); break; }
  401. switch vs := d.vs; vs & 0x7 {
  402. case bincFlBin32:
  403. d.decFloatPre(vs, 4)
  404. f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
  405. case bincFlBin64:
  406. d.decFloatPre(vs, 8)
  407. f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
  408. default:
  409. decErr("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs)
  410. }
  411. return
  412. }
  413. func (d *bincDecDriver) decUint() (v uint64) {
  414. // need to inline the code (interface conversion and type assertion expensive)
  415. switch d.vs {
  416. case 0:
  417. v = uint64(d.r.readn1())
  418. case 1:
  419. d.r.readb(d.b[6:])
  420. v = uint64(bigen.Uint16(d.b[6:]))
  421. case 2:
  422. d.b[4] = 0
  423. d.r.readb(d.b[5:])
  424. v = uint64(bigen.Uint32(d.b[4:]))
  425. case 3:
  426. d.r.readb(d.b[4:])
  427. v = uint64(bigen.Uint32(d.b[4:]))
  428. case 4, 5, 6:
  429. lim := int(7 - d.vs)
  430. d.r.readb(d.b[lim:])
  431. for i := 0; i < lim; i++ {
  432. d.b[i] = 0
  433. }
  434. v = uint64(bigen.Uint64(d.b[:]))
  435. case 7:
  436. d.r.readb(d.b[:])
  437. v = uint64(bigen.Uint64(d.b[:]))
  438. default:
  439. decErr("unsigned integers with greater than 64 bits of precision not supported")
  440. }
  441. return
  442. }
  443. func (d *bincDecDriver) decIntAny() (ui uint64, i int64, neg bool) {
  444. switch d.vd {
  445. case bincVdPosInt:
  446. ui = d.decUint()
  447. i = int64(ui)
  448. case bincVdNegInt:
  449. ui = d.decUint()
  450. i = -(int64(ui))
  451. neg = true
  452. case bincVdSmallInt:
  453. i = int64(d.vs) + 1
  454. ui = uint64(d.vs) + 1
  455. case bincVdSpecial:
  456. switch d.vs {
  457. case bincSpZero:
  458. //i = 0
  459. case bincSpNegOne:
  460. neg = true
  461. ui = 1
  462. i = -1
  463. default:
  464. decErr("numeric decode fails for special value: d.vs: 0x%x", d.vs)
  465. }
  466. default:
  467. decErr("number can only be decoded from uint or int values. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd)
  468. }
  469. return
  470. }
  471. func (d *bincDecDriver) decodeInt(bitsize uint8) (i int64) {
  472. _, i, _ = d.decIntAny()
  473. checkOverflow(0, i, bitsize)
  474. d.bdRead = false
  475. return
  476. }
  477. func (d *bincDecDriver) decodeUint(bitsize uint8) (ui uint64) {
  478. ui, i, neg := d.decIntAny()
  479. if neg {
  480. decErr("Assigning negative signed value: %v, to unsigned type", i)
  481. }
  482. checkOverflow(ui, 0, bitsize)
  483. d.bdRead = false
  484. return
  485. }
  486. func (d *bincDecDriver) decodeFloat(chkOverflow32 bool) (f float64) {
  487. switch d.vd {
  488. case bincVdSpecial:
  489. d.bdRead = false
  490. switch d.vs {
  491. case bincSpNan:
  492. return math.NaN()
  493. case bincSpPosInf:
  494. return math.Inf(1)
  495. case bincSpZeroFloat, bincSpZero:
  496. return
  497. case bincSpNegInf:
  498. return math.Inf(-1)
  499. default:
  500. decErr("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs)
  501. }
  502. case bincVdFloat:
  503. f = d.decFloat()
  504. default:
  505. _, i, _ := d.decIntAny()
  506. f = float64(i)
  507. }
  508. checkOverflowFloat32(f, chkOverflow32)
  509. d.bdRead = false
  510. return
  511. }
  512. // bool can be decoded from bool only (single byte).
  513. func (d *bincDecDriver) decodeBool() (b bool) {
  514. switch d.bd {
  515. case (bincVdSpecial | bincSpFalse):
  516. // b = false
  517. case (bincVdSpecial | bincSpTrue):
  518. b = true
  519. default:
  520. decErr("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  521. }
  522. d.bdRead = false
  523. return
  524. }
  525. func (d *bincDecDriver) readMapLen() (length int) {
  526. if d.vd != bincVdMap {
  527. decErr("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd)
  528. }
  529. length = d.decLen()
  530. d.bdRead = false
  531. return
  532. }
  533. func (d *bincDecDriver) readArrayLen() (length int) {
  534. if d.vd != bincVdArray {
  535. decErr("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd)
  536. }
  537. length = d.decLen()
  538. d.bdRead = false
  539. return
  540. }
  541. func (d *bincDecDriver) decLen() int {
  542. if d.vs > 3 {
  543. return int(d.vs - 4)
  544. }
  545. return int(d.decLenNumber())
  546. }
  547. func (d *bincDecDriver) decLenNumber() (v uint64) {
  548. switch d.vs {
  549. case 0:
  550. v = uint64(d.r.readn1())
  551. case 1:
  552. d.r.readb(d.b[6:])
  553. v = uint64(bigen.Uint16(d.b[6:]))
  554. case 2:
  555. d.r.readb(d.b[4:])
  556. v = uint64(bigen.Uint32(d.b[4:]))
  557. default:
  558. d.r.readb(d.b[:])
  559. v = bigen.Uint64(d.b[:])
  560. }
  561. return
  562. }
  563. func (d *bincDecDriver) decodeString() (s string) {
  564. switch d.vd {
  565. case bincVdString, bincVdByteArray:
  566. if length := d.decLen(); length > 0 {
  567. s = string(d.r.readn(length))
  568. }
  569. case bincVdSymbol:
  570. //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
  571. //extract symbol
  572. //if containsStringVal, read it and put in map
  573. //else look in map for string value
  574. var symbol uint32
  575. vs := d.vs
  576. //fmt.Printf(">>>> d.vs: 0b%b, & 0x8: %v, & 0x4: %v\n", d.vs, vs & 0x8, vs & 0x4)
  577. if vs&0x8 == 0 {
  578. symbol = uint32(d.r.readn1())
  579. } else {
  580. symbol = uint32(d.r.readUint16())
  581. }
  582. if d.m == nil {
  583. d.m = make(map[uint32]string, 16)
  584. }
  585. if vs&0x4 == 0 {
  586. s = d.m[symbol]
  587. } else {
  588. var slen int
  589. switch vs & 0x3 {
  590. case 0:
  591. slen = int(d.r.readn1())
  592. case 1:
  593. slen = int(d.r.readUint16())
  594. case 2:
  595. slen = int(d.r.readUint32())
  596. case 3:
  597. slen = int(d.r.readUint64())
  598. }
  599. s = string(d.r.readn(slen))
  600. d.m[symbol] = s
  601. }
  602. default:
  603. decErr("Invalid d.vd for string. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x",
  604. bincVdString, bincVdByteArray, bincVdSymbol, d.vd)
  605. }
  606. d.bdRead = false
  607. return
  608. }
  609. func (d *bincDecDriver) decodeBytes(bs []byte) (bsOut []byte, changed bool) {
  610. var clen int
  611. switch d.vd {
  612. case bincVdString, bincVdByteArray:
  613. clen = d.decLen()
  614. default:
  615. decErr("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x",
  616. bincVdString, bincVdByteArray, d.vd)
  617. }
  618. if clen > 0 {
  619. // if no contents in stream, don't update the passed byteslice
  620. if len(bs) != clen {
  621. if len(bs) > clen {
  622. bs = bs[:clen]
  623. } else {
  624. bs = make([]byte, clen)
  625. }
  626. bsOut = bs
  627. changed = true
  628. }
  629. d.r.readb(bs)
  630. }
  631. d.bdRead = false
  632. return
  633. }
  634. func (d *bincDecDriver) decodeExt(rv reflect.Value, xtag uint64, ext Ext, _ *Decoder) (realxtag uint64) {
  635. if xtag > 0xff {
  636. decErr("decodeExt: tag must be <= 0xff; got: %v", xtag)
  637. }
  638. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  639. realxtag = uint64(realxtag1)
  640. if ext == nil {
  641. re := rv.Interface().(*RawExt)
  642. re.Tag = realxtag
  643. re.Data = xbs
  644. } else {
  645. ext.ReadExt(rv, xbs)
  646. }
  647. return
  648. }
  649. func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  650. switch d.vd {
  651. case bincVdCustomExt:
  652. l := d.decLen()
  653. xtag = d.r.readn1()
  654. if verifyTag && xtag != tag {
  655. decErr("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  656. }
  657. xbs = d.r.readn(l)
  658. case bincVdByteArray:
  659. xbs, _ = d.decodeBytes(nil)
  660. default:
  661. decErr("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd)
  662. }
  663. d.bdRead = false
  664. return
  665. }
  666. func (d *bincDecDriver) decodeNaked(_ *Decoder) (v interface{}, vt valueType, decodeFurther bool) {
  667. d.initReadNext()
  668. switch d.vd {
  669. case bincVdSpecial:
  670. switch d.vs {
  671. case bincSpNil:
  672. vt = valueTypeNil
  673. case bincSpFalse:
  674. vt = valueTypeBool
  675. v = false
  676. case bincSpTrue:
  677. vt = valueTypeBool
  678. v = true
  679. case bincSpNan:
  680. vt = valueTypeFloat
  681. v = math.NaN()
  682. case bincSpPosInf:
  683. vt = valueTypeFloat
  684. v = math.Inf(1)
  685. case bincSpNegInf:
  686. vt = valueTypeFloat
  687. v = math.Inf(-1)
  688. case bincSpZeroFloat:
  689. vt = valueTypeFloat
  690. v = float64(0)
  691. case bincSpZero:
  692. vt = valueTypeUint
  693. v = uint64(0) // int8(0)
  694. case bincSpNegOne:
  695. vt = valueTypeInt
  696. v = int64(-1) // int8(-1)
  697. default:
  698. decErr("decodeNaked: Unrecognized special value 0x%x", d.vs)
  699. }
  700. case bincVdSmallInt:
  701. vt = valueTypeUint
  702. v = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
  703. case bincVdPosInt:
  704. vt = valueTypeUint
  705. v = d.decUint()
  706. case bincVdNegInt:
  707. vt = valueTypeInt
  708. v = -(int64(d.decUint()))
  709. case bincVdFloat:
  710. vt = valueTypeFloat
  711. v = d.decFloat()
  712. case bincVdSymbol:
  713. vt = valueTypeSymbol
  714. v = d.decodeString()
  715. case bincVdString:
  716. vt = valueTypeString
  717. v = d.decodeString()
  718. case bincVdByteArray:
  719. vt = valueTypeBytes
  720. v, _ = d.decodeBytes(nil)
  721. case bincVdTimestamp:
  722. vt = valueTypeTimestamp
  723. tt, err := decodeTime(d.r.readn(int(d.vs)))
  724. if err != nil {
  725. panic(err)
  726. }
  727. v = tt
  728. case bincVdCustomExt:
  729. vt = valueTypeExt
  730. l := d.decLen()
  731. var re RawExt
  732. re.Tag = uint64(d.r.readn1())
  733. re.Data = d.r.readn(l)
  734. v = &re
  735. vt = valueTypeExt
  736. case bincVdArray:
  737. vt = valueTypeArray
  738. decodeFurther = true
  739. case bincVdMap:
  740. vt = valueTypeMap
  741. decodeFurther = true
  742. default:
  743. decErr("decodeNaked: Unrecognized d.vd: 0x%x", d.vd)
  744. }
  745. if !decodeFurther {
  746. d.bdRead = false
  747. }
  748. if vt == valueTypeUint && d.h.SignedInteger {
  749. d.bdType = valueTypeInt
  750. v = int64(v.(uint64))
  751. }
  752. return
  753. }
  754. //------------------------------------
  755. //BincHandle is a Handle for the Binc Schema-Free Encoding Format
  756. //defined at https://github.com/ugorji/binc .
  757. //
  758. //BincHandle currently supports all Binc features with the following EXCEPTIONS:
  759. // - only integers up to 64 bits of precision are supported.
  760. // big integers are unsupported.
  761. // - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
  762. // extended precision and decimal IEEE 754 floats are unsupported.
  763. // - Only UTF-8 strings supported.
  764. // Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
  765. //
  766. //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
  767. type BincHandle struct {
  768. BasicHandle
  769. }
  770. func (h *BincHandle) newEncDriver(w encWriter) encDriver {
  771. return &bincEncDriver{w: w}
  772. }
  773. func (h *BincHandle) newDecDriver(r decReader) decDriver {
  774. return &bincDecDriver{r: r, h: h}
  775. }
  776. var _ decDriver = (*bincDecDriver)(nil)
  777. var _ encDriver = (*bincEncDriver)(nil)