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 {
|
|
|
|
*io.BinWriter
|
2021-07-06 16:34:02 +00:00
|
|
|
buf *io.BufBinWriter
|
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) {
|
2020-06-03 12:55:06 +00:00
|
|
|
w := io.NewBufBinWriter()
|
2021-07-06 16:32:52 +00:00
|
|
|
sc := serContext{
|
|
|
|
BinWriter: w.BinWriter,
|
2021-07-06 16:34:02 +00:00
|
|
|
buf: w,
|
2021-07-06 16:32:52 +00:00
|
|
|
allowInvalid: false,
|
|
|
|
seen: make(map[Item]bool),
|
|
|
|
}
|
|
|
|
sc.serialize(item)
|
2020-06-03 12:55:06 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
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-06 16:32:52 +00:00
|
|
|
sc := serContext{
|
|
|
|
BinWriter: w,
|
|
|
|
allowInvalid: false,
|
|
|
|
seen: make(map[Item]bool),
|
|
|
|
}
|
|
|
|
sc.serialize(item)
|
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) {
|
2020-12-18 09:28:01 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
2021-07-06 16:32:52 +00:00
|
|
|
sc := serContext{
|
|
|
|
BinWriter: bw.BinWriter,
|
2021-07-06 16:34:02 +00:00
|
|
|
buf: bw,
|
2021-07-06 16:32:52 +00:00
|
|
|
allowInvalid: true,
|
|
|
|
seen: make(map[Item]bool),
|
|
|
|
}
|
|
|
|
sc.serialize(item)
|
2020-12-18 09:28:01 +00:00
|
|
|
if bw.Err != nil {
|
|
|
|
w.WriteBytes([]byte{byte(InvalidT)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteBytes(bw.Bytes())
|
|
|
|
}
|
|
|
|
|
2021-07-06 16:32:52 +00:00
|
|
|
func (w *serContext) serialize(item Item) {
|
2021-07-06 14:51:46 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-07-06 16:32:52 +00:00
|
|
|
if w.seen[item] {
|
2021-07-06 21:18:00 +00:00
|
|
|
w.Err = ErrRecursive
|
2020-06-03 12:55:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := item.(type) {
|
|
|
|
case *ByteArray:
|
|
|
|
w.WriteBytes([]byte{byte(ByteArrayT)})
|
|
|
|
w.WriteVarBytes(t.Value().([]byte))
|
|
|
|
case *Buffer:
|
|
|
|
w.WriteBytes([]byte{byte(BufferT)})
|
|
|
|
w.WriteVarBytes(t.Value().([]byte))
|
|
|
|
case *Bool:
|
|
|
|
w.WriteBytes([]byte{byte(BooleanT)})
|
|
|
|
w.WriteBool(t.Value().(bool))
|
|
|
|
case *BigInteger:
|
|
|
|
w.WriteBytes([]byte{byte(IntegerT)})
|
2020-06-04 18:11:27 +00:00
|
|
|
w.WriteVarBytes(bigint.ToBytes(t.Value().(*big.Int)))
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Interop:
|
2021-07-06 16:32:52 +00:00
|
|
|
if w.allowInvalid {
|
2020-12-18 09:28:01 +00:00
|
|
|
w.WriteBytes([]byte{byte(InteropT)})
|
2021-07-06 16:32:52 +00:00
|
|
|
} else {
|
2021-07-06 21:18:00 +00:00
|
|
|
w.Err = 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 {
|
|
|
|
w.WriteBytes([]byte{byte(ArrayT)})
|
|
|
|
} else {
|
|
|
|
w.WriteBytes([]byte{byte(StructT)})
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := t.Value().([]Item)
|
|
|
|
w.WriteVarUint(uint64(len(arr)))
|
|
|
|
for i := range arr {
|
2021-07-06 16:32:52 +00:00
|
|
|
w.serialize(arr[i])
|
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
|
|
|
|
|
|
|
w.WriteBytes([]byte{byte(MapT)})
|
|
|
|
w.WriteVarUint(uint64(len(t.Value().([]MapElement))))
|
|
|
|
for i := range t.Value().([]MapElement) {
|
2021-07-06 16:32:52 +00:00
|
|
|
w.serialize(t.Value().([]MapElement)[i].Key)
|
|
|
|
w.serialize(t.Value().([]MapElement)[i].Value)
|
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:
|
|
|
|
w.WriteB(byte(AnyT))
|
2021-07-06 16:34:02 +00:00
|
|
|
case nil:
|
|
|
|
if w.allowInvalid {
|
|
|
|
w.WriteBytes([]byte{byte(InvalidT)})
|
|
|
|
} else {
|
2021-07-06 21:18:00 +00:00
|
|
|
w.Err = fmt.Errorf("%w: nil", ErrUnserializable)
|
2021-07-06 16:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Err == nil && w.buf != nil && w.buf.Len() > MaxSize {
|
2021-07-06 21:18:00 +00:00
|
|
|
w.Err = errTooBigSize
|
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
|
|
|
|
}
|
|
|
|
}
|