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