binc.go 28 KB

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