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, b, 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, b, 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// ReadNilBytes tries to read a "nil" byte 257// off of 'b' and return the remaining bytes. 258// Possible errors: 259// - ErrShortBytes (too few bytes) 260// - TypeError{} (not a 'nil') 261// - InvalidPrefixError 262func ReadNilBytes(b []byte) ([]byte, error) { 263 if len(b) < 1 { 264 return nil, ErrShortBytes 265 } 266 if b[0] != mnil { 267 return b, badPrefix(NilType, b[0]) 268 } 269 return b[1:], nil 270} 271 272// ReadFloat64Bytes tries to read a float64 273// from 'b' and return the value and the remaining bytes. 274// Possible errors: 275// - ErrShortBytes (too few bytes) 276// - TypeError{} (not a float64) 277func ReadFloat64Bytes(b []byte) (f float64, o []byte, err error) { 278 if len(b) < 9 { 279 if len(b) >= 5 && b[0] == mfloat32 { 280 var tf float32 281 tf, o, err = ReadFloat32Bytes(b) 282 f = float64(tf) 283 return 284 } 285 err = ErrShortBytes 286 return 287 } 288 289 if b[0] != mfloat64 { 290 if b[0] == mfloat32 { 291 var tf float32 292 tf, o, err = ReadFloat32Bytes(b) 293 f = float64(tf) 294 return 295 } 296 err = badPrefix(Float64Type, b[0]) 297 return 298 } 299 300 f = math.Float64frombits(getMuint64(b)) 301 o = b[9:] 302 return 303} 304 305// ReadFloat32Bytes tries to read a float64 306// from 'b' and return the value and the remaining bytes. 307// Possible errors: 308// - ErrShortBytes (too few bytes) 309// - TypeError{} (not a float32) 310func ReadFloat32Bytes(b []byte) (f float32, o []byte, err error) { 311 if len(b) < 5 { 312 err = ErrShortBytes 313 return 314 } 315 316 if b[0] != mfloat32 { 317 err = TypeError{Method: Float32Type, Encoded: getType(b[0])} 318 return 319 } 320 321 f = math.Float32frombits(getMuint32(b)) 322 o = b[5:] 323 return 324} 325 326// ReadBoolBytes tries to read a float64 327// from 'b' and return the value and the remaining bytes. 328// Possible errors: 329// - ErrShortBytes (too few bytes) 330// - TypeError{} (not a bool) 331func ReadBoolBytes(b []byte) (bool, []byte, error) { 332 if len(b) < 1 { 333 return false, b, ErrShortBytes 334 } 335 switch b[0] { 336 case mtrue: 337 return true, b[1:], nil 338 case mfalse: 339 return false, b[1:], nil 340 default: 341 return false, b, badPrefix(BoolType, b[0]) 342 } 343} 344 345// ReadInt64Bytes tries to read an int64 346// from 'b' and return the value and the remaining bytes. 347// Possible errors: 348// - ErrShortBytes (too few bytes) 349// - TypeError (not a int) 350func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) { 351 l := len(b) 352 if l < 1 { 353 return 0, nil, ErrShortBytes 354 } 355 356 lead := b[0] 357 if isfixint(lead) { 358 i = int64(rfixint(lead)) 359 o = b[1:] 360 return 361 } 362 if isnfixint(lead) { 363 i = int64(rnfixint(lead)) 364 o = b[1:] 365 return 366 } 367 368 switch lead { 369 case mint8: 370 if l < 2 { 371 err = ErrShortBytes 372 return 373 } 374 i = int64(getMint8(b)) 375 o = b[2:] 376 return 377 378 case muint8: 379 if l < 2 { 380 err = ErrShortBytes 381 return 382 } 383 i = int64(getMuint8(b)) 384 o = b[2:] 385 return 386 387 case mint16: 388 if l < 3 { 389 err = ErrShortBytes 390 return 391 } 392 i = int64(getMint16(b)) 393 o = b[3:] 394 return 395 396 case muint16: 397 if l < 3 { 398 err = ErrShortBytes 399 return 400 } 401 i = int64(getMuint16(b)) 402 o = b[3:] 403 return 404 405 case mint32: 406 if l < 5 { 407 err = ErrShortBytes 408 return 409 } 410 i = int64(getMint32(b)) 411 o = b[5:] 412 return 413 414 case muint32: 415 if l < 5 { 416 err = ErrShortBytes 417 return 418 } 419 i = int64(getMuint32(b)) 420 o = b[5:] 421 return 422 423 case mint64: 424 if l < 9 { 425 err = ErrShortBytes 426 return 427 } 428 i = int64(getMint64(b)) 429 o = b[9:] 430 return 431 432 case muint64: 433 if l < 9 { 434 err = ErrShortBytes 435 return 436 } 437 u := getMuint64(b) 438 if u > math.MaxInt64 { 439 err = UintOverflow{Value: u, FailedBitsize: 64} 440 return 441 } 442 i = int64(u) 443 o = b[9:] 444 return 445 446 default: 447 err = badPrefix(IntType, lead) 448 return 449 } 450} 451 452// ReadInt32Bytes tries to read an int32 453// from 'b' and return the value and the remaining bytes. 454// Possible errors: 455// - ErrShortBytes (too few bytes) 456// - TypeError{} (not a int) 457// - IntOverflow{} (value doesn't fit in int32) 458func ReadInt32Bytes(b []byte) (int32, []byte, error) { 459 i, o, err := ReadInt64Bytes(b) 460 if i > math.MaxInt32 || i < math.MinInt32 { 461 return 0, o, IntOverflow{Value: i, FailedBitsize: 32} 462 } 463 return int32(i), o, err 464} 465 466// ReadInt16Bytes tries to read an int16 467// from 'b' and return the value and the remaining bytes. 468// Possible errors: 469// - ErrShortBytes (too few bytes) 470// - TypeError{} (not a int) 471// - IntOverflow{} (value doesn't fit in int16) 472func ReadInt16Bytes(b []byte) (int16, []byte, error) { 473 i, o, err := ReadInt64Bytes(b) 474 if i > math.MaxInt16 || i < math.MinInt16 { 475 return 0, o, IntOverflow{Value: i, FailedBitsize: 16} 476 } 477 return int16(i), o, err 478} 479 480// ReadInt8Bytes tries to read an int16 481// from 'b' and return the value and the remaining bytes. 482// Possible errors: 483// - ErrShortBytes (too few bytes) 484// - TypeError{} (not a int) 485// - IntOverflow{} (value doesn't fit in int8) 486func ReadInt8Bytes(b []byte) (int8, []byte, error) { 487 i, o, err := ReadInt64Bytes(b) 488 if i > math.MaxInt8 || i < math.MinInt8 { 489 return 0, o, IntOverflow{Value: i, FailedBitsize: 8} 490 } 491 return int8(i), o, err 492} 493 494// ReadIntBytes tries to read an int 495// from 'b' and return the value and the remaining bytes. 496// Possible errors: 497// - ErrShortBytes (too few bytes) 498// - TypeError{} (not a int) 499// - IntOverflow{} (value doesn't fit in int; 32-bit platforms only) 500func ReadIntBytes(b []byte) (int, []byte, error) { 501 if smallint { 502 i, b, err := ReadInt32Bytes(b) 503 return int(i), b, err 504 } 505 i, b, err := ReadInt64Bytes(b) 506 return int(i), b, err 507} 508 509// ReadUint64Bytes tries to read a uint64 510// from 'b' and return the value and the remaining bytes. 511// Possible errors: 512// - ErrShortBytes (too few bytes) 513// - TypeError{} (not a uint) 514func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) { 515 l := len(b) 516 if l < 1 { 517 return 0, nil, ErrShortBytes 518 } 519 520 lead := b[0] 521 if isfixint(lead) { 522 u = uint64(rfixint(lead)) 523 o = b[1:] 524 return 525 } 526 527 switch lead { 528 case mint8: 529 if l < 2 { 530 err = ErrShortBytes 531 return 532 } 533 v := int64(getMint8(b)) 534 if v < 0 { 535 err = UintBelowZero{Value: v} 536 return 537 } 538 u = uint64(v) 539 o = b[2:] 540 return 541 542 case muint8: 543 if l < 2 { 544 err = ErrShortBytes 545 return 546 } 547 u = uint64(getMuint8(b)) 548 o = b[2:] 549 return 550 551 case mint16: 552 if l < 3 { 553 err = ErrShortBytes 554 return 555 } 556 v := int64(getMint16(b)) 557 if v < 0 { 558 err = UintBelowZero{Value: v} 559 return 560 } 561 u = uint64(v) 562 o = b[3:] 563 return 564 565 case muint16: 566 if l < 3 { 567 err = ErrShortBytes 568 return 569 } 570 u = uint64(getMuint16(b)) 571 o = b[3:] 572 return 573 574 case mint32: 575 if l < 5 { 576 err = ErrShortBytes 577 return 578 } 579 v := int64(getMint32(b)) 580 if v < 0 { 581 err = UintBelowZero{Value: v} 582 return 583 } 584 u = uint64(v) 585 o = b[5:] 586 return 587 588 case muint32: 589 if l < 5 { 590 err = ErrShortBytes 591 return 592 } 593 u = uint64(getMuint32(b)) 594 o = b[5:] 595 return 596 597 case mint64: 598 if l < 9 { 599 err = ErrShortBytes 600 return 601 } 602 v := int64(getMint64(b)) 603 if v < 0 { 604 err = UintBelowZero{Value: v} 605 return 606 } 607 u = uint64(v) 608 o = b[9:] 609 return 610 611 case muint64: 612 if l < 9 { 613 err = ErrShortBytes 614 return 615 } 616 u = getMuint64(b) 617 o = b[9:] 618 return 619 620 default: 621 if isnfixint(lead) { 622 err = UintBelowZero{Value: int64(rnfixint(lead))} 623 } else { 624 err = badPrefix(UintType, lead) 625 } 626 return 627 } 628} 629 630// ReadUint32Bytes tries to read a uint32 631// from 'b' and return the value and the remaining bytes. 632// Possible errors: 633// - ErrShortBytes (too few bytes) 634// - TypeError{} (not a uint) 635// - UintOverflow{} (value too large for uint32) 636func ReadUint32Bytes(b []byte) (uint32, []byte, error) { 637 v, o, err := ReadUint64Bytes(b) 638 if v > math.MaxUint32 { 639 return 0, nil, UintOverflow{Value: v, FailedBitsize: 32} 640 } 641 return uint32(v), o, err 642} 643 644// ReadUint16Bytes tries to read a uint16 645// from 'b' and return the value and the remaining bytes. 646// Possible errors: 647// - ErrShortBytes (too few bytes) 648// - TypeError{} (not a uint) 649// - UintOverflow{} (value too large for uint16) 650func ReadUint16Bytes(b []byte) (uint16, []byte, error) { 651 v, o, err := ReadUint64Bytes(b) 652 if v > math.MaxUint16 { 653 return 0, nil, UintOverflow{Value: v, FailedBitsize: 16} 654 } 655 return uint16(v), o, err 656} 657 658// ReadUint8Bytes tries to read a uint8 659// from 'b' and return the value and the remaining bytes. 660// Possible errors: 661// - ErrShortBytes (too few bytes) 662// - TypeError{} (not a uint) 663// - UintOverflow{} (value too large for uint8) 664func ReadUint8Bytes(b []byte) (uint8, []byte, error) { 665 v, o, err := ReadUint64Bytes(b) 666 if v > math.MaxUint8 { 667 return 0, nil, UintOverflow{Value: v, FailedBitsize: 8} 668 } 669 return uint8(v), o, err 670} 671 672// ReadUintBytes tries to read a uint 673// from 'b' and return the value and the remaining bytes. 674// Possible errors: 675// - ErrShortBytes (too few bytes) 676// - TypeError{} (not a uint) 677// - UintOverflow{} (value too large for uint; 32-bit platforms only) 678func ReadUintBytes(b []byte) (uint, []byte, error) { 679 if smallint { 680 u, b, err := ReadUint32Bytes(b) 681 return uint(u), b, err 682 } 683 u, b, err := ReadUint64Bytes(b) 684 return uint(u), b, err 685} 686 687// ReadByteBytes is analogous to ReadUint8Bytes 688func ReadByteBytes(b []byte) (byte, []byte, error) { 689 return ReadUint8Bytes(b) 690} 691 692// ReadBytesBytes reads a 'bin' object 693// from 'b' and returns its vaue and 694// the remaining bytes in 'b'. 695// Possible errors: 696// - ErrShortBytes (too few bytes) 697// - TypeError{} (not a 'bin' object) 698func ReadBytesBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) { 699 return readBytesBytes(b, scratch, false) 700} 701 702func readBytesBytes(b []byte, scratch []byte, zc bool) (v []byte, o []byte, err error) { 703 l := len(b) 704 if l < 1 { 705 return nil, nil, ErrShortBytes 706 } 707 708 lead := b[0] 709 var read int 710 switch lead { 711 case mbin8: 712 if l < 2 { 713 err = ErrShortBytes 714 return 715 } 716 717 read = int(b[1]) 718 b = b[2:] 719 720 case mbin16: 721 if l < 3 { 722 err = ErrShortBytes 723 return 724 } 725 read = int(big.Uint16(b[1:])) 726 b = b[3:] 727 728 case mbin32: 729 if l < 5 { 730 err = ErrShortBytes 731 return 732 } 733 read = int(big.Uint32(b[1:])) 734 b = b[5:] 735 736 default: 737 err = badPrefix(BinType, lead) 738 return 739 } 740 741 if len(b) < read { 742 err = ErrShortBytes 743 return 744 } 745 746 // zero-copy 747 if zc { 748 v = b[0:read] 749 o = b[read:] 750 return 751 } 752 753 if cap(scratch) >= read { 754 v = scratch[0:read] 755 } else { 756 v = make([]byte, read) 757 } 758 759 o = b[copy(v, b):] 760 return 761} 762 763// ReadBytesZC extracts the messagepack-encoded 764// binary field without copying. The returned []byte 765// points to the same memory as the input slice. 766// Possible errors: 767// - ErrShortBytes (b not long enough) 768// - TypeError{} (object not 'bin') 769func ReadBytesZC(b []byte) (v []byte, o []byte, err error) { 770 return readBytesBytes(b, nil, true) 771} 772 773func ReadExactBytes(b []byte, into []byte) (o []byte, err error) { 774 l := len(b) 775 if l < 1 { 776 err = ErrShortBytes 777 return 778 } 779 780 lead := b[0] 781 var read uint32 782 var skip int 783 switch lead { 784 case mbin8: 785 if l < 2 { 786 err = ErrShortBytes 787 return 788 } 789 790 read = uint32(b[1]) 791 skip = 2 792 793 case mbin16: 794 if l < 3 { 795 err = ErrShortBytes 796 return 797 } 798 read = uint32(big.Uint16(b[1:])) 799 skip = 3 800 801 case mbin32: 802 if l < 5 { 803 err = ErrShortBytes 804 return 805 } 806 read = uint32(big.Uint32(b[1:])) 807 skip = 5 808 809 default: 810 err = badPrefix(BinType, lead) 811 return 812 } 813 814 if read != uint32(len(into)) { 815 err = ArrayError{Wanted: uint32(len(into)), Got: read} 816 return 817 } 818 819 o = b[skip+copy(into, b[skip:]):] 820 return 821} 822 823// ReadStringZC reads a messagepack string field 824// without copying. The returned []byte points 825// to the same memory as the input slice. 826// Possible errors: 827// - ErrShortBytes (b not long enough) 828// - TypeError{} (object not 'str') 829func ReadStringZC(b []byte) (v []byte, o []byte, err error) { 830 l := len(b) 831 if l < 1 { 832 return nil, nil, ErrShortBytes 833 } 834 835 lead := b[0] 836 var read int 837 838 if isfixstr(lead) { 839 read = int(rfixstr(lead)) 840 b = b[1:] 841 } else { 842 switch lead { 843 case mstr8: 844 if l < 2 { 845 err = ErrShortBytes 846 return 847 } 848 read = int(b[1]) 849 b = b[2:] 850 851 case mstr16: 852 if l < 3 { 853 err = ErrShortBytes 854 return 855 } 856 read = int(big.Uint16(b[1:])) 857 b = b[3:] 858 859 case mstr32: 860 if l < 5 { 861 err = ErrShortBytes 862 return 863 } 864 read = int(big.Uint32(b[1:])) 865 b = b[5:] 866 867 default: 868 err = TypeError{Method: StrType, Encoded: getType(lead)} 869 return 870 } 871 } 872 873 if len(b) < read { 874 err = ErrShortBytes 875 return 876 } 877 878 v = b[0:read] 879 o = b[read:] 880 return 881} 882 883// ReadStringBytes reads a 'str' object 884// from 'b' and returns its value and the 885// remaining bytes in 'b'. 886// Possible errors: 887// - ErrShortBytes (b not long enough) 888// - TypeError{} (not 'str' type) 889// - InvalidPrefixError 890func ReadStringBytes(b []byte) (string, []byte, error) { 891 v, o, err := ReadStringZC(b) 892 return string(v), o, err 893} 894 895// ReadStringAsBytes reads a 'str' object 896// into a slice of bytes. 'v' is the value of 897// the 'str' object, which may reside in memory 898// pointed to by 'scratch.' 'o' is the remaining bytes 899// in 'b.'' 900// Possible errors: 901// - ErrShortBytes (b not long enough) 902// - TypeError{} (not 'str' type) 903// - InvalidPrefixError (unknown type marker) 904func ReadStringAsBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) { 905 var tmp []byte 906 tmp, o, err = ReadStringZC(b) 907 v = append(scratch[:0], tmp...) 908 return 909} 910 911// ReadComplex128Bytes reads a complex128 912// extension object from 'b' and returns the 913// remaining bytes. 914// Possible errors: 915// - ErrShortBytes (not enough bytes in 'b') 916// - TypeError{} (object not a complex128) 917// - InvalidPrefixError 918// - ExtensionTypeError{} (object an extension of the correct size, but not a complex128) 919func ReadComplex128Bytes(b []byte) (c complex128, o []byte, err error) { 920 if len(b) < 18 { 921 err = ErrShortBytes 922 return 923 } 924 if b[0] != mfixext16 { 925 err = badPrefix(Complex128Type, b[0]) 926 return 927 } 928 if int8(b[1]) != Complex128Extension { 929 err = errExt(int8(b[1]), Complex128Extension) 930 return 931 } 932 c = complex(math.Float64frombits(big.Uint64(b[2:])), 933 math.Float64frombits(big.Uint64(b[10:]))) 934 o = b[18:] 935 return 936} 937 938// ReadComplex64Bytes reads a complex64 939// extension object from 'b' and returns the 940// remaining bytes. 941// Possible errors: 942// - ErrShortBytes (not enough bytes in 'b') 943// - TypeError{} (object not a complex64) 944// - ExtensionTypeError{} (object an extension of the correct size, but not a complex64) 945func ReadComplex64Bytes(b []byte) (c complex64, o []byte, err error) { 946 if len(b) < 10 { 947 err = ErrShortBytes 948 return 949 } 950 if b[0] != mfixext8 { 951 err = badPrefix(Complex64Type, b[0]) 952 return 953 } 954 if b[1] != Complex64Extension { 955 err = errExt(int8(b[1]), Complex64Extension) 956 return 957 } 958 c = complex(math.Float32frombits(big.Uint32(b[2:])), 959 math.Float32frombits(big.Uint32(b[6:]))) 960 o = b[10:] 961 return 962} 963 964// ReadTimeBytes reads a time.Time 965// extension object from 'b' and returns the 966// remaining bytes. 967// Possible errors: 968// - ErrShortBytes (not enough bytes in 'b') 969// - TypeError{} (object not a complex64) 970// - ExtensionTypeError{} (object an extension of the correct size, but not a time.Time) 971func ReadTimeBytes(b []byte) (t time.Time, o []byte, err error) { 972 if len(b) < 15 { 973 err = ErrShortBytes 974 return 975 } 976 if b[0] != mext8 || b[1] != 12 { 977 err = badPrefix(TimeType, b[0]) 978 return 979 } 980 if int8(b[2]) != TimeExtension { 981 err = errExt(int8(b[2]), TimeExtension) 982 return 983 } 984 sec, nsec := getUnix(b[3:]) 985 t = time.Unix(sec, int64(nsec)).Local() 986 o = b[15:] 987 return 988} 989 990// ReadMapStrIntfBytes reads a map[string]interface{} 991// out of 'b' and returns the map and remaining bytes. 992// If 'old' is non-nil, the values will be read into that map. 993func ReadMapStrIntfBytes(b []byte, old map[string]interface{}) (v map[string]interface{}, o []byte, err error) { 994 var sz uint32 995 o = b 996 sz, o, err = ReadMapHeaderBytes(o) 997 998 if err != nil { 999 return 1000 } 1001 1002 if old != nil { 1003 for key := range old { 1004 delete(old, key) 1005 } 1006 v = old 1007 } else { 1008 v = make(map[string]interface{}, int(sz)) 1009 } 1010 1011 for z := uint32(0); z < sz; z++ { 1012 if len(o) < 1 { 1013 err = ErrShortBytes 1014 return 1015 } 1016 var key []byte 1017 key, o, err = ReadMapKeyZC(o) 1018 if err != nil { 1019 return 1020 } 1021 var val interface{} 1022 val, o, err = ReadIntfBytes(o) 1023 if err != nil { 1024 return 1025 } 1026 v[string(key)] = val 1027 } 1028 return 1029} 1030 1031// ReadIntfBytes attempts to read 1032// the next object out of 'b' as a raw interface{} and 1033// return the remaining bytes. 1034func ReadIntfBytes(b []byte) (i interface{}, o []byte, err error) { 1035 if len(b) < 1 { 1036 err = ErrShortBytes 1037 return 1038 } 1039 1040 k := NextType(b) 1041 1042 switch k { 1043 case MapType: 1044 i, o, err = ReadMapStrIntfBytes(b, nil) 1045 return 1046 1047 case ArrayType: 1048 var sz uint32 1049 sz, o, err = ReadArrayHeaderBytes(b) 1050 if err != nil { 1051 return 1052 } 1053 j := make([]interface{}, int(sz)) 1054 i = j 1055 for d := range j { 1056 j[d], o, err = ReadIntfBytes(o) 1057 if err != nil { 1058 return 1059 } 1060 } 1061 return 1062 1063 case Float32Type: 1064 i, o, err = ReadFloat32Bytes(b) 1065 return 1066 1067 case Float64Type: 1068 i, o, err = ReadFloat64Bytes(b) 1069 return 1070 1071 case IntType: 1072 i, o, err = ReadInt64Bytes(b) 1073 return 1074 1075 case UintType: 1076 i, o, err = ReadUint64Bytes(b) 1077 return 1078 1079 case BoolType: 1080 i, o, err = ReadBoolBytes(b) 1081 return 1082 1083 case TimeType: 1084 i, o, err = ReadTimeBytes(b) 1085 return 1086 1087 case Complex64Type: 1088 i, o, err = ReadComplex64Bytes(b) 1089 return 1090 1091 case Complex128Type: 1092 i, o, err = ReadComplex128Bytes(b) 1093 return 1094 1095 case ExtensionType: 1096 var t int8 1097 t, err = peekExtension(b) 1098 if err != nil { 1099 return 1100 } 1101 // use a user-defined extension, 1102 // if it's been registered 1103 f, ok := extensionReg[t] 1104 if ok { 1105 e := f() 1106 o, err = ReadExtensionBytes(b, e) 1107 i = e 1108 return 1109 } 1110 // last resort is a raw extension 1111 e := RawExtension{} 1112 e.Type = int8(t) 1113 o, err = ReadExtensionBytes(b, &e) 1114 i = &e 1115 return 1116 1117 case NilType: 1118 o, err = ReadNilBytes(b) 1119 return 1120 1121 case BinType: 1122 i, o, err = ReadBytesBytes(b, nil) 1123 return 1124 1125 case StrType: 1126 i, o, err = ReadStringBytes(b) 1127 return 1128 1129 default: 1130 err = InvalidPrefixError(b[0]) 1131 return 1132 } 1133} 1134 1135// Skip skips the next object in 'b' and 1136// returns the remaining bytes. If the object 1137// is a map or array, all of its elements 1138// will be skipped. 1139// Possible Errors: 1140// - ErrShortBytes (not enough bytes in b) 1141// - InvalidPrefixError (bad encoding) 1142func Skip(b []byte) ([]byte, error) { 1143 sz, asz, err := getSize(b) 1144 if err != nil { 1145 return b, err 1146 } 1147 if uintptr(len(b)) < sz { 1148 return b, ErrShortBytes 1149 } 1150 b = b[sz:] 1151 for asz > 0 { 1152 b, err = Skip(b) 1153 if err != nil { 1154 return b, err 1155 } 1156 asz-- 1157 } 1158 return b, nil 1159} 1160 1161// returns (skip N bytes, skip M objects, error) 1162func getSize(b []byte) (uintptr, uintptr, error) { 1163 l := len(b) 1164 if l == 0 { 1165 return 0, 0, ErrShortBytes 1166 } 1167 lead := b[0] 1168 spec := &sizes[lead] // get type information 1169 size, mode := spec.size, spec.extra 1170 if size == 0 { 1171 return 0, 0, InvalidPrefixError(lead) 1172 } 1173 if mode >= 0 { // fixed composites 1174 return uintptr(size), uintptr(mode), nil 1175 } 1176 if l < int(size) { 1177 return 0, 0, ErrShortBytes 1178 } 1179 switch mode { 1180 case extra8: 1181 return uintptr(size) + uintptr(b[1]), 0, nil 1182 case extra16: 1183 return uintptr(size) + uintptr(big.Uint16(b[1:])), 0, nil 1184 case extra32: 1185 return uintptr(size) + uintptr(big.Uint32(b[1:])), 0, nil 1186 case map16v: 1187 return uintptr(size), 2 * uintptr(big.Uint16(b[1:])), nil 1188 case map32v: 1189 return uintptr(size), 2 * uintptr(big.Uint32(b[1:])), nil 1190 case array16v: 1191 return uintptr(size), uintptr(big.Uint16(b[1:])), nil 1192 case array32v: 1193 return uintptr(size), uintptr(big.Uint32(b[1:])), nil 1194 default: 1195 return 0, 0, fatal 1196 } 1197} 1198