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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SerializeItem encodes given Item into the byte slice.
|
|
|
|
func SerializeItem(item Item) ([]byte, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
EncodeBinaryStackItem(item, w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinaryStackItem encodes given Item into the given BinWriter. It's
|
|
|
|
// similar to io.Serializable's EncodeBinary, but works with Item
|
|
|
|
// interface.
|
|
|
|
func EncodeBinaryStackItem(item Item, w *io.BinWriter) {
|
2020-12-18 09:28:01 +00:00
|
|
|
serializeItemTo(item, w, false, make(map[Item]bool))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:28:01 +00:00
|
|
|
// EncodeBinaryStackItemAppExec encodes given Item into the given BinWriter. It's
|
|
|
|
// similar to EncodeBinaryStackItem but allows to encode interop (only type, value is lost).
|
|
|
|
func EncodeBinaryStackItemAppExec(item Item, w *io.BinWriter) {
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
serializeItemTo(item, bw.BinWriter, true, make(map[Item]bool))
|
|
|
|
if bw.Err != nil {
|
|
|
|
w.WriteBytes([]byte{byte(InvalidT)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteBytes(bw.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeItemTo(item Item, w *io.BinWriter, allowInvalid bool, seen map[Item]bool) {
|
2021-07-06 14:51:46 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
if seen[item] {
|
|
|
|
w.Err = errors.New("recursive structures can't be serialized")
|
|
|
|
return
|
|
|
|
}
|
2020-12-18 09:28:01 +00:00
|
|
|
if item == nil && allowInvalid {
|
|
|
|
w.WriteBytes([]byte{byte(InvalidT)})
|
|
|
|
return
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
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:
|
2020-12-18 09:28:01 +00:00
|
|
|
if allowInvalid {
|
|
|
|
w.WriteBytes([]byte{byte(InteropT)})
|
|
|
|
return
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
w.Err = errors.New("interop item can't be serialized")
|
|
|
|
case *Array, *Struct:
|
|
|
|
seen[item] = true
|
|
|
|
|
|
|
|
_, 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 {
|
2020-12-18 09:28:01 +00:00
|
|
|
serializeItemTo(arr[i], w, allowInvalid, seen)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-06 14:10:31 +00:00
|
|
|
delete(seen, item)
|
2020-06-03 12:55:06 +00:00
|
|
|
case *Map:
|
|
|
|
seen[item] = true
|
|
|
|
|
|
|
|
w.WriteBytes([]byte{byte(MapT)})
|
|
|
|
w.WriteVarUint(uint64(len(t.Value().([]MapElement))))
|
|
|
|
for i := range t.Value().([]MapElement) {
|
2020-12-18 09:28:01 +00:00
|
|
|
serializeItemTo(t.Value().([]MapElement)[i].Key, w, allowInvalid, seen)
|
|
|
|
serializeItemTo(t.Value().([]MapElement)[i].Value, w, allowInvalid, seen)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-07-06 14:10:31 +00:00
|
|
|
delete(seen, item)
|
2020-06-18 20:22:15 +00:00
|
|
|
case Null:
|
|
|
|
w.WriteB(byte(AnyT))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeserializeItem decodes Item from the given byte slice.
|
|
|
|
func DeserializeItem(data []byte) (Item, error) {
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
|
|
|
item := DecodeBinaryStackItem(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinaryStackItem decodes previously serialized Item from the given
|
|
|
|
// 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.
|
|
|
|
func DecodeBinaryStackItem(r *io.BinReader) Item {
|
2020-12-18 09:28:01 +00:00
|
|
|
return decodeBinaryStackItem(r, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinaryStackItemAppExec is similar to DecodeBinaryStackItem
|
|
|
|
// but allows Interop values to be present.
|
|
|
|
func DecodeBinaryStackItemAppExec(r *io.BinReader) Item {
|
|
|
|
return decodeBinaryStackItem(r, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBinaryStackItem(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++ {
|
|
|
|
arr[i] = DecodeBinaryStackItem(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t == ArrayT {
|
|
|
|
return NewArray(arr)
|
|
|
|
}
|
|
|
|
return NewStruct(arr)
|
|
|
|
case MapT:
|
|
|
|
size := int(r.ReadVarUint())
|
|
|
|
m := NewMap()
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
key := DecodeBinaryStackItem(r)
|
|
|
|
value := DecodeBinaryStackItem(r)
|
|
|
|
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
|
|
|
|
}
|
2020-07-06 11:35:55 +00:00
|
|
|
r.Err = fmt.Errorf("unknown type: %v", t)
|
2020-06-03 12:55:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|