binc.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. // Copyright (c) 2012-2018 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. h *BincHandle
  51. w encWriter
  52. m map[string]uint16 // symbols
  53. b [scratchByteArrayLen]byte
  54. s uint16 // symbols sequencer
  55. // c containerState
  56. encDriverTrackContainerWriter
  57. noBuiltInTypes
  58. // encNoSeparator
  59. }
  60. func (e *bincEncDriver) EncodeNil() {
  61. e.w.writen1(bincVdSpecial<<4 | bincSpNil)
  62. }
  63. func (e *bincEncDriver) EncodeTime(t time.Time) {
  64. if t.IsZero() {
  65. e.EncodeNil()
  66. } else {
  67. bs := bincEncodeTime(t)
  68. e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
  69. e.w.writeb(bs)
  70. }
  71. }
  72. func (e *bincEncDriver) EncodeBool(b bool) {
  73. if b {
  74. e.w.writen1(bincVdSpecial<<4 | bincSpTrue)
  75. } else {
  76. e.w.writen1(bincVdSpecial<<4 | bincSpFalse)
  77. }
  78. }
  79. func (e *bincEncDriver) EncodeFloat32(f float32) {
  80. if f == 0 {
  81. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  82. return
  83. }
  84. e.w.writen1(bincVdFloat<<4 | bincFlBin32)
  85. bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
  86. }
  87. func (e *bincEncDriver) EncodeFloat64(f float64) {
  88. if f == 0 {
  89. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  90. return
  91. }
  92. bigen.PutUint64(e.b[:8], math.Float64bits(f))
  93. if bincDoPrune {
  94. i := 7
  95. for ; i >= 0 && (e.b[i] == 0); i-- {
  96. }
  97. i++
  98. if i <= 6 {
  99. e.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
  100. e.w.writen1(byte(i))
  101. e.w.writeb(e.b[:i])
  102. return
  103. }
  104. }
  105. e.w.writen1(bincVdFloat<<4 | bincFlBin64)
  106. e.w.writeb(e.b[:8])
  107. }
  108. func (e *bincEncDriver) encIntegerPrune(bd byte, pos bool, v uint64, lim uint8) {
  109. if lim == 4 {
  110. bigen.PutUint32(e.b[:lim], uint32(v))
  111. } else {
  112. bigen.PutUint64(e.b[:lim], v)
  113. }
  114. if bincDoPrune {
  115. i := pruneSignExt(e.b[:lim], pos)
  116. e.w.writen1(bd | lim - 1 - byte(i))
  117. e.w.writeb(e.b[i:lim])
  118. } else {
  119. e.w.writen1(bd | lim - 1)
  120. e.w.writeb(e.b[:lim])
  121. }
  122. }
  123. func (e *bincEncDriver) EncodeInt(v int64) {
  124. const nbd byte = bincVdNegInt << 4
  125. if v >= 0 {
  126. e.encUint(bincVdPosInt<<4, true, uint64(v))
  127. } else if v == -1 {
  128. e.w.writen1(bincVdSpecial<<4 | bincSpNegOne)
  129. } else {
  130. e.encUint(bincVdNegInt<<4, false, uint64(-v))
  131. }
  132. }
  133. func (e *bincEncDriver) EncodeUint(v uint64) {
  134. e.encUint(bincVdPosInt<<4, true, v)
  135. }
  136. func (e *bincEncDriver) encUint(bd byte, pos bool, v uint64) {
  137. if v == 0 {
  138. e.w.writen1(bincVdSpecial<<4 | bincSpZero)
  139. } else if pos && v >= 1 && v <= 16 {
  140. e.w.writen1(bincVdSmallInt<<4 | byte(v-1))
  141. } else if v <= math.MaxUint8 {
  142. e.w.writen2(bd|0x0, byte(v))
  143. } else if v <= math.MaxUint16 {
  144. e.w.writen1(bd | 0x01)
  145. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  146. } else if v <= math.MaxUint32 {
  147. e.encIntegerPrune(bd, pos, v, 4)
  148. } else {
  149. e.encIntegerPrune(bd, pos, v, 8)
  150. }
  151. }
  152. func (e *bincEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
  153. bs := ext.WriteExt(rv)
  154. if bs == nil {
  155. e.EncodeNil()
  156. return
  157. }
  158. e.encodeExtPreamble(uint8(xtag), len(bs))
  159. e.w.writeb(bs)
  160. }
  161. func (e *bincEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  162. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  163. e.w.writeb(re.Data)
  164. }
  165. func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
  166. e.encLen(bincVdCustomExt<<4, uint64(length))
  167. e.w.writen1(xtag)
  168. }
  169. func (e *bincEncDriver) WriteArrayStart(length int) {
  170. e.encLen(bincVdArray<<4, uint64(length))
  171. e.c = containerArrayStart
  172. }
  173. func (e *bincEncDriver) WriteMapStart(length int) {
  174. e.encLen(bincVdMap<<4, uint64(length))
  175. e.c = containerMapStart
  176. }
  177. func (e *bincEncDriver) EncodeString(c charEncoding, v string) {
  178. if e.c == containerMapKey && c == cUTF8 && (e.h.AsSymbols == 0 || e.h.AsSymbols == 1) {
  179. e.EncodeSymbol(v)
  180. return
  181. }
  182. l := uint64(len(v))
  183. e.encBytesLen(c, l)
  184. if l > 0 {
  185. e.w.writestr(v)
  186. }
  187. }
  188. func (e *bincEncDriver) EncodeSymbol(v string) {
  189. // if WriteSymbolsNoRefs {
  190. // e.encodeString(cUTF8, v)
  191. // return
  192. // }
  193. //symbols only offer benefit when string length > 1.
  194. //This is because strings with length 1 take only 2 bytes to store
  195. //(bd with embedded length, and single byte for string val).
  196. l := len(v)
  197. if l == 0 {
  198. e.encBytesLen(cUTF8, 0)
  199. return
  200. } else if l == 1 {
  201. e.encBytesLen(cUTF8, 1)
  202. e.w.writen1(v[0])
  203. return
  204. }
  205. if e.m == nil {
  206. e.m = make(map[string]uint16, 16)
  207. }
  208. ui, ok := e.m[v]
  209. if ok {
  210. if ui <= math.MaxUint8 {
  211. e.w.writen2(bincVdSymbol<<4, byte(ui))
  212. } else {
  213. e.w.writen1(bincVdSymbol<<4 | 0x8)
  214. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  215. }
  216. } else {
  217. e.s++
  218. ui = e.s
  219. //ui = uint16(atomic.AddUint32(&e.s, 1))
  220. e.m[v] = ui
  221. var lenprec uint8
  222. if l <= math.MaxUint8 {
  223. // lenprec = 0
  224. } else if l <= math.MaxUint16 {
  225. lenprec = 1
  226. } else if int64(l) <= math.MaxUint32 {
  227. lenprec = 2
  228. } else {
  229. lenprec = 3
  230. }
  231. if ui <= math.MaxUint8 {
  232. e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
  233. } else {
  234. e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
  235. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  236. }
  237. if lenprec == 0 {
  238. e.w.writen1(byte(l))
  239. } else if lenprec == 1 {
  240. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(l))
  241. } else if lenprec == 2 {
  242. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(l))
  243. } else {
  244. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(l))
  245. }
  246. e.w.writestr(v)
  247. }
  248. }
  249. func (e *bincEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
  250. if v == nil {
  251. e.EncodeNil()
  252. return
  253. }
  254. l := uint64(len(v))
  255. e.encBytesLen(c, l)
  256. if l > 0 {
  257. e.w.writeb(v)
  258. }
  259. }
  260. func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
  261. //TODO: support bincUnicodeOther (for now, just use string or bytearray)
  262. if c == cRAW {
  263. e.encLen(bincVdByteArray<<4, length)
  264. } else {
  265. e.encLen(bincVdString<<4, length)
  266. }
  267. }
  268. func (e *bincEncDriver) encLen(bd byte, l uint64) {
  269. if l < 12 {
  270. e.w.writen1(bd | uint8(l+4))
  271. } else {
  272. e.encLenNumber(bd, l)
  273. }
  274. }
  275. func (e *bincEncDriver) encLenNumber(bd byte, v uint64) {
  276. if v <= math.MaxUint8 {
  277. e.w.writen2(bd, byte(v))
  278. } else if v <= math.MaxUint16 {
  279. e.w.writen1(bd | 0x01)
  280. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  281. } else if v <= math.MaxUint32 {
  282. e.w.writen1(bd | 0x02)
  283. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
  284. } else {
  285. e.w.writen1(bd | 0x03)
  286. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(v))
  287. }
  288. }
  289. //------------------------------------
  290. type bincDecSymbol struct {
  291. s string
  292. b []byte
  293. i uint16
  294. }
  295. type bincDecDriver struct {
  296. d *Decoder
  297. h *BincHandle
  298. r decReader
  299. br bool // bytes reader
  300. bdRead bool
  301. bd byte
  302. vd byte
  303. vs byte
  304. // noStreamingCodec
  305. // decNoSeparator
  306. b [scratchByteArrayLen]byte
  307. // linear searching on this slice is ok,
  308. // because we typically expect < 32 symbols in each stream.
  309. s []bincDecSymbol
  310. decDriverNoopContainerReader
  311. noBuiltInTypes
  312. }
  313. func (d *bincDecDriver) readNextBd() {
  314. d.bd = d.r.readn1()
  315. d.vd = d.bd >> 4
  316. d.vs = d.bd & 0x0f
  317. d.bdRead = true
  318. }
  319. func (d *bincDecDriver) uncacheRead() {
  320. if d.bdRead {
  321. d.r.unreadn1()
  322. d.bdRead = false
  323. }
  324. }
  325. func (d *bincDecDriver) ContainerType() (vt valueType) {
  326. if !d.bdRead {
  327. d.readNextBd()
  328. }
  329. if d.vd == bincVdSpecial && d.vs == bincSpNil {
  330. return valueTypeNil
  331. } else if d.vd == bincVdByteArray {
  332. return valueTypeBytes
  333. } else if d.vd == bincVdString {
  334. return valueTypeString
  335. } else if d.vd == bincVdArray {
  336. return valueTypeArray
  337. } else if d.vd == bincVdMap {
  338. return valueTypeMap
  339. }
  340. // else {
  341. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  342. // }
  343. return valueTypeUnset
  344. }
  345. func (d *bincDecDriver) TryDecodeAsNil() bool {
  346. if !d.bdRead {
  347. d.readNextBd()
  348. }
  349. if d.bd == bincVdSpecial<<4|bincSpNil {
  350. d.bdRead = false
  351. return true
  352. }
  353. return false
  354. }
  355. func (d *bincDecDriver) DecodeTime() (t time.Time) {
  356. if !d.bdRead {
  357. d.readNextBd()
  358. }
  359. if d.bd == bincVdSpecial<<4|bincSpNil {
  360. d.bdRead = false
  361. return
  362. }
  363. if d.vd != bincVdTimestamp {
  364. d.d.errorf("Invalid d.vd. Expecting 0x%x. Received: 0x%x", bincVdTimestamp, d.vd)
  365. return
  366. }
  367. t, err := bincDecodeTime(d.r.readx(int(d.vs)))
  368. if err != nil {
  369. panic(err)
  370. }
  371. d.bdRead = false
  372. return
  373. }
  374. func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
  375. if vs&0x8 == 0 {
  376. d.r.readb(d.b[0:defaultLen])
  377. } else {
  378. l := d.r.readn1()
  379. if l > 8 {
  380. d.d.errorf("At most 8 bytes used to represent float. Received: %v bytes", l)
  381. return
  382. }
  383. for i := l; i < 8; i++ {
  384. d.b[i] = 0
  385. }
  386. d.r.readb(d.b[0:l])
  387. }
  388. }
  389. func (d *bincDecDriver) decFloat() (f float64) {
  390. //if true { f = math.Float64frombits(bigen.Uint64(d.r.readx(8))); break; }
  391. if x := d.vs & 0x7; x == bincFlBin32 {
  392. d.decFloatPre(d.vs, 4)
  393. f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
  394. } else if x == bincFlBin64 {
  395. d.decFloatPre(d.vs, 8)
  396. f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
  397. } else {
  398. d.d.errorf("only float32 and float64 are supported. d.vd: 0x%x, d.vs: 0x%x", d.vd, d.vs)
  399. return
  400. }
  401. return
  402. }
  403. func (d *bincDecDriver) decUint() (v uint64) {
  404. // need to inline the code (interface conversion and type assertion expensive)
  405. switch d.vs {
  406. case 0:
  407. v = uint64(d.r.readn1())
  408. case 1:
  409. d.r.readb(d.b[6:8])
  410. v = uint64(bigen.Uint16(d.b[6:8]))
  411. case 2:
  412. d.b[4] = 0
  413. d.r.readb(d.b[5:8])
  414. v = uint64(bigen.Uint32(d.b[4:8]))
  415. case 3:
  416. d.r.readb(d.b[4:8])
  417. v = uint64(bigen.Uint32(d.b[4:8]))
  418. case 4, 5, 6:
  419. lim := int(7 - d.vs)
  420. d.r.readb(d.b[lim:8])
  421. for i := 0; i < lim; i++ {
  422. d.b[i] = 0
  423. }
  424. v = uint64(bigen.Uint64(d.b[:8]))
  425. case 7:
  426. d.r.readb(d.b[:8])
  427. v = uint64(bigen.Uint64(d.b[:8]))
  428. default:
  429. d.d.errorf("unsigned integers with greater than 64 bits of precision not supported")
  430. return
  431. }
  432. return
  433. }
  434. func (d *bincDecDriver) decCheckInteger() (ui uint64, neg bool) {
  435. if !d.bdRead {
  436. d.readNextBd()
  437. }
  438. vd, vs := d.vd, d.vs
  439. if vd == bincVdPosInt {
  440. ui = d.decUint()
  441. } else if vd == bincVdNegInt {
  442. ui = d.decUint()
  443. neg = true
  444. } else if vd == bincVdSmallInt {
  445. ui = uint64(d.vs) + 1
  446. } else if vd == bincVdSpecial {
  447. if vs == bincSpZero {
  448. //i = 0
  449. } else if vs == bincSpNegOne {
  450. neg = true
  451. ui = 1
  452. } else {
  453. d.d.errorf("numeric decode fails for special value: d.vs: 0x%x", d.vs)
  454. return
  455. }
  456. } else {
  457. 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)
  458. return
  459. }
  460. return
  461. }
  462. func (d *bincDecDriver) DecodeInt64() (i int64) {
  463. ui, neg := d.decCheckInteger()
  464. i = chkOvf.SignedIntV(ui)
  465. if neg {
  466. i = -i
  467. }
  468. d.bdRead = false
  469. return
  470. }
  471. func (d *bincDecDriver) DecodeUint64() (ui uint64) {
  472. ui, neg := d.decCheckInteger()
  473. if neg {
  474. d.d.errorf("Assigning negative signed value to unsigned type")
  475. return
  476. }
  477. d.bdRead = false
  478. return
  479. }
  480. func (d *bincDecDriver) DecodeFloat64() (f float64) {
  481. if !d.bdRead {
  482. d.readNextBd()
  483. }
  484. vd, vs := d.vd, d.vs
  485. if vd == bincVdSpecial {
  486. d.bdRead = false
  487. if vs == bincSpNan {
  488. return math.NaN()
  489. } else if vs == bincSpPosInf {
  490. return math.Inf(1)
  491. } else if vs == bincSpZeroFloat || vs == bincSpZero {
  492. return
  493. } else if vs == bincSpNegInf {
  494. return math.Inf(-1)
  495. } else {
  496. d.d.errorf("Invalid d.vs decoding float where d.vd=bincVdSpecial: %v", d.vs)
  497. return
  498. }
  499. } else if vd == bincVdFloat {
  500. f = d.decFloat()
  501. } else {
  502. f = float64(d.DecodeInt64())
  503. }
  504. d.bdRead = false
  505. return
  506. }
  507. // bool can be decoded from bool only (single byte).
  508. func (d *bincDecDriver) DecodeBool() (b bool) {
  509. if !d.bdRead {
  510. d.readNextBd()
  511. }
  512. if bd := d.bd; bd == (bincVdSpecial | bincSpFalse) {
  513. // b = false
  514. } else if bd == (bincVdSpecial | bincSpTrue) {
  515. b = true
  516. } else {
  517. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  518. return
  519. }
  520. d.bdRead = false
  521. return
  522. }
  523. func (d *bincDecDriver) ReadMapStart() (length int) {
  524. if !d.bdRead {
  525. d.readNextBd()
  526. }
  527. if d.vd != bincVdMap {
  528. d.d.errorf("Invalid d.vd for map. Expecting 0x%x. Got: 0x%x", bincVdMap, d.vd)
  529. return
  530. }
  531. length = d.decLen()
  532. d.bdRead = false
  533. return
  534. }
  535. func (d *bincDecDriver) ReadArrayStart() (length int) {
  536. if !d.bdRead {
  537. d.readNextBd()
  538. }
  539. if d.vd != bincVdArray {
  540. d.d.errorf("Invalid d.vd for array. Expecting 0x%x. Got: 0x%x", bincVdArray, d.vd)
  541. return
  542. }
  543. length = d.decLen()
  544. d.bdRead = false
  545. return
  546. }
  547. func (d *bincDecDriver) decLen() int {
  548. if d.vs > 3 {
  549. return int(d.vs - 4)
  550. }
  551. return int(d.decLenNumber())
  552. }
  553. func (d *bincDecDriver) decLenNumber() (v uint64) {
  554. if x := d.vs; x == 0 {
  555. v = uint64(d.r.readn1())
  556. } else if x == 1 {
  557. d.r.readb(d.b[6:8])
  558. v = uint64(bigen.Uint16(d.b[6:8]))
  559. } else if x == 2 {
  560. d.r.readb(d.b[4:8])
  561. v = uint64(bigen.Uint32(d.b[4:8]))
  562. } else {
  563. d.r.readb(d.b[:8])
  564. v = bigen.Uint64(d.b[:8])
  565. }
  566. return
  567. }
  568. func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (bs2 []byte, s string) {
  569. if !d.bdRead {
  570. d.readNextBd()
  571. }
  572. if d.bd == bincVdSpecial<<4|bincSpNil {
  573. d.bdRead = false
  574. return
  575. }
  576. var slen = -1
  577. // var ok bool
  578. switch d.vd {
  579. case bincVdString, bincVdByteArray:
  580. slen = d.decLen()
  581. if zerocopy {
  582. if d.br {
  583. bs2 = d.r.readx(slen)
  584. } else if len(bs) == 0 {
  585. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, d.b[:])
  586. } else {
  587. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs)
  588. }
  589. } else {
  590. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs)
  591. }
  592. if withString {
  593. s = string(bs2)
  594. }
  595. case bincVdSymbol:
  596. // zerocopy doesn't apply for symbols,
  597. // as the values must be stored in a table for later use.
  598. //
  599. //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
  600. //extract symbol
  601. //if containsStringVal, read it and put in map
  602. //else look in map for string value
  603. var symbol uint16
  604. vs := d.vs
  605. if vs&0x8 == 0 {
  606. symbol = uint16(d.r.readn1())
  607. } else {
  608. symbol = uint16(bigen.Uint16(d.r.readx(2)))
  609. }
  610. if d.s == nil {
  611. d.s = make([]bincDecSymbol, 0, 16)
  612. }
  613. if vs&0x4 == 0 {
  614. for i := range d.s {
  615. j := &d.s[i]
  616. if j.i == symbol {
  617. bs2 = j.b
  618. if withString {
  619. if j.s == "" && bs2 != nil {
  620. j.s = string(bs2)
  621. }
  622. s = j.s
  623. }
  624. break
  625. }
  626. }
  627. } else {
  628. switch vs & 0x3 {
  629. case 0:
  630. slen = int(d.r.readn1())
  631. case 1:
  632. slen = int(bigen.Uint16(d.r.readx(2)))
  633. case 2:
  634. slen = int(bigen.Uint32(d.r.readx(4)))
  635. case 3:
  636. slen = int(bigen.Uint64(d.r.readx(8)))
  637. }
  638. // since using symbols, do not store any part of
  639. // the parameter bs in the map, as it might be a shared buffer.
  640. // bs2 = decByteSlice(d.r, slen, bs)
  641. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, nil)
  642. if withString {
  643. s = string(bs2)
  644. }
  645. d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2})
  646. }
  647. default:
  648. d.d.errorf("Invalid d.vd. Expecting string:0x%x, bytearray:0x%x or symbol: 0x%x. Got: 0x%x",
  649. bincVdString, bincVdByteArray, bincVdSymbol, d.vd)
  650. return
  651. }
  652. d.bdRead = false
  653. return
  654. }
  655. func (d *bincDecDriver) DecodeString() (s string) {
  656. // DecodeBytes does not accommodate symbols, whose impl stores string version in map.
  657. // Use decStringAndBytes directly.
  658. // return string(d.DecodeBytes(d.b[:], true, true))
  659. _, s = d.decStringAndBytes(d.b[:], true, true)
  660. return
  661. }
  662. func (d *bincDecDriver) DecodeStringAsBytes() (s []byte) {
  663. s, _ = d.decStringAndBytes(d.b[:], false, true)
  664. return
  665. }
  666. func (d *bincDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  667. if !d.bdRead {
  668. d.readNextBd()
  669. }
  670. if d.bd == bincVdSpecial<<4|bincSpNil {
  671. d.bdRead = false
  672. return nil
  673. }
  674. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  675. if d.vd == bincVdArray {
  676. bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  677. return
  678. }
  679. var clen int
  680. if d.vd == bincVdString || d.vd == bincVdByteArray {
  681. clen = d.decLen()
  682. } else {
  683. d.d.errorf("Invalid d.vd for bytes. Expecting string:0x%x or bytearray:0x%x. Got: 0x%x",
  684. bincVdString, bincVdByteArray, d.vd)
  685. return
  686. }
  687. d.bdRead = false
  688. if zerocopy {
  689. if d.br {
  690. return d.r.readx(clen)
  691. } else if len(bs) == 0 {
  692. bs = d.b[:]
  693. }
  694. }
  695. return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs)
  696. }
  697. func (d *bincDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  698. if xtag > 0xff {
  699. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  700. return
  701. }
  702. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  703. realxtag = uint64(realxtag1)
  704. if ext == nil {
  705. re := rv.(*RawExt)
  706. re.Tag = realxtag
  707. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  708. } else {
  709. ext.ReadExt(rv, xbs)
  710. }
  711. return
  712. }
  713. func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  714. if !d.bdRead {
  715. d.readNextBd()
  716. }
  717. if d.vd == bincVdCustomExt {
  718. l := d.decLen()
  719. xtag = d.r.readn1()
  720. if verifyTag && xtag != tag {
  721. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  722. return
  723. }
  724. xbs = d.r.readx(l)
  725. } else if d.vd == bincVdByteArray {
  726. xbs = d.DecodeBytes(nil, true)
  727. } else {
  728. d.d.errorf("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.vd)
  729. return
  730. }
  731. d.bdRead = false
  732. return
  733. }
  734. func (d *bincDecDriver) DecodeNaked() {
  735. if !d.bdRead {
  736. d.readNextBd()
  737. }
  738. n := d.d.n
  739. var decodeFurther bool
  740. switch d.vd {
  741. case bincVdSpecial:
  742. switch d.vs {
  743. case bincSpNil:
  744. n.v = valueTypeNil
  745. case bincSpFalse:
  746. n.v = valueTypeBool
  747. n.b = false
  748. case bincSpTrue:
  749. n.v = valueTypeBool
  750. n.b = true
  751. case bincSpNan:
  752. n.v = valueTypeFloat
  753. n.f = math.NaN()
  754. case bincSpPosInf:
  755. n.v = valueTypeFloat
  756. n.f = math.Inf(1)
  757. case bincSpNegInf:
  758. n.v = valueTypeFloat
  759. n.f = math.Inf(-1)
  760. case bincSpZeroFloat:
  761. n.v = valueTypeFloat
  762. n.f = float64(0)
  763. case bincSpZero:
  764. n.v = valueTypeUint
  765. n.u = uint64(0) // int8(0)
  766. case bincSpNegOne:
  767. n.v = valueTypeInt
  768. n.i = int64(-1) // int8(-1)
  769. default:
  770. d.d.errorf("decodeNaked: Unrecognized special value 0x%x", d.vs)
  771. }
  772. case bincVdSmallInt:
  773. n.v = valueTypeUint
  774. n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
  775. case bincVdPosInt:
  776. n.v = valueTypeUint
  777. n.u = d.decUint()
  778. case bincVdNegInt:
  779. n.v = valueTypeInt
  780. n.i = -(int64(d.decUint()))
  781. case bincVdFloat:
  782. n.v = valueTypeFloat
  783. n.f = d.decFloat()
  784. case bincVdSymbol:
  785. n.v = valueTypeSymbol
  786. n.s = d.DecodeString()
  787. case bincVdString:
  788. n.v = valueTypeString
  789. n.s = d.DecodeString()
  790. case bincVdByteArray:
  791. n.v = valueTypeBytes
  792. n.l = d.DecodeBytes(nil, false)
  793. case bincVdTimestamp:
  794. n.v = valueTypeTime
  795. tt, err := bincDecodeTime(d.r.readx(int(d.vs)))
  796. if err != nil {
  797. panic(err)
  798. }
  799. n.t = tt
  800. case bincVdCustomExt:
  801. n.v = valueTypeExt
  802. l := d.decLen()
  803. n.u = uint64(d.r.readn1())
  804. n.l = d.r.readx(l)
  805. case bincVdArray:
  806. n.v = valueTypeArray
  807. decodeFurther = true
  808. case bincVdMap:
  809. n.v = valueTypeMap
  810. decodeFurther = true
  811. default:
  812. d.d.errorf("decodeNaked: Unrecognized d.vd: 0x%x", d.vd)
  813. }
  814. if !decodeFurther {
  815. d.bdRead = false
  816. }
  817. if n.v == valueTypeUint && d.h.SignedInteger {
  818. n.v = valueTypeInt
  819. n.i = int64(n.u)
  820. }
  821. return
  822. }
  823. //------------------------------------
  824. //BincHandle is a Handle for the Binc Schema-Free Encoding Format
  825. //defined at https://github.com/ugorji/binc .
  826. //
  827. //BincHandle currently supports all Binc features with the following EXCEPTIONS:
  828. // - only integers up to 64 bits of precision are supported.
  829. // big integers are unsupported.
  830. // - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
  831. // extended precision and decimal IEEE 754 floats are unsupported.
  832. // - Only UTF-8 strings supported.
  833. // Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
  834. //
  835. //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
  836. type BincHandle struct {
  837. BasicHandle
  838. binaryEncodingType
  839. noElemSeparators
  840. // AsSymbols defines what should be encoded as symbols.
  841. //
  842. // Encoding as symbols can reduce the encoded size significantly.
  843. //
  844. // However, during decoding, each string to be encoded as a symbol must
  845. // be checked to see if it has been seen before. Consequently, encoding time
  846. // will increase if using symbols, because string comparisons has a clear cost.
  847. //
  848. // Values:
  849. // - 0: default: library uses best judgement
  850. // - 1: use symbols
  851. // - 2: do not use symbols
  852. AsSymbols byte
  853. // AsSymbols: may later on introduce more options ...
  854. // - m: map keys
  855. // - s: struct fields
  856. // - n: none
  857. // - a: all: same as m, s, ...
  858. }
  859. // Name returns the name of the handle: binc
  860. func (h *BincHandle) Name() string { return "binc" }
  861. // SetBytesExt sets an extension
  862. func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  863. return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
  864. }
  865. func (h *BincHandle) newEncDriver(e *Encoder) encDriver {
  866. return &bincEncDriver{e: e, h: h, w: e.w}
  867. }
  868. func (h *BincHandle) newDecDriver(d *Decoder) decDriver {
  869. return &bincDecDriver{d: d, h: h, r: d.r, br: d.bytes}
  870. }
  871. func (e *bincEncDriver) reset() {
  872. e.w = e.e.w
  873. e.s = 0
  874. e.c = 0
  875. e.m = nil
  876. }
  877. func (d *bincDecDriver) reset() {
  878. d.r, d.br = d.d.r, d.d.bytes
  879. d.s = nil
  880. d.bd, d.bdRead, d.vd, d.vs = 0, false, 0, 0
  881. }
  882. // var timeDigits = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
  883. // EncodeTime encodes a time.Time as a []byte, including
  884. // information on the instant in time and UTC offset.
  885. //
  886. // Format Description
  887. //
  888. // A timestamp is composed of 3 components:
  889. //
  890. // - secs: signed integer representing seconds since unix epoch
  891. // - nsces: unsigned integer representing fractional seconds as a
  892. // nanosecond offset within secs, in the range 0 <= nsecs < 1e9
  893. // - tz: signed integer representing timezone offset in minutes east of UTC,
  894. // and a dst (daylight savings time) flag
  895. //
  896. // When encoding a timestamp, the first byte is the descriptor, which
  897. // defines which components are encoded and how many bytes are used to
  898. // encode secs and nsecs components. *If secs/nsecs is 0 or tz is UTC, it
  899. // is not encoded in the byte array explicitly*.
  900. //
  901. // Descriptor 8 bits are of the form `A B C DDD EE`:
  902. // A: Is secs component encoded? 1 = true
  903. // B: Is nsecs component encoded? 1 = true
  904. // C: Is tz component encoded? 1 = true
  905. // DDD: Number of extra bytes for secs (range 0-7).
  906. // If A = 1, secs encoded in DDD+1 bytes.
  907. // If A = 0, secs is not encoded, and is assumed to be 0.
  908. // If A = 1, then we need at least 1 byte to encode secs.
  909. // DDD says the number of extra bytes beyond that 1.
  910. // E.g. if DDD=0, then secs is represented in 1 byte.
  911. // if DDD=2, then secs is represented in 3 bytes.
  912. // EE: Number of extra bytes for nsecs (range 0-3).
  913. // If B = 1, nsecs encoded in EE+1 bytes (similar to secs/DDD above)
  914. //
  915. // Following the descriptor bytes, subsequent bytes are:
  916. //
  917. // secs component encoded in `DDD + 1` bytes (if A == 1)
  918. // nsecs component encoded in `EE + 1` bytes (if B == 1)
  919. // tz component encoded in 2 bytes (if C == 1)
  920. //
  921. // secs and nsecs components are integers encoded in a BigEndian
  922. // 2-complement encoding format.
  923. //
  924. // tz component is encoded as 2 bytes (16 bits). Most significant bit 15 to
  925. // Least significant bit 0 are described below:
  926. //
  927. // Timezone offset has a range of -12:00 to +14:00 (ie -720 to +840 minutes).
  928. // Bit 15 = have\_dst: set to 1 if we set the dst flag.
  929. // Bit 14 = dst\_on: set to 1 if dst is in effect at the time, or 0 if not.
  930. // Bits 13..0 = timezone offset in minutes. It is a signed integer in Big Endian format.
  931. //
  932. func bincEncodeTime(t time.Time) []byte {
  933. //t := rv.Interface().(time.Time)
  934. tsecs, tnsecs := t.Unix(), t.Nanosecond()
  935. var (
  936. bd byte
  937. btmp [8]byte
  938. bs [16]byte
  939. i int = 1
  940. )
  941. l := t.Location()
  942. if l == time.UTC {
  943. l = nil
  944. }
  945. if tsecs != 0 {
  946. bd = bd | 0x80
  947. bigen.PutUint64(btmp[:], uint64(tsecs))
  948. f := pruneSignExt(btmp[:], tsecs >= 0)
  949. bd = bd | (byte(7-f) << 2)
  950. copy(bs[i:], btmp[f:])
  951. i = i + (8 - f)
  952. }
  953. if tnsecs != 0 {
  954. bd = bd | 0x40
  955. bigen.PutUint32(btmp[:4], uint32(tnsecs))
  956. f := pruneSignExt(btmp[:4], true)
  957. bd = bd | byte(3-f)
  958. copy(bs[i:], btmp[f:4])
  959. i = i + (4 - f)
  960. }
  961. if l != nil {
  962. bd = bd | 0x20
  963. // Note that Go Libs do not give access to dst flag.
  964. _, zoneOffset := t.Zone()
  965. //zoneName, zoneOffset := t.Zone()
  966. zoneOffset /= 60
  967. z := uint16(zoneOffset)
  968. bigen.PutUint16(btmp[:2], z)
  969. // clear dst flags
  970. bs[i] = btmp[0] & 0x3f
  971. bs[i+1] = btmp[1]
  972. i = i + 2
  973. }
  974. bs[0] = bd
  975. return bs[0:i]
  976. }
  977. // bincDecodeTime decodes a []byte into a time.Time.
  978. func bincDecodeTime(bs []byte) (tt time.Time, err error) {
  979. bd := bs[0]
  980. var (
  981. tsec int64
  982. tnsec uint32
  983. tz uint16
  984. i byte = 1
  985. i2 byte
  986. n byte
  987. )
  988. if bd&(1<<7) != 0 {
  989. var btmp [8]byte
  990. n = ((bd >> 2) & 0x7) + 1
  991. i2 = i + n
  992. copy(btmp[8-n:], bs[i:i2])
  993. //if first bit of bs[i] is set, then fill btmp[0..8-n] with 0xff (ie sign extend it)
  994. if bs[i]&(1<<7) != 0 {
  995. copy(btmp[0:8-n], bsAll0xff)
  996. //for j,k := byte(0), 8-n; j < k; j++ { btmp[j] = 0xff }
  997. }
  998. i = i2
  999. tsec = int64(bigen.Uint64(btmp[:]))
  1000. }
  1001. if bd&(1<<6) != 0 {
  1002. var btmp [4]byte
  1003. n = (bd & 0x3) + 1
  1004. i2 = i + n
  1005. copy(btmp[4-n:], bs[i:i2])
  1006. i = i2
  1007. tnsec = bigen.Uint32(btmp[:])
  1008. }
  1009. if bd&(1<<5) == 0 {
  1010. tt = time.Unix(tsec, int64(tnsec)).UTC()
  1011. return
  1012. }
  1013. // In stdlib time.Parse, when a date is parsed without a zone name, it uses "" as zone name.
  1014. // However, we need name here, so it can be shown when time is printed.
  1015. // Zone name is in form: UTC-08:00.
  1016. // Note that Go Libs do not give access to dst flag, so we ignore dst bits
  1017. i2 = i + 2
  1018. tz = bigen.Uint16(bs[i:i2])
  1019. // i = i2
  1020. // sign extend sign bit into top 2 MSB (which were dst bits):
  1021. if tz&(1<<13) == 0 { // positive
  1022. tz = tz & 0x3fff //clear 2 MSBs: dst bits
  1023. } else { // negative
  1024. tz = tz | 0xc000 //set 2 MSBs: dst bits
  1025. }
  1026. tzint := int16(tz)
  1027. if tzint == 0 {
  1028. tt = time.Unix(tsec, int64(tnsec)).UTC()
  1029. } else {
  1030. // For Go Time, do not use a descriptive timezone.
  1031. // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
  1032. // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
  1033. // var zoneName = timeLocUTCName(tzint)
  1034. tt = time.Unix(tsec, int64(tnsec)).In(time.FixedZone("", int(tzint)*60))
  1035. }
  1036. return
  1037. }
  1038. var _ decDriver = (*bincDecDriver)(nil)
  1039. var _ encDriver = (*bincEncDriver)(nil)