binc.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "math"
  6. "reflect"
  7. "time"
  8. )
  9. const bincDoPrune = true // No longer needed. Needed before as C lib did not support pruning.
  10. // vd as low 4 bits (there are 16 slots)
  11. const (
  12. bincVdSpecial byte = iota
  13. bincVdPosInt
  14. bincVdNegInt
  15. bincVdFloat
  16. bincVdString
  17. bincVdByteArray
  18. bincVdArray
  19. bincVdMap
  20. bincVdTimestamp
  21. bincVdSmallInt
  22. bincVdUnicodeOther
  23. bincVdSymbol
  24. bincVdDecimal
  25. _ // open slot
  26. _ // open slot
  27. bincVdCustomExt = 0x0f
  28. )
  29. const (
  30. bincSpNil byte = iota
  31. bincSpFalse
  32. bincSpTrue
  33. bincSpNan
  34. bincSpPosInf
  35. bincSpNegInf
  36. bincSpZeroFloat
  37. bincSpZero
  38. bincSpNegOne
  39. )
  40. const (
  41. bincFlBin16 byte = iota
  42. bincFlBin32
  43. _ // bincFlBin32e
  44. bincFlBin64
  45. _ // bincFlBin64e
  46. // others not currently supported
  47. )
  48. type bincEncDriver struct {
  49. e *Encoder
  50. w encWriter
  51. m map[string]uint16 // symbols
  52. b [scratchByteArrayLen]byte
  53. s uint16 // symbols sequencer
  54. encNoSeparator
  55. }
  56. func (e *bincEncDriver) IsBuiltinType(rt uintptr) bool {
  57. return rt == timeTypId
  58. }
  59. func (e *bincEncDriver) EncodeBuiltin(rt uintptr, v interface{}) {
  60. if rt == timeTypId {
  61. var bs []byte
  62. switch x := v.(type) {
  63. case time.Time:
  64. bs = encodeTime(x)
  65. case *time.Time:
  66. bs = encodeTime(*x)
  67. default:
  68. e.e.errorf("binc error encoding builtin: expect time.Time, received %T", v)
  69. }
  70. e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
  71. e.w.writeb(bs)
  72. }
  73. }
  74. func (e *bincEncDriver) EncodeNil() {
  75. e.w.writen1(bincVdSpecial<<4 | bincSpNil)
  76. }
  77. func (e *bincEncDriver) EncodeBool(b bool) {
  78. if b {
  79. e.w.writen1(bincVdSpecial<<4 | bincSpTrue)
  80. } else {
  81. e.w.writen1(bincVdSpecial<<4 | bincSpFalse)
  82. }
  83. }
  84. func (e *bincEncDriver) EncodeFloat32(f float32) {
  85. if f == 0 {
  86. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  87. return
  88. }
  89. e.w.writen1(bincVdFloat<<4 | bincFlBin32)
  90. bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
  91. }
  92. func (e *bincEncDriver) EncodeFloat64(f float64) {
  93. if f == 0 {
  94. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  95. return
  96. }
  97. bigen.PutUint64(e.b[:8], math.Float64bits(f))
  98. if bincDoPrune {
  99. i := 7
  100. for ; i >= 0 && (e.b[i] == 0); i-- {
  101. }
  102. i++
  103. if i <= 6 {
  104. e.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
  105. e.w.writen1(byte(i))
  106. e.w.writeb(e.b[:i])
  107. return
  108. }
  109. }
  110. e.w.writen1(bincVdFloat<<4 | bincFlBin64)
  111. e.w.writeb(e.b[:8])
  112. }
  113. func (e *bincEncDriver) encIntegerPrune(bd byte, pos bool, v uint64, lim uint8) {
  114. if lim == 4 {
  115. bigen.PutUint32(e.b[:lim], uint32(v))
  116. } else {
  117. bigen.PutUint64(e.b[:lim], v)
  118. }
  119. if bincDoPrune {
  120. i := pruneSignExt(e.b[:lim], pos)
  121. e.w.writen1(bd | lim - 1 - byte(i))
  122. e.w.writeb(e.b[i:lim])
  123. } else {
  124. e.w.writen1(bd | lim - 1)
  125. e.w.writeb(e.b[:lim])
  126. }
  127. }
  128. func (e *bincEncDriver) EncodeInt(v int64) {
  129. const nbd byte = bincVdNegInt << 4
  130. if v >= 0 {
  131. e.encUint(bincVdPosInt<<4, true, uint64(v))
  132. } else if v == -1 {
  133. e.w.writen1(bincVdSpecial<<4 | bincSpNegOne)
  134. } else {
  135. e.encUint(bincVdNegInt<<4, false, uint64(-v))
  136. }
  137. }
  138. func (e *bincEncDriver) EncodeUint(v uint64) {
  139. e.encUint(bincVdPosInt<<4, true, v)
  140. }
  141. func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) {
  142. if v == 0 {
  143. e.w.writen1(bincVdSpecial<<4 | bincSpZero)
  144. } else if pos && v >= 1 && v <= 16 {
  145. e.w.writen1(bincVdSmallInt<<4 | byte(v-1))
  146. } else if v <= math.MaxUint8 {
  147. e.w.writen2(bd|0x0, byte(v))
  148. } else if v <= math.MaxUint16 {
  149. e.w.writen1(bd | 0x01)
  150. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  151. } else if v <= math.MaxUint32 {
  152. e.encIntegerPrune(bd, pos, v, 4)
  153. } else {
  154. e.encIntegerPrune(bd, pos, v, 8)
  155. }
  156. }
  157. func (e *bincEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
  158. bs := ext.WriteExt(rv)
  159. if bs == nil {
  160. e.EncodeNil()
  161. return
  162. }
  163. e.encodeExtPreamble(uint8(xtag), len(bs))
  164. e.w.writeb(bs)
  165. }
  166. func (e *bincEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  167. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  168. e.w.writeb(re.Data)
  169. }
  170. func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
  171. e.encLen(bincVdCustomExt<<4, uint64(length))
  172. e.w.writen1(xtag)
  173. }
  174. func (e *bincEncDriver) EncodeArrayStart(length int) {
  175. e.encLen(bincVdArray<<4, uint64(length))
  176. }
  177. func (e *bincEncDriver) EncodeMapStart(length int) {
  178. e.encLen(bincVdMap<<4, uint64(length))
  179. }
  180. func (e *bincEncDriver) EncodeString(c charEncoding, v string) {
  181. l := uint64(len(v))
  182. e.encBytesLen(c, l)
  183. if l > 0 {
  184. e.w.writestr(v)
  185. }
  186. }
  187. func (e *bincEncDriver) EncodeSymbol(v string) {
  188. // if WriteSymbolsNoRefs {
  189. // e.encodeString(c_UTF8, v)
  190. // return
  191. // }
  192. //symbols only offer benefit when string length > 1.
  193. //This is because strings with length 1 take only 2 bytes to store
  194. //(bd with embedded length, and single byte for string val).
  195. l := len(v)
  196. if l == 0 {
  197. e.encBytesLen(c_UTF8, 0)
  198. return
  199. } else if l == 1 {
  200. e.encBytesLen(c_UTF8, 1)
  201. e.w.writen1(v[0])
  202. return
  203. }
  204. if e.m == nil {
  205. e.m = make(map[string]uint16, 16)
  206. }
  207. ui, ok := e.m[v]
  208. if ok {
  209. if ui <= math.MaxUint8 {
  210. e.w.writen2(bincVdSymbol<<4, byte(ui))
  211. } else {
  212. e.w.writen1(bincVdSymbol<<4 | 0x8)
  213. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  214. }
  215. } else {
  216. e.s++
  217. ui = e.s
  218. //ui = uint16(atomic.AddUint32(&e.s, 1))
  219. e.m[v] = ui
  220. var lenprec uint8
  221. if l <= math.MaxUint8 {
  222. // lenprec = 0
  223. } else if l <= math.MaxUint16 {
  224. lenprec = 1
  225. } else if int64(l) <= math.MaxUint32 {
  226. lenprec = 2
  227. } else {
  228. lenprec = 3
  229. }
  230. if ui <= math.MaxUint8 {
  231. e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
  232. } else {
  233. e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
  234. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  235. }
  236. if lenprec == 0 {
  237. e.w.writen1(byte(l))
  238. } else if lenprec == 1 {
  239. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(l))
  240. } else if lenprec == 2 {
  241. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(l))
  242. } else {
  243. bigenHelper{e.b[:8], 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. if v <= math.MaxUint8 {
  272. e.w.writen2(bd, byte(v))
  273. } else if v <= math.MaxUint16 {
  274. e.w.writen1(bd | 0x01)
  275. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  276. } else if v <= math.MaxUint32 {
  277. e.w.writen1(bd | 0x02)
  278. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
  279. } else {
  280. e.w.writen1(bd | 0x03)
  281. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(v))
  282. }
  283. }
  284. //------------------------------------
  285. type bincDecSymbol struct {
  286. s string
  287. b []byte
  288. i uint16
  289. }
  290. type bincDecDriver struct {
  291. d *Decoder
  292. h *BincHandle
  293. r decReader
  294. br bool // bytes reader
  295. bdRead bool
  296. bd byte
  297. vd byte
  298. vs byte
  299. noStreamingCodec
  300. decNoSeparator
  301. b [scratchByteArrayLen]byte
  302. // linear searching on this slice is ok,
  303. // because we typically expect < 32 symbols in each stream.
  304. s []bincDecSymbol
  305. }
  306. func (d *bincDecDriver) readNextBd() {
  307. d.bd = d.r.readn1()
  308. d.vd = d.bd >> 4
  309. d.vs = d.bd & 0x0f
  310. d.bdRead = true
  311. }
  312. func (d *bincDecDriver) uncacheRead() {
  313. if d.bdRead {
  314. d.r.unreadn1()
  315. d.bdRead = false
  316. }
  317. }
  318. func (d *bincDecDriver) ContainerType() (vt valueType) {
  319. if d.vd == bincVdSpecial && d.vs == bincSpNil {
  320. return valueTypeNil
  321. } else if d.vd == bincVdByteArray {
  322. return valueTypeBytes
  323. } else if d.vd == bincVdString {
  324. return valueTypeString
  325. } else if d.vd == bincVdArray {
  326. return valueTypeArray
  327. } else if d.vd == bincVdMap {
  328. return valueTypeMap
  329. } else {
  330. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  331. }
  332. return valueTypeUnset
  333. }
  334. func (d *bincDecDriver) TryDecodeAsNil() bool {
  335. if !d.bdRead {
  336. d.readNextBd()
  337. }
  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. if !d.bdRead {
  349. d.readNextBd()
  350. }
  351. if rt == timeTypId {
  352. if d.vd != bincVdTimestamp {
  353. d.d.errorf("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd)
  354. return
  355. }
  356. tt, err := decodeTime(d.r.readx(int(d.vs)))
  357. if err != nil {
  358. panic(err)
  359. }
  360. var vt *time.Time = v.(*time.Time)
  361. *vt = tt
  362. d.bdRead = false
  363. }
  364. }
  365. func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
  366. if vs&0x8 == 0 {
  367. d.r.readb(d.b[0:defaultLen])
  368. } else {
  369. l := d.r.readn1()
  370. if l > 8 {
  371. d.d.errorf("At most 8 bytes used to represent float. Received: %v bytes", l)
  372. return
  373. }
  374. for i := l; i < 8; i++ {
  375. d.b[i] = 0
  376. }
  377. d.r.readb(d.b[0:l])
  378. }
  379. }
  380. func (d *bincDecDriver) decFloat() (f float64) {
  381. //if true { f = math.Float64frombits(bigen.Uint64(d.r.readx(8))); break; }
  382. if x := d.vs & 0x7; x == bincFlBin32 {
  383. d.decFloatPre(d.vs, 4)
  384. f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
  385. } else if x == bincFlBin64 {
  386. d.decFloatPre(d.vs, 8)
  387. f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
  388. } else {
  389. d.d.errorf("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs)
  390. return
  391. }
  392. return
  393. }
  394. func (d *bincDecDriver) decUint() (v uint64) {
  395. // need to inline the code (interface conversion and type assertion expensive)
  396. switch d.vs {
  397. case 0:
  398. v = uint64(d.r.readn1())
  399. case 1:
  400. d.r.readb(d.b[6:8])
  401. v = uint64(bigen.Uint16(d.b[6:8]))
  402. case 2:
  403. d.b[4] = 0
  404. d.r.readb(d.b[5:8])
  405. v = uint64(bigen.Uint32(d.b[4:8]))
  406. case 3:
  407. d.r.readb(d.b[4:8])
  408. v = uint64(bigen.Uint32(d.b[4:8]))
  409. case 4, 5, 6:
  410. lim := int(7 - d.vs)
  411. d.r.readb(d.b[lim:8])
  412. for i := 0; i < lim; i++ {
  413. d.b[i] = 0
  414. }
  415. v = uint64(bigen.Uint64(d.b[:8]))
  416. case 7:
  417. d.r.readb(d.b[:8])
  418. v = uint64(bigen.Uint64(d.b[:8]))
  419. default:
  420. d.d.errorf("unsigned integers with greater than 64 bits of precision not supported")
  421. return
  422. }
  423. return
  424. }
  425. func (d *bincDecDriver) decCheckInteger() (ui uint64, neg bool) {
  426. if !d.bdRead {
  427. d.readNextBd()
  428. }
  429. vd, vs := d.vd, d.vs
  430. if vd == bincVdPosInt {
  431. ui = d.decUint()
  432. } else if vd == bincVdNegInt {
  433. ui = d.decUint()
  434. neg = true
  435. } else if vd == bincVdSmallInt {
  436. ui = uint64(d.vs) + 1
  437. } else if vd == bincVdSpecial {
  438. if vs == bincSpZero {
  439. //i = 0
  440. } else if vs == bincSpNegOne {
  441. neg = true
  442. ui = 1
  443. } else {
  444. d.d.errorf("numeric decode fails for special value: d.vs: 0x%x", d.vs)
  445. return
  446. }
  447. } else {
  448. d.d.errorf("number can only be decoded from uint or int values. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd)
  449. return
  450. }
  451. return
  452. }
  453. func (d *bincDecDriver) DecodeInt(bitsize uint8) (i int64) {
  454. ui, neg := d.decCheckInteger()
  455. i, overflow := chkOvf.SignedInt(ui)
  456. if overflow {
  457. d.d.errorf("simple: overflow converting %v to signed integer", ui)
  458. return
  459. }
  460. if neg {
  461. i = -i
  462. }
  463. if chkOvf.Int(i, bitsize) {
  464. d.d.errorf("binc: overflow integer: %v", i)
  465. return
  466. }
  467. d.bdRead = false
  468. return
  469. }
  470. func (d *bincDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  471. ui, neg := d.decCheckInteger()
  472. if neg {
  473. d.d.errorf("Assigning negative signed value to unsigned type")
  474. return
  475. }
  476. if chkOvf.Uint(ui, bitsize) {
  477. d.d.errorf("binc: overflow integer: %v", ui)
  478. return
  479. }
  480. d.bdRead = false
  481. return
  482. }
  483. func (d *bincDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  484. if !d.bdRead {
  485. d.readNextBd()
  486. }
  487. vd, vs := d.vd, d.vs
  488. if vd == bincVdSpecial {
  489. d.bdRead = false
  490. if vs == bincSpNan {
  491. return math.NaN()
  492. } else if vs == bincSpPosInf {
  493. return math.Inf(1)
  494. } else if vs == bincSpZeroFloat || vs == bincSpZero {
  495. return
  496. } else if vs == bincSpNegInf {
  497. return math.Inf(-1)
  498. } else {
  499. d.d.errorf("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs)
  500. return
  501. }
  502. } else if vd == bincVdFloat {
  503. f = d.decFloat()
  504. } else {
  505. f = float64(d.DecodeInt(64))
  506. }
  507. if chkOverflow32 && chkOvf.Float32(f) {
  508. d.d.errorf("binc: float32 overflow: %v", f)
  509. return
  510. }
  511. d.bdRead = false
  512. return
  513. }
  514. // bool can be decoded from bool only (single byte).
  515. func (d *bincDecDriver) DecodeBool() (b bool) {
  516. if !d.bdRead {
  517. d.readNextBd()
  518. }
  519. if bd := d.bd; bd == (bincVdSpecial | bincSpFalse) {
  520. // b = false
  521. } else if bd == (bincVdSpecial | bincSpTrue) {
  522. b = true
  523. } else {
  524. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  525. return
  526. }
  527. d.bdRead = false
  528. return
  529. }
  530. func (d *bincDecDriver) ReadMapStart() (length int) {
  531. if d.vd != bincVdMap {
  532. d.d.errorf("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd)
  533. return
  534. }
  535. length = d.decLen()
  536. d.bdRead = false
  537. return
  538. }
  539. func (d *bincDecDriver) ReadArrayStart() (length int) {
  540. if d.vd != bincVdArray {
  541. d.d.errorf("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd)
  542. return
  543. }
  544. length = d.decLen()
  545. d.bdRead = false
  546. return
  547. }
  548. func (d *bincDecDriver) decLen() int {
  549. if d.vs > 3 {
  550. return int(d.vs - 4)
  551. }
  552. return int(d.decLenNumber())
  553. }
  554. func (d *bincDecDriver) decLenNumber() (v uint64) {
  555. if x := d.vs; x == 0 {
  556. v = uint64(d.r.readn1())
  557. } else if x == 1 {
  558. d.r.readb(d.b[6:8])
  559. v = uint64(bigen.Uint16(d.b[6:8]))
  560. } else if x == 2 {
  561. d.r.readb(d.b[4:8])
  562. v = uint64(bigen.Uint32(d.b[4:8]))
  563. } else {
  564. d.r.readb(d.b[:8])
  565. v = bigen.Uint64(d.b[:8])
  566. }
  567. return
  568. }
  569. func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (bs2 []byte, s string) {
  570. if !d.bdRead {
  571. d.readNextBd()
  572. }
  573. if d.bd == bincVdSpecial<<4|bincSpNil {
  574. d.bdRead = false
  575. return
  576. }
  577. var slen int = -1
  578. // var ok bool
  579. switch d.vd {
  580. case bincVdString, bincVdByteArray:
  581. slen = d.decLen()
  582. if zerocopy {
  583. if d.br {
  584. bs2 = d.r.readx(slen)
  585. } else if len(bs) == 0 {
  586. bs2 = decByteSlice(d.r, slen, d.b[:])
  587. } else {
  588. bs2 = decByteSlice(d.r, slen, bs)
  589. }
  590. } else {
  591. bs2 = decByteSlice(d.r, slen, bs)
  592. }
  593. if withString {
  594. s = string(bs2)
  595. }
  596. case bincVdSymbol:
  597. // zerocopy doesn't apply for symbols,
  598. // as the values must be stored in a table for later use.
  599. //
  600. //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
  601. //extract symbol
  602. //if containsStringVal, read it and put in map
  603. //else look in map for string value
  604. var symbol uint16
  605. vs := d.vs
  606. if vs&0x8 == 0 {
  607. symbol = uint16(d.r.readn1())
  608. } else {
  609. symbol = uint16(bigen.Uint16(d.r.readx(2)))
  610. }
  611. if d.s == nil {
  612. d.s = make([]bincDecSymbol, 0, 16)
  613. }
  614. if vs&0x4 == 0 {
  615. for i := range d.s {
  616. j := &d.s[i]
  617. if j.i == symbol {
  618. bs2 = j.b
  619. if withString {
  620. if j.s == "" && bs2 != nil {
  621. j.s = string(bs2)
  622. }
  623. s = j.s
  624. }
  625. break
  626. }
  627. }
  628. } else {
  629. switch vs & 0x3 {
  630. case 0:
  631. slen = int(d.r.readn1())
  632. case 1:
  633. slen = int(bigen.Uint16(d.r.readx(2)))
  634. case 2:
  635. slen = int(bigen.Uint32(d.r.readx(4)))
  636. case 3:
  637. slen = int(bigen.Uint64(d.r.readx(8)))
  638. }
  639. // since using symbols, do not store any part of
  640. // the parameter bs in the map, as it might be a shared buffer.
  641. // bs2 = decByteSlice(d.r, slen, bs)
  642. bs2 = decByteSlice(d.r, slen, nil)
  643. if withString {
  644. s = string(bs2)
  645. }
  646. d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2})
  647. }
  648. default:
  649. d.d.errorf("Invalid d.vd. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x",
  650. bincVdString, bincVdByteArray, bincVdSymbol, d.vd)
  651. return
  652. }
  653. d.bdRead = false
  654. return
  655. }
  656. func (d *bincDecDriver) DecodeString() (s string) {
  657. // DecodeBytes does not accommodate symbols, whose impl stores string version in map.
  658. // Use decStringAndBytes directly.
  659. // return string(d.DecodeBytes(d.b[:], true, true))
  660. _, s = d.decStringAndBytes(d.b[:], true, true)
  661. return
  662. }
  663. func (d *bincDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
  664. if isstring {
  665. bsOut, _ = d.decStringAndBytes(bs, false, zerocopy)
  666. return
  667. }
  668. if !d.bdRead {
  669. d.readNextBd()
  670. }
  671. if d.bd == bincVdSpecial<<4|bincSpNil {
  672. d.bdRead = false
  673. return nil
  674. }
  675. var clen int
  676. if d.vd == bincVdString || d.vd == bincVdByteArray {
  677. clen = d.decLen()
  678. } else {
  679. d.d.errorf("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x",
  680. bincVdString, bincVdByteArray, d.vd)
  681. return
  682. }
  683. d.bdRead = false
  684. if zerocopy {
  685. if d.br {
  686. return d.r.readx(clen)
  687. } else if len(bs) == 0 {
  688. bs = d.b[:]
  689. }
  690. }
  691. return decByteSlice(d.r, clen, bs)
  692. }
  693. func (d *bincDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  694. if xtag > 0xff {
  695. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  696. return
  697. }
  698. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  699. realxtag = uint64(realxtag1)
  700. if ext == nil {
  701. re := rv.(*RawExt)
  702. re.Tag = realxtag
  703. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  704. } else {
  705. ext.ReadExt(rv, xbs)
  706. }
  707. return
  708. }
  709. func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  710. if !d.bdRead {
  711. d.readNextBd()
  712. }
  713. if d.vd == bincVdCustomExt {
  714. l := d.decLen()
  715. xtag = d.r.readn1()
  716. if verifyTag && xtag != tag {
  717. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  718. return
  719. }
  720. xbs = d.r.readx(l)
  721. } else if d.vd == bincVdByteArray {
  722. xbs = d.DecodeBytes(nil, false, true)
  723. } else {
  724. d.d.errorf("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd)
  725. return
  726. }
  727. d.bdRead = false
  728. return
  729. }
  730. func (d *bincDecDriver) DecodeNaked() {
  731. if !d.bdRead {
  732. d.readNextBd()
  733. }
  734. n := &d.d.n
  735. var decodeFurther bool
  736. switch d.vd {
  737. case bincVdSpecial:
  738. switch d.vs {
  739. case bincSpNil:
  740. n.v = valueTypeNil
  741. case bincSpFalse:
  742. n.v = valueTypeBool
  743. n.b = false
  744. case bincSpTrue:
  745. n.v = valueTypeBool
  746. n.b = true
  747. case bincSpNan:
  748. n.v = valueTypeFloat
  749. n.f = math.NaN()
  750. case bincSpPosInf:
  751. n.v = valueTypeFloat
  752. n.f = math.Inf(1)
  753. case bincSpNegInf:
  754. n.v = valueTypeFloat
  755. n.f = math.Inf(-1)
  756. case bincSpZeroFloat:
  757. n.v = valueTypeFloat
  758. n.f = float64(0)
  759. case bincSpZero:
  760. n.v = valueTypeUint
  761. n.u = uint64(0) // int8(0)
  762. case bincSpNegOne:
  763. n.v = valueTypeInt
  764. n.i = int64(-1) // int8(-1)
  765. default:
  766. d.d.errorf("decodeNaked: Unrecognized special value 0x%x", d.vs)
  767. }
  768. case bincVdSmallInt:
  769. n.v = valueTypeUint
  770. n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
  771. case bincVdPosInt:
  772. n.v = valueTypeUint
  773. n.u = d.decUint()
  774. case bincVdNegInt:
  775. n.v = valueTypeInt
  776. n.i = -(int64(d.decUint()))
  777. case bincVdFloat:
  778. n.v = valueTypeFloat
  779. n.f = d.decFloat()
  780. case bincVdSymbol:
  781. n.v = valueTypeSymbol
  782. n.s = d.DecodeString()
  783. case bincVdString:
  784. n.v = valueTypeString
  785. n.s = d.DecodeString()
  786. case bincVdByteArray:
  787. n.v = valueTypeBytes
  788. n.l = d.DecodeBytes(nil, false, false)
  789. case bincVdTimestamp:
  790. n.v = valueTypeTimestamp
  791. tt, err := decodeTime(d.r.readx(int(d.vs)))
  792. if err != nil {
  793. panic(err)
  794. }
  795. n.t = tt
  796. case bincVdCustomExt:
  797. n.v = valueTypeExt
  798. l := d.decLen()
  799. n.u = uint64(d.r.readn1())
  800. n.l = d.r.readx(l)
  801. case bincVdArray:
  802. n.v = valueTypeArray
  803. decodeFurther = true
  804. case bincVdMap:
  805. n.v = valueTypeMap
  806. decodeFurther = true
  807. default:
  808. d.d.errorf("decodeNaked: Unrecognized d.vd: 0x%x", d.vd)
  809. }
  810. if !decodeFurther {
  811. d.bdRead = false
  812. }
  813. if n.v == valueTypeUint && d.h.SignedInteger {
  814. n.v = valueTypeInt
  815. n.i = int64(n.u)
  816. }
  817. return
  818. }
  819. //------------------------------------
  820. //BincHandle is a Handle for the Binc Schema-Free Encoding Format
  821. //defined at https://github.com/ugorji/binc .
  822. //
  823. //BincHandle currently supports all Binc features with the following EXCEPTIONS:
  824. // - only integers up to 64 bits of precision are supported.
  825. // big integers are unsupported.
  826. // - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
  827. // extended precision and decimal IEEE 754 floats are unsupported.
  828. // - Only UTF-8 strings supported.
  829. // Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
  830. //
  831. //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
  832. type BincHandle struct {
  833. BasicHandle
  834. binaryEncodingType
  835. }
  836. func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  837. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  838. }
  839. func (h *BincHandle) newEncDriver(e *Encoder) encDriver {
  840. return &bincEncDriver{e: e, w: e.w}
  841. }
  842. func (h *BincHandle) newDecDriver(d *Decoder) decDriver {
  843. return &bincDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  844. }
  845. func (e *bincEncDriver) reset() {
  846. e.w = e.e.w
  847. e.s = 0
  848. e.m = nil
  849. }
  850. func (d *bincDecDriver) reset() {
  851. d.r = d.d.r
  852. d.s = nil
  853. d.bd, d.bdRead, d.vd, d.vs = 0, false, 0, 0
  854. }
  855. var _ decDriver = (*bincDecDriver)(nil)
  856. var _ encDriver = (*bincEncDriver)(nil)