binc.go 20 KB

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