2015-07-07 02:54:22 +02:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 18:48:40 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 02:54:22 +02:00
|
|
|
//
|
2015-07-23 18:35:11 +02:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 02:54:22 +02:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 18:48:40 +02:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 02:54:22 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 18:48:40 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 02:54:22 +02:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 18:48:40 +02:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 02:54:22 +02:00
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
package rlp
|
|
|
|
|
|
|
|
import (
|
2014-11-24 19:01:25 +01:00
|
|
|
"bufio"
|
2015-03-18 13:17:39 +01:00
|
|
|
"bytes"
|
2014-11-13 20:31:48 +01:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2015-03-23 15:02:55 +01:00
|
|
|
"strings"
|
2014-11-13 20:31:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-08 02:48:07 -07:00
|
|
|
// EOL is returned when the end of the current list
|
|
|
|
// has been reached during streaming.
|
|
|
|
EOL = errors.New("rlp: end of list")
|
|
|
|
|
|
|
|
// Actual Errors
|
|
|
|
ErrExpectedString = errors.New("rlp: expected String or Byte")
|
|
|
|
ErrExpectedList = errors.New("rlp: expected List")
|
|
|
|
ErrCanonInt = errors.New("rlp: non-canonical integer format")
|
|
|
|
ErrCanonSize = errors.New("rlp: non-canonical size information")
|
|
|
|
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
|
|
|
|
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
|
|
|
|
ErrMoreThanOneValue = errors.New("rlp: input contains more than one value")
|
|
|
|
|
|
|
|
// internal errors
|
|
|
|
errNotInList = errors.New("rlp: call of ListEnd outside of any list")
|
|
|
|
errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL")
|
|
|
|
errUintOverflow = errors.New("rlp: uint overflow")
|
2014-11-13 20:31:48 +01:00
|
|
|
errNoPointer = errors.New("rlp: interface given to Decode must be a pointer")
|
|
|
|
errDecodeIntoNil = errors.New("rlp: pointer given to Decode must not be nil")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Decoder is implemented by types that require custom RLP
|
|
|
|
// decoding rules or need to decode into private fields.
|
|
|
|
//
|
|
|
|
// The DecodeRLP method should read one value from the given
|
|
|
|
// Stream. It is not forbidden to read less or more, but it might
|
|
|
|
// be confusing.
|
|
|
|
type Decoder interface {
|
|
|
|
DecodeRLP(*Stream) error
|
|
|
|
}
|
|
|
|
|
2014-11-24 19:01:25 +01:00
|
|
|
// Decode parses RLP-encoded data from r and stores the result in the
|
|
|
|
// value pointed to by val. Val must be a non-nil pointer. If r does
|
|
|
|
// not implement ByteReader, Decode will do its own buffering.
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
|
|
|
// Decode uses the following type-dependent decoding rules:
|
|
|
|
//
|
|
|
|
// If the type implements the Decoder interface, decode calls
|
|
|
|
// DecodeRLP.
|
|
|
|
//
|
2015-04-17 01:16:46 +02:00
|
|
|
// To decode into a pointer, Decode will decode into the value pointed
|
|
|
|
// to. If the pointer is nil, a new value of the pointer's element
|
|
|
|
// type is allocated. If the pointer is non-nil, the existing value
|
2017-06-12 14:45:17 +02:00
|
|
|
// will be reused.
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
|
|
|
// To decode into a struct, Decode expects the input to be an RLP
|
|
|
|
// list. The decoded elements of the list are assigned to each public
|
2015-04-17 01:16:46 +02:00
|
|
|
// field in the order given by the struct's definition. The input list
|
|
|
|
// must contain an element for each decoded field. Decode returns an
|
|
|
|
// error if there are too few or too many elements.
|
|
|
|
//
|
2017-03-07 12:37:53 +01:00
|
|
|
// The decoding of struct fields honours certain struct tags, "tail",
|
|
|
|
// "nil" and "-".
|
|
|
|
//
|
|
|
|
// The "-" tag ignores fields.
|
|
|
|
//
|
|
|
|
// For an explanation of "tail", see the example.
|
|
|
|
//
|
|
|
|
// The "nil" tag applies to pointer-typed fields and changes the decoding
|
|
|
|
// rules for the field such that input values of size zero decode as a nil
|
|
|
|
// pointer. This tag can be useful when decoding recursive types.
|
2015-04-17 01:16:46 +02:00
|
|
|
//
|
|
|
|
// type StructWithEmptyOK struct {
|
|
|
|
// Foo *[20]byte `rlp:"nil"`
|
|
|
|
// }
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
|
|
|
// To decode into a slice, the input must be a list and the resulting
|
2015-04-17 02:01:38 +02:00
|
|
|
// slice will contain the input elements in order. For byte slices,
|
2015-04-17 03:11:24 +02:00
|
|
|
// the input must be an RLP string. Array types decode similarly, with
|
|
|
|
// the additional restriction that the number of input elements (or
|
|
|
|
// bytes) must match the array's length.
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
|
|
|
// To decode into a Go string, the input must be an RLP string. The
|
2015-04-17 01:16:46 +02:00
|
|
|
// input bytes are taken as-is and will not necessarily be valid UTF-8.
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
2014-12-09 10:41:47 +01:00
|
|
|
// To decode into an unsigned integer type, the input must also be an RLP
|
2014-11-13 20:31:48 +01:00
|
|
|
// string. The bytes are interpreted as a big endian representation of
|
|
|
|
// the integer. If the RLP string is larger than the bit size of the
|
|
|
|
// type, Decode will return an error. Decode also supports *big.Int.
|
|
|
|
// There is no size limit for big integers.
|
|
|
|
//
|
|
|
|
// To decode into an interface value, Decode stores one of these
|
|
|
|
// in the value:
|
|
|
|
//
|
2015-04-17 01:16:46 +02:00
|
|
|
// []interface{}, for RLP lists
|
|
|
|
// []byte, for RLP strings
|
2014-11-13 20:31:48 +01:00
|
|
|
//
|
2014-12-09 10:41:47 +01:00
|
|
|
// Non-empty interface types are not supported, nor are booleans,
|
|
|
|
// signed integers, floating point numbers, maps, channels and
|
|
|
|
// functions.
|
2015-03-23 15:02:55 +01:00
|
|
|
//
|
|
|
|
// Note that Decode does not set an input limit for all readers
|
|
|
|
// and may be vulnerable to panics cause by huge value sizes. If
|
|
|
|
// you need an input limit, use
|
|
|
|
//
|
|
|
|
// NewStream(r, limit).Decode(val)
|
2014-11-24 19:01:25 +01:00
|
|
|
func Decode(r io.Reader, val interface{}) error {
|
2015-03-23 15:02:55 +01:00
|
|
|
// TODO: this could use a Stream from a pool.
|
|
|
|
return NewStream(r, 0).Decode(val)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:17:39 +01:00
|
|
|
// DecodeBytes parses RLP data from b into val.
|
|
|
|
// Please see the documentation of Decode for the decoding rules.
|
2015-07-18 01:47:17 +02:00
|
|
|
// The input must contain exactly one value and no trailing data.
|
2015-03-18 13:17:39 +01:00
|
|
|
func DecodeBytes(b []byte, val interface{}) error {
|
2015-03-23 15:02:55 +01:00
|
|
|
// TODO: this could use a Stream from a pool.
|
2015-07-18 01:47:17 +02:00
|
|
|
r := bytes.NewReader(b)
|
|
|
|
if err := NewStream(r, uint64(len(b))).Decode(val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.Len() > 0 {
|
|
|
|
return ErrMoreThanOneValue
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-18 13:17:39 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 16:00:48 +01:00
|
|
|
type decodeError struct {
|
|
|
|
msg string
|
|
|
|
typ reflect.Type
|
2015-01-05 17:10:42 +01:00
|
|
|
ctx []string
|
2014-11-25 16:00:48 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 17:10:42 +01:00
|
|
|
func (err *decodeError) Error() string {
|
|
|
|
ctx := ""
|
|
|
|
if len(err.ctx) > 0 {
|
|
|
|
ctx = ", decoding into "
|
|
|
|
for i := len(err.ctx) - 1; i >= 0; i-- {
|
|
|
|
ctx += err.ctx[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("rlp: %s for %v%s", err.msg, err.typ, ctx)
|
2014-11-25 16:00:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 13:55:47 +01:00
|
|
|
func wrapStreamError(err error, typ reflect.Type) error {
|
|
|
|
switch err {
|
2015-04-04 21:29:23 +02:00
|
|
|
case ErrCanonInt:
|
2015-04-14 16:09:06 +02:00
|
|
|
return &decodeError{msg: "non-canonical integer (leading zero bytes)", typ: typ}
|
|
|
|
case ErrCanonSize:
|
|
|
|
return &decodeError{msg: "non-canonical size information", typ: typ}
|
2014-12-09 13:55:47 +01:00
|
|
|
case ErrExpectedList:
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "expected input list", typ: typ}
|
2014-12-09 13:55:47 +01:00
|
|
|
case ErrExpectedString:
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "expected input string or byte", typ: typ}
|
2014-12-09 13:55:47 +01:00
|
|
|
case errUintOverflow:
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "input string too long", typ: typ}
|
2014-12-09 13:55:47 +01:00
|
|
|
case errNotAtEOL:
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "input list has too many elements", typ: typ}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func addErrorContext(err error, ctx string) error {
|
|
|
|
if decErr, ok := err.(*decodeError); ok {
|
|
|
|
decErr.ctx = append(decErr.ctx, ctx)
|
2014-12-09 13:55:47 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-09 10:39:39 +01:00
|
|
|
var (
|
|
|
|
decoderInterface = reflect.TypeOf(new(Decoder)).Elem()
|
|
|
|
bigInt = reflect.TypeOf(big.Int{})
|
|
|
|
)
|
|
|
|
|
2015-04-17 01:16:46 +02:00
|
|
|
func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
|
2014-12-09 10:39:39 +01:00
|
|
|
kind := typ.Kind()
|
|
|
|
switch {
|
2015-09-09 03:34:15 +02:00
|
|
|
case typ == rawValueType:
|
|
|
|
return decodeRawValue, nil
|
2014-12-09 10:39:39 +01:00
|
|
|
case typ.Implements(decoderInterface):
|
|
|
|
return decodeDecoder, nil
|
|
|
|
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface):
|
|
|
|
return decodeDecoderNoPtr, nil
|
|
|
|
case typ.AssignableTo(reflect.PtrTo(bigInt)):
|
|
|
|
return decodeBigInt, nil
|
|
|
|
case typ.AssignableTo(bigInt):
|
|
|
|
return decodeBigIntNoPtr, nil
|
2014-12-09 10:41:47 +01:00
|
|
|
case isUint(kind):
|
|
|
|
return decodeUint, nil
|
2015-08-13 11:12:38 +03:00
|
|
|
case kind == reflect.Bool:
|
|
|
|
return decodeBool, nil
|
2014-12-09 10:39:39 +01:00
|
|
|
case kind == reflect.String:
|
|
|
|
return decodeString, nil
|
|
|
|
case kind == reflect.Slice || kind == reflect.Array:
|
2015-12-21 21:05:20 +01:00
|
|
|
return makeListDecoder(typ, tags)
|
2014-12-09 10:39:39 +01:00
|
|
|
case kind == reflect.Struct:
|
|
|
|
return makeStructDecoder(typ)
|
|
|
|
case kind == reflect.Ptr:
|
2015-04-17 01:16:46 +02:00
|
|
|
if tags.nilOK {
|
|
|
|
return makeOptionalPtrDecoder(typ)
|
|
|
|
}
|
2014-12-09 10:39:39 +01:00
|
|
|
return makePtrDecoder(typ)
|
2015-01-15 23:21:41 +01:00
|
|
|
case kind == reflect.Interface:
|
2014-12-09 10:39:39 +01:00
|
|
|
return decodeInterface, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 03:34:15 +02:00
|
|
|
func decodeRawValue(s *Stream, val reflect.Value) error {
|
|
|
|
r, err := s.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val.SetBytes(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
func decodeUint(s *Stream, val reflect.Value) error {
|
2014-11-25 16:00:48 +01:00
|
|
|
typ := val.Type()
|
|
|
|
num, err := s.uint(typ.Bits())
|
2014-12-09 13:55:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
val.SetUint(num)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-13 11:12:38 +03:00
|
|
|
func decodeBool(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bool()
|
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
|
|
|
}
|
|
|
|
val.SetBool(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
func decodeString(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bytes()
|
|
|
|
if err != nil {
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
val.SetString(string(b))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBigIntNoPtr(s *Stream, val reflect.Value) error {
|
|
|
|
return decodeBigInt(s, val.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBigInt(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bytes()
|
|
|
|
if err != nil {
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
i := val.Interface().(*big.Int)
|
|
|
|
if i == nil {
|
|
|
|
i = new(big.Int)
|
|
|
|
val.Set(reflect.ValueOf(i))
|
|
|
|
}
|
2015-04-14 16:09:06 +02:00
|
|
|
// Reject leading zero bytes
|
2015-04-04 21:29:23 +02:00
|
|
|
if len(b) > 0 && b[0] == 0 {
|
|
|
|
return wrapStreamError(ErrCanonInt, val.Type())
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
i.SetBytes(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-21 21:05:20 +01:00
|
|
|
func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
|
2014-11-13 20:31:48 +01:00
|
|
|
etype := typ.Elem()
|
|
|
|
if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) {
|
|
|
|
if typ.Kind() == reflect.Array {
|
|
|
|
return decodeByteArray, nil
|
|
|
|
}
|
2018-05-08 02:48:07 -07:00
|
|
|
return decodeByteSlice, nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-17 01:16:46 +02:00
|
|
|
etypeinfo, err := cachedTypeInfo1(etype, tags{})
|
2014-11-13 20:31:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-21 21:05:20 +01:00
|
|
|
var dec decoder
|
|
|
|
switch {
|
|
|
|
case typ.Kind() == reflect.Array:
|
|
|
|
dec = func(s *Stream, val reflect.Value) error {
|
2015-01-05 17:10:42 +01:00
|
|
|
return decodeListArray(s, val, etypeinfo.decoder)
|
2015-12-21 21:05:20 +01:00
|
|
|
}
|
|
|
|
case tag.tail:
|
|
|
|
// A slice with "tail" tag can occur as the last field
|
2017-06-12 14:45:17 +02:00
|
|
|
// of a struct and is supposed to swallow all remaining
|
2015-12-21 21:05:20 +01:00
|
|
|
// list elements. The struct decoder already called s.List,
|
|
|
|
// proceed directly to decoding the elements.
|
|
|
|
dec = func(s *Stream, val reflect.Value) error {
|
|
|
|
return decodeSliceElems(s, val, etypeinfo.decoder)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
dec = func(s *Stream, val reflect.Value) error {
|
2015-01-05 17:10:42 +01:00
|
|
|
return decodeListSlice(s, val, etypeinfo.decoder)
|
|
|
|
}
|
2015-12-21 21:05:20 +01:00
|
|
|
}
|
|
|
|
return dec, nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 10:44:48 +01:00
|
|
|
func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error {
|
2014-11-13 20:31:48 +01:00
|
|
|
size, err := s.List()
|
|
|
|
if err != nil {
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
if size == 0 {
|
2014-12-09 10:44:48 +01:00
|
|
|
val.Set(reflect.MakeSlice(val.Type(), 0, 0))
|
2014-11-13 20:31:48 +01:00
|
|
|
return s.ListEnd()
|
|
|
|
}
|
2015-12-21 21:05:20 +01:00
|
|
|
if err := decodeSliceElems(s, val, elemdec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.ListEnd()
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
|
2015-12-21 21:05:20 +01:00
|
|
|
func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
|
2014-11-13 20:31:48 +01:00
|
|
|
i := 0
|
2014-12-09 10:44:48 +01:00
|
|
|
for ; ; i++ {
|
|
|
|
// grow slice if necessary
|
|
|
|
if i >= val.Cap() {
|
|
|
|
newcap := val.Cap() + val.Cap()/2
|
|
|
|
if newcap < 4 {
|
|
|
|
newcap = 4
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2014-12-09 10:44:48 +01:00
|
|
|
newv := reflect.MakeSlice(val.Type(), val.Len(), newcap)
|
|
|
|
reflect.Copy(newv, val)
|
|
|
|
val.Set(newv)
|
|
|
|
}
|
|
|
|
if i >= val.Len() {
|
|
|
|
val.SetLen(i + 1)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
// decode into element
|
|
|
|
if err := elemdec(s, val.Index(i)); err == EOL {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-01-05 17:10:42 +01:00
|
|
|
return addErrorContext(err, fmt.Sprint("[", i, "]"))
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < val.Len() {
|
2014-12-09 10:44:48 +01:00
|
|
|
val.SetLen(i)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-12-21 21:05:20 +01:00
|
|
|
return nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 10:44:48 +01:00
|
|
|
func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
|
2015-12-21 21:05:20 +01:00
|
|
|
if _, err := s.List(); err != nil {
|
2015-04-14 16:09:06 +02:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-12-09 10:44:48 +01:00
|
|
|
}
|
|
|
|
vlen := val.Len()
|
|
|
|
i := 0
|
|
|
|
for ; i < vlen; i++ {
|
|
|
|
if err := elemdec(s, val.Index(i)); err == EOL {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-01-05 17:10:42 +01:00
|
|
|
return addErrorContext(err, fmt.Sprint("[", i, "]"))
|
2014-12-09 10:44:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < vlen {
|
2015-04-17 03:11:24 +02:00
|
|
|
return &decodeError{msg: "input list has too few elements", typ: val.Type()}
|
2014-12-09 10:44:48 +01:00
|
|
|
}
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(s.ListEnd(), val.Type())
|
2014-12-09 10:44:48 +01:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
func decodeByteSlice(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bytes()
|
2015-04-16 10:15:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-16 10:15:14 +02:00
|
|
|
val.SetBytes(b)
|
|
|
|
return nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func decodeByteArray(s *Stream, val reflect.Value) error {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-17 03:11:24 +02:00
|
|
|
vlen := val.Len()
|
2014-11-13 20:31:48 +01:00
|
|
|
switch kind {
|
|
|
|
case Byte:
|
2015-04-17 03:11:24 +02:00
|
|
|
if vlen == 0 {
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "input string too long", typ: val.Type()}
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-17 03:11:24 +02:00
|
|
|
if vlen > 1 {
|
|
|
|
return &decodeError{msg: "input string too short", typ: val.Type()}
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
bv, _ := s.Uint()
|
|
|
|
val.Index(0).SetUint(bv)
|
|
|
|
case String:
|
2015-04-17 03:11:24 +02:00
|
|
|
if uint64(vlen) < size {
|
2015-01-05 17:10:42 +01:00
|
|
|
return &decodeError{msg: "input string too long", typ: val.Type()}
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-17 03:11:24 +02:00
|
|
|
if uint64(vlen) > size {
|
|
|
|
return &decodeError{msg: "input string too short", typ: val.Type()}
|
|
|
|
}
|
|
|
|
slice := val.Slice(0, vlen).Interface().([]byte)
|
2014-11-13 20:31:48 +01:00
|
|
|
if err := s.readFull(slice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 10:15:14 +02:00
|
|
|
// Reject cases where single byte encoding should have been used.
|
2015-07-18 16:13:20 +02:00
|
|
|
if size == 1 && slice[0] < 128 {
|
2015-04-16 10:15:14 +02:00
|
|
|
return wrapStreamError(ErrCanonSize, val.Type())
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
case List:
|
2015-04-17 02:01:38 +02:00
|
|
|
return wrapStreamError(ErrExpectedString, val.Type())
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStructDecoder(typ reflect.Type) (decoder, error) {
|
2014-12-30 11:40:32 +01:00
|
|
|
fields, err := structFields(typ)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
dec := func(s *Stream, val reflect.Value) (err error) {
|
2015-12-21 21:05:20 +01:00
|
|
|
if _, err := s.List(); err != nil {
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(err, typ)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
for _, f := range fields {
|
2015-12-21 21:05:20 +01:00
|
|
|
err := f.info.decoder(s, val.Field(f.index))
|
2014-11-13 20:31:48 +01:00
|
|
|
if err == EOL {
|
2015-04-17 01:16:46 +02:00
|
|
|
return &decodeError{msg: "too few elements", typ: typ}
|
2014-11-13 20:31:48 +01:00
|
|
|
} else if err != nil {
|
2015-01-05 17:10:42 +01:00
|
|
|
return addErrorContext(err, "."+typ.Field(f.index).Name)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 13:55:47 +01:00
|
|
|
return wrapStreamError(s.ListEnd(), typ)
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2015-04-17 01:16:46 +02:00
|
|
|
// makePtrDecoder creates a decoder that decodes into
|
|
|
|
// the pointer's element type.
|
2014-11-13 20:31:48 +01:00
|
|
|
func makePtrDecoder(typ reflect.Type) (decoder, error) {
|
|
|
|
etype := typ.Elem()
|
2015-04-17 01:16:46 +02:00
|
|
|
etypeinfo, err := cachedTypeInfo1(etype, tags{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dec := func(s *Stream, val reflect.Value) (err error) {
|
|
|
|
newval := val
|
|
|
|
if val.IsNil() {
|
|
|
|
newval = reflect.New(etype)
|
|
|
|
}
|
|
|
|
if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
|
|
|
|
val.Set(newval)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeOptionalPtrDecoder creates a decoder that decodes empty values
|
|
|
|
// as nil. Non-empty values are decoded into a value of the element type,
|
|
|
|
// just like makePtrDecoder does.
|
|
|
|
//
|
|
|
|
// This decoder is used for pointer-typed struct fields with struct tag "nil".
|
|
|
|
func makeOptionalPtrDecoder(typ reflect.Type) (decoder, error) {
|
|
|
|
etype := typ.Elem()
|
|
|
|
etypeinfo, err := cachedTypeInfo1(etype, tags{})
|
2014-11-13 20:31:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dec := func(s *Stream, val reflect.Value) (err error) {
|
2015-04-14 12:28:19 +02:00
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil || size == 0 && kind != Byte {
|
2015-03-20 22:33:40 +01:00
|
|
|
// rearm s.Kind. This is important because the input
|
|
|
|
// position must advance to the next value even though
|
|
|
|
// we don't read anything.
|
|
|
|
s.kind = -1
|
|
|
|
// set the pointer to nil.
|
|
|
|
val.Set(reflect.Zero(typ))
|
2014-11-13 20:31:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
newval := val
|
|
|
|
if val.IsNil() {
|
|
|
|
newval = reflect.New(etype)
|
|
|
|
}
|
|
|
|
if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
|
|
|
|
val.Set(newval)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ifsliceType = reflect.TypeOf([]interface{}{})
|
|
|
|
|
|
|
|
func decodeInterface(s *Stream, val reflect.Value) error {
|
2015-01-15 23:21:41 +01:00
|
|
|
if val.Type().NumMethod() != 0 {
|
|
|
|
return fmt.Errorf("rlp: type %v is not RLP-serializable", val.Type())
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
kind, _, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kind == List {
|
|
|
|
slice := reflect.New(ifsliceType).Elem()
|
2014-12-09 10:44:48 +01:00
|
|
|
if err := decodeListSlice(s, slice, decodeInterface); err != nil {
|
2014-11-13 20:31:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
val.Set(slice)
|
|
|
|
} else {
|
|
|
|
b, err := s.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val.Set(reflect.ValueOf(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This decoder is used for non-pointer values of types
|
|
|
|
// that implement the Decoder interface using a pointer receiver.
|
|
|
|
func decodeDecoderNoPtr(s *Stream, val reflect.Value) error {
|
|
|
|
return val.Addr().Interface().(Decoder).DecodeRLP(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeDecoder(s *Stream, val reflect.Value) error {
|
|
|
|
// Decoder instances are not handled using the pointer rule if the type
|
|
|
|
// implements Decoder with pointer receiver (i.e. always)
|
|
|
|
// because it might handle empty values specially.
|
|
|
|
// We need to allocate one here in this case, like makePtrDecoder does.
|
|
|
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
|
|
|
val.Set(reflect.New(val.Type().Elem()))
|
|
|
|
}
|
|
|
|
return val.Interface().(Decoder).DecodeRLP(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kind represents the kind of value contained in an RLP stream.
|
|
|
|
type Kind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Byte Kind = iota
|
|
|
|
String
|
|
|
|
List
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k Kind) String() string {
|
|
|
|
switch k {
|
|
|
|
case Byte:
|
|
|
|
return "Byte"
|
|
|
|
case String:
|
|
|
|
return "String"
|
|
|
|
case List:
|
|
|
|
return "List"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("Unknown(%d)", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByteReader must be implemented by any input reader for a Stream. It
|
|
|
|
// is implemented by e.g. bufio.Reader and bytes.Reader.
|
|
|
|
type ByteReader interface {
|
|
|
|
io.Reader
|
|
|
|
io.ByteReader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream can be used for piecemeal decoding of an input stream. This
|
|
|
|
// is useful if the input is very large or if the decoding rules for a
|
|
|
|
// type depend on the input structure. Stream does not keep an
|
|
|
|
// internal buffer. After decoding a value, the input reader will be
|
|
|
|
// positioned just before the type information for the next value.
|
|
|
|
//
|
|
|
|
// When decoding a list and the input position reaches the declared
|
|
|
|
// length of the list, all operations will return error EOL.
|
|
|
|
// The end of the list must be acknowledged using ListEnd to continue
|
|
|
|
// reading the enclosing list.
|
|
|
|
//
|
|
|
|
// Stream is not safe for concurrent use.
|
|
|
|
type Stream struct {
|
2015-03-23 15:02:55 +01:00
|
|
|
r ByteReader
|
|
|
|
|
|
|
|
// number of bytes remaining to be read from r.
|
|
|
|
remaining uint64
|
|
|
|
limited bool
|
|
|
|
|
|
|
|
// auxiliary buffer for integer decoding
|
2014-11-13 20:31:48 +01:00
|
|
|
uintbuf []byte
|
|
|
|
|
|
|
|
kind Kind // kind of value ahead
|
|
|
|
size uint64 // size of value ahead
|
|
|
|
byteval byte // value of single byte in type tag
|
2015-04-14 16:09:06 +02:00
|
|
|
kinderr error // error from last readKind
|
2014-11-13 20:31:48 +01:00
|
|
|
stack []listpos
|
|
|
|
}
|
|
|
|
|
|
|
|
type listpos struct{ pos, size uint64 }
|
|
|
|
|
2015-03-23 15:02:55 +01:00
|
|
|
// NewStream creates a new decoding stream reading from r.
|
|
|
|
//
|
|
|
|
// If r implements the ByteReader interface, Stream will
|
|
|
|
// not introduce any buffering.
|
|
|
|
//
|
|
|
|
// For non-toplevel values, Stream returns ErrElemTooLarge
|
|
|
|
// for values that do not fit into the enclosing list.
|
|
|
|
//
|
|
|
|
// Stream supports an optional input limit. If a limit is set, the
|
|
|
|
// size of any toplevel value will be checked against the remaining
|
|
|
|
// input length. Stream operations that encounter a value exceeding
|
|
|
|
// the remaining input length will return ErrValueTooLarge. The limit
|
|
|
|
// can be set by passing a non-zero value for inputLimit.
|
|
|
|
//
|
|
|
|
// If r is a bytes.Reader or strings.Reader, the input limit is set to
|
|
|
|
// the length of r's underlying data unless an explicit limit is
|
|
|
|
// provided.
|
|
|
|
func NewStream(r io.Reader, inputLimit uint64) *Stream {
|
2014-11-24 19:01:25 +01:00
|
|
|
s := new(Stream)
|
2015-03-23 15:02:55 +01:00
|
|
|
s.Reset(r, inputLimit)
|
2014-11-24 19:01:25 +01:00
|
|
|
return s
|
|
|
|
}
|
2014-11-24 19:02:04 +01:00
|
|
|
|
|
|
|
// NewListStream creates a new stream that pretends to be positioned
|
|
|
|
// at an encoded list of the given length.
|
|
|
|
func NewListStream(r io.Reader, len uint64) *Stream {
|
|
|
|
s := new(Stream)
|
2015-03-23 15:02:55 +01:00
|
|
|
s.Reset(r, len)
|
2014-11-24 19:02:04 +01:00
|
|
|
s.kind = List
|
|
|
|
s.size = len
|
|
|
|
return s
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes reads an RLP string and returns its contents as a byte slice.
|
|
|
|
// If the input does not contain an RLP string, the returned
|
|
|
|
// error will be ErrExpectedString.
|
|
|
|
func (s *Stream) Bytes() ([]byte, error) {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch kind {
|
|
|
|
case Byte:
|
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return []byte{s.byteval}, nil
|
|
|
|
case String:
|
|
|
|
b := make([]byte, size)
|
|
|
|
if err = s.readFull(b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-18 16:13:20 +02:00
|
|
|
if size == 1 && b[0] < 128 {
|
2015-04-16 10:15:14 +02:00
|
|
|
return nil, ErrCanonSize
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
return b, nil
|
|
|
|
default:
|
|
|
|
return nil, ErrExpectedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 00:49:31 +01:00
|
|
|
// Raw reads a raw encoded value including RLP type information.
|
|
|
|
func (s *Stream) Raw() ([]byte, error) {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if kind == Byte {
|
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return []byte{s.byteval}, nil
|
|
|
|
}
|
|
|
|
// the original header has already been read and is no longer
|
|
|
|
// available. read content and put a new header in front of it.
|
|
|
|
start := headsize(size)
|
|
|
|
buf := make([]byte, uint64(start)+size)
|
|
|
|
if err := s.readFull(buf[start:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if kind == String {
|
2017-08-24 13:10:50 +03:00
|
|
|
puthead(buf, 0x80, 0xB7, size)
|
2015-03-21 00:49:31 +01:00
|
|
|
} else {
|
|
|
|
puthead(buf, 0xC0, 0xF7, size)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
// Uint reads an RLP string of up to 8 bytes and returns its contents
|
|
|
|
// as an unsigned integer. If the input does not contain an RLP string, the
|
|
|
|
// returned error will be ErrExpectedString.
|
|
|
|
func (s *Stream) Uint() (uint64, error) {
|
|
|
|
return s.uint(64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) uint(maxbits int) (uint64, error) {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
switch kind {
|
|
|
|
case Byte:
|
2015-04-14 16:09:06 +02:00
|
|
|
if s.byteval == 0 {
|
|
|
|
return 0, ErrCanonInt
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return uint64(s.byteval), nil
|
|
|
|
case String:
|
|
|
|
if size > uint64(maxbits/8) {
|
2014-11-25 16:00:48 +01:00
|
|
|
return 0, errUintOverflow
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-14 16:09:06 +02:00
|
|
|
v, err := s.readUint(byte(size))
|
2015-04-16 10:15:14 +02:00
|
|
|
switch {
|
|
|
|
case err == ErrCanonSize:
|
2015-04-14 16:09:06 +02:00
|
|
|
// Adjust error because we're not reading a size right now.
|
2015-04-16 10:15:14 +02:00
|
|
|
return 0, ErrCanonInt
|
|
|
|
case err != nil:
|
|
|
|
return 0, err
|
2015-07-18 16:13:20 +02:00
|
|
|
case size > 0 && v < 128:
|
2015-04-16 10:15:14 +02:00
|
|
|
return 0, ErrCanonSize
|
|
|
|
default:
|
|
|
|
return v, nil
|
2015-04-14 16:09:06 +02:00
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
default:
|
|
|
|
return 0, ErrExpectedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 11:12:38 +03:00
|
|
|
// Bool reads an RLP string of up to 1 byte and returns its contents
|
2017-06-12 14:45:17 +02:00
|
|
|
// as a boolean. If the input does not contain an RLP string, the
|
2015-08-13 11:12:38 +03:00
|
|
|
// returned error will be ErrExpectedString.
|
|
|
|
func (s *Stream) Bool() (bool, error) {
|
|
|
|
num, err := s.uint(8)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
switch num {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
case 1:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("rlp: invalid boolean value: %d", num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
// List starts decoding an RLP list. If the input does not contain a
|
|
|
|
// list, the returned error will be ErrExpectedList. When the list's
|
|
|
|
// end has been reached, any Stream operation will return EOL.
|
|
|
|
func (s *Stream) List() (size uint64, err error) {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if kind != List {
|
|
|
|
return 0, ErrExpectedList
|
|
|
|
}
|
|
|
|
s.stack = append(s.stack, listpos{0, size})
|
|
|
|
s.kind = -1
|
|
|
|
s.size = 0
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListEnd returns to the enclosing list.
|
|
|
|
// The input reader must be positioned at the end of a list.
|
|
|
|
func (s *Stream) ListEnd() error {
|
|
|
|
if len(s.stack) == 0 {
|
|
|
|
return errNotInList
|
|
|
|
}
|
|
|
|
tos := s.stack[len(s.stack)-1]
|
|
|
|
if tos.pos != tos.size {
|
|
|
|
return errNotAtEOL
|
|
|
|
}
|
|
|
|
s.stack = s.stack[:len(s.stack)-1] // pop
|
|
|
|
if len(s.stack) > 0 {
|
|
|
|
s.stack[len(s.stack)-1].pos += tos.size
|
|
|
|
}
|
|
|
|
s.kind = -1
|
|
|
|
s.size = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes a value and stores the result in the value pointed
|
|
|
|
// to by val. Please see the documentation for the Decode function
|
|
|
|
// to learn about the decoding rules.
|
|
|
|
func (s *Stream) Decode(val interface{}) error {
|
|
|
|
if val == nil {
|
|
|
|
return errDecodeIntoNil
|
|
|
|
}
|
|
|
|
rval := reflect.ValueOf(val)
|
|
|
|
rtyp := rval.Type()
|
|
|
|
if rtyp.Kind() != reflect.Ptr {
|
|
|
|
return errNoPointer
|
|
|
|
}
|
|
|
|
if rval.IsNil() {
|
|
|
|
return errDecodeIntoNil
|
|
|
|
}
|
2015-04-17 01:16:46 +02:00
|
|
|
info, err := cachedTypeInfo(rtyp.Elem(), tags{})
|
2014-11-13 20:31:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-05 17:10:42 +01:00
|
|
|
|
|
|
|
err = info.decoder(s, rval.Elem())
|
|
|
|
if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 {
|
|
|
|
// add decode target type to error so context has more meaning
|
|
|
|
decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")"))
|
|
|
|
}
|
|
|
|
return err
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:01:25 +01:00
|
|
|
// Reset discards any information about the current decoding context
|
2015-03-23 15:02:55 +01:00
|
|
|
// and starts reading from r. This method is meant to facilitate reuse
|
|
|
|
// of a preallocated Stream across many decoding operations.
|
|
|
|
//
|
|
|
|
// If r does not also implement ByteReader, Stream will do its own
|
|
|
|
// buffering.
|
|
|
|
func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
|
|
|
|
if inputLimit > 0 {
|
|
|
|
s.remaining = inputLimit
|
|
|
|
s.limited = true
|
|
|
|
} else {
|
|
|
|
// Attempt to automatically discover
|
|
|
|
// the limit when reading from a byte slice.
|
|
|
|
switch br := r.(type) {
|
|
|
|
case *bytes.Reader:
|
|
|
|
s.remaining = uint64(br.Len())
|
|
|
|
s.limited = true
|
|
|
|
case *strings.Reader:
|
|
|
|
s.remaining = uint64(br.Len())
|
|
|
|
s.limited = true
|
|
|
|
default:
|
|
|
|
s.limited = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wrap r with a buffer if it doesn't have one.
|
2014-11-24 19:01:25 +01:00
|
|
|
bufr, ok := r.(ByteReader)
|
|
|
|
if !ok {
|
|
|
|
bufr = bufio.NewReader(r)
|
|
|
|
}
|
|
|
|
s.r = bufr
|
2015-03-23 15:02:55 +01:00
|
|
|
// Reset the decoding context.
|
2014-11-24 19:01:25 +01:00
|
|
|
s.stack = s.stack[:0]
|
|
|
|
s.size = 0
|
|
|
|
s.kind = -1
|
2015-04-14 16:09:06 +02:00
|
|
|
s.kinderr = nil
|
2014-11-24 19:01:25 +01:00
|
|
|
if s.uintbuf == nil {
|
|
|
|
s.uintbuf = make([]byte, 8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
// Kind returns the kind and size of the next value in the
|
|
|
|
// input stream.
|
|
|
|
//
|
|
|
|
// The returned size is the number of bytes that make up the value.
|
|
|
|
// For kind == Byte, the size is zero because the value is
|
|
|
|
// contained in the type tag.
|
|
|
|
//
|
|
|
|
// The first call to Kind will read size information from the input
|
|
|
|
// reader and leave it positioned at the start of the actual bytes of
|
|
|
|
// the value. Subsequent calls to Kind (until the value is decoded)
|
|
|
|
// will not advance the input reader and return cached information.
|
|
|
|
func (s *Stream) Kind() (kind Kind, size uint64, err error) {
|
|
|
|
var tos *listpos
|
|
|
|
if len(s.stack) > 0 {
|
|
|
|
tos = &s.stack[len(s.stack)-1]
|
|
|
|
}
|
|
|
|
if s.kind < 0 {
|
2015-04-14 16:09:06 +02:00
|
|
|
s.kinderr = nil
|
2015-04-14 00:54:12 +02:00
|
|
|
// Don't read further if we're at the end of the
|
2015-03-23 15:02:55 +01:00
|
|
|
// innermost list.
|
2014-11-13 20:31:48 +01:00
|
|
|
if tos != nil && tos.pos == tos.size {
|
|
|
|
return 0, 0, EOL
|
|
|
|
}
|
2015-04-14 16:09:06 +02:00
|
|
|
s.kind, s.size, s.kinderr = s.readKind()
|
|
|
|
if s.kinderr == nil {
|
|
|
|
if tos == nil {
|
|
|
|
// At toplevel, check that the value is smaller
|
|
|
|
// than the remaining input length.
|
|
|
|
if s.limited && s.size > s.remaining {
|
|
|
|
s.kinderr = ErrValueTooLarge
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Inside a list, check that the value doesn't overflow the list.
|
|
|
|
if s.size > tos.size-tos.pos {
|
|
|
|
s.kinderr = ErrElemTooLarge
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 15:02:55 +01:00
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
2015-04-14 16:09:06 +02:00
|
|
|
// Note: this might return a sticky error generated
|
|
|
|
// by an earlier call to readKind.
|
|
|
|
return s.kind, s.size, s.kinderr
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readKind() (kind Kind, size uint64, err error) {
|
|
|
|
b, err := s.readByte()
|
|
|
|
if err != nil {
|
2015-04-28 10:28:15 +02:00
|
|
|
if len(s.stack) == 0 {
|
|
|
|
// At toplevel, Adjust the error to actual EOF. io.EOF is
|
|
|
|
// used by callers to determine when to stop decoding.
|
|
|
|
switch err {
|
|
|
|
case io.ErrUnexpectedEOF:
|
|
|
|
err = io.EOF
|
|
|
|
case ErrValueTooLarge:
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
s.byteval = 0
|
|
|
|
switch {
|
|
|
|
case b < 0x80:
|
|
|
|
// For a single byte whose value is in the [0x00, 0x7F] range, that byte
|
|
|
|
// is its own RLP encoding.
|
|
|
|
s.byteval = b
|
|
|
|
return Byte, 0, nil
|
|
|
|
case b < 0xB8:
|
|
|
|
// Otherwise, if a string is 0-55 bytes long,
|
|
|
|
// the RLP encoding consists of a single byte with value 0x80 plus the
|
|
|
|
// length of the string followed by the string. The range of the first
|
|
|
|
// byte is thus [0x80, 0xB7].
|
|
|
|
return String, uint64(b - 0x80), nil
|
|
|
|
case b < 0xC0:
|
|
|
|
// If a string is more than 55 bytes long, the
|
|
|
|
// RLP encoding consists of a single byte with value 0xB7 plus the length
|
|
|
|
// of the length of the string in binary form, followed by the length of
|
|
|
|
// the string, followed by the string. For example, a length-1024 string
|
|
|
|
// would be encoded as 0xB90400 followed by the string. The range of
|
|
|
|
// the first byte is thus [0xB8, 0xBF].
|
|
|
|
size, err = s.readUint(b - 0xB7)
|
2015-04-14 16:09:06 +02:00
|
|
|
if err == nil && size < 56 {
|
|
|
|
err = ErrCanonSize
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
return String, size, err
|
|
|
|
case b < 0xF8:
|
|
|
|
// If the total payload of a list
|
|
|
|
// (i.e. the combined length of all its items) is 0-55 bytes long, the
|
|
|
|
// RLP encoding consists of a single byte with value 0xC0 plus the length
|
|
|
|
// of the list followed by the concatenation of the RLP encodings of the
|
|
|
|
// items. The range of the first byte is thus [0xC0, 0xF7].
|
|
|
|
return List, uint64(b - 0xC0), nil
|
|
|
|
default:
|
|
|
|
// If the total payload of a list is more than 55 bytes long,
|
|
|
|
// the RLP encoding consists of a single byte with value 0xF7
|
|
|
|
// plus the length of the length of the payload in binary
|
|
|
|
// form, followed by the length of the payload, followed by
|
|
|
|
// the concatenation of the RLP encodings of the items. The
|
|
|
|
// range of the first byte is thus [0xF8, 0xFF].
|
|
|
|
size, err = s.readUint(b - 0xF7)
|
2015-04-14 16:09:06 +02:00
|
|
|
if err == nil && size < 56 {
|
|
|
|
err = ErrCanonSize
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
return List, size, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readUint(size byte) (uint64, error) {
|
2015-04-14 16:09:06 +02:00
|
|
|
switch size {
|
|
|
|
case 0:
|
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return 0, nil
|
|
|
|
case 1:
|
2014-11-13 20:31:48 +01:00
|
|
|
b, err := s.readByte()
|
|
|
|
return uint64(b), err
|
2015-04-14 16:09:06 +02:00
|
|
|
default:
|
|
|
|
start := int(8 - size)
|
|
|
|
for i := 0; i < start; i++ {
|
|
|
|
s.uintbuf[i] = 0
|
|
|
|
}
|
|
|
|
if err := s.readFull(s.uintbuf[start:]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if s.uintbuf[start] == 0 {
|
|
|
|
// Note: readUint is also used to decode integer
|
|
|
|
// values. The error needs to be adjusted to become
|
|
|
|
// ErrCanonInt in this case.
|
|
|
|
return 0, ErrCanonSize
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint64(s.uintbuf), nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readFull(buf []byte) (err error) {
|
2015-04-28 10:28:15 +02:00
|
|
|
if err := s.willRead(uint64(len(buf))); err != nil {
|
|
|
|
return err
|
2015-03-23 15:02:55 +01:00
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
var nn, n int
|
|
|
|
for n < len(buf) && err == nil {
|
|
|
|
nn, err = s.r.Read(buf[n:])
|
|
|
|
n += nn
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readByte() (byte, error) {
|
2015-04-28 10:28:15 +02:00
|
|
|
if err := s.willRead(1); err != nil {
|
|
|
|
return 0, err
|
2015-03-23 15:02:55 +01:00
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
b, err := s.r.ReadByte()
|
2015-04-28 10:28:15 +02:00
|
|
|
if err == io.EOF {
|
2014-11-13 20:31:48 +01:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2015-04-28 10:28:15 +02:00
|
|
|
func (s *Stream) willRead(n uint64) error {
|
2014-11-13 20:31:48 +01:00
|
|
|
s.kind = -1 // rearm Kind
|
2015-04-28 10:28:15 +02:00
|
|
|
|
2014-11-13 20:31:48 +01:00
|
|
|
if len(s.stack) > 0 {
|
2015-04-28 10:28:15 +02:00
|
|
|
// check list overflow
|
|
|
|
tos := s.stack[len(s.stack)-1]
|
|
|
|
if n > tos.size-tos.pos {
|
|
|
|
return ErrElemTooLarge
|
|
|
|
}
|
2014-11-13 20:31:48 +01:00
|
|
|
s.stack[len(s.stack)-1].pos += n
|
|
|
|
}
|
2015-04-28 10:28:15 +02:00
|
|
|
if s.limited {
|
|
|
|
if n > s.remaining {
|
|
|
|
return ErrValueTooLarge
|
|
|
|
}
|
|
|
|
s.remaining -= n
|
|
|
|
}
|
|
|
|
return nil
|
2014-11-13 20:31:48 +01:00
|
|
|
}
|