binc.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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) ContainerType() (vt valueType) {
  313. if d.vd == bincVdSpecial && d.vs == bincSpNil {
  314. return valueTypeNil
  315. } else if d.vd == bincVdByteArray {
  316. return valueTypeBytes
  317. } else if d.vd == bincVdString {
  318. return valueTypeString
  319. } else if d.vd == bincVdArray {
  320. return valueTypeArray
  321. } else if d.vd == bincVdMap {
  322. return valueTypeMap
  323. } else {
  324. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  325. }
  326. return valueTypeUnset
  327. }
  328. func (d *bincDecDriver) TryDecodeAsNil() bool {
  329. if !d.bdRead {
  330. d.readNextBd()
  331. }
  332. if d.bd == bincVdSpecial<<4|bincSpNil {
  333. d.bdRead = false
  334. return true
  335. }
  336. return false
  337. }
  338. func (d *bincDecDriver) IsBuiltinType(rt uintptr) bool {
  339. return rt == timeTypId
  340. }
  341. func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {
  342. if !d.bdRead {
  343. d.readNextBd()
  344. }
  345. if rt == timeTypId {
  346. if d.vd != bincVdTimestamp {
  347. d.d.errorf("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd)
  348. return
  349. }
  350. tt, err := decodeTime(d.r.readx(int(d.vs)))
  351. if err != nil {
  352. panic(err)
  353. }
  354. var vt *time.Time = v.(*time.Time)
  355. *vt = tt
  356. d.bdRead = false
  357. }
  358. }
  359. func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
  360. if vs&0x8 == 0 {
  361. d.r.readb(d.b[0:defaultLen])
  362. } else {
  363. l := d.r.readn1()
  364. if l > 8 {
  365. d.d.errorf("At most 8 bytes used to represent float. Received: %v bytes", l)
  366. return
  367. }
  368. for i := l; i < 8; i++ {
  369. d.b[i] = 0
  370. }
  371. d.r.readb(d.b[0:l])
  372. }
  373. }
  374. func (d *bincDecDriver) decFloat() (f float64) {
  375. //if true { f = math.Float64frombits(bigen.Uint64(d.r.readx(8))); break; }
  376. if x := d.vs & 0x7; x == bincFlBin32 {
  377. d.decFloatPre(d.vs, 4)
  378. f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
  379. } else if x == bincFlBin64 {
  380. d.decFloatPre(d.vs, 8)
  381. f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
  382. } else {
  383. d.d.errorf("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs)
  384. return
  385. }
  386. return
  387. }
  388. func (d *bincDecDriver) decUint() (v uint64) {
  389. // need to inline the code (interface conversion and type assertion expensive)
  390. switch d.vs {
  391. case 0:
  392. v = uint64(d.r.readn1())
  393. case 1:
  394. d.r.readb(d.b[6:8])
  395. v = uint64(bigen.Uint16(d.b[6:8]))
  396. case 2:
  397. d.b[4] = 0
  398. d.r.readb(d.b[5:8])
  399. v = uint64(bigen.Uint32(d.b[4:8]))
  400. case 3:
  401. d.r.readb(d.b[4:8])
  402. v = uint64(bigen.Uint32(d.b[4:8]))
  403. case 4, 5, 6:
  404. lim := int(7 - d.vs)
  405. d.r.readb(d.b[lim:8])
  406. for i := 0; i < lim; i++ {
  407. d.b[i] = 0
  408. }
  409. v = uint64(bigen.Uint64(d.b[:8]))
  410. case 7:
  411. d.r.readb(d.b[:8])
  412. v = uint64(bigen.Uint64(d.b[:8]))
  413. default:
  414. d.d.errorf("unsigned integers with greater than 64 bits of precision not supported")
  415. return
  416. }
  417. return
  418. }
  419. func (d *bincDecDriver) decCheckInteger() (ui uint64, neg bool) {
  420. if !d.bdRead {
  421. d.readNextBd()
  422. }
  423. vd, vs := d.vd, d.vs
  424. if vd == bincVdPosInt {
  425. ui = d.decUint()
  426. } else if vd == bincVdNegInt {
  427. ui = d.decUint()
  428. neg = true
  429. } else if vd == bincVdSmallInt {
  430. ui = uint64(d.vs) + 1
  431. } else if vd == bincVdSpecial {
  432. if vs == bincSpZero {
  433. //i = 0
  434. } else if vs == bincSpNegOne {
  435. neg = true
  436. ui = 1
  437. } else {
  438. d.d.errorf("numeric decode fails for special value: d.vs: 0x%x", d.vs)
  439. return
  440. }
  441. } else {
  442. 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)
  443. return
  444. }
  445. return
  446. }
  447. func (d *bincDecDriver) DecodeInt(bitsize uint8) (i int64) {
  448. ui, neg := d.decCheckInteger()
  449. i, overflow := chkOvf.SignedInt(ui)
  450. if overflow {
  451. d.d.errorf("simple: overflow converting %v to signed integer", ui)
  452. return
  453. }
  454. if neg {
  455. i = -i
  456. }
  457. if chkOvf.Int(i, bitsize) {
  458. d.d.errorf("binc: overflow integer: %v", i)
  459. return
  460. }
  461. d.bdRead = false
  462. return
  463. }
  464. func (d *bincDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  465. ui, neg := d.decCheckInteger()
  466. if neg {
  467. d.d.errorf("Assigning negative signed value to unsigned type")
  468. return
  469. }
  470. if chkOvf.Uint(ui, bitsize) {
  471. d.d.errorf("binc: overflow integer: %v", ui)
  472. return
  473. }
  474. d.bdRead = false
  475. return
  476. }
  477. func (d *bincDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  478. if !d.bdRead {
  479. d.readNextBd()
  480. }
  481. vd, vs := d.vd, d.vs
  482. if vd == bincVdSpecial {
  483. d.bdRead = false
  484. if vs == bincSpNan {
  485. return math.NaN()
  486. } else if vs == bincSpPosInf {
  487. return math.Inf(1)
  488. } else if vs == bincSpZeroFloat || vs == bincSpZero {
  489. return
  490. } else if vs == bincSpNegInf {
  491. return math.Inf(-1)
  492. } else {
  493. d.d.errorf("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs)
  494. return
  495. }
  496. } else if vd == bincVdFloat {
  497. f = d.decFloat()
  498. } else {
  499. f = float64(d.DecodeInt(64))
  500. }
  501. if chkOverflow32 && chkOvf.Float32(f) {
  502. d.d.errorf("binc: float32 overflow: %v", f)
  503. return
  504. }
  505. d.bdRead = false
  506. return
  507. }
  508. // bool can be decoded from bool only (single byte).
  509. func (d *bincDecDriver) DecodeBool() (b bool) {
  510. if !d.bdRead {
  511. d.readNextBd()
  512. }
  513. if bd := d.bd; bd == (bincVdSpecial | bincSpFalse) {
  514. // b = false
  515. } else if bd == (bincVdSpecial | bincSpTrue) {
  516. b = true
  517. } else {
  518. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  519. return
  520. }
  521. d.bdRead = false
  522. return
  523. }
  524. func (d *bincDecDriver) ReadMapStart() (length int) {
  525. if d.vd != bincVdMap {
  526. d.d.errorf("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd)
  527. return
  528. }
  529. length = d.decLen()
  530. d.bdRead = false
  531. return
  532. }
  533. func (d *bincDecDriver) ReadArrayStart() (length int) {
  534. if d.vd != bincVdArray {
  535. d.d.errorf("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd)
  536. return
  537. }
  538. length = d.decLen()
  539. d.bdRead = false
  540. return
  541. }
  542. func (d *bincDecDriver) decLen() int {
  543. if d.vs > 3 {
  544. return int(d.vs - 4)
  545. }
  546. return int(d.decLenNumber())
  547. }
  548. func (d *bincDecDriver) decLenNumber() (v uint64) {
  549. if x := d.vs; x == 0 {
  550. v = uint64(d.r.readn1())
  551. } else if x == 1 {
  552. d.r.readb(d.b[6:8])
  553. v = uint64(bigen.Uint16(d.b[6:8]))
  554. } else if x == 2 {
  555. d.r.readb(d.b[4:8])
  556. v = uint64(bigen.Uint32(d.b[4:8]))
  557. } else {
  558. d.r.readb(d.b[:8])
  559. v = bigen.Uint64(d.b[:8])
  560. }
  561. return
  562. }
  563. func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (bs2 []byte, s string) {
  564. if !d.bdRead {
  565. d.readNextBd()
  566. }
  567. if d.bd == bincVdSpecial<<4|bincSpNil {
  568. d.bdRead = false
  569. return
  570. }
  571. var slen int = -1
  572. // var ok bool
  573. switch d.vd {
  574. case bincVdString, bincVdByteArray:
  575. slen = d.decLen()
  576. if zerocopy {
  577. if d.br {
  578. bs2 = d.r.readx(slen)
  579. } else if len(bs) == 0 {
  580. bs2 = decByteSlice(d.r, slen, d.b[:])
  581. } else {
  582. bs2 = decByteSlice(d.r, slen, bs)
  583. }
  584. } else {
  585. bs2 = decByteSlice(d.r, slen, bs)
  586. }
  587. if withString {
  588. s = string(bs2)
  589. }
  590. case bincVdSymbol:
  591. // zerocopy doesn't apply for symbols,
  592. // as the values must be stored in a table for later use.
  593. //
  594. //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
  595. //extract symbol
  596. //if containsStringVal, read it and put in map
  597. //else look in map for string value
  598. var symbol uint16
  599. vs := d.vs
  600. if vs&0x8 == 0 {
  601. symbol = uint16(d.r.readn1())
  602. } else {
  603. symbol = uint16(bigen.Uint16(d.r.readx(2)))
  604. }
  605. if d.s == nil {
  606. d.s = make([]bincDecSymbol, 0, 16)
  607. }
  608. if vs&0x4 == 0 {
  609. for i := range d.s {
  610. j := &d.s[i]
  611. if j.i == symbol {
  612. bs2 = j.b
  613. if withString {
  614. if j.s == "" && bs2 != nil {
  615. j.s = string(bs2)
  616. }
  617. s = j.s
  618. }
  619. break
  620. }
  621. }
  622. } else {
  623. switch vs & 0x3 {
  624. case 0:
  625. slen = int(d.r.readn1())
  626. case 1:
  627. slen = int(bigen.Uint16(d.r.readx(2)))
  628. case 2:
  629. slen = int(bigen.Uint32(d.r.readx(4)))
  630. case 3:
  631. slen = int(bigen.Uint64(d.r.readx(8)))
  632. }
  633. // since using symbols, do not store any part of
  634. // the parameter bs in the map, as it might be a shared buffer.
  635. // bs2 = decByteSlice(d.r, slen, bs)
  636. bs2 = decByteSlice(d.r, slen, nil)
  637. if withString {
  638. s = string(bs2)
  639. }
  640. d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2})
  641. }
  642. default:
  643. d.d.errorf("Invalid d.vd. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x",
  644. bincVdString, bincVdByteArray, bincVdSymbol, d.vd)
  645. return
  646. }
  647. d.bdRead = false
  648. return
  649. }
  650. func (d *bincDecDriver) DecodeString() (s string) {
  651. // DecodeBytes does not accomodate symbols, whose impl stores string version in map.
  652. // Use decStringAndBytes directly.
  653. // return string(d.DecodeBytes(d.b[:], true, true))
  654. _, s = d.decStringAndBytes(d.b[:], true, true)
  655. return
  656. }
  657. func (d *bincDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
  658. if isstring {
  659. bsOut, _ = d.decStringAndBytes(bs, false, zerocopy)
  660. return
  661. }
  662. if !d.bdRead {
  663. d.readNextBd()
  664. }
  665. if d.bd == bincVdSpecial<<4|bincSpNil {
  666. d.bdRead = false
  667. return nil
  668. }
  669. var clen int
  670. if d.vd == bincVdString || d.vd == bincVdByteArray {
  671. clen = d.decLen()
  672. } else {
  673. d.d.errorf("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x",
  674. bincVdString, bincVdByteArray, d.vd)
  675. return
  676. }
  677. d.bdRead = false
  678. if zerocopy {
  679. if d.br {
  680. return d.r.readx(clen)
  681. } else if len(bs) == 0 {
  682. bs = d.b[:]
  683. }
  684. }
  685. return decByteSlice(d.r, clen, bs)
  686. }
  687. func (d *bincDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  688. if xtag > 0xff {
  689. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  690. return
  691. }
  692. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  693. realxtag = uint64(realxtag1)
  694. if ext == nil {
  695. re := rv.(*RawExt)
  696. re.Tag = realxtag
  697. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  698. } else {
  699. ext.ReadExt(rv, xbs)
  700. }
  701. return
  702. }
  703. func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  704. if !d.bdRead {
  705. d.readNextBd()
  706. }
  707. if d.vd == bincVdCustomExt {
  708. l := d.decLen()
  709. xtag = d.r.readn1()
  710. if verifyTag && xtag != tag {
  711. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  712. return
  713. }
  714. xbs = d.r.readx(l)
  715. } else if d.vd == bincVdByteArray {
  716. xbs = d.DecodeBytes(nil, false, true)
  717. } else {
  718. d.d.errorf("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd)
  719. return
  720. }
  721. d.bdRead = false
  722. return
  723. }
  724. func (d *bincDecDriver) DecodeNaked() {
  725. if !d.bdRead {
  726. d.readNextBd()
  727. }
  728. n := &d.d.n
  729. var decodeFurther bool
  730. switch d.vd {
  731. case bincVdSpecial:
  732. switch d.vs {
  733. case bincSpNil:
  734. n.v = valueTypeNil
  735. case bincSpFalse:
  736. n.v = valueTypeBool
  737. n.b = false
  738. case bincSpTrue:
  739. n.v = valueTypeBool
  740. n.b = true
  741. case bincSpNan:
  742. n.v = valueTypeFloat
  743. n.f = math.NaN()
  744. case bincSpPosInf:
  745. n.v = valueTypeFloat
  746. n.f = math.Inf(1)
  747. case bincSpNegInf:
  748. n.v = valueTypeFloat
  749. n.f = math.Inf(-1)
  750. case bincSpZeroFloat:
  751. n.v = valueTypeFloat
  752. n.f = float64(0)
  753. case bincSpZero:
  754. n.v = valueTypeUint
  755. n.u = uint64(0) // int8(0)
  756. case bincSpNegOne:
  757. n.v = valueTypeInt
  758. n.i = int64(-1) // int8(-1)
  759. default:
  760. d.d.errorf("decodeNaked: Unrecognized special value 0x%x", d.vs)
  761. }
  762. case bincVdSmallInt:
  763. n.v = valueTypeUint
  764. n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
  765. case bincVdPosInt:
  766. n.v = valueTypeUint
  767. n.u = d.decUint()
  768. case bincVdNegInt:
  769. n.v = valueTypeInt
  770. n.i = -(int64(d.decUint()))
  771. case bincVdFloat:
  772. n.v = valueTypeFloat
  773. n.f = d.decFloat()
  774. case bincVdSymbol:
  775. n.v = valueTypeSymbol
  776. n.s = d.DecodeString()
  777. case bincVdString:
  778. n.v = valueTypeString
  779. n.s = d.DecodeString()
  780. case bincVdByteArray:
  781. n.v = valueTypeBytes
  782. n.l = d.DecodeBytes(nil, false, false)
  783. case bincVdTimestamp:
  784. n.v = valueTypeTimestamp
  785. tt, err := decodeTime(d.r.readx(int(d.vs)))
  786. if err != nil {
  787. panic(err)
  788. }
  789. n.t = tt
  790. case bincVdCustomExt:
  791. n.v = valueTypeExt
  792. l := d.decLen()
  793. n.u = uint64(d.r.readn1())
  794. n.l = d.r.readx(l)
  795. case bincVdArray:
  796. n.v = valueTypeArray
  797. decodeFurther = true
  798. case bincVdMap:
  799. n.v = valueTypeMap
  800. decodeFurther = true
  801. default:
  802. d.d.errorf("decodeNaked: Unrecognized d.vd: 0x%x", d.vd)
  803. }
  804. if !decodeFurther {
  805. d.bdRead = false
  806. }
  807. if n.v == valueTypeUint && d.h.SignedInteger {
  808. n.v = valueTypeInt
  809. n.i = int64(n.u)
  810. }
  811. return
  812. }
  813. //------------------------------------
  814. //BincHandle is a Handle for the Binc Schema-Free Encoding Format
  815. //defined at https://github.com/ugorji/binc .
  816. //
  817. //BincHandle currently supports all Binc features with the following EXCEPTIONS:
  818. // - only integers up to 64 bits of precision are supported.
  819. // big integers are unsupported.
  820. // - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
  821. // extended precision and decimal IEEE 754 floats are unsupported.
  822. // - Only UTF-8 strings supported.
  823. // Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
  824. //
  825. //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
  826. type BincHandle struct {
  827. BasicHandle
  828. binaryEncodingType
  829. }
  830. func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  831. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  832. }
  833. func (h *BincHandle) newEncDriver(e *Encoder) encDriver {
  834. return &bincEncDriver{e: e, w: e.w}
  835. }
  836. func (h *BincHandle) newDecDriver(d *Decoder) decDriver {
  837. return &bincDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  838. }
  839. func (e *bincEncDriver) reset() {
  840. e.w = e.e.w
  841. }
  842. func (d *bincDecDriver) reset() {
  843. d.r = d.d.r
  844. }
  845. var _ decDriver = (*bincDecDriver)(nil)
  846. var _ encDriver = (*bincEncDriver)(nil)