read_bytes.go 23 KB

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