github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/rlp/decode.go (about)

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