1// Go support for Protocol Buffers - Google's data interchange format 2// 3// Copyright 2010 The Go Authors. All rights reserved. 4// https://github.com/golang/protobuf 5// 6// Redistribution and use in source and binary forms, with or without 7// modification, are permitted provided that the following conditions are 8// met: 9// 10// * Redistributions of source code must retain the above copyright 11// notice, this list of conditions and the following disclaimer. 12// * Redistributions in binary form must reproduce the above 13// copyright notice, this list of conditions and the following disclaimer 14// in the documentation and/or other materials provided with the 15// distribution. 16// * Neither the name of Google Inc. nor the names of its 17// contributors may be used to endorse or promote products derived from 18// this software without specific prior written permission. 19// 20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32package proto 33 34/* 35 * Routines for decoding protocol buffer data to construct in-memory representations. 36 */ 37 38import ( 39 "errors" 40 "fmt" 41 "io" 42 "os" 43 "reflect" 44) 45 46// errOverflow is returned when an integer is too large to be represented. 47var errOverflow = errors.New("proto: integer overflow") 48 49// ErrInternalBadWireType is returned by generated code when an incorrect 50// wire type is encountered. It does not get returned to user code. 51var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof") 52 53// The fundamental decoders that interpret bytes on the wire. 54// Those that take integer types all return uint64 and are 55// therefore of type valueDecoder. 56 57// DecodeVarint reads a varint-encoded integer from the slice. 58// It returns the integer and the number of bytes consumed, or 59// zero if there is not enough. 60// This is the format for the 61// int32, int64, uint32, uint64, bool, and enum 62// protocol buffer types. 63func DecodeVarint(buf []byte) (x uint64, n int) { 64 for shift := uint(0); shift < 64; shift += 7 { 65 if n >= len(buf) { 66 return 0, 0 67 } 68 b := uint64(buf[n]) 69 n++ 70 x |= (b & 0x7F) << shift 71 if (b & 0x80) == 0 { 72 return x, n 73 } 74 } 75 76 // The number is too large to represent in a 64-bit value. 77 return 0, 0 78} 79 80func (p *Buffer) decodeVarintSlow() (x uint64, err error) { 81 i := p.index 82 l := len(p.buf) 83 84 for shift := uint(0); shift < 64; shift += 7 { 85 if i >= l { 86 err = io.ErrUnexpectedEOF 87 return 88 } 89 b := p.buf[i] 90 i++ 91 x |= (uint64(b) & 0x7F) << shift 92 if b < 0x80 { 93 p.index = i 94 return 95 } 96 } 97 98 // The number is too large to represent in a 64-bit value. 99 err = errOverflow 100 return 101} 102 103// DecodeVarint reads a varint-encoded integer from the Buffer. 104// This is the format for the 105// int32, int64, uint32, uint64, bool, and enum 106// protocol buffer types. 107func (p *Buffer) DecodeVarint() (x uint64, err error) { 108 i := p.index 109 buf := p.buf 110 111 if i >= len(buf) { 112 return 0, io.ErrUnexpectedEOF 113 } else if buf[i] < 0x80 { 114 p.index++ 115 return uint64(buf[i]), nil 116 } else if len(buf)-i < 10 { 117 return p.decodeVarintSlow() 118 } 119 120 var b uint64 121 // we already checked the first byte 122 x = uint64(buf[i]) - 0x80 123 i++ 124 125 b = uint64(buf[i]) 126 i++ 127 x += b << 7 128 if b&0x80 == 0 { 129 goto done 130 } 131 x -= 0x80 << 7 132 133 b = uint64(buf[i]) 134 i++ 135 x += b << 14 136 if b&0x80 == 0 { 137 goto done 138 } 139 x -= 0x80 << 14 140 141 b = uint64(buf[i]) 142 i++ 143 x += b << 21 144 if b&0x80 == 0 { 145 goto done 146 } 147 x -= 0x80 << 21 148 149 b = uint64(buf[i]) 150 i++ 151 x += b << 28 152 if b&0x80 == 0 { 153 goto done 154 } 155 x -= 0x80 << 28 156 157 b = uint64(buf[i]) 158 i++ 159 x += b << 35 160 if b&0x80 == 0 { 161 goto done 162 } 163 x -= 0x80 << 35 164 165 b = uint64(buf[i]) 166 i++ 167 x += b << 42 168 if b&0x80 == 0 { 169 goto done 170 } 171 x -= 0x80 << 42 172 173 b = uint64(buf[i]) 174 i++ 175 x += b << 49 176 if b&0x80 == 0 { 177 goto done 178 } 179 x -= 0x80 << 49 180 181 b = uint64(buf[i]) 182 i++ 183 x += b << 56 184 if b&0x80 == 0 { 185 goto done 186 } 187 x -= 0x80 << 56 188 189 b = uint64(buf[i]) 190 i++ 191 x += b << 63 192 if b&0x80 == 0 { 193 goto done 194 } 195 // x -= 0x80 << 63 // Always zero. 196 197 return 0, errOverflow 198 199done: 200 p.index = i 201 return x, nil 202} 203 204// DecodeFixed64 reads a 64-bit integer from the Buffer. 205// This is the format for the 206// fixed64, sfixed64, and double protocol buffer types. 207func (p *Buffer) DecodeFixed64() (x uint64, err error) { 208 // x, err already 0 209 i := p.index + 8 210 if i < 0 || i > len(p.buf) { 211 err = io.ErrUnexpectedEOF 212 return 213 } 214 p.index = i 215 216 x = uint64(p.buf[i-8]) 217 x |= uint64(p.buf[i-7]) << 8 218 x |= uint64(p.buf[i-6]) << 16 219 x |= uint64(p.buf[i-5]) << 24 220 x |= uint64(p.buf[i-4]) << 32 221 x |= uint64(p.buf[i-3]) << 40 222 x |= uint64(p.buf[i-2]) << 48 223 x |= uint64(p.buf[i-1]) << 56 224 return 225} 226 227// DecodeFixed32 reads a 32-bit integer from the Buffer. 228// This is the format for the 229// fixed32, sfixed32, and float protocol buffer types. 230func (p *Buffer) DecodeFixed32() (x uint64, err error) { 231 // x, err already 0 232 i := p.index + 4 233 if i < 0 || i > len(p.buf) { 234 err = io.ErrUnexpectedEOF 235 return 236 } 237 p.index = i 238 239 x = uint64(p.buf[i-4]) 240 x |= uint64(p.buf[i-3]) << 8 241 x |= uint64(p.buf[i-2]) << 16 242 x |= uint64(p.buf[i-1]) << 24 243 return 244} 245 246// DecodeZigzag64 reads a zigzag-encoded 64-bit integer 247// from the Buffer. 248// This is the format used for the sint64 protocol buffer type. 249func (p *Buffer) DecodeZigzag64() (x uint64, err error) { 250 x, err = p.DecodeVarint() 251 if err != nil { 252 return 253 } 254 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63) 255 return 256} 257 258// DecodeZigzag32 reads a zigzag-encoded 32-bit integer 259// from the Buffer. 260// This is the format used for the sint32 protocol buffer type. 261func (p *Buffer) DecodeZigzag32() (x uint64, err error) { 262 x, err = p.DecodeVarint() 263 if err != nil { 264 return 265 } 266 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31)) 267 return 268} 269 270// These are not ValueDecoders: they produce an array of bytes or a string. 271// bytes, embedded messages 272 273// DecodeRawBytes reads a count-delimited byte buffer from the Buffer. 274// This is the format used for the bytes protocol buffer 275// type and for embedded messages. 276func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) { 277 n, err := p.DecodeVarint() 278 if err != nil { 279 return nil, err 280 } 281 282 nb := int(n) 283 if nb < 0 { 284 return nil, fmt.Errorf("proto: bad byte length %d", nb) 285 } 286 end := p.index + nb 287 if end < p.index || end > len(p.buf) { 288 return nil, io.ErrUnexpectedEOF 289 } 290 291 if !alloc { 292 // todo: check if can get more uses of alloc=false 293 buf = p.buf[p.index:end] 294 p.index += nb 295 return 296 } 297 298 buf = make([]byte, nb) 299 copy(buf, p.buf[p.index:]) 300 p.index += nb 301 return 302} 303 304// DecodeStringBytes reads an encoded string from the Buffer. 305// This is the format used for the proto2 string type. 306func (p *Buffer) DecodeStringBytes() (s string, err error) { 307 buf, err := p.DecodeRawBytes(false) 308 if err != nil { 309 return 310 } 311 return string(buf), nil 312} 313 314// Skip the next item in the buffer. Its wire type is decoded and presented as an argument. 315// If the protocol buffer has extensions, and the field matches, add it as an extension. 316// Otherwise, if the XXX_unrecognized field exists, append the skipped data there. 317func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error { 318 oi := o.index 319 320 err := o.skip(t, tag, wire) 321 if err != nil { 322 return err 323 } 324 325 if !unrecField.IsValid() { 326 return nil 327 } 328 329 ptr := structPointer_Bytes(base, unrecField) 330 331 // Add the skipped field to struct field 332 obuf := o.buf 333 334 o.buf = *ptr 335 o.EncodeVarint(uint64(tag<<3 | wire)) 336 *ptr = append(o.buf, obuf[oi:o.index]...) 337 338 o.buf = obuf 339 340 return nil 341} 342 343// Skip the next item in the buffer. Its wire type is decoded and presented as an argument. 344func (o *Buffer) skip(t reflect.Type, tag, wire int) error { 345 346 var u uint64 347 var err error 348 349 switch wire { 350 case WireVarint: 351 _, err = o.DecodeVarint() 352 case WireFixed64: 353 _, err = o.DecodeFixed64() 354 case WireBytes: 355 _, err = o.DecodeRawBytes(false) 356 case WireFixed32: 357 _, err = o.DecodeFixed32() 358 case WireStartGroup: 359 for { 360 u, err = o.DecodeVarint() 361 if err != nil { 362 break 363 } 364 fwire := int(u & 0x7) 365 if fwire == WireEndGroup { 366 break 367 } 368 ftag := int(u >> 3) 369 err = o.skip(t, ftag, fwire) 370 if err != nil { 371 break 372 } 373 } 374 default: 375 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t) 376 } 377 return err 378} 379 380// Unmarshaler is the interface representing objects that can 381// unmarshal themselves. The method should reset the receiver before 382// decoding starts. The argument points to data that may be 383// overwritten, so implementations should not keep references to the 384// buffer. 385type Unmarshaler interface { 386 Unmarshal([]byte) error 387} 388 389// Unmarshal parses the protocol buffer representation in buf and places the 390// decoded result in pb. If the struct underlying pb does not match 391// the data in buf, the results can be unpredictable. 392// 393// Unmarshal resets pb before starting to unmarshal, so any 394// existing data in pb is always removed. Use UnmarshalMerge 395// to preserve and append to existing data. 396func Unmarshal(buf []byte, pb Message) error { 397 pb.Reset() 398 return UnmarshalMerge(buf, pb) 399} 400 401// UnmarshalMerge parses the protocol buffer representation in buf and 402// writes the decoded result to pb. If the struct underlying pb does not match 403// the data in buf, the results can be unpredictable. 404// 405// UnmarshalMerge merges into existing data in pb. 406// Most code should use Unmarshal instead. 407func UnmarshalMerge(buf []byte, pb Message) error { 408 // If the object can unmarshal itself, let it. 409 if u, ok := pb.(Unmarshaler); ok { 410 return u.Unmarshal(buf) 411 } 412 return NewBuffer(buf).Unmarshal(pb) 413} 414 415// DecodeMessage reads a count-delimited message from the Buffer. 416func (p *Buffer) DecodeMessage(pb Message) error { 417 enc, err := p.DecodeRawBytes(false) 418 if err != nil { 419 return err 420 } 421 return NewBuffer(enc).Unmarshal(pb) 422} 423 424// DecodeGroup reads a tag-delimited group from the Buffer. 425func (p *Buffer) DecodeGroup(pb Message) error { 426 typ, base, err := getbase(pb) 427 if err != nil { 428 return err 429 } 430 return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), true, base) 431} 432 433// Unmarshal parses the protocol buffer representation in the 434// Buffer and places the decoded result in pb. If the struct 435// underlying pb does not match the data in the buffer, the results can be 436// unpredictable. 437// 438// Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal. 439func (p *Buffer) Unmarshal(pb Message) error { 440 // If the object can unmarshal itself, let it. 441 if u, ok := pb.(Unmarshaler); ok { 442 err := u.Unmarshal(p.buf[p.index:]) 443 p.index = len(p.buf) 444 return err 445 } 446 447 typ, base, err := getbase(pb) 448 if err != nil { 449 return err 450 } 451 452 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base) 453 454 if collectStats { 455 stats.Decode++ 456 } 457 458 return err 459} 460 461// unmarshalType does the work of unmarshaling a structure. 462func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error { 463 var state errorState 464 required, reqFields := prop.reqCount, uint64(0) 465 466 var err error 467 for err == nil && o.index < len(o.buf) { 468 oi := o.index 469 var u uint64 470 u, err = o.DecodeVarint() 471 if err != nil { 472 break 473 } 474 wire := int(u & 0x7) 475 if wire == WireEndGroup { 476 if is_group { 477 if required > 0 { 478 // Not enough information to determine the exact field. 479 // (See below.) 480 return &RequiredNotSetError{"{Unknown}"} 481 } 482 return nil // input is satisfied 483 } 484 return fmt.Errorf("proto: %s: wiretype end group for non-group", st) 485 } 486 tag := int(u >> 3) 487 if tag <= 0 { 488 return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire) 489 } 490 fieldnum, ok := prop.decoderTags.get(tag) 491 if !ok { 492 // Maybe it's an extension? 493 if prop.extendable { 494 if e, _ := extendable(structPointer_Interface(base, st)); isExtensionField(e, int32(tag)) { 495 if err = o.skip(st, tag, wire); err == nil { 496 extmap := e.extensionsWrite() 497 ext := extmap[int32(tag)] // may be missing 498 ext.enc = append(ext.enc, o.buf[oi:o.index]...) 499 extmap[int32(tag)] = ext 500 } 501 continue 502 } 503 } 504 // Maybe it's a oneof? 505 if prop.oneofUnmarshaler != nil { 506 m := structPointer_Interface(base, st).(Message) 507 // First return value indicates whether tag is a oneof field. 508 ok, err = prop.oneofUnmarshaler(m, tag, wire, o) 509 if err == ErrInternalBadWireType { 510 // Map the error to something more descriptive. 511 // Do the formatting here to save generated code space. 512 err = fmt.Errorf("bad wiretype for oneof field in %T", m) 513 } 514 if ok { 515 continue 516 } 517 } 518 err = o.skipAndSave(st, tag, wire, base, prop.unrecField) 519 continue 520 } 521 p := prop.Prop[fieldnum] 522 523 if p.dec == nil { 524 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name) 525 continue 526 } 527 dec := p.dec 528 if wire != WireStartGroup && wire != p.WireType { 529 if wire == WireBytes && p.packedDec != nil { 530 // a packable field 531 dec = p.packedDec 532 } else { 533 err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType) 534 continue 535 } 536 } 537 decErr := dec(o, p, base) 538 if decErr != nil && !state.shouldContinue(decErr, p) { 539 err = decErr 540 } 541 if err == nil && p.Required { 542 // Successfully decoded a required field. 543 if tag <= 64 { 544 // use bitmap for fields 1-64 to catch field reuse. 545 var mask uint64 = 1 << uint64(tag-1) 546 if reqFields&mask == 0 { 547 // new required field 548 reqFields |= mask 549 required-- 550 } 551 } else { 552 // This is imprecise. It can be fooled by a required field 553 // with a tag > 64 that is encoded twice; that's very rare. 554 // A fully correct implementation would require allocating 555 // a data structure, which we would like to avoid. 556 required-- 557 } 558 } 559 } 560 if err == nil { 561 if is_group { 562 return io.ErrUnexpectedEOF 563 } 564 if state.err != nil { 565 return state.err 566 } 567 if required > 0 { 568 // Not enough information to determine the exact field. If we use extra 569 // CPU, we could determine the field only if the missing required field 570 // has a tag <= 64 and we check reqFields. 571 return &RequiredNotSetError{"{Unknown}"} 572 } 573 } 574 return err 575} 576 577// Individual type decoders 578// For each, 579// u is the decoded value, 580// v is a pointer to the field (pointer) in the struct 581 582// Sizes of the pools to allocate inside the Buffer. 583// The goal is modest amortization and allocation 584// on at least 16-byte boundaries. 585const ( 586 boolPoolSize = 16 587 uint32PoolSize = 8 588 uint64PoolSize = 4 589) 590 591// Decode a bool. 592func (o *Buffer) dec_bool(p *Properties, base structPointer) error { 593 u, err := p.valDec(o) 594 if err != nil { 595 return err 596 } 597 if len(o.bools) == 0 { 598 o.bools = make([]bool, boolPoolSize) 599 } 600 o.bools[0] = u != 0 601 *structPointer_Bool(base, p.field) = &o.bools[0] 602 o.bools = o.bools[1:] 603 return nil 604} 605 606func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error { 607 u, err := p.valDec(o) 608 if err != nil { 609 return err 610 } 611 *structPointer_BoolVal(base, p.field) = u != 0 612 return nil 613} 614 615// Decode an int32. 616func (o *Buffer) dec_int32(p *Properties, base structPointer) error { 617 u, err := p.valDec(o) 618 if err != nil { 619 return err 620 } 621 word32_Set(structPointer_Word32(base, p.field), o, uint32(u)) 622 return nil 623} 624 625func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error { 626 u, err := p.valDec(o) 627 if err != nil { 628 return err 629 } 630 word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u)) 631 return nil 632} 633 634// Decode an int64. 635func (o *Buffer) dec_int64(p *Properties, base structPointer) error { 636 u, err := p.valDec(o) 637 if err != nil { 638 return err 639 } 640 word64_Set(structPointer_Word64(base, p.field), o, u) 641 return nil 642} 643 644func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error { 645 u, err := p.valDec(o) 646 if err != nil { 647 return err 648 } 649 word64Val_Set(structPointer_Word64Val(base, p.field), o, u) 650 return nil 651} 652 653// Decode a string. 654func (o *Buffer) dec_string(p *Properties, base structPointer) error { 655 s, err := o.DecodeStringBytes() 656 if err != nil { 657 return err 658 } 659 *structPointer_String(base, p.field) = &s 660 return nil 661} 662 663func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error { 664 s, err := o.DecodeStringBytes() 665 if err != nil { 666 return err 667 } 668 *structPointer_StringVal(base, p.field) = s 669 return nil 670} 671 672// Decode a slice of bytes ([]byte). 673func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error { 674 b, err := o.DecodeRawBytes(true) 675 if err != nil { 676 return err 677 } 678 *structPointer_Bytes(base, p.field) = b 679 return nil 680} 681 682// Decode a slice of bools ([]bool). 683func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error { 684 u, err := p.valDec(o) 685 if err != nil { 686 return err 687 } 688 v := structPointer_BoolSlice(base, p.field) 689 *v = append(*v, u != 0) 690 return nil 691} 692 693// Decode a slice of bools ([]bool) in packed format. 694func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error { 695 v := structPointer_BoolSlice(base, p.field) 696 697 nn, err := o.DecodeVarint() 698 if err != nil { 699 return err 700 } 701 nb := int(nn) // number of bytes of encoded bools 702 fin := o.index + nb 703 if fin < o.index { 704 return errOverflow 705 } 706 707 y := *v 708 for o.index < fin { 709 u, err := p.valDec(o) 710 if err != nil { 711 return err 712 } 713 y = append(y, u != 0) 714 } 715 716 *v = y 717 return nil 718} 719 720// Decode a slice of int32s ([]int32). 721func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error { 722 u, err := p.valDec(o) 723 if err != nil { 724 return err 725 } 726 structPointer_Word32Slice(base, p.field).Append(uint32(u)) 727 return nil 728} 729 730// Decode a slice of int32s ([]int32) in packed format. 731func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error { 732 v := structPointer_Word32Slice(base, p.field) 733 734 nn, err := o.DecodeVarint() 735 if err != nil { 736 return err 737 } 738 nb := int(nn) // number of bytes of encoded int32s 739 740 fin := o.index + nb 741 if fin < o.index { 742 return errOverflow 743 } 744 for o.index < fin { 745 u, err := p.valDec(o) 746 if err != nil { 747 return err 748 } 749 v.Append(uint32(u)) 750 } 751 return nil 752} 753 754// Decode a slice of int64s ([]int64). 755func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error { 756 u, err := p.valDec(o) 757 if err != nil { 758 return err 759 } 760 761 structPointer_Word64Slice(base, p.field).Append(u) 762 return nil 763} 764 765// Decode a slice of int64s ([]int64) in packed format. 766func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error { 767 v := structPointer_Word64Slice(base, p.field) 768 769 nn, err := o.DecodeVarint() 770 if err != nil { 771 return err 772 } 773 nb := int(nn) // number of bytes of encoded int64s 774 775 fin := o.index + nb 776 if fin < o.index { 777 return errOverflow 778 } 779 for o.index < fin { 780 u, err := p.valDec(o) 781 if err != nil { 782 return err 783 } 784 v.Append(u) 785 } 786 return nil 787} 788 789// Decode a slice of strings ([]string). 790func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error { 791 s, err := o.DecodeStringBytes() 792 if err != nil { 793 return err 794 } 795 v := structPointer_StringSlice(base, p.field) 796 *v = append(*v, s) 797 return nil 798} 799 800// Decode a slice of slice of bytes ([][]byte). 801func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error { 802 b, err := o.DecodeRawBytes(true) 803 if err != nil { 804 return err 805 } 806 v := structPointer_BytesSlice(base, p.field) 807 *v = append(*v, b) 808 return nil 809} 810 811// Decode a map field. 812func (o *Buffer) dec_new_map(p *Properties, base structPointer) error { 813 raw, err := o.DecodeRawBytes(false) 814 if err != nil { 815 return err 816 } 817 oi := o.index // index at the end of this map entry 818 o.index -= len(raw) // move buffer back to start of map entry 819 820 mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V 821 if mptr.Elem().IsNil() { 822 mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem())) 823 } 824 v := mptr.Elem() // map[K]V 825 826 // Prepare addressable doubly-indirect placeholders for the key and value types. 827 // See enc_new_map for why. 828 keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K 829 keybase := toStructPointer(keyptr.Addr()) // **K 830 831 var valbase structPointer 832 var valptr reflect.Value 833 switch p.mtype.Elem().Kind() { 834 case reflect.Slice: 835 // []byte 836 var dummy []byte 837 valptr = reflect.ValueOf(&dummy) // *[]byte 838 valbase = toStructPointer(valptr) // *[]byte 839 case reflect.Ptr: 840 // message; valptr is **Msg; need to allocate the intermediate pointer 841 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V 842 valptr.Set(reflect.New(valptr.Type().Elem())) 843 valbase = toStructPointer(valptr) 844 default: 845 // everything else 846 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V 847 valbase = toStructPointer(valptr.Addr()) // **V 848 } 849 850 // Decode. 851 // This parses a restricted wire format, namely the encoding of a message 852 // with two fields. See enc_new_map for the format. 853 for o.index < oi { 854 // tagcode for key and value properties are always a single byte 855 // because they have tags 1 and 2. 856 tagcode := o.buf[o.index] 857 o.index++ 858 switch tagcode { 859 case p.mkeyprop.tagcode[0]: 860 if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil { 861 return err 862 } 863 case p.mvalprop.tagcode[0]: 864 if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil { 865 return err 866 } 867 default: 868 // TODO: Should we silently skip this instead? 869 return fmt.Errorf("proto: bad map data tag %d", raw[0]) 870 } 871 } 872 keyelem, valelem := keyptr.Elem(), valptr.Elem() 873 if !keyelem.IsValid() { 874 keyelem = reflect.Zero(p.mtype.Key()) 875 } 876 if !valelem.IsValid() { 877 valelem = reflect.Zero(p.mtype.Elem()) 878 } 879 880 v.SetMapIndex(keyelem, valelem) 881 return nil 882} 883 884// Decode a group. 885func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error { 886 bas := structPointer_GetStructPointer(base, p.field) 887 if structPointer_IsNil(bas) { 888 // allocate new nested message 889 bas = toStructPointer(reflect.New(p.stype)) 890 structPointer_SetStructPointer(base, p.field, bas) 891 } 892 return o.unmarshalType(p.stype, p.sprop, true, bas) 893} 894 895// Decode an embedded message. 896func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) { 897 raw, e := o.DecodeRawBytes(false) 898 if e != nil { 899 return e 900 } 901 902 bas := structPointer_GetStructPointer(base, p.field) 903 if structPointer_IsNil(bas) { 904 // allocate new nested message 905 bas = toStructPointer(reflect.New(p.stype)) 906 structPointer_SetStructPointer(base, p.field, bas) 907 } 908 909 // If the object can unmarshal itself, let it. 910 if p.isUnmarshaler { 911 iv := structPointer_Interface(bas, p.stype) 912 return iv.(Unmarshaler).Unmarshal(raw) 913 } 914 915 obuf := o.buf 916 oi := o.index 917 o.buf = raw 918 o.index = 0 919 920 err = o.unmarshalType(p.stype, p.sprop, false, bas) 921 o.buf = obuf 922 o.index = oi 923 924 return err 925} 926 927// Decode a slice of embedded messages. 928func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error { 929 return o.dec_slice_struct(p, false, base) 930} 931 932// Decode a slice of embedded groups. 933func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error { 934 return o.dec_slice_struct(p, true, base) 935} 936 937// Decode a slice of structs ([]*struct). 938func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error { 939 v := reflect.New(p.stype) 940 bas := toStructPointer(v) 941 structPointer_StructPointerSlice(base, p.field).Append(bas) 942 943 if is_group { 944 err := o.unmarshalType(p.stype, p.sprop, is_group, bas) 945 return err 946 } 947 948 raw, err := o.DecodeRawBytes(false) 949 if err != nil { 950 return err 951 } 952 953 // If the object can unmarshal itself, let it. 954 if p.isUnmarshaler { 955 iv := v.Interface() 956 return iv.(Unmarshaler).Unmarshal(raw) 957 } 958 959 obuf := o.buf 960 oi := o.index 961 o.buf = raw 962 o.index = 0 963 964 err = o.unmarshalType(p.stype, p.sprop, is_group, bas) 965 966 o.buf = obuf 967 o.index = oi 968 969 return err 970} 971