read_bytes.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. package msgp
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "math"
  6. "time"
  7. )
  8. var big = binary.BigEndian
  9. // NextType returns the type of the next
  10. // object in the slice. If the length
  11. // of the input is zero, it returns
  12. // [InvalidType].
  13. func NextType(b []byte) Type {
  14. if len(b) == 0 {
  15. return InvalidType
  16. }
  17. spec := getBytespec(b[0])
  18. t := spec.typ
  19. if t == ExtensionType && len(b) > int(spec.size) {
  20. var tp int8
  21. if spec.extra == constsize {
  22. tp = int8(b[1])
  23. } else {
  24. tp = int8(b[spec.size-1])
  25. }
  26. switch tp {
  27. case TimeExtension:
  28. return TimeType
  29. case Complex128Extension:
  30. return Complex128Type
  31. case Complex64Extension:
  32. return Complex64Type
  33. default:
  34. return ExtensionType
  35. }
  36. }
  37. return t
  38. }
  39. // IsNil returns true if len(b)>0 and
  40. // the leading byte is a 'nil' MessagePack
  41. // byte; false otherwise
  42. func IsNil(b []byte) bool {
  43. if len(b) != 0 && b[0] == mnil {
  44. return true
  45. }
  46. return false
  47. }
  48. // Raw is raw MessagePack.
  49. // Raw allows you to read and write
  50. // data without interpreting its contents.
  51. type Raw []byte
  52. // MarshalMsg implements [Marshaler].
  53. // It appends the raw contents of 'raw'
  54. // to the provided byte slice. If 'raw'
  55. // is 0 bytes, 'nil' will be appended instead.
  56. func (r Raw) MarshalMsg(b []byte) ([]byte, error) {
  57. i := len(r)
  58. if i == 0 {
  59. return AppendNil(b), nil
  60. }
  61. o, l := ensure(b, i)
  62. copy(o[l:], []byte(r))
  63. return o, nil
  64. }
  65. // UnmarshalMsg implements [Unmarshaler].
  66. // It sets the contents of *Raw to be the next
  67. // object in the provided byte slice.
  68. func (r *Raw) UnmarshalMsg(b []byte) ([]byte, error) {
  69. l := len(b)
  70. out, err := Skip(b)
  71. if err != nil {
  72. return b, err
  73. }
  74. rlen := l - len(out)
  75. if IsNil(b[:rlen]) {
  76. rlen = 0
  77. }
  78. if cap(*r) < rlen {
  79. *r = make(Raw, rlen)
  80. } else {
  81. *r = (*r)[0:rlen]
  82. }
  83. copy(*r, b[:rlen])
  84. return out, nil
  85. }
  86. // EncodeMsg implements [Encodable].
  87. // It writes the raw bytes to the writer.
  88. // If r is empty, it writes 'nil' instead.
  89. func (r Raw) EncodeMsg(w *Writer) error {
  90. if len(r) == 0 {
  91. return w.WriteNil()
  92. }
  93. _, err := w.Write([]byte(r))
  94. return err
  95. }
  96. // DecodeMsg implements [Decodable].
  97. // It sets the value of *Raw to be the
  98. // next object on the wire.
  99. func (r *Raw) DecodeMsg(f *Reader) error {
  100. *r = (*r)[:0]
  101. err := appendNext(f, (*[]byte)(r))
  102. if IsNil(*r) {
  103. *r = (*r)[:0]
  104. }
  105. return err
  106. }
  107. // Msgsize implements [Sizer].
  108. func (r Raw) Msgsize() int {
  109. l := len(r)
  110. if l == 0 {
  111. return 1 // for 'nil'
  112. }
  113. return l
  114. }
  115. func appendNext(f *Reader, d *[]byte) error {
  116. amt, o, err := getNextSize(f.R)
  117. if err != nil {
  118. return err
  119. }
  120. var i int
  121. *d, i = ensure(*d, int(amt))
  122. _, err = f.R.ReadFull((*d)[i:])
  123. if err != nil {
  124. return err
  125. }
  126. for o > 0 {
  127. err = appendNext(f, d)
  128. if err != nil {
  129. return err
  130. }
  131. o--
  132. }
  133. return nil
  134. }
  135. // MarshalJSON implements [json.Marshaler].
  136. func (r *Raw) MarshalJSON() ([]byte, error) {
  137. var buf bytes.Buffer
  138. _, err := UnmarshalAsJSON(&buf, []byte(*r))
  139. return buf.Bytes(), err
  140. }
  141. // ReadMapHeaderBytes reads a map header size
  142. // from 'b' and returns the remaining bytes.
  143. //
  144. // Possible errors:
  145. //
  146. // - [ErrShortBytes] (too few bytes)
  147. // - [TypeError] (not a map)
  148. func ReadMapHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
  149. l := len(b)
  150. if l < 1 {
  151. err = ErrShortBytes
  152. return
  153. }
  154. lead := b[0]
  155. if isfixmap(lead) {
  156. sz = uint32(rfixmap(lead))
  157. o = b[1:]
  158. return
  159. }
  160. switch lead {
  161. case mmap16:
  162. if l < 3 {
  163. err = ErrShortBytes
  164. return
  165. }
  166. sz = uint32(big.Uint16(b[1:]))
  167. o = b[3:]
  168. return
  169. case mmap32:
  170. if l < 5 {
  171. err = ErrShortBytes
  172. return
  173. }
  174. sz = big.Uint32(b[1:])
  175. o = b[5:]
  176. return
  177. default:
  178. err = badPrefix(MapType, lead)
  179. return
  180. }
  181. }
  182. // ReadMapKeyZC attempts to read a map key
  183. // from 'b' and returns the key bytes and the remaining bytes
  184. //
  185. // Possible errors:
  186. //
  187. // - [ErrShortBytes] (too few bytes)
  188. // - [TypeError] (not a str or bin)
  189. func ReadMapKeyZC(b []byte) ([]byte, []byte, error) {
  190. o, x, err := ReadStringZC(b)
  191. if err != nil {
  192. if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
  193. return ReadBytesZC(b)
  194. }
  195. return nil, b, err
  196. }
  197. return o, x, nil
  198. }
  199. // ReadArrayHeaderBytes attempts to read
  200. // the array header size off of 'b' and return
  201. // the size and remaining bytes.
  202. //
  203. // Possible errors:
  204. //
  205. // - [ErrShortBytes] (too few bytes)
  206. // - [TypeError] (not an array)
  207. func ReadArrayHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
  208. if len(b) < 1 {
  209. return 0, nil, ErrShortBytes
  210. }
  211. lead := b[0]
  212. if isfixarray(lead) {
  213. sz = uint32(rfixarray(lead))
  214. o = b[1:]
  215. return
  216. }
  217. switch lead {
  218. case marray16:
  219. if len(b) < 3 {
  220. err = ErrShortBytes
  221. return
  222. }
  223. sz = uint32(big.Uint16(b[1:]))
  224. o = b[3:]
  225. return
  226. case marray32:
  227. if len(b) < 5 {
  228. err = ErrShortBytes
  229. return
  230. }
  231. sz = big.Uint32(b[1:])
  232. o = b[5:]
  233. return
  234. default:
  235. err = badPrefix(ArrayType, lead)
  236. return
  237. }
  238. }
  239. // ReadBytesHeader reads the 'bin' header size
  240. // off of 'b' and returns the size and remaining bytes.
  241. //
  242. // Possible errors:
  243. //
  244. // - [ErrShortBytes] (too few bytes)
  245. // - [TypeError] (not a bin object)
  246. func ReadBytesHeader(b []byte) (sz uint32, o []byte, err error) {
  247. if len(b) < 1 {
  248. return 0, nil, ErrShortBytes
  249. }
  250. switch b[0] {
  251. case mbin8:
  252. if len(b) < 2 {
  253. err = ErrShortBytes
  254. return
  255. }
  256. sz = uint32(b[1])
  257. o = b[2:]
  258. return
  259. case mbin16:
  260. if len(b) < 3 {
  261. err = ErrShortBytes
  262. return
  263. }
  264. sz = uint32(big.Uint16(b[1:]))
  265. o = b[3:]
  266. return
  267. case mbin32:
  268. if len(b) < 5 {
  269. err = ErrShortBytes
  270. return
  271. }
  272. sz = big.Uint32(b[1:])
  273. o = b[5:]
  274. return
  275. default:
  276. err = badPrefix(BinType, b[0])
  277. return
  278. }
  279. }
  280. // ReadNilBytes tries to read a "nil" byte
  281. // off of 'b' and return the remaining bytes.
  282. //
  283. // Possible errors:
  284. //
  285. // - [ErrShortBytes] (too few bytes)
  286. // - [TypeError] (not a 'nil')
  287. // - [InvalidPrefixError]
  288. func ReadNilBytes(b []byte) ([]byte, error) {
  289. if len(b) < 1 {
  290. return nil, ErrShortBytes
  291. }
  292. if b[0] != mnil {
  293. return b, badPrefix(NilType, b[0])
  294. }
  295. return b[1:], nil
  296. }
  297. // ReadFloat64Bytes tries to read a float64
  298. // from 'b' and return the value and the remaining bytes.
  299. //
  300. // Possible errors:
  301. //
  302. // - [ErrShortBytes] (too few bytes)
  303. // - [TypeError] (not a float64)
  304. func ReadFloat64Bytes(b []byte) (f float64, o []byte, err error) {
  305. if len(b) < 9 {
  306. if len(b) >= 5 && b[0] == mfloat32 {
  307. var tf float32
  308. tf, o, err = ReadFloat32Bytes(b)
  309. f = float64(tf)
  310. return
  311. }
  312. err = ErrShortBytes
  313. return
  314. }
  315. if b[0] != mfloat64 {
  316. if b[0] == mfloat32 {
  317. var tf float32
  318. tf, o, err = ReadFloat32Bytes(b)
  319. f = float64(tf)
  320. return
  321. }
  322. err = badPrefix(Float64Type, b[0])
  323. return
  324. }
  325. f = math.Float64frombits(getMuint64(b))
  326. o = b[9:]
  327. return
  328. }
  329. // ReadFloat32Bytes tries to read a float64
  330. // from 'b' and return the value and the remaining bytes.
  331. //
  332. // Possible errors:
  333. //
  334. // - [ErrShortBytes] (too few bytes)
  335. // - [TypeError] (not a float32)
  336. func ReadFloat32Bytes(b []byte) (f float32, o []byte, err error) {
  337. if len(b) < 5 {
  338. err = ErrShortBytes
  339. return
  340. }
  341. if b[0] != mfloat32 {
  342. err = TypeError{Method: Float32Type, Encoded: getType(b[0])}
  343. return
  344. }
  345. f = math.Float32frombits(getMuint32(b))
  346. o = b[5:]
  347. return
  348. }
  349. // ReadBoolBytes tries to read a float64
  350. // from 'b' and return the value and the remaining bytes.
  351. //
  352. // Possible errors:
  353. //
  354. // - [ErrShortBytes] (too few bytes)
  355. // - [TypeError] (not a bool)
  356. func ReadBoolBytes(b []byte) (bool, []byte, error) {
  357. if len(b) < 1 {
  358. return false, b, ErrShortBytes
  359. }
  360. switch b[0] {
  361. case mtrue:
  362. return true, b[1:], nil
  363. case mfalse:
  364. return false, b[1:], nil
  365. default:
  366. return false, b, badPrefix(BoolType, b[0])
  367. }
  368. }
  369. // ReadDurationBytes tries to read a time.Duration
  370. // from 'b' and return the value and the remaining bytes.
  371. //
  372. // Possible errors:
  373. //
  374. // - [ErrShortBytes] (too few bytes)
  375. // - TypeError (not a int)
  376. func ReadDurationBytes(b []byte) (d time.Duration, o []byte, err error) {
  377. i, o, err := ReadInt64Bytes(b)
  378. return time.Duration(i), o, err
  379. }
  380. // ReadInt64Bytes tries to read an int64
  381. // from 'b' and return the value and the remaining bytes.
  382. //
  383. // Possible errors:
  384. //
  385. // - [ErrShortBytes] (too few bytes)
  386. // - [TypeError] (not a int)
  387. func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
  388. l := len(b)
  389. if l < 1 {
  390. return 0, nil, ErrShortBytes
  391. }
  392. lead := b[0]
  393. if isfixint(lead) {
  394. i = int64(rfixint(lead))
  395. o = b[1:]
  396. return
  397. }
  398. if isnfixint(lead) {
  399. i = int64(rnfixint(lead))
  400. o = b[1:]
  401. return
  402. }
  403. switch lead {
  404. case mint8:
  405. if l < 2 {
  406. err = ErrShortBytes
  407. return
  408. }
  409. i = int64(getMint8(b))
  410. o = b[2:]
  411. return
  412. case muint8:
  413. if l < 2 {
  414. err = ErrShortBytes
  415. return
  416. }
  417. i = int64(getMuint8(b))
  418. o = b[2:]
  419. return
  420. case mint16:
  421. if l < 3 {
  422. err = ErrShortBytes
  423. return
  424. }
  425. i = int64(getMint16(b))
  426. o = b[3:]
  427. return
  428. case muint16:
  429. if l < 3 {
  430. err = ErrShortBytes
  431. return
  432. }
  433. i = int64(getMuint16(b))
  434. o = b[3:]
  435. return
  436. case mint32:
  437. if l < 5 {
  438. err = ErrShortBytes
  439. return
  440. }
  441. i = int64(getMint32(b))
  442. o = b[5:]
  443. return
  444. case muint32:
  445. if l < 5 {
  446. err = ErrShortBytes
  447. return
  448. }
  449. i = int64(getMuint32(b))
  450. o = b[5:]
  451. return
  452. case mint64:
  453. if l < 9 {
  454. err = ErrShortBytes
  455. return
  456. }
  457. i = int64(getMint64(b))
  458. o = b[9:]
  459. return
  460. case muint64:
  461. if l < 9 {
  462. err = ErrShortBytes
  463. return
  464. }
  465. u := getMuint64(b)
  466. if u > math.MaxInt64 {
  467. err = UintOverflow{Value: u, FailedBitsize: 64}
  468. return
  469. }
  470. i = int64(u)
  471. o = b[9:]
  472. return
  473. default:
  474. err = badPrefix(IntType, lead)
  475. return
  476. }
  477. }
  478. // ReadInt32Bytes tries to read an int32
  479. // from 'b' and return the value and the remaining bytes.
  480. //
  481. // Possible errors:
  482. //
  483. // - [ErrShortBytes] (too few bytes)
  484. // - [TypeError] (not a int)
  485. // - [IntOverflow] (value doesn't fit in int32)
  486. func ReadInt32Bytes(b []byte) (int32, []byte, error) {
  487. i, o, err := ReadInt64Bytes(b)
  488. if i > math.MaxInt32 || i < math.MinInt32 {
  489. return 0, o, IntOverflow{Value: i, FailedBitsize: 32}
  490. }
  491. return int32(i), o, err
  492. }
  493. // ReadInt16Bytes tries to read an int16
  494. // from 'b' and return the value and the remaining bytes.
  495. //
  496. // Possible errors:
  497. //
  498. // - [ErrShortBytes] (too few bytes)
  499. // - [TypeError] (not a int)
  500. // - [IntOverflow] (value doesn't fit in int16)
  501. func ReadInt16Bytes(b []byte) (int16, []byte, error) {
  502. i, o, err := ReadInt64Bytes(b)
  503. if i > math.MaxInt16 || i < math.MinInt16 {
  504. return 0, o, IntOverflow{Value: i, FailedBitsize: 16}
  505. }
  506. return int16(i), o, err
  507. }
  508. // ReadInt8Bytes tries to read an int16
  509. // from 'b' and return the value and the remaining bytes.
  510. //
  511. // Possible errors:
  512. //
  513. // - [ErrShortBytes] (too few bytes)
  514. // - [TypeError] (not a int)
  515. // - [IntOverflow] (value doesn't fit in int8)
  516. func ReadInt8Bytes(b []byte) (int8, []byte, error) {
  517. i, o, err := ReadInt64Bytes(b)
  518. if i > math.MaxInt8 || i < math.MinInt8 {
  519. return 0, o, IntOverflow{Value: i, FailedBitsize: 8}
  520. }
  521. return int8(i), o, err
  522. }
  523. // ReadIntBytes tries to read an int
  524. // from 'b' and return the value and the remaining bytes.
  525. //
  526. // Possible errors:
  527. //
  528. // - [ErrShortBytes] (too few bytes)
  529. // - [TypeError] (not a int)
  530. // - [IntOverflow] (value doesn't fit in int; 32-bit platforms only)
  531. func ReadIntBytes(b []byte) (int, []byte, error) {
  532. if smallint {
  533. i, b, err := ReadInt32Bytes(b)
  534. return int(i), b, err
  535. }
  536. i, b, err := ReadInt64Bytes(b)
  537. return int(i), b, err
  538. }
  539. // ReadUint64Bytes tries to read a uint64
  540. // from 'b' and return the value and the remaining bytes.
  541. //
  542. // Possible errors:
  543. //
  544. // - [ErrShortBytes] (too few bytes)
  545. // - [TypeError] (not a uint)
  546. func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
  547. l := len(b)
  548. if l < 1 {
  549. return 0, nil, ErrShortBytes
  550. }
  551. lead := b[0]
  552. if isfixint(lead) {
  553. u = uint64(rfixint(lead))
  554. o = b[1:]
  555. return
  556. }
  557. switch lead {
  558. case mint8:
  559. if l < 2 {
  560. err = ErrShortBytes
  561. return
  562. }
  563. v := int64(getMint8(b))
  564. if v < 0 {
  565. err = UintBelowZero{Value: v}
  566. return
  567. }
  568. u = uint64(v)
  569. o = b[2:]
  570. return
  571. case muint8:
  572. if l < 2 {
  573. err = ErrShortBytes
  574. return
  575. }
  576. u = uint64(getMuint8(b))
  577. o = b[2:]
  578. return
  579. case mint16:
  580. if l < 3 {
  581. err = ErrShortBytes
  582. return
  583. }
  584. v := int64(getMint16(b))
  585. if v < 0 {
  586. err = UintBelowZero{Value: v}
  587. return
  588. }
  589. u = uint64(v)
  590. o = b[3:]
  591. return
  592. case muint16:
  593. if l < 3 {
  594. err = ErrShortBytes
  595. return
  596. }
  597. u = uint64(getMuint16(b))
  598. o = b[3:]
  599. return
  600. case mint32:
  601. if l < 5 {
  602. err = ErrShortBytes
  603. return
  604. }
  605. v := int64(getMint32(b))
  606. if v < 0 {
  607. err = UintBelowZero{Value: v}
  608. return
  609. }
  610. u = uint64(v)
  611. o = b[5:]
  612. return
  613. case muint32:
  614. if l < 5 {
  615. err = ErrShortBytes
  616. return
  617. }
  618. u = uint64(getMuint32(b))
  619. o = b[5:]
  620. return
  621. case mint64:
  622. if l < 9 {
  623. err = ErrShortBytes
  624. return
  625. }
  626. v := int64(getMint64(b))
  627. if v < 0 {
  628. err = UintBelowZero{Value: v}
  629. return
  630. }
  631. u = uint64(v)
  632. o = b[9:]
  633. return
  634. case muint64:
  635. if l < 9 {
  636. err = ErrShortBytes
  637. return
  638. }
  639. u = getMuint64(b)
  640. o = b[9:]
  641. return
  642. default:
  643. if isnfixint(lead) {
  644. err = UintBelowZero{Value: int64(rnfixint(lead))}
  645. } else {
  646. err = badPrefix(UintType, lead)
  647. }
  648. return
  649. }
  650. }
  651. // ReadUint32Bytes tries to read a uint32
  652. // from 'b' and return the value and the remaining bytes.
  653. //
  654. // Possible errors:
  655. //
  656. // - [ErrShortBytes] (too few bytes)
  657. // - [TypeError] (not a uint)
  658. // - [UintOverflow] (value too large for uint32)
  659. func ReadUint32Bytes(b []byte) (uint32, []byte, error) {
  660. v, o, err := ReadUint64Bytes(b)
  661. if v > math.MaxUint32 {
  662. return 0, nil, UintOverflow{Value: v, FailedBitsize: 32}
  663. }
  664. return uint32(v), o, err
  665. }
  666. // ReadUint16Bytes tries to read a uint16
  667. // from 'b' and return the value and the remaining bytes.
  668. //
  669. // Possible errors:
  670. //
  671. // - [ErrShortBytes] (too few bytes)
  672. // - [TypeError] (not a uint)
  673. // - [UintOverflow] (value too large for uint16)
  674. func ReadUint16Bytes(b []byte) (uint16, []byte, error) {
  675. v, o, err := ReadUint64Bytes(b)
  676. if v > math.MaxUint16 {
  677. return 0, nil, UintOverflow{Value: v, FailedBitsize: 16}
  678. }
  679. return uint16(v), o, err
  680. }
  681. // ReadUint8Bytes tries to read a uint8
  682. // from 'b' and return the value and the remaining bytes.
  683. //
  684. // Possible errors:
  685. //
  686. // - [ErrShortBytes] (too few bytes)
  687. // - [TypeError] (not a uint)
  688. // - [UintOverflow] (value too large for uint8)
  689. func ReadUint8Bytes(b []byte) (uint8, []byte, error) {
  690. v, o, err := ReadUint64Bytes(b)
  691. if v > math.MaxUint8 {
  692. return 0, nil, UintOverflow{Value: v, FailedBitsize: 8}
  693. }
  694. return uint8(v), o, err
  695. }
  696. // ReadUintBytes tries to read a uint
  697. // from 'b' and return the value and the remaining bytes.
  698. //
  699. // Possible errors:
  700. //
  701. // - [ErrShortBytes] (too few bytes)
  702. // - [TypeError] (not a uint)
  703. // - [UintOverflow] (value too large for uint; 32-bit platforms only)
  704. func ReadUintBytes(b []byte) (uint, []byte, error) {
  705. if smallint {
  706. u, b, err := ReadUint32Bytes(b)
  707. return uint(u), b, err
  708. }
  709. u, b, err := ReadUint64Bytes(b)
  710. return uint(u), b, err
  711. }
  712. // ReadByteBytes is analogous to ReadUint8Bytes
  713. func ReadByteBytes(b []byte) (byte, []byte, error) {
  714. return ReadUint8Bytes(b)
  715. }
  716. // ReadBytesBytes reads a 'bin' object
  717. // from 'b' and returns its vaue and
  718. // the remaining bytes in 'b'.
  719. //
  720. // Possible errors:
  721. //
  722. // - [ErrShortBytes] (too few bytes)
  723. // - [TypeError] (not a 'bin' object)
  724. func ReadBytesBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
  725. return readBytesBytes(b, scratch, false)
  726. }
  727. func readBytesBytes(b []byte, scratch []byte, zc bool) (v []byte, o []byte, err error) {
  728. l := len(b)
  729. if l < 1 {
  730. return nil, nil, ErrShortBytes
  731. }
  732. lead := b[0]
  733. var read int
  734. switch lead {
  735. case mbin8:
  736. if l < 2 {
  737. err = ErrShortBytes
  738. return
  739. }
  740. read = int(b[1])
  741. b = b[2:]
  742. case mbin16:
  743. if l < 3 {
  744. err = ErrShortBytes
  745. return
  746. }
  747. read = int(big.Uint16(b[1:]))
  748. b = b[3:]
  749. case mbin32:
  750. if l < 5 {
  751. err = ErrShortBytes
  752. return
  753. }
  754. read = int(big.Uint32(b[1:]))
  755. b = b[5:]
  756. default:
  757. err = badPrefix(BinType, lead)
  758. return
  759. }
  760. if len(b) < read {
  761. err = ErrShortBytes
  762. return
  763. }
  764. // zero-copy
  765. if zc {
  766. v = b[0:read]
  767. o = b[read:]
  768. return
  769. }
  770. if cap(scratch) >= read {
  771. v = scratch[0:read]
  772. } else {
  773. v = make([]byte, read)
  774. }
  775. o = b[copy(v, b):]
  776. return
  777. }
  778. // ReadBytesZC extracts the messagepack-encoded
  779. // binary field without copying. The returned []byte
  780. // points to the same memory as the input slice.
  781. //
  782. // Possible errors:
  783. //
  784. // - [ErrShortBytes] (b not long enough)
  785. // - [TypeError] (object not 'bin')
  786. func ReadBytesZC(b []byte) (v []byte, o []byte, err error) {
  787. return readBytesBytes(b, nil, true)
  788. }
  789. func ReadExactBytes(b []byte, into []byte) (o []byte, err error) {
  790. l := len(b)
  791. if l < 1 {
  792. err = ErrShortBytes
  793. return
  794. }
  795. lead := b[0]
  796. var read uint32
  797. var skip int
  798. switch lead {
  799. case mbin8:
  800. if l < 2 {
  801. err = ErrShortBytes
  802. return
  803. }
  804. read = uint32(b[1])
  805. skip = 2
  806. case mbin16:
  807. if l < 3 {
  808. err = ErrShortBytes
  809. return
  810. }
  811. read = uint32(big.Uint16(b[1:]))
  812. skip = 3
  813. case mbin32:
  814. if l < 5 {
  815. err = ErrShortBytes
  816. return
  817. }
  818. read = uint32(big.Uint32(b[1:]))
  819. skip = 5
  820. default:
  821. err = badPrefix(BinType, lead)
  822. return
  823. }
  824. if read != uint32(len(into)) {
  825. err = ArrayError{Wanted: uint32(len(into)), Got: read}
  826. return
  827. }
  828. o = b[skip+copy(into, b[skip:]):]
  829. return
  830. }
  831. // ReadStringZC reads a messagepack string field
  832. // without copying. The returned []byte points
  833. // to the same memory as the input slice.
  834. //
  835. // Possible errors:
  836. //
  837. // - [ErrShortBytes] (b not long enough)
  838. // - [TypeError] (object not 'str')
  839. func ReadStringZC(b []byte) (v []byte, o []byte, err error) {
  840. l := len(b)
  841. if l < 1 {
  842. return nil, nil, ErrShortBytes
  843. }
  844. lead := b[0]
  845. var read int
  846. if isfixstr(lead) {
  847. read = int(rfixstr(lead))
  848. b = b[1:]
  849. } else {
  850. switch lead {
  851. case mstr8:
  852. if l < 2 {
  853. err = ErrShortBytes
  854. return
  855. }
  856. read = int(b[1])
  857. b = b[2:]
  858. case mstr16:
  859. if l < 3 {
  860. err = ErrShortBytes
  861. return
  862. }
  863. read = int(big.Uint16(b[1:]))
  864. b = b[3:]
  865. case mstr32:
  866. if l < 5 {
  867. err = ErrShortBytes
  868. return
  869. }
  870. read = int(big.Uint32(b[1:]))
  871. b = b[5:]
  872. default:
  873. err = TypeError{Method: StrType, Encoded: getType(lead)}
  874. return
  875. }
  876. }
  877. if len(b) < read {
  878. err = ErrShortBytes
  879. return
  880. }
  881. v = b[0:read]
  882. o = b[read:]
  883. return
  884. }
  885. // ReadStringBytes reads a 'str' object
  886. // from 'b' and returns its value and the
  887. // remaining bytes in 'b'.
  888. //
  889. // Possible errors:
  890. //
  891. // - [ErrShortBytes] (b not long enough)
  892. // - [TypeError] (not 'str' type)
  893. // - [InvalidPrefixError]
  894. func ReadStringBytes(b []byte) (string, []byte, error) {
  895. v, o, err := ReadStringZC(b)
  896. return string(v), o, err
  897. }
  898. // ReadStringAsBytes reads a 'str' object
  899. // into a slice of bytes. 'v' is the value of
  900. // the 'str' object, which may reside in memory
  901. // pointed to by 'scratch.' 'o' is the remaining bytes
  902. // in 'b'.
  903. //
  904. // Possible errors:
  905. //
  906. // - [ErrShortBytes] (b not long enough)
  907. // - [TypeError] (not 'str' type)
  908. // - [InvalidPrefixError] (unknown type marker)
  909. func ReadStringAsBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
  910. var tmp []byte
  911. tmp, o, err = ReadStringZC(b)
  912. v = append(scratch[:0], tmp...)
  913. return
  914. }
  915. // ReadComplex128Bytes reads a complex128
  916. // extension object from 'b' and returns the
  917. // remaining bytes.
  918. //
  919. // Possible errors:
  920. //
  921. // - [ErrShortBytes] (not enough bytes in 'b')
  922. // - [TypeError] (object not a complex128)
  923. // - [InvalidPrefixError]
  924. // - [ExtensionTypeError] (object an extension of the correct size, but not a complex128)
  925. func ReadComplex128Bytes(b []byte) (c complex128, o []byte, err error) {
  926. if len(b) < 18 {
  927. err = ErrShortBytes
  928. return
  929. }
  930. if b[0] != mfixext16 {
  931. err = badPrefix(Complex128Type, b[0])
  932. return
  933. }
  934. if int8(b[1]) != Complex128Extension {
  935. err = errExt(int8(b[1]), Complex128Extension)
  936. return
  937. }
  938. c = complex(math.Float64frombits(big.Uint64(b[2:])),
  939. math.Float64frombits(big.Uint64(b[10:])))
  940. o = b[18:]
  941. return
  942. }
  943. // ReadComplex64Bytes reads a complex64
  944. // extension object from 'b' and returns the
  945. // remaining bytes.
  946. //
  947. // Possible errors:
  948. //
  949. // - [ErrShortBytes] (not enough bytes in 'b')
  950. // - [TypeError] (object not a complex64)
  951. // - [ExtensionTypeError] (object an extension of the correct size, but not a complex64)
  952. func ReadComplex64Bytes(b []byte) (c complex64, o []byte, err error) {
  953. if len(b) < 10 {
  954. err = ErrShortBytes
  955. return
  956. }
  957. if b[0] != mfixext8 {
  958. err = badPrefix(Complex64Type, b[0])
  959. return
  960. }
  961. if b[1] != Complex64Extension {
  962. err = errExt(int8(b[1]), Complex64Extension)
  963. return
  964. }
  965. c = complex(math.Float32frombits(big.Uint32(b[2:])),
  966. math.Float32frombits(big.Uint32(b[6:])))
  967. o = b[10:]
  968. return
  969. }
  970. // ReadTimeBytes reads a time.Time
  971. // extension object from 'b' and returns the
  972. // remaining bytes.
  973. //
  974. // Possible errors:
  975. //
  976. // - [ErrShortBytes] (not enough bytes in 'b')
  977. // - [TypeError] (object not a complex64)
  978. // - [ExtensionTypeError] (object an extension of the correct size, but not a time.Time)
  979. func ReadTimeBytes(b []byte) (t time.Time, o []byte, err error) {
  980. if len(b) < 15 {
  981. err = ErrShortBytes
  982. return
  983. }
  984. if b[0] != mext8 || b[1] != 12 {
  985. err = badPrefix(TimeType, b[0])
  986. return
  987. }
  988. if int8(b[2]) != TimeExtension {
  989. err = errExt(int8(b[2]), TimeExtension)
  990. return
  991. }
  992. sec, nsec := getUnix(b[3:])
  993. t = time.Unix(sec, int64(nsec)).Local()
  994. o = b[15:]
  995. return
  996. }
  997. // ReadMapStrIntfBytes reads a map[string]interface{}
  998. // out of 'b' and returns the map and remaining bytes.
  999. // If 'old' is non-nil, the values will be read into that map.
  1000. func ReadMapStrIntfBytes(b []byte, old map[string]interface{}) (v map[string]interface{}, o []byte, err error) {
  1001. var sz uint32
  1002. o = b
  1003. sz, o, err = ReadMapHeaderBytes(o)
  1004. if err != nil {
  1005. return
  1006. }
  1007. if old != nil {
  1008. for key := range old {
  1009. delete(old, key)
  1010. }
  1011. v = old
  1012. } else {
  1013. v = make(map[string]interface{}, int(sz))
  1014. }
  1015. for z := uint32(0); z < sz; z++ {
  1016. if len(o) < 1 {
  1017. err = ErrShortBytes
  1018. return
  1019. }
  1020. var key []byte
  1021. key, o, err = ReadMapKeyZC(o)
  1022. if err != nil {
  1023. return
  1024. }
  1025. var val interface{}
  1026. val, o, err = ReadIntfBytes(o)
  1027. if err != nil {
  1028. return
  1029. }
  1030. v[string(key)] = val
  1031. }
  1032. return
  1033. }
  1034. // ReadIntfBytes attempts to read
  1035. // the next object out of 'b' as a raw interface{} and
  1036. // return the remaining bytes.
  1037. func ReadIntfBytes(b []byte) (i interface{}, o []byte, err error) {
  1038. if len(b) < 1 {
  1039. err = ErrShortBytes
  1040. return
  1041. }
  1042. k := NextType(b)
  1043. switch k {
  1044. case MapType:
  1045. i, o, err = ReadMapStrIntfBytes(b, nil)
  1046. return
  1047. case ArrayType:
  1048. var sz uint32
  1049. sz, o, err = ReadArrayHeaderBytes(b)
  1050. if err != nil {
  1051. return
  1052. }
  1053. j := make([]interface{}, int(sz))
  1054. i = j
  1055. for d := range j {
  1056. j[d], o, err = ReadIntfBytes(o)
  1057. if err != nil {
  1058. return
  1059. }
  1060. }
  1061. return
  1062. case Float32Type:
  1063. i, o, err = ReadFloat32Bytes(b)
  1064. return
  1065. case Float64Type:
  1066. i, o, err = ReadFloat64Bytes(b)
  1067. return
  1068. case IntType:
  1069. i, o, err = ReadInt64Bytes(b)
  1070. return
  1071. case UintType:
  1072. i, o, err = ReadUint64Bytes(b)
  1073. return
  1074. case BoolType:
  1075. i, o, err = ReadBoolBytes(b)
  1076. return
  1077. case TimeType:
  1078. i, o, err = ReadTimeBytes(b)
  1079. return
  1080. case Complex64Type:
  1081. i, o, err = ReadComplex64Bytes(b)
  1082. return
  1083. case Complex128Type:
  1084. i, o, err = ReadComplex128Bytes(b)
  1085. return
  1086. case ExtensionType:
  1087. var t int8
  1088. t, err = peekExtension(b)
  1089. if err != nil {
  1090. return
  1091. }
  1092. // use a user-defined extension,
  1093. // if it's been registered
  1094. f, ok := extensionReg[t]
  1095. if ok {
  1096. e := f()
  1097. o, err = ReadExtensionBytes(b, e)
  1098. i = e
  1099. return
  1100. }
  1101. // last resort is a raw extension
  1102. e := RawExtension{}
  1103. e.Type = int8(t)
  1104. o, err = ReadExtensionBytes(b, &e)
  1105. i = &e
  1106. return
  1107. case NilType:
  1108. o, err = ReadNilBytes(b)
  1109. return
  1110. case BinType:
  1111. i, o, err = ReadBytesBytes(b, nil)
  1112. return
  1113. case StrType:
  1114. i, o, err = ReadStringBytes(b)
  1115. return
  1116. default:
  1117. err = InvalidPrefixError(b[0])
  1118. return
  1119. }
  1120. }
  1121. // Skip skips the next object in 'b' and
  1122. // returns the remaining bytes. If the object
  1123. // is a map or array, all of its elements
  1124. // will be skipped.
  1125. //
  1126. // Possible errors:
  1127. //
  1128. // - [ErrShortBytes] (not enough bytes in b)
  1129. // - [InvalidPrefixError] (bad encoding)
  1130. func Skip(b []byte) ([]byte, error) {
  1131. sz, asz, err := getSize(b)
  1132. if err != nil {
  1133. return b, err
  1134. }
  1135. if uintptr(len(b)) < sz {
  1136. return b, ErrShortBytes
  1137. }
  1138. b = b[sz:]
  1139. for asz > 0 {
  1140. b, err = Skip(b)
  1141. if err != nil {
  1142. return b, err
  1143. }
  1144. asz--
  1145. }
  1146. return b, nil
  1147. }
  1148. // returns (skip N bytes, skip M objects, error)
  1149. func getSize(b []byte) (uintptr, uintptr, error) {
  1150. l := len(b)
  1151. if l == 0 {
  1152. return 0, 0, ErrShortBytes
  1153. }
  1154. lead := b[0]
  1155. spec := getBytespec(lead) // get type information
  1156. size, mode := spec.size, spec.extra
  1157. if size == 0 {
  1158. return 0, 0, InvalidPrefixError(lead)
  1159. }
  1160. if mode >= 0 { // fixed composites
  1161. return uintptr(size), uintptr(mode), nil
  1162. }
  1163. if l < int(size) {
  1164. return 0, 0, ErrShortBytes
  1165. }
  1166. switch mode {
  1167. case extra8:
  1168. return uintptr(size) + uintptr(b[1]), 0, nil
  1169. case extra16:
  1170. return uintptr(size) + uintptr(big.Uint16(b[1:])), 0, nil
  1171. case extra32:
  1172. return uintptr(size) + uintptr(big.Uint32(b[1:])), 0, nil
  1173. case map16v:
  1174. return uintptr(size), 2 * uintptr(big.Uint16(b[1:])), nil
  1175. case map32v:
  1176. return uintptr(size), 2 * uintptr(big.Uint32(b[1:])), nil
  1177. case array16v:
  1178. return uintptr(size), uintptr(big.Uint16(b[1:])), nil
  1179. case array32v:
  1180. return uintptr(size), uintptr(big.Uint32(b[1:])), nil
  1181. default:
  1182. return 0, 0, fatal
  1183. }
  1184. }