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