binc.go 18 KB

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