simple.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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) ContainerType() (vt valueType) {
  144. if d.bd == simpleVdNil {
  145. return valueTypeNil
  146. } else if d.bd == simpleVdByteArray || d.bd == simpleVdByteArray+1 ||
  147. d.bd == simpleVdByteArray+2 || d.bd == simpleVdByteArray+3 || d.bd == simpleVdByteArray+4 {
  148. return valueTypeBytes
  149. } else if d.bd == simpleVdString || d.bd == simpleVdString+1 ||
  150. d.bd == simpleVdString+2 || d.bd == simpleVdString+3 || d.bd == simpleVdString+4 {
  151. return valueTypeString
  152. } else if d.bd == simpleVdArray || d.bd == simpleVdArray+1 ||
  153. d.bd == simpleVdArray+2 || d.bd == simpleVdArray+3 || d.bd == simpleVdArray+4 {
  154. return valueTypeArray
  155. } else if d.bd == simpleVdMap || d.bd == simpleVdMap+1 ||
  156. d.bd == simpleVdMap+2 || d.bd == simpleVdMap+3 || d.bd == simpleVdMap+4 {
  157. return valueTypeMap
  158. } else {
  159. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  160. }
  161. return valueTypeUnset
  162. }
  163. func (d *simpleDecDriver) TryDecodeAsNil() bool {
  164. if !d.bdRead {
  165. d.readNextBd()
  166. }
  167. if d.bd == simpleVdNil {
  168. d.bdRead = false
  169. return true
  170. }
  171. return false
  172. }
  173. func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
  174. if !d.bdRead {
  175. d.readNextBd()
  176. }
  177. switch d.bd {
  178. case simpleVdPosInt:
  179. ui = uint64(d.r.readn1())
  180. case simpleVdPosInt + 1:
  181. ui = uint64(bigen.Uint16(d.r.readx(2)))
  182. case simpleVdPosInt + 2:
  183. ui = uint64(bigen.Uint32(d.r.readx(4)))
  184. case simpleVdPosInt + 3:
  185. ui = uint64(bigen.Uint64(d.r.readx(8)))
  186. case simpleVdNegInt:
  187. ui = uint64(d.r.readn1())
  188. neg = true
  189. case simpleVdNegInt + 1:
  190. ui = uint64(bigen.Uint16(d.r.readx(2)))
  191. neg = true
  192. case simpleVdNegInt + 2:
  193. ui = uint64(bigen.Uint32(d.r.readx(4)))
  194. neg = true
  195. case simpleVdNegInt + 3:
  196. ui = uint64(bigen.Uint64(d.r.readx(8)))
  197. neg = true
  198. default:
  199. d.d.errorf("decIntAny: Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
  200. return
  201. }
  202. // don't do this check, because callers may only want the unsigned value.
  203. // if ui > math.MaxInt64 {
  204. // d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
  205. // return
  206. // }
  207. return
  208. }
  209. func (d *simpleDecDriver) DecodeInt(bitsize uint8) (i int64) {
  210. ui, neg := d.decCheckInteger()
  211. i, overflow := chkOvf.SignedInt(ui)
  212. if overflow {
  213. d.d.errorf("simple: overflow converting %v to signed integer", ui)
  214. return
  215. }
  216. if neg {
  217. i = -i
  218. }
  219. if chkOvf.Int(i, bitsize) {
  220. d.d.errorf("simple: overflow integer: %v", i)
  221. return
  222. }
  223. d.bdRead = false
  224. return
  225. }
  226. func (d *simpleDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  227. ui, neg := d.decCheckInteger()
  228. if neg {
  229. d.d.errorf("Assigning negative signed value to unsigned type")
  230. return
  231. }
  232. if chkOvf.Uint(ui, bitsize) {
  233. d.d.errorf("simple: overflow integer: %v", ui)
  234. return
  235. }
  236. d.bdRead = false
  237. return
  238. }
  239. func (d *simpleDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  240. if !d.bdRead {
  241. d.readNextBd()
  242. }
  243. if d.bd == simpleVdFloat32 {
  244. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  245. } else if d.bd == simpleVdFloat64 {
  246. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  247. } else {
  248. if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
  249. f = float64(d.DecodeInt(64))
  250. } else {
  251. d.d.errorf("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
  252. return
  253. }
  254. }
  255. if chkOverflow32 && chkOvf.Float32(f) {
  256. d.d.errorf("msgpack: float32 overflow: %v", f)
  257. return
  258. }
  259. d.bdRead = false
  260. return
  261. }
  262. // bool can be decoded from bool only (single byte).
  263. func (d *simpleDecDriver) DecodeBool() (b bool) {
  264. if !d.bdRead {
  265. d.readNextBd()
  266. }
  267. if d.bd == simpleVdTrue {
  268. b = true
  269. } else if d.bd == simpleVdFalse {
  270. } else {
  271. d.d.errorf("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  272. return
  273. }
  274. d.bdRead = false
  275. return
  276. }
  277. func (d *simpleDecDriver) ReadMapStart() (length int) {
  278. d.bdRead = false
  279. return d.decLen()
  280. }
  281. func (d *simpleDecDriver) ReadArrayStart() (length int) {
  282. d.bdRead = false
  283. return d.decLen()
  284. }
  285. func (d *simpleDecDriver) decLen() int {
  286. switch d.bd % 8 {
  287. case 0:
  288. return 0
  289. case 1:
  290. return int(d.r.readn1())
  291. case 2:
  292. return int(bigen.Uint16(d.r.readx(2)))
  293. case 3:
  294. ui := uint64(bigen.Uint32(d.r.readx(4)))
  295. if chkOvf.Uint(ui, intBitsize) {
  296. d.d.errorf("simple: overflow integer: %v", ui)
  297. return 0
  298. }
  299. return int(ui)
  300. case 4:
  301. ui := bigen.Uint64(d.r.readx(8))
  302. if chkOvf.Uint(ui, intBitsize) {
  303. d.d.errorf("simple: overflow integer: %v", ui)
  304. return 0
  305. }
  306. return int(ui)
  307. }
  308. d.d.errorf("decLen: Cannot read length: bd%8 must be in range 0..4. Got: %d", d.bd%8)
  309. return -1
  310. }
  311. func (d *simpleDecDriver) DecodeString() (s string) {
  312. return string(d.DecodeBytes(d.b[:], true, true))
  313. }
  314. func (d *simpleDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) {
  315. if !d.bdRead {
  316. d.readNextBd()
  317. }
  318. if d.bd == simpleVdNil {
  319. d.bdRead = false
  320. return
  321. }
  322. clen := d.decLen()
  323. d.bdRead = false
  324. if zerocopy {
  325. if d.br {
  326. return d.r.readx(clen)
  327. } else if len(bs) == 0 {
  328. bs = d.b[:]
  329. }
  330. }
  331. return decByteSlice(d.r, clen, bs)
  332. }
  333. func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  334. if xtag > 0xff {
  335. d.d.errorf("decodeExt: tag must be <= 0xff; got: %v", xtag)
  336. return
  337. }
  338. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  339. realxtag = uint64(realxtag1)
  340. if ext == nil {
  341. re := rv.(*RawExt)
  342. re.Tag = realxtag
  343. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  344. } else {
  345. ext.ReadExt(rv, xbs)
  346. }
  347. return
  348. }
  349. func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  350. if !d.bdRead {
  351. d.readNextBd()
  352. }
  353. switch d.bd {
  354. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  355. l := d.decLen()
  356. xtag = d.r.readn1()
  357. if verifyTag && xtag != tag {
  358. d.d.errorf("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  359. return
  360. }
  361. xbs = d.r.readx(l)
  362. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  363. xbs = d.DecodeBytes(nil, false, true)
  364. default:
  365. d.d.errorf("Invalid d.bd for extensions (Expecting extensions or byte array). Got: 0x%x", d.bd)
  366. return
  367. }
  368. d.bdRead = false
  369. return
  370. }
  371. func (d *simpleDecDriver) DecodeNaked() {
  372. if !d.bdRead {
  373. d.readNextBd()
  374. }
  375. n := &d.d.n
  376. var decodeFurther bool
  377. switch d.bd {
  378. case simpleVdNil:
  379. n.v = valueTypeNil
  380. case simpleVdFalse:
  381. n.v = valueTypeBool
  382. n.b = false
  383. case simpleVdTrue:
  384. n.v = valueTypeBool
  385. n.b = true
  386. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  387. if d.h.SignedInteger {
  388. n.v = valueTypeInt
  389. n.i = d.DecodeInt(64)
  390. } else {
  391. n.v = valueTypeUint
  392. n.u = d.DecodeUint(64)
  393. }
  394. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  395. n.v = valueTypeInt
  396. n.i = d.DecodeInt(64)
  397. case simpleVdFloat32:
  398. n.v = valueTypeFloat
  399. n.f = d.DecodeFloat(true)
  400. case simpleVdFloat64:
  401. n.v = valueTypeFloat
  402. n.f = d.DecodeFloat(false)
  403. case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  404. n.v = valueTypeString
  405. n.s = d.DecodeString()
  406. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  407. n.v = valueTypeBytes
  408. n.l = d.DecodeBytes(nil, false, false)
  409. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  410. n.v = valueTypeExt
  411. l := d.decLen()
  412. n.u = uint64(d.r.readn1())
  413. n.l = d.r.readx(l)
  414. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  415. n.v = valueTypeArray
  416. decodeFurther = true
  417. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  418. n.v = valueTypeMap
  419. decodeFurther = true
  420. default:
  421. d.d.errorf("decodeNaked: Unrecognized d.bd: 0x%x", d.bd)
  422. }
  423. if !decodeFurther {
  424. d.bdRead = false
  425. }
  426. return
  427. }
  428. //------------------------------------
  429. // SimpleHandle is a Handle for a very simple encoding format.
  430. //
  431. // simple is a simplistic codec similar to binc, but not as compact.
  432. // - Encoding of a value is always preceeded by the descriptor byte (bd)
  433. // - True, false, nil are encoded fully in 1 byte (the descriptor)
  434. // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
  435. // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
  436. // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
  437. // - Lenght of containers (strings, bytes, array, map, extensions)
  438. // are encoded in 0, 1, 2, 4 or 8 bytes.
  439. // Zero-length containers have no length encoded.
  440. // For others, the number of bytes is given by pow(2, bd%3)
  441. // - maps are encoded as [bd] [length] [[key][value]]...
  442. // - arrays are encoded as [bd] [length] [value]...
  443. // - extensions are encoded as [bd] [length] [tag] [byte]...
  444. // - strings/bytearrays are encoded as [bd] [length] [byte]...
  445. //
  446. // The full spec will be published soon.
  447. type SimpleHandle struct {
  448. BasicHandle
  449. binaryEncodingType
  450. }
  451. func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  452. return h.SetExt(rt, tag, &setExtWrapper{b: ext})
  453. }
  454. func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver {
  455. return &simpleEncDriver{e: e, w: e.w, h: h}
  456. }
  457. func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver {
  458. return &simpleDecDriver{d: d, r: d.r, h: h, br: d.bytes}
  459. }
  460. func (e *simpleEncDriver) reset() {
  461. e.w = e.e.w
  462. }
  463. func (d *simpleDecDriver) reset() {
  464. d.r = d.d.r
  465. }
  466. var _ decDriver = (*simpleDecDriver)(nil)
  467. var _ encDriver = (*simpleEncDriver)(nil)