1package msgp
2
3import (
4	"bytes"
5	"encoding/binary"
6	"math"
7	"time"
8)
9
10var big = binary.BigEndian
11
12// NextType returns the type of the next
13// object in the slice. If the length
14// of the input is zero, it returns
15// InvalidType.
16func NextType(b []byte) Type {
17	if len(b) == 0 {
18		return InvalidType
19	}
20	spec := sizes[b[0]]
21	t := spec.typ
22	if t == ExtensionType && len(b) > int(spec.size) {
23		var tp int8
24		if spec.extra == constsize {
25			tp = int8(b[1])
26		} else {
27			tp = int8(b[spec.size-1])
28		}
29		switch tp {
30		case TimeExtension:
31			return TimeType
32		case Complex128Extension:
33			return Complex128Type
34		case Complex64Extension:
35			return Complex64Type
36		default:
37			return ExtensionType
38		}
39	}
40	return t
41}
42
43// IsNil returns true if len(b)>0 and
44// the leading byte is a 'nil' MessagePack
45// byte; false otherwise
46func IsNil(b []byte) bool {
47	if len(b) != 0 && b[0] == mnil {
48		return true
49	}
50	return false
51}
52
53// Raw is raw MessagePack.
54// Raw allows you to read and write
55// data without interpreting its contents.
56type Raw []byte
57
58// MarshalMsg implements msgp.Marshaler.
59// It appends the raw contents of 'raw'
60// to the provided byte slice. If 'raw'
61// is 0 bytes, 'nil' will be appended instead.
62func (r Raw) MarshalMsg(b []byte) ([]byte, error) {
63	i := len(r)
64	if i == 0 {
65		return AppendNil(b), nil
66	}
67	o, l := ensure(b, i)
68	copy(o[l:], []byte(r))
69	return o, nil
70}
71
72// UnmarshalMsg implements msgp.Unmarshaler.
73// It sets the contents of *Raw to be the next
74// object in the provided byte slice.
75func (r *Raw) UnmarshalMsg(b []byte) ([]byte, error) {
76	l := len(b)
77	out, err := Skip(b)
78	if err != nil {
79		return b, err
80	}
81	rlen := l - len(out)
82	if IsNil(b[:rlen]) {
83		rlen = 0
84	}
85	if cap(*r) < rlen {
86		*r = make(Raw, rlen)
87	} else {
88		*r = (*r)[0:rlen]
89	}
90	copy(*r, b[:rlen])
91	return out, nil
92}
93
94// EncodeMsg implements msgp.Encodable.
95// It writes the raw bytes to the writer.
96// If r is empty, it writes 'nil' instead.
97func (r Raw) EncodeMsg(w *Writer) error {
98	if len(r) == 0 {
99		return w.WriteNil()
100	}
101	_, err := w.Write([]byte(r))
102	return err
103}
104
105// DecodeMsg implements msgp.Decodable.
106// It sets the value of *Raw to be the
107// next object on the wire.
108func (r *Raw) DecodeMsg(f *Reader) error {
109	*r = (*r)[:0]
110	err := appendNext(f, (*[]byte)(r))
111	if IsNil(*r) {
112		*r = (*r)[:0]
113	}
114	return err
115}
116
117// Msgsize implements msgp.Sizer
118func (r Raw) Msgsize() int {
119	l := len(r)
120	if l == 0 {
121		return 1 // for 'nil'
122	}
123	return l
124}
125
126func appendNext(f *Reader, d *[]byte) error {
127	amt, o, err := getNextSize(f.R)
128	if err != nil {
129		return err
130	}
131	var i int
132	*d, i = ensure(*d, int(amt))
133	_, err = f.R.ReadFull((*d)[i:])
134	if err != nil {
135		return err
136	}
137	for o > 0 {
138		err = appendNext(f, d)
139		if err != nil {
140			return err
141		}
142		o--
143	}
144	return nil
145}
146
147// MarshalJSON implements json.Marshaler
148func (r *Raw) MarshalJSON() ([]byte, error) {
149	var buf bytes.Buffer
150	_, err := UnmarshalAsJSON(&buf, []byte(*r))
151	return buf.Bytes(), err
152}
153
154// ReadMapHeaderBytes reads a map header size
155// from 'b' and returns the remaining bytes.
156// Possible errors:
157// - ErrShortBytes (too few bytes)
158// - TypeError{} (not a map)
159func ReadMapHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
160	l := len(b)
161	if l < 1 {
162		err = ErrShortBytes
163		return
164	}
165
166	lead := b[0]
167	if isfixmap(lead) {
168		sz = uint32(rfixmap(lead))
169		o = b[1:]
170		return
171	}
172
173	switch lead {
174	case mmap16:
175		if l < 3 {
176			err = ErrShortBytes
177			return
178		}
179		sz = uint32(big.Uint16(b[1:]))
180		o = b[3:]
181		return
182
183	case mmap32:
184		if l < 5 {
185			err = ErrShortBytes
186			return
187		}
188		sz = big.Uint32(b[1:])
189		o = b[5:]
190		return
191
192	default:
193		err = badPrefix(MapType, lead)
194		return
195	}
196}
197
198// ReadMapKeyZC attempts to read a map key
199// from 'b' and returns the key bytes and the remaining bytes
200// Possible errors:
201// - ErrShortBytes (too few bytes)
202// - TypeError{} (not a str or bin)
203func ReadMapKeyZC(b []byte) ([]byte, []byte, error) {
204	o, x, err := ReadStringZC(b)
205	if err != nil {
206		if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
207			return ReadBytesZC(b)
208		}
209		return nil, b, err
210	}
211	return o, x, nil
212}
213
214// ReadArrayHeaderBytes attempts to read
215// the array header size off of 'b' and return
216// the size and remaining bytes.
217// Possible errors:
218// - ErrShortBytes (too few bytes)
219// - TypeError{} (not an array)
220func ReadArrayHeaderBytes(b []byte) (sz uint32, o []byte, err error) {
221	if len(b) < 1 {
222		return 0, nil, ErrShortBytes
223	}
224	lead := b[0]
225	if isfixarray(lead) {
226		sz = uint32(rfixarray(lead))
227		o = b[1:]
228		return
229	}
230
231	switch lead {
232	case marray16:
233		if len(b) < 3 {
234			err = ErrShortBytes
235			return
236		}
237		sz = uint32(big.Uint16(b[1:]))
238		o = b[3:]
239		return
240
241	case marray32:
242		if len(b) < 5 {
243			err = ErrShortBytes
244			return
245		}
246		sz = big.Uint32(b[1:])
247		o = b[5:]
248		return
249
250	default:
251		err = badPrefix(ArrayType, lead)
252		return
253	}
254}
255
256// ReadBytesHeader reads the 'bin' header size
257// off of 'b' and returns the size and remaining bytes.
258// Possible errors:
259// - ErrShortBytes (too few bytes)
260// - TypeError{} (not a bin object)
261func ReadBytesHeader(b []byte) (sz uint32, o []byte, err error) {
262	if len(b) < 1 {
263		return 0, nil, ErrShortBytes
264	}
265	switch b[0] {
266	case mbin8:
267		if len(b) < 2 {
268			err = ErrShortBytes
269			return
270		}
271		sz = uint32(b[1])
272		o = b[2:]
273		return
274	case mbin16:
275		if len(b) < 3 {
276			err = ErrShortBytes
277			return
278		}
279		sz = uint32(big.Uint16(b[1:]))
280		o = b[3:]
281		return
282	case mbin32:
283		if len(b) < 5 {
284			err = ErrShortBytes
285			return
286		}
287		sz = big.Uint32(b[1:])
288		o = b[5:]
289		return
290	default:
291		err = badPrefix(BinType, b[0])
292		return
293	}
294}
295
296// ReadNilBytes tries to read a "nil" byte
297// off of 'b' and return the remaining bytes.
298// Possible errors:
299// - ErrShortBytes (too few bytes)
300// - TypeError{} (not a 'nil')
301// - InvalidPrefixError
302func ReadNilBytes(b []byte) ([]byte, error) {
303	if len(b) < 1 {
304		return nil, ErrShortBytes
305	}
306	if b[0] != mnil {
307		return b, badPrefix(NilType, b[0])
308	}
309	return b[1:], nil
310}
311
312// ReadFloat64Bytes tries to read a float64
313// from 'b' and return the value and the remaining bytes.
314// Possible errors:
315// - ErrShortBytes (too few bytes)
316// - TypeError{} (not a float64)
317func ReadFloat64Bytes(b []byte) (f float64, o []byte, err error) {
318	if len(b) < 9 {
319		if len(b) >= 5 && b[0] == mfloat32 {
320			var tf float32
321			tf, o, err = ReadFloat32Bytes(b)
322			f = float64(tf)
323			return
324		}
325		err = ErrShortBytes
326		return
327	}
328
329	if b[0] != mfloat64 {
330		if b[0] == mfloat32 {
331			var tf float32
332			tf, o, err = ReadFloat32Bytes(b)
333			f = float64(tf)
334			return
335		}
336		err = badPrefix(Float64Type, b[0])
337		return
338	}
339
340	f = math.Float64frombits(getMuint64(b))
341	o = b[9:]
342	return
343}
344
345// ReadFloat32Bytes tries to read a float64
346// from 'b' and return the value and the remaining bytes.
347// Possible errors:
348// - ErrShortBytes (too few bytes)
349// - TypeError{} (not a float32)
350func ReadFloat32Bytes(b []byte) (f float32, o []byte, err error) {
351	if len(b) < 5 {
352		err = ErrShortBytes
353		return
354	}
355
356	if b[0] != mfloat32 {
357		err = TypeError{Method: Float32Type, Encoded: getType(b[0])}
358		return
359	}
360
361	f = math.Float32frombits(getMuint32(b))
362	o = b[5:]
363	return
364}
365
366// ReadBoolBytes tries to read a float64
367// from 'b' and return the value and the remaining bytes.
368// Possible errors:
369// - ErrShortBytes (too few bytes)
370// - TypeError{} (not a bool)
371func ReadBoolBytes(b []byte) (bool, []byte, error) {
372	if len(b) < 1 {
373		return false, b, ErrShortBytes
374	}
375	switch b[0] {
376	case mtrue:
377		return true, b[1:], nil
378	case mfalse:
379		return false, b[1:], nil
380	default:
381		return false, b, badPrefix(BoolType, b[0])
382	}
383}
384
385// ReadInt64Bytes tries to read an int64
386// from 'b' and return the value and the remaining bytes.
387// Possible errors:
388// - ErrShortBytes (too few bytes)
389// - TypeError (not a int)
390func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
391	l := len(b)
392	if l < 1 {
393		return 0, nil, ErrShortBytes
394	}
395
396	lead := b[0]
397	if isfixint(lead) {
398		i = int64(rfixint(lead))
399		o = b[1:]
400		return
401	}
402	if isnfixint(lead) {
403		i = int64(rnfixint(lead))
404		o = b[1:]
405		return
406	}
407
408	switch lead {
409	case mint8:
410		if l < 2 {
411			err = ErrShortBytes
412			return
413		}
414		i = int64(getMint8(b))
415		o = b[2:]
416		return
417
418	case muint8:
419		if l < 2 {
420			err = ErrShortBytes
421			return
422		}
423		i = int64(getMuint8(b))
424		o = b[2:]
425		return
426
427	case mint16:
428		if l < 3 {
429			err = ErrShortBytes
430			return
431		}
432		i = int64(getMint16(b))
433		o = b[3:]
434		return
435
436	case muint16:
437		if l < 3 {
438			err = ErrShortBytes
439			return
440		}
441		i = int64(getMuint16(b))
442		o = b[3:]
443		return
444
445	case mint32:
446		if l < 5 {
447			err = ErrShortBytes
448			return
449		}
450		i = int64(getMint32(b))
451		o = b[5:]
452		return
453
454	case muint32:
455		if l < 5 {
456			err = ErrShortBytes
457			return
458		}
459		i = int64(getMuint32(b))
460		o = b[5:]
461		return
462
463	case mint64:
464		if l < 9 {
465			err = ErrShortBytes
466			return
467		}
468		i = int64(getMint64(b))
469		o = b[9:]
470		return
471
472	case muint64:
473		if l < 9 {
474			err = ErrShortBytes
475			return
476		}
477		u := getMuint64(b)
478		if u > math.MaxInt64 {
479			err = UintOverflow{Value: u, FailedBitsize: 64}
480			return
481		}
482		i = int64(u)
483		o = b[9:]
484		return
485
486	default:
487		err = badPrefix(IntType, lead)
488		return
489	}
490}
491
492// ReadInt32Bytes tries to read an int32
493// from 'b' and return the value and the remaining bytes.
494// Possible errors:
495// - ErrShortBytes (too few bytes)
496// - TypeError{} (not a int)
497// - IntOverflow{} (value doesn't fit in int32)
498func ReadInt32Bytes(b []byte) (int32, []byte, error) {
499	i, o, err := ReadInt64Bytes(b)
500	if i > math.MaxInt32 || i < math.MinInt32 {
501		return 0, o, IntOverflow{Value: i, FailedBitsize: 32}
502	}
503	return int32(i), o, err
504}
505
506// ReadInt16Bytes tries to read an int16
507// from 'b' and return the value and the remaining bytes.
508// Possible errors:
509// - ErrShortBytes (too few bytes)
510// - TypeError{} (not a int)
511// - IntOverflow{} (value doesn't fit in int16)
512func ReadInt16Bytes(b []byte) (int16, []byte, error) {
513	i, o, err := ReadInt64Bytes(b)
514	if i > math.MaxInt16 || i < math.MinInt16 {
515		return 0, o, IntOverflow{Value: i, FailedBitsize: 16}
516	}
517	return int16(i), o, err
518}
519
520// ReadInt8Bytes tries to read an int16
521// from 'b' and return the value and the remaining bytes.
522// Possible errors:
523// - ErrShortBytes (too few bytes)
524// - TypeError{} (not a int)
525// - IntOverflow{} (value doesn't fit in int8)
526func ReadInt8Bytes(b []byte) (int8, []byte, error) {
527	i, o, err := ReadInt64Bytes(b)
528	if i > math.MaxInt8 || i < math.MinInt8 {
529		return 0, o, IntOverflow{Value: i, FailedBitsize: 8}
530	}
531	return int8(i), o, err
532}
533
534// ReadIntBytes tries to read an int
535// from 'b' and return the value and the remaining bytes.
536// Possible errors:
537// - ErrShortBytes (too few bytes)
538// - TypeError{} (not a int)
539// - IntOverflow{} (value doesn't fit in int; 32-bit platforms only)
540func ReadIntBytes(b []byte) (int, []byte, error) {
541	if smallint {
542		i, b, err := ReadInt32Bytes(b)
543		return int(i), b, err
544	}
545	i, b, err := ReadInt64Bytes(b)
546	return int(i), b, err
547}
548
549// ReadUint64Bytes tries to read a uint64
550// from 'b' and return the value and the remaining bytes.
551// Possible errors:
552// - ErrShortBytes (too few bytes)
553// - TypeError{} (not a uint)
554func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
555	l := len(b)
556	if l < 1 {
557		return 0, nil, ErrShortBytes
558	}
559
560	lead := b[0]
561	if isfixint(lead) {
562		u = uint64(rfixint(lead))
563		o = b[1:]
564		return
565	}
566
567	switch lead {
568	case mint8:
569		if l < 2 {
570			err = ErrShortBytes
571			return
572		}
573		v := int64(getMint8(b))
574		if v < 0 {
575			err = UintBelowZero{Value: v}
576			return
577		}
578		u = uint64(v)
579		o = b[2:]
580		return
581
582	case muint8:
583		if l < 2 {
584			err = ErrShortBytes
585			return
586		}
587		u = uint64(getMuint8(b))
588		o = b[2:]
589		return
590
591	case mint16:
592		if l < 3 {
593			err = ErrShortBytes
594			return
595		}
596		v := int64(getMint16(b))
597		if v < 0 {
598			err = UintBelowZero{Value: v}
599			return
600		}
601		u = uint64(v)
602		o = b[3:]
603		return
604
605	case muint16:
606		if l < 3 {
607			err = ErrShortBytes
608			return
609		}
610		u = uint64(getMuint16(b))
611		o = b[3:]
612		return
613
614	case mint32:
615		if l < 5 {
616			err = ErrShortBytes
617			return
618		}
619		v := int64(getMint32(b))
620		if v < 0 {
621			err = UintBelowZero{Value: v}
622			return
623		}
624		u = uint64(v)
625		o = b[5:]
626		return
627
628	case muint32:
629		if l < 5 {
630			err = ErrShortBytes
631			return
632		}
633		u = uint64(getMuint32(b))
634		o = b[5:]
635		return
636
637	case mint64:
638		if l < 9 {
639			err = ErrShortBytes
640			return
641		}
642		v := int64(getMint64(b))
643		if v < 0 {
644			err = UintBelowZero{Value: v}
645			return
646		}
647		u = uint64(v)
648		o = b[9:]
649		return
650
651	case muint64:
652		if l < 9 {
653			err = ErrShortBytes
654			return
655		}
656		u = getMuint64(b)
657		o = b[9:]
658		return
659
660	default:
661		if isnfixint(lead) {
662			err = UintBelowZero{Value: int64(rnfixint(lead))}
663		} else {
664			err = badPrefix(UintType, lead)
665		}
666		return
667	}
668}
669
670// ReadUint32Bytes tries to read a uint32
671// from 'b' and return the value and the remaining bytes.
672// Possible errors:
673// - ErrShortBytes (too few bytes)
674// - TypeError{} (not a uint)
675// - UintOverflow{} (value too large for uint32)
676func ReadUint32Bytes(b []byte) (uint32, []byte, error) {
677	v, o, err := ReadUint64Bytes(b)
678	if v > math.MaxUint32 {
679		return 0, nil, UintOverflow{Value: v, FailedBitsize: 32}
680	}
681	return uint32(v), o, err
682}
683
684// ReadUint16Bytes tries to read a uint16
685// from 'b' and return the value and the remaining bytes.
686// Possible errors:
687// - ErrShortBytes (too few bytes)
688// - TypeError{} (not a uint)
689// - UintOverflow{} (value too large for uint16)
690func ReadUint16Bytes(b []byte) (uint16, []byte, error) {
691	v, o, err := ReadUint64Bytes(b)
692	if v > math.MaxUint16 {
693		return 0, nil, UintOverflow{Value: v, FailedBitsize: 16}
694	}
695	return uint16(v), o, err
696}
697
698// ReadUint8Bytes tries to read a uint8
699// from 'b' and return the value and the remaining bytes.
700// Possible errors:
701// - ErrShortBytes (too few bytes)
702// - TypeError{} (not a uint)
703// - UintOverflow{} (value too large for uint8)
704func ReadUint8Bytes(b []byte) (uint8, []byte, error) {
705	v, o, err := ReadUint64Bytes(b)
706	if v > math.MaxUint8 {
707		return 0, nil, UintOverflow{Value: v, FailedBitsize: 8}
708	}
709	return uint8(v), o, err
710}
711
712// ReadUintBytes tries to read a uint
713// from 'b' and return the value and the remaining bytes.
714// Possible errors:
715// - ErrShortBytes (too few bytes)
716// - TypeError{} (not a uint)
717// - UintOverflow{} (value too large for uint; 32-bit platforms only)
718func ReadUintBytes(b []byte) (uint, []byte, error) {
719	if smallint {
720		u, b, err := ReadUint32Bytes(b)
721		return uint(u), b, err
722	}
723	u, b, err := ReadUint64Bytes(b)
724	return uint(u), b, err
725}
726
727// ReadByteBytes is analogous to ReadUint8Bytes
728func ReadByteBytes(b []byte) (byte, []byte, error) {
729	return ReadUint8Bytes(b)
730}
731
732// ReadBytesBytes reads a 'bin' object
733// from 'b' and returns its vaue and
734// the remaining bytes in 'b'.
735// Possible errors:
736// - ErrShortBytes (too few bytes)
737// - TypeError{} (not a 'bin' object)
738func ReadBytesBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
739	return readBytesBytes(b, scratch, false)
740}
741
742func readBytesBytes(b []byte, scratch []byte, zc bool) (v []byte, o []byte, err error) {
743	l := len(b)
744	if l < 1 {
745		return nil, nil, ErrShortBytes
746	}
747
748	lead := b[0]
749	var read int
750	switch lead {
751	case mbin8:
752		if l < 2 {
753			err = ErrShortBytes
754			return
755		}
756
757		read = int(b[1])
758		b = b[2:]
759
760	case mbin16:
761		if l < 3 {
762			err = ErrShortBytes
763			return
764		}
765		read = int(big.Uint16(b[1:]))
766		b = b[3:]
767
768	case mbin32:
769		if l < 5 {
770			err = ErrShortBytes
771			return
772		}
773		read = int(big.Uint32(b[1:]))
774		b = b[5:]
775
776	default:
777		err = badPrefix(BinType, lead)
778		return
779	}
780
781	if len(b) < read {
782		err = ErrShortBytes
783		return
784	}
785
786	// zero-copy
787	if zc {
788		v = b[0:read]
789		o = b[read:]
790		return
791	}
792
793	if cap(scratch) >= read {
794		v = scratch[0:read]
795	} else {
796		v = make([]byte, read)
797	}
798
799	o = b[copy(v, b):]
800	return
801}
802
803// ReadBytesZC extracts the messagepack-encoded
804// binary field without copying. The returned []byte
805// points to the same memory as the input slice.
806// Possible errors:
807// - ErrShortBytes (b not long enough)
808// - TypeError{} (object not 'bin')
809func ReadBytesZC(b []byte) (v []byte, o []byte, err error) {
810	return readBytesBytes(b, nil, true)
811}
812
813func ReadExactBytes(b []byte, into []byte) (o []byte, err error) {
814	l := len(b)
815	if l < 1 {
816		err = ErrShortBytes
817		return
818	}
819
820	lead := b[0]
821	var read uint32
822	var skip int
823	switch lead {
824	case mbin8:
825		if l < 2 {
826			err = ErrShortBytes
827			return
828		}
829
830		read = uint32(b[1])
831		skip = 2
832
833	case mbin16:
834		if l < 3 {
835			err = ErrShortBytes
836			return
837		}
838		read = uint32(big.Uint16(b[1:]))
839		skip = 3
840
841	case mbin32:
842		if l < 5 {
843			err = ErrShortBytes
844			return
845		}
846		read = uint32(big.Uint32(b[1:]))
847		skip = 5
848
849	default:
850		err = badPrefix(BinType, lead)
851		return
852	}
853
854	if read != uint32(len(into)) {
855		err = ArrayError{Wanted: uint32(len(into)), Got: read}
856		return
857	}
858
859	o = b[skip+copy(into, b[skip:]):]
860	return
861}
862
863// ReadStringZC reads a messagepack string field
864// without copying. The returned []byte points
865// to the same memory as the input slice.
866// Possible errors:
867// - ErrShortBytes (b not long enough)
868// - TypeError{} (object not 'str')
869func ReadStringZC(b []byte) (v []byte, o []byte, err error) {
870	l := len(b)
871	if l < 1 {
872		return nil, nil, ErrShortBytes
873	}
874
875	lead := b[0]
876	var read int
877
878	if isfixstr(lead) {
879		read = int(rfixstr(lead))
880		b = b[1:]
881	} else {
882		switch lead {
883		case mstr8:
884			if l < 2 {
885				err = ErrShortBytes
886				return
887			}
888			read = int(b[1])
889			b = b[2:]
890
891		case mstr16:
892			if l < 3 {
893				err = ErrShortBytes
894				return
895			}
896			read = int(big.Uint16(b[1:]))
897			b = b[3:]
898
899		case mstr32:
900			if l < 5 {
901				err = ErrShortBytes
902				return
903			}
904			read = int(big.Uint32(b[1:]))
905			b = b[5:]
906
907		default:
908			err = TypeError{Method: StrType, Encoded: getType(lead)}
909			return
910		}
911	}
912
913	if len(b) < read {
914		err = ErrShortBytes
915		return
916	}
917
918	v = b[0:read]
919	o = b[read:]
920	return
921}
922
923// ReadStringBytes reads a 'str' object
924// from 'b' and returns its value and the
925// remaining bytes in 'b'.
926// Possible errors:
927// - ErrShortBytes (b not long enough)
928// - TypeError{} (not 'str' type)
929// - InvalidPrefixError
930func ReadStringBytes(b []byte) (string, []byte, error) {
931	v, o, err := ReadStringZC(b)
932	return string(v), o, err
933}
934
935// ReadStringAsBytes reads a 'str' object
936// into a slice of bytes. 'v' is the value of
937// the 'str' object, which may reside in memory
938// pointed to by 'scratch.' 'o' is the remaining bytes
939// in 'b.''
940// Possible errors:
941// - ErrShortBytes (b not long enough)
942// - TypeError{} (not 'str' type)
943// - InvalidPrefixError (unknown type marker)
944func ReadStringAsBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
945	var tmp []byte
946	tmp, o, err = ReadStringZC(b)
947	v = append(scratch[:0], tmp...)
948	return
949}
950
951// ReadComplex128Bytes reads a complex128
952// extension object from 'b' and returns the
953// remaining bytes.
954// Possible errors:
955// - ErrShortBytes (not enough bytes in 'b')
956// - TypeError{} (object not a complex128)
957// - InvalidPrefixError
958// - ExtensionTypeError{} (object an extension of the correct size, but not a complex128)
959func ReadComplex128Bytes(b []byte) (c complex128, o []byte, err error) {
960	if len(b) < 18 {
961		err = ErrShortBytes
962		return
963	}
964	if b[0] != mfixext16 {
965		err = badPrefix(Complex128Type, b[0])
966		return
967	}
968	if int8(b[1]) != Complex128Extension {
969		err = errExt(int8(b[1]), Complex128Extension)
970		return
971	}
972	c = complex(math.Float64frombits(big.Uint64(b[2:])),
973		math.Float64frombits(big.Uint64(b[10:])))
974	o = b[18:]
975	return
976}
977
978// ReadComplex64Bytes reads a complex64
979// extension object from 'b' and returns the
980// remaining bytes.
981// Possible errors:
982// - ErrShortBytes (not enough bytes in 'b')
983// - TypeError{} (object not a complex64)
984// - ExtensionTypeError{} (object an extension of the correct size, but not a complex64)
985func ReadComplex64Bytes(b []byte) (c complex64, o []byte, err error) {
986	if len(b) < 10 {
987		err = ErrShortBytes
988		return
989	}
990	if b[0] != mfixext8 {
991		err = badPrefix(Complex64Type, b[0])
992		return
993	}
994	if b[1] != Complex64Extension {
995		err = errExt(int8(b[1]), Complex64Extension)
996		return
997	}
998	c = complex(math.Float32frombits(big.Uint32(b[2:])),
999		math.Float32frombits(big.Uint32(b[6:])))
1000	o = b[10:]
1001	return
1002}
1003
1004// ReadTimeBytes reads a time.Time
1005// extension object from 'b' and returns the
1006// remaining bytes.
1007// Possible errors:
1008// - ErrShortBytes (not enough bytes in 'b')
1009// - TypeError{} (object not a complex64)
1010// - ExtensionTypeError{} (object an extension of the correct size, but not a time.Time)
1011func ReadTimeBytes(b []byte) (t time.Time, o []byte, err error) {
1012	if len(b) < 15 {
1013		err = ErrShortBytes
1014		return
1015	}
1016	if b[0] != mext8 || b[1] != 12 {
1017		err = badPrefix(TimeType, b[0])
1018		return
1019	}
1020	if int8(b[2]) != TimeExtension {
1021		err = errExt(int8(b[2]), TimeExtension)
1022		return
1023	}
1024	sec, nsec := getUnix(b[3:])
1025	t = time.Unix(sec, int64(nsec)).Local()
1026	o = b[15:]
1027	return
1028}
1029
1030// ReadMapStrIntfBytes reads a map[string]interface{}
1031// out of 'b' and returns the map and remaining bytes.
1032// If 'old' is non-nil, the values will be read into that map.
1033func ReadMapStrIntfBytes(b []byte, old map[string]interface{}) (v map[string]interface{}, o []byte, err error) {
1034	var sz uint32
1035	o = b
1036	sz, o, err = ReadMapHeaderBytes(o)
1037
1038	if err != nil {
1039		return
1040	}
1041
1042	if old != nil {
1043		for key := range old {
1044			delete(old, key)
1045		}
1046		v = old
1047	} else {
1048		v = make(map[string]interface{}, int(sz))
1049	}
1050
1051	for z := uint32(0); z < sz; z++ {
1052		if len(o) < 1 {
1053			err = ErrShortBytes
1054			return
1055		}
1056		var key []byte
1057		key, o, err = ReadMapKeyZC(o)
1058		if err != nil {
1059			return
1060		}
1061		var val interface{}
1062		val, o, err = ReadIntfBytes(o)
1063		if err != nil {
1064			return
1065		}
1066		v[string(key)] = val
1067	}
1068	return
1069}
1070
1071// ReadIntfBytes attempts to read
1072// the next object out of 'b' as a raw interface{} and
1073// return the remaining bytes.
1074func ReadIntfBytes(b []byte) (i interface{}, o []byte, err error) {
1075	if len(b) < 1 {
1076		err = ErrShortBytes
1077		return
1078	}
1079
1080	k := NextType(b)
1081
1082	switch k {
1083	case MapType:
1084		i, o, err = ReadMapStrIntfBytes(b, nil)
1085		return
1086
1087	case ArrayType:
1088		var sz uint32
1089		sz, o, err = ReadArrayHeaderBytes(b)
1090		if err != nil {
1091			return
1092		}
1093		j := make([]interface{}, int(sz))
1094		i = j
1095		for d := range j {
1096			j[d], o, err = ReadIntfBytes(o)
1097			if err != nil {
1098				return
1099			}
1100		}
1101		return
1102
1103	case Float32Type:
1104		i, o, err = ReadFloat32Bytes(b)
1105		return
1106
1107	case Float64Type:
1108		i, o, err = ReadFloat64Bytes(b)
1109		return
1110
1111	case IntType:
1112		i, o, err = ReadInt64Bytes(b)
1113		return
1114
1115	case UintType:
1116		i, o, err = ReadUint64Bytes(b)
1117		return
1118
1119	case BoolType:
1120		i, o, err = ReadBoolBytes(b)
1121		return
1122
1123	case TimeType:
1124		i, o, err = ReadTimeBytes(b)
1125		return
1126
1127	case Complex64Type:
1128		i, o, err = ReadComplex64Bytes(b)
1129		return
1130
1131	case Complex128Type:
1132		i, o, err = ReadComplex128Bytes(b)
1133		return
1134
1135	case ExtensionType:
1136		var t int8
1137		t, err = peekExtension(b)
1138		if err != nil {
1139			return
1140		}
1141		// use a user-defined extension,
1142		// if it's been registered
1143		f, ok := extensionReg[t]
1144		if ok {
1145			e := f()
1146			o, err = ReadExtensionBytes(b, e)
1147			i = e
1148			return
1149		}
1150		// last resort is a raw extension
1151		e := RawExtension{}
1152		e.Type = int8(t)
1153		o, err = ReadExtensionBytes(b, &e)
1154		i = &e
1155		return
1156
1157	case NilType:
1158		o, err = ReadNilBytes(b)
1159		return
1160
1161	case BinType:
1162		i, o, err = ReadBytesBytes(b, nil)
1163		return
1164
1165	case StrType:
1166		i, o, err = ReadStringBytes(b)
1167		return
1168
1169	default:
1170		err = InvalidPrefixError(b[0])
1171		return
1172	}
1173}
1174
1175// Skip skips the next object in 'b' and
1176// returns the remaining bytes. If the object
1177// is a map or array, all of its elements
1178// will be skipped.
1179// Possible Errors:
1180// - ErrShortBytes (not enough bytes in b)
1181// - InvalidPrefixError (bad encoding)
1182func Skip(b []byte) ([]byte, error) {
1183	sz, asz, err := getSize(b)
1184	if err != nil {
1185		return b, err
1186	}
1187	if uintptr(len(b)) < sz {
1188		return b, ErrShortBytes
1189	}
1190	b = b[sz:]
1191	for asz > 0 {
1192		b, err = Skip(b)
1193		if err != nil {
1194			return b, err
1195		}
1196		asz--
1197	}
1198	return b, nil
1199}
1200
1201// returns (skip N bytes, skip M objects, error)
1202func getSize(b []byte) (uintptr, uintptr, error) {
1203	l := len(b)
1204	if l == 0 {
1205		return 0, 0, ErrShortBytes
1206	}
1207	lead := b[0]
1208	spec := &sizes[lead] // get type information
1209	size, mode := spec.size, spec.extra
1210	if size == 0 {
1211		return 0, 0, InvalidPrefixError(lead)
1212	}
1213	if mode >= 0 { // fixed composites
1214		return uintptr(size), uintptr(mode), nil
1215	}
1216	if l < int(size) {
1217		return 0, 0, ErrShortBytes
1218	}
1219	switch mode {
1220	case extra8:
1221		return uintptr(size) + uintptr(b[1]), 0, nil
1222	case extra16:
1223		return uintptr(size) + uintptr(big.Uint16(b[1:])), 0, nil
1224	case extra32:
1225		return uintptr(size) + uintptr(big.Uint32(b[1:])), 0, nil
1226	case map16v:
1227		return uintptr(size), 2 * uintptr(big.Uint16(b[1:])), nil
1228	case map32v:
1229		return uintptr(size), 2 * uintptr(big.Uint32(b[1:])), nil
1230	case array16v:
1231		return uintptr(size), uintptr(big.Uint16(b[1:])), nil
1232	case array32v:
1233		return uintptr(size), uintptr(big.Uint32(b[1:])), nil
1234	default:
1235		return 0, 0, fatal
1236	}
1237}
1238