decode.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rlp
  17. import (
  18. "bufio"
  19. "bytes"
  20. "encoding/binary"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "math/big"
  25. "reflect"
  26. "strings"
  27. "sync"
  28. "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct"
  29. )
  30. //lint:ignore ST1012 EOL is not an error.
  31. // EOL is returned when the end of the current list
  32. // has been reached during streaming.
  33. var EOL = errors.New("rlp: end of list")
  34. var (
  35. ErrExpectedString = errors.New("rlp: expected String or Byte")
  36. ErrExpectedList = errors.New("rlp: expected List")
  37. ErrCanonInt = errors.New("rlp: non-canonical integer format")
  38. ErrCanonSize = errors.New("rlp: non-canonical size information")
  39. ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
  40. ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
  41. ErrMoreThanOneValue = errors.New("rlp: input contains more than one value")
  42. // internal errors
  43. errNotInList = errors.New("rlp: call of ListEnd outside of any list")
  44. errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL")
  45. errUintOverflow = errors.New("rlp: uint overflow")
  46. errNoPointer = errors.New("rlp: interface given to Decode must be a pointer")
  47. errDecodeIntoNil = errors.New("rlp: pointer given to Decode must not be nil")
  48. streamPool = sync.Pool{
  49. New: func() interface{} { return new(Stream) },
  50. }
  51. )
  52. // Decoder is implemented by types that require custom RLP decoding rules or need to decode
  53. // into private fields.
  54. //
  55. // The DecodeRLP method should read one value from the given Stream. It is not forbidden to
  56. // read less or more, but it might be confusing.
  57. type Decoder interface {
  58. DecodeRLP(*Stream) error
  59. }
  60. // Decode parses RLP-encoded data from r and stores the result in the value pointed to by
  61. // val. Please see package-level documentation for the decoding rules. Val must be a
  62. // non-nil pointer.
  63. //
  64. // If r does not implement ByteReader, Decode will do its own buffering.
  65. //
  66. // Note that Decode does not set an input limit for all readers and may be vulnerable to
  67. // panics cause by huge value sizes. If you need an input limit, use
  68. //
  69. // NewStream(r, limit).Decode(val)
  70. func Decode(r io.Reader, val interface{}) error {
  71. stream := streamPool.Get().(*Stream)
  72. defer streamPool.Put(stream)
  73. stream.Reset(r, 0)
  74. return stream.Decode(val)
  75. }
  76. // DecodeBytes parses RLP data from b into val. Please see package-level documentation for
  77. // the decoding rules. The input must contain exactly one value and no trailing data.
  78. func DecodeBytes(b []byte, val interface{}) error {
  79. r := bytes.NewReader(b)
  80. stream := streamPool.Get().(*Stream)
  81. defer streamPool.Put(stream)
  82. stream.Reset(r, uint64(len(b)))
  83. if err := stream.Decode(val); err != nil {
  84. return err
  85. }
  86. if r.Len() > 0 {
  87. return ErrMoreThanOneValue
  88. }
  89. return nil
  90. }
  91. type decodeError struct {
  92. msg string
  93. typ reflect.Type
  94. ctx []string
  95. }
  96. func (err *decodeError) Error() string {
  97. ctx := ""
  98. if len(err.ctx) > 0 {
  99. ctx = ", decoding into "
  100. for i := len(err.ctx) - 1; i >= 0; i-- {
  101. ctx += err.ctx[i]
  102. }
  103. }
  104. return fmt.Sprintf("rlp: %s for %v%s", err.msg, err.typ, ctx)
  105. }
  106. func wrapStreamError(err error, typ reflect.Type) error {
  107. switch err {
  108. case ErrCanonInt:
  109. return &decodeError{msg: "non-canonical integer (leading zero bytes)", typ: typ}
  110. case ErrCanonSize:
  111. return &decodeError{msg: "non-canonical size information", typ: typ}
  112. case ErrExpectedList:
  113. return &decodeError{msg: "expected input list", typ: typ}
  114. case ErrExpectedString:
  115. return &decodeError{msg: "expected input string or byte", typ: typ}
  116. case errUintOverflow:
  117. return &decodeError{msg: "input string too long", typ: typ}
  118. case errNotAtEOL:
  119. return &decodeError{msg: "input list has too many elements", typ: typ}
  120. }
  121. return err
  122. }
  123. func addErrorContext(err error, ctx string) error {
  124. if decErr, ok := err.(*decodeError); ok {
  125. decErr.ctx = append(decErr.ctx, ctx)
  126. }
  127. return err
  128. }
  129. var (
  130. decoderInterface = reflect.TypeOf(new(Decoder)).Elem()
  131. bigInt = reflect.TypeOf(big.Int{})
  132. )
  133. func makeDecoder(typ reflect.Type, tags rlpstruct.Tags) (dec decoder, err error) {
  134. kind := typ.Kind()
  135. switch {
  136. case typ == rawValueType:
  137. return decodeRawValue, nil
  138. case typ.AssignableTo(reflect.PtrTo(bigInt)):
  139. return decodeBigInt, nil
  140. case typ.AssignableTo(bigInt):
  141. return decodeBigIntNoPtr, nil
  142. case kind == reflect.Ptr:
  143. return makePtrDecoder(typ, tags)
  144. case reflect.PtrTo(typ).Implements(decoderInterface):
  145. return decodeDecoder, nil
  146. case isUint(kind):
  147. return decodeUint, nil
  148. case kind == reflect.Bool:
  149. return decodeBool, nil
  150. case kind == reflect.String:
  151. return decodeString, nil
  152. case kind == reflect.Slice || kind == reflect.Array:
  153. return makeListDecoder(typ, tags)
  154. case kind == reflect.Struct:
  155. return makeStructDecoder(typ)
  156. case kind == reflect.Interface:
  157. return decodeInterface, nil
  158. default:
  159. return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
  160. }
  161. }
  162. func decodeRawValue(s *Stream, val reflect.Value) error {
  163. r, err := s.Raw()
  164. if err != nil {
  165. return err
  166. }
  167. val.SetBytes(r)
  168. return nil
  169. }
  170. func decodeUint(s *Stream, val reflect.Value) error {
  171. typ := val.Type()
  172. num, err := s.uint(typ.Bits())
  173. if err != nil {
  174. return wrapStreamError(err, val.Type())
  175. }
  176. val.SetUint(num)
  177. return nil
  178. }
  179. func decodeBool(s *Stream, val reflect.Value) error {
  180. b, err := s.Bool()
  181. if err != nil {
  182. return wrapStreamError(err, val.Type())
  183. }
  184. val.SetBool(b)
  185. return nil
  186. }
  187. func decodeString(s *Stream, val reflect.Value) error {
  188. b, err := s.Bytes()
  189. if err != nil {
  190. return wrapStreamError(err, val.Type())
  191. }
  192. val.SetString(string(b))
  193. return nil
  194. }
  195. func decodeBigIntNoPtr(s *Stream, val reflect.Value) error {
  196. return decodeBigInt(s, val.Addr())
  197. }
  198. func decodeBigInt(s *Stream, val reflect.Value) error {
  199. i := val.Interface().(*big.Int)
  200. if i == nil {
  201. i = new(big.Int)
  202. val.Set(reflect.ValueOf(i))
  203. }
  204. err := s.decodeBigInt(i)
  205. if err != nil {
  206. return wrapStreamError(err, val.Type())
  207. }
  208. return nil
  209. }
  210. func makeListDecoder(typ reflect.Type, tag rlpstruct.Tags) (decoder, error) {
  211. etype := typ.Elem()
  212. if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) {
  213. if typ.Kind() == reflect.Array {
  214. return decodeByteArray, nil
  215. }
  216. return decodeByteSlice, nil
  217. }
  218. etypeinfo := theTC.infoWhileGenerating(etype, rlpstruct.Tags{})
  219. if etypeinfo.decoderErr != nil {
  220. return nil, etypeinfo.decoderErr
  221. }
  222. var dec decoder
  223. switch {
  224. case typ.Kind() == reflect.Array:
  225. dec = func(s *Stream, val reflect.Value) error {
  226. return decodeListArray(s, val, etypeinfo.decoder)
  227. }
  228. case tag.Tail:
  229. // A slice with "tail" tag can occur as the last field
  230. // of a struct and is supposed to swallow all remaining
  231. // list elements. The struct decoder already called s.List,
  232. // proceed directly to decoding the elements.
  233. dec = func(s *Stream, val reflect.Value) error {
  234. return decodeSliceElems(s, val, etypeinfo.decoder)
  235. }
  236. default:
  237. dec = func(s *Stream, val reflect.Value) error {
  238. return decodeListSlice(s, val, etypeinfo.decoder)
  239. }
  240. }
  241. return dec, nil
  242. }
  243. func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error {
  244. size, err := s.List()
  245. if err != nil {
  246. return wrapStreamError(err, val.Type())
  247. }
  248. if size == 0 {
  249. val.Set(reflect.MakeSlice(val.Type(), 0, 0))
  250. return s.ListEnd()
  251. }
  252. if err := decodeSliceElems(s, val, elemdec); err != nil {
  253. return err
  254. }
  255. return s.ListEnd()
  256. }
  257. func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
  258. i := 0
  259. for ; ; i++ {
  260. // grow slice if necessary
  261. if i >= val.Cap() {
  262. newcap := val.Cap() + val.Cap()/2
  263. if newcap < 4 {
  264. newcap = 4
  265. }
  266. newv := reflect.MakeSlice(val.Type(), val.Len(), newcap)
  267. reflect.Copy(newv, val)
  268. val.Set(newv)
  269. }
  270. if i >= val.Len() {
  271. val.SetLen(i + 1)
  272. }
  273. // decode into element
  274. if err := elemdec(s, val.Index(i)); err == EOL {
  275. break
  276. } else if err != nil {
  277. return addErrorContext(err, fmt.Sprint("[", i, "]"))
  278. }
  279. }
  280. if i < val.Len() {
  281. val.SetLen(i)
  282. }
  283. return nil
  284. }
  285. func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
  286. if _, err := s.List(); err != nil {
  287. return wrapStreamError(err, val.Type())
  288. }
  289. vlen := val.Len()
  290. i := 0
  291. for ; i < vlen; i++ {
  292. if err := elemdec(s, val.Index(i)); err == EOL {
  293. break
  294. } else if err != nil {
  295. return addErrorContext(err, fmt.Sprint("[", i, "]"))
  296. }
  297. }
  298. if i < vlen {
  299. return &decodeError{msg: "input list has too few elements", typ: val.Type()}
  300. }
  301. return wrapStreamError(s.ListEnd(), val.Type())
  302. }
  303. func decodeByteSlice(s *Stream, val reflect.Value) error {
  304. b, err := s.Bytes()
  305. if err != nil {
  306. return wrapStreamError(err, val.Type())
  307. }
  308. val.SetBytes(b)
  309. return nil
  310. }
  311. func decodeByteArray(s *Stream, val reflect.Value) error {
  312. kind, size, err := s.Kind()
  313. if err != nil {
  314. return err
  315. }
  316. slice := byteArrayBytes(val, val.Len())
  317. switch kind {
  318. case Byte:
  319. if len(slice) == 0 {
  320. return &decodeError{msg: "input string too long", typ: val.Type()}
  321. } else if len(slice) > 1 {
  322. return &decodeError{msg: "input string too short", typ: val.Type()}
  323. }
  324. slice[0] = s.byteval
  325. s.kind = -1
  326. case String:
  327. if uint64(len(slice)) < size {
  328. return &decodeError{msg: "input string too long", typ: val.Type()}
  329. }
  330. if uint64(len(slice)) > size {
  331. return &decodeError{msg: "input string too short", typ: val.Type()}
  332. }
  333. if err := s.readFull(slice); err != nil {
  334. return err
  335. }
  336. // Reject cases where single byte encoding should have been used.
  337. if size == 1 && slice[0] < 128 {
  338. return wrapStreamError(ErrCanonSize, val.Type())
  339. }
  340. case List:
  341. return wrapStreamError(ErrExpectedString, val.Type())
  342. }
  343. return nil
  344. }
  345. func makeStructDecoder(typ reflect.Type) (decoder, error) {
  346. fields, err := structFields(typ)
  347. if err != nil {
  348. return nil, err
  349. }
  350. for _, f := range fields {
  351. if f.info.decoderErr != nil {
  352. return nil, structFieldError{typ, f.index, f.info.decoderErr}
  353. }
  354. }
  355. dec := func(s *Stream, val reflect.Value) (err error) {
  356. if _, err := s.List(); err != nil {
  357. return wrapStreamError(err, typ)
  358. }
  359. for i, f := range fields {
  360. err := f.info.decoder(s, val.Field(f.index))
  361. if err == EOL {
  362. if f.optional {
  363. // The field is optional, so reaching the end of the list before
  364. // reaching the last field is acceptable. All remaining undecoded
  365. // fields are zeroed.
  366. zeroFields(val, fields[i:])
  367. break
  368. }
  369. return &decodeError{msg: "too few elements", typ: typ}
  370. } else if err != nil {
  371. return addErrorContext(err, "."+typ.Field(f.index).Name)
  372. }
  373. }
  374. return wrapStreamError(s.ListEnd(), typ)
  375. }
  376. return dec, nil
  377. }
  378. func zeroFields(structval reflect.Value, fields []field) {
  379. for _, f := range fields {
  380. fv := structval.Field(f.index)
  381. fv.Set(reflect.Zero(fv.Type()))
  382. }
  383. }
  384. // makePtrDecoder creates a decoder that decodes into the pointer's element type.
  385. func makePtrDecoder(typ reflect.Type, tag rlpstruct.Tags) (decoder, error) {
  386. etype := typ.Elem()
  387. etypeinfo := theTC.infoWhileGenerating(etype, rlpstruct.Tags{})
  388. switch {
  389. case etypeinfo.decoderErr != nil:
  390. return nil, etypeinfo.decoderErr
  391. case !tag.NilOK:
  392. return makeSimplePtrDecoder(etype, etypeinfo), nil
  393. default:
  394. return makeNilPtrDecoder(etype, etypeinfo, tag), nil
  395. }
  396. }
  397. func makeSimplePtrDecoder(etype reflect.Type, etypeinfo *typeinfo) decoder {
  398. return func(s *Stream, val reflect.Value) (err error) {
  399. newval := val
  400. if val.IsNil() {
  401. newval = reflect.New(etype)
  402. }
  403. if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
  404. val.Set(newval)
  405. }
  406. return err
  407. }
  408. }
  409. // makeNilPtrDecoder creates a decoder that decodes empty values as nil. Non-empty
  410. // values are decoded into a value of the element type, just like makePtrDecoder does.
  411. //
  412. // This decoder is used for pointer-typed struct fields with struct tag "nil".
  413. func makeNilPtrDecoder(etype reflect.Type, etypeinfo *typeinfo, ts rlpstruct.Tags) decoder {
  414. typ := reflect.PtrTo(etype)
  415. nilPtr := reflect.Zero(typ)
  416. // Determine the value kind that results in nil pointer.
  417. nilKind := typeNilKind(etype, ts)
  418. return func(s *Stream, val reflect.Value) (err error) {
  419. kind, size, err := s.Kind()
  420. if err != nil {
  421. val.Set(nilPtr)
  422. return wrapStreamError(err, typ)
  423. }
  424. // Handle empty values as a nil pointer.
  425. if kind != Byte && size == 0 {
  426. if kind != nilKind {
  427. return &decodeError{
  428. msg: fmt.Sprintf("wrong kind of empty value (got %v, want %v)", kind, nilKind),
  429. typ: typ,
  430. }
  431. }
  432. // rearm s.Kind. This is important because the input
  433. // position must advance to the next value even though
  434. // we don't read anything.
  435. s.kind = -1
  436. val.Set(nilPtr)
  437. return nil
  438. }
  439. newval := val
  440. if val.IsNil() {
  441. newval = reflect.New(etype)
  442. }
  443. if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
  444. val.Set(newval)
  445. }
  446. return err
  447. }
  448. }
  449. var ifsliceType = reflect.TypeOf([]interface{}{})
  450. func decodeInterface(s *Stream, val reflect.Value) error {
  451. if val.Type().NumMethod() != 0 {
  452. return fmt.Errorf("rlp: type %v is not RLP-serializable", val.Type())
  453. }
  454. kind, _, err := s.Kind()
  455. if err != nil {
  456. return err
  457. }
  458. if kind == List {
  459. slice := reflect.New(ifsliceType).Elem()
  460. if err := decodeListSlice(s, slice, decodeInterface); err != nil {
  461. return err
  462. }
  463. val.Set(slice)
  464. } else {
  465. b, err := s.Bytes()
  466. if err != nil {
  467. return err
  468. }
  469. val.Set(reflect.ValueOf(b))
  470. }
  471. return nil
  472. }
  473. func decodeDecoder(s *Stream, val reflect.Value) error {
  474. return val.Addr().Interface().(Decoder).DecodeRLP(s)
  475. }
  476. // Kind represents the kind of value contained in an RLP stream.
  477. type Kind int8
  478. const (
  479. Byte Kind = iota
  480. String
  481. List
  482. )
  483. func (k Kind) String() string {
  484. switch k {
  485. case Byte:
  486. return "Byte"
  487. case String:
  488. return "String"
  489. case List:
  490. return "List"
  491. default:
  492. return fmt.Sprintf("Unknown(%d)", k)
  493. }
  494. }
  495. // ByteReader must be implemented by any input reader for a Stream. It
  496. // is implemented by e.g. bufio.Reader and bytes.Reader.
  497. type ByteReader interface {
  498. io.Reader
  499. io.ByteReader
  500. }
  501. // Stream can be used for piecemeal decoding of an input stream. This
  502. // is useful if the input is very large or if the decoding rules for a
  503. // type depend on the input structure. Stream does not keep an
  504. // internal buffer. After decoding a value, the input reader will be
  505. // positioned just before the type information for the next value.
  506. //
  507. // When decoding a list and the input position reaches the declared
  508. // length of the list, all operations will return error EOL.
  509. // The end of the list must be acknowledged using ListEnd to continue
  510. // reading the enclosing list.
  511. //
  512. // Stream is not safe for concurrent use.
  513. type Stream struct {
  514. r ByteReader
  515. remaining uint64 // number of bytes remaining to be read from r
  516. size uint64 // size of value ahead
  517. kinderr error // error from last readKind
  518. stack []uint64 // list sizes
  519. uintbuf [32]byte // auxiliary buffer for integer decoding
  520. kind Kind // kind of value ahead
  521. byteval byte // value of single byte in type tag
  522. limited bool // true if input limit is in effect
  523. }
  524. // NewStream creates a new decoding stream reading from r.
  525. //
  526. // If r implements the ByteReader interface, Stream will
  527. // not introduce any buffering.
  528. //
  529. // For non-toplevel values, Stream returns ErrElemTooLarge
  530. // for values that do not fit into the enclosing list.
  531. //
  532. // Stream supports an optional input limit. If a limit is set, the
  533. // size of any toplevel value will be checked against the remaining
  534. // input length. Stream operations that encounter a value exceeding
  535. // the remaining input length will return ErrValueTooLarge. The limit
  536. // can be set by passing a non-zero value for inputLimit.
  537. //
  538. // If r is a bytes.Reader or strings.Reader, the input limit is set to
  539. // the length of r's underlying data unless an explicit limit is
  540. // provided.
  541. func NewStream(r io.Reader, inputLimit uint64) *Stream {
  542. s := new(Stream)
  543. s.Reset(r, inputLimit)
  544. return s
  545. }
  546. // NewListStream creates a new stream that pretends to be positioned
  547. // at an encoded list of the given length.
  548. func NewListStream(r io.Reader, len uint64) *Stream {
  549. s := new(Stream)
  550. s.Reset(r, len)
  551. s.kind = List
  552. s.size = len
  553. return s
  554. }
  555. // Bytes reads an RLP string and returns its contents as a byte slice.
  556. // If the input does not contain an RLP string, the returned
  557. // error will be ErrExpectedString.
  558. func (s *Stream) Bytes() ([]byte, error) {
  559. kind, size, err := s.Kind()
  560. if err != nil {
  561. return nil, err
  562. }
  563. switch kind {
  564. case Byte:
  565. s.kind = -1 // rearm Kind
  566. return []byte{s.byteval}, nil
  567. case String:
  568. b := make([]byte, size)
  569. if err = s.readFull(b); err != nil {
  570. return nil, err
  571. }
  572. if size == 1 && b[0] < 128 {
  573. return nil, ErrCanonSize
  574. }
  575. return b, nil
  576. default:
  577. return nil, ErrExpectedString
  578. }
  579. }
  580. // ReadBytes decodes the next RLP value and stores the result in b.
  581. // The value size must match len(b) exactly.
  582. func (s *Stream) ReadBytes(b []byte) error {
  583. kind, size, err := s.Kind()
  584. if err != nil {
  585. return err
  586. }
  587. switch kind {
  588. case Byte:
  589. if len(b) != 1 {
  590. return fmt.Errorf("input value has wrong size 1, want %d", len(b))
  591. }
  592. b[0] = s.byteval
  593. s.kind = -1 // rearm Kind
  594. return nil
  595. case String:
  596. if uint64(len(b)) != size {
  597. return fmt.Errorf("input value has wrong size %d, want %d", size, len(b))
  598. }
  599. if err = s.readFull(b); err != nil {
  600. return err
  601. }
  602. if size == 1 && b[0] < 128 {
  603. return ErrCanonSize
  604. }
  605. return nil
  606. default:
  607. return ErrExpectedString
  608. }
  609. }
  610. // Raw reads a raw encoded value including RLP type information.
  611. func (s *Stream) Raw() ([]byte, error) {
  612. kind, size, err := s.Kind()
  613. if err != nil {
  614. return nil, err
  615. }
  616. if kind == Byte {
  617. s.kind = -1 // rearm Kind
  618. return []byte{s.byteval}, nil
  619. }
  620. // The original header has already been read and is no longer
  621. // available. Read content and put a new header in front of it.
  622. start := headsize(size)
  623. buf := make([]byte, uint64(start)+size)
  624. if err := s.readFull(buf[start:]); err != nil {
  625. return nil, err
  626. }
  627. if kind == String {
  628. puthead(buf, 0x80, 0xB7, size)
  629. } else {
  630. puthead(buf, 0xC0, 0xF7, size)
  631. }
  632. return buf, nil
  633. }
  634. // Uint reads an RLP string of up to 8 bytes and returns its contents
  635. // as an unsigned integer. If the input does not contain an RLP string, the
  636. // returned error will be ErrExpectedString.
  637. //
  638. // Deprecated: use s.Uint64 instead.
  639. func (s *Stream) Uint() (uint64, error) {
  640. return s.uint(64)
  641. }
  642. func (s *Stream) Uint64() (uint64, error) {
  643. return s.uint(64)
  644. }
  645. func (s *Stream) Uint32() (uint32, error) {
  646. i, err := s.uint(32)
  647. return uint32(i), err
  648. }
  649. func (s *Stream) Uint16() (uint16, error) {
  650. i, err := s.uint(16)
  651. return uint16(i), err
  652. }
  653. func (s *Stream) Uint8() (uint8, error) {
  654. i, err := s.uint(8)
  655. return uint8(i), err
  656. }
  657. func (s *Stream) uint(maxbits int) (uint64, error) {
  658. kind, size, err := s.Kind()
  659. if err != nil {
  660. return 0, err
  661. }
  662. switch kind {
  663. case Byte:
  664. if s.byteval == 0 {
  665. return 0, ErrCanonInt
  666. }
  667. s.kind = -1 // rearm Kind
  668. return uint64(s.byteval), nil
  669. case String:
  670. if size > uint64(maxbits/8) {
  671. return 0, errUintOverflow
  672. }
  673. v, err := s.readUint(byte(size))
  674. switch {
  675. case err == ErrCanonSize:
  676. // Adjust error because we're not reading a size right now.
  677. return 0, ErrCanonInt
  678. case err != nil:
  679. return 0, err
  680. case size > 0 && v < 128:
  681. return 0, ErrCanonSize
  682. default:
  683. return v, nil
  684. }
  685. default:
  686. return 0, ErrExpectedString
  687. }
  688. }
  689. // Bool reads an RLP string of up to 1 byte and returns its contents
  690. // as a boolean. If the input does not contain an RLP string, the
  691. // returned error will be ErrExpectedString.
  692. func (s *Stream) Bool() (bool, error) {
  693. num, err := s.uint(8)
  694. if err != nil {
  695. return false, err
  696. }
  697. switch num {
  698. case 0:
  699. return false, nil
  700. case 1:
  701. return true, nil
  702. default:
  703. return false, fmt.Errorf("rlp: invalid boolean value: %d", num)
  704. }
  705. }
  706. // List starts decoding an RLP list. If the input does not contain a
  707. // list, the returned error will be ErrExpectedList. When the list's
  708. // end has been reached, any Stream operation will return EOL.
  709. func (s *Stream) List() (size uint64, err error) {
  710. kind, size, err := s.Kind()
  711. if err != nil {
  712. return 0, err
  713. }
  714. if kind != List {
  715. return 0, ErrExpectedList
  716. }
  717. // Remove size of inner list from outer list before pushing the new size
  718. // onto the stack. This ensures that the remaining outer list size will
  719. // be correct after the matching call to ListEnd.
  720. if inList, limit := s.listLimit(); inList {
  721. s.stack[len(s.stack)-1] = limit - size
  722. }
  723. s.stack = append(s.stack, size)
  724. s.kind = -1
  725. s.size = 0
  726. return size, nil
  727. }
  728. // ListEnd returns to the enclosing list.
  729. // The input reader must be positioned at the end of a list.
  730. func (s *Stream) ListEnd() error {
  731. // Ensure that no more data is remaining in the current list.
  732. if inList, listLimit := s.listLimit(); !inList {
  733. return errNotInList
  734. } else if listLimit > 0 {
  735. return errNotAtEOL
  736. }
  737. s.stack = s.stack[:len(s.stack)-1] // pop
  738. s.kind = -1
  739. s.size = 0
  740. return nil
  741. }
  742. // MoreDataInList reports whether the current list context contains
  743. // more data to be read.
  744. func (s *Stream) MoreDataInList() bool {
  745. _, listLimit := s.listLimit()
  746. return listLimit > 0
  747. }
  748. // BigInt decodes an arbitrary-size integer value.
  749. func (s *Stream) BigInt() (*big.Int, error) {
  750. i := new(big.Int)
  751. if err := s.decodeBigInt(i); err != nil {
  752. return nil, err
  753. }
  754. return i, nil
  755. }
  756. func (s *Stream) decodeBigInt(dst *big.Int) error {
  757. var buffer []byte
  758. kind, size, err := s.Kind()
  759. switch {
  760. case err != nil:
  761. return err
  762. case kind == List:
  763. return ErrExpectedString
  764. case kind == Byte:
  765. buffer = s.uintbuf[:1]
  766. buffer[0] = s.byteval
  767. s.kind = -1 // re-arm Kind
  768. case size == 0:
  769. // Avoid zero-length read.
  770. s.kind = -1
  771. case size <= uint64(len(s.uintbuf)):
  772. // For integers smaller than s.uintbuf, allocating a buffer
  773. // can be avoided.
  774. buffer = s.uintbuf[:size]
  775. if err := s.readFull(buffer); err != nil {
  776. return err
  777. }
  778. // Reject inputs where single byte encoding should have been used.
  779. if size == 1 && buffer[0] < 128 {
  780. return ErrCanonSize
  781. }
  782. default:
  783. // For large integers, a temporary buffer is needed.
  784. buffer = make([]byte, size)
  785. if err := s.readFull(buffer); err != nil {
  786. return err
  787. }
  788. }
  789. // Reject leading zero bytes.
  790. if len(buffer) > 0 && buffer[0] == 0 {
  791. return ErrCanonInt
  792. }
  793. // Set the integer bytes.
  794. dst.SetBytes(buffer)
  795. return nil
  796. }
  797. // Decode decodes a value and stores the result in the value pointed
  798. // to by val. Please see the documentation for the Decode function
  799. // to learn about the decoding rules.
  800. func (s *Stream) Decode(val interface{}) error {
  801. if val == nil {
  802. return errDecodeIntoNil
  803. }
  804. rval := reflect.ValueOf(val)
  805. rtyp := rval.Type()
  806. if rtyp.Kind() != reflect.Ptr {
  807. return errNoPointer
  808. }
  809. if rval.IsNil() {
  810. return errDecodeIntoNil
  811. }
  812. decoder, err := cachedDecoder(rtyp.Elem())
  813. if err != nil {
  814. return err
  815. }
  816. err = decoder(s, rval.Elem())
  817. if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 {
  818. // Add decode target type to error so context has more meaning.
  819. decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")"))
  820. }
  821. return err
  822. }
  823. // Reset discards any information about the current decoding context
  824. // and starts reading from r. This method is meant to facilitate reuse
  825. // of a preallocated Stream across many decoding operations.
  826. //
  827. // If r does not also implement ByteReader, Stream will do its own
  828. // buffering.
  829. func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
  830. if inputLimit > 0 {
  831. s.remaining = inputLimit
  832. s.limited = true
  833. } else {
  834. // Attempt to automatically discover
  835. // the limit when reading from a byte slice.
  836. switch br := r.(type) {
  837. case *bytes.Reader:
  838. s.remaining = uint64(br.Len())
  839. s.limited = true
  840. case *bytes.Buffer:
  841. s.remaining = uint64(br.Len())
  842. s.limited = true
  843. case *strings.Reader:
  844. s.remaining = uint64(br.Len())
  845. s.limited = true
  846. default:
  847. s.limited = false
  848. }
  849. }
  850. // Wrap r with a buffer if it doesn't have one.
  851. bufr, ok := r.(ByteReader)
  852. if !ok {
  853. bufr = bufio.NewReader(r)
  854. }
  855. s.r = bufr
  856. // Reset the decoding context.
  857. s.stack = s.stack[:0]
  858. s.size = 0
  859. s.kind = -1
  860. s.kinderr = nil
  861. s.byteval = 0
  862. s.uintbuf = [32]byte{}
  863. }
  864. // Kind returns the kind and size of the next value in the
  865. // input stream.
  866. //
  867. // The returned size is the number of bytes that make up the value.
  868. // For kind == Byte, the size is zero because the value is
  869. // contained in the type tag.
  870. //
  871. // The first call to Kind will read size information from the input
  872. // reader and leave it positioned at the start of the actual bytes of
  873. // the value. Subsequent calls to Kind (until the value is decoded)
  874. // will not advance the input reader and return cached information.
  875. func (s *Stream) Kind() (kind Kind, size uint64, err error) {
  876. if s.kind >= 0 {
  877. return s.kind, s.size, s.kinderr
  878. }
  879. // Check for end of list. This needs to be done here because readKind
  880. // checks against the list size, and would return the wrong error.
  881. inList, listLimit := s.listLimit()
  882. if inList && listLimit == 0 {
  883. return 0, 0, EOL
  884. }
  885. // Read the actual size tag.
  886. s.kind, s.size, s.kinderr = s.readKind()
  887. if s.kinderr == nil {
  888. // Check the data size of the value ahead against input limits. This
  889. // is done here because many decoders require allocating an input
  890. // buffer matching the value size. Checking it here protects those
  891. // decoders from inputs declaring very large value size.
  892. if inList && s.size > listLimit {
  893. s.kinderr = ErrElemTooLarge
  894. } else if s.limited && s.size > s.remaining {
  895. s.kinderr = ErrValueTooLarge
  896. }
  897. }
  898. return s.kind, s.size, s.kinderr
  899. }
  900. func (s *Stream) readKind() (kind Kind, size uint64, err error) {
  901. b, err := s.readByte()
  902. if err != nil {
  903. if len(s.stack) == 0 {
  904. // At toplevel, Adjust the error to actual EOF. io.EOF is
  905. // used by callers to determine when to stop decoding.
  906. switch err {
  907. case io.ErrUnexpectedEOF:
  908. err = io.EOF
  909. case ErrValueTooLarge:
  910. err = io.EOF
  911. }
  912. }
  913. return 0, 0, err
  914. }
  915. s.byteval = 0
  916. switch {
  917. case b < 0x80:
  918. // For a single byte whose value is in the [0x00, 0x7F] range, that byte
  919. // is its own RLP encoding.
  920. s.byteval = b
  921. return Byte, 0, nil
  922. case b < 0xB8:
  923. // Otherwise, if a string is 0-55 bytes long, the RLP encoding consists
  924. // of a single byte with value 0x80 plus the length of the string
  925. // followed by the string. The range of the first byte is thus [0x80, 0xB7].
  926. return String, uint64(b - 0x80), nil
  927. case b < 0xC0:
  928. // If a string is more than 55 bytes long, the RLP encoding consists of a
  929. // single byte with value 0xB7 plus the length of the length of the
  930. // string in binary form, followed by the length of the string, followed
  931. // by the string. For example, a length-1024 string would be encoded as
  932. // 0xB90400 followed by the string. The range of the first byte is thus
  933. // [0xB8, 0xBF].
  934. size, err = s.readUint(b - 0xB7)
  935. if err == nil && size < 56 {
  936. err = ErrCanonSize
  937. }
  938. return String, size, err
  939. case b < 0xF8:
  940. // If the total payload of a list (i.e. the combined length of all its
  941. // items) is 0-55 bytes long, the RLP encoding consists of a single byte
  942. // with value 0xC0 plus the length of the list followed by the
  943. // concatenation of the RLP encodings of the items. The range of the
  944. // first byte is thus [0xC0, 0xF7].
  945. return List, uint64(b - 0xC0), nil
  946. default:
  947. // If the total payload of a list is more than 55 bytes long, the RLP
  948. // encoding consists of a single byte with value 0xF7 plus the length of
  949. // the length of the payload in binary form, followed by the length of
  950. // the payload, followed by the concatenation of the RLP encodings of
  951. // the items. The range of the first byte is thus [0xF8, 0xFF].
  952. size, err = s.readUint(b - 0xF7)
  953. if err == nil && size < 56 {
  954. err = ErrCanonSize
  955. }
  956. return List, size, err
  957. }
  958. }
  959. func (s *Stream) readUint(size byte) (uint64, error) {
  960. switch size {
  961. case 0:
  962. s.kind = -1 // rearm Kind
  963. return 0, nil
  964. case 1:
  965. b, err := s.readByte()
  966. return uint64(b), err
  967. default:
  968. buffer := s.uintbuf[:8]
  969. for i := range buffer {
  970. buffer[i] = 0
  971. }
  972. start := int(8 - size)
  973. if err := s.readFull(buffer[start:]); err != nil {
  974. return 0, err
  975. }
  976. if buffer[start] == 0 {
  977. // Note: readUint is also used to decode integer values.
  978. // The error needs to be adjusted to become ErrCanonInt in this case.
  979. return 0, ErrCanonSize
  980. }
  981. return binary.BigEndian.Uint64(buffer[:]), nil
  982. }
  983. }
  984. // readFull reads into buf from the underlying stream.
  985. func (s *Stream) readFull(buf []byte) (err error) {
  986. if err := s.willRead(uint64(len(buf))); err != nil {
  987. return err
  988. }
  989. var nn, n int
  990. for n < len(buf) && err == nil {
  991. nn, err = s.r.Read(buf[n:])
  992. n += nn
  993. }
  994. if err == io.EOF {
  995. if n < len(buf) {
  996. err = io.ErrUnexpectedEOF
  997. } else {
  998. // Readers are allowed to give EOF even though the read succeeded.
  999. // In such cases, we discard the EOF, like io.ReadFull() does.
  1000. err = nil
  1001. }
  1002. }
  1003. return err
  1004. }
  1005. // readByte reads a single byte from the underlying stream.
  1006. func (s *Stream) readByte() (byte, error) {
  1007. if err := s.willRead(1); err != nil {
  1008. return 0, err
  1009. }
  1010. b, err := s.r.ReadByte()
  1011. if err == io.EOF {
  1012. err = io.ErrUnexpectedEOF
  1013. }
  1014. return b, err
  1015. }
  1016. // willRead is called before any read from the underlying stream. It checks
  1017. // n against size limits, and updates the limits if n doesn't overflow them.
  1018. func (s *Stream) willRead(n uint64) error {
  1019. s.kind = -1 // rearm Kind
  1020. if inList, limit := s.listLimit(); inList {
  1021. if n > limit {
  1022. return ErrElemTooLarge
  1023. }
  1024. s.stack[len(s.stack)-1] = limit - n
  1025. }
  1026. if s.limited {
  1027. if n > s.remaining {
  1028. return ErrValueTooLarge
  1029. }
  1030. s.remaining -= n
  1031. }
  1032. return nil
  1033. }
  1034. // listLimit returns the amount of data remaining in the innermost list.
  1035. func (s *Stream) listLimit() (inList bool, limit uint64) {
  1036. if len(s.stack) == 0 {
  1037. return false, 0
  1038. }
  1039. return true, s.stack[len(s.stack)-1]
  1040. }