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 encoding data into the wire format for protocol buffers. 36 */ 37 38import ( 39 "errors" 40 "fmt" 41 "reflect" 42 "sort" 43) 44 45// RequiredNotSetError is the error returned if Marshal is called with 46// a protocol buffer struct whose required fields have not 47// all been initialized. It is also the error returned if Unmarshal is 48// called with an encoded protocol buffer that does not include all the 49// required fields. 50// 51// When printed, RequiredNotSetError reports the first unset required field in a 52// message. If the field cannot be precisely determined, it is reported as 53// "{Unknown}". 54type RequiredNotSetError struct { 55 field string 56} 57 58func (e *RequiredNotSetError) Error() string { 59 return fmt.Sprintf("proto: required field %q not set", e.field) 60} 61 62var ( 63 // errRepeatedHasNil is the error returned if Marshal is called with 64 // a struct with a repeated field containing a nil element. 65 errRepeatedHasNil = errors.New("proto: repeated field has nil element") 66 67 // ErrNil is the error returned if Marshal is called with nil. 68 ErrNil = errors.New("proto: Marshal called with nil") 69) 70 71// The fundamental encoders that put bytes on the wire. 72// Those that take integer types all accept uint64 and are 73// therefore of type valueEncoder. 74 75const maxVarintBytes = 10 // maximum length of a varint 76 77// EncodeVarint returns the varint encoding of x. 78// This is the format for the 79// int32, int64, uint32, uint64, bool, and enum 80// protocol buffer types. 81// Not used by the package itself, but helpful to clients 82// wishing to use the same encoding. 83func EncodeVarint(x uint64) []byte { 84 var buf [maxVarintBytes]byte 85 var n int 86 for n = 0; x > 127; n++ { 87 buf[n] = 0x80 | uint8(x&0x7F) 88 x >>= 7 89 } 90 buf[n] = uint8(x) 91 n++ 92 return buf[0:n] 93} 94 95// EncodeVarint writes a varint-encoded integer to the Buffer. 96// This is the format for the 97// int32, int64, uint32, uint64, bool, and enum 98// protocol buffer types. 99func (p *Buffer) EncodeVarint(x uint64) error { 100 for x >= 1<<7 { 101 p.buf = append(p.buf, uint8(x&0x7f|0x80)) 102 x >>= 7 103 } 104 p.buf = append(p.buf, uint8(x)) 105 return nil 106} 107 108// SizeVarint returns the varint encoding size of an integer. 109func SizeVarint(x uint64) int { 110 return sizeVarint(x) 111} 112 113func sizeVarint(x uint64) (n int) { 114 for { 115 n++ 116 x >>= 7 117 if x == 0 { 118 break 119 } 120 } 121 return n 122} 123 124// EncodeFixed64 writes a 64-bit integer to the Buffer. 125// This is the format for the 126// fixed64, sfixed64, and double protocol buffer types. 127func (p *Buffer) EncodeFixed64(x uint64) error { 128 p.buf = append(p.buf, 129 uint8(x), 130 uint8(x>>8), 131 uint8(x>>16), 132 uint8(x>>24), 133 uint8(x>>32), 134 uint8(x>>40), 135 uint8(x>>48), 136 uint8(x>>56)) 137 return nil 138} 139 140func sizeFixed64(x uint64) int { 141 return 8 142} 143 144// EncodeFixed32 writes a 32-bit integer to the Buffer. 145// This is the format for the 146// fixed32, sfixed32, and float protocol buffer types. 147func (p *Buffer) EncodeFixed32(x uint64) error { 148 p.buf = append(p.buf, 149 uint8(x), 150 uint8(x>>8), 151 uint8(x>>16), 152 uint8(x>>24)) 153 return nil 154} 155 156func sizeFixed32(x uint64) int { 157 return 4 158} 159 160// EncodeZigzag64 writes a zigzag-encoded 64-bit integer 161// to the Buffer. 162// This is the format used for the sint64 protocol buffer type. 163func (p *Buffer) EncodeZigzag64(x uint64) error { 164 // use signed number to get arithmetic right shift. 165 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) 166} 167 168func sizeZigzag64(x uint64) int { 169 return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) 170} 171 172// EncodeZigzag32 writes a zigzag-encoded 32-bit integer 173// to the Buffer. 174// This is the format used for the sint32 protocol buffer type. 175func (p *Buffer) EncodeZigzag32(x uint64) error { 176 // use signed number to get arithmetic right shift. 177 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) 178} 179 180func sizeZigzag32(x uint64) int { 181 return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) 182} 183 184// EncodeRawBytes writes a count-delimited byte buffer to the Buffer. 185// This is the format used for the bytes protocol buffer 186// type and for embedded messages. 187func (p *Buffer) EncodeRawBytes(b []byte) error { 188 p.EncodeVarint(uint64(len(b))) 189 p.buf = append(p.buf, b...) 190 return nil 191} 192 193func sizeRawBytes(b []byte) int { 194 return sizeVarint(uint64(len(b))) + 195 len(b) 196} 197 198// EncodeStringBytes writes an encoded string to the Buffer. 199// This is the format used for the proto2 string type. 200func (p *Buffer) EncodeStringBytes(s string) error { 201 p.EncodeVarint(uint64(len(s))) 202 p.buf = append(p.buf, s...) 203 return nil 204} 205 206func sizeStringBytes(s string) int { 207 return sizeVarint(uint64(len(s))) + 208 len(s) 209} 210 211// Marshaler is the interface representing objects that can marshal themselves. 212type Marshaler interface { 213 Marshal() ([]byte, error) 214} 215 216// Marshal takes the protocol buffer 217// and encodes it into the wire format, returning the data. 218func Marshal(pb Message) ([]byte, error) { 219 // Can the object marshal itself? 220 if m, ok := pb.(Marshaler); ok { 221 return m.Marshal() 222 } 223 p := NewBuffer(nil) 224 err := p.Marshal(pb) 225 var state errorState 226 if err != nil && !state.shouldContinue(err, nil) { 227 return nil, err 228 } 229 if p.buf == nil && err == nil { 230 // Return a non-nil slice on success. 231 return []byte{}, nil 232 } 233 return p.buf, err 234} 235 236// EncodeMessage writes the protocol buffer to the Buffer, 237// prefixed by a varint-encoded length. 238func (p *Buffer) EncodeMessage(pb Message) error { 239 t, base, err := getbase(pb) 240 if structPointer_IsNil(base) { 241 return ErrNil 242 } 243 if err == nil { 244 var state errorState 245 err = p.enc_len_struct(GetProperties(t.Elem()), base, &state) 246 } 247 return err 248} 249 250// Marshal takes the protocol buffer 251// and encodes it into the wire format, writing the result to the 252// Buffer. 253func (p *Buffer) Marshal(pb Message) error { 254 // Can the object marshal itself? 255 if m, ok := pb.(Marshaler); ok { 256 data, err := m.Marshal() 257 if err != nil { 258 return err 259 } 260 p.buf = append(p.buf, data...) 261 return nil 262 } 263 264 t, base, err := getbase(pb) 265 if structPointer_IsNil(base) { 266 return ErrNil 267 } 268 if err == nil { 269 err = p.enc_struct(GetProperties(t.Elem()), base) 270 } 271 272 if collectStats { 273 stats.Encode++ 274 } 275 276 return err 277} 278 279// Size returns the encoded size of a protocol buffer. 280func Size(pb Message) (n int) { 281 // Can the object marshal itself? If so, Size is slow. 282 // TODO: add Size to Marshaler, or add a Sizer interface. 283 if m, ok := pb.(Marshaler); ok { 284 b, _ := m.Marshal() 285 return len(b) 286 } 287 288 t, base, err := getbase(pb) 289 if structPointer_IsNil(base) { 290 return 0 291 } 292 if err == nil { 293 n = size_struct(GetProperties(t.Elem()), base) 294 } 295 296 if collectStats { 297 stats.Size++ 298 } 299 300 return 301} 302 303// Individual type encoders. 304 305// Encode a bool. 306func (o *Buffer) enc_bool(p *Properties, base structPointer) error { 307 v := *structPointer_Bool(base, p.field) 308 if v == nil { 309 return ErrNil 310 } 311 x := 0 312 if *v { 313 x = 1 314 } 315 o.buf = append(o.buf, p.tagcode...) 316 p.valEnc(o, uint64(x)) 317 return nil 318} 319 320func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error { 321 v := *structPointer_BoolVal(base, p.field) 322 if !v { 323 return ErrNil 324 } 325 o.buf = append(o.buf, p.tagcode...) 326 p.valEnc(o, 1) 327 return nil 328} 329 330func size_bool(p *Properties, base structPointer) int { 331 v := *structPointer_Bool(base, p.field) 332 if v == nil { 333 return 0 334 } 335 return len(p.tagcode) + 1 // each bool takes exactly one byte 336} 337 338func size_proto3_bool(p *Properties, base structPointer) int { 339 v := *structPointer_BoolVal(base, p.field) 340 if !v && !p.oneof { 341 return 0 342 } 343 return len(p.tagcode) + 1 // each bool takes exactly one byte 344} 345 346// Encode an int32. 347func (o *Buffer) enc_int32(p *Properties, base structPointer) error { 348 v := structPointer_Word32(base, p.field) 349 if word32_IsNil(v) { 350 return ErrNil 351 } 352 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range 353 o.buf = append(o.buf, p.tagcode...) 354 p.valEnc(o, uint64(x)) 355 return nil 356} 357 358func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error { 359 v := structPointer_Word32Val(base, p.field) 360 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range 361 if x == 0 { 362 return ErrNil 363 } 364 o.buf = append(o.buf, p.tagcode...) 365 p.valEnc(o, uint64(x)) 366 return nil 367} 368 369func size_int32(p *Properties, base structPointer) (n int) { 370 v := structPointer_Word32(base, p.field) 371 if word32_IsNil(v) { 372 return 0 373 } 374 x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range 375 n += len(p.tagcode) 376 n += p.valSize(uint64(x)) 377 return 378} 379 380func size_proto3_int32(p *Properties, base structPointer) (n int) { 381 v := structPointer_Word32Val(base, p.field) 382 x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range 383 if x == 0 && !p.oneof { 384 return 0 385 } 386 n += len(p.tagcode) 387 n += p.valSize(uint64(x)) 388 return 389} 390 391// Encode a uint32. 392// Exactly the same as int32, except for no sign extension. 393func (o *Buffer) enc_uint32(p *Properties, base structPointer) error { 394 v := structPointer_Word32(base, p.field) 395 if word32_IsNil(v) { 396 return ErrNil 397 } 398 x := word32_Get(v) 399 o.buf = append(o.buf, p.tagcode...) 400 p.valEnc(o, uint64(x)) 401 return nil 402} 403 404func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error { 405 v := structPointer_Word32Val(base, p.field) 406 x := word32Val_Get(v) 407 if x == 0 { 408 return ErrNil 409 } 410 o.buf = append(o.buf, p.tagcode...) 411 p.valEnc(o, uint64(x)) 412 return nil 413} 414 415func size_uint32(p *Properties, base structPointer) (n int) { 416 v := structPointer_Word32(base, p.field) 417 if word32_IsNil(v) { 418 return 0 419 } 420 x := word32_Get(v) 421 n += len(p.tagcode) 422 n += p.valSize(uint64(x)) 423 return 424} 425 426func size_proto3_uint32(p *Properties, base structPointer) (n int) { 427 v := structPointer_Word32Val(base, p.field) 428 x := word32Val_Get(v) 429 if x == 0 && !p.oneof { 430 return 0 431 } 432 n += len(p.tagcode) 433 n += p.valSize(uint64(x)) 434 return 435} 436 437// Encode an int64. 438func (o *Buffer) enc_int64(p *Properties, base structPointer) error { 439 v := structPointer_Word64(base, p.field) 440 if word64_IsNil(v) { 441 return ErrNil 442 } 443 x := word64_Get(v) 444 o.buf = append(o.buf, p.tagcode...) 445 p.valEnc(o, x) 446 return nil 447} 448 449func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error { 450 v := structPointer_Word64Val(base, p.field) 451 x := word64Val_Get(v) 452 if x == 0 { 453 return ErrNil 454 } 455 o.buf = append(o.buf, p.tagcode...) 456 p.valEnc(o, x) 457 return nil 458} 459 460func size_int64(p *Properties, base structPointer) (n int) { 461 v := structPointer_Word64(base, p.field) 462 if word64_IsNil(v) { 463 return 0 464 } 465 x := word64_Get(v) 466 n += len(p.tagcode) 467 n += p.valSize(x) 468 return 469} 470 471func size_proto3_int64(p *Properties, base structPointer) (n int) { 472 v := structPointer_Word64Val(base, p.field) 473 x := word64Val_Get(v) 474 if x == 0 && !p.oneof { 475 return 0 476 } 477 n += len(p.tagcode) 478 n += p.valSize(x) 479 return 480} 481 482// Encode a string. 483func (o *Buffer) enc_string(p *Properties, base structPointer) error { 484 v := *structPointer_String(base, p.field) 485 if v == nil { 486 return ErrNil 487 } 488 x := *v 489 o.buf = append(o.buf, p.tagcode...) 490 o.EncodeStringBytes(x) 491 return nil 492} 493 494func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error { 495 v := *structPointer_StringVal(base, p.field) 496 if v == "" { 497 return ErrNil 498 } 499 o.buf = append(o.buf, p.tagcode...) 500 o.EncodeStringBytes(v) 501 return nil 502} 503 504func size_string(p *Properties, base structPointer) (n int) { 505 v := *structPointer_String(base, p.field) 506 if v == nil { 507 return 0 508 } 509 x := *v 510 n += len(p.tagcode) 511 n += sizeStringBytes(x) 512 return 513} 514 515func size_proto3_string(p *Properties, base structPointer) (n int) { 516 v := *structPointer_StringVal(base, p.field) 517 if v == "" && !p.oneof { 518 return 0 519 } 520 n += len(p.tagcode) 521 n += sizeStringBytes(v) 522 return 523} 524 525// All protocol buffer fields are nillable, but be careful. 526func isNil(v reflect.Value) bool { 527 switch v.Kind() { 528 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: 529 return v.IsNil() 530 } 531 return false 532} 533 534// Encode a message struct. 535func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { 536 var state errorState 537 structp := structPointer_GetStructPointer(base, p.field) 538 if structPointer_IsNil(structp) { 539 return ErrNil 540 } 541 542 // Can the object marshal itself? 543 if p.isMarshaler { 544 m := structPointer_Interface(structp, p.stype).(Marshaler) 545 data, err := m.Marshal() 546 if err != nil && !state.shouldContinue(err, nil) { 547 return err 548 } 549 o.buf = append(o.buf, p.tagcode...) 550 o.EncodeRawBytes(data) 551 return state.err 552 } 553 554 o.buf = append(o.buf, p.tagcode...) 555 return o.enc_len_struct(p.sprop, structp, &state) 556} 557 558func size_struct_message(p *Properties, base structPointer) int { 559 structp := structPointer_GetStructPointer(base, p.field) 560 if structPointer_IsNil(structp) { 561 return 0 562 } 563 564 // Can the object marshal itself? 565 if p.isMarshaler { 566 m := structPointer_Interface(structp, p.stype).(Marshaler) 567 data, _ := m.Marshal() 568 n0 := len(p.tagcode) 569 n1 := sizeRawBytes(data) 570 return n0 + n1 571 } 572 573 n0 := len(p.tagcode) 574 n1 := size_struct(p.sprop, structp) 575 n2 := sizeVarint(uint64(n1)) // size of encoded length 576 return n0 + n1 + n2 577} 578 579// Encode a group struct. 580func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { 581 var state errorState 582 b := structPointer_GetStructPointer(base, p.field) 583 if structPointer_IsNil(b) { 584 return ErrNil 585 } 586 587 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) 588 err := o.enc_struct(p.sprop, b) 589 if err != nil && !state.shouldContinue(err, nil) { 590 return err 591 } 592 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) 593 return state.err 594} 595 596func size_struct_group(p *Properties, base structPointer) (n int) { 597 b := structPointer_GetStructPointer(base, p.field) 598 if structPointer_IsNil(b) { 599 return 0 600 } 601 602 n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup)) 603 n += size_struct(p.sprop, b) 604 n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup)) 605 return 606} 607 608// Encode a slice of bools ([]bool). 609func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { 610 s := *structPointer_BoolSlice(base, p.field) 611 l := len(s) 612 if l == 0 { 613 return ErrNil 614 } 615 for _, x := range s { 616 o.buf = append(o.buf, p.tagcode...) 617 v := uint64(0) 618 if x { 619 v = 1 620 } 621 p.valEnc(o, v) 622 } 623 return nil 624} 625 626func size_slice_bool(p *Properties, base structPointer) int { 627 s := *structPointer_BoolSlice(base, p.field) 628 l := len(s) 629 if l == 0 { 630 return 0 631 } 632 return l * (len(p.tagcode) + 1) // each bool takes exactly one byte 633} 634 635// Encode a slice of bools ([]bool) in packed format. 636func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { 637 s := *structPointer_BoolSlice(base, p.field) 638 l := len(s) 639 if l == 0 { 640 return ErrNil 641 } 642 o.buf = append(o.buf, p.tagcode...) 643 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte 644 for _, x := range s { 645 v := uint64(0) 646 if x { 647 v = 1 648 } 649 p.valEnc(o, v) 650 } 651 return nil 652} 653 654func size_slice_packed_bool(p *Properties, base structPointer) (n int) { 655 s := *structPointer_BoolSlice(base, p.field) 656 l := len(s) 657 if l == 0 { 658 return 0 659 } 660 n += len(p.tagcode) 661 n += sizeVarint(uint64(l)) 662 n += l // each bool takes exactly one byte 663 return 664} 665 666// Encode a slice of bytes ([]byte). 667func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { 668 s := *structPointer_Bytes(base, p.field) 669 if s == nil { 670 return ErrNil 671 } 672 o.buf = append(o.buf, p.tagcode...) 673 o.EncodeRawBytes(s) 674 return nil 675} 676 677func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error { 678 s := *structPointer_Bytes(base, p.field) 679 if len(s) == 0 { 680 return ErrNil 681 } 682 o.buf = append(o.buf, p.tagcode...) 683 o.EncodeRawBytes(s) 684 return nil 685} 686 687func size_slice_byte(p *Properties, base structPointer) (n int) { 688 s := *structPointer_Bytes(base, p.field) 689 if s == nil && !p.oneof { 690 return 0 691 } 692 n += len(p.tagcode) 693 n += sizeRawBytes(s) 694 return 695} 696 697func size_proto3_slice_byte(p *Properties, base structPointer) (n int) { 698 s := *structPointer_Bytes(base, p.field) 699 if len(s) == 0 && !p.oneof { 700 return 0 701 } 702 n += len(p.tagcode) 703 n += sizeRawBytes(s) 704 return 705} 706 707// Encode a slice of int32s ([]int32). 708func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { 709 s := structPointer_Word32Slice(base, p.field) 710 l := s.Len() 711 if l == 0 { 712 return ErrNil 713 } 714 for i := 0; i < l; i++ { 715 o.buf = append(o.buf, p.tagcode...) 716 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range 717 p.valEnc(o, uint64(x)) 718 } 719 return nil 720} 721 722func size_slice_int32(p *Properties, base structPointer) (n int) { 723 s := structPointer_Word32Slice(base, p.field) 724 l := s.Len() 725 if l == 0 { 726 return 0 727 } 728 for i := 0; i < l; i++ { 729 n += len(p.tagcode) 730 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range 731 n += p.valSize(uint64(x)) 732 } 733 return 734} 735 736// Encode a slice of int32s ([]int32) in packed format. 737func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { 738 s := structPointer_Word32Slice(base, p.field) 739 l := s.Len() 740 if l == 0 { 741 return ErrNil 742 } 743 // TODO: Reuse a Buffer. 744 buf := NewBuffer(nil) 745 for i := 0; i < l; i++ { 746 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range 747 p.valEnc(buf, uint64(x)) 748 } 749 750 o.buf = append(o.buf, p.tagcode...) 751 o.EncodeVarint(uint64(len(buf.buf))) 752 o.buf = append(o.buf, buf.buf...) 753 return nil 754} 755 756func size_slice_packed_int32(p *Properties, base structPointer) (n int) { 757 s := structPointer_Word32Slice(base, p.field) 758 l := s.Len() 759 if l == 0 { 760 return 0 761 } 762 var bufSize int 763 for i := 0; i < l; i++ { 764 x := int32(s.Index(i)) // permit sign extension to use full 64-bit range 765 bufSize += p.valSize(uint64(x)) 766 } 767 768 n += len(p.tagcode) 769 n += sizeVarint(uint64(bufSize)) 770 n += bufSize 771 return 772} 773 774// Encode a slice of uint32s ([]uint32). 775// Exactly the same as int32, except for no sign extension. 776func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error { 777 s := structPointer_Word32Slice(base, p.field) 778 l := s.Len() 779 if l == 0 { 780 return ErrNil 781 } 782 for i := 0; i < l; i++ { 783 o.buf = append(o.buf, p.tagcode...) 784 x := s.Index(i) 785 p.valEnc(o, uint64(x)) 786 } 787 return nil 788} 789 790func size_slice_uint32(p *Properties, base structPointer) (n int) { 791 s := structPointer_Word32Slice(base, p.field) 792 l := s.Len() 793 if l == 0 { 794 return 0 795 } 796 for i := 0; i < l; i++ { 797 n += len(p.tagcode) 798 x := s.Index(i) 799 n += p.valSize(uint64(x)) 800 } 801 return 802} 803 804// Encode a slice of uint32s ([]uint32) in packed format. 805// Exactly the same as int32, except for no sign extension. 806func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error { 807 s := structPointer_Word32Slice(base, p.field) 808 l := s.Len() 809 if l == 0 { 810 return ErrNil 811 } 812 // TODO: Reuse a Buffer. 813 buf := NewBuffer(nil) 814 for i := 0; i < l; i++ { 815 p.valEnc(buf, uint64(s.Index(i))) 816 } 817 818 o.buf = append(o.buf, p.tagcode...) 819 o.EncodeVarint(uint64(len(buf.buf))) 820 o.buf = append(o.buf, buf.buf...) 821 return nil 822} 823 824func size_slice_packed_uint32(p *Properties, base structPointer) (n int) { 825 s := structPointer_Word32Slice(base, p.field) 826 l := s.Len() 827 if l == 0 { 828 return 0 829 } 830 var bufSize int 831 for i := 0; i < l; i++ { 832 bufSize += p.valSize(uint64(s.Index(i))) 833 } 834 835 n += len(p.tagcode) 836 n += sizeVarint(uint64(bufSize)) 837 n += bufSize 838 return 839} 840 841// Encode a slice of int64s ([]int64). 842func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { 843 s := structPointer_Word64Slice(base, p.field) 844 l := s.Len() 845 if l == 0 { 846 return ErrNil 847 } 848 for i := 0; i < l; i++ { 849 o.buf = append(o.buf, p.tagcode...) 850 p.valEnc(o, s.Index(i)) 851 } 852 return nil 853} 854 855func size_slice_int64(p *Properties, base structPointer) (n int) { 856 s := structPointer_Word64Slice(base, p.field) 857 l := s.Len() 858 if l == 0 { 859 return 0 860 } 861 for i := 0; i < l; i++ { 862 n += len(p.tagcode) 863 n += p.valSize(s.Index(i)) 864 } 865 return 866} 867 868// Encode a slice of int64s ([]int64) in packed format. 869func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { 870 s := structPointer_Word64Slice(base, p.field) 871 l := s.Len() 872 if l == 0 { 873 return ErrNil 874 } 875 // TODO: Reuse a Buffer. 876 buf := NewBuffer(nil) 877 for i := 0; i < l; i++ { 878 p.valEnc(buf, s.Index(i)) 879 } 880 881 o.buf = append(o.buf, p.tagcode...) 882 o.EncodeVarint(uint64(len(buf.buf))) 883 o.buf = append(o.buf, buf.buf...) 884 return nil 885} 886 887func size_slice_packed_int64(p *Properties, base structPointer) (n int) { 888 s := structPointer_Word64Slice(base, p.field) 889 l := s.Len() 890 if l == 0 { 891 return 0 892 } 893 var bufSize int 894 for i := 0; i < l; i++ { 895 bufSize += p.valSize(s.Index(i)) 896 } 897 898 n += len(p.tagcode) 899 n += sizeVarint(uint64(bufSize)) 900 n += bufSize 901 return 902} 903 904// Encode a slice of slice of bytes ([][]byte). 905func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { 906 ss := *structPointer_BytesSlice(base, p.field) 907 l := len(ss) 908 if l == 0 { 909 return ErrNil 910 } 911 for i := 0; i < l; i++ { 912 o.buf = append(o.buf, p.tagcode...) 913 o.EncodeRawBytes(ss[i]) 914 } 915 return nil 916} 917 918func size_slice_slice_byte(p *Properties, base structPointer) (n int) { 919 ss := *structPointer_BytesSlice(base, p.field) 920 l := len(ss) 921 if l == 0 { 922 return 0 923 } 924 n += l * len(p.tagcode) 925 for i := 0; i < l; i++ { 926 n += sizeRawBytes(ss[i]) 927 } 928 return 929} 930 931// Encode a slice of strings ([]string). 932func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { 933 ss := *structPointer_StringSlice(base, p.field) 934 l := len(ss) 935 for i := 0; i < l; i++ { 936 o.buf = append(o.buf, p.tagcode...) 937 o.EncodeStringBytes(ss[i]) 938 } 939 return nil 940} 941 942func size_slice_string(p *Properties, base structPointer) (n int) { 943 ss := *structPointer_StringSlice(base, p.field) 944 l := len(ss) 945 n += l * len(p.tagcode) 946 for i := 0; i < l; i++ { 947 n += sizeStringBytes(ss[i]) 948 } 949 return 950} 951 952// Encode a slice of message structs ([]*struct). 953func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { 954 var state errorState 955 s := structPointer_StructPointerSlice(base, p.field) 956 l := s.Len() 957 958 for i := 0; i < l; i++ { 959 structp := s.Index(i) 960 if structPointer_IsNil(structp) { 961 return errRepeatedHasNil 962 } 963 964 // Can the object marshal itself? 965 if p.isMarshaler { 966 m := structPointer_Interface(structp, p.stype).(Marshaler) 967 data, err := m.Marshal() 968 if err != nil && !state.shouldContinue(err, nil) { 969 return err 970 } 971 o.buf = append(o.buf, p.tagcode...) 972 o.EncodeRawBytes(data) 973 continue 974 } 975 976 o.buf = append(o.buf, p.tagcode...) 977 err := o.enc_len_struct(p.sprop, structp, &state) 978 if err != nil && !state.shouldContinue(err, nil) { 979 if err == ErrNil { 980 return errRepeatedHasNil 981 } 982 return err 983 } 984 } 985 return state.err 986} 987 988func size_slice_struct_message(p *Properties, base structPointer) (n int) { 989 s := structPointer_StructPointerSlice(base, p.field) 990 l := s.Len() 991 n += l * len(p.tagcode) 992 for i := 0; i < l; i++ { 993 structp := s.Index(i) 994 if structPointer_IsNil(structp) { 995 return // return the size up to this point 996 } 997 998 // Can the object marshal itself? 999 if p.isMarshaler { 1000 m := structPointer_Interface(structp, p.stype).(Marshaler) 1001 data, _ := m.Marshal() 1002 n += len(p.tagcode) 1003 n += sizeRawBytes(data) 1004 continue 1005 } 1006 1007 n0 := size_struct(p.sprop, structp) 1008 n1 := sizeVarint(uint64(n0)) // size of encoded length 1009 n += n0 + n1 1010 } 1011 return 1012} 1013 1014// Encode a slice of group structs ([]*struct). 1015func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { 1016 var state errorState 1017 s := structPointer_StructPointerSlice(base, p.field) 1018 l := s.Len() 1019 1020 for i := 0; i < l; i++ { 1021 b := s.Index(i) 1022 if structPointer_IsNil(b) { 1023 return errRepeatedHasNil 1024 } 1025 1026 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) 1027 1028 err := o.enc_struct(p.sprop, b) 1029 1030 if err != nil && !state.shouldContinue(err, nil) { 1031 if err == ErrNil { 1032 return errRepeatedHasNil 1033 } 1034 return err 1035 } 1036 1037 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) 1038 } 1039 return state.err 1040} 1041 1042func size_slice_struct_group(p *Properties, base structPointer) (n int) { 1043 s := structPointer_StructPointerSlice(base, p.field) 1044 l := s.Len() 1045 1046 n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup)) 1047 n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup)) 1048 for i := 0; i < l; i++ { 1049 b := s.Index(i) 1050 if structPointer_IsNil(b) { 1051 return // return size up to this point 1052 } 1053 1054 n += size_struct(p.sprop, b) 1055 } 1056 return 1057} 1058 1059// Encode an extension map. 1060func (o *Buffer) enc_map(p *Properties, base structPointer) error { 1061 v := *structPointer_ExtMap(base, p.field) 1062 if err := encodeExtensionMap(v); err != nil { 1063 return err 1064 } 1065 // Fast-path for common cases: zero or one extensions. 1066 if len(v) <= 1 { 1067 for _, e := range v { 1068 o.buf = append(o.buf, e.enc...) 1069 } 1070 return nil 1071 } 1072 1073 // Sort keys to provide a deterministic encoding. 1074 keys := make([]int, 0, len(v)) 1075 for k := range v { 1076 keys = append(keys, int(k)) 1077 } 1078 sort.Ints(keys) 1079 1080 for _, k := range keys { 1081 o.buf = append(o.buf, v[int32(k)].enc...) 1082 } 1083 return nil 1084} 1085 1086func size_map(p *Properties, base structPointer) int { 1087 v := *structPointer_ExtMap(base, p.field) 1088 return sizeExtensionMap(v) 1089} 1090 1091// Encode a map field. 1092func (o *Buffer) enc_new_map(p *Properties, base structPointer) error { 1093 var state errorState // XXX: or do we need to plumb this through? 1094 1095 /* 1096 A map defined as 1097 map<key_type, value_type> map_field = N; 1098 is encoded in the same way as 1099 message MapFieldEntry { 1100 key_type key = 1; 1101 value_type value = 2; 1102 } 1103 repeated MapFieldEntry map_field = N; 1104 */ 1105 1106 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V 1107 if v.Len() == 0 { 1108 return nil 1109 } 1110 1111 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) 1112 1113 enc := func() error { 1114 if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil { 1115 return err 1116 } 1117 if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil { 1118 return err 1119 } 1120 return nil 1121 } 1122 1123 // Don't sort map keys. It is not required by the spec, and C++ doesn't do it. 1124 for _, key := range v.MapKeys() { 1125 val := v.MapIndex(key) 1126 1127 // The only illegal map entry values are nil message pointers. 1128 if val.Kind() == reflect.Ptr && val.IsNil() { 1129 return errors.New("proto: map has nil element") 1130 } 1131 1132 keycopy.Set(key) 1133 valcopy.Set(val) 1134 1135 o.buf = append(o.buf, p.tagcode...) 1136 if err := o.enc_len_thing(enc, &state); err != nil { 1137 return err 1138 } 1139 } 1140 return nil 1141} 1142 1143func size_new_map(p *Properties, base structPointer) int { 1144 v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V 1145 1146 keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) 1147 1148 n := 0 1149 for _, key := range v.MapKeys() { 1150 val := v.MapIndex(key) 1151 keycopy.Set(key) 1152 valcopy.Set(val) 1153 1154 // Tag codes for key and val are the responsibility of the sub-sizer. 1155 keysize := p.mkeyprop.size(p.mkeyprop, keybase) 1156 valsize := p.mvalprop.size(p.mvalprop, valbase) 1157 entry := keysize + valsize 1158 // Add on tag code and length of map entry itself. 1159 n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry 1160 } 1161 return n 1162} 1163 1164// mapEncodeScratch returns a new reflect.Value matching the map's value type, 1165// and a structPointer suitable for passing to an encoder or sizer. 1166func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) { 1167 // Prepare addressable doubly-indirect placeholders for the key and value types. 1168 // This is needed because the element-type encoders expect **T, but the map iteration produces T. 1169 1170 keycopy = reflect.New(mapType.Key()).Elem() // addressable K 1171 keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K 1172 keyptr.Set(keycopy.Addr()) // 1173 keybase = toStructPointer(keyptr.Addr()) // **K 1174 1175 // Value types are more varied and require special handling. 1176 switch mapType.Elem().Kind() { 1177 case reflect.Slice: 1178 // []byte 1179 var dummy []byte 1180 valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte 1181 valbase = toStructPointer(valcopy.Addr()) 1182 case reflect.Ptr: 1183 // message; the generated field type is map[K]*Msg (so V is *Msg), 1184 // so we only need one level of indirection. 1185 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V 1186 valbase = toStructPointer(valcopy.Addr()) 1187 default: 1188 // everything else 1189 valcopy = reflect.New(mapType.Elem()).Elem() // addressable V 1190 valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V 1191 valptr.Set(valcopy.Addr()) // 1192 valbase = toStructPointer(valptr.Addr()) // **V 1193 } 1194 return 1195} 1196 1197// Encode a struct. 1198func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error { 1199 var state errorState 1200 // Encode fields in tag order so that decoders may use optimizations 1201 // that depend on the ordering. 1202 // https://developers.google.com/protocol-buffers/docs/encoding#order 1203 for _, i := range prop.order { 1204 p := prop.Prop[i] 1205 if p.enc != nil { 1206 err := p.enc(o, p, base) 1207 if err != nil { 1208 if err == ErrNil { 1209 if p.Required && state.err == nil { 1210 state.err = &RequiredNotSetError{p.Name} 1211 } 1212 } else if err == errRepeatedHasNil { 1213 // Give more context to nil values in repeated fields. 1214 return errors.New("repeated field " + p.OrigName + " has nil element") 1215 } else if !state.shouldContinue(err, p) { 1216 return err 1217 } 1218 } 1219 } 1220 } 1221 1222 // Do oneof fields. 1223 if prop.oneofMarshaler != nil { 1224 m := structPointer_Interface(base, prop.stype).(Message) 1225 if err := prop.oneofMarshaler(m, o); err != nil { 1226 return err 1227 } 1228 } 1229 1230 // Add unrecognized fields at the end. 1231 if prop.unrecField.IsValid() { 1232 v := *structPointer_Bytes(base, prop.unrecField) 1233 if len(v) > 0 { 1234 o.buf = append(o.buf, v...) 1235 } 1236 } 1237 1238 return state.err 1239} 1240 1241func size_struct(prop *StructProperties, base structPointer) (n int) { 1242 for _, i := range prop.order { 1243 p := prop.Prop[i] 1244 if p.size != nil { 1245 n += p.size(p, base) 1246 } 1247 } 1248 1249 // Add unrecognized fields at the end. 1250 if prop.unrecField.IsValid() { 1251 v := *structPointer_Bytes(base, prop.unrecField) 1252 n += len(v) 1253 } 1254 1255 // Factor in any oneof fields. 1256 if prop.oneofSizer != nil { 1257 m := structPointer_Interface(base, prop.stype).(Message) 1258 n += prop.oneofSizer(m) 1259 } 1260 1261 return 1262} 1263 1264var zeroes [20]byte // longer than any conceivable sizeVarint 1265 1266// Encode a struct, preceded by its encoded length (as a varint). 1267func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error { 1268 return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state) 1269} 1270 1271// Encode something, preceded by its encoded length (as a varint). 1272func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error { 1273 iLen := len(o.buf) 1274 o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length 1275 iMsg := len(o.buf) 1276 err := enc() 1277 if err != nil && !state.shouldContinue(err, nil) { 1278 return err 1279 } 1280 lMsg := len(o.buf) - iMsg 1281 lLen := sizeVarint(uint64(lMsg)) 1282 switch x := lLen - (iMsg - iLen); { 1283 case x > 0: // actual length is x bytes larger than the space we reserved 1284 // Move msg x bytes right. 1285 o.buf = append(o.buf, zeroes[:x]...) 1286 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) 1287 case x < 0: // actual length is x bytes smaller than the space we reserved 1288 // Move msg x bytes left. 1289 copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) 1290 o.buf = o.buf[:len(o.buf)+x] // x is negative 1291 } 1292 // Encode the length in the reserved space. 1293 o.buf = o.buf[:iLen] 1294 o.EncodeVarint(uint64(lMsg)) 1295 o.buf = o.buf[:len(o.buf)+lMsg] 1296 return state.err 1297} 1298 1299// errorState maintains the first error that occurs and updates that error 1300// with additional context. 1301type errorState struct { 1302 err error 1303} 1304 1305// shouldContinue reports whether encoding should continue upon encountering the 1306// given error. If the error is RequiredNotSetError, shouldContinue returns true 1307// and, if this is the first appearance of that error, remembers it for future 1308// reporting. 1309// 1310// If prop is not nil, it may update any error with additional context about the 1311// field with the error. 1312func (s *errorState) shouldContinue(err error, prop *Properties) bool { 1313 // Ignore unset required fields. 1314 reqNotSet, ok := err.(*RequiredNotSetError) 1315 if !ok { 1316 return false 1317 } 1318 if s.err == nil { 1319 if prop != nil { 1320 err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field} 1321 } 1322 s.err = err 1323 } 1324 return true 1325} 1326