http urls monitor.

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