func (d *Decoder) NextPrefix(elemSize uintptr) uint64 {
	n := d.NextUint64()
	if n > 1<<31-1 || n*uint64(elemSize) > MaxSliceSize {
		d.err = ErrSliceTooLarge{Len: n, ElemSize: uint64(elemSize)}
		return 0
	}
	return n
}
NextPrefix is like NextUint64, but performs sanity checks on the prefix.
Specifically, if the prefix multiplied by elemSize exceeds MaxSliceSize,
NextPrefix returns 0 and sets d.Err().
package encoding
import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"os"
	"reflect"
)
const (
	MaxObjectSize = 12e6
	MaxSliceSize = 5e6  
)
var (
	errBadPointer = errors.New("cannot decode into invalid pointer")
)
type ErrObjectTooLarge uint64
func (e ErrObjectTooLarge) Error() string {
	return fmt.Sprintf("encoded object (>= %v bytes) exceeds size limit (%v bytes)", uint64(e), uint64(MaxObjectSize))
}
type ErrSliceTooLarge struct {
	Len      uint64
	ElemSize uint64
}
func (e ErrSliceTooLarge) Error() string {
	return fmt.Sprintf("encoded slice (%v*%v bytes) exceeds size limit (%v bytes)", e.Len, e.ElemSize, uint64(MaxSliceSize))
}
type (
	SiaMarshaler interface {
		MarshalSia(io.Writer) error
	}
	SiaUnmarshaler interface {
		UnmarshalSia(io.Reader) error
	}
)
type Encoder struct {
	w   io.Writer
	buf [8]byte
	err error
}
func (e *Encoder) Write(p []byte) (int, error) {
	if e.err != nil {
		return 0, e.err
	}
	var n int
	n, e.err = e.w.Write(p)
	if n != len(p) && e.err == nil {
		e.err = io.ErrShortWrite
	}
	return n, e.err
}
func (e *Encoder) WriteByte(b byte) error {
	if e.err != nil {
		return e.err
	}
	e.buf[0] = b
	e.Write(e.buf[:1])
	return e.err
}
func (e *Encoder) WriteBool(b bool) error {
	if b {
		return e.WriteByte(1)
	}
	return e.WriteByte(0)
}
func (e *Encoder) WriteUint64(u uint64) error {
	if e.err != nil {
		return e.err
	}
	binary.LittleEndian.PutUint64(e.buf[:8], u)
	e.Write(e.buf[:8])
	return e.err
}
func (e *Encoder) WriteInt(i int) error {
	return e.WriteUint64(uint64(i))
}
func (e *Encoder) WritePrefixedBytes(p []byte) error {
	e.WriteInt(len(p))
	e.Write(p)
	return e.err
}
func (e *Encoder) Err() error {
	return e.err
}
func (e *Encoder) Encode(v interface{}) error {
	return e.encode(reflect.ValueOf(v))
}
func (e *Encoder) EncodeAll(vs ...interface{}) error {
	for _, v := range vs {
		if err := e.Encode(v); err != nil {
			return err
		}
	}
	return nil
}
func (e *Encoder) encode(val reflect.Value) error {
	if e.err != nil {
		return e.err
	}
	if val.CanInterface() {
		if m, ok := val.Interface().(SiaMarshaler); ok {
			return m.MarshalSia(e.w)
		}
	}
	switch val.Kind() {
	case reflect.Ptr:
		if err := e.Encode(!val.IsNil()); err != nil {
			return err
		}
		if !val.IsNil() {
			return e.encode(val.Elem())
		}
	case reflect.Bool:
		return e.WriteBool(val.Bool())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return e.WriteUint64(uint64(val.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return e.WriteUint64(val.Uint())
	case reflect.String:
		return e.WritePrefixedBytes([]byte(val.String()))
	case reflect.Slice:
		if err := e.WriteInt(val.Len()); err != nil {
			return err
		}
		if val.Len() == 0 {
			return nil
		}
		fallthrough
	case reflect.Array:
		if val.Type().Elem().Kind() == reflect.Uint8 {
			if val.CanAddr() {
				_, err := e.Write(val.Slice(0, val.Len()).Bytes())
				return err
			}
			slice := reflect.MakeSlice(reflect.SliceOf(val.Type().Elem()), val.Len(), val.Len())
			reflect.Copy(slice, val)
			_, err := e.Write(slice.Bytes())
			return err
		}
		for i := 0; i < val.Len(); i++ {
			if err := e.encode(val.Index(i)); err != nil {
				return err
			}
		}
		return nil
	case reflect.Struct:
		for i := 0; i < val.NumField(); i++ {
			if err := e.encode(val.Field(i)); err != nil {
				return err
			}
		}
		return nil
	}
	panic("could not marshal type " + val.Type().String())
}
func NewEncoder(w io.Writer) *Encoder {
	if e, ok := w.(*Encoder); ok {
		return e
	}
	return &Encoder{w: w}
}
func Marshal(v interface{}) []byte {
	b := new(bytes.Buffer)
	NewEncoder(b).Encode(v)  
	return b.Bytes()
}
func MarshalAll(vs ...interface{}) []byte {
	b := new(bytes.Buffer)
	enc := NewEncoder(b)
	_ = enc.EncodeAll(vs...)
	return b.Bytes()
}
func WriteFile(filename string, v interface{}) error {
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()
	err = NewEncoder(file).Encode(v)
	if err != nil {
		return errors.New("error while writing " + filename + ": " + err.Error())
	}
	return nil
}
type Decoder struct {
	r   io.Reader
	buf [8]byte
	err error
	n   int  
}
func (d *Decoder) Read(p []byte) (int, error) {
	if d.err != nil {
		return 0, d.err
	}
	var n int
	n, d.err = d.r.Read(p)
	d.n += n
	if d.n > MaxObjectSize {
		d.err = ErrObjectTooLarge(d.n)
	}
	return n, d.err
}
func (d *Decoder) ReadFull(p []byte) {
	if d.err != nil {
		return
	}
	n, err := io.ReadFull(d.r, p)
	if err != nil {
		d.err = err
	}
	d.n += n
	if d.n > MaxObjectSize {
		d.err = ErrObjectTooLarge(d.n)
	}
}
func (d *Decoder) ReadPrefixedBytes() []byte {
	n := d.NextPrefix(1)  
	if buf, ok := d.r.(*bytes.Buffer); ok {
		b := buf.Next(int(n))
		d.n += len(b)
		if len(b) < int(n) && d.err == nil {
			d.err = io.ErrUnexpectedEOF
		}
		return b
	}
	b := make([]byte, n)
	d.ReadFull(b)
	if d.err != nil {
		return nil
	}
	return b
}
func (d *Decoder) NextUint64() uint64 {
	d.ReadFull(d.buf[:8])
	if d.err != nil {
		return 0
	}
	return DecUint64(d.buf[:])
}
func (d *Decoder) NextBool() bool {
	d.ReadFull(d.buf[:1])
	if d.buf[0] > 1 && d.err == nil {
		d.err = errors.New("boolean value was not 0 or 1")
	}
	return d.buf[0] == 1
}