read.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. package msgp
  2. import (
  3. "io"
  4. "math"
  5. "sync"
  6. "time"
  7. "github.com/philhofer/fwd"
  8. )
  9. // where we keep old *Readers
  10. var readerPool = sync.Pool{New: func() interface{} { return &Reader{} }}
  11. // Type is a MessagePack wire type,
  12. // including this package's built-in
  13. // extension types.
  14. type Type byte
  15. // MessagePack Types
  16. //
  17. // The zero value of Type
  18. // is InvalidType.
  19. const (
  20. InvalidType Type = iota
  21. // MessagePack built-in types
  22. StrType
  23. BinType
  24. MapType
  25. ArrayType
  26. Float64Type
  27. Float32Type
  28. BoolType
  29. IntType
  30. UintType
  31. NilType
  32. DurationType
  33. ExtensionType
  34. // pseudo-types provided
  35. // by extensions
  36. Complex64Type
  37. Complex128Type
  38. TimeType
  39. _maxtype
  40. )
  41. // String implements fmt.Stringer
  42. func (t Type) String() string {
  43. switch t {
  44. case StrType:
  45. return "str"
  46. case BinType:
  47. return "bin"
  48. case MapType:
  49. return "map"
  50. case ArrayType:
  51. return "array"
  52. case Float64Type:
  53. return "float64"
  54. case Float32Type:
  55. return "float32"
  56. case BoolType:
  57. return "bool"
  58. case UintType:
  59. return "uint"
  60. case IntType:
  61. return "int"
  62. case ExtensionType:
  63. return "ext"
  64. case NilType:
  65. return "nil"
  66. default:
  67. return "<invalid>"
  68. }
  69. }
  70. func freeR(m *Reader) {
  71. readerPool.Put(m)
  72. }
  73. // Unmarshaler is the interface fulfilled
  74. // by objects that know how to unmarshal
  75. // themselves from MessagePack.
  76. // UnmarshalMsg unmarshals the object
  77. // from binary, returing any leftover
  78. // bytes and any errors encountered.
  79. type Unmarshaler interface {
  80. UnmarshalMsg([]byte) ([]byte, error)
  81. }
  82. // Decodable is the interface fulfilled
  83. // by objects that know how to read
  84. // themselves from a *Reader.
  85. type Decodable interface {
  86. DecodeMsg(*Reader) error
  87. }
  88. // Decode decodes 'd' from 'r'.
  89. func Decode(r io.Reader, d Decodable) error {
  90. rd := NewReader(r)
  91. err := d.DecodeMsg(rd)
  92. freeR(rd)
  93. return err
  94. }
  95. // NewReader returns a *Reader that
  96. // reads from the provided reader. The
  97. // reader will be buffered.
  98. func NewReader(r io.Reader) *Reader {
  99. p := readerPool.Get().(*Reader)
  100. if p.R == nil {
  101. p.R = fwd.NewReader(r)
  102. } else {
  103. p.R.Reset(r)
  104. }
  105. return p
  106. }
  107. // NewReaderSize returns a *Reader with a buffer of the given size.
  108. // (This is vastly preferable to passing the decoder a reader that is already buffered.)
  109. func NewReaderSize(r io.Reader, sz int) *Reader {
  110. return &Reader{R: fwd.NewReaderSize(r, sz)}
  111. }
  112. // NewReaderBuf returns a *Reader with a provided buffer.
  113. func NewReaderBuf(r io.Reader, buf []byte) *Reader {
  114. return &Reader{R: fwd.NewReaderBuf(r, buf)}
  115. }
  116. // Reader wraps an io.Reader and provides
  117. // methods to read MessagePack-encoded values
  118. // from it. Readers are buffered.
  119. type Reader struct {
  120. // R is the buffered reader
  121. // that the Reader uses
  122. // to decode MessagePack.
  123. // The Reader itself
  124. // is stateless; all the
  125. // buffering is done
  126. // within R.
  127. R *fwd.Reader
  128. scratch []byte
  129. }
  130. // Read implements `io.Reader`
  131. func (m *Reader) Read(p []byte) (int, error) {
  132. return m.R.Read(p)
  133. }
  134. // CopyNext reads the next object from m without decoding it and writes it to w.
  135. // It avoids unnecessary copies internally.
  136. func (m *Reader) CopyNext(w io.Writer) (int64, error) {
  137. sz, o, err := getNextSize(m.R)
  138. if err != nil {
  139. return 0, err
  140. }
  141. var n int64
  142. // Opportunistic optimization: if we can fit the whole thing in the m.R
  143. // buffer, then just get a pointer to that, and pass it to w.Write,
  144. // avoiding an allocation.
  145. if int(sz) <= m.R.BufferSize() {
  146. var nn int
  147. var buf []byte
  148. buf, err = m.R.Next(int(sz))
  149. if err != nil {
  150. if err == io.ErrUnexpectedEOF {
  151. err = ErrShortBytes
  152. }
  153. return 0, err
  154. }
  155. nn, err = w.Write(buf)
  156. n += int64(nn)
  157. } else {
  158. // Fall back to io.CopyN.
  159. // May avoid allocating if w is a ReaderFrom (e.g. bytes.Buffer)
  160. n, err = io.CopyN(w, m.R, int64(sz))
  161. if err == io.ErrUnexpectedEOF {
  162. err = ErrShortBytes
  163. }
  164. }
  165. if err != nil {
  166. return n, err
  167. } else if n < int64(sz) {
  168. return n, io.ErrShortWrite
  169. }
  170. // for maps and slices, read elements
  171. for x := uintptr(0); x < o; x++ {
  172. var n2 int64
  173. n2, err = m.CopyNext(w)
  174. if err != nil {
  175. return n, err
  176. }
  177. n += n2
  178. }
  179. return n, nil
  180. }
  181. // ReadFull implements `io.ReadFull`
  182. func (m *Reader) ReadFull(p []byte) (int, error) {
  183. return m.R.ReadFull(p)
  184. }
  185. // Reset resets the underlying reader.
  186. func (m *Reader) Reset(r io.Reader) { m.R.Reset(r) }
  187. // Buffered returns the number of bytes currently in the read buffer.
  188. func (m *Reader) Buffered() int { return m.R.Buffered() }
  189. // BufferSize returns the capacity of the read buffer.
  190. func (m *Reader) BufferSize() int { return m.R.BufferSize() }
  191. // NextType returns the next object type to be decoded.
  192. func (m *Reader) NextType() (Type, error) {
  193. p, err := m.R.Peek(1)
  194. if err != nil {
  195. return InvalidType, err
  196. }
  197. t := getType(p[0])
  198. if t == InvalidType {
  199. return t, InvalidPrefixError(p[0])
  200. }
  201. if t == ExtensionType {
  202. v, err := m.peekExtensionType()
  203. if err != nil {
  204. return InvalidType, err
  205. }
  206. switch v {
  207. case Complex64Extension:
  208. return Complex64Type, nil
  209. case Complex128Extension:
  210. return Complex128Type, nil
  211. case TimeExtension:
  212. return TimeType, nil
  213. }
  214. }
  215. return t, nil
  216. }
  217. // IsNil returns whether or not
  218. // the next byte is a null messagepack byte
  219. func (m *Reader) IsNil() bool {
  220. p, err := m.R.Peek(1)
  221. return err == nil && p[0] == mnil
  222. }
  223. // getNextSize returns the size of the next object on the wire.
  224. // returns (obj size, obj elements, error)
  225. // only maps and arrays have non-zero obj elements
  226. // for maps and arrays, obj size does not include elements
  227. //
  228. // use uintptr b/c it's guaranteed to be large enough
  229. // to hold whatever we can fit in memory.
  230. func getNextSize(r *fwd.Reader) (uintptr, uintptr, error) {
  231. b, err := r.Peek(1)
  232. if err != nil {
  233. return 0, 0, err
  234. }
  235. lead := b[0]
  236. spec := getBytespec(lead)
  237. size, mode := spec.size, spec.extra
  238. if size == 0 {
  239. return 0, 0, InvalidPrefixError(lead)
  240. }
  241. if mode >= 0 {
  242. return uintptr(size), uintptr(mode), nil
  243. }
  244. b, err = r.Peek(int(size))
  245. if err != nil {
  246. return 0, 0, err
  247. }
  248. switch mode {
  249. case extra8:
  250. return uintptr(size) + uintptr(b[1]), 0, nil
  251. case extra16:
  252. return uintptr(size) + uintptr(big.Uint16(b[1:])), 0, nil
  253. case extra32:
  254. return uintptr(size) + uintptr(big.Uint32(b[1:])), 0, nil
  255. case map16v:
  256. return uintptr(size), 2 * uintptr(big.Uint16(b[1:])), nil
  257. case map32v:
  258. return uintptr(size), 2 * uintptr(big.Uint32(b[1:])), nil
  259. case array16v:
  260. return uintptr(size), uintptr(big.Uint16(b[1:])), nil
  261. case array32v:
  262. return uintptr(size), uintptr(big.Uint32(b[1:])), nil
  263. default:
  264. return 0, 0, fatal
  265. }
  266. }
  267. // Skip skips over the next object, regardless of
  268. // its type. If it is an array or map, the whole array
  269. // or map will be skipped.
  270. func (m *Reader) Skip() error {
  271. var (
  272. v uintptr // bytes
  273. o uintptr // objects
  274. err error
  275. p []byte
  276. )
  277. // we can use the faster
  278. // method if we have enough
  279. // buffered data
  280. if m.R.Buffered() >= 5 {
  281. p, err = m.R.Peek(5)
  282. if err != nil {
  283. return err
  284. }
  285. v, o, err = getSize(p)
  286. if err != nil {
  287. return err
  288. }
  289. } else {
  290. v, o, err = getNextSize(m.R)
  291. if err != nil {
  292. return err
  293. }
  294. }
  295. // 'v' is always non-zero
  296. // if err == nil
  297. _, err = m.R.Skip(int(v))
  298. if err != nil {
  299. return err
  300. }
  301. // for maps and slices, skip elements
  302. for x := uintptr(0); x < o; x++ {
  303. err = m.Skip()
  304. if err != nil {
  305. return err
  306. }
  307. }
  308. return nil
  309. }
  310. // ReadMapHeader reads the next object
  311. // as a map header and returns the size
  312. // of the map and the number of bytes written.
  313. // It will return a TypeError{} if the next
  314. // object is not a map.
  315. func (m *Reader) ReadMapHeader() (sz uint32, err error) {
  316. var p []byte
  317. var lead byte
  318. p, err = m.R.Peek(1)
  319. if err != nil {
  320. return
  321. }
  322. lead = p[0]
  323. if isfixmap(lead) {
  324. sz = uint32(rfixmap(lead))
  325. _, err = m.R.Skip(1)
  326. return
  327. }
  328. switch lead {
  329. case mmap16:
  330. p, err = m.R.Next(3)
  331. if err != nil {
  332. return
  333. }
  334. sz = uint32(big.Uint16(p[1:]))
  335. return
  336. case mmap32:
  337. p, err = m.R.Next(5)
  338. if err != nil {
  339. return
  340. }
  341. sz = big.Uint32(p[1:])
  342. return
  343. default:
  344. err = badPrefix(MapType, lead)
  345. return
  346. }
  347. }
  348. // ReadMapKey reads either a 'str' or 'bin' field from
  349. // the reader and returns the value as a []byte. It uses
  350. // scratch for storage if it is large enough.
  351. func (m *Reader) ReadMapKey(scratch []byte) ([]byte, error) {
  352. out, err := m.ReadStringAsBytes(scratch)
  353. if err != nil {
  354. if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
  355. return m.ReadBytes(scratch)
  356. }
  357. return nil, err
  358. }
  359. return out, nil
  360. }
  361. // ReadMapKeyPtr returns a []byte pointing to the contents
  362. // of a valid map key. The key cannot be empty, and it
  363. // must be shorter than the total buffer size of the
  364. // *Reader. Additionally, the returned slice is only
  365. // valid until the next *Reader method call. Users
  366. // should exercise extreme care when using this
  367. // method; writing into the returned slice may
  368. // corrupt future reads.
  369. func (m *Reader) ReadMapKeyPtr() ([]byte, error) {
  370. p, err := m.R.Peek(1)
  371. if err != nil {
  372. return nil, err
  373. }
  374. lead := p[0]
  375. var read int
  376. if isfixstr(lead) {
  377. read = int(rfixstr(lead))
  378. m.R.Skip(1)
  379. goto fill
  380. }
  381. switch lead {
  382. case mstr8, mbin8:
  383. p, err = m.R.Next(2)
  384. if err != nil {
  385. return nil, err
  386. }
  387. read = int(p[1])
  388. case mstr16, mbin16:
  389. p, err = m.R.Next(3)
  390. if err != nil {
  391. return nil, err
  392. }
  393. read = int(big.Uint16(p[1:]))
  394. case mstr32, mbin32:
  395. p, err = m.R.Next(5)
  396. if err != nil {
  397. return nil, err
  398. }
  399. read = int(big.Uint32(p[1:]))
  400. default:
  401. return nil, badPrefix(StrType, lead)
  402. }
  403. fill:
  404. if read == 0 {
  405. return nil, ErrShortBytes
  406. }
  407. return m.R.Next(read)
  408. }
  409. // ReadArrayHeader reads the next object as an
  410. // array header and returns the size of the array
  411. // and the number of bytes read.
  412. func (m *Reader) ReadArrayHeader() (sz uint32, err error) {
  413. var lead byte
  414. var p []byte
  415. p, err = m.R.Peek(1)
  416. if err != nil {
  417. return
  418. }
  419. lead = p[0]
  420. if isfixarray(lead) {
  421. sz = uint32(rfixarray(lead))
  422. _, err = m.R.Skip(1)
  423. return
  424. }
  425. switch lead {
  426. case marray16:
  427. p, err = m.R.Next(3)
  428. if err != nil {
  429. return
  430. }
  431. sz = uint32(big.Uint16(p[1:]))
  432. return
  433. case marray32:
  434. p, err = m.R.Next(5)
  435. if err != nil {
  436. return
  437. }
  438. sz = big.Uint32(p[1:])
  439. return
  440. default:
  441. err = badPrefix(ArrayType, lead)
  442. return
  443. }
  444. }
  445. // ReadNil reads a 'nil' MessagePack byte from the reader
  446. func (m *Reader) ReadNil() error {
  447. p, err := m.R.Peek(1)
  448. if err != nil {
  449. return err
  450. }
  451. if p[0] != mnil {
  452. return badPrefix(NilType, p[0])
  453. }
  454. _, err = m.R.Skip(1)
  455. return err
  456. }
  457. // ReadFloat64 reads a float64 from the reader.
  458. // (If the value on the wire is encoded as a float32,
  459. // it will be up-cast to a float64.)
  460. func (m *Reader) ReadFloat64() (f float64, err error) {
  461. var p []byte
  462. p, err = m.R.Peek(9)
  463. if err != nil {
  464. // we'll allow a coversion from float32 to float64,
  465. // since we don't lose any precision
  466. if err == io.EOF && len(p) > 0 && p[0] == mfloat32 {
  467. ef, err := m.ReadFloat32()
  468. return float64(ef), err
  469. }
  470. return
  471. }
  472. if p[0] != mfloat64 {
  473. // see above
  474. if p[0] == mfloat32 {
  475. ef, err := m.ReadFloat32()
  476. return float64(ef), err
  477. }
  478. err = badPrefix(Float64Type, p[0])
  479. return
  480. }
  481. f = math.Float64frombits(getMuint64(p))
  482. _, err = m.R.Skip(9)
  483. return
  484. }
  485. // ReadFloat32 reads a float32 from the reader
  486. func (m *Reader) ReadFloat32() (f float32, err error) {
  487. var p []byte
  488. p, err = m.R.Peek(5)
  489. if err != nil {
  490. return
  491. }
  492. if p[0] != mfloat32 {
  493. err = badPrefix(Float32Type, p[0])
  494. return
  495. }
  496. f = math.Float32frombits(getMuint32(p))
  497. _, err = m.R.Skip(5)
  498. return
  499. }
  500. // ReadBool reads a bool from the reader
  501. func (m *Reader) ReadBool() (b bool, err error) {
  502. var p []byte
  503. p, err = m.R.Peek(1)
  504. if err != nil {
  505. return
  506. }
  507. switch p[0] {
  508. case mtrue:
  509. b = true
  510. case mfalse:
  511. default:
  512. err = badPrefix(BoolType, p[0])
  513. return
  514. }
  515. _, err = m.R.Skip(1)
  516. return
  517. }
  518. // ReadDuration reads a time.Duration from the reader
  519. func (m *Reader) ReadDuration() (d time.Duration, err error) {
  520. i, err := m.ReadInt64()
  521. return time.Duration(i), err
  522. }
  523. // ReadInt64 reads an int64 from the reader
  524. func (m *Reader) ReadInt64() (i int64, err error) {
  525. var p []byte
  526. var lead byte
  527. p, err = m.R.Peek(1)
  528. if err != nil {
  529. return
  530. }
  531. lead = p[0]
  532. if isfixint(lead) {
  533. i = int64(rfixint(lead))
  534. _, err = m.R.Skip(1)
  535. return
  536. } else if isnfixint(lead) {
  537. i = int64(rnfixint(lead))
  538. _, err = m.R.Skip(1)
  539. return
  540. }
  541. switch lead {
  542. case mint8:
  543. p, err = m.R.Next(2)
  544. if err != nil {
  545. return
  546. }
  547. i = int64(getMint8(p))
  548. return
  549. case muint8:
  550. p, err = m.R.Next(2)
  551. if err != nil {
  552. return
  553. }
  554. i = int64(getMuint8(p))
  555. return
  556. case mint16:
  557. p, err = m.R.Next(3)
  558. if err != nil {
  559. return
  560. }
  561. i = int64(getMint16(p))
  562. return
  563. case muint16:
  564. p, err = m.R.Next(3)
  565. if err != nil {
  566. return
  567. }
  568. i = int64(getMuint16(p))
  569. return
  570. case mint32:
  571. p, err = m.R.Next(5)
  572. if err != nil {
  573. return
  574. }
  575. i = int64(getMint32(p))
  576. return
  577. case muint32:
  578. p, err = m.R.Next(5)
  579. if err != nil {
  580. return
  581. }
  582. i = int64(getMuint32(p))
  583. return
  584. case mint64:
  585. p, err = m.R.Next(9)
  586. if err != nil {
  587. return
  588. }
  589. i = getMint64(p)
  590. return
  591. case muint64:
  592. p, err = m.R.Next(9)
  593. if err != nil {
  594. return
  595. }
  596. u := getMuint64(p)
  597. if u > math.MaxInt64 {
  598. err = UintOverflow{Value: u, FailedBitsize: 64}
  599. return
  600. }
  601. i = int64(u)
  602. return
  603. default:
  604. err = badPrefix(IntType, lead)
  605. return
  606. }
  607. }
  608. // ReadInt32 reads an int32 from the reader
  609. func (m *Reader) ReadInt32() (i int32, err error) {
  610. var in int64
  611. in, err = m.ReadInt64()
  612. if in > math.MaxInt32 || in < math.MinInt32 {
  613. err = IntOverflow{Value: in, FailedBitsize: 32}
  614. return
  615. }
  616. i = int32(in)
  617. return
  618. }
  619. // ReadInt16 reads an int16 from the reader
  620. func (m *Reader) ReadInt16() (i int16, err error) {
  621. var in int64
  622. in, err = m.ReadInt64()
  623. if in > math.MaxInt16 || in < math.MinInt16 {
  624. err = IntOverflow{Value: in, FailedBitsize: 16}
  625. return
  626. }
  627. i = int16(in)
  628. return
  629. }
  630. // ReadInt8 reads an int8 from the reader
  631. func (m *Reader) ReadInt8() (i int8, err error) {
  632. var in int64
  633. in, err = m.ReadInt64()
  634. if in > math.MaxInt8 || in < math.MinInt8 {
  635. err = IntOverflow{Value: in, FailedBitsize: 8}
  636. return
  637. }
  638. i = int8(in)
  639. return
  640. }
  641. // ReadInt reads an int from the reader
  642. func (m *Reader) ReadInt() (i int, err error) {
  643. if smallint {
  644. var in int32
  645. in, err = m.ReadInt32()
  646. i = int(in)
  647. return
  648. }
  649. var in int64
  650. in, err = m.ReadInt64()
  651. i = int(in)
  652. return
  653. }
  654. // ReadUint64 reads a uint64 from the reader
  655. func (m *Reader) ReadUint64() (u uint64, err error) {
  656. var p []byte
  657. var lead byte
  658. p, err = m.R.Peek(1)
  659. if err != nil {
  660. return
  661. }
  662. lead = p[0]
  663. if isfixint(lead) {
  664. u = uint64(rfixint(lead))
  665. _, err = m.R.Skip(1)
  666. return
  667. }
  668. switch lead {
  669. case mint8:
  670. p, err = m.R.Next(2)
  671. if err != nil {
  672. return
  673. }
  674. v := int64(getMint8(p))
  675. if v < 0 {
  676. err = UintBelowZero{Value: v}
  677. return
  678. }
  679. u = uint64(v)
  680. return
  681. case muint8:
  682. p, err = m.R.Next(2)
  683. if err != nil {
  684. return
  685. }
  686. u = uint64(getMuint8(p))
  687. return
  688. case mint16:
  689. p, err = m.R.Next(3)
  690. if err != nil {
  691. return
  692. }
  693. v := int64(getMint16(p))
  694. if v < 0 {
  695. err = UintBelowZero{Value: v}
  696. return
  697. }
  698. u = uint64(v)
  699. return
  700. case muint16:
  701. p, err = m.R.Next(3)
  702. if err != nil {
  703. return
  704. }
  705. u = uint64(getMuint16(p))
  706. return
  707. case mint32:
  708. p, err = m.R.Next(5)
  709. if err != nil {
  710. return
  711. }
  712. v := int64(getMint32(p))
  713. if v < 0 {
  714. err = UintBelowZero{Value: v}
  715. return
  716. }
  717. u = uint64(v)
  718. return
  719. case muint32:
  720. p, err = m.R.Next(5)
  721. if err != nil {
  722. return
  723. }
  724. u = uint64(getMuint32(p))
  725. return
  726. case mint64:
  727. p, err = m.R.Next(9)
  728. if err != nil {
  729. return
  730. }
  731. v := int64(getMint64(p))
  732. if v < 0 {
  733. err = UintBelowZero{Value: v}
  734. return
  735. }
  736. u = uint64(v)
  737. return
  738. case muint64:
  739. p, err = m.R.Next(9)
  740. if err != nil {
  741. return
  742. }
  743. u = getMuint64(p)
  744. return
  745. default:
  746. if isnfixint(lead) {
  747. err = UintBelowZero{Value: int64(rnfixint(lead))}
  748. } else {
  749. err = badPrefix(UintType, lead)
  750. }
  751. return
  752. }
  753. }
  754. // ReadUint32 reads a uint32 from the reader
  755. func (m *Reader) ReadUint32() (u uint32, err error) {
  756. var in uint64
  757. in, err = m.ReadUint64()
  758. if in > math.MaxUint32 {
  759. err = UintOverflow{Value: in, FailedBitsize: 32}
  760. return
  761. }
  762. u = uint32(in)
  763. return
  764. }
  765. // ReadUint16 reads a uint16 from the reader
  766. func (m *Reader) ReadUint16() (u uint16, err error) {
  767. var in uint64
  768. in, err = m.ReadUint64()
  769. if in > math.MaxUint16 {
  770. err = UintOverflow{Value: in, FailedBitsize: 16}
  771. return
  772. }
  773. u = uint16(in)
  774. return
  775. }
  776. // ReadUint8 reads a uint8 from the reader
  777. func (m *Reader) ReadUint8() (u uint8, err error) {
  778. var in uint64
  779. in, err = m.ReadUint64()
  780. if in > math.MaxUint8 {
  781. err = UintOverflow{Value: in, FailedBitsize: 8}
  782. return
  783. }
  784. u = uint8(in)
  785. return
  786. }
  787. // ReadUint reads a uint from the reader
  788. func (m *Reader) ReadUint() (u uint, err error) {
  789. if smallint {
  790. var un uint32
  791. un, err = m.ReadUint32()
  792. u = uint(un)
  793. return
  794. }
  795. var un uint64
  796. un, err = m.ReadUint64()
  797. u = uint(un)
  798. return
  799. }
  800. // ReadByte is analogous to ReadUint8.
  801. //
  802. // NOTE: this is *not* an implementation
  803. // of io.ByteReader.
  804. func (m *Reader) ReadByte() (b byte, err error) {
  805. var in uint64
  806. in, err = m.ReadUint64()
  807. if in > math.MaxUint8 {
  808. err = UintOverflow{Value: in, FailedBitsize: 8}
  809. return
  810. }
  811. b = byte(in)
  812. return
  813. }
  814. // ReadBytes reads a MessagePack 'bin' object
  815. // from the reader and returns its value. It may
  816. // use 'scratch' for storage if it is non-nil.
  817. func (m *Reader) ReadBytes(scratch []byte) (b []byte, err error) {
  818. var p []byte
  819. var lead byte
  820. p, err = m.R.Peek(2)
  821. if err != nil {
  822. return
  823. }
  824. lead = p[0]
  825. var read int64
  826. switch lead {
  827. case mbin8:
  828. read = int64(p[1])
  829. m.R.Skip(2)
  830. case mbin16:
  831. p, err = m.R.Next(3)
  832. if err != nil {
  833. return
  834. }
  835. read = int64(big.Uint16(p[1:]))
  836. case mbin32:
  837. p, err = m.R.Next(5)
  838. if err != nil {
  839. return
  840. }
  841. read = int64(big.Uint32(p[1:]))
  842. default:
  843. err = badPrefix(BinType, lead)
  844. return
  845. }
  846. if int64(cap(scratch)) < read {
  847. b = make([]byte, read)
  848. } else {
  849. b = scratch[0:read]
  850. }
  851. _, err = m.R.ReadFull(b)
  852. return
  853. }
  854. // ReadBytesHeader reads the size header
  855. // of a MessagePack 'bin' object. The user
  856. // is responsible for dealing with the next
  857. // 'sz' bytes from the reader in an application-specific
  858. // way.
  859. func (m *Reader) ReadBytesHeader() (sz uint32, err error) {
  860. var p []byte
  861. p, err = m.R.Peek(1)
  862. if err != nil {
  863. return
  864. }
  865. switch p[0] {
  866. case mbin8:
  867. p, err = m.R.Next(2)
  868. if err != nil {
  869. return
  870. }
  871. sz = uint32(p[1])
  872. return
  873. case mbin16:
  874. p, err = m.R.Next(3)
  875. if err != nil {
  876. return
  877. }
  878. sz = uint32(big.Uint16(p[1:]))
  879. return
  880. case mbin32:
  881. p, err = m.R.Next(5)
  882. if err != nil {
  883. return
  884. }
  885. sz = uint32(big.Uint32(p[1:]))
  886. return
  887. default:
  888. err = badPrefix(BinType, p[0])
  889. return
  890. }
  891. }
  892. // ReadExactBytes reads a MessagePack 'bin'-encoded
  893. // object off of the wire into the provided slice. An
  894. // ArrayError will be returned if the object is not
  895. // exactly the length of the input slice.
  896. func (m *Reader) ReadExactBytes(into []byte) error {
  897. p, err := m.R.Peek(2)
  898. if err != nil {
  899. return err
  900. }
  901. lead := p[0]
  902. var read int64 // bytes to read
  903. var skip int // prefix size to skip
  904. switch lead {
  905. case mbin8:
  906. read = int64(p[1])
  907. skip = 2
  908. case mbin16:
  909. p, err = m.R.Peek(3)
  910. if err != nil {
  911. return err
  912. }
  913. read = int64(big.Uint16(p[1:]))
  914. skip = 3
  915. case mbin32:
  916. p, err = m.R.Peek(5)
  917. if err != nil {
  918. return err
  919. }
  920. read = int64(big.Uint32(p[1:]))
  921. skip = 5
  922. default:
  923. return badPrefix(BinType, lead)
  924. }
  925. if read != int64(len(into)) {
  926. return ArrayError{Wanted: uint32(len(into)), Got: uint32(read)}
  927. }
  928. m.R.Skip(skip)
  929. _, err = m.R.ReadFull(into)
  930. return err
  931. }
  932. // ReadStringAsBytes reads a MessagePack 'str' (utf-8) string
  933. // and returns its value as bytes. It may use 'scratch' for storage
  934. // if it is non-nil.
  935. func (m *Reader) ReadStringAsBytes(scratch []byte) (b []byte, err error) {
  936. var p []byte
  937. var lead byte
  938. p, err = m.R.Peek(1)
  939. if err != nil {
  940. return
  941. }
  942. lead = p[0]
  943. var read int64
  944. if isfixstr(lead) {
  945. read = int64(rfixstr(lead))
  946. m.R.Skip(1)
  947. goto fill
  948. }
  949. switch lead {
  950. case mstr8:
  951. p, err = m.R.Next(2)
  952. if err != nil {
  953. return
  954. }
  955. read = int64(uint8(p[1]))
  956. case mstr16:
  957. p, err = m.R.Next(3)
  958. if err != nil {
  959. return
  960. }
  961. read = int64(big.Uint16(p[1:]))
  962. case mstr32:
  963. p, err = m.R.Next(5)
  964. if err != nil {
  965. return
  966. }
  967. read = int64(big.Uint32(p[1:]))
  968. default:
  969. err = badPrefix(StrType, lead)
  970. return
  971. }
  972. fill:
  973. if int64(cap(scratch)) < read {
  974. b = make([]byte, read)
  975. } else {
  976. b = scratch[0:read]
  977. }
  978. _, err = m.R.ReadFull(b)
  979. return
  980. }
  981. // ReadStringHeader reads a string header
  982. // off of the wire. The user is then responsible
  983. // for dealing with the next 'sz' bytes from
  984. // the reader in an application-specific manner.
  985. func (m *Reader) ReadStringHeader() (sz uint32, err error) {
  986. var p []byte
  987. p, err = m.R.Peek(1)
  988. if err != nil {
  989. return
  990. }
  991. lead := p[0]
  992. if isfixstr(lead) {
  993. sz = uint32(rfixstr(lead))
  994. m.R.Skip(1)
  995. return
  996. }
  997. switch lead {
  998. case mstr8:
  999. p, err = m.R.Next(2)
  1000. if err != nil {
  1001. return
  1002. }
  1003. sz = uint32(p[1])
  1004. return
  1005. case mstr16:
  1006. p, err = m.R.Next(3)
  1007. if err != nil {
  1008. return
  1009. }
  1010. sz = uint32(big.Uint16(p[1:]))
  1011. return
  1012. case mstr32:
  1013. p, err = m.R.Next(5)
  1014. if err != nil {
  1015. return
  1016. }
  1017. sz = big.Uint32(p[1:])
  1018. return
  1019. default:
  1020. err = badPrefix(StrType, lead)
  1021. return
  1022. }
  1023. }
  1024. // ReadString reads a utf-8 string from the reader
  1025. func (m *Reader) ReadString() (s string, err error) {
  1026. var p []byte
  1027. var lead byte
  1028. var read int64
  1029. p, err = m.R.Peek(1)
  1030. if err != nil {
  1031. return
  1032. }
  1033. lead = p[0]
  1034. if isfixstr(lead) {
  1035. read = int64(rfixstr(lead))
  1036. m.R.Skip(1)
  1037. goto fill
  1038. }
  1039. switch lead {
  1040. case mstr8:
  1041. p, err = m.R.Next(2)
  1042. if err != nil {
  1043. return
  1044. }
  1045. read = int64(uint8(p[1]))
  1046. case mstr16:
  1047. p, err = m.R.Next(3)
  1048. if err != nil {
  1049. return
  1050. }
  1051. read = int64(big.Uint16(p[1:]))
  1052. case mstr32:
  1053. p, err = m.R.Next(5)
  1054. if err != nil {
  1055. return
  1056. }
  1057. read = int64(big.Uint32(p[1:]))
  1058. default:
  1059. err = badPrefix(StrType, lead)
  1060. return
  1061. }
  1062. fill:
  1063. if read == 0 {
  1064. s, err = "", nil
  1065. return
  1066. }
  1067. // reading into the memory
  1068. // that will become the string
  1069. // itself has vastly superior
  1070. // worst-case performance, because
  1071. // the reader buffer doesn't have
  1072. // to be large enough to hold the string.
  1073. // the idea here is to make it more
  1074. // difficult for someone malicious
  1075. // to cause the system to run out of
  1076. // memory by sending very large strings.
  1077. //
  1078. // NOTE: this works because the argument
  1079. // passed to (*fwd.Reader).ReadFull escapes
  1080. // to the heap; its argument may, in turn,
  1081. // be passed to the underlying reader, and
  1082. // thus escape analysis *must* conclude that
  1083. // 'out' escapes.
  1084. out := make([]byte, read)
  1085. _, err = m.R.ReadFull(out)
  1086. if err != nil {
  1087. return
  1088. }
  1089. s = UnsafeString(out)
  1090. return
  1091. }
  1092. // ReadComplex64 reads a complex64 from the reader
  1093. func (m *Reader) ReadComplex64() (f complex64, err error) {
  1094. var p []byte
  1095. p, err = m.R.Peek(10)
  1096. if err != nil {
  1097. return
  1098. }
  1099. if p[0] != mfixext8 {
  1100. err = badPrefix(Complex64Type, p[0])
  1101. return
  1102. }
  1103. if int8(p[1]) != Complex64Extension {
  1104. err = errExt(int8(p[1]), Complex64Extension)
  1105. return
  1106. }
  1107. f = complex(math.Float32frombits(big.Uint32(p[2:])),
  1108. math.Float32frombits(big.Uint32(p[6:])))
  1109. _, err = m.R.Skip(10)
  1110. return
  1111. }
  1112. // ReadComplex128 reads a complex128 from the reader
  1113. func (m *Reader) ReadComplex128() (f complex128, err error) {
  1114. var p []byte
  1115. p, err = m.R.Peek(18)
  1116. if err != nil {
  1117. return
  1118. }
  1119. if p[0] != mfixext16 {
  1120. err = badPrefix(Complex128Type, p[0])
  1121. return
  1122. }
  1123. if int8(p[1]) != Complex128Extension {
  1124. err = errExt(int8(p[1]), Complex128Extension)
  1125. return
  1126. }
  1127. f = complex(math.Float64frombits(big.Uint64(p[2:])),
  1128. math.Float64frombits(big.Uint64(p[10:])))
  1129. _, err = m.R.Skip(18)
  1130. return
  1131. }
  1132. // ReadMapStrIntf reads a MessagePack map into a map[string]interface{}.
  1133. // (You must pass a non-nil map into the function.)
  1134. func (m *Reader) ReadMapStrIntf(mp map[string]interface{}) (err error) {
  1135. var sz uint32
  1136. sz, err = m.ReadMapHeader()
  1137. if err != nil {
  1138. return
  1139. }
  1140. for key := range mp {
  1141. delete(mp, key)
  1142. }
  1143. for i := uint32(0); i < sz; i++ {
  1144. var key string
  1145. var val interface{}
  1146. key, err = m.ReadString()
  1147. if err != nil {
  1148. return
  1149. }
  1150. val, err = m.ReadIntf()
  1151. if err != nil {
  1152. return
  1153. }
  1154. mp[key] = val
  1155. }
  1156. return
  1157. }
  1158. // ReadTime reads a time.Time object from the reader.
  1159. // The returned time's location will be set to time.Local.
  1160. func (m *Reader) ReadTime() (t time.Time, err error) {
  1161. var p []byte
  1162. p, err = m.R.Peek(15)
  1163. if err != nil {
  1164. return
  1165. }
  1166. if p[0] != mext8 || p[1] != 12 {
  1167. err = badPrefix(TimeType, p[0])
  1168. return
  1169. }
  1170. if int8(p[2]) != TimeExtension {
  1171. err = errExt(int8(p[2]), TimeExtension)
  1172. return
  1173. }
  1174. sec, nsec := getUnix(p[3:])
  1175. t = time.Unix(sec, int64(nsec)).Local()
  1176. _, err = m.R.Skip(15)
  1177. return
  1178. }
  1179. // ReadIntf reads out the next object as a raw interface{}.
  1180. // Arrays are decoded as []interface{}, and maps are decoded
  1181. // as map[string]interface{}. Integers are decoded as int64
  1182. // and unsigned integers are decoded as uint64.
  1183. func (m *Reader) ReadIntf() (i interface{}, err error) {
  1184. var t Type
  1185. t, err = m.NextType()
  1186. if err != nil {
  1187. return
  1188. }
  1189. switch t {
  1190. case BoolType:
  1191. i, err = m.ReadBool()
  1192. return
  1193. case IntType:
  1194. i, err = m.ReadInt64()
  1195. return
  1196. case UintType:
  1197. i, err = m.ReadUint64()
  1198. return
  1199. case BinType:
  1200. i, err = m.ReadBytes(nil)
  1201. return
  1202. case StrType:
  1203. i, err = m.ReadString()
  1204. return
  1205. case Complex64Type:
  1206. i, err = m.ReadComplex64()
  1207. return
  1208. case Complex128Type:
  1209. i, err = m.ReadComplex128()
  1210. return
  1211. case TimeType:
  1212. i, err = m.ReadTime()
  1213. return
  1214. case DurationType:
  1215. i, err = m.ReadDuration()
  1216. return
  1217. case ExtensionType:
  1218. var t int8
  1219. t, err = m.peekExtensionType()
  1220. if err != nil {
  1221. return
  1222. }
  1223. f, ok := extensionReg[t]
  1224. if ok {
  1225. e := f()
  1226. err = m.ReadExtension(e)
  1227. i = e
  1228. return
  1229. }
  1230. var e RawExtension
  1231. e.Type = t
  1232. err = m.ReadExtension(&e)
  1233. i = &e
  1234. return
  1235. case MapType:
  1236. mp := make(map[string]interface{})
  1237. err = m.ReadMapStrIntf(mp)
  1238. i = mp
  1239. return
  1240. case NilType:
  1241. err = m.ReadNil()
  1242. i = nil
  1243. return
  1244. case Float32Type:
  1245. i, err = m.ReadFloat32()
  1246. return
  1247. case Float64Type:
  1248. i, err = m.ReadFloat64()
  1249. return
  1250. case ArrayType:
  1251. var sz uint32
  1252. sz, err = m.ReadArrayHeader()
  1253. if err != nil {
  1254. return
  1255. }
  1256. out := make([]interface{}, int(sz))
  1257. for j := range out {
  1258. out[j], err = m.ReadIntf()
  1259. if err != nil {
  1260. return
  1261. }
  1262. }
  1263. i = out
  1264. return
  1265. default:
  1266. return nil, fatal // unreachable
  1267. }
  1268. }