binc.go 29 KB

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