github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/third_party/code.google.com/p/gogoprotobuf/proto/decode.go (about) 1 // Go support for Protocol Buffers - Google's data interchange format 2 // 3 // Copyright 2010 The Go Authors. All rights reserved. 4 // http://code.google.com/p/goprotobuf/ 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 32 package proto 33 34 /* 35 * Routines for decoding protocol buffer data to construct in-memory representations. 36 */ 37 38 import ( 39 "errors" 40 "fmt" 41 "io" 42 "os" 43 "reflect" 44 ) 45 46 // ErrWrongType occurs when the wire encoding for the field disagrees with 47 // that specified in the type being decoded. This is usually caused by attempting 48 // to convert an encoded protocol buffer into a struct of the wrong type. 49 var ErrWrongType = errors.New("proto: field/encoding mismatch: wrong type for field") 50 51 // errOverflow is returned when an integer is too large to be represented. 52 var errOverflow = errors.New("proto: integer overflow") 53 54 // The fundamental decoders that interpret bytes on the wire. 55 // Those that take integer types all return uint64 and are 56 // therefore of type valueDecoder. 57 58 // DecodeVarint reads a varint-encoded integer from the slice. 59 // It returns the integer and the number of bytes consumed, or 60 // zero if there is not enough. 61 // This is the format for the 62 // int32, int64, uint32, uint64, bool, and enum 63 // protocol buffer types. 64 func DecodeVarint(buf []byte) (x uint64, n int) { 65 // x, n already 0 66 for shift := uint(0); shift < 64; shift += 7 { 67 if n >= len(buf) { 68 return 0, 0 69 } 70 b := uint64(buf[n]) 71 n++ 72 x |= (b & 0x7F) << shift 73 if (b & 0x80) == 0 { 74 return x, n 75 } 76 } 77 78 // The number is too large to represent in a 64-bit value. 79 return 0, 0 80 } 81 82 // DecodeVarint reads a varint-encoded integer from the Buffer. 83 // This is the format for the 84 // int32, int64, uint32, uint64, bool, and enum 85 // protocol buffer types. 86 func (p *Buffer) DecodeVarint() (x uint64, err error) { 87 // x, err already 0 88 89 i := p.index 90 l := len(p.buf) 91 92 for shift := uint(0); shift < 64; shift += 7 { 93 if i >= l { 94 err = io.ErrUnexpectedEOF 95 return 96 } 97 b := p.buf[i] 98 i++ 99 x |= (uint64(b) & 0x7F) << shift 100 if b < 0x80 { 101 p.index = i 102 return 103 } 104 } 105 106 // The number is too large to represent in a 64-bit value. 107 err = errOverflow 108 return 109 } 110 111 // DecodeFixed64 reads a 64-bit integer from the Buffer. 112 // This is the format for the 113 // fixed64, sfixed64, and double protocol buffer types. 114 func (p *Buffer) DecodeFixed64() (x uint64, err error) { 115 // x, err already 0 116 i := p.index + 8 117 if i < 0 || i > len(p.buf) { 118 err = io.ErrUnexpectedEOF 119 return 120 } 121 p.index = i 122 123 x = uint64(p.buf[i-8]) 124 x |= uint64(p.buf[i-7]) << 8 125 x |= uint64(p.buf[i-6]) << 16 126 x |= uint64(p.buf[i-5]) << 24 127 x |= uint64(p.buf[i-4]) << 32 128 x |= uint64(p.buf[i-3]) << 40 129 x |= uint64(p.buf[i-2]) << 48 130 x |= uint64(p.buf[i-1]) << 56 131 return 132 } 133 134 // DecodeFixed32 reads a 32-bit integer from the Buffer. 135 // This is the format for the 136 // fixed32, sfixed32, and float protocol buffer types. 137 func (p *Buffer) DecodeFixed32() (x uint64, err error) { 138 // x, err already 0 139 i := p.index + 4 140 if i < 0 || i > len(p.buf) { 141 err = io.ErrUnexpectedEOF 142 return 143 } 144 p.index = i 145 146 x = uint64(p.buf[i-4]) 147 x |= uint64(p.buf[i-3]) << 8 148 x |= uint64(p.buf[i-2]) << 16 149 x |= uint64(p.buf[i-1]) << 24 150 return 151 } 152 153 // DecodeZigzag64 reads a zigzag-encoded 64-bit integer 154 // from the Buffer. 155 // This is the format used for the sint64 protocol buffer type. 156 func (p *Buffer) DecodeZigzag64() (x uint64, err error) { 157 x, err = p.DecodeVarint() 158 if err != nil { 159 return 160 } 161 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63) 162 return 163 } 164 165 // DecodeZigzag32 reads a zigzag-encoded 32-bit integer 166 // from the Buffer. 167 // This is the format used for the sint32 protocol buffer type. 168 func (p *Buffer) DecodeZigzag32() (x uint64, err error) { 169 x, err = p.DecodeVarint() 170 if err != nil { 171 return 172 } 173 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31)) 174 return 175 } 176 177 // These are not ValueDecoders: they produce an array of bytes or a string. 178 // bytes, embedded messages 179 180 // DecodeRawBytes reads a count-delimited byte buffer from the Buffer. 181 // This is the format used for the bytes protocol buffer 182 // type and for embedded messages. 183 func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) { 184 n, err := p.DecodeVarint() 185 if err != nil { 186 return 187 } 188 189 nb := int(n) 190 if nb < 0 { 191 return nil, fmt.Errorf("proto: bad byte length %d", nb) 192 } 193 end := p.index + nb 194 if end < p.index || end > len(p.buf) { 195 return nil, io.ErrUnexpectedEOF 196 } 197 198 if !alloc { 199 // todo: check if can get more uses of alloc=false 200 buf = p.buf[p.index:end] 201 p.index += nb 202 return 203 } 204 205 buf = make([]byte, nb) 206 copy(buf, p.buf[p.index:]) 207 p.index += nb 208 return 209 } 210 211 // DecodeStringBytes reads an encoded string from the Buffer. 212 // This is the format used for the proto2 string type. 213 func (p *Buffer) DecodeStringBytes() (s string, err error) { 214 buf, err := p.DecodeRawBytes(false) 215 if err != nil { 216 return 217 } 218 return string(buf), nil 219 } 220 221 // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. 222 // If the protocol buffer has extensions, and the field matches, add it as an extension. 223 // Otherwise, if the XXX_unrecognized field exists, append the skipped data there. 224 func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error { 225 oi := o.index 226 227 err := o.skip(t, tag, wire) 228 if err != nil { 229 return err 230 } 231 232 if !unrecField.IsValid() { 233 return nil 234 } 235 236 ptr := structPointer_Bytes(base, unrecField) 237 238 if *ptr == nil { 239 // This is the first skipped element, 240 // allocate a new buffer. 241 *ptr = o.bufalloc() 242 } 243 244 // Add the skipped field to struct field 245 obuf := o.buf 246 247 o.buf = *ptr 248 o.EncodeVarint(uint64(tag<<3 | wire)) 249 *ptr = append(o.buf, obuf[oi:o.index]...) 250 251 o.buf = obuf 252 253 return nil 254 } 255 256 // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. 257 func (o *Buffer) skip(t reflect.Type, tag, wire int) error { 258 259 var u uint64 260 var err error 261 262 switch wire { 263 case WireVarint: 264 _, err = o.DecodeVarint() 265 case WireFixed64: 266 _, err = o.DecodeFixed64() 267 case WireBytes: 268 _, err = o.DecodeRawBytes(false) 269 case WireFixed32: 270 _, err = o.DecodeFixed32() 271 case WireStartGroup: 272 for { 273 u, err = o.DecodeVarint() 274 if err != nil { 275 break 276 } 277 fwire := int(u & 0x7) 278 if fwire == WireEndGroup { 279 break 280 } 281 ftag := int(u >> 3) 282 err = o.skip(t, ftag, fwire) 283 if err != nil { 284 break 285 } 286 } 287 default: 288 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t) 289 } 290 return err 291 } 292 293 // Unmarshaler is the interface representing objects that can 294 // unmarshal themselves. The method should reset the receiver before 295 // decoding starts. The argument points to data that may be 296 // overwritten, so implementations should not keep references to the 297 // buffer. 298 type Unmarshaler interface { 299 Unmarshal([]byte) error 300 } 301 302 // Unmarshal parses the protocol buffer representation in buf and places the 303 // decoded result in pb. If the struct underlying pb does not match 304 // the data in buf, the results can be unpredictable. 305 // 306 // Unmarshal resets pb before starting to unmarshal, so any 307 // existing data in pb is always removed. Use UnmarshalMerge 308 // to preserve and append to existing data. 309 func Unmarshal(buf []byte, pb Message) error { 310 pb.Reset() 311 return UnmarshalMerge(buf, pb) 312 } 313 314 // UnmarshalMerge parses the protocol buffer representation in buf and 315 // writes the decoded result to pb. If the struct underlying pb does not match 316 // the data in buf, the results can be unpredictable. 317 // 318 // UnmarshalMerge merges into existing data in pb. 319 // Most code should use Unmarshal instead. 320 func UnmarshalMerge(buf []byte, pb Message) error { 321 // If the object can unmarshal itself, let it. 322 if u, ok := pb.(Unmarshaler); ok { 323 return u.Unmarshal(buf) 324 } 325 return NewBuffer(buf).Unmarshal(pb) 326 } 327 328 // Unmarshal parses the protocol buffer representation in the 329 // Buffer and places the decoded result in pb. If the struct 330 // underlying pb does not match the data in the buffer, the results can be 331 // unpredictable. 332 func (p *Buffer) Unmarshal(pb Message) error { 333 // If the object can unmarshal itself, let it. 334 if u, ok := pb.(Unmarshaler); ok { 335 err := u.Unmarshal(p.buf[p.index:]) 336 p.index = len(p.buf) 337 return err 338 } 339 340 typ, base, err := getbase(pb) 341 if err != nil { 342 return err 343 } 344 345 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base) 346 347 if collectStats { 348 stats.Decode++ 349 } 350 351 return err 352 } 353 354 // unmarshalType does the work of unmarshaling a structure. 355 func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error { 356 var state errorState 357 required, reqFields := prop.reqCount, uint64(0) 358 359 var err error 360 for err == nil && o.index < len(o.buf) { 361 oi := o.index 362 var u uint64 363 u, err = o.DecodeVarint() 364 if err != nil { 365 break 366 } 367 wire := int(u & 0x7) 368 if wire == WireEndGroup { 369 if is_group { 370 return nil // input is satisfied 371 } 372 return ErrWrongType 373 } 374 tag := int(u >> 3) 375 if tag <= 0 { 376 return fmt.Errorf("proto: illegal tag %d", tag) 377 } 378 fieldnum, ok := prop.decoderTags.get(tag) 379 if !ok { 380 // Maybe it's an extension? 381 if prop.extendable { 382 if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) { 383 if err = o.skip(st, tag, wire); err == nil { 384 ext := e.ExtensionMap()[int32(tag)] // may be missing 385 ext.enc = append(ext.enc, o.buf[oi:o.index]...) 386 e.ExtensionMap()[int32(tag)] = ext 387 } 388 continue 389 } 390 } 391 err = o.skipAndSave(st, tag, wire, base, prop.unrecField) 392 continue 393 } 394 p := prop.Prop[fieldnum] 395 396 if p.dec == nil { 397 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name) 398 continue 399 } 400 dec := p.dec 401 if wire != WireStartGroup && wire != p.WireType { 402 if wire == WireBytes && p.packedDec != nil { 403 // a packable field 404 dec = p.packedDec 405 } else { 406 err = ErrWrongType 407 continue 408 } 409 } 410 decErr := dec(o, p, base) 411 if decErr != nil && !state.shouldContinue(decErr, p) { 412 err = decErr 413 } 414 if err == nil && p.Required { 415 // Successfully decoded a required field. 416 if tag <= 64 { 417 // use bitmap for fields 1-64 to catch field reuse. 418 var mask uint64 = 1 << uint64(tag-1) 419 if reqFields&mask == 0 { 420 // new required field 421 reqFields |= mask 422 required-- 423 } 424 } else { 425 // This is imprecise. It can be fooled by a required field 426 // with a tag > 64 that is encoded twice; that's very rare. 427 // A fully correct implementation would require allocating 428 // a data structure, which we would like to avoid. 429 required-- 430 } 431 } 432 } 433 if err == nil { 434 if is_group { 435 return io.ErrUnexpectedEOF 436 } 437 if state.err != nil { 438 return state.err 439 } 440 if required > 0 { 441 // Not enough information to determine the exact field. If we use extra 442 // CPU, we could determine the field only if the missing required field 443 // has a tag <= 64 and we check reqFields. 444 return &RequiredNotSetError{"{Unknown}"} 445 } 446 } 447 return err 448 } 449 450 // Individual type decoders 451 // For each, 452 // u is the decoded value, 453 // v is a pointer to the field (pointer) in the struct 454 455 // Sizes of the pools to allocate inside the Buffer. 456 // The goal is modest amortization and allocation 457 // on at least 16-byte boundaries. 458 const ( 459 boolPoolSize = 16 460 uint32PoolSize = 8 461 uint64PoolSize = 4 462 ) 463 464 // Decode a bool. 465 func (o *Buffer) dec_bool(p *Properties, base structPointer) error { 466 u, err := p.valDec(o) 467 if err != nil { 468 return err 469 } 470 if len(o.bools) == 0 { 471 o.bools = make([]bool, boolPoolSize) 472 } 473 o.bools[0] = u != 0 474 *structPointer_Bool(base, p.field) = &o.bools[0] 475 o.bools = o.bools[1:] 476 return nil 477 } 478 479 // Decode an int32. 480 func (o *Buffer) dec_int32(p *Properties, base structPointer) error { 481 u, err := p.valDec(o) 482 if err != nil { 483 return err 484 } 485 word32_Set(structPointer_Word32(base, p.field), o, uint32(u)) 486 return nil 487 } 488 489 // Decode an int64. 490 func (o *Buffer) dec_int64(p *Properties, base structPointer) error { 491 u, err := p.valDec(o) 492 if err != nil { 493 return err 494 } 495 word64_Set(structPointer_Word64(base, p.field), o, u) 496 return nil 497 } 498 499 // Decode a string. 500 func (o *Buffer) dec_string(p *Properties, base structPointer) error { 501 s, err := o.DecodeStringBytes() 502 if err != nil { 503 return err 504 } 505 sp := new(string) 506 *sp = s 507 *structPointer_String(base, p.field) = sp 508 return nil 509 } 510 511 // Decode a slice of bytes ([]byte). 512 func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error { 513 b, err := o.DecodeRawBytes(true) 514 if err != nil { 515 return err 516 } 517 *structPointer_Bytes(base, p.field) = b 518 return nil 519 } 520 521 // Decode a slice of bools ([]bool). 522 func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error { 523 u, err := p.valDec(o) 524 if err != nil { 525 return err 526 } 527 v := structPointer_BoolSlice(base, p.field) 528 *v = append(*v, u != 0) 529 return nil 530 } 531 532 // Decode a slice of bools ([]bool) in packed format. 533 func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error { 534 v := structPointer_BoolSlice(base, p.field) 535 536 nn, err := o.DecodeVarint() 537 if err != nil { 538 return err 539 } 540 nb := int(nn) // number of bytes of encoded bools 541 542 y := *v 543 for i := 0; i < nb; i++ { 544 u, err := p.valDec(o) 545 if err != nil { 546 return err 547 } 548 y = append(y, u != 0) 549 } 550 551 *v = y 552 return nil 553 } 554 555 // Decode a slice of int32s ([]int32). 556 func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error { 557 u, err := p.valDec(o) 558 if err != nil { 559 return err 560 } 561 structPointer_Word32Slice(base, p.field).Append(uint32(u)) 562 return nil 563 } 564 565 // Decode a slice of int32s ([]int32) in packed format. 566 func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error { 567 v := structPointer_Word32Slice(base, p.field) 568 569 nn, err := o.DecodeVarint() 570 if err != nil { 571 return err 572 } 573 nb := int(nn) // number of bytes of encoded int32s 574 575 fin := o.index + nb 576 if fin < o.index { 577 return errOverflow 578 } 579 for o.index < fin { 580 u, err := p.valDec(o) 581 if err != nil { 582 return err 583 } 584 v.Append(uint32(u)) 585 } 586 return nil 587 } 588 589 // Decode a slice of int64s ([]int64). 590 func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error { 591 u, err := p.valDec(o) 592 if err != nil { 593 return err 594 } 595 596 structPointer_Word64Slice(base, p.field).Append(u) 597 return nil 598 } 599 600 // Decode a slice of int64s ([]int64) in packed format. 601 func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error { 602 v := structPointer_Word64Slice(base, p.field) 603 604 nn, err := o.DecodeVarint() 605 if err != nil { 606 return err 607 } 608 nb := int(nn) // number of bytes of encoded int64s 609 610 fin := o.index + nb 611 if fin < o.index { 612 return errOverflow 613 } 614 for o.index < fin { 615 u, err := p.valDec(o) 616 if err != nil { 617 return err 618 } 619 v.Append(u) 620 } 621 return nil 622 } 623 624 // Decode a slice of strings ([]string). 625 func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error { 626 s, err := o.DecodeStringBytes() 627 if err != nil { 628 return err 629 } 630 v := structPointer_StringSlice(base, p.field) 631 *v = append(*v, s) 632 return nil 633 } 634 635 // Decode a slice of slice of bytes ([][]byte). 636 func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error { 637 b, err := o.DecodeRawBytes(true) 638 if err != nil { 639 return err 640 } 641 v := structPointer_BytesSlice(base, p.field) 642 *v = append(*v, b) 643 return nil 644 } 645 646 // Decode a group. 647 func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error { 648 bas := structPointer_GetStructPointer(base, p.field) 649 if structPointer_IsNil(bas) { 650 // allocate new nested message 651 bas = toStructPointer(reflect.New(p.stype)) 652 structPointer_SetStructPointer(base, p.field, bas) 653 } 654 return o.unmarshalType(p.stype, p.sprop, true, bas) 655 } 656 657 // Decode an embedded message. 658 func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) { 659 raw, e := o.DecodeRawBytes(false) 660 if e != nil { 661 return e 662 } 663 664 bas := structPointer_GetStructPointer(base, p.field) 665 if structPointer_IsNil(bas) { 666 // allocate new nested message 667 bas = toStructPointer(reflect.New(p.stype)) 668 structPointer_SetStructPointer(base, p.field, bas) 669 } 670 671 // If the object can unmarshal itself, let it. 672 if p.isUnmarshaler { 673 iv := structPointer_Interface(bas, p.stype) 674 return iv.(Unmarshaler).Unmarshal(raw) 675 } 676 677 obuf := o.buf 678 oi := o.index 679 o.buf = raw 680 o.index = 0 681 682 err = o.unmarshalType(p.stype, p.sprop, false, bas) 683 o.buf = obuf 684 o.index = oi 685 686 return err 687 } 688 689 // Decode a slice of embedded messages. 690 func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error { 691 return o.dec_slice_struct(p, false, base) 692 } 693 694 // Decode a slice of embedded groups. 695 func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error { 696 return o.dec_slice_struct(p, true, base) 697 } 698 699 // Decode a slice of structs ([]*struct). 700 func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error { 701 v := reflect.New(p.stype) 702 bas := toStructPointer(v) 703 structPointer_StructPointerSlice(base, p.field).Append(bas) 704 705 if is_group { 706 err := o.unmarshalType(p.stype, p.sprop, is_group, bas) 707 return err 708 } 709 710 raw, err := o.DecodeRawBytes(false) 711 if err != nil { 712 return err 713 } 714 715 // If the object can unmarshal itself, let it. 716 if p.isUnmarshaler { 717 iv := v.Interface() 718 return iv.(Unmarshaler).Unmarshal(raw) 719 } 720 721 obuf := o.buf 722 oi := o.index 723 o.buf = raw 724 o.index = 0 725 726 err = o.unmarshalType(p.stype, p.sprop, is_group, bas) 727 728 o.buf = obuf 729 o.index = oi 730 731 return err 732 }