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