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-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
|
|
|
|
seen map[Item]bool
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
seen: make(map[Item]bool),
|
|
|
|
}
|
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,
|
|
|
|
seen: make(map[Item]bool),
|
|
|
|
}
|
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-06 16:32:52 +00:00
|
|
|
if w.seen[item] {
|
2021-07-09 12:26:56 +00:00
|
|
|
return ErrRecursive
|
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-06 16:32:52 +00:00
|
|
|
w.seen[item] = true
|
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-06 16:32:52 +00:00
|
|
|
delete(w.seen, item)
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Map:
|
2021-07-06 16:32:52 +00:00
|
|
|
w.seen[item] = true
|
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-06 16:32:52 +00:00
|
|
|
delete(w.seen, item)
|
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 {
|
|
|
|
return decodeBinary(r, false)
|
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 {
|
|
|
|
return decodeBinary(r, true)
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:56:23 +00:00
|
|
|
func decodeBinary(r *io.BinReader, allowInvalid bool) Item {
|
2020-06-03 12:55:06 +00:00
|
|
|
var t = Type(r.ReadB())
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
arr := make([]Item, size)
|
|
|
|
for i := 0; i < size; i++ {
|
2021-07-06 16:56:23 +00:00
|
|
|
arr[i] = DecodeBinary(r)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t == ArrayT {
|
|
|
|
return NewArray(arr)
|
|
|
|
}
|
|
|
|
return NewStruct(arr)
|
|
|
|
case MapT:
|
|
|
|
size := int(r.ReadVarUint())
|
|
|
|
m := NewMap()
|
|
|
|
for i := 0; i < size; i++ {
|
2021-07-06 16:56:23 +00:00
|
|
|
key := DecodeBinary(r)
|
|
|
|
value := DecodeBinary(r)
|
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:
|
|
|
|
if allowInvalid {
|
|
|
|
return NewInterop(nil)
|
|
|
|
}
|
|
|
|
fallthrough
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2020-12-18 09:28:01 +00:00
|
|
|
if t == InvalidT && allowInvalid {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|