func ReadStringAsBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
	var tmp []byte
	tmp, o, err = ReadStringZC(b)
	v = append(scratch[:0], tmp...)
	return
}
ReadStringAsBytes reads a 'str' object
into a slice of bytes. 'v' is the value of
the 'str' object, which may reside in memory
pointed to by 'scratch.' 'o' is the remaining bytes
in 'b.''
Possible errors:
- ErrShortBytes (b not long enough)
- TypeError{} (not 'str' type)
- InvalidPrefixError (unknown type marker)
package msgp
import (
	"bytes"
	"encoding/binary"
	"math"
	"time"
)
var big = binary.BigEndian
func NextType(b []byte) Type {
	if len(b) == 0 {
		return InvalidType
	}
	spec := sizes[b[0]]
	t := spec.typ
	if t == ExtensionType && len(b) > int(spec.size) {
		var tp int8
		if spec.extra == constsize {
			tp = int8(b[1])
		} else {
			tp = int8(b[spec.size-1])
		}
		switch tp {
		case TimeExtension:
			return TimeType
		case Complex128Extension:
			return Complex128Type
		case Complex64Extension:
			return Complex64Type
		default:
			return ExtensionType
		}
	}
	return t
}
func IsNil(b []byte) bool {
	if len(b) != 0 && b[0] == mnil {
		return true
	}
	return false
}
type Raw []byte
func (r Raw) MarshalMsg(b []byte) ([]byte, error) {
	i := len(r)
	if i == 0 {
		return AppendNil(b), nil
	}
	o, l := ensure(b, i)
	copy(o[l:], []byte(r))
	return o, nil
}
func (r *Raw) UnmarshalMsg(b []byte) ([]byte, error) {
	l := len(b)
	out, err := Skip(b)
	if err != nil {
		return b, err
	}
	rlen := l - len(out)
	if IsNil(b[:rlen]) {
		rlen = 0
	}
	if cap(*r) < rlen {
		*r = make(Raw, rlen)
	} else {
		*r = (*r)[0:rlen]
	}
	copy(*r, b[:rlen])
	return out, nil
}
func (r Raw) EncodeMsg(w *Writer) error {
	if len(r) == 0 {
		return w.WriteNil()
	}
	_, err := w.Write([]byte(r))
	return err
}
func (r *Raw) DecodeMsg(f *Reader) error {
	*r = (*r)[:0]
	err := appendNext(f, (*[]byte)(r))
	if IsNil(*r) {
		*r = (*r)[:0]
	}
	return err
}
func (r Raw) Msgsize() int {
	l := len(r)
	if l == 0 {
		return 1  
	}
	return l
}
func appendNext(f *Reader, d *[]byte) error {
	amt, o, err := getNextSize(f.R)
	if err != nil {
		return err
	}
	var i int
	*d, i = ensure(*d, int(amt))
	_, err = f.R.ReadFull((*d)[i:])
	if err != nil {
		return err
	}
	for o > 0 {
		err = appendNext(f, d)
		if err != nil {
			return err
		}
		o--
	}
	return nil
}
func (r *Raw) MarshalJSON() ([]byte, error) {
	var buf bytes.Buffer
	_, err := UnmarshalAsJSON(&buf, []byte(*r))
	return buf.Bytes(), err
}
func ReadMapHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
	l := len(b)
	if l < 1 {
		err = ErrShortBytes
		return
	}
	lead := b[0]
	if isfixmap(lead) {
		sz = uint32(rfixmap(lead))
		o = b[1:]
		return
	}
	switch lead {
	case mmap16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		sz = uint32(big.Uint16(b[1:]))
		o = b[3:]
		return
	case mmap32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		sz = big.Uint32(b[1:])
		o = b[5:]
		return
	default:
		err = badPrefix(MapType, lead)
		return
	}
}
func ReadMapKeyZC(b []byte) ([]byte, []byte, error) {
	o, x, err := ReadStringZC(b)
	if err != nil {
		if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
			return ReadBytesZC(b)
		}
		return nil, b, err
	}
	return o, x, nil
}
func ReadArrayHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
	if len(b) < 1 {
		return 0, nil, ErrShortBytes
	}
	lead := b[0]
	if isfixarray(lead) {
		sz = uint32(rfixarray(lead))
		o = b[1:]
		return
	}
	switch lead {
	case marray16:
		if len(b) < 3 {
			err = ErrShortBytes
			return
		}
		sz = uint32(big.Uint16(b[1:]))
		o = b[3:]
		return
	case marray32:
		if len(b) < 5 {
			err = ErrShortBytes
			return
		}
		sz = big.Uint32(b[1:])
		o = b[5:]
		return
	default:
		err = badPrefix(ArrayType, lead)
		return
	}
}
func ReadNilBytes(b []byte) ([]byte, error) {
	if len(b) < 1 {
		return nil, ErrShortBytes
	}
	if b[0] != mnil {
		return b, badPrefix(NilType, b[0])
	}
	return b[1:], nil
}
func ReadFloat64Bytes(b []byte) (f float64, o []byte, err error) {
	if len(b) < 9 {
		if len(b) >= 5 && b[0] == mfloat32 {
			var tf float32
			tf, o, err = ReadFloat32Bytes(b)
			f = float64(tf)
			return
		}
		err = ErrShortBytes
		return
	}
	if b[0] != mfloat64 {
		if b[0] == mfloat32 {
			var tf float32
			tf, o, err = ReadFloat32Bytes(b)
			f = float64(tf)
			return
		}
		err = badPrefix(Float64Type, b[0])
		return
	}
	f = math.Float64frombits(getMuint64(b))
	o = b[9:]
	return
}
func ReadFloat32Bytes(b []byte) (f float32, o []byte, err error) {
	if len(b) < 5 {
		err = ErrShortBytes
		return
	}
	if b[0] != mfloat32 {
		err = TypeError{Method: Float32Type, Encoded: getType(b[0])}
		return
	}
	f = math.Float32frombits(getMuint32(b))
	o = b[5:]
	return
}
func ReadBoolBytes(b []byte) (bool, []byte, error) {
	if len(b) < 1 {
		return false, b, ErrShortBytes
	}
	switch b[0] {
	case mtrue:
		return true, b[1:], nil
	case mfalse:
		return false, b[1:], nil
	default:
		return false, b, badPrefix(BoolType, b[0])
	}
}
func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
	l := len(b)
	if l < 1 {
		return 0, nil, ErrShortBytes
	}
	lead := b[0]
	if isfixint(lead) {
		i = int64(rfixint(lead))
		o = b[1:]
		return
	}
	if isnfixint(lead) {
		i = int64(rnfixint(lead))
		o = b[1:]
		return
	}
	switch lead {
	case mint8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		i = int64(getMint8(b))
		o = b[2:]
		return
	case muint8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		i = int64(getMuint8(b))
		o = b[2:]
		return
	case mint16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		i = int64(getMint16(b))
		o = b[3:]
		return
	case muint16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		i = int64(getMuint16(b))
		o = b[3:]
		return
	case mint32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		i = int64(getMint32(b))
		o = b[5:]
		return
	case muint32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		i = int64(getMuint32(b))
		o = b[5:]
		return
	case mint64:
		if l < 9 {
			err = ErrShortBytes
			return
		}
		i = int64(getMint64(b))
		o = b[9:]
		return
	case muint64:
		if l < 9 {
			err = ErrShortBytes
			return
		}
		u := getMuint64(b)
		if u > math.MaxInt64 {
			err = UintOverflow{Value: u, FailedBitsize: 64}
			return
		}
		i = int64(u)
		o = b[9:]
		return
	default:
		err = badPrefix(IntType, lead)
		return
	}
}
func ReadInt32Bytes(b []byte) (int32, []byte, error) {
	i, o, err := ReadInt64Bytes(b)
	if i > math.MaxInt32 || i < math.MinInt32 {
		return 0, o, IntOverflow{Value: i, FailedBitsize: 32}
	}
	return int32(i), o, err
}
func ReadInt16Bytes(b []byte) (int16, []byte, error) {
	i, o, err := ReadInt64Bytes(b)
	if i > math.MaxInt16 || i < math.MinInt16 {
		return 0, o, IntOverflow{Value: i, FailedBitsize: 16}
	}
	return int16(i), o, err
}
func ReadInt8Bytes(b []byte) (int8, []byte, error) {
	i, o, err := ReadInt64Bytes(b)
	if i > math.MaxInt8 || i < math.MinInt8 {
		return 0, o, IntOverflow{Value: i, FailedBitsize: 8}
	}
	return int8(i), o, err
}
func ReadIntBytes(b []byte) (int, []byte, error) {
	if smallint {
		i, b, err := ReadInt32Bytes(b)
		return int(i), b, err
	}
	i, b, err := ReadInt64Bytes(b)
	return int(i), b, err
}
func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
	l := len(b)
	if l < 1 {
		return 0, nil, ErrShortBytes
	}
	lead := b[0]
	if isfixint(lead) {
		u = uint64(rfixint(lead))
		o = b[1:]
		return
	}
	switch lead {
	case mint8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		v := int64(getMint8(b))
		if v < 0 {
			err = UintBelowZero{Value: v}
			return
		}
		u = uint64(v)
		o = b[2:]
		return
	case muint8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		u = uint64(getMuint8(b))
		o = b[2:]
		return
	case mint16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		v := int64(getMint16(b))
		if v < 0 {
			err = UintBelowZero{Value: v}
			return
		}
		u = uint64(v)
		o = b[3:]
		return
	case muint16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		u = uint64(getMuint16(b))
		o = b[3:]
		return
	case mint32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		v := int64(getMint32(b))
		if v < 0 {
			err = UintBelowZero{Value: v}
			return
		}
		u = uint64(v)
		o = b[5:]
		return
	case muint32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		u = uint64(getMuint32(b))
		o = b[5:]
		return
	case mint64:
		if l < 9 {
			err = ErrShortBytes
			return
		}
		v := int64(getMint64(b))
		if v < 0 {
			err = UintBelowZero{Value: v}
			return
		}
		u = uint64(v)
		o = b[9:]
		return
	case muint64:
		if l < 9 {
			err = ErrShortBytes
			return
		}
		u = getMuint64(b)
		o = b[9:]
		return
	default:
		if isnfixint(lead) {
			err = UintBelowZero{Value: int64(rnfixint(lead))}
		} else {
			err = badPrefix(UintType, lead)
		}
		return
	}
}
func ReadUint32Bytes(b []byte) (uint32, []byte, error) {
	v, o, err := ReadUint64Bytes(b)
	if v > math.MaxUint32 {
		return 0, nil, UintOverflow{Value: v, FailedBitsize: 32}
	}
	return uint32(v), o, err
}
func ReadUint16Bytes(b []byte) (uint16, []byte, error) {
	v, o, err := ReadUint64Bytes(b)
	if v > math.MaxUint16 {
		return 0, nil, UintOverflow{Value: v, FailedBitsize: 16}
	}
	return uint16(v), o, err
}
func ReadUint8Bytes(b []byte) (uint8, []byte, error) {
	v, o, err := ReadUint64Bytes(b)
	if v > math.MaxUint8 {
		return 0, nil, UintOverflow{Value: v, FailedBitsize: 8}
	}
	return uint8(v), o, err
}
func ReadUintBytes(b []byte) (uint, []byte, error) {
	if smallint {
		u, b, err := ReadUint32Bytes(b)
		return uint(u), b, err
	}
	u, b, err := ReadUint64Bytes(b)
	return uint(u), b, err
}
func ReadByteBytes(b []byte) (byte, []byte, error) {
	return ReadUint8Bytes(b)
}
func ReadBytesBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
	return readBytesBytes(b, scratch, false)
}
func readBytesBytes(b, scratch []byte, zc bool) (v, o []byte, err error) {
	l := len(b)
	if l < 1 {
		return nil, nil, ErrShortBytes
	}
	lead := b[0]
	var read int
	switch lead {
	case mbin8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		read = int(b[1])
		b = b[2:]
	case mbin16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		read = int(big.Uint16(b[1:]))
		b = b[3:]
	case mbin32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		read = int(big.Uint32(b[1:]))
		b = b[5:]
	default:
		err = badPrefix(BinType, lead)
		return
	}
	if len(b) < read {
		err = ErrShortBytes
		return
	}
	if zc {
		v = b[0:read]
		o = b[read:]
		return
	}
	if cap(scratch) >= read {
		v = scratch[0:read]
	} else {
		v = make([]byte, read)
	}
	o = b[copy(v, b):]
	return
}
func ReadBytesZC(b []byte) (v, o []byte, err error) {
	return readBytesBytes(b, nil, true)
}
func ReadExactBytes(b []byte, into []byte) (o []byte, err error) {
	l := len(b)
	if l < 1 {
		err = ErrShortBytes
		return
	}
	lead := b[0]
	var read uint32
	var skip int
	switch lead {
	case mbin8:
		if l < 2 {
			err = ErrShortBytes
			return
		}
		read = uint32(b[1])
		skip = 2
	case mbin16:
		if l < 3 {
			err = ErrShortBytes
			return
		}
		read = uint32(big.Uint16(b[1:]))
		skip = 3
	case mbin32:
		if l < 5 {
			err = ErrShortBytes
			return
		}
		read = uint32(big.Uint32(b[1:]))
		skip = 5
	default:
		err = badPrefix(BinType, lead)
		return
	}
	if read != uint32(len(into)) {
		err = ArrayError{Wanted: uint32(len(into)), Got: read}
		return
	}
	o = b[skip+copy(into, b[skip:]):]
	return
}
func ReadStringZC(b []byte) (v []byte, o []byte, err error) {
	l := len(b)
	if l < 1 {
		return nil, nil, ErrShortBytes
	}
	lead := b[0]
	var read int
	if isfixstr(lead) {
		read = int(rfixstr(lead))
		b = b[1:]
	} else {
		switch lead {
		case mstr8:
			if l < 2 {
				err = ErrShortBytes
				return
			}
			read = int(b[1])
			b = b[2:]
		case mstr16:
			if l < 3 {
				err = ErrShortBytes
				return
			}
			read = int(big.Uint16(b[1:]))
			b = b[3:]
		case mstr32:
			if l < 5 {
				err = ErrShortBytes
				return
			}
			read = int(big.Uint32(b[1:]))
			b = b[5:]
		default:
			err = TypeError{Method: StrType, Encoded: getType(lead)}
			return
		}
	}
	if len(b) < read {
		err = ErrShortBytes
		return
	}
	v = b[0:read]
	o = b[read:]
	return
}
func ReadStringBytes(b []byte) (string, []byte, error) {
	v, o, err := ReadStringZC(b)
	return string(v), o, err
}