simple.go 15 KB

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