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. }
  103. func (e *bincEncDriver) EncodeNil() {
  104. e.w.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.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
  112. e.w.writeb(bs)
  113. }
  114. }
  115. func (e *bincEncDriver) EncodeBool(b bool) {
  116. if b {
  117. e.w.writen1(bincVdSpecial<<4 | bincSpTrue)
  118. } else {
  119. e.w.writen1(bincVdSpecial<<4 | bincSpFalse)
  120. }
  121. }
  122. func (e *bincEncDriver) EncodeFloat32(f float32) {
  123. if f == 0 {
  124. e.w.writen1(bincVdSpecial<<4 | bincSpZeroFloat)
  125. return
  126. }
  127. e.w.writen1(bincVdFloat<<4 | bincFlBin32)
  128. bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
  129. }
  130. func (e *bincEncDriver) EncodeFloat64(f float64) {
  131. if f == 0 {
  132. e.w.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.w.writen1(bincVdFloat<<4 | 0x8 | bincFlBin64)
  143. e.w.writen1(byte(i))
  144. e.w.writeb(e.b[:i])
  145. return
  146. }
  147. }
  148. e.w.writen1(bincVdFloat<<4 | bincFlBin64)
  149. e.w.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.w.writen1(bd | lim - 1 - byte(i))
  160. e.w.writeb(e.b[i:lim])
  161. } else {
  162. e.w.writen1(bd | lim - 1)
  163. e.w.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.w.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.w.writen1(bincVdSpecial<<4 | bincSpZero)
  182. } else if pos && v >= 1 && v <= 16 {
  183. e.w.writen1(bincVdSmallInt<<4 | byte(v-1))
  184. } else if v <= math.MaxUint8 {
  185. e.w.writen2(bd|0x0, byte(v))
  186. } else if v <= math.MaxUint16 {
  187. e.w.writen1(bd | 0x01)
  188. bigenHelper{e.b[:2], 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(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
  196. bs := ext.WriteExt(rv)
  197. if bs == nil {
  198. e.EncodeNil()
  199. return
  200. }
  201. e.encodeExtPreamble(uint8(xtag), len(bs))
  202. e.w.writeb(bs)
  203. }
  204. func (e *bincEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  205. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  206. e.w.writeb(re.Data)
  207. }
  208. func (e *bincEncDriver) encodeExtPreamble(xtag byte, length int) {
  209. e.encLen(bincVdCustomExt<<4, uint64(length))
  210. e.w.writen1(xtag)
  211. }
  212. func (e *bincEncDriver) WriteArrayStart(length int) {
  213. e.encLen(bincVdArray<<4, uint64(length))
  214. e.c = containerArrayStart
  215. }
  216. func (e *bincEncDriver) WriteMapStart(length int) {
  217. e.encLen(bincVdMap<<4, uint64(length))
  218. e.c = containerMapStart
  219. }
  220. func (e *bincEncDriver) EncodeSymbol(v string) {
  221. // if WriteSymbolsNoRefs {
  222. // e.encodeString(cUTF8, v)
  223. // return
  224. // }
  225. //symbols only offer benefit when string length > 1.
  226. //This is because strings with length 1 take only 2 bytes to store
  227. //(bd with embedded length, and single byte for string val).
  228. l := len(v)
  229. if l == 0 {
  230. e.encBytesLen(cUTF8, 0)
  231. return
  232. } else if l == 1 {
  233. e.encBytesLen(cUTF8, 1)
  234. e.w.writen1(v[0])
  235. return
  236. }
  237. if e.m == nil {
  238. e.m = make(map[string]uint16, 16)
  239. }
  240. ui, ok := e.m[v]
  241. if ok {
  242. if ui <= math.MaxUint8 {
  243. e.w.writen2(bincVdSymbol<<4, byte(ui))
  244. } else {
  245. e.w.writen1(bincVdSymbol<<4 | 0x8)
  246. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  247. }
  248. } else {
  249. e.s++
  250. ui = e.s
  251. //ui = uint16(atomic.AddUint32(&e.s, 1))
  252. e.m[v] = ui
  253. var lenprec uint8
  254. if l <= math.MaxUint8 {
  255. // lenprec = 0
  256. } else if l <= math.MaxUint16 {
  257. lenprec = 1
  258. } else if int64(l) <= math.MaxUint32 {
  259. lenprec = 2
  260. } else {
  261. lenprec = 3
  262. }
  263. if ui <= math.MaxUint8 {
  264. e.w.writen2(bincVdSymbol<<4|0x0|0x4|lenprec, byte(ui))
  265. } else {
  266. e.w.writen1(bincVdSymbol<<4 | 0x8 | 0x4 | lenprec)
  267. bigenHelper{e.b[:2], e.w}.writeUint16(ui)
  268. }
  269. if lenprec == 0 {
  270. e.w.writen1(byte(l))
  271. } else if lenprec == 1 {
  272. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(l))
  273. } else if lenprec == 2 {
  274. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(l))
  275. } else {
  276. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(l))
  277. }
  278. e.w.writestr(v)
  279. }
  280. }
  281. func (e *bincEncDriver) EncodeString(c charEncoding, v string) {
  282. if e.c == containerMapKey && c == cUTF8 && (e.h.AsSymbols == 0 || e.h.AsSymbols == 1) {
  283. e.EncodeSymbol(v)
  284. return
  285. }
  286. l := uint64(len(v))
  287. e.encBytesLen(c, l)
  288. if l > 0 {
  289. e.w.writestr(v)
  290. }
  291. }
  292. func (e *bincEncDriver) EncodeStringEnc(c charEncoding, v string) {
  293. if e.c == containerMapKey && c == cUTF8 && (e.h.AsSymbols == 0 || e.h.AsSymbols == 1) {
  294. e.EncodeSymbol(v)
  295. return
  296. }
  297. l := uint64(len(v))
  298. e.encLen(bincVdString<<4, l) // e.encBytesLen(c, l)
  299. if l > 0 {
  300. e.w.writestr(v)
  301. }
  302. }
  303. func (e *bincEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
  304. if v == nil {
  305. e.EncodeNil()
  306. return
  307. }
  308. l := uint64(len(v))
  309. e.encBytesLen(c, l)
  310. if l > 0 {
  311. e.w.writeb(v)
  312. }
  313. }
  314. func (e *bincEncDriver) EncodeStringBytesRaw(v []byte) {
  315. if v == nil {
  316. e.EncodeNil()
  317. return
  318. }
  319. l := uint64(len(v))
  320. e.encLen(bincVdByteArray<<4, l) // e.encBytesLen(c, l)
  321. if l > 0 {
  322. e.w.writeb(v)
  323. }
  324. }
  325. func (e *bincEncDriver) encBytesLen(c charEncoding, length uint64) {
  326. //TODO: support bincUnicodeOther (for now, just use string or bytearray)
  327. if c == cRAW {
  328. e.encLen(bincVdByteArray<<4, length)
  329. } else {
  330. e.encLen(bincVdString<<4, length)
  331. }
  332. }
  333. func (e *bincEncDriver) encLen(bd byte, l uint64) {
  334. if l < 12 {
  335. e.w.writen1(bd | uint8(l+4))
  336. } else {
  337. e.encLenNumber(bd, l)
  338. }
  339. }
  340. func (e *bincEncDriver) encLenNumber(bd byte, v uint64) {
  341. if v <= math.MaxUint8 {
  342. e.w.writen2(bd, byte(v))
  343. } else if v <= math.MaxUint16 {
  344. e.w.writen1(bd | 0x01)
  345. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  346. } else if v <= math.MaxUint32 {
  347. e.w.writen1(bd | 0x02)
  348. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
  349. } else {
  350. e.w.writen1(bd | 0x03)
  351. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(v))
  352. }
  353. }
  354. //------------------------------------
  355. type bincDecSymbol struct {
  356. s string
  357. b []byte
  358. i uint16
  359. }
  360. type bincDecDriver struct {
  361. decDriverNoopContainerReader
  362. noBuiltInTypes
  363. d *Decoder
  364. h *BincHandle
  365. r *decReaderSwitch
  366. br bool // bytes reader
  367. bdRead bool
  368. bd byte
  369. vd byte
  370. vs byte
  371. _ [3]byte // padding
  372. // linear searching on this slice is ok,
  373. // because we typically expect < 32 symbols in each stream.
  374. s []bincDecSymbol
  375. // noStreamingCodec
  376. // decNoSeparator
  377. b [(8 + 1) * 8]byte // scratch
  378. }
  379. func (d *bincDecDriver) readNextBd() {
  380. d.bd = d.r.readn1()
  381. d.vd = d.bd >> 4
  382. d.vs = d.bd & 0x0f
  383. d.bdRead = true
  384. }
  385. func (d *bincDecDriver) uncacheRead() {
  386. if d.bdRead {
  387. d.r.unreadn1()
  388. d.bdRead = false
  389. }
  390. }
  391. func (d *bincDecDriver) ContainerType() (vt valueType) {
  392. if !d.bdRead {
  393. d.readNextBd()
  394. }
  395. if d.vd == bincVdSpecial && d.vs == bincSpNil {
  396. return valueTypeNil
  397. } else if d.vd == bincVdByteArray {
  398. return valueTypeBytes
  399. } else if d.vd == bincVdString {
  400. return valueTypeString
  401. } else if d.vd == bincVdArray {
  402. return valueTypeArray
  403. } else if d.vd == bincVdMap {
  404. return valueTypeMap
  405. }
  406. // else {
  407. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  408. // }
  409. return valueTypeUnset
  410. }
  411. func (d *bincDecDriver) TryDecodeAsNil() bool {
  412. if !d.bdRead {
  413. d.readNextBd()
  414. }
  415. if d.bd == bincVdSpecial<<4|bincSpNil {
  416. d.bdRead = false
  417. return true
  418. }
  419. return false
  420. }
  421. func (d *bincDecDriver) DecodeTime() (t time.Time) {
  422. if !d.bdRead {
  423. d.readNextBd()
  424. }
  425. if d.bd == bincVdSpecial<<4|bincSpNil {
  426. d.bdRead = false
  427. return
  428. }
  429. if d.vd != bincVdTimestamp {
  430. d.d.errorf("cannot decode time - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  431. return
  432. }
  433. t, err := bincDecodeTime(d.r.readx(int(d.vs)))
  434. if err != nil {
  435. panic(err)
  436. }
  437. d.bdRead = false
  438. return
  439. }
  440. func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
  441. if vs&0x8 == 0 {
  442. d.r.readb(d.b[0:defaultLen])
  443. } else {
  444. l := d.r.readn1()
  445. if l > 8 {
  446. d.d.errorf("cannot read float - at most 8 bytes used to represent float - received %v bytes", l)
  447. return
  448. }
  449. for i := l; i < 8; i++ {
  450. d.b[i] = 0
  451. }
  452. d.r.readb(d.b[0:l])
  453. }
  454. }
  455. func (d *bincDecDriver) decFloat() (f float64) {
  456. //if true { f = math.Float64frombits(bigen.Uint64(d.r.readx(8))); break; }
  457. if x := d.vs & 0x7; x == bincFlBin32 {
  458. d.decFloatPre(d.vs, 4)
  459. f = float64(math.Float32frombits(bigen.Uint32(d.b[0:4])))
  460. } else if x == bincFlBin64 {
  461. d.decFloatPre(d.vs, 8)
  462. f = math.Float64frombits(bigen.Uint64(d.b[0:8]))
  463. } else {
  464. d.d.errorf("read float - only float32 and float64 are supported - %s %x-%x/%s",
  465. msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  466. return
  467. }
  468. return
  469. }
  470. func (d *bincDecDriver) decUint() (v uint64) {
  471. // need to inline the code (interface conversion and type assertion expensive)
  472. switch d.vs {
  473. case 0:
  474. v = uint64(d.r.readn1())
  475. case 1:
  476. d.r.readb(d.b[6:8])
  477. v = uint64(bigen.Uint16(d.b[6:8]))
  478. case 2:
  479. d.b[4] = 0
  480. d.r.readb(d.b[5:8])
  481. v = uint64(bigen.Uint32(d.b[4:8]))
  482. case 3:
  483. d.r.readb(d.b[4:8])
  484. v = uint64(bigen.Uint32(d.b[4:8]))
  485. case 4, 5, 6:
  486. lim := int(7 - d.vs)
  487. d.r.readb(d.b[lim:8])
  488. for i := 0; i < lim; i++ {
  489. d.b[i] = 0
  490. }
  491. v = uint64(bigen.Uint64(d.b[:8]))
  492. case 7:
  493. d.r.readb(d.b[:8])
  494. v = uint64(bigen.Uint64(d.b[:8]))
  495. default:
  496. d.d.errorf("unsigned integers with greater than 64 bits of precision not supported")
  497. return
  498. }
  499. return
  500. }
  501. func (d *bincDecDriver) decCheckInteger() (ui uint64, neg bool) {
  502. if !d.bdRead {
  503. d.readNextBd()
  504. }
  505. vd, vs := d.vd, d.vs
  506. if vd == bincVdPosInt {
  507. ui = d.decUint()
  508. } else if vd == bincVdNegInt {
  509. ui = d.decUint()
  510. neg = true
  511. } else if vd == bincVdSmallInt {
  512. ui = uint64(d.vs) + 1
  513. } else if vd == bincVdSpecial {
  514. if vs == bincSpZero {
  515. //i = 0
  516. } else if vs == bincSpNegOne {
  517. neg = true
  518. ui = 1
  519. } else {
  520. d.d.errorf("integer decode fails - invalid special value from descriptor %x-%x/%s",
  521. d.vd, d.vs, bincdesc(d.vd, d.vs))
  522. return
  523. }
  524. } else {
  525. d.d.errorf("integer can only be decoded from int/uint. d.bd: 0x%x, d.vd: 0x%x", d.bd, d.vd)
  526. return
  527. }
  528. return
  529. }
  530. func (d *bincDecDriver) DecodeInt64() (i int64) {
  531. ui, neg := d.decCheckInteger()
  532. i = chkOvf.SignedIntV(ui)
  533. if neg {
  534. i = -i
  535. }
  536. d.bdRead = false
  537. return
  538. }
  539. func (d *bincDecDriver) DecodeUint64() (ui uint64) {
  540. ui, neg := d.decCheckInteger()
  541. if neg {
  542. d.d.errorf("assigning negative signed value to unsigned integer type")
  543. return
  544. }
  545. d.bdRead = false
  546. return
  547. }
  548. func (d *bincDecDriver) DecodeFloat64() (f float64) {
  549. if !d.bdRead {
  550. d.readNextBd()
  551. }
  552. vd, vs := d.vd, d.vs
  553. if vd == bincVdSpecial {
  554. d.bdRead = false
  555. if vs == bincSpNan {
  556. return math.NaN()
  557. } else if vs == bincSpPosInf {
  558. return math.Inf(1)
  559. } else if vs == bincSpZeroFloat || vs == bincSpZero {
  560. return
  561. } else if vs == bincSpNegInf {
  562. return math.Inf(-1)
  563. } else {
  564. d.d.errorf("float - invalid special value from descriptor %x-%x/%s",
  565. d.vd, d.vs, bincdesc(d.vd, d.vs))
  566. return
  567. }
  568. } else if vd == bincVdFloat {
  569. f = d.decFloat()
  570. } else {
  571. f = float64(d.DecodeInt64())
  572. }
  573. d.bdRead = false
  574. return
  575. }
  576. // bool can be decoded from bool only (single byte).
  577. func (d *bincDecDriver) DecodeBool() (b bool) {
  578. if !d.bdRead {
  579. d.readNextBd()
  580. }
  581. if bd := d.bd; bd == (bincVdSpecial | bincSpFalse) {
  582. // b = false
  583. } else if bd == (bincVdSpecial | bincSpTrue) {
  584. b = true
  585. } else {
  586. d.d.errorf("bool - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  587. return
  588. }
  589. d.bdRead = false
  590. return
  591. }
  592. func (d *bincDecDriver) ReadMapStart() (length int) {
  593. if !d.bdRead {
  594. d.readNextBd()
  595. }
  596. if d.vd != bincVdMap {
  597. d.d.errorf("map - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  598. return
  599. }
  600. length = d.decLen()
  601. d.bdRead = false
  602. return
  603. }
  604. func (d *bincDecDriver) ReadArrayStart() (length int) {
  605. if !d.bdRead {
  606. d.readNextBd()
  607. }
  608. if d.vd != bincVdArray {
  609. d.d.errorf("array - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  610. return
  611. }
  612. length = d.decLen()
  613. d.bdRead = false
  614. return
  615. }
  616. func (d *bincDecDriver) decLen() int {
  617. if d.vs > 3 {
  618. return int(d.vs - 4)
  619. }
  620. return int(d.decLenNumber())
  621. }
  622. func (d *bincDecDriver) decLenNumber() (v uint64) {
  623. if x := d.vs; x == 0 {
  624. v = uint64(d.r.readn1())
  625. } else if x == 1 {
  626. d.r.readb(d.b[6:8])
  627. v = uint64(bigen.Uint16(d.b[6:8]))
  628. } else if x == 2 {
  629. d.r.readb(d.b[4:8])
  630. v = uint64(bigen.Uint32(d.b[4:8]))
  631. } else {
  632. d.r.readb(d.b[:8])
  633. v = bigen.Uint64(d.b[:8])
  634. }
  635. return
  636. }
  637. func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (
  638. bs2 []byte, s string) {
  639. if !d.bdRead {
  640. d.readNextBd()
  641. }
  642. if d.bd == bincVdSpecial<<4|bincSpNil {
  643. d.bdRead = false
  644. return
  645. }
  646. var slen = -1
  647. // var ok bool
  648. switch d.vd {
  649. case bincVdString, bincVdByteArray:
  650. slen = d.decLen()
  651. if zerocopy {
  652. if d.br {
  653. bs2 = d.r.readx(slen)
  654. } else if len(bs) == 0 {
  655. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, d.b[:])
  656. } else {
  657. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs)
  658. }
  659. } else {
  660. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs)
  661. }
  662. if withString {
  663. s = string(bs2)
  664. }
  665. case bincVdSymbol:
  666. // zerocopy doesn't apply for symbols,
  667. // as the values must be stored in a table for later use.
  668. //
  669. //from vs: extract numSymbolBytes, containsStringVal, strLenPrecision,
  670. //extract symbol
  671. //if containsStringVal, read it and put in map
  672. //else look in map for string value
  673. var symbol uint16
  674. vs := d.vs
  675. if vs&0x8 == 0 {
  676. symbol = uint16(d.r.readn1())
  677. } else {
  678. symbol = uint16(bigen.Uint16(d.r.readx(2)))
  679. }
  680. if d.s == nil {
  681. d.s = make([]bincDecSymbol, 0, 16)
  682. }
  683. if vs&0x4 == 0 {
  684. for i := range d.s {
  685. j := &d.s[i]
  686. if j.i == symbol {
  687. bs2 = j.b
  688. if withString {
  689. if j.s == "" && bs2 != nil {
  690. j.s = string(bs2)
  691. }
  692. s = j.s
  693. }
  694. break
  695. }
  696. }
  697. } else {
  698. switch vs & 0x3 {
  699. case 0:
  700. slen = int(d.r.readn1())
  701. case 1:
  702. slen = int(bigen.Uint16(d.r.readx(2)))
  703. case 2:
  704. slen = int(bigen.Uint32(d.r.readx(4)))
  705. case 3:
  706. slen = int(bigen.Uint64(d.r.readx(8)))
  707. }
  708. // since using symbols, do not store any part of
  709. // the parameter bs in the map, as it might be a shared buffer.
  710. // bs2 = decByteSlice(d.r, slen, bs)
  711. bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, nil)
  712. if withString {
  713. s = string(bs2)
  714. }
  715. d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2})
  716. }
  717. default:
  718. d.d.errorf("string/bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  719. return
  720. }
  721. d.bdRead = false
  722. return
  723. }
  724. func (d *bincDecDriver) DecodeString() (s string) {
  725. // DecodeBytes does not accommodate symbols, whose impl stores string version in map.
  726. // Use decStringAndBytes directly.
  727. // return string(d.DecodeBytes(d.b[:], true, true))
  728. _, s = d.decStringAndBytes(d.b[:], true, true)
  729. return
  730. }
  731. func (d *bincDecDriver) DecodeStringAsBytes() (s []byte) {
  732. s, _ = d.decStringAndBytes(d.b[:], false, true)
  733. return
  734. }
  735. func (d *bincDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  736. if !d.bdRead {
  737. d.readNextBd()
  738. }
  739. if d.bd == bincVdSpecial<<4|bincSpNil {
  740. d.bdRead = false
  741. return nil
  742. }
  743. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  744. if d.vd == bincVdArray {
  745. bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  746. return
  747. }
  748. var clen int
  749. if d.vd == bincVdString || d.vd == bincVdByteArray {
  750. clen = d.decLen()
  751. } else {
  752. d.d.errorf("bytes - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  753. return
  754. }
  755. d.bdRead = false
  756. if zerocopy {
  757. if d.br {
  758. return d.r.readx(clen)
  759. } else if len(bs) == 0 {
  760. bs = d.b[:]
  761. }
  762. }
  763. return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs)
  764. }
  765. func (d *bincDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  766. if xtag > 0xff {
  767. d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
  768. return
  769. }
  770. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  771. realxtag = uint64(realxtag1)
  772. if ext == nil {
  773. re := rv.(*RawExt)
  774. re.Tag = realxtag
  775. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  776. } else {
  777. ext.ReadExt(rv, xbs)
  778. }
  779. return
  780. }
  781. func (d *bincDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  782. if !d.bdRead {
  783. d.readNextBd()
  784. }
  785. if d.vd == bincVdCustomExt {
  786. l := d.decLen()
  787. xtag = d.r.readn1()
  788. if verifyTag && xtag != tag {
  789. d.d.errorf("wrong extension tag - got %b, expecting: %v", xtag, tag)
  790. return
  791. }
  792. if d.br {
  793. xbs = d.r.readx(l)
  794. } else {
  795. xbs = decByteSlice(d.r, l, d.d.h.MaxInitLen, d.d.b[:])
  796. }
  797. } else if d.vd == bincVdByteArray {
  798. xbs = d.DecodeBytes(nil, true)
  799. } else {
  800. d.d.errorf("ext - expecting extensions or byte array - %s %x-%x/%s",
  801. msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  802. return
  803. }
  804. d.bdRead = false
  805. return
  806. }
  807. func (d *bincDecDriver) DecodeNaked() {
  808. if !d.bdRead {
  809. d.readNextBd()
  810. }
  811. n := d.d.n
  812. var decodeFurther bool
  813. switch d.vd {
  814. case bincVdSpecial:
  815. switch d.vs {
  816. case bincSpNil:
  817. n.v = valueTypeNil
  818. case bincSpFalse:
  819. n.v = valueTypeBool
  820. n.b = false
  821. case bincSpTrue:
  822. n.v = valueTypeBool
  823. n.b = true
  824. case bincSpNan:
  825. n.v = valueTypeFloat
  826. n.f = math.NaN()
  827. case bincSpPosInf:
  828. n.v = valueTypeFloat
  829. n.f = math.Inf(1)
  830. case bincSpNegInf:
  831. n.v = valueTypeFloat
  832. n.f = math.Inf(-1)
  833. case bincSpZeroFloat:
  834. n.v = valueTypeFloat
  835. n.f = float64(0)
  836. case bincSpZero:
  837. n.v = valueTypeUint
  838. n.u = uint64(0) // int8(0)
  839. case bincSpNegOne:
  840. n.v = valueTypeInt
  841. n.i = int64(-1) // int8(-1)
  842. default:
  843. d.d.errorf("cannot infer value - unrecognized special value from descriptor %x-%x/%s",
  844. d.vd, d.vs, bincdesc(d.vd, d.vs))
  845. }
  846. case bincVdSmallInt:
  847. n.v = valueTypeUint
  848. n.u = uint64(int8(d.vs)) + 1 // int8(d.vs) + 1
  849. case bincVdPosInt:
  850. n.v = valueTypeUint
  851. n.u = d.decUint()
  852. case bincVdNegInt:
  853. n.v = valueTypeInt
  854. n.i = -(int64(d.decUint()))
  855. case bincVdFloat:
  856. n.v = valueTypeFloat
  857. n.f = d.decFloat()
  858. case bincVdSymbol:
  859. n.v = valueTypeSymbol
  860. n.s = d.DecodeString()
  861. case bincVdString:
  862. n.v = valueTypeString
  863. n.s = d.DecodeString()
  864. case bincVdByteArray:
  865. n.v = valueTypeBytes
  866. n.l = d.DecodeBytes(nil, false)
  867. case bincVdTimestamp:
  868. n.v = valueTypeTime
  869. tt, err := bincDecodeTime(d.r.readx(int(d.vs)))
  870. if err != nil {
  871. panic(err)
  872. }
  873. n.t = tt
  874. case bincVdCustomExt:
  875. n.v = valueTypeExt
  876. l := d.decLen()
  877. n.u = uint64(d.r.readn1())
  878. if d.br {
  879. n.l = d.r.readx(l)
  880. } else {
  881. n.l = decByteSlice(d.r, l, d.d.h.MaxInitLen, d.d.b[:])
  882. }
  883. case bincVdArray:
  884. n.v = valueTypeArray
  885. decodeFurther = true
  886. case bincVdMap:
  887. n.v = valueTypeMap
  888. decodeFurther = true
  889. default:
  890. d.d.errorf("cannot infer value - %s %x-%x/%s", msgBadDesc, d.vd, d.vs, bincdesc(d.vd, d.vs))
  891. }
  892. if !decodeFurther {
  893. d.bdRead = false
  894. }
  895. if n.v == valueTypeUint && d.h.SignedInteger {
  896. n.v = valueTypeInt
  897. n.i = int64(n.u)
  898. }
  899. return
  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)