PageRenderTime 127ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/hashicorp/go-msgpack/codec/simple.go

https://gitlab.com/hasura/kubernetes
Go | 461 lines | 379 code | 50 blank | 32 comment | 32 complexity | 285422664a8450409341246f316cac0f MD5 | raw file
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. import "math"
  5. const (
  6. _ uint8 = iota
  7. simpleVdNil = 1
  8. simpleVdFalse = 2
  9. simpleVdTrue = 3
  10. simpleVdFloat32 = 4
  11. simpleVdFloat64 = 5
  12. // each lasts for 4 (ie n, n+1, n+2, n+3)
  13. simpleVdPosInt = 8
  14. simpleVdNegInt = 12
  15. // containers: each lasts for 4 (ie n, n+1, n+2, ... n+7)
  16. simpleVdString = 216
  17. simpleVdByteArray = 224
  18. simpleVdArray = 232
  19. simpleVdMap = 240
  20. simpleVdExt = 248
  21. )
  22. type simpleEncDriver struct {
  23. h *SimpleHandle
  24. w encWriter
  25. //b [8]byte
  26. }
  27. func (e *simpleEncDriver) isBuiltinType(rt uintptr) bool {
  28. return false
  29. }
  30. func (e *simpleEncDriver) encodeBuiltin(rt uintptr, v interface{}) {
  31. }
  32. func (e *simpleEncDriver) encodeNil() {
  33. e.w.writen1(simpleVdNil)
  34. }
  35. func (e *simpleEncDriver) encodeBool(b bool) {
  36. if b {
  37. e.w.writen1(simpleVdTrue)
  38. } else {
  39. e.w.writen1(simpleVdFalse)
  40. }
  41. }
  42. func (e *simpleEncDriver) encodeFloat32(f float32) {
  43. e.w.writen1(simpleVdFloat32)
  44. e.w.writeUint32(math.Float32bits(f))
  45. }
  46. func (e *simpleEncDriver) encodeFloat64(f float64) {
  47. e.w.writen1(simpleVdFloat64)
  48. e.w.writeUint64(math.Float64bits(f))
  49. }
  50. func (e *simpleEncDriver) encodeInt(v int64) {
  51. if v < 0 {
  52. e.encUint(uint64(-v), simpleVdNegInt)
  53. } else {
  54. e.encUint(uint64(v), simpleVdPosInt)
  55. }
  56. }
  57. func (e *simpleEncDriver) encodeUint(v uint64) {
  58. e.encUint(v, simpleVdPosInt)
  59. }
  60. func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
  61. switch {
  62. case v <= math.MaxUint8:
  63. e.w.writen2(bd, uint8(v))
  64. case v <= math.MaxUint16:
  65. e.w.writen1(bd + 1)
  66. e.w.writeUint16(uint16(v))
  67. case v <= math.MaxUint32:
  68. e.w.writen1(bd + 2)
  69. e.w.writeUint32(uint32(v))
  70. case v <= math.MaxUint64:
  71. e.w.writen1(bd + 3)
  72. e.w.writeUint64(v)
  73. }
  74. }
  75. func (e *simpleEncDriver) encLen(bd byte, length int) {
  76. switch {
  77. case length == 0:
  78. e.w.writen1(bd)
  79. case length <= math.MaxUint8:
  80. e.w.writen1(bd + 1)
  81. e.w.writen1(uint8(length))
  82. case length <= math.MaxUint16:
  83. e.w.writen1(bd + 2)
  84. e.w.writeUint16(uint16(length))
  85. case int64(length) <= math.MaxUint32:
  86. e.w.writen1(bd + 3)
  87. e.w.writeUint32(uint32(length))
  88. default:
  89. e.w.writen1(bd + 4)
  90. e.w.writeUint64(uint64(length))
  91. }
  92. }
  93. func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
  94. e.encLen(simpleVdExt, length)
  95. e.w.writen1(xtag)
  96. }
  97. func (e *simpleEncDriver) encodeArrayPreamble(length int) {
  98. e.encLen(simpleVdArray, length)
  99. }
  100. func (e *simpleEncDriver) encodeMapPreamble(length int) {
  101. e.encLen(simpleVdMap, length)
  102. }
  103. func (e *simpleEncDriver) encodeString(c charEncoding, v string) {
  104. e.encLen(simpleVdString, len(v))
  105. e.w.writestr(v)
  106. }
  107. func (e *simpleEncDriver) encodeSymbol(v string) {
  108. e.encodeString(c_UTF8, v)
  109. }
  110. func (e *simpleEncDriver) encodeStringBytes(c charEncoding, v []byte) {
  111. e.encLen(simpleVdByteArray, len(v))
  112. e.w.writeb(v)
  113. }
  114. //------------------------------------
  115. type simpleDecDriver struct {
  116. h *SimpleHandle
  117. r decReader
  118. bdRead bool
  119. bdType valueType
  120. bd byte
  121. //b [8]byte
  122. }
  123. func (d *simpleDecDriver) initReadNext() {
  124. if d.bdRead {
  125. return
  126. }
  127. d.bd = d.r.readn1()
  128. d.bdRead = true
  129. d.bdType = valueTypeUnset
  130. }
  131. func (d *simpleDecDriver) currentEncodedType() valueType {
  132. if d.bdType == valueTypeUnset {
  133. switch d.bd {
  134. case simpleVdNil:
  135. d.bdType = valueTypeNil
  136. case simpleVdTrue, simpleVdFalse:
  137. d.bdType = valueTypeBool
  138. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  139. d.bdType = valueTypeUint
  140. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  141. d.bdType = valueTypeInt
  142. case simpleVdFloat32, simpleVdFloat64:
  143. d.bdType = valueTypeFloat
  144. case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  145. d.bdType = valueTypeString
  146. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  147. d.bdType = valueTypeBytes
  148. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  149. d.bdType = valueTypeExt
  150. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  151. d.bdType = valueTypeArray
  152. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  153. d.bdType = valueTypeMap
  154. default:
  155. decErr("currentEncodedType: Unrecognized d.vd: 0x%x", d.bd)
  156. }
  157. }
  158. return d.bdType
  159. }
  160. func (d *simpleDecDriver) tryDecodeAsNil() bool {
  161. if d.bd == simpleVdNil {
  162. d.bdRead = false
  163. return true
  164. }
  165. return false
  166. }
  167. func (d *simpleDecDriver) isBuiltinType(rt uintptr) bool {
  168. return false
  169. }
  170. func (d *simpleDecDriver) decodeBuiltin(rt uintptr, v interface{}) {
  171. }
  172. func (d *simpleDecDriver) decIntAny() (ui uint64, i int64, neg bool) {
  173. switch d.bd {
  174. case simpleVdPosInt:
  175. ui = uint64(d.r.readn1())
  176. i = int64(ui)
  177. case simpleVdPosInt + 1:
  178. ui = uint64(d.r.readUint16())
  179. i = int64(ui)
  180. case simpleVdPosInt + 2:
  181. ui = uint64(d.r.readUint32())
  182. i = int64(ui)
  183. case simpleVdPosInt + 3:
  184. ui = uint64(d.r.readUint64())
  185. i = int64(ui)
  186. case simpleVdNegInt:
  187. ui = uint64(d.r.readn1())
  188. i = -(int64(ui))
  189. neg = true
  190. case simpleVdNegInt + 1:
  191. ui = uint64(d.r.readUint16())
  192. i = -(int64(ui))
  193. neg = true
  194. case simpleVdNegInt + 2:
  195. ui = uint64(d.r.readUint32())
  196. i = -(int64(ui))
  197. neg = true
  198. case simpleVdNegInt + 3:
  199. ui = uint64(d.r.readUint64())
  200. i = -(int64(ui))
  201. neg = true
  202. default:
  203. decErr("decIntAny: Integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
  204. }
  205. // don't do this check, because callers may only want the unsigned value.
  206. // if ui > math.MaxInt64 {
  207. // decErr("decIntAny: Integer out of range for signed int64: %v", ui)
  208. // }
  209. return
  210. }
  211. func (d *simpleDecDriver) decodeInt(bitsize uint8) (i int64) {
  212. _, i, _ = d.decIntAny()
  213. checkOverflow(0, i, bitsize)
  214. d.bdRead = false
  215. return
  216. }
  217. func (d *simpleDecDriver) decodeUint(bitsize uint8) (ui uint64) {
  218. ui, i, neg := d.decIntAny()
  219. if neg {
  220. decErr("Assigning negative signed value: %v, to unsigned type", i)
  221. }
  222. checkOverflow(ui, 0, bitsize)
  223. d.bdRead = false
  224. return
  225. }
  226. func (d *simpleDecDriver) decodeFloat(chkOverflow32 bool) (f float64) {
  227. switch d.bd {
  228. case simpleVdFloat32:
  229. f = float64(math.Float32frombits(d.r.readUint32()))
  230. case simpleVdFloat64:
  231. f = math.Float64frombits(d.r.readUint64())
  232. default:
  233. if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
  234. _, i, _ := d.decIntAny()
  235. f = float64(i)
  236. } else {
  237. decErr("Float only valid from float32/64: Invalid descriptor: %v", d.bd)
  238. }
  239. }
  240. checkOverflowFloat32(f, chkOverflow32)
  241. d.bdRead = false
  242. return
  243. }
  244. // bool can be decoded from bool only (single byte).
  245. func (d *simpleDecDriver) decodeBool() (b bool) {
  246. switch d.bd {
  247. case simpleVdTrue:
  248. b = true
  249. case simpleVdFalse:
  250. default:
  251. decErr("Invalid single-byte value for bool: %s: %x", msgBadDesc, d.bd)
  252. }
  253. d.bdRead = false
  254. return
  255. }
  256. func (d *simpleDecDriver) readMapLen() (length int) {
  257. d.bdRead = false
  258. return d.decLen()
  259. }
  260. func (d *simpleDecDriver) readArrayLen() (length int) {
  261. d.bdRead = false
  262. return d.decLen()
  263. }
  264. func (d *simpleDecDriver) decLen() int {
  265. switch d.bd % 8 {
  266. case 0:
  267. return 0
  268. case 1:
  269. return int(d.r.readn1())
  270. case 2:
  271. return int(d.r.readUint16())
  272. case 3:
  273. ui := uint64(d.r.readUint32())
  274. checkOverflow(ui, 0, intBitsize)
  275. return int(ui)
  276. case 4:
  277. ui := d.r.readUint64()
  278. checkOverflow(ui, 0, intBitsize)
  279. return int(ui)
  280. }
  281. decErr("decLen: Cannot read length: bd%8 must be in range 0..4. Got: %d", d.bd%8)
  282. return -1
  283. }
  284. func (d *simpleDecDriver) decodeString() (s string) {
  285. s = string(d.r.readn(d.decLen()))
  286. d.bdRead = false
  287. return
  288. }
  289. func (d *simpleDecDriver) decodeBytes(bs []byte) (bsOut []byte, changed bool) {
  290. if clen := d.decLen(); clen > 0 {
  291. // if no contents in stream, don't update the passed byteslice
  292. if len(bs) != clen {
  293. if len(bs) > clen {
  294. bs = bs[:clen]
  295. } else {
  296. bs = make([]byte, clen)
  297. }
  298. bsOut = bs
  299. changed = true
  300. }
  301. d.r.readb(bs)
  302. }
  303. d.bdRead = false
  304. return
  305. }
  306. func (d *simpleDecDriver) decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  307. switch d.bd {
  308. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  309. l := d.decLen()
  310. xtag = d.r.readn1()
  311. if verifyTag && xtag != tag {
  312. decErr("Wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  313. }
  314. xbs = d.r.readn(l)
  315. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  316. xbs, _ = d.decodeBytes(nil)
  317. default:
  318. decErr("Invalid d.vd for extensions (Expecting extensions or byte array). Got: 0x%x", d.bd)
  319. }
  320. d.bdRead = false
  321. return
  322. }
  323. func (d *simpleDecDriver) decodeNaked() (v interface{}, vt valueType, decodeFurther bool) {
  324. d.initReadNext()
  325. switch d.bd {
  326. case simpleVdNil:
  327. vt = valueTypeNil
  328. case simpleVdFalse:
  329. vt = valueTypeBool
  330. v = false
  331. case simpleVdTrue:
  332. vt = valueTypeBool
  333. v = true
  334. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  335. vt = valueTypeUint
  336. ui, _, _ := d.decIntAny()
  337. v = ui
  338. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  339. vt = valueTypeInt
  340. _, i, _ := d.decIntAny()
  341. v = i
  342. case simpleVdFloat32:
  343. vt = valueTypeFloat
  344. v = d.decodeFloat(true)
  345. case simpleVdFloat64:
  346. vt = valueTypeFloat
  347. v = d.decodeFloat(false)
  348. case simpleVdString, simpleVdString + 1, simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  349. vt = valueTypeString
  350. v = d.decodeString()
  351. case simpleVdByteArray, simpleVdByteArray + 1, simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  352. vt = valueTypeBytes
  353. v, _ = d.decodeBytes(nil)
  354. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  355. vt = valueTypeExt
  356. l := d.decLen()
  357. var re RawExt
  358. re.Tag = d.r.readn1()
  359. re.Data = d.r.readn(l)
  360. v = &re
  361. vt = valueTypeExt
  362. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  363. vt = valueTypeArray
  364. decodeFurther = true
  365. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  366. vt = valueTypeMap
  367. decodeFurther = true
  368. default:
  369. decErr("decodeNaked: Unrecognized d.vd: 0x%x", d.bd)
  370. }
  371. if !decodeFurther {
  372. d.bdRead = false
  373. }
  374. return
  375. }
  376. //------------------------------------
  377. // SimpleHandle is a Handle for a very simple encoding format.
  378. //
  379. // simple is a simplistic codec similar to binc, but not as compact.
  380. // - Encoding of a value is always preceeded by the descriptor byte (bd)
  381. // - True, false, nil are encoded fully in 1 byte (the descriptor)
  382. // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
  383. // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
  384. // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
  385. // - Lenght of containers (strings, bytes, array, map, extensions)
  386. // are encoded in 0, 1, 2, 4 or 8 bytes.
  387. // Zero-length containers have no length encoded.
  388. // For others, the number of bytes is given by pow(2, bd%3)
  389. // - maps are encoded as [bd] [length] [[key][value]]...
  390. // - arrays are encoded as [bd] [length] [value]...
  391. // - extensions are encoded as [bd] [length] [tag] [byte]...
  392. // - strings/bytearrays are encoded as [bd] [length] [byte]...
  393. //
  394. // The full spec will be published soon.
  395. type SimpleHandle struct {
  396. BasicHandle
  397. }
  398. func (h *SimpleHandle) newEncDriver(w encWriter) encDriver {
  399. return &simpleEncDriver{w: w, h: h}
  400. }
  401. func (h *SimpleHandle) newDecDriver(r decReader) decDriver {
  402. return &simpleDecDriver{r: r, h: h}
  403. }
  404. func (_ *SimpleHandle) writeExt() bool {
  405. return true
  406. }
  407. func (h *SimpleHandle) getBasicHandle() *BasicHandle {
  408. return &h.BasicHandle
  409. }
  410. var _ decDriver = (*simpleDecDriver)(nil)
  411. var _ encDriver = (*simpleEncDriver)(nil)