David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 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 | |
| 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 | // errOverflow is returned when an integer is too large to be represented. |
| 47 | var 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. |
| 51 | var 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. |
| 63 | func 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 | |
| 80 | func (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. |
| 107 | func (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 | |
| 199 | done: |
| 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. |
| 207 | func (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. |
| 230 | func (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. |
| 249 | func (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. |
| 261 | func (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. |
| 276 | func (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. |
| 306 | func (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. |
| 317 | func (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. |
| 344 | func (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. |
| 385 | type 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. |
| 396 | func 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. |
| 407 | func 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. |
| 416 | func (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. |
| 425 | func (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. |
| 439 | func (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. |
| 462 | func (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, eok := structPointer_Interface(base, st).(extensionsBytes); eok { |
| 495 | if isExtensionField(e, int32(tag)) { |
| 496 | if err = o.skip(st, tag, wire); err == nil { |
| 497 | ext := e.GetExtensions() |
| 498 | *ext = append(*ext, o.buf[oi:o.index]...) |
| 499 | } |
| 500 | continue |
| 501 | } |
| 502 | } else if e, _ := extendable(structPointer_Interface(base, st)); isExtensionField(e, int32(tag)) { |
| 503 | if err = o.skip(st, tag, wire); err == nil { |
| 504 | extmap := e.extensionsWrite() |
| 505 | ext := extmap[int32(tag)] // may be missing |
| 506 | ext.enc = append(ext.enc, o.buf[oi:o.index]...) |
| 507 | extmap[int32(tag)] = ext |
| 508 | } |
| 509 | continue |
| 510 | } |
| 511 | } |
| 512 | // Maybe it's a oneof? |
| 513 | if prop.oneofUnmarshaler != nil { |
| 514 | m := structPointer_Interface(base, st).(Message) |
| 515 | // First return value indicates whether tag is a oneof field. |
| 516 | ok, err = prop.oneofUnmarshaler(m, tag, wire, o) |
| 517 | if err == ErrInternalBadWireType { |
| 518 | // Map the error to something more descriptive. |
| 519 | // Do the formatting here to save generated code space. |
| 520 | err = fmt.Errorf("bad wiretype for oneof field in %T", m) |
| 521 | } |
| 522 | if ok { |
| 523 | continue |
| 524 | } |
| 525 | } |
| 526 | err = o.skipAndSave(st, tag, wire, base, prop.unrecField) |
| 527 | continue |
| 528 | } |
| 529 | p := prop.Prop[fieldnum] |
| 530 | |
| 531 | if p.dec == nil { |
| 532 | fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name) |
| 533 | continue |
| 534 | } |
| 535 | dec := p.dec |
| 536 | if wire != WireStartGroup && wire != p.WireType { |
| 537 | if wire == WireBytes && p.packedDec != nil { |
| 538 | // a packable field |
| 539 | dec = p.packedDec |
| 540 | } else { |
| 541 | err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType) |
| 542 | continue |
| 543 | } |
| 544 | } |
| 545 | decErr := dec(o, p, base) |
| 546 | if decErr != nil && !state.shouldContinue(decErr, p) { |
| 547 | err = decErr |
| 548 | } |
| 549 | if err == nil && p.Required { |
| 550 | // Successfully decoded a required field. |
| 551 | if tag <= 64 { |
| 552 | // use bitmap for fields 1-64 to catch field reuse. |
| 553 | var mask uint64 = 1 << uint64(tag-1) |
| 554 | if reqFields&mask == 0 { |
| 555 | // new required field |
| 556 | reqFields |= mask |
| 557 | required-- |
| 558 | } |
| 559 | } else { |
| 560 | // This is imprecise. It can be fooled by a required field |
| 561 | // with a tag > 64 that is encoded twice; that's very rare. |
| 562 | // A fully correct implementation would require allocating |
| 563 | // a data structure, which we would like to avoid. |
| 564 | required-- |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | if err == nil { |
| 569 | if is_group { |
| 570 | return io.ErrUnexpectedEOF |
| 571 | } |
| 572 | if state.err != nil { |
| 573 | return state.err |
| 574 | } |
| 575 | if required > 0 { |
| 576 | // Not enough information to determine the exact field. If we use extra |
| 577 | // CPU, we could determine the field only if the missing required field |
| 578 | // has a tag <= 64 and we check reqFields. |
| 579 | return &RequiredNotSetError{"{Unknown}"} |
| 580 | } |
| 581 | } |
| 582 | return err |
| 583 | } |
| 584 | |
| 585 | // Individual type decoders |
| 586 | // For each, |
| 587 | // u is the decoded value, |
| 588 | // v is a pointer to the field (pointer) in the struct |
| 589 | |
| 590 | // Sizes of the pools to allocate inside the Buffer. |
| 591 | // The goal is modest amortization and allocation |
| 592 | // on at least 16-byte boundaries. |
| 593 | const ( |
| 594 | boolPoolSize = 16 |
| 595 | uint32PoolSize = 8 |
| 596 | uint64PoolSize = 4 |
| 597 | ) |
| 598 | |
| 599 | // Decode a bool. |
| 600 | func (o *Buffer) dec_bool(p *Properties, base structPointer) error { |
| 601 | u, err := p.valDec(o) |
| 602 | if err != nil { |
| 603 | return err |
| 604 | } |
| 605 | if len(o.bools) == 0 { |
| 606 | o.bools = make([]bool, boolPoolSize) |
| 607 | } |
| 608 | o.bools[0] = u != 0 |
| 609 | *structPointer_Bool(base, p.field) = &o.bools[0] |
| 610 | o.bools = o.bools[1:] |
| 611 | return nil |
| 612 | } |
| 613 | |
| 614 | func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error { |
| 615 | u, err := p.valDec(o) |
| 616 | if err != nil { |
| 617 | return err |
| 618 | } |
| 619 | *structPointer_BoolVal(base, p.field) = u != 0 |
| 620 | return nil |
| 621 | } |
| 622 | |
| 623 | // Decode an int32. |
| 624 | func (o *Buffer) dec_int32(p *Properties, base structPointer) error { |
| 625 | u, err := p.valDec(o) |
| 626 | if err != nil { |
| 627 | return err |
| 628 | } |
| 629 | word32_Set(structPointer_Word32(base, p.field), o, uint32(u)) |
| 630 | return nil |
| 631 | } |
| 632 | |
| 633 | func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error { |
| 634 | u, err := p.valDec(o) |
| 635 | if err != nil { |
| 636 | return err |
| 637 | } |
| 638 | word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u)) |
| 639 | return nil |
| 640 | } |
| 641 | |
| 642 | // Decode an int64. |
| 643 | func (o *Buffer) dec_int64(p *Properties, base structPointer) error { |
| 644 | u, err := p.valDec(o) |
| 645 | if err != nil { |
| 646 | return err |
| 647 | } |
| 648 | word64_Set(structPointer_Word64(base, p.field), o, u) |
| 649 | return nil |
| 650 | } |
| 651 | |
| 652 | func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error { |
| 653 | u, err := p.valDec(o) |
| 654 | if err != nil { |
| 655 | return err |
| 656 | } |
| 657 | word64Val_Set(structPointer_Word64Val(base, p.field), o, u) |
| 658 | return nil |
| 659 | } |
| 660 | |
| 661 | // Decode a string. |
| 662 | func (o *Buffer) dec_string(p *Properties, base structPointer) error { |
| 663 | s, err := o.DecodeStringBytes() |
| 664 | if err != nil { |
| 665 | return err |
| 666 | } |
| 667 | *structPointer_String(base, p.field) = &s |
| 668 | return nil |
| 669 | } |
| 670 | |
| 671 | func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error { |
| 672 | s, err := o.DecodeStringBytes() |
| 673 | if err != nil { |
| 674 | return err |
| 675 | } |
| 676 | *structPointer_StringVal(base, p.field) = s |
| 677 | return nil |
| 678 | } |
| 679 | |
| 680 | // Decode a slice of bytes ([]byte). |
| 681 | func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error { |
| 682 | b, err := o.DecodeRawBytes(true) |
| 683 | if err != nil { |
| 684 | return err |
| 685 | } |
| 686 | *structPointer_Bytes(base, p.field) = b |
| 687 | return nil |
| 688 | } |
| 689 | |
| 690 | // Decode a slice of bools ([]bool). |
| 691 | func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error { |
| 692 | u, err := p.valDec(o) |
| 693 | if err != nil { |
| 694 | return err |
| 695 | } |
| 696 | v := structPointer_BoolSlice(base, p.field) |
| 697 | *v = append(*v, u != 0) |
| 698 | return nil |
| 699 | } |
| 700 | |
| 701 | // Decode a slice of bools ([]bool) in packed format. |
| 702 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error { |
| 703 | v := structPointer_BoolSlice(base, p.field) |
| 704 | |
| 705 | nn, err := o.DecodeVarint() |
| 706 | if err != nil { |
| 707 | return err |
| 708 | } |
| 709 | nb := int(nn) // number of bytes of encoded bools |
| 710 | fin := o.index + nb |
| 711 | if fin < o.index { |
| 712 | return errOverflow |
| 713 | } |
| 714 | |
| 715 | y := *v |
| 716 | for o.index < fin { |
| 717 | u, err := p.valDec(o) |
| 718 | if err != nil { |
| 719 | return err |
| 720 | } |
| 721 | y = append(y, u != 0) |
| 722 | } |
| 723 | |
| 724 | *v = y |
| 725 | return nil |
| 726 | } |
| 727 | |
| 728 | // Decode a slice of int32s ([]int32). |
| 729 | func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error { |
| 730 | u, err := p.valDec(o) |
| 731 | if err != nil { |
| 732 | return err |
| 733 | } |
| 734 | structPointer_Word32Slice(base, p.field).Append(uint32(u)) |
| 735 | return nil |
| 736 | } |
| 737 | |
| 738 | // Decode a slice of int32s ([]int32) in packed format. |
| 739 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error { |
| 740 | v := structPointer_Word32Slice(base, p.field) |
| 741 | |
| 742 | nn, err := o.DecodeVarint() |
| 743 | if err != nil { |
| 744 | return err |
| 745 | } |
| 746 | nb := int(nn) // number of bytes of encoded int32s |
| 747 | |
| 748 | fin := o.index + nb |
| 749 | if fin < o.index { |
| 750 | return errOverflow |
| 751 | } |
| 752 | for o.index < fin { |
| 753 | u, err := p.valDec(o) |
| 754 | if err != nil { |
| 755 | return err |
| 756 | } |
| 757 | v.Append(uint32(u)) |
| 758 | } |
| 759 | return nil |
| 760 | } |
| 761 | |
| 762 | // Decode a slice of int64s ([]int64). |
| 763 | func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error { |
| 764 | u, err := p.valDec(o) |
| 765 | if err != nil { |
| 766 | return err |
| 767 | } |
| 768 | |
| 769 | structPointer_Word64Slice(base, p.field).Append(u) |
| 770 | return nil |
| 771 | } |
| 772 | |
| 773 | // Decode a slice of int64s ([]int64) in packed format. |
| 774 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error { |
| 775 | v := structPointer_Word64Slice(base, p.field) |
| 776 | |
| 777 | nn, err := o.DecodeVarint() |
| 778 | if err != nil { |
| 779 | return err |
| 780 | } |
| 781 | nb := int(nn) // number of bytes of encoded int64s |
| 782 | |
| 783 | fin := o.index + nb |
| 784 | if fin < o.index { |
| 785 | return errOverflow |
| 786 | } |
| 787 | for o.index < fin { |
| 788 | u, err := p.valDec(o) |
| 789 | if err != nil { |
| 790 | return err |
| 791 | } |
| 792 | v.Append(u) |
| 793 | } |
| 794 | return nil |
| 795 | } |
| 796 | |
| 797 | // Decode a slice of strings ([]string). |
| 798 | func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error { |
| 799 | s, err := o.DecodeStringBytes() |
| 800 | if err != nil { |
| 801 | return err |
| 802 | } |
| 803 | v := structPointer_StringSlice(base, p.field) |
| 804 | *v = append(*v, s) |
| 805 | return nil |
| 806 | } |
| 807 | |
| 808 | // Decode a slice of slice of bytes ([][]byte). |
| 809 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error { |
| 810 | b, err := o.DecodeRawBytes(true) |
| 811 | if err != nil { |
| 812 | return err |
| 813 | } |
| 814 | v := structPointer_BytesSlice(base, p.field) |
| 815 | *v = append(*v, b) |
| 816 | return nil |
| 817 | } |
| 818 | |
| 819 | // Decode a map field. |
| 820 | func (o *Buffer) dec_new_map(p *Properties, base structPointer) error { |
| 821 | raw, err := o.DecodeRawBytes(false) |
| 822 | if err != nil { |
| 823 | return err |
| 824 | } |
| 825 | oi := o.index // index at the end of this map entry |
| 826 | o.index -= len(raw) // move buffer back to start of map entry |
| 827 | |
| 828 | mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V |
| 829 | if mptr.Elem().IsNil() { |
| 830 | mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem())) |
| 831 | } |
| 832 | v := mptr.Elem() // map[K]V |
| 833 | |
| 834 | // Prepare addressable doubly-indirect placeholders for the key and value types. |
| 835 | // See enc_new_map for why. |
| 836 | keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K |
| 837 | keybase := toStructPointer(keyptr.Addr()) // **K |
| 838 | |
| 839 | var valbase structPointer |
| 840 | var valptr reflect.Value |
| 841 | switch p.mtype.Elem().Kind() { |
| 842 | case reflect.Slice: |
| 843 | // []byte |
| 844 | var dummy []byte |
| 845 | valptr = reflect.ValueOf(&dummy) // *[]byte |
| 846 | valbase = toStructPointer(valptr) // *[]byte |
| 847 | case reflect.Ptr: |
| 848 | // message; valptr is **Msg; need to allocate the intermediate pointer |
| 849 | valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V |
| 850 | valptr.Set(reflect.New(valptr.Type().Elem())) |
| 851 | valbase = toStructPointer(valptr) |
| 852 | default: |
| 853 | // everything else |
| 854 | valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V |
| 855 | valbase = toStructPointer(valptr.Addr()) // **V |
| 856 | } |
| 857 | |
| 858 | // Decode. |
| 859 | // This parses a restricted wire format, namely the encoding of a message |
| 860 | // with two fields. See enc_new_map for the format. |
| 861 | for o.index < oi { |
| 862 | // tagcode for key and value properties are always a single byte |
| 863 | // because they have tags 1 and 2. |
| 864 | tagcode := o.buf[o.index] |
| 865 | o.index++ |
| 866 | switch tagcode { |
| 867 | case p.mkeyprop.tagcode[0]: |
| 868 | if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil { |
| 869 | return err |
| 870 | } |
| 871 | case p.mvalprop.tagcode[0]: |
| 872 | if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil { |
| 873 | return err |
| 874 | } |
| 875 | default: |
| 876 | // TODO: Should we silently skip this instead? |
| 877 | return fmt.Errorf("proto: bad map data tag %d", raw[0]) |
| 878 | } |
| 879 | } |
| 880 | keyelem, valelem := keyptr.Elem(), valptr.Elem() |
| 881 | if !keyelem.IsValid() { |
| 882 | keyelem = reflect.Zero(p.mtype.Key()) |
| 883 | } |
| 884 | if !valelem.IsValid() { |
| 885 | valelem = reflect.Zero(p.mtype.Elem()) |
| 886 | } |
| 887 | |
| 888 | v.SetMapIndex(keyelem, valelem) |
| 889 | return nil |
| 890 | } |
| 891 | |
| 892 | // Decode a group. |
| 893 | func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error { |
| 894 | bas := structPointer_GetStructPointer(base, p.field) |
| 895 | if structPointer_IsNil(bas) { |
| 896 | // allocate new nested message |
| 897 | bas = toStructPointer(reflect.New(p.stype)) |
| 898 | structPointer_SetStructPointer(base, p.field, bas) |
| 899 | } |
| 900 | return o.unmarshalType(p.stype, p.sprop, true, bas) |
| 901 | } |
| 902 | |
| 903 | // Decode an embedded message. |
| 904 | func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) { |
| 905 | raw, e := o.DecodeRawBytes(false) |
| 906 | if e != nil { |
| 907 | return e |
| 908 | } |
| 909 | |
| 910 | bas := structPointer_GetStructPointer(base, p.field) |
| 911 | if structPointer_IsNil(bas) { |
| 912 | // allocate new nested message |
| 913 | bas = toStructPointer(reflect.New(p.stype)) |
| 914 | structPointer_SetStructPointer(base, p.field, bas) |
| 915 | } |
| 916 | |
| 917 | // If the object can unmarshal itself, let it. |
| 918 | if p.isUnmarshaler { |
| 919 | iv := structPointer_Interface(bas, p.stype) |
| 920 | return iv.(Unmarshaler).Unmarshal(raw) |
| 921 | } |
| 922 | |
| 923 | obuf := o.buf |
| 924 | oi := o.index |
| 925 | o.buf = raw |
| 926 | o.index = 0 |
| 927 | |
| 928 | err = o.unmarshalType(p.stype, p.sprop, false, bas) |
| 929 | o.buf = obuf |
| 930 | o.index = oi |
| 931 | |
| 932 | return err |
| 933 | } |
| 934 | |
| 935 | // Decode a slice of embedded messages. |
| 936 | func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error { |
| 937 | return o.dec_slice_struct(p, false, base) |
| 938 | } |
| 939 | |
| 940 | // Decode a slice of embedded groups. |
| 941 | func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error { |
| 942 | return o.dec_slice_struct(p, true, base) |
| 943 | } |
| 944 | |
| 945 | // Decode a slice of structs ([]*struct). |
| 946 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error { |
| 947 | v := reflect.New(p.stype) |
| 948 | bas := toStructPointer(v) |
| 949 | structPointer_StructPointerSlice(base, p.field).Append(bas) |
| 950 | |
| 951 | if is_group { |
| 952 | err := o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| 953 | return err |
| 954 | } |
| 955 | |
| 956 | raw, err := o.DecodeRawBytes(false) |
| 957 | if err != nil { |
| 958 | return err |
| 959 | } |
| 960 | |
| 961 | // If the object can unmarshal itself, let it. |
| 962 | if p.isUnmarshaler { |
| 963 | iv := v.Interface() |
| 964 | return iv.(Unmarshaler).Unmarshal(raw) |
| 965 | } |
| 966 | |
| 967 | obuf := o.buf |
| 968 | oi := o.index |
| 969 | o.buf = raw |
| 970 | o.index = 0 |
| 971 | |
| 972 | err = o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| 973 | |
| 974 | o.buf = obuf |
| 975 | o.index = oi |
| 976 | |
| 977 | return err |
| 978 | } |