2019-09-16 09:18:13 +00:00
|
|
|
package io
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-15 11:58:19 +00:00
|
|
|
"bytes"
|
2019-02-25 22:44:14 +00:00
|
|
|
"encoding/binary"
|
2019-11-15 19:33:28 +00:00
|
|
|
"fmt"
|
2019-02-25 22:44:14 +00:00
|
|
|
"io"
|
2019-11-13 07:36:29 +00:00
|
|
|
"reflect"
|
2019-02-25 22:44:14 +00:00
|
|
|
)
|
|
|
|
|
2019-11-15 19:33:28 +00:00
|
|
|
// maxArraySize is a maximums size of an array which can be decoded.
|
|
|
|
// It is taken from https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L130
|
|
|
|
const maxArraySize = 0x1000000
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// BinReader is a convenient wrapper around a io.Reader and err object.
|
|
|
|
// Used to simplify error handling when reading into a struct with many fields.
|
2019-02-25 22:44:14 +00:00
|
|
|
type BinReader struct {
|
2019-09-15 11:58:19 +00:00
|
|
|
r io.Reader
|
2019-12-12 15:52:23 +00:00
|
|
|
u64 []byte
|
|
|
|
u32 []byte
|
|
|
|
u16 []byte
|
|
|
|
u8 []byte
|
2019-02-25 22:44:14 +00:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2019-09-15 11:58:19 +00:00
|
|
|
// NewBinReaderFromIO makes a BinReader from io.Reader.
|
|
|
|
func NewBinReaderFromIO(ior io.Reader) *BinReader {
|
2019-12-12 15:52:23 +00:00
|
|
|
u64 := make([]byte, 8)
|
|
|
|
u32 := u64[:4]
|
|
|
|
u16 := u64[:2]
|
|
|
|
u8 := u64[:1]
|
|
|
|
return &BinReader{r: ior, u64: u64, u32: u32, u16: u16, u8: u8}
|
2019-09-15 11:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBinReaderFromBuf makes a BinReader from byte buffer.
|
|
|
|
func NewBinReaderFromBuf(b []byte) *BinReader {
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
return NewBinReaderFromIO(r)
|
|
|
|
}
|
|
|
|
|
2019-08-28 10:24:06 +00:00
|
|
|
// ReadLE reads from the underlying io.Reader
|
2019-10-22 14:56:03 +00:00
|
|
|
// into the interface v in little-endian format.
|
2019-08-28 10:24:06 +00:00
|
|
|
func (r *BinReader) ReadLE(v interface{}) {
|
2019-02-25 22:44:14 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-09-15 11:58:19 +00:00
|
|
|
r.Err = binary.Read(r.r, binary.LittleEndian, v)
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
2019-12-12 15:52:23 +00:00
|
|
|
// ReadU64LE reads a little-endian encoded uint64 value from the underlying
|
|
|
|
// io.Reader. On read failures it returns zero.
|
|
|
|
func (r *BinReader) ReadU64LE() uint64 {
|
|
|
|
r.ReadBytes(r.u64)
|
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint64(r.u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadU32LE reads a little-endian encoded uint32 value from the underlying
|
|
|
|
// io.Reader. On read failures it returns zero.
|
|
|
|
func (r *BinReader) ReadU32LE() uint32 {
|
|
|
|
r.ReadBytes(r.u32)
|
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(r.u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadU16LE reads a little-endian encoded uint16 value from the underlying
|
|
|
|
// io.Reader. On read failures it returns zero.
|
|
|
|
func (r *BinReader) ReadU16LE() uint16 {
|
|
|
|
r.ReadBytes(r.u16)
|
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint16(r.u16)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadU16BE reads a big-endian encoded uint16 value from the underlying
|
|
|
|
// io.Reader. On read failures it returns zero.
|
|
|
|
func (r *BinReader) ReadU16BE() uint16 {
|
|
|
|
r.ReadBytes(r.u16)
|
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint16(r.u16)
|
|
|
|
}
|
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
// ReadB reads a byte from the underlying io.Reader. On read failures it
|
2019-12-12 15:52:23 +00:00
|
|
|
// returns zero.
|
2019-12-12 17:17:50 +00:00
|
|
|
func (r *BinReader) ReadB() byte {
|
2019-12-12 15:52:23 +00:00
|
|
|
r.ReadBytes(r.u8)
|
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return r.u8[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadBool reads a boolean value encoded in a zero/non-zero byte from the
|
|
|
|
// underlying io.Reader. On read failures it returns false.
|
|
|
|
func (r *BinReader) ReadBool() bool {
|
2019-12-12 17:17:50 +00:00
|
|
|
return r.ReadB() != 0
|
2019-12-12 15:52:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 07:50:03 +00:00
|
|
|
// ReadArray reads array into value which must be
|
|
|
|
// a pointer to a slice.
|
2019-11-15 19:33:28 +00:00
|
|
|
func (r *BinReader) ReadArray(t interface{}, maxSize ...int) {
|
2019-11-14 07:50:03 +00:00
|
|
|
value := reflect.ValueOf(t)
|
|
|
|
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Slice {
|
|
|
|
panic(value.Type().String() + " is not a pointer to a slice")
|
|
|
|
}
|
|
|
|
|
2019-11-13 07:36:29 +00:00
|
|
|
if r.Err != nil {
|
2019-11-14 07:50:03 +00:00
|
|
|
return
|
2019-11-13 07:36:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:13:52 +00:00
|
|
|
sliceType := value.Elem().Type()
|
|
|
|
elemType := sliceType.Elem()
|
|
|
|
isPtr := elemType.Kind() == reflect.Ptr
|
|
|
|
|
2019-11-15 19:33:28 +00:00
|
|
|
ms := maxArraySize
|
|
|
|
if len(maxSize) != 0 {
|
|
|
|
ms = maxSize[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
lu := r.ReadVarUint()
|
|
|
|
if lu > uint64(ms) {
|
|
|
|
r.Err = fmt.Errorf("array is too big (%d)", lu)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l := int(lu)
|
2019-11-13 07:36:29 +00:00
|
|
|
arr := reflect.MakeSlice(sliceType, l, l)
|
2019-11-14 07:50:03 +00:00
|
|
|
|
2019-11-13 07:36:29 +00:00
|
|
|
for i := 0; i < l; i++ {
|
2019-11-14 07:50:03 +00:00
|
|
|
var elem reflect.Value
|
|
|
|
if isPtr {
|
|
|
|
elem = reflect.New(elemType.Elem())
|
|
|
|
arr.Index(i).Set(elem)
|
|
|
|
} else {
|
|
|
|
elem = arr.Index(i).Addr()
|
|
|
|
}
|
2019-11-13 07:36:29 +00:00
|
|
|
|
2019-12-06 15:13:52 +00:00
|
|
|
el, ok := elem.Interface().(decodable)
|
|
|
|
if !ok {
|
|
|
|
panic(elemType.String() + "is not decodable")
|
|
|
|
}
|
2019-11-14 07:50:03 +00:00
|
|
|
|
2019-12-06 15:13:52 +00:00
|
|
|
el.DecodeBinary(r)
|
2019-11-14 07:50:03 +00:00
|
|
|
}
|
2019-11-13 07:36:29 +00:00
|
|
|
|
2019-12-06 15:13:52 +00:00
|
|
|
value.Elem().Set(arr)
|
2019-11-13 07:36:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 10:24:06 +00:00
|
|
|
// ReadBE reads from the underlying io.Reader
|
2019-10-22 14:56:03 +00:00
|
|
|
// into the interface v in big-endian format.
|
2019-08-28 10:24:06 +00:00
|
|
|
func (r *BinReader) ReadBE(v interface{}) {
|
2019-02-25 22:44:14 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-09-15 11:58:19 +00:00
|
|
|
r.Err = binary.Read(r.r, binary.BigEndian, v)
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 10:24:06 +00:00
|
|
|
// ReadVarUint reads a variable-length-encoded integer from the
|
2019-10-22 14:56:03 +00:00
|
|
|
// underlying reader.
|
2019-08-28 10:24:06 +00:00
|
|
|
func (r *BinReader) ReadVarUint() uint64 {
|
2019-08-29 10:24:03 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
var b = r.ReadB()
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
if b == 0xfd {
|
2019-12-12 16:20:03 +00:00
|
|
|
return uint64(r.ReadU16LE())
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
if b == 0xfe {
|
2019-12-12 16:20:03 +00:00
|
|
|
return uint64(r.ReadU32LE())
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
if b == 0xff {
|
2019-12-12 16:20:03 +00:00
|
|
|
return r.ReadU64LE()
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(b)
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:37:37 +00:00
|
|
|
// ReadVarBytes reads the next set of bytes from the underlying reader.
|
2019-08-28 10:24:06 +00:00
|
|
|
// ReadVarUInt() is used to determine how large that slice is
|
2019-12-06 15:37:37 +00:00
|
|
|
func (r *BinReader) ReadVarBytes() []byte {
|
2019-08-28 10:24:06 +00:00
|
|
|
n := r.ReadVarUint()
|
2019-02-25 22:44:14 +00:00
|
|
|
b := make([]byte, n)
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(b)
|
2019-02-25 22:44:14 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:37:46 +00:00
|
|
|
// ReadBytes copies fixed-size buffer from the reader to provided slice.
|
|
|
|
func (r *BinReader) ReadBytes(buf []byte) {
|
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, r.Err = io.ReadFull(r.r, buf)
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:37:37 +00:00
|
|
|
// ReadString calls ReadVarBytes and casts the results as a string.
|
2019-08-28 10:24:06 +00:00
|
|
|
func (r *BinReader) ReadString() string {
|
2019-12-06 15:37:37 +00:00
|
|
|
b := r.ReadVarBytes()
|
2019-02-25 22:44:14 +00:00
|
|
|
return string(b)
|
|
|
|
}
|