decode_number.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package msgpack
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "github.com/vmihailenco/msgpack/v5/msgpcode"
  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 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. // DecodeUint64 decodes msgpack int8/16/32/64 and uint8/16/32/64
  69. // into Go uint64.
  70. func (d *Decoder) DecodeUint64() (uint64, error) {
  71. c, err := d.readCode()
  72. if err != nil {
  73. return 0, err
  74. }
  75. return d.uint(c)
  76. }
  77. func (d *Decoder) uint(c byte) (uint64, error) {
  78. if c == msgpcode.Nil {
  79. return 0, nil
  80. }
  81. if msgpcode.IsFixedNum(c) {
  82. return uint64(int8(c)), nil
  83. }
  84. switch c {
  85. case msgpcode.Uint8:
  86. n, err := d.uint8()
  87. return uint64(n), err
  88. case msgpcode.Int8:
  89. n, err := d.int8()
  90. return uint64(n), err
  91. case msgpcode.Uint16:
  92. n, err := d.uint16()
  93. return uint64(n), err
  94. case msgpcode.Int16:
  95. n, err := d.int16()
  96. return uint64(n), err
  97. case msgpcode.Uint32:
  98. n, err := d.uint32()
  99. return uint64(n), err
  100. case msgpcode.Int32:
  101. n, err := d.int32()
  102. return uint64(n), err
  103. case msgpcode.Uint64, msgpcode.Int64:
  104. return d.uint64()
  105. }
  106. return 0, fmt.Errorf("msgpack: invalid code=%x decoding uint64", c)
  107. }
  108. // DecodeInt64 decodes msgpack int8/16/32/64 and uint8/16/32/64
  109. // into Go int64.
  110. func (d *Decoder) DecodeInt64() (int64, error) {
  111. c, err := d.readCode()
  112. if err != nil {
  113. return 0, err
  114. }
  115. return d.int(c)
  116. }
  117. func (d *Decoder) int(c byte) (int64, error) {
  118. if c == msgpcode.Nil {
  119. return 0, nil
  120. }
  121. if msgpcode.IsFixedNum(c) {
  122. return int64(int8(c)), nil
  123. }
  124. switch c {
  125. case msgpcode.Uint8:
  126. n, err := d.uint8()
  127. return int64(n), err
  128. case msgpcode.Int8:
  129. n, err := d.uint8()
  130. return int64(int8(n)), err
  131. case msgpcode.Uint16:
  132. n, err := d.uint16()
  133. return int64(n), err
  134. case msgpcode.Int16:
  135. n, err := d.uint16()
  136. return int64(int16(n)), err
  137. case msgpcode.Uint32:
  138. n, err := d.uint32()
  139. return int64(n), err
  140. case msgpcode.Int32:
  141. n, err := d.uint32()
  142. return int64(int32(n)), err
  143. case msgpcode.Uint64, msgpcode.Int64:
  144. n, err := d.uint64()
  145. return int64(n), err
  146. }
  147. return 0, fmt.Errorf("msgpack: invalid code=%x decoding int64", c)
  148. }
  149. func (d *Decoder) DecodeFloat32() (float32, error) {
  150. c, err := d.readCode()
  151. if err != nil {
  152. return 0, err
  153. }
  154. return d.float32(c)
  155. }
  156. func (d *Decoder) float32(c byte) (float32, error) {
  157. if c == msgpcode.Float {
  158. n, err := d.uint32()
  159. if err != nil {
  160. return 0, err
  161. }
  162. return math.Float32frombits(n), nil
  163. }
  164. n, err := d.int(c)
  165. if err != nil {
  166. return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
  167. }
  168. return float32(n), nil
  169. }
  170. // DecodeFloat64 decodes msgpack float32/64 into Go float64.
  171. func (d *Decoder) DecodeFloat64() (float64, error) {
  172. c, err := d.readCode()
  173. if err != nil {
  174. return 0, err
  175. }
  176. return d.float64(c)
  177. }
  178. func (d *Decoder) float64(c byte) (float64, error) {
  179. switch c {
  180. case msgpcode.Float:
  181. n, err := d.float32(c)
  182. if err != nil {
  183. return 0, err
  184. }
  185. return float64(n), nil
  186. case msgpcode.Double:
  187. n, err := d.uint64()
  188. if err != nil {
  189. return 0, err
  190. }
  191. return math.Float64frombits(n), nil
  192. }
  193. n, err := d.int(c)
  194. if err != nil {
  195. return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c)
  196. }
  197. return float64(n), nil
  198. }
  199. func (d *Decoder) DecodeUint() (uint, error) {
  200. n, err := d.DecodeUint64()
  201. return uint(n), err
  202. }
  203. func (d *Decoder) DecodeUint8() (uint8, error) {
  204. n, err := d.DecodeUint64()
  205. return uint8(n), err
  206. }
  207. func (d *Decoder) DecodeUint16() (uint16, error) {
  208. n, err := d.DecodeUint64()
  209. return uint16(n), err
  210. }
  211. func (d *Decoder) DecodeUint32() (uint32, error) {
  212. n, err := d.DecodeUint64()
  213. return uint32(n), err
  214. }
  215. func (d *Decoder) DecodeInt() (int, error) {
  216. n, err := d.DecodeInt64()
  217. return int(n), err
  218. }
  219. func (d *Decoder) DecodeInt8() (int8, error) {
  220. n, err := d.DecodeInt64()
  221. return int8(n), err
  222. }
  223. func (d *Decoder) DecodeInt16() (int16, error) {
  224. n, err := d.DecodeInt64()
  225. return int16(n), err
  226. }
  227. func (d *Decoder) DecodeInt32() (int32, error) {
  228. n, err := d.DecodeInt64()
  229. return int32(n), err
  230. }
  231. func decodeFloat32Value(d *Decoder, v reflect.Value) error {
  232. f, err := d.DecodeFloat32()
  233. if err != nil {
  234. return err
  235. }
  236. v.SetFloat(float64(f))
  237. return nil
  238. }
  239. func decodeFloat64Value(d *Decoder, v reflect.Value) error {
  240. f, err := d.DecodeFloat64()
  241. if err != nil {
  242. return err
  243. }
  244. v.SetFloat(f)
  245. return nil
  246. }
  247. func decodeInt64Value(d *Decoder, v reflect.Value) error {
  248. n, err := d.DecodeInt64()
  249. if err != nil {
  250. return err
  251. }
  252. v.SetInt(n)
  253. return nil
  254. }
  255. func decodeUint64Value(d *Decoder, v reflect.Value) error {
  256. n, err := d.DecodeUint64()
  257. if err != nil {
  258. return err
  259. }
  260. v.SetUint(n)
  261. return nil
  262. }