simple.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 (
  9. _ uint8 = iota
  10. simpleVdNil = 1
  11. simpleVdFalse = 2
  12. simpleVdTrue = 3
  13. simpleVdFloat32 = 4
  14. simpleVdFloat64 = 5
  15. // each lasts for 4 (ie n, n+1, n+2, n+3)
  16. simpleVdPosInt = 8
  17. simpleVdNegInt = 12
  18. simpleVdTime = 24
  19. // containers: each lasts for 4 (ie n, n+1, n+2, ... n+7)
  20. simpleVdString = 216
  21. simpleVdByteArray = 224
  22. simpleVdArray = 232
  23. simpleVdMap = 240
  24. simpleVdExt = 248
  25. )
  26. type simpleEncDriver struct {
  27. noBuiltInTypes
  28. encDriverNoopContainerWriter
  29. h *SimpleHandle
  30. b [8]byte
  31. _ [6]uint64 // padding (cache-aligned)
  32. e Encoder
  33. }
  34. func (e *simpleEncDriver) encoder() *Encoder {
  35. return &e.e
  36. }
  37. func (e *simpleEncDriver) EncodeNil() {
  38. e.e.encWr.writen1(simpleVdNil)
  39. }
  40. func (e *simpleEncDriver) EncodeBool(b bool) {
  41. if e.h.EncZeroValuesAsNil && e.e.c != containerMapKey && !b {
  42. e.EncodeNil()
  43. return
  44. }
  45. if b {
  46. e.e.encWr.writen1(simpleVdTrue)
  47. } else {
  48. e.e.encWr.writen1(simpleVdFalse)
  49. }
  50. }
  51. func (e *simpleEncDriver) EncodeFloat32(f float32) {
  52. if e.h.EncZeroValuesAsNil && e.e.c != containerMapKey && f == 0.0 {
  53. e.EncodeNil()
  54. return
  55. }
  56. e.e.encWr.writen1(simpleVdFloat32)
  57. bigenHelper{e.b[:4], e.e.w()}.writeUint32(math.Float32bits(f))
  58. }
  59. func (e *simpleEncDriver) EncodeFloat64(f float64) {
  60. if e.h.EncZeroValuesAsNil && e.e.c != containerMapKey && f == 0.0 {
  61. e.EncodeNil()
  62. return
  63. }
  64. e.e.encWr.writen1(simpleVdFloat64)
  65. bigenHelper{e.b[:8], e.e.w()}.writeUint64(math.Float64bits(f))
  66. }
  67. func (e *simpleEncDriver) EncodeInt(v int64) {
  68. if v < 0 {
  69. e.encUint(uint64(-v), simpleVdNegInt)
  70. } else {
  71. e.encUint(uint64(v), simpleVdPosInt)
  72. }
  73. }
  74. func (e *simpleEncDriver) EncodeUint(v uint64) {
  75. e.encUint(v, simpleVdPosInt)
  76. }
  77. func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
  78. if e.h.EncZeroValuesAsNil && e.e.c != containerMapKey && v == 0 {
  79. e.EncodeNil()
  80. return
  81. }
  82. if v <= math.MaxUint8 {
  83. e.e.encWr.writen2(bd, uint8(v))
  84. } else if v <= math.MaxUint16 {
  85. e.e.encWr.writen1(bd + 1)
  86. bigenHelper{e.b[:2], e.e.w()}.writeUint16(uint16(v))
  87. } else if v <= math.MaxUint32 {
  88. e.e.encWr.writen1(bd + 2)
  89. bigenHelper{e.b[:4], e.e.w()}.writeUint32(uint32(v))
  90. } else { // if v <= math.MaxUint64 {
  91. e.e.encWr.writen1(bd + 3)
  92. bigenHelper{e.b[:8], e.e.w()}.writeUint64(v)
  93. }
  94. }
  95. func (e *simpleEncDriver) encLen(bd byte, length int) {
  96. if length == 0 {
  97. e.e.encWr.writen1(bd)
  98. } else if length <= math.MaxUint8 {
  99. e.e.encWr.writen1(bd + 1)
  100. e.e.encWr.writen1(uint8(length))
  101. } else if length <= math.MaxUint16 {
  102. e.e.encWr.writen1(bd + 2)
  103. bigenHelper{e.b[:2], e.e.w()}.writeUint16(uint16(length))
  104. } else if int64(length) <= math.MaxUint32 {
  105. e.e.encWr.writen1(bd + 3)
  106. bigenHelper{e.b[:4], e.e.w()}.writeUint32(uint32(length))
  107. } else {
  108. e.e.encWr.writen1(bd + 4)
  109. bigenHelper{e.b[:8], e.e.w()}.writeUint64(uint64(length))
  110. }
  111. }
  112. func (e *simpleEncDriver) EncodeExt(v interface{}, xtag uint64, ext Ext) {
  113. var bs []byte
  114. if ext == SelfExt {
  115. bs = e.e.blist.get(1024)[:0]
  116. e.e.sideEncode(v, &bs)
  117. } else {
  118. bs = ext.WriteExt(v)
  119. }
  120. if bs == nil {
  121. e.EncodeNil()
  122. return
  123. }
  124. e.encodeExtPreamble(uint8(xtag), len(bs))
  125. e.e.encWr.writeb(bs)
  126. if ext == SelfExt {
  127. e.e.blist.put(bs)
  128. }
  129. }
  130. func (e *simpleEncDriver) EncodeRawExt(re *RawExt) {
  131. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  132. e.e.encWr.writeb(re.Data)
  133. }
  134. func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
  135. e.encLen(simpleVdExt, length)
  136. e.e.encWr.writen1(xtag)
  137. }
  138. func (e *simpleEncDriver) WriteArrayStart(length int) {
  139. e.encLen(simpleVdArray, length)
  140. }
  141. func (e *simpleEncDriver) WriteMapStart(length int) {
  142. e.encLen(simpleVdMap, length)
  143. }
  144. func (e *simpleEncDriver) EncodeString(v string) {
  145. if e.h.EncZeroValuesAsNil && e.e.c != containerMapKey && v == "" {
  146. e.EncodeNil()
  147. return
  148. }
  149. if e.h.StringToRaw {
  150. e.encLen(simpleVdByteArray, len(v))
  151. } else {
  152. e.encLen(simpleVdString, len(v))
  153. }
  154. e.e.encWr.writestr(v)
  155. }
  156. func (e *simpleEncDriver) EncodeStringBytesRaw(v []byte) {
  157. // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == nil {
  158. if v == nil {
  159. e.EncodeNil()
  160. return
  161. }
  162. e.encLen(simpleVdByteArray, len(v))
  163. e.e.encWr.writeb(v)
  164. }
  165. func (e *simpleEncDriver) EncodeTime(t time.Time) {
  166. // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && t.IsZero() {
  167. if t.IsZero() {
  168. e.EncodeNil()
  169. return
  170. }
  171. v, err := t.MarshalBinary()
  172. if err != nil {
  173. e.e.errorv(err)
  174. return
  175. }
  176. // time.Time marshalbinary takes about 14 bytes.
  177. e.e.encWr.writen2(simpleVdTime, uint8(len(v)))
  178. e.e.encWr.writeb(v)
  179. }
  180. //------------------------------------
  181. type simpleDecDriver struct {
  182. h *SimpleHandle
  183. bdRead bool
  184. bd byte
  185. fnil bool
  186. noBuiltInTypes
  187. decDriverNoopContainerReader
  188. _ [6]uint64 // padding
  189. d Decoder
  190. }
  191. func (d *simpleDecDriver) decoder() *Decoder {
  192. return &d.d
  193. }
  194. func (d *simpleDecDriver) readNextBd() {
  195. d.bd = d.d.decRd.readn1()
  196. d.bdRead = true
  197. }
  198. // func (d *simpleDecDriver) uncacheRead() {
  199. // if d.bdRead {
  200. // d.d.decRd.unreadn1()
  201. // d.bdRead = false
  202. // }
  203. // }
  204. func (d *simpleDecDriver) advanceNil() (null bool) {
  205. d.fnil = false
  206. if !d.bdRead {
  207. d.readNextBd()
  208. }
  209. if d.bd == simpleVdNil {
  210. d.bdRead = false
  211. d.fnil = true
  212. null = true
  213. }
  214. return
  215. }
  216. // func (d *simpleDecDriver) Nil() bool {
  217. // return d.fnil
  218. // }
  219. func (d *simpleDecDriver) ContainerType() (vt valueType) {
  220. if !d.bdRead {
  221. d.readNextBd()
  222. }
  223. d.fnil = false
  224. switch d.bd {
  225. case simpleVdNil:
  226. d.bdRead = false
  227. d.fnil = true
  228. return valueTypeNil
  229. case simpleVdByteArray, simpleVdByteArray + 1,
  230. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  231. return valueTypeBytes
  232. case simpleVdString, simpleVdString + 1,
  233. simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  234. return valueTypeString
  235. case simpleVdArray, simpleVdArray + 1,
  236. simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  237. return valueTypeArray
  238. case simpleVdMap, simpleVdMap + 1,
  239. simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  240. return valueTypeMap
  241. }
  242. return valueTypeUnset
  243. }
  244. func (d *simpleDecDriver) TryNil() bool {
  245. return d.advanceNil()
  246. }
  247. func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
  248. switch d.bd {
  249. case simpleVdPosInt:
  250. ui = uint64(d.d.decRd.readn1())
  251. case simpleVdPosInt + 1:
  252. ui = uint64(bigen.Uint16(d.d.decRd.readx(2)))
  253. case simpleVdPosInt + 2:
  254. ui = uint64(bigen.Uint32(d.d.decRd.readx(4)))
  255. case simpleVdPosInt + 3:
  256. ui = uint64(bigen.Uint64(d.d.decRd.readx(8)))
  257. case simpleVdNegInt:
  258. ui = uint64(d.d.decRd.readn1())
  259. neg = true
  260. case simpleVdNegInt + 1:
  261. ui = uint64(bigen.Uint16(d.d.decRd.readx(2)))
  262. neg = true
  263. case simpleVdNegInt + 2:
  264. ui = uint64(bigen.Uint32(d.d.decRd.readx(4)))
  265. neg = true
  266. case simpleVdNegInt + 3:
  267. ui = uint64(bigen.Uint64(d.d.decRd.readx(8)))
  268. neg = true
  269. default:
  270. d.d.errorf("integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
  271. return
  272. }
  273. // DO NOT do this check below, because callers may only want the unsigned value:
  274. //
  275. // if ui > math.MaxInt64 {
  276. // d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
  277. // return
  278. // }
  279. return
  280. }
  281. func (d *simpleDecDriver) DecodeInt64() (i int64) {
  282. if d.advanceNil() {
  283. return
  284. }
  285. ui, neg := d.decCheckInteger()
  286. i = chkOvf.SignedIntV(ui)
  287. if neg {
  288. i = -i
  289. }
  290. d.bdRead = false
  291. return
  292. }
  293. func (d *simpleDecDriver) DecodeUint64() (ui uint64) {
  294. if d.advanceNil() {
  295. return
  296. }
  297. ui, neg := d.decCheckInteger()
  298. if neg {
  299. d.d.errorf("assigning negative signed value to unsigned type")
  300. return
  301. }
  302. d.bdRead = false
  303. return
  304. }
  305. func (d *simpleDecDriver) DecodeFloat64() (f float64) {
  306. if d.advanceNil() {
  307. return
  308. }
  309. if d.bd == simpleVdFloat32 {
  310. f = float64(math.Float32frombits(bigen.Uint32(d.d.decRd.readx(4))))
  311. } else if d.bd == simpleVdFloat64 {
  312. f = math.Float64frombits(bigen.Uint64(d.d.decRd.readx(8)))
  313. } else {
  314. if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
  315. f = float64(d.DecodeInt64())
  316. } else {
  317. d.d.errorf("float only valid from float32/64: Invalid descriptor: %v", d.bd)
  318. return
  319. }
  320. }
  321. d.bdRead = false
  322. return
  323. }
  324. // bool can be decoded from bool only (single byte).
  325. func (d *simpleDecDriver) DecodeBool() (b bool) {
  326. if d.advanceNil() {
  327. return
  328. }
  329. if d.bd == simpleVdFalse {
  330. } else if d.bd == simpleVdTrue {
  331. b = true
  332. } else {
  333. d.d.errorf("cannot decode bool - %s: %x", msgBadDesc, d.bd)
  334. return
  335. }
  336. d.bdRead = false
  337. return
  338. }
  339. func (d *simpleDecDriver) ReadMapStart() (length int) {
  340. if d.advanceNil() {
  341. return decContainerLenNil
  342. }
  343. d.bdRead = false
  344. return d.decLen()
  345. }
  346. func (d *simpleDecDriver) ReadArrayStart() (length int) {
  347. if d.advanceNil() {
  348. return decContainerLenNil
  349. }
  350. d.bdRead = false
  351. return d.decLen()
  352. }
  353. func (d *simpleDecDriver) decLen() int {
  354. switch d.bd % 8 {
  355. case 0:
  356. return 0
  357. case 1:
  358. return int(d.d.decRd.readn1())
  359. case 2:
  360. return int(bigen.Uint16(d.d.decRd.readx(2)))
  361. case 3:
  362. ui := uint64(bigen.Uint32(d.d.decRd.readx(4)))
  363. if chkOvf.Uint(ui, intBitsize) {
  364. d.d.errorf("overflow integer: %v", ui)
  365. return 0
  366. }
  367. return int(ui)
  368. case 4:
  369. ui := bigen.Uint64(d.d.decRd.readx(8))
  370. if chkOvf.Uint(ui, intBitsize) {
  371. d.d.errorf("overflow integer: %v", ui)
  372. return 0
  373. }
  374. return int(ui)
  375. }
  376. d.d.errorf("cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8)
  377. return -1
  378. }
  379. func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) {
  380. return d.DecodeBytes(d.d.b[:], true)
  381. }
  382. func (d *simpleDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  383. if d.advanceNil() {
  384. return
  385. }
  386. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  387. if d.bd >= simpleVdArray && d.bd <= simpleVdMap+4 {
  388. if len(bs) == 0 && zerocopy {
  389. bs = d.d.b[:]
  390. }
  391. // bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  392. slen := d.ReadArrayStart()
  393. bs = usableByteSlice(bs, slen)
  394. for i := 0; i < len(bs); i++ {
  395. bs[i] = uint8(chkOvf.UintV(d.DecodeUint64(), 8))
  396. }
  397. return bs
  398. }
  399. clen := d.decLen()
  400. d.bdRead = false
  401. if d.d.bytes && (zerocopy || d.h.ZeroCopy) {
  402. return d.d.decRd.rb.readx(uint(clen))
  403. }
  404. if zerocopy && len(bs) == 0 {
  405. bs = d.d.b[:]
  406. }
  407. return decByteSlice(d.d.r(), clen, d.d.h.MaxInitLen, bs)
  408. }
  409. func (d *simpleDecDriver) DecodeTime() (t time.Time) {
  410. if d.advanceNil() {
  411. return
  412. }
  413. if d.bd != simpleVdTime {
  414. d.d.errorf("invalid descriptor for time.Time - expect 0x%x, received 0x%x", simpleVdTime, d.bd)
  415. return
  416. }
  417. d.bdRead = false
  418. clen := int(d.d.decRd.readn1())
  419. b := d.d.decRd.readx(uint(clen))
  420. if err := (&t).UnmarshalBinary(b); err != nil {
  421. d.d.errorv(err)
  422. }
  423. return
  424. }
  425. func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) {
  426. if xtag > 0xff {
  427. d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
  428. return
  429. }
  430. if d.advanceNil() {
  431. return
  432. }
  433. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  434. realxtag := uint64(realxtag1)
  435. if ext == nil {
  436. re := rv.(*RawExt)
  437. re.Tag = realxtag
  438. re.Data = detachZeroCopyBytes(d.d.bytes, re.Data, xbs)
  439. } else if ext == SelfExt {
  440. d.d.sideDecode(rv, xbs)
  441. } else {
  442. ext.ReadExt(rv, xbs)
  443. }
  444. }
  445. func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  446. switch d.bd {
  447. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  448. l := d.decLen()
  449. xtag = d.d.decRd.readn1()
  450. if verifyTag && xtag != tag {
  451. d.d.errorf("wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  452. return
  453. }
  454. if d.d.bytes {
  455. xbs = d.d.decRd.rb.readx(uint(l))
  456. } else {
  457. xbs = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:])
  458. }
  459. case simpleVdByteArray, simpleVdByteArray + 1,
  460. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  461. xbs = d.DecodeBytes(nil, true)
  462. default:
  463. d.d.errorf("ext - %s - expecting extensions/bytearray, got: 0x%x", msgBadDesc, d.bd)
  464. return
  465. }
  466. d.bdRead = false
  467. return
  468. }
  469. func (d *simpleDecDriver) DecodeNaked() {
  470. if !d.bdRead {
  471. d.readNextBd()
  472. }
  473. d.fnil = false
  474. n := d.d.naked()
  475. var decodeFurther bool
  476. switch d.bd {
  477. case simpleVdNil:
  478. n.v = valueTypeNil
  479. d.fnil = true
  480. case simpleVdFalse:
  481. n.v = valueTypeBool
  482. n.b = false
  483. case simpleVdTrue:
  484. n.v = valueTypeBool
  485. n.b = true
  486. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  487. if d.h.SignedInteger {
  488. n.v = valueTypeInt
  489. n.i = d.DecodeInt64()
  490. } else {
  491. n.v = valueTypeUint
  492. n.u = d.DecodeUint64()
  493. }
  494. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  495. n.v = valueTypeInt
  496. n.i = d.DecodeInt64()
  497. case simpleVdFloat32:
  498. n.v = valueTypeFloat
  499. n.f = d.DecodeFloat64()
  500. case simpleVdFloat64:
  501. n.v = valueTypeFloat
  502. n.f = d.DecodeFloat64()
  503. case simpleVdTime:
  504. n.v = valueTypeTime
  505. n.t = d.DecodeTime()
  506. case simpleVdString, simpleVdString + 1,
  507. simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  508. n.v = valueTypeString
  509. n.s = string(d.DecodeStringAsBytes())
  510. case simpleVdByteArray, simpleVdByteArray + 1,
  511. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  512. fauxUnionReadRawBytes(d, &d.d, n, d.h.RawToString)
  513. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  514. n.v = valueTypeExt
  515. l := d.decLen()
  516. n.u = uint64(d.d.decRd.readn1())
  517. if d.d.bytes {
  518. n.l = d.d.decRd.rb.readx(uint(l))
  519. } else {
  520. n.l = decByteSlice(d.d.r(), l, d.d.h.MaxInitLen, d.d.b[:])
  521. }
  522. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2,
  523. simpleVdArray + 3, simpleVdArray + 4:
  524. n.v = valueTypeArray
  525. decodeFurther = true
  526. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  527. n.v = valueTypeMap
  528. decodeFurther = true
  529. default:
  530. d.d.errorf("cannot infer value - %s 0x%x", msgBadDesc, d.bd)
  531. }
  532. if !decodeFurther {
  533. d.bdRead = false
  534. }
  535. }
  536. //------------------------------------
  537. // SimpleHandle is a Handle for a very simple encoding format.
  538. //
  539. // simple is a simplistic codec similar to binc, but not as compact.
  540. // - Encoding of a value is always preceded by the descriptor byte (bd)
  541. // - True, false, nil are encoded fully in 1 byte (the descriptor)
  542. // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
  543. // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
  544. // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
  545. // - Length of containers (strings, bytes, array, map, extensions)
  546. // are encoded in 0, 1, 2, 4 or 8 bytes.
  547. // Zero-length containers have no length encoded.
  548. // For others, the number of bytes is given by pow(2, bd%3)
  549. // - maps are encoded as [bd] [length] [[key][value]]...
  550. // - arrays are encoded as [bd] [length] [value]...
  551. // - extensions are encoded as [bd] [length] [tag] [byte]...
  552. // - strings/bytearrays are encoded as [bd] [length] [byte]...
  553. // - time.Time are encoded as [bd] [length] [byte]...
  554. //
  555. // The full spec will be published soon.
  556. type SimpleHandle struct {
  557. binaryEncodingType
  558. BasicHandle
  559. // EncZeroValuesAsNil says to encode zero values for numbers, bool, string, etc as nil
  560. EncZeroValuesAsNil bool
  561. _ [7]uint64 // padding (cache-aligned)
  562. }
  563. // Name returns the name of the handle: simple
  564. func (h *SimpleHandle) Name() string { return "simple" }
  565. func (h *SimpleHandle) newEncDriver() encDriver {
  566. var e = &simpleEncDriver{h: h}
  567. e.e.e = e
  568. e.e.init(h)
  569. e.reset()
  570. return e
  571. }
  572. func (h *SimpleHandle) newDecDriver() decDriver {
  573. d := &simpleDecDriver{h: h}
  574. d.d.d = d
  575. d.d.init(h)
  576. d.reset()
  577. return d
  578. }
  579. func (e *simpleEncDriver) reset() {
  580. }
  581. func (d *simpleDecDriver) reset() {
  582. d.bd, d.bdRead = 0, false
  583. d.fnil = false
  584. }
  585. var _ decDriver = (*simpleDecDriver)(nil)
  586. var _ encDriver = (*simpleEncDriver)(nil)