github.com/klaytn/klaytn@v1.12.1/rlp/decode.go (about)

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