You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
5.5 KiB

  1. package msgpack
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "github.com/vmihailenco/msgpack/codes"
  7. )
  8. func (d *Decoder) skipN(n int) error {
  9. _, err := d.readN(n)
  10. return err
  11. }
  12. func (d *Decoder) uint8() (uint8, error) {
  13. c, err := d.readCode()
  14. if err != nil {
  15. return 0, err
  16. }
  17. return uint8(c), nil
  18. }
  19. func (d *Decoder) int8() (int8, error) {
  20. n, err := d.uint8()
  21. return int8(n), err
  22. }
  23. func (d *Decoder) uint16() (uint16, error) {
  24. b, err := d.readN(2)
  25. if err != nil {
  26. return 0, err
  27. }
  28. return (uint16(b[0]) << 8) | uint16(b[1]), nil
  29. }
  30. func (d *Decoder) int16() (int16, error) {
  31. n, err := d.uint16()
  32. return int16(n), err
  33. }
  34. func (d *Decoder) uint32() (uint32, error) {
  35. b, err := d.readN(4)
  36. if err != nil {
  37. return 0, err
  38. }
  39. n := (uint32(b[0]) << 24) |
  40. (uint32(b[1]) << 16) |
  41. (uint32(b[2]) << 8) |
  42. uint32(b[3])
  43. return n, nil
  44. }
  45. func (d *Decoder) int32() (int32, error) {
  46. n, err := d.uint32()
  47. return int32(n), err
  48. }
  49. func (d *Decoder) uint64() (uint64, error) {
  50. b, err := d.readN(8)
  51. if err != nil {
  52. return 0, err
  53. }
  54. n := (uint64(b[0]) << 56) |
  55. (uint64(b[1]) << 48) |
  56. (uint64(b[2]) << 40) |
  57. (uint64(b[3]) << 32) |
  58. (uint64(b[4]) << 24) |
  59. (uint64(b[5]) << 16) |
  60. (uint64(b[6]) << 8) |
  61. uint64(b[7])
  62. return n, nil
  63. }
  64. func (d *Decoder) int64() (int64, error) {
  65. n, err := d.uint64()
  66. return int64(n), err
  67. }
  68. func (d *Decoder) DecodeUint64() (uint64, error) {
  69. c, err := d.readCode()
  70. if err != nil {
  71. return 0, err
  72. }
  73. return d.uint(c)
  74. }
  75. func (d *Decoder) uint(c codes.Code) (uint64, error) {
  76. if c == codes.Nil {
  77. return 0, nil
  78. }
  79. if codes.IsFixedNum(c) {
  80. return uint64(int8(c)), nil
  81. }
  82. switch c {
  83. case codes.Uint8:
  84. n, err := d.uint8()
  85. return uint64(n), err
  86. case codes.Int8:
  87. n, err := d.int8()
  88. return uint64(n), err
  89. case codes.Uint16:
  90. n, err := d.uint16()
  91. return uint64(n), err
  92. case codes.Int16:
  93. n, err := d.int16()
  94. return uint64(n), err
  95. case codes.Uint32:
  96. n, err := d.uint32()
  97. return uint64(n), err
  98. case codes.Int32:
  99. n, err := d.int32()
  100. return uint64(n), err
  101. case codes.Uint64, codes.Int64:
  102. return d.uint64()
  103. }
  104. return 0, fmt.Errorf("msgpack: invalid code=%x decoding uint64", c)
  105. }
  106. func (d *Decoder) DecodeInt64() (int64, error) {
  107. c, err := d.readCode()
  108. if err != nil {
  109. return 0, err
  110. }
  111. return d.int(c)
  112. }
  113. func (d *Decoder) int(c codes.Code) (int64, error) {
  114. if c == codes.Nil {
  115. return 0, nil
  116. }
  117. if codes.IsFixedNum(c) {
  118. return int64(int8(c)), nil
  119. }
  120. switch c {
  121. case codes.Uint8:
  122. n, err := d.uint8()
  123. return int64(n), err
  124. case codes.Int8:
  125. n, err := d.uint8()
  126. return int64(int8(n)), err
  127. case codes.Uint16:
  128. n, err := d.uint16()
  129. return int64(n), err
  130. case codes.Int16:
  131. n, err := d.uint16()
  132. return int64(int16(n)), err
  133. case codes.Uint32:
  134. n, err := d.uint32()
  135. return int64(n), err
  136. case codes.Int32:
  137. n, err := d.uint32()
  138. return int64(int32(n)), err
  139. case codes.Uint64, codes.Int64:
  140. n, err := d.uint64()
  141. return int64(n), err
  142. }
  143. return 0, fmt.Errorf("msgpack: invalid code=%x decoding int64", c)
  144. }
  145. func (d *Decoder) DecodeFloat32() (float32, error) {
  146. c, err := d.readCode()
  147. if err != nil {
  148. return 0, err
  149. }
  150. return d.float32(c)
  151. }
  152. func (d *Decoder) float32(c codes.Code) (float32, error) {
  153. if c == codes.Float {
  154. n, err := d.uint32()
  155. if err != nil {
  156. return 0, err
  157. }
  158. return math.Float32frombits(n), nil
  159. }
  160. n, err := d.int(c)
  161. if err != nil {
  162. return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
  163. }
  164. return float32(n), nil
  165. }
  166. func (d *Decoder) DecodeFloat64() (float64, error) {
  167. c, err := d.readCode()
  168. if err != nil {
  169. return 0, err
  170. }
  171. return d.float64(c)
  172. }
  173. func (d *Decoder) float64(c codes.Code) (float64, error) {
  174. switch c {
  175. case codes.Float:
  176. n, err := d.float32(c)
  177. if err != nil {
  178. return 0, err
  179. }
  180. return float64(n), nil
  181. case codes.Double:
  182. n, err := d.uint64()
  183. if err != nil {
  184. return 0, err
  185. }
  186. return math.Float64frombits(n), nil
  187. }
  188. n, err := d.int(c)
  189. if err != nil {
  190. return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
  191. }
  192. return float64(n), nil
  193. }
  194. func (d *Decoder) DecodeUint() (uint, error) {
  195. n, err := d.DecodeUint64()
  196. return uint(n), err
  197. }
  198. func (d *Decoder) DecodeUint8() (uint8, error) {
  199. n, err := d.DecodeUint64()
  200. return uint8(n), err
  201. }
  202. func (d *Decoder) DecodeUint16() (uint16, error) {
  203. n, err := d.DecodeUint64()
  204. return uint16(n), err
  205. }
  206. func (d *Decoder) DecodeUint32() (uint32, error) {
  207. n, err := d.DecodeUint64()
  208. return uint32(n), err
  209. }
  210. func (d *Decoder) DecodeInt() (int, error) {
  211. n, err := d.DecodeInt64()
  212. return int(n), err
  213. }
  214. func (d *Decoder) DecodeInt8() (int8, error) {
  215. n, err := d.DecodeInt64()
  216. return int8(n), err
  217. }
  218. func (d *Decoder) DecodeInt16() (int16, error) {
  219. n, err := d.DecodeInt64()
  220. return int16(n), err
  221. }
  222. func (d *Decoder) DecodeInt32() (int32, error) {
  223. n, err := d.DecodeInt64()
  224. return int32(n), err
  225. }
  226. func decodeFloat32Value(d *Decoder, v reflect.Value) error {
  227. f, err := d.DecodeFloat32()
  228. if err != nil {
  229. return err
  230. }
  231. if err = mustSet(v); err != nil {
  232. return err
  233. }
  234. v.SetFloat(float64(f))
  235. return nil
  236. }
  237. func decodeFloat64Value(d *Decoder, v reflect.Value) error {
  238. f, err := d.DecodeFloat64()
  239. if err != nil {
  240. return err
  241. }
  242. if err = mustSet(v); err != nil {
  243. return err
  244. }
  245. v.SetFloat(f)
  246. return nil
  247. }
  248. func decodeInt64Value(d *Decoder, v reflect.Value) error {
  249. n, err := d.DecodeInt64()
  250. if err != nil {
  251. return err
  252. }
  253. if err = mustSet(v); err != nil {
  254. return err
  255. }
  256. v.SetInt(n)
  257. return nil
  258. }
  259. func decodeUint64Value(d *Decoder, v reflect.Value) error {
  260. n, err := d.DecodeUint64()
  261. if err != nil {
  262. return err
  263. }
  264. if err = mustSet(v); err != nil {
  265. return err
  266. }
  267. v.SetUint(n)
  268. return nil
  269. }