binc.go 30 KB

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