github.com/theQRL/go-zond@v0.1.1/rlp/decode.go (about)

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