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