binc.go 17 KB

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