2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-11-13 19:31:48 +00:00
|
|
|
package rlp
|
|
|
|
|
|
|
|
import (
|
2014-11-24 18:01:25 +00:00
|
|
|
"bufio"
|
2015-03-18 12:17:39 +00:00
|
|
|
"bytes"
|
2014-11-13 19:31:48 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2015-03-23 14:02:55 +00:00
|
|
|
"strings"
|
2019-02-25 10:34:08 +00:00
|
|
|
"sync"
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp/internal/rlpstruct"
|
2023-03-17 10:51:55 +00:00
|
|
|
"github.com/holiman/uint256"
|
2014-11-13 19:31:48 +00:00
|
|
|
)
|
|
|
|
|
2019-11-25 12:41:53 +00:00
|
|
|
//lint:ignore ST1012 EOL is not an error.
|
|
|
|
|
|
|
|
// EOL is returned when the end of the current list
|
|
|
|
// has been reached during streaming.
|
|
|
|
var EOL = errors.New("rlp: end of list")
|
2018-05-08 09:48:07 +00:00
|
|
|
|
2019-11-25 12:41:53 +00:00
|
|
|
var (
|
2018-05-08 09:48:07 +00:00
|
|
|
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 19:31:48 +00: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")
|
2023-03-17 10:51:55 +00:00
|
|
|
errUint256Large = errors.New("rlp: value too large for uint256")
|
2019-02-25 10:34:08 +00:00
|
|
|
|
|
|
|
streamPool = sync.Pool{
|
|
|
|
New: func() interface{} { return new(Stream) },
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
)
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// Decoder is implemented by types that require custom RLP decoding rules or need to decode
|
|
|
|
// into private fields.
|
2014-11-13 19:31:48 +00:00
|
|
|
//
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// 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.
|
2014-11-13 19:31:48 +00:00
|
|
|
type Decoder interface {
|
|
|
|
DecodeRLP(*Stream) error
|
|
|
|
}
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// Decode parses RLP-encoded data from r and stores the result in the value pointed to by
|
|
|
|
// val. Please see package-level documentation for the decoding rules. Val must be a
|
|
|
|
// non-nil pointer.
|
2014-11-13 19:31:48 +00:00
|
|
|
//
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// If r does not implement ByteReader, Decode will do its own buffering.
|
2014-11-13 19:31:48 +00:00
|
|
|
//
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00: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
|
2015-03-23 14:02:55 +00:00
|
|
|
//
|
2022-09-10 11:25:40 +00:00
|
|
|
// NewStream(r, limit).Decode(val)
|
2014-11-24 18:01:25 +00:00
|
|
|
func Decode(r io.Reader, val interface{}) error {
|
2019-02-25 10:34:08 +00:00
|
|
|
stream := streamPool.Get().(*Stream)
|
|
|
|
defer streamPool.Put(stream)
|
|
|
|
|
|
|
|
stream.Reset(r, 0)
|
|
|
|
return stream.Decode(val)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// DecodeBytes parses RLP data from b into val. Please see package-level documentation for
|
|
|
|
// the decoding rules. The input must contain exactly one value and no trailing data.
|
2015-03-18 12:17:39 +00:00
|
|
|
func DecodeBytes(b []byte, val interface{}) error {
|
2015-07-17 23:47:17 +00:00
|
|
|
r := bytes.NewReader(b)
|
2019-02-25 10:34:08 +00:00
|
|
|
|
|
|
|
stream := streamPool.Get().(*Stream)
|
|
|
|
defer streamPool.Put(stream)
|
|
|
|
|
|
|
|
stream.Reset(r, uint64(len(b)))
|
|
|
|
if err := stream.Decode(val); err != nil {
|
2015-07-17 23:47:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.Len() > 0 {
|
|
|
|
return ErrMoreThanOneValue
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-18 12:17:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 15:00:48 +00:00
|
|
|
type decodeError struct {
|
|
|
|
msg string
|
|
|
|
typ reflect.Type
|
2015-01-05 16:10:42 +00:00
|
|
|
ctx []string
|
2014-11-25 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 16:10:42 +00: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 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 12:55:47 +00:00
|
|
|
func wrapStreamError(err error, typ reflect.Type) error {
|
|
|
|
switch err {
|
2015-04-04 19:29:23 +00:00
|
|
|
case ErrCanonInt:
|
2015-04-14 14:09:06 +00: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 12:55:47 +00:00
|
|
|
case ErrExpectedList:
|
2015-01-05 16:10:42 +00:00
|
|
|
return &decodeError{msg: "expected input list", typ: typ}
|
2014-12-09 12:55:47 +00:00
|
|
|
case ErrExpectedString:
|
2015-01-05 16:10:42 +00:00
|
|
|
return &decodeError{msg: "expected input string or byte", typ: typ}
|
2014-12-09 12:55:47 +00:00
|
|
|
case errUintOverflow:
|
2015-01-05 16:10:42 +00:00
|
|
|
return &decodeError{msg: "input string too long", typ: typ}
|
2014-12-09 12:55:47 +00:00
|
|
|
case errNotAtEOL:
|
2015-01-05 16:10:42 +00: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 12:55:47 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-09 09:39:39 +00:00
|
|
|
var (
|
|
|
|
decoderInterface = reflect.TypeOf(new(Decoder)).Elem()
|
|
|
|
bigInt = reflect.TypeOf(big.Int{})
|
2023-03-17 10:51:55 +00:00
|
|
|
u256Int = reflect.TypeOf(uint256.Int{})
|
2014-12-09 09:39:39 +00:00
|
|
|
)
|
|
|
|
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
func makeDecoder(typ reflect.Type, tags rlpstruct.Tags) (dec decoder, err error) {
|
2014-12-09 09:39:39 +00:00
|
|
|
kind := typ.Kind()
|
|
|
|
switch {
|
2015-09-09 01:34:15 +00:00
|
|
|
case typ == rawValueType:
|
|
|
|
return decodeRawValue, nil
|
2014-12-09 09:39:39 +00:00
|
|
|
case typ.AssignableTo(reflect.PtrTo(bigInt)):
|
|
|
|
return decodeBigInt, nil
|
|
|
|
case typ.AssignableTo(bigInt):
|
|
|
|
return decodeBigIntNoPtr, nil
|
2023-03-17 10:51:55 +00:00
|
|
|
case typ == reflect.PtrTo(u256Int):
|
|
|
|
return decodeU256, nil
|
|
|
|
case typ == u256Int:
|
|
|
|
return decodeU256NoPtr, nil
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
case kind == reflect.Ptr:
|
|
|
|
return makePtrDecoder(typ, tags)
|
|
|
|
case reflect.PtrTo(typ).Implements(decoderInterface):
|
|
|
|
return decodeDecoder, nil
|
2014-12-09 09:41:47 +00:00
|
|
|
case isUint(kind):
|
|
|
|
return decodeUint, nil
|
2015-08-13 08:12:38 +00:00
|
|
|
case kind == reflect.Bool:
|
|
|
|
return decodeBool, nil
|
2014-12-09 09:39:39 +00:00
|
|
|
case kind == reflect.String:
|
|
|
|
return decodeString, nil
|
|
|
|
case kind == reflect.Slice || kind == reflect.Array:
|
2015-12-21 20:05:20 +00:00
|
|
|
return makeListDecoder(typ, tags)
|
2014-12-09 09:39:39 +00:00
|
|
|
case kind == reflect.Struct:
|
|
|
|
return makeStructDecoder(typ)
|
2015-01-15 22:21:41 +00:00
|
|
|
case kind == reflect.Interface:
|
2014-12-09 09:39:39 +00:00
|
|
|
return decodeInterface, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 01:34:15 +00: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 19:31:48 +00:00
|
|
|
func decodeUint(s *Stream, val reflect.Value) error {
|
2014-11-25 15:00:48 +00:00
|
|
|
typ := val.Type()
|
|
|
|
num, err := s.uint(typ.Bits())
|
2014-12-09 12:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
val.SetUint(num)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-13 08:12:38 +00: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 19:31:48 +00:00
|
|
|
func decodeString(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bytes()
|
|
|
|
if err != nil {
|
2014-12-09 12:55:47 +00:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 19:31:48 +00: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 {
|
|
|
|
i := val.Interface().(*big.Int)
|
|
|
|
if i == nil {
|
|
|
|
i = new(big.Int)
|
|
|
|
val.Set(reflect.ValueOf(i))
|
|
|
|
}
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
|
|
|
|
err := s.decodeBigInt(i)
|
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:51:55 +00:00
|
|
|
func decodeU256NoPtr(s *Stream, val reflect.Value) error {
|
|
|
|
return decodeU256(s, val.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeU256(s *Stream, val reflect.Value) error {
|
|
|
|
i := val.Interface().(*uint256.Int)
|
|
|
|
if i == nil {
|
|
|
|
i = new(uint256.Int)
|
|
|
|
val.Set(reflect.ValueOf(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.ReadUint256(i)
|
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
func makeListDecoder(typ reflect.Type, tag rlpstruct.Tags) (decoder, error) {
|
2014-11-13 19:31:48 +00: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 09:48:07 +00:00
|
|
|
return decodeByteSlice, nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
etypeinfo := theTC.infoWhileGenerating(etype, rlpstruct.Tags{})
|
2019-05-14 13:09:56 +00:00
|
|
|
if etypeinfo.decoderErr != nil {
|
|
|
|
return nil, etypeinfo.decoderErr
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-12-21 20:05:20 +00:00
|
|
|
var dec decoder
|
|
|
|
switch {
|
|
|
|
case typ.Kind() == reflect.Array:
|
|
|
|
dec = func(s *Stream, val reflect.Value) error {
|
2015-01-05 16:10:42 +00:00
|
|
|
return decodeListArray(s, val, etypeinfo.decoder)
|
2015-12-21 20:05:20 +00:00
|
|
|
}
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
case tag.Tail:
|
2015-12-21 20:05:20 +00:00
|
|
|
// A slice with "tail" tag can occur as the last field
|
2017-06-12 12:45:17 +00:00
|
|
|
// of a struct and is supposed to swallow all remaining
|
2015-12-21 20:05:20 +00: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 16:10:42 +00:00
|
|
|
return decodeListSlice(s, val, etypeinfo.decoder)
|
|
|
|
}
|
2015-12-21 20:05:20 +00:00
|
|
|
}
|
|
|
|
return dec, nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 09:44:48 +00:00
|
|
|
func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error {
|
2014-11-13 19:31:48 +00:00
|
|
|
size, err := s.List()
|
|
|
|
if err != nil {
|
2014-12-09 12:55:47 +00:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
if size == 0 {
|
2014-12-09 09:44:48 +00:00
|
|
|
val.Set(reflect.MakeSlice(val.Type(), 0, 0))
|
2014-11-13 19:31:48 +00:00
|
|
|
return s.ListEnd()
|
|
|
|
}
|
2015-12-21 20:05:20 +00:00
|
|
|
if err := decodeSliceElems(s, val, elemdec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.ListEnd()
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
|
2015-12-21 20:05:20 +00:00
|
|
|
func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
|
2014-11-13 19:31:48 +00:00
|
|
|
i := 0
|
2014-12-09 09:44:48 +00: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 19:31:48 +00:00
|
|
|
}
|
2014-12-09 09:44:48 +00: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 19:31:48 +00:00
|
|
|
}
|
|
|
|
// decode into element
|
|
|
|
if err := elemdec(s, val.Index(i)); err == EOL {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-01-05 16:10:42 +00:00
|
|
|
return addErrorContext(err, fmt.Sprint("[", i, "]"))
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < val.Len() {
|
2014-12-09 09:44:48 +00:00
|
|
|
val.SetLen(i)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-12-21 20:05:20 +00:00
|
|
|
return nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 09:44:48 +00:00
|
|
|
func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error {
|
2015-12-21 20:05:20 +00:00
|
|
|
if _, err := s.List(); err != nil {
|
2015-04-14 14:09:06 +00:00
|
|
|
return wrapStreamError(err, val.Type())
|
2014-12-09 09:44:48 +00: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 16:10:42 +00:00
|
|
|
return addErrorContext(err, fmt.Sprint("[", i, "]"))
|
2014-12-09 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < vlen {
|
2015-04-17 01:11:24 +00:00
|
|
|
return &decodeError{msg: "input list has too few elements", typ: val.Type()}
|
2014-12-09 09:44:48 +00:00
|
|
|
}
|
2014-12-09 12:55:47 +00:00
|
|
|
return wrapStreamError(s.ListEnd(), val.Type())
|
2014-12-09 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 19:31:48 +00:00
|
|
|
func decodeByteSlice(s *Stream, val reflect.Value) error {
|
|
|
|
b, err := s.Bytes()
|
2015-04-16 08:15:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return wrapStreamError(err, val.Type())
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-04-16 08:15:14 +00:00
|
|
|
val.SetBytes(b)
|
|
|
|
return nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func decodeByteArray(s *Stream, val reflect.Value) error {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-25 17:01:10 +00:00
|
|
|
slice := byteArrayBytes(val, val.Len())
|
2014-11-13 19:31:48 +00:00
|
|
|
switch kind {
|
|
|
|
case Byte:
|
2021-05-22 13:10:16 +00:00
|
|
|
if len(slice) == 0 {
|
2015-01-05 16:10:42 +00:00
|
|
|
return &decodeError{msg: "input string too long", typ: val.Type()}
|
2021-05-22 13:10:16 +00:00
|
|
|
} else if len(slice) > 1 {
|
2015-04-17 01:11:24 +00:00
|
|
|
return &decodeError{msg: "input string too short", typ: val.Type()}
|
|
|
|
}
|
2021-05-22 13:10:16 +00:00
|
|
|
slice[0] = s.byteval
|
|
|
|
s.kind = -1
|
2014-11-13 19:31:48 +00:00
|
|
|
case String:
|
2021-05-22 13:10:16 +00:00
|
|
|
if uint64(len(slice)) < size {
|
2015-01-05 16:10:42 +00:00
|
|
|
return &decodeError{msg: "input string too long", typ: val.Type()}
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2021-05-22 13:10:16 +00:00
|
|
|
if uint64(len(slice)) > size {
|
2015-04-17 01:11:24 +00:00
|
|
|
return &decodeError{msg: "input string too short", typ: val.Type()}
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
if err := s.readFull(slice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 08:15:14 +00:00
|
|
|
// Reject cases where single byte encoding should have been used.
|
2015-07-18 14:13:20 +00:00
|
|
|
if size == 1 && slice[0] < 128 {
|
2015-04-16 08:15:14 +00:00
|
|
|
return wrapStreamError(ErrCanonSize, val.Type())
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
case List:
|
2015-04-17 00:01:38 +00:00
|
|
|
return wrapStreamError(ErrExpectedString, val.Type())
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStructDecoder(typ reflect.Type) (decoder, error) {
|
2014-12-30 10:40:32 +00:00
|
|
|
fields, err := structFields(typ)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
for _, f := range fields {
|
|
|
|
if f.info.decoderErr != nil {
|
|
|
|
return nil, structFieldError{typ, f.index, f.info.decoderErr}
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
dec := func(s *Stream, val reflect.Value) (err error) {
|
2015-12-21 20:05:20 +00:00
|
|
|
if _, err := s.List(); err != nil {
|
2014-12-09 12:55:47 +00:00
|
|
|
return wrapStreamError(err, typ)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2021-05-07 12:37:13 +00:00
|
|
|
for i, f := range fields {
|
2015-12-21 20:05:20 +00:00
|
|
|
err := f.info.decoder(s, val.Field(f.index))
|
2014-11-13 19:31:48 +00:00
|
|
|
if err == EOL {
|
2021-05-07 12:37:13 +00:00
|
|
|
if f.optional {
|
|
|
|
// The field is optional, so reaching the end of the list before
|
|
|
|
// reaching the last field is acceptable. All remaining undecoded
|
|
|
|
// fields are zeroed.
|
|
|
|
zeroFields(val, fields[i:])
|
|
|
|
break
|
|
|
|
}
|
2015-04-16 23:16:46 +00:00
|
|
|
return &decodeError{msg: "too few elements", typ: typ}
|
2014-11-13 19:31:48 +00:00
|
|
|
} else if err != nil {
|
2015-01-05 16:10:42 +00:00
|
|
|
return addErrorContext(err, "."+typ.Field(f.index).Name)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 12:55:47 +00:00
|
|
|
return wrapStreamError(s.ListEnd(), typ)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:37:13 +00:00
|
|
|
func zeroFields(structval reflect.Value, fields []field) {
|
|
|
|
for _, f := range fields {
|
|
|
|
fv := structval.Field(f.index)
|
|
|
|
fv.Set(reflect.Zero(fv.Type()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// makePtrDecoder creates a decoder that decodes into the pointer's element type.
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
func makePtrDecoder(typ reflect.Type, tag rlpstruct.Tags) (decoder, error) {
|
2014-11-13 19:31:48 +00:00
|
|
|
etype := typ.Elem()
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
etypeinfo := theTC.infoWhileGenerating(etype, rlpstruct.Tags{})
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
switch {
|
|
|
|
case etypeinfo.decoderErr != nil:
|
2019-05-14 13:09:56 +00:00
|
|
|
return nil, etypeinfo.decoderErr
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
case !tag.NilOK:
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
return makeSimplePtrDecoder(etype, etypeinfo), nil
|
|
|
|
default:
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
return makeNilPtrDecoder(etype, etypeinfo, tag), nil
|
2015-04-16 23:16:46 +00:00
|
|
|
}
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeSimplePtrDecoder(etype reflect.Type, etypeinfo *typeinfo) decoder {
|
|
|
|
return func(s *Stream, val reflect.Value) (err error) {
|
2015-04-16 23:16:46 +00:00
|
|
|
newval := val
|
|
|
|
if val.IsNil() {
|
|
|
|
newval = reflect.New(etype)
|
|
|
|
}
|
|
|
|
if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
|
|
|
|
val.Set(newval)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
// makeNilPtrDecoder 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.
|
2015-04-16 23:16:46 +00:00
|
|
|
//
|
|
|
|
// This decoder is used for pointer-typed struct fields with struct tag "nil".
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
func makeNilPtrDecoder(etype reflect.Type, etypeinfo *typeinfo, ts rlpstruct.Tags) decoder {
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
typ := reflect.PtrTo(etype)
|
|
|
|
nilPtr := reflect.Zero(typ)
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
|
|
|
|
// Determine the value kind that results in nil pointer.
|
|
|
|
nilKind := typeNilKind(etype, ts)
|
|
|
|
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
return func(s *Stream, val reflect.Value) (err error) {
|
2015-04-14 10:28:19 +00:00
|
|
|
kind, size, err := s.Kind()
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
if err != nil {
|
|
|
|
val.Set(nilPtr)
|
|
|
|
return wrapStreamError(err, typ)
|
|
|
|
}
|
|
|
|
// Handle empty values as a nil pointer.
|
|
|
|
if kind != Byte && size == 0 {
|
|
|
|
if kind != nilKind {
|
|
|
|
return &decodeError{
|
|
|
|
msg: fmt.Sprintf("wrong kind of empty value (got %v, want %v)", kind, nilKind),
|
|
|
|
typ: typ,
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 21:33:40 +00: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
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
val.Set(nilPtr)
|
|
|
|
return nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
newval := val
|
|
|
|
if val.IsNil() {
|
|
|
|
newval = reflect.New(etype)
|
|
|
|
}
|
|
|
|
if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
|
|
|
|
val.Set(newval)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ifsliceType = reflect.TypeOf([]interface{}{})
|
|
|
|
|
|
|
|
func decodeInterface(s *Stream, val reflect.Value) error {
|
2015-01-15 22:21:41 +00:00
|
|
|
if val.Type().NumMethod() != 0 {
|
|
|
|
return fmt.Errorf("rlp: type %v is not RLP-serializable", val.Type())
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
kind, _, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kind == List {
|
|
|
|
slice := reflect.New(ifsliceType).Elem()
|
2014-12-09 09:44:48 +00:00
|
|
|
if err := decodeListSlice(s, slice, decodeInterface); err != nil {
|
2014-11-13 19:31:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
val.Set(slice)
|
|
|
|
} else {
|
|
|
|
b, err := s.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val.Set(reflect.ValueOf(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeDecoder(s *Stream, val reflect.Value) error {
|
rlp: improve nil pointer handling (#20064)
* rlp: improve nil pointer handling
In both encoder and decoder, the rules for encoding nil pointers were a
bit hard to understand, and didn't leave much choice. Since RLP allows
two empty values (empty list, empty string), any protocol built on RLP
must choose either of these values to represent the null value in a
certain context.
This change adds choice in the form of two new struct tags, "nilString"
and "nilList". These can be used to specify how a nil pointer value is
encoded. The "nil" tag still exists, but its implementation is now
explicit and defines exactly how nil pointers are handled in a single
place.
Another important change in this commit is how nil pointers and the
Encoder interface interact. The EncodeRLP method was previously called
even on nil values, which was supposed to give users a choice of how
their value would be handled when nil. It turns out this is a stupid
idea. If you create a network protocol containing an object defined in
another package, it's better to be able to say that the object should be
a list or string when nil in the definition of the protocol message
rather than defining the encoding of nil on the object itself.
As of this commit, the encoding rules for pointers now take precedence
over the Encoder interface rule. I think the "nil" tag will work fine
for most cases. For special kinds of objects which are a struct in Go
but strings in RLP, code using the object can specify the desired
encoding of nil using the "nilString" and "nilList" tags.
* rlp: propagate struct field type errors
If a struct contained fields of undecodable type, the encoder and
decoder would panic instead of returning an error. Fix this by
propagating type errors in makeStruct{Writer,Decoder} and add a test.
2019-09-13 09:10:57 +00:00
|
|
|
return val.Addr().Interface().(Decoder).DecodeRLP(s)
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kind represents the kind of value contained in an RLP stream.
|
2021-05-18 10:10:27 +00:00
|
|
|
type Kind int8
|
2014-11-13 19:31:48 +00:00
|
|
|
|
|
|
|
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 14:02:55 +00:00
|
|
|
r ByteReader
|
|
|
|
|
2021-05-18 10:10:27 +00:00
|
|
|
remaining uint64 // number of bytes remaining to be read from r
|
|
|
|
size uint64 // size of value ahead
|
|
|
|
kinderr error // error from last readKind
|
|
|
|
stack []uint64 // list sizes
|
2021-05-25 19:56:25 +00:00
|
|
|
uintbuf [32]byte // auxiliary buffer for integer decoding
|
2021-05-18 10:10:27 +00:00
|
|
|
kind Kind // kind of value ahead
|
|
|
|
byteval byte // value of single byte in type tag
|
|
|
|
limited bool // true if input limit is in effect
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 14:02:55 +00: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 18:01:25 +00:00
|
|
|
s := new(Stream)
|
2015-03-23 14:02:55 +00:00
|
|
|
s.Reset(r, inputLimit)
|
2014-11-24 18:01:25 +00:00
|
|
|
return s
|
|
|
|
}
|
2014-11-24 18:02:04 +00: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 14:02:55 +00:00
|
|
|
s.Reset(r, len)
|
2014-11-24 18:02:04 +00:00
|
|
|
s.kind = List
|
|
|
|
s.size = len
|
|
|
|
return s
|
2014-11-13 19:31:48 +00: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 14:13:20 +00:00
|
|
|
if size == 1 && b[0] < 128 {
|
2015-04-16 08:15:14 +00:00
|
|
|
return nil, ErrCanonSize
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
return b, nil
|
|
|
|
default:
|
|
|
|
return nil, ErrExpectedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
// ReadBytes decodes the next RLP value and stores the result in b.
|
|
|
|
// The value size must match len(b) exactly.
|
|
|
|
func (s *Stream) ReadBytes(b []byte) error {
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch kind {
|
|
|
|
case Byte:
|
|
|
|
if len(b) != 1 {
|
|
|
|
return fmt.Errorf("input value has wrong size 1, want %d", len(b))
|
|
|
|
}
|
|
|
|
b[0] = s.byteval
|
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return nil
|
|
|
|
case String:
|
|
|
|
if uint64(len(b)) != size {
|
|
|
|
return fmt.Errorf("input value has wrong size %d, want %d", size, len(b))
|
|
|
|
}
|
|
|
|
if err = s.readFull(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if size == 1 && b[0] < 128 {
|
|
|
|
return ErrCanonSize
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return ErrExpectedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 23:49:31 +00: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
|
|
|
|
}
|
2021-05-18 10:10:27 +00:00
|
|
|
// The original header has already been read and is no longer
|
|
|
|
// available. Read content and put a new header in front of it.
|
2015-03-20 23:49:31 +00:00
|
|
|
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 10:10:50 +00:00
|
|
|
puthead(buf, 0x80, 0xB7, size)
|
2015-03-20 23:49:31 +00:00
|
|
|
} else {
|
|
|
|
puthead(buf, 0xC0, 0xF7, size)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 19:31:48 +00: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.
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use s.Uint64 instead.
|
2014-11-13 19:31:48 +00:00
|
|
|
func (s *Stream) Uint() (uint64, error) {
|
|
|
|
return s.uint(64)
|
|
|
|
}
|
|
|
|
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
func (s *Stream) Uint64() (uint64, error) {
|
|
|
|
return s.uint(64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Uint32() (uint32, error) {
|
|
|
|
i, err := s.uint(32)
|
|
|
|
return uint32(i), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Uint16() (uint16, error) {
|
|
|
|
i, err := s.uint(16)
|
|
|
|
return uint16(i), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Uint8() (uint8, error) {
|
|
|
|
i, err := s.uint(8)
|
|
|
|
return uint8(i), err
|
|
|
|
}
|
|
|
|
|
2014-11-13 19:31:48 +00:00
|
|
|
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 14:09:06 +00:00
|
|
|
if s.byteval == 0 {
|
|
|
|
return 0, ErrCanonInt
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return uint64(s.byteval), nil
|
|
|
|
case String:
|
|
|
|
if size > uint64(maxbits/8) {
|
2014-11-25 15:00:48 +00:00
|
|
|
return 0, errUintOverflow
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-04-14 14:09:06 +00:00
|
|
|
v, err := s.readUint(byte(size))
|
2015-04-16 08:15:14 +00:00
|
|
|
switch {
|
|
|
|
case err == ErrCanonSize:
|
2015-04-14 14:09:06 +00:00
|
|
|
// Adjust error because we're not reading a size right now.
|
2015-04-16 08:15:14 +00:00
|
|
|
return 0, ErrCanonInt
|
|
|
|
case err != nil:
|
|
|
|
return 0, err
|
2015-07-18 14:13:20 +00:00
|
|
|
case size > 0 && v < 128:
|
2015-04-16 08:15:14 +00:00
|
|
|
return 0, ErrCanonSize
|
|
|
|
default:
|
|
|
|
return v, nil
|
2015-04-14 14:09:06 +00:00
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
default:
|
|
|
|
return 0, ErrExpectedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 08:12:38 +00:00
|
|
|
// Bool reads an RLP string of up to 1 byte and returns its contents
|
2017-06-12 12:45:17 +00:00
|
|
|
// as a boolean. If the input does not contain an RLP string, the
|
2015-08-13 08:12:38 +00: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 19:31:48 +00: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
|
|
|
|
}
|
2021-05-18 10:10:27 +00:00
|
|
|
|
|
|
|
// Remove size of inner list from outer list before pushing the new size
|
|
|
|
// onto the stack. This ensures that the remaining outer list size will
|
|
|
|
// be correct after the matching call to ListEnd.
|
|
|
|
if inList, limit := s.listLimit(); inList {
|
|
|
|
s.stack[len(s.stack)-1] = limit - size
|
|
|
|
}
|
|
|
|
s.stack = append(s.stack, size)
|
2014-11-13 19:31:48 +00:00
|
|
|
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 {
|
2021-05-18 10:10:27 +00:00
|
|
|
// Ensure that no more data is remaining in the current list.
|
|
|
|
if inList, listLimit := s.listLimit(); !inList {
|
2014-11-13 19:31:48 +00:00
|
|
|
return errNotInList
|
2021-05-18 10:10:27 +00:00
|
|
|
} else if listLimit > 0 {
|
2014-11-13 19:31:48 +00:00
|
|
|
return errNotAtEOL
|
|
|
|
}
|
|
|
|
s.stack = s.stack[:len(s.stack)-1] // pop
|
|
|
|
s.kind = -1
|
|
|
|
s.size = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method
implementations. The generated methods will behave identically to the
reflect-based encoder, but run faster because there is no reflection overhead.
Package rlp now provides the EncoderBuffer type for incremental encoding. This
is used by generated code, but the new methods can also be useful for
hand-written encoders.
There is also experimental support for generating DecodeRLP, and some new
methods have been added to the existing Stream type to support this. Creating
decoders with rlpgen is not recommended at this time because the generated
methods create very poor error reporting.
More detail about package rlp changes:
* rlp: externalize struct field processing / validation
This adds a new package, rlp/internal/rlpstruct, in preparation for the
RLP encoder generator.
I think the struct field rules are subtle enough to warrant extracting
this into their own package, even though it means that a bunch of
adapter code is needed for converting to/from rlpstruct.Type.
* rlp: add more decoder methods (for rlpgen)
This adds new methods on rlp.Stream:
- Uint64, Uint32, Uint16, Uint8, BigInt
- ReadBytes for decoding into []byte
- MoreDataInList - useful for optional list elements
* rlp: expose encoder buffer (for rlpgen)
This exposes the internal encoder buffer type for use in EncodeRLP
implementations.
The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to
encBuffer. It is implemented this way to ensure the global encBuffer pool
is handled correctly.
2022-02-16 17:14:12 +00:00
|
|
|
// MoreDataInList reports whether the current list context contains
|
|
|
|
// more data to be read.
|
|
|
|
func (s *Stream) MoreDataInList() bool {
|
|
|
|
_, listLimit := s.listLimit()
|
|
|
|
return listLimit > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigInt decodes an arbitrary-size integer value.
|
|
|
|
func (s *Stream) BigInt() (*big.Int, error) {
|
|
|
|
i := new(big.Int)
|
|
|
|
if err := s.decodeBigInt(i); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) decodeBigInt(dst *big.Int) error {
|
|
|
|
var buffer []byte
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case kind == List:
|
|
|
|
return ErrExpectedString
|
|
|
|
case kind == Byte:
|
|
|
|
buffer = s.uintbuf[:1]
|
|
|
|
buffer[0] = s.byteval
|
|
|
|
s.kind = -1 // re-arm Kind
|
|
|
|
case size == 0:
|
|
|
|
// Avoid zero-length read.
|
|
|
|
s.kind = -1
|
|
|
|
case size <= uint64(len(s.uintbuf)):
|
|
|
|
// For integers smaller than s.uintbuf, allocating a buffer
|
|
|
|
// can be avoided.
|
|
|
|
buffer = s.uintbuf[:size]
|
|
|
|
if err := s.readFull(buffer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Reject inputs where single byte encoding should have been used.
|
|
|
|
if size == 1 && buffer[0] < 128 {
|
|
|
|
return ErrCanonSize
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// For large integers, a temporary buffer is needed.
|
|
|
|
buffer = make([]byte, size)
|
|
|
|
if err := s.readFull(buffer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject leading zero bytes.
|
|
|
|
if len(buffer) > 0 && buffer[0] == 0 {
|
|
|
|
return ErrCanonInt
|
|
|
|
}
|
|
|
|
// Set the integer bytes.
|
|
|
|
dst.SetBytes(buffer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:51:55 +00:00
|
|
|
// ReadUint256 decodes the next value as a uint256.
|
|
|
|
func (s *Stream) ReadUint256(dst *uint256.Int) error {
|
|
|
|
var buffer []byte
|
|
|
|
kind, size, err := s.Kind()
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case kind == List:
|
|
|
|
return ErrExpectedString
|
|
|
|
case kind == Byte:
|
|
|
|
buffer = s.uintbuf[:1]
|
|
|
|
buffer[0] = s.byteval
|
|
|
|
s.kind = -1 // re-arm Kind
|
|
|
|
case size == 0:
|
|
|
|
// Avoid zero-length read.
|
|
|
|
s.kind = -1
|
|
|
|
case size <= uint64(len(s.uintbuf)):
|
|
|
|
// All possible uint256 values fit into s.uintbuf.
|
|
|
|
buffer = s.uintbuf[:size]
|
|
|
|
if err := s.readFull(buffer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Reject inputs where single byte encoding should have been used.
|
|
|
|
if size == 1 && buffer[0] < 128 {
|
|
|
|
return ErrCanonSize
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errUint256Large
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject leading zero bytes.
|
|
|
|
if len(buffer) > 0 && buffer[0] == 0 {
|
|
|
|
return ErrCanonInt
|
|
|
|
}
|
|
|
|
// Set the integer bytes.
|
|
|
|
dst.SetBytes(buffer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 19:31:48 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-05-14 13:09:56 +00:00
|
|
|
decoder, err := cachedDecoder(rtyp.Elem())
|
2014-11-13 19:31:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-05 16:10:42 +00:00
|
|
|
|
2019-05-14 13:09:56 +00:00
|
|
|
err = decoder(s, rval.Elem())
|
2015-01-05 16:10:42 +00:00
|
|
|
if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 {
|
2021-05-18 10:10:27 +00:00
|
|
|
// Add decode target type to error so context has more meaning.
|
2015-01-05 16:10:42 +00:00
|
|
|
decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")"))
|
|
|
|
}
|
|
|
|
return err
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 18:01:25 +00:00
|
|
|
// Reset discards any information about the current decoding context
|
2015-03-23 14:02:55 +00: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
|
2021-05-18 10:10:27 +00:00
|
|
|
case *bytes.Buffer:
|
|
|
|
s.remaining = uint64(br.Len())
|
|
|
|
s.limited = true
|
2015-03-23 14:02:55 +00:00
|
|
|
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 18:01:25 +00:00
|
|
|
bufr, ok := r.(ByteReader)
|
|
|
|
if !ok {
|
|
|
|
bufr = bufio.NewReader(r)
|
|
|
|
}
|
|
|
|
s.r = bufr
|
2015-03-23 14:02:55 +00:00
|
|
|
// Reset the decoding context.
|
2014-11-24 18:01:25 +00:00
|
|
|
s.stack = s.stack[:0]
|
|
|
|
s.size = 0
|
|
|
|
s.kind = -1
|
2015-04-14 14:09:06 +00:00
|
|
|
s.kinderr = nil
|
2019-02-25 10:34:08 +00:00
|
|
|
s.byteval = 0
|
2021-05-25 19:56:25 +00:00
|
|
|
s.uintbuf = [32]byte{}
|
2014-11-24 18:01:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 19:31:48 +00: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) {
|
2021-05-18 10:10:27 +00:00
|
|
|
if s.kind >= 0 {
|
|
|
|
return s.kind, s.size, s.kinderr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for end of list. This needs to be done here because readKind
|
|
|
|
// checks against the list size, and would return the wrong error.
|
|
|
|
inList, listLimit := s.listLimit()
|
|
|
|
if inList && listLimit == 0 {
|
|
|
|
return 0, 0, EOL
|
|
|
|
}
|
|
|
|
// Read the actual size tag.
|
|
|
|
s.kind, s.size, s.kinderr = s.readKind()
|
|
|
|
if s.kinderr == nil {
|
|
|
|
// Check the data size of the value ahead against input limits. This
|
|
|
|
// is done here because many decoders require allocating an input
|
|
|
|
// buffer matching the value size. Checking it here protects those
|
|
|
|
// decoders from inputs declaring very large value size.
|
|
|
|
if inList && s.size > listLimit {
|
|
|
|
s.kinderr = ErrElemTooLarge
|
|
|
|
} else if s.limited && s.size > s.remaining {
|
|
|
|
s.kinderr = ErrValueTooLarge
|
2015-03-23 14:02:55 +00:00
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-04-14 14:09:06 +00:00
|
|
|
return s.kind, s.size, s.kinderr
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readKind() (kind Kind, size uint64, err error) {
|
|
|
|
b, err := s.readByte()
|
|
|
|
if err != nil {
|
2015-04-28 08:28:15 +00: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 19:31:48 +00: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:
|
2021-05-18 10:10:27 +00:00
|
|
|
// 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].
|
2014-11-13 19:31:48 +00:00
|
|
|
return String, uint64(b - 0x80), nil
|
|
|
|
case b < 0xC0:
|
2021-05-18 10:10:27 +00:00
|
|
|
// 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].
|
2014-11-13 19:31:48 +00:00
|
|
|
size, err = s.readUint(b - 0xB7)
|
2015-04-14 14:09:06 +00:00
|
|
|
if err == nil && size < 56 {
|
|
|
|
err = ErrCanonSize
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
return String, size, err
|
|
|
|
case b < 0xF8:
|
2021-05-18 10:10:27 +00:00
|
|
|
// 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].
|
2014-11-13 19:31:48 +00:00
|
|
|
return List, uint64(b - 0xC0), nil
|
|
|
|
default:
|
2021-05-18 10:10:27 +00:00
|
|
|
// 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].
|
2014-11-13 19:31:48 +00:00
|
|
|
size, err = s.readUint(b - 0xF7)
|
2015-04-14 14:09:06 +00:00
|
|
|
if err == nil && size < 56 {
|
|
|
|
err = ErrCanonSize
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
return List, size, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) readUint(size byte) (uint64, error) {
|
2015-04-14 14:09:06 +00:00
|
|
|
switch size {
|
|
|
|
case 0:
|
|
|
|
s.kind = -1 // rearm Kind
|
|
|
|
return 0, nil
|
|
|
|
case 1:
|
2014-11-13 19:31:48 +00:00
|
|
|
b, err := s.readByte()
|
|
|
|
return uint64(b), err
|
2015-04-14 14:09:06 +00:00
|
|
|
default:
|
2021-05-25 19:56:25 +00:00
|
|
|
buffer := s.uintbuf[:8]
|
|
|
|
for i := range buffer {
|
|
|
|
buffer[i] = 0
|
|
|
|
}
|
2015-04-14 14:09:06 +00:00
|
|
|
start := int(8 - size)
|
2021-05-25 19:56:25 +00:00
|
|
|
if err := s.readFull(buffer[start:]); err != nil {
|
2015-04-14 14:09:06 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-05-25 19:56:25 +00:00
|
|
|
if buffer[start] == 0 {
|
2021-05-18 10:10:27 +00:00
|
|
|
// Note: readUint is also used to decode integer values.
|
|
|
|
// The error needs to be adjusted to become ErrCanonInt in this case.
|
2015-04-14 14:09:06 +00:00
|
|
|
return 0, ErrCanonSize
|
|
|
|
}
|
2021-05-25 19:56:25 +00:00
|
|
|
return binary.BigEndian.Uint64(buffer[:]), nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:10:27 +00:00
|
|
|
// readFull reads into buf from the underlying stream.
|
2014-11-13 19:31:48 +00:00
|
|
|
func (s *Stream) readFull(buf []byte) (err error) {
|
2015-04-28 08:28:15 +00:00
|
|
|
if err := s.willRead(uint64(len(buf))); err != nil {
|
|
|
|
return err
|
2015-03-23 14:02:55 +00:00
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
var nn, n int
|
|
|
|
for n < len(buf) && err == nil {
|
|
|
|
nn, err = s.r.Read(buf[n:])
|
|
|
|
n += nn
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
2021-02-18 09:19:49 +00:00
|
|
|
if n < len(buf) {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
} else {
|
|
|
|
// Readers are allowed to give EOF even though the read succeeded.
|
|
|
|
// In such cases, we discard the EOF, like io.ReadFull() does.
|
|
|
|
err = nil
|
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:10:27 +00:00
|
|
|
// readByte reads a single byte from the underlying stream.
|
2014-11-13 19:31:48 +00:00
|
|
|
func (s *Stream) readByte() (byte, error) {
|
2015-04-28 08:28:15 +00:00
|
|
|
if err := s.willRead(1); err != nil {
|
|
|
|
return 0, err
|
2015-03-23 14:02:55 +00:00
|
|
|
}
|
2014-11-13 19:31:48 +00:00
|
|
|
b, err := s.r.ReadByte()
|
2015-04-28 08:28:15 +00:00
|
|
|
if err == io.EOF {
|
2014-11-13 19:31:48 +00:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:10:27 +00:00
|
|
|
// willRead is called before any read from the underlying stream. It checks
|
|
|
|
// n against size limits, and updates the limits if n doesn't overflow them.
|
2015-04-28 08:28:15 +00:00
|
|
|
func (s *Stream) willRead(n uint64) error {
|
2014-11-13 19:31:48 +00:00
|
|
|
s.kind = -1 // rearm Kind
|
2015-04-28 08:28:15 +00:00
|
|
|
|
2021-05-18 10:10:27 +00:00
|
|
|
if inList, limit := s.listLimit(); inList {
|
|
|
|
if n > limit {
|
2015-04-28 08:28:15 +00:00
|
|
|
return ErrElemTooLarge
|
|
|
|
}
|
2021-05-18 10:10:27 +00:00
|
|
|
s.stack[len(s.stack)-1] = limit - n
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2015-04-28 08:28:15 +00:00
|
|
|
if s.limited {
|
|
|
|
if n > s.remaining {
|
|
|
|
return ErrValueTooLarge
|
|
|
|
}
|
|
|
|
s.remaining -= n
|
|
|
|
}
|
|
|
|
return nil
|
2014-11-13 19:31:48 +00:00
|
|
|
}
|
2021-05-18 10:10:27 +00:00
|
|
|
|
|
|
|
// listLimit returns the amount of data remaining in the innermost list.
|
|
|
|
func (s *Stream) listLimit() (inList bool, limit uint64) {
|
|
|
|
if len(s.stack) == 0 {
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
return true, s.stack[len(s.stack)-1]
|
|
|
|
}
|