simple.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Copyright (c) 2012-2015 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. )
  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. // containers: each lasts for 4 (ie n, n+1, n+2, ... n+7)
  19. simpleVdString = 216
  20. simpleVdByteArray = 224
  21. simpleVdArray = 232
  22. simpleVdMap = 240
  23. simpleVdExt = 248
  24. )
  25. type simpleEncDriver struct {
  26. noBuiltInTypes
  27. encNoSeparator
  28. e *Encoder
  29. h *SimpleHandle
  30. w encWriter
  31. b [8]byte
  32. }
  33. func (e *simpleEncDriver) EncodeNil() {
  34. e.w.writen1(simpleVdNil)
  35. }
  36. func (e *simpleEncDriver) EncodeBool(b bool) {
  37. if b {
  38. e.w.writen1(simpleVdTrue)
  39. } else {
  40. e.w.writen1(simpleVdFalse)
  41. }
  42. }
  43. func (e *simpleEncDriver) EncodeFloat32(f float32) {
  44. e.w.writen1(simpleVdFloat32)
  45. bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
  46. }
  47. func (e *simpleEncDriver) EncodeFloat64(f float64) {
  48. e.w.writen1(simpleVdFloat64)
  49. bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f))
  50. }
  51. func (e *simpleEncDriver) EncodeInt(v int64) {
  52. if v < 0 {
  53. e.encUint(uint64(-v), simpleVdNegInt)
  54. } else {
  55. e.encUint(uint64(v), simpleVdPosInt)
  56. }
  57. }
  58. func (e *simpleEncDriver) EncodeUint(v uint64) {
  59. e.encUint(v, simpleVdPosInt)
  60. }
  61. func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
  62. if v <= math.MaxUint8 {
  63. e.w.writen2(bd, uint8(v))
  64. } else if v <= math.MaxUint16 {
  65. e.w.writen1(bd + 1)
  66. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  67. } else if v <= math.MaxUint32 {
  68. e.w.writen1(bd + 2)
  69. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
  70. } else { // if v <= math.MaxUint64 {
  71. e.w.writen1(bd + 3)
  72. bigenHelper{e.b[:8], e.w}.writeUint64(v)
  73. }
  74. }
  75. func (e *simpleEncDriver) encLen(bd byte, length int) {
  76. if length == 0 {
  77. e.w.writen1(bd)
  78. } else if length <= math.MaxUint8 {
  79. e.w.writen1(bd + 1)
  80. e.w.writen1(uint8(length))
  81. } else if length <= math.MaxUint16 {
  82. e.w.writen1(bd + 2)
  83. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length))
  84. } else if int64(length) <= math.MaxUint32 {
  85. e.w.writen1(bd + 3)
  86. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length))
  87. } else {
  88. e.w.writen1(bd + 4)
  89. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length))
  90. }
  91. }
  92. func (e *simpleEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
  93. bs := ext.WriteExt(rv)
  94. if bs == nil {
  95. e.EncodeNil()
  96. return
  97. }
  98. e.encodeExtPreamble(uint8(xtag), len(bs))
  99. e.w.writeb(bs)
  100. }
  101. func (e *simpleEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  102. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  103. e.w.writeb(re.Data)
  104. }
  105. func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
  106. e.encLen(simpleVdExt, length)
  107. e.w.writen1(xtag)
  108. }
  109. func (e *simpleEncDriver) EncodeArrayStart(length int) {
  110. e.encLen(simpleVdArray, length)
  111. }
  112. func (e *simpleEncDriver) EncodeMapStart(length int) {
  113. e.encLen(simpleVdMap, length)
  114. }
  115. func (e *simpleEncDriver) EncodeString(c charEncoding, v string) {
  116. e.encLen(simpleVdString, len(v))
  117. e.w.writestr(v)
  118. }
  119. func (e *simpleEncDriver) EncodeSymbol(v string) {
  120. e.EncodeString(c_UTF8, v)
  121. }
  122. func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
  123. e.encLen(simpleVdByteArray, len(v))
  124. e.w.writeb(v)
  125. }
  126. //------------------------------------
  127. type simpleDecDriver struct {
  128. d *Decoder
  129. h *SimpleHandle
  130. r decReader
  131. bdRead bool
  132. bd byte
  133. br bool // bytes reader
  134. noBuiltInTypes
  135. noStreamingCodec
  136. decNoSeparator
  137. b [scratchByteArrayLen]byte
  138. }
  139. func (d *simpleDecDriver) readNextBd() {
  140. d.bd = d.r.readn1()
  141. d.bdRead = true
  142. }
  143. func (d *simpleDecDriver) uncacheRead() {
  144. if d.bdRead {
  145. d.r.unreadn1()
  146. d.bdRead = false
  147. }
  148. }
  149. func (d *simpleDecDriver) ContainerType() (vt valueType) {
  150. if d.bd == simpleVdNil {
  151. return valueTypeNil
  152. } else if d.bd == simpleVdByteArray || d.bd == simpleVdByteArray+1 ||
  153. d.bd == simpleVdByteArray+2 || d.bd == simpleVdByteArray+3 || d.bd == simpleVdByteArray+4 {
  154. return valueTypeBytes
  155. } else if d.bd == simpleVdString || d.bd == simpleVdString+1 ||
  156. d.bd == simpleVdString+2 || d.bd == simpleVdString+3 || d.bd == simpleVdString+4 {
  157. return valueTypeString
  158. } else if d.bd == simpleVdArray || d.bd == simpleVdArray+1 ||
  159. d.bd == simpleVdArray+2 || d.bd == simpleVdArray+3 || d.bd == simpleVdArray+4 {
  160. return valueTypeArray
  161. } else if d.bd == simpleVdMap || d.bd == simpleVdMap+1 ||
  162. d.bd == simpleVdMap+2 || d.bd == simpleVdMap+3 || d.bd == simpleVdMap+4 {
  163. return valueTypeMap
  164. } else {
  165. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  166. }
  167. return valueTypeUnset
  168. }
  169. func (d *simpleDecDriver) TryDecodeAsNil() bool {
  170. if !d.bdRead {
  171. d.readNextBd()
  172. }
  173. if d.bd == simpleVdNil {
  174. d.bdRead = false
  175. return true
  176. }
  177. return false
  178. }
  179. func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
  180. if !d.bdRead {
  181. d.readNextBd()
  182. }
  183. switch d.bd {
  184. case simpleVdPosInt:
  185. ui = uint64(d.r.readn1())
  186. case simpleVdPosInt + 1:
  187. ui = uint64(bigen.Uint16(d.r.readx(2)))
  188. case simpleVdPosInt + 2:
  189. ui = uint64(bigen.Uint32(d.r.readx(4)))
  190. case simpleVdPosInt + 3:
  191. ui = uint64(bigen.Uint64(d.r.readx(8)))
  192. case simpleVdNegInt:
  193. ui = uint64(d.r.readn1())
  194. neg = true
  195. case simpleVdNegInt + 1:
  196. ui = uint64(bigen.Uint16(d.r.readx(2)))
  197. neg = true
  198. case simpleVdNegInt + 2:
  199. ui = uint64(bigen.Uint32(d.r.readx(4)))
  200. neg = true
  201. case simpleVdNegInt + 3:
  202. ui = uint64(bigen.Uint64(d.r.readx(8)))
  203. neg = true
  204. default:
  205. d.d.errorf("decIntAny: Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
  206. return
  207. }
  208. // don't do this check, because callers may only want the unsigned value.
  209. // if ui > math.MaxInt64 {
  210. // d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
  211. // return
  212. // }
  213. return
  214. }
  215. func (d *simpleDecDriver) DecodeInt(bitsize uint8) (i int64) {
  216. ui, neg := d.decCheckInteger()
  217. i, overflow := chkOvf.SignedInt(ui)
  218. if overflow {
  219. d.d.errorf("simple: overflow converting %v to signed integer", ui)
  220. return
  221. }
  222. if neg {
  223. i = -i
  224. }
  225. if chkOvf.Int(i, bitsize) {
  226. d.d.errorf("simple: overflow integer: %v", i)
  227. return
  228. }
  229. d.bdRead = false
  230. return
  231. }
  232. func (d *simpleDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  233. ui, neg := d.decCheckInteger()
  234. if neg {
  235. d.d.errorf("Assigning negative signed value to unsigned type")
  236. return
  237. }
  238. if chkOvf.Uint(ui, bitsize) {
  239. d.d.errorf("simple: overflow integer: %v", ui)
  240. return
  241. }
  242. d.bdRead = false
  243. return
  244. }
  245. func (d *simpleDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  246. if !d.bdRead {
  247. d.readNextBd()
  248. }
  249. if d.bd == simpleVdFloat32 {
  250. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  251. } else if d.bd == simpleVdFloat64 {
  252. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  253. } else {
  254. if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
  255. f = float64(d.DecodeInt(64))
  256. } else {
  257. d.d.errorf("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
  258. return
  259. }
  260. }
  261. if chkOverflow32 && chkOvf.Float32(f) {
  262. d.d.errorf("msgpack: float32 overflow: %v", f)
  263. return
  264. }
  265. d.bdRead = false
  266. return
  267. }
  268. // bool can be decoded from bool only (single byte).
  269. func (d *simpleDecDriver) DecodeBool() (b bool) {
  270. if !d.bdRead {
  271. d.readNextBd()
  272. }
  273. if d.bd == simpleVdTrue {
  274. b = true
  275. } else if d.bd == simpleVdFalse {
  276. } else {
  277. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  278. return
  279. }
  280. d.bdRead = false
  281. return
  282. }
  283. func (d *simpleDecDriver) ReadMapStart() (length int) {
  284. d.bdRead = false
  285. return d.decLen()
  286. }
  287. func (d *simpleDecDriver) ReadArrayStart() (length int) {
  288. d.bdRead = false
  289. return d.decLen()
  290. }
  291. func (d *simpleDecDriver) decLen() int {
  292. switch d.bd % 8 {
  293. case 0:
  294. return 0
  295. case 1:
  296. return int(d.r.readn1())
  297. case 2:
  298. return int(bigen.Uint16(d.r.readx(2)))
  299. case 3:
  300. ui := uint64(bigen.Uint32(d.r.readx(4)))
  301. if chkOvf.Uint(ui, intBitsize) {
  302. d.d.errorf("simple: overflow integer: %v", ui)
  303. return 0
  304. }
  305. return int(ui)
  306. case 4:
  307. ui := bigen.Uint64(d.r.readx(8))
  308. if chkOvf.Uint(ui, intBitsize) {
  309. d.d.errorf("simple: overflow integer: %v", ui)
  310. return 0
  311. }
  312. return int(ui)
  313. }
  314. d.d.errorf("decLen: Cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8)
  315. return -1
  316. }
  317. func (d *simpleDecDriver) DecodeString() (s string) {
  318. return string(d.DecodeBytes(d.b[:], true, true))
  319. }
  320. func (d *simpleDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
  321. if !d.bdRead {
  322. d.readNextBd()
  323. }
  324. if d.bd == simpleVdNil {
  325. d.bdRead = false
  326. return
  327. }
  328. clen := d.decLen()
  329. d.bdRead = false
  330. if zerocopy {
  331. if d.br {
  332. return d.r.readx(clen)
  333. } else if len(bs) == 0 {
  334. bs = d.b[:]
  335. }
  336. }
  337. return decByteSlice(d.r, clen, bs)
  338. }
  339. func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  340. if xtag > 0xff {
  341. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  342. return
  343. }
  344. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  345. realxtag = uint64(realxtag1)
  346. if ext == nil {
  347. re := rv.(*RawExt)
  348. re.Tag = realxtag
  349. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  350. } else {
  351. ext.ReadExt(rv, xbs)
  352. }
  353. return
  354. }
  355. func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  356. if !d.bdRead {
  357. d.readNextBd()
  358. }
  359. switch d.bd {
  360. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  361. l := d.decLen()
  362. xtag = d.r.readn1()
  363. if verifyTag && xtag != tag {
  364. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  365. return
  366. }
  367. xbs = d.r.readx(l)
  368. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  369. xbs = d.DecodeBytes(nil, false, true)
  370. default:
  371. d.d.errorf("Invalid d.bd for extensions (Expecting extensions or byte array). Got: 0x%x", d.bd)
  372. return
  373. }
  374. d.bdRead = false
  375. return
  376. }
  377. func (d *simpleDecDriver) DecodeNaked() {
  378. if !d.bdRead {
  379. d.readNextBd()
  380. }
  381. n := &d.d.n
  382. var decodeFurther bool
  383. switch d.bd {
  384. case simpleVdNil:
  385. n.v = valueTypeNil
  386. case simpleVdFalse:
  387. n.v = valueTypeBool
  388. n.b = false
  389. case simpleVdTrue:
  390. n.v = valueTypeBool
  391. n.b = true
  392. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  393. if d.h.SignedInteger {
  394. n.v = valueTypeInt
  395. n.i = d.DecodeInt(64)
  396. } else {
  397. n.v = valueTypeUint
  398. n.u = d.DecodeUint(64)
  399. }
  400. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  401. n.v = valueTypeInt
  402. n.i = d.DecodeInt(64)
  403. case simpleVdFloat32:
  404. n.v = valueTypeFloat
  405. n.f = d.DecodeFloat(true)
  406. case simpleVdFloat64:
  407. n.v = valueTypeFloat
  408. n.f = d.DecodeFloat(false)
  409. case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  410. n.v = valueTypeString
  411. n.s = d.DecodeString()
  412. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  413. n.v = valueTypeBytes
  414. n.l = d.DecodeBytes(nil, false, false)
  415. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  416. n.v = valueTypeExt
  417. l := d.decLen()
  418. n.u = uint64(d.r.readn1())
  419. n.l = d.r.readx(l)
  420. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  421. n.v = valueTypeArray
  422. decodeFurther = true
  423. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  424. n.v = valueTypeMap
  425. decodeFurther = true
  426. default:
  427. d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
  428. }
  429. if !decodeFurther {
  430. d.bdRead = false
  431. }
  432. return
  433. }
  434. //------------------------------------
  435. // SimpleHandle is a Handle for a very simple encoding format.
  436. //
  437. // simple is a simplistic codec similar to binc, but not as compact.
  438. // - Encoding of a value is always preceded by the descriptor byte (bd)
  439. // - True, false, nil are encoded fully in 1 byte (the descriptor)
  440. // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
  441. // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
  442. // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
  443. // - Lenght of containers (strings, bytes, array, map, extensions)
  444. // are encoded in 0, 1, 2, 4 or 8 bytes.
  445. // Zero-length containers have no length encoded.
  446. // For others, the number of bytes is given by pow(2, bd%3)
  447. // - maps are encoded as [bd] [length] [[key][value]]...
  448. // - arrays are encoded as [bd] [length] [value]...
  449. // - extensions are encoded as [bd] [length] [tag] [byte]...
  450. // - strings/bytearrays are encoded as [bd] [length] [byte]...
  451. //
  452. // The full spec will be published soon.
  453. type SimpleHandle struct {
  454. BasicHandle
  455. binaryEncodingType
  456. }
  457. func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  458. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  459. }
  460. func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver {
  461. return &simpleEncDriver{e: e, w: e.w, h: h}
  462. }
  463. func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver {
  464. return &simpleDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  465. }
  466. func (e *simpleEncDriver) reset() {
  467. e.w = e.e.w
  468. }
  469. func (d *simpleDecDriver) reset() {
  470. d.r = d.d.r
  471. d.bd, d.bdRead = 0, false
  472. }
  473. var _ decDriver = (*simpleDecDriver)(nil)
  474. var _ encDriver = (*simpleEncDriver)(nil)