2020-06-03 12:55:06 +00:00
|
|
|
package stackitem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-07-06 11:35:55 +00:00
|
|
|
"fmt"
|
2020-06-03 12:55:06 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
)
|
|
|
|
|
2021-07-17 19:21:33 +00:00
|
|
|
// MaxDeserialized is the maximum number one deserialized item can contain
|
|
|
|
// (including itself).
|
|
|
|
const MaxDeserialized = 2048
|
|
|
|
|
2021-07-06 21:18:00 +00:00
|
|
|
// ErrRecursive is returned on attempts to serialize some recursive stack item
|
|
|
|
// (like array including an item with reference to the same array).
|
|
|
|
var ErrRecursive = errors.New("recursive item")
|
|
|
|
|
|
|
|
// ErrUnserializable is returned on attempt to serialize some item that can't
|
|
|
|
// be serialized (like Interop item or Pointer).
|
|
|
|
var ErrUnserializable = errors.New("unserializable")
|
|
|
|
|
2021-07-06 16:32:52 +00:00
|
|
|
// serContext is an internal serialization context.
|
|
|
|
type serContext struct {
|
2021-07-09 12:26:56 +00:00
|
|
|
uv [9]byte
|
|
|
|
data []byte
|
2021-07-06 16:32:52 +00:00
|
|
|
allowInvalid bool
|
2021-07-09 13:09:33 +00:00
|
|
|
seen map[Item]sliceNoPointer
|
2021-07-06 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 19:21:33 +00:00
|
|
|
// deserContext is an internal deserialization context.
|
|
|
|
type deserContext struct {
|
|
|
|
*io.BinReader
|
|
|
|
allowInvalid bool
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// Serialize encodes given Item into the byte slice.
|
|
|
|
func Serialize(item Item) ([]byte, error) {
|
2021-07-06 16:32:52 +00:00
|
|
|
sc := serContext{
|
|
|
|
allowInvalid: false,
|
2021-07-09 13:09:33 +00:00
|
|
|
seen: make(map[Item]sliceNoPointer),
|
2021-07-06 16:32:52 +00:00
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
err := sc.serialize(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
return sc.data, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// EncodeBinary encodes given Item into the given BinWriter. It's
|
2020-06-03 12:55:06 +00:00
|
|
|
// similar to io.Serializable's EncodeBinary, but works with Item
|
|
|
|
// interface.
|
2021-07-06 16:56:23 +00:00
|
|
|
func EncodeBinary(item Item, w *io.BinWriter) {
|
2021-07-09 12:26:56 +00:00
|
|
|
data, err := Serialize(item)
|
|
|
|
if err != nil {
|
|
|
|
w.Err = err
|
|
|
|
return
|
2021-07-06 16:32:52 +00:00
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
w.WriteBytes(data)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// EncodeBinaryProtected encodes given Item into the given BinWriter. It's
|
|
|
|
// similar to EncodeBinary but allows to encode interop items (only type,
|
|
|
|
// value is lost) and doesn't return any errors in w, instead if error
|
|
|
|
// (like recursive array) is encountered it just writes special InvalidT
|
|
|
|
// type of element to w.
|
|
|
|
func EncodeBinaryProtected(item Item, w *io.BinWriter) {
|
2021-07-06 16:32:52 +00:00
|
|
|
sc := serContext{
|
|
|
|
allowInvalid: true,
|
2021-07-09 13:09:33 +00:00
|
|
|
seen: make(map[Item]sliceNoPointer),
|
2021-07-06 16:32:52 +00:00
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
err := sc.serialize(item)
|
|
|
|
if err != nil {
|
2020-12-18 09:28:01 +00:00
|
|
|
w.WriteBytes([]byte{byte(InvalidT)})
|
|
|
|
return
|
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
w.WriteBytes(sc.data)
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 12:26:56 +00:00
|
|
|
func (w *serContext) serialize(item Item) error {
|
2021-07-09 13:09:33 +00:00
|
|
|
if v, ok := w.seen[item]; ok {
|
|
|
|
if v.start == v.end {
|
|
|
|
return ErrRecursive
|
|
|
|
}
|
|
|
|
if len(w.data)+v.end-v.start > MaxSize {
|
|
|
|
return ErrTooBig
|
|
|
|
}
|
|
|
|
w.data = append(w.data, w.data[v.start:v.end]...)
|
|
|
|
return nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 13:09:33 +00:00
|
|
|
start := len(w.data)
|
2020-06-03 12:55:06 +00:00
|
|
|
switch t := item.(type) {
|
|
|
|
case *ByteArray:
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(ByteArrayT))
|
|
|
|
data := t.Value().([]byte)
|
|
|
|
w.appendVarUint(uint64(len(data)))
|
|
|
|
w.data = append(w.data, data...)
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Buffer:
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(BufferT))
|
|
|
|
data := t.Value().([]byte)
|
|
|
|
w.appendVarUint(uint64(len(data)))
|
|
|
|
w.data = append(w.data, data...)
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Bool:
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(BooleanT))
|
|
|
|
if t.Value().(bool) {
|
|
|
|
w.data = append(w.data, 1)
|
|
|
|
} else {
|
|
|
|
w.data = append(w.data, 0)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
case *BigInteger:
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(IntegerT))
|
|
|
|
data := bigint.ToBytes(t.Value().(*big.Int))
|
|
|
|
w.appendVarUint(uint64(len(data)))
|
|
|
|
w.data = append(w.data, data...)
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Interop:
|
2021-07-06 16:32:52 +00:00
|
|
|
if w.allowInvalid {
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(InteropT))
|
2021-07-06 16:32:52 +00:00
|
|
|
} else {
|
2021-07-09 12:26:56 +00:00
|
|
|
return fmt.Errorf("%w: Interop", ErrUnserializable)
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Array, *Struct:
|
2021-07-09 13:09:33 +00:00
|
|
|
w.seen[item] = sliceNoPointer{}
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
_, isArray := t.(*Array)
|
|
|
|
if isArray {
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(ArrayT))
|
2020-06-03 12:55:06 +00:00
|
|
|
} else {
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(StructT))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arr := t.Value().([]Item)
|
2021-07-09 12:26:56 +00:00
|
|
|
w.appendVarUint(uint64(len(arr)))
|
2020-06-03 12:55:06 +00:00
|
|
|
for i := range arr {
|
2021-07-09 12:26:56 +00:00
|
|
|
if err := w.serialize(arr[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-09 13:09:33 +00:00
|
|
|
w.seen[item] = sliceNoPointer{start, len(w.data)}
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Map:
|
2021-07-09 13:09:33 +00:00
|
|
|
w.seen[item] = sliceNoPointer{}
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2021-07-09 12:26:56 +00:00
|
|
|
elems := t.Value().([]MapElement)
|
|
|
|
w.data = append(w.data, byte(MapT))
|
|
|
|
w.appendVarUint(uint64(len(elems)))
|
|
|
|
for i := range elems {
|
|
|
|
if err := w.serialize(elems[i].Key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := w.serialize(elems[i].Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-09 13:09:33 +00:00
|
|
|
w.seen[item] = sliceNoPointer{start, len(w.data)}
|
2020-06-18 20:22:15 +00:00
|
|
|
case Null:
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(AnyT))
|
2021-07-06 16:34:02 +00:00
|
|
|
case nil:
|
|
|
|
if w.allowInvalid {
|
2021-07-09 12:26:56 +00:00
|
|
|
w.data = append(w.data, byte(InvalidT))
|
2021-07-06 16:34:02 +00:00
|
|
|
} else {
|
2021-07-09 12:26:56 +00:00
|
|
|
return fmt.Errorf("%w: nil", ErrUnserializable)
|
2021-07-06 16:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:26:56 +00:00
|
|
|
if len(w.data) > MaxSize {
|
|
|
|
return errTooBigSize
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-09 12:26:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *serContext) appendVarUint(val uint64) {
|
|
|
|
n := io.PutVarUint(w.uv[:], val)
|
|
|
|
w.data = append(w.data, w.uv[:n]...)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// Deserialize decodes Item from the given byte slice.
|
|
|
|
func Deserialize(data []byte) (Item, error) {
|
2020-06-03 12:55:06 +00:00
|
|
|
r := io.NewBinReaderFromBuf(data)
|
2021-07-06 16:56:23 +00:00
|
|
|
item := DecodeBinary(r)
|
2020-06-03 12:55:06 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// DecodeBinary decodes previously serialized Item from the given
|
2020-06-03 12:55:06 +00:00
|
|
|
// reader. It's similar to the io.Serializable's DecodeBinary(), but implemented
|
|
|
|
// as a function because Item itself is an interface. Caveat: always check
|
|
|
|
// reader's error value before using the returned Item.
|
2021-07-06 16:56:23 +00:00
|
|
|
func DecodeBinary(r *io.BinReader) Item {
|
2021-07-17 19:21:33 +00:00
|
|
|
dc := deserContext{
|
|
|
|
BinReader: r,
|
|
|
|
allowInvalid: false,
|
|
|
|
limit: MaxDeserialized,
|
|
|
|
}
|
|
|
|
return dc.decodeBinary()
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
// DecodeBinaryProtected is similar to DecodeBinary but allows Interop and
|
|
|
|
// Invalid values to be present (making it symmetric to EncodeBinaryProtected).
|
|
|
|
func DecodeBinaryProtected(r *io.BinReader) Item {
|
2021-07-17 19:21:33 +00:00
|
|
|
dc := deserContext{
|
|
|
|
BinReader: r,
|
|
|
|
allowInvalid: true,
|
|
|
|
limit: MaxDeserialized,
|
|
|
|
}
|
|
|
|
return dc.decodeBinary()
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 19:21:33 +00:00
|
|
|
func (r *deserContext) decodeBinary() Item {
|
2020-06-03 12:55:06 +00:00
|
|
|
var t = Type(r.ReadB())
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-17 19:21:33 +00:00
|
|
|
r.limit--
|
|
|
|
if r.limit < 0 {
|
|
|
|
r.Err = errTooBigElements
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
switch t {
|
2020-07-06 11:35:55 +00:00
|
|
|
case ByteArrayT, BufferT:
|
2020-10-07 20:08:20 +00:00
|
|
|
data := r.ReadVarBytes(MaxSize)
|
2021-06-28 15:07:25 +00:00
|
|
|
if t == ByteArrayT {
|
|
|
|
return NewByteArray(data)
|
|
|
|
}
|
|
|
|
return NewBuffer(data)
|
2020-06-03 12:55:06 +00:00
|
|
|
case BooleanT:
|
|
|
|
var b = r.ReadBool()
|
|
|
|
return NewBool(b)
|
|
|
|
case IntegerT:
|
2020-10-07 20:07:10 +00:00
|
|
|
data := r.ReadVarBytes(bigint.MaxBytesLen)
|
2020-06-04 18:11:27 +00:00
|
|
|
num := bigint.FromBytes(data)
|
2020-06-03 12:55:06 +00:00
|
|
|
return NewBigInteger(num)
|
|
|
|
case ArrayT, StructT:
|
|
|
|
size := int(r.ReadVarUint())
|
2021-07-16 09:28:11 +00:00
|
|
|
if size > MaxArraySize {
|
|
|
|
r.Err = errTooBigArray
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := make([]Item, size)
|
|
|
|
for i := 0; i < size; i++ {
|
2021-07-17 19:21:33 +00:00
|
|
|
arr[i] = r.decodeBinary()
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t == ArrayT {
|
|
|
|
return NewArray(arr)
|
|
|
|
}
|
|
|
|
return NewStruct(arr)
|
|
|
|
case MapT:
|
|
|
|
size := int(r.ReadVarUint())
|
2021-07-16 09:28:11 +00:00
|
|
|
if size > MaxArraySize {
|
|
|
|
r.Err = errTooBigArray
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
m := NewMap()
|
|
|
|
for i := 0; i < size; i++ {
|
2021-07-17 19:21:33 +00:00
|
|
|
key := r.decodeBinary()
|
|
|
|
value := r.decodeBinary()
|
2020-06-03 12:55:06 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m.Add(key, value)
|
|
|
|
}
|
|
|
|
return m
|
2020-06-18 20:22:15 +00:00
|
|
|
case AnyT:
|
|
|
|
return Null{}
|
2020-12-18 09:28:01 +00:00
|
|
|
case InteropT:
|
2021-07-17 19:21:33 +00:00
|
|
|
if r.allowInvalid {
|
2020-12-18 09:28:01 +00:00
|
|
|
return NewInterop(nil)
|
|
|
|
}
|
|
|
|
fallthrough
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-17 19:21:33 +00:00
|
|
|
if t == InvalidT && r.allowInvalid {
|
2020-12-18 09:28:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-06 21:18:00 +00:00
|
|
|
r.Err = fmt.Errorf("%w: %v", ErrInvalidType, t)
|
2020-06-03 12:55:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
|
|
|
|
// SerializeConvertible serializes Convertible into a slice of bytes.
|
|
|
|
func SerializeConvertible(conv Convertible) ([]byte, error) {
|
|
|
|
item, err := conv.ToStackItem()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Serialize(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeserializeConvertible deserializes Convertible from a slice of bytes.
|
|
|
|
func DeserializeConvertible(data []byte, conv Convertible) error {
|
|
|
|
item, err := Deserialize(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conv.FromStackItem(item)
|
|
|
|
}
|