binc.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
  867. case bincVdTimestamp:
  868. n.v = valueTypeTime
  869. tt, err := bincDecodeTime(d.r.readx(uint(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(uint(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. }
  900. //------------------------------------
  901. //BincHandle is a Handle for the Binc Schema-Free Encoding Format
  902. //defined at https://github.com/ugorji/binc .
  903. //
  904. //BincHandle currently supports all Binc features with the following EXCEPTIONS:
  905. // - only integers up to 64 bits of precision are supported.
  906. // big integers are unsupported.
  907. // - Only IEEE 754 binary32 and binary64 floats are supported (ie Go float32 and float64 types).
  908. // extended precision and decimal IEEE 754 floats are unsupported.
  909. // - Only UTF-8 strings supported.
  910. // Unicode_Other Binc types (UTF16, UTF32) are currently unsupported.
  911. //
  912. //Note that these EXCEPTIONS are temporary and full support is possible and may happen soon.
  913. type BincHandle struct {
  914. BasicHandle
  915. binaryEncodingType
  916. noElemSeparators
  917. // AsSymbols defines what should be encoded as symbols.
  918. //
  919. // Encoding as symbols can reduce the encoded size significantly.
  920. //
  921. // However, during decoding, each string to be encoded as a symbol must
  922. // be checked to see if it has been seen before. Consequently, encoding time
  923. // will increase if using symbols, because string comparisons has a clear cost.
  924. //
  925. // Values:
  926. // - 0: default: library uses best judgement
  927. // - 1: use symbols
  928. // - 2: do not use symbols
  929. AsSymbols uint8
  930. // AsSymbols: may later on introduce more options ...
  931. // - m: map keys
  932. // - s: struct fields
  933. // - n: none
  934. // - a: all: same as m, s, ...
  935. // _ [1]uint64 // padding
  936. }
  937. // Name returns the name of the handle: binc
  938. func (h *BincHandle) Name() string { return "binc" }
  939. // SetBytesExt sets an extension
  940. func (h *BincHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  941. return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
  942. }
  943. func (h *BincHandle) newEncDriver(e *Encoder) encDriver {
  944. return &bincEncDriver{e: e, h: h, w: e.w}
  945. }
  946. func (h *BincHandle) newDecDriver(d *Decoder) decDriver {
  947. return &bincDecDriver{d: d, h: h, r: d.r, br: d.bytes}
  948. }
  949. func (e *bincEncDriver) reset() {
  950. e.w = e.e.w
  951. e.s = 0
  952. e.c = 0
  953. e.m = nil
  954. }
  955. func (d *bincDecDriver) reset() {
  956. d.r, d.br = d.d.r, d.d.bytes
  957. d.s = nil
  958. d.bd, d.bdRead, d.vd, d.vs = 0, false, 0, 0
  959. }
  960. // var timeDigits = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
  961. // EncodeTime encodes a time.Time as a []byte, including
  962. // information on the instant in time and UTC offset.
  963. //
  964. // Format Description
  965. //
  966. // A timestamp is composed of 3 components:
  967. //
  968. // - secs: signed integer representing seconds since unix epoch
  969. // - nsces: unsigned integer representing fractional seconds as a
  970. // nanosecond offset within secs, in the range 0 <= nsecs < 1e9
  971. // - tz: signed integer representing timezone offset in minutes east of UTC,
  972. // and a dst (daylight savings time) flag
  973. //
  974. // When encoding a timestamp, the first byte is the descriptor, which
  975. // defines which components are encoded and how many bytes are used to
  976. // encode secs and nsecs components. *If secs/nsecs is 0 or tz is UTC, it
  977. // is not encoded in the byte array explicitly*.
  978. //
  979. // Descriptor 8 bits are of the form `A B C DDD EE`:
  980. // A: Is secs component encoded? 1 = true
  981. // B: Is nsecs component encoded? 1 = true
  982. // C: Is tz component encoded? 1 = true
  983. // DDD: Number of extra bytes for secs (range 0-7).
  984. // If A = 1, secs encoded in DDD+1 bytes.
  985. // If A = 0, secs is not encoded, and is assumed to be 0.
  986. // If A = 1, then we need at least 1 byte to encode secs.
  987. // DDD says the number of extra bytes beyond that 1.
  988. // E.g. if DDD=0, then secs is represented in 1 byte.
  989. // if DDD=2, then secs is represented in 3 bytes.
  990. // EE: Number of extra bytes for nsecs (range 0-3).
  991. // If B = 1, nsecs encoded in EE+1 bytes (similar to secs/DDD above)
  992. //
  993. // Following the descriptor bytes, subsequent bytes are:
  994. //
  995. // secs component encoded in `DDD + 1` bytes (if A == 1)
  996. // nsecs component encoded in `EE + 1` bytes (if B == 1)
  997. // tz component encoded in 2 bytes (if C == 1)
  998. //
  999. // secs and nsecs components are integers encoded in a BigEndian
  1000. // 2-complement encoding format.
  1001. //
  1002. // tz component is encoded as 2 bytes (16 bits). Most significant bit 15 to
  1003. // Least significant bit 0 are described below:
  1004. //
  1005. // Timezone offset has a range of -12:00 to +14:00 (ie -720 to +840 minutes).
  1006. // Bit 15 = have\_dst: set to 1 if we set the dst flag.
  1007. // Bit 14 = dst\_on: set to 1 if dst is in effect at the time, or 0 if not.
  1008. // Bits 13..0 = timezone offset in minutes. It is a signed integer in Big Endian format.
  1009. //
  1010. func bincEncodeTime(t time.Time) []byte {
  1011. //t := rv.Interface().(time.Time)
  1012. tsecs, tnsecs := t.Unix(), t.Nanosecond()
  1013. var (
  1014. bd byte
  1015. btmp [8]byte
  1016. bs [16]byte
  1017. i int = 1
  1018. )
  1019. l := t.Location()
  1020. if l == time.UTC {
  1021. l = nil
  1022. }
  1023. if tsecs != 0 {
  1024. bd = bd | 0x80
  1025. bigen.PutUint64(btmp[:], uint64(tsecs))
  1026. f := pruneSignExt(btmp[:], tsecs >= 0)
  1027. bd = bd | (byte(7-f) << 2)
  1028. copy(bs[i:], btmp[f:])
  1029. i = i + (8 - f)
  1030. }
  1031. if tnsecs != 0 {
  1032. bd = bd | 0x40
  1033. bigen.PutUint32(btmp[:4], uint32(tnsecs))
  1034. f := pruneSignExt(btmp[:4], true)
  1035. bd = bd | byte(3-f)
  1036. copy(bs[i:], btmp[f:4])
  1037. i = i + (4 - f)
  1038. }
  1039. if l != nil {
  1040. bd = bd | 0x20
  1041. // Note that Go Libs do not give access to dst flag.
  1042. _, zoneOffset := t.Zone()
  1043. //zoneName, zoneOffset := t.Zone()
  1044. zoneOffset /= 60
  1045. z := uint16(zoneOffset)
  1046. bigen.PutUint16(btmp[:2], z)
  1047. // clear dst flags
  1048. bs[i] = btmp[0] & 0x3f
  1049. bs[i+1] = btmp[1]
  1050. i = i + 2
  1051. }
  1052. bs[0] = bd
  1053. return bs[0:i]
  1054. }
  1055. // bincDecodeTime decodes a []byte into a time.Time.
  1056. func bincDecodeTime(bs []byte) (tt time.Time, err error) {
  1057. bd := bs[0]
  1058. var (
  1059. tsec int64
  1060. tnsec uint32
  1061. tz uint16
  1062. i byte = 1
  1063. i2 byte
  1064. n byte
  1065. )
  1066. if bd&(1<<7) != 0 {
  1067. var btmp [8]byte
  1068. n = ((bd >> 2) & 0x7) + 1
  1069. i2 = i + n
  1070. copy(btmp[8-n:], bs[i:i2])
  1071. //if first bit of bs[i] is set, then fill btmp[0..8-n] with 0xff (ie sign extend it)
  1072. if bs[i]&(1<<7) != 0 {
  1073. copy(btmp[0:8-n], bsAll0xff)
  1074. //for j,k := byte(0), 8-n; j < k; j++ { btmp[j] = 0xff }
  1075. }
  1076. i = i2
  1077. tsec = int64(bigen.Uint64(btmp[:]))
  1078. }
  1079. if bd&(1<<6) != 0 {
  1080. var btmp [4]byte
  1081. n = (bd & 0x3) + 1
  1082. i2 = i + n
  1083. copy(btmp[4-n:], bs[i:i2])
  1084. i = i2
  1085. tnsec = bigen.Uint32(btmp[:])
  1086. }
  1087. if bd&(1<<5) == 0 {
  1088. tt = time.Unix(tsec, int64(tnsec)).UTC()
  1089. return
  1090. }
  1091. // In stdlib time.Parse, when a date is parsed without a zone name, it uses "" as zone name.
  1092. // However, we need name here, so it can be shown when time is printf.d.
  1093. // Zone name is in form: UTC-08:00.
  1094. // Note that Go Libs do not give access to dst flag, so we ignore dst bits
  1095. i2 = i + 2
  1096. tz = bigen.Uint16(bs[i:i2])
  1097. // i = i2
  1098. // sign extend sign bit into top 2 MSB (which were dst bits):
  1099. if tz&(1<<13) == 0 { // positive
  1100. tz = tz & 0x3fff //clear 2 MSBs: dst bits
  1101. } else { // negative
  1102. tz = tz | 0xc000 //set 2 MSBs: dst bits
  1103. }
  1104. tzint := int16(tz)
  1105. if tzint == 0 {
  1106. tt = time.Unix(tsec, int64(tnsec)).UTC()
  1107. } else {
  1108. // For Go Time, do not use a descriptive timezone.
  1109. // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
  1110. // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
  1111. // var zoneName = timeLocUTCName(tzint)
  1112. tt = time.Unix(tsec, int64(tnsec)).In(time.FixedZone("", int(tzint)*60))
  1113. }
  1114. return
  1115. }
  1116. var _ decDriver = (*bincDecDriver)(nil)
  1117. var _ encDriver = (*bincEncDriver)(nil)