blob: 07288a250b47add2f1fa939d9951a307918360f7 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2010 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
Rob Pikeaaa3a622010-03-20 22:32:34 -07005//
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 (
Rob Pikea17fdd92011-11-02 12:43:05 -070039 "errors"
Rob Pikeaaa3a622010-03-20 22:32:34 -070040 "fmt"
41 "io"
42 "os"
43 "reflect"
Rob Pikeaaa3a622010-03-20 22:32:34 -070044)
45
Adam Langley28c83cb2013-07-13 14:54:54 +100046// errOverflow is returned when an integer is too large to be represented.
47var errOverflow = errors.New("proto: integer overflow")
48
David Symonds59b73b32015-08-24 13:22:02 +100049// 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
Rob Pikeaaa3a622010-03-20 22:32:34 -070053// 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 // x, n already 0
Adam Langley28c83cb2013-07-13 14:54:54 +100065 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070066 if n >= len(buf) {
67 return 0, 0
68 }
69 b := uint64(buf[n])
70 n++
71 x |= (b & 0x7F) << shift
72 if (b & 0x80) == 0 {
Adam Langley28c83cb2013-07-13 14:54:54 +100073 return x, n
Rob Pikeaaa3a622010-03-20 22:32:34 -070074 }
75 }
Adam Langley28c83cb2013-07-13 14:54:54 +100076
77 // The number is too large to represent in a 64-bit value.
78 return 0, 0
Rob Pikeaaa3a622010-03-20 22:32:34 -070079}
80
81// DecodeVarint reads a varint-encoded integer from the Buffer.
82// This is the format for the
83// int32, int64, uint32, uint64, bool, and enum
84// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070085func (p *Buffer) DecodeVarint() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070086 // x, err already 0
87
88 i := p.index
89 l := len(p.buf)
90
Adam Langley28c83cb2013-07-13 14:54:54 +100091 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070092 if i >= l {
93 err = io.ErrUnexpectedEOF
94 return
95 }
96 b := p.buf[i]
97 i++
98 x |= (uint64(b) & 0x7F) << shift
99 if b < 0x80 {
Adam Langley28c83cb2013-07-13 14:54:54 +1000100 p.index = i
101 return
Rob Pikeaaa3a622010-03-20 22:32:34 -0700102 }
103 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000104
105 // The number is too large to represent in a 64-bit value.
106 err = errOverflow
Rob Pikeaaa3a622010-03-20 22:32:34 -0700107 return
108}
109
110// DecodeFixed64 reads a 64-bit integer from the Buffer.
111// This is the format for the
112// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700113func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700114 // x, err already 0
115 i := p.index + 8
Adam Langley28c83cb2013-07-13 14:54:54 +1000116 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700117 err = io.ErrUnexpectedEOF
118 return
119 }
120 p.index = i
121
122 x = uint64(p.buf[i-8])
123 x |= uint64(p.buf[i-7]) << 8
124 x |= uint64(p.buf[i-6]) << 16
125 x |= uint64(p.buf[i-5]) << 24
126 x |= uint64(p.buf[i-4]) << 32
127 x |= uint64(p.buf[i-3]) << 40
128 x |= uint64(p.buf[i-2]) << 48
129 x |= uint64(p.buf[i-1]) << 56
130 return
131}
132
133// DecodeFixed32 reads a 32-bit integer from the Buffer.
134// This is the format for the
135// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700136func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700137 // x, err already 0
138 i := p.index + 4
Adam Langley28c83cb2013-07-13 14:54:54 +1000139 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700140 err = io.ErrUnexpectedEOF
141 return
142 }
143 p.index = i
144
145 x = uint64(p.buf[i-4])
146 x |= uint64(p.buf[i-3]) << 8
147 x |= uint64(p.buf[i-2]) << 16
148 x |= uint64(p.buf[i-1]) << 24
149 return
150}
151
152// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
153// from the Buffer.
154// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700155func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700156 x, err = p.DecodeVarint()
157 if err != nil {
158 return
159 }
160 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
161 return
162}
163
164// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
165// from the Buffer.
166// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700167func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700168 x, err = p.DecodeVarint()
169 if err != nil {
170 return
171 }
172 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
173 return
174}
175
176// These are not ValueDecoders: they produce an array of bytes or a string.
177// bytes, embedded messages
178
179// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
180// This is the format used for the bytes protocol buffer
181// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700182func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700183 n, err := p.DecodeVarint()
184 if err != nil {
David Symonds3ea3e052014-12-22 16:15:28 +1100185 return nil, err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700186 }
187
188 nb := int(n)
David Symonds22ac1502012-01-18 12:37:12 +1100189 if nb < 0 {
190 return nil, fmt.Errorf("proto: bad byte length %d", nb)
191 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000192 end := p.index + nb
193 if end < p.index || end > len(p.buf) {
David Symonds22ac1502012-01-18 12:37:12 +1100194 return nil, io.ErrUnexpectedEOF
Rob Pikeaaa3a622010-03-20 22:32:34 -0700195 }
196
197 if !alloc {
198 // todo: check if can get more uses of alloc=false
Adam Langley28c83cb2013-07-13 14:54:54 +1000199 buf = p.buf[p.index:end]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700200 p.index += nb
201 return
202 }
203
204 buf = make([]byte, nb)
205 copy(buf, p.buf[p.index:])
206 p.index += nb
207 return
208}
209
210// DecodeStringBytes reads an encoded string from the Buffer.
211// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700212func (p *Buffer) DecodeStringBytes() (s string, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700213 buf, err := p.DecodeRawBytes(false)
214 if err != nil {
215 return
216 }
217 return string(buf), nil
218}
219
220// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
221// If the protocol buffer has extensions, and the field matches, add it as an extension.
222// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000223func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700224 oi := o.index
225
226 err := o.skip(t, tag, wire)
227 if err != nil {
228 return err
229 }
230
Russ Coxd4ce3f12012-09-12 10:36:26 +1000231 if !unrecField.IsValid() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700232 return nil
233 }
234
Russ Coxd4ce3f12012-09-12 10:36:26 +1000235 ptr := structPointer_Bytes(base, unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700236
Rob Pikeaaa3a622010-03-20 22:32:34 -0700237 // Add the skipped field to struct field
238 obuf := o.buf
239
240 o.buf = *ptr
241 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800242 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700243
244 o.buf = obuf
245
246 return nil
247}
248
249// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700250func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700251
252 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700253 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700254
255 switch wire {
256 case WireVarint:
257 _, err = o.DecodeVarint()
258 case WireFixed64:
259 _, err = o.DecodeFixed64()
260 case WireBytes:
261 _, err = o.DecodeRawBytes(false)
262 case WireFixed32:
263 _, err = o.DecodeFixed32()
264 case WireStartGroup:
265 for {
266 u, err = o.DecodeVarint()
267 if err != nil {
268 break
269 }
270 fwire := int(u & 0x7)
271 if fwire == WireEndGroup {
272 break
273 }
274 ftag := int(u >> 3)
275 err = o.skip(t, ftag, fwire)
276 if err != nil {
277 break
278 }
279 }
280 default:
David Symonds22ac1502012-01-18 12:37:12 +1100281 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700282 }
283 return err
284}
285
David Symonds7d3c6802012-11-08 08:20:18 +1100286// Unmarshaler is the interface representing objects that can
287// unmarshal themselves. The method should reset the receiver before
288// decoding starts. The argument points to data that may be
289// overwritten, so implementations should not keep references to the
290// buffer.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700291type Unmarshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700292 Unmarshal([]byte) error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700293}
294
295// Unmarshal parses the protocol buffer representation in buf and places the
296// decoded result in pb. If the struct underlying pb does not match
297// the data in buf, the results can be unpredictable.
David Symonds19285602012-07-02 16:04:28 -0700298//
David Symonds525838c2012-07-20 15:42:49 +1000299// Unmarshal resets pb before starting to unmarshal, so any
David Symondsd4b52d02013-01-15 14:30:26 +1100300// existing data in pb is always removed. Use UnmarshalMerge
David Symonds525838c2012-07-20 15:42:49 +1000301// to preserve and append to existing data.
David Symonds9f60f432012-06-14 09:45:25 +1000302func Unmarshal(buf []byte, pb Message) error {
David Symonds525838c2012-07-20 15:42:49 +1000303 pb.Reset()
David Symondsd4b52d02013-01-15 14:30:26 +1100304 return UnmarshalMerge(buf, pb)
David Symonds19285602012-07-02 16:04:28 -0700305}
306
David Symondsd4b52d02013-01-15 14:30:26 +1100307// UnmarshalMerge parses the protocol buffer representation in buf and
David Symonds19285602012-07-02 16:04:28 -0700308// writes the decoded result to pb. If the struct underlying pb does not match
309// the data in buf, the results can be unpredictable.
310//
David Symondsd4b52d02013-01-15 14:30:26 +1100311// UnmarshalMerge merges into existing data in pb.
David Symonds525838c2012-07-20 15:42:49 +1000312// Most code should use Unmarshal instead.
David Symondsd4b52d02013-01-15 14:30:26 +1100313func UnmarshalMerge(buf []byte, pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700314 // If the object can unmarshal itself, let it.
315 if u, ok := pb.(Unmarshaler); ok {
316 return u.Unmarshal(buf)
317 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700318 return NewBuffer(buf).Unmarshal(pb)
319}
320
David Symonds59b73b32015-08-24 13:22:02 +1000321// DecodeMessage reads a count-delimited message from the Buffer.
322func (p *Buffer) DecodeMessage(pb Message) error {
323 enc, err := p.DecodeRawBytes(false)
324 if err != nil {
325 return err
326 }
327 return NewBuffer(enc).Unmarshal(pb)
328}
329
330// DecodeGroup reads a tag-delimited group from the Buffer.
331func (p *Buffer) DecodeGroup(pb Message) error {
332 typ, base, err := getbase(pb)
333 if err != nil {
334 return err
335 }
336 return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), true, base)
337}
338
Rob Pikeaaa3a622010-03-20 22:32:34 -0700339// Unmarshal parses the protocol buffer representation in the
340// Buffer and places the decoded result in pb. If the struct
341// underlying pb does not match the data in the buffer, the results can be
342// unpredictable.
David Symonds9f60f432012-06-14 09:45:25 +1000343func (p *Buffer) Unmarshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700344 // If the object can unmarshal itself, let it.
345 if u, ok := pb.(Unmarshaler); ok {
346 err := u.Unmarshal(p.buf[p.index:])
347 p.index = len(p.buf)
348 return err
349 }
350
Rob Pikeaaa3a622010-03-20 22:32:34 -0700351 typ, base, err := getbase(pb)
352 if err != nil {
353 return err
354 }
355
David Symonds6a6f82c2012-08-22 09:18:54 +1000356 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700357
David Symonds9f60f432012-06-14 09:45:25 +1000358 if collectStats {
359 stats.Decode++
360 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700361
362 return err
363}
364
365// unmarshalType does the work of unmarshaling a structure.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000366func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000367 var state errorState
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700368 required, reqFields := prop.reqCount, uint64(0)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700369
Rob Pikea17fdd92011-11-02 12:43:05 -0700370 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700371 for err == nil && o.index < len(o.buf) {
372 oi := o.index
373 var u uint64
374 u, err = o.DecodeVarint()
375 if err != nil {
376 break
377 }
378 wire := int(u & 0x7)
379 if wire == WireEndGroup {
380 if is_group {
381 return nil // input is satisfied
382 }
David Symondsbaeae8b2014-06-23 09:41:27 +1000383 return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700384 }
385 tag := int(u >> 3)
David Symonds6e50db52012-02-11 15:56:22 +1100386 if tag <= 0 {
David Symonds50386d22014-10-28 11:00:50 +1100387 return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
David Symonds6e50db52012-02-11 15:56:22 +1100388 }
David Symonds2bba1b22012-09-26 14:53:08 +1000389 fieldnum, ok := prop.decoderTags.get(tag)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700390 if !ok {
391 // Maybe it's an extension?
Russ Coxd4ce3f12012-09-12 10:36:26 +1000392 if prop.extendable {
matloob@google.come51d0022016-05-23 09:09:04 -0400393 if e, _ := extendable(structPointer_Interface(base, st)); isExtensionField(e, int32(tag)) {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000394 if err = o.skip(st, tag, wire); err == nil {
matloob@google.come51d0022016-05-23 09:09:04 -0400395 extmap := e.extensionsWrite()
396 ext := extmap[int32(tag)] // may be missing
Russ Coxd4ce3f12012-09-12 10:36:26 +1000397 ext.enc = append(ext.enc, o.buf[oi:o.index]...)
matloob@google.come51d0022016-05-23 09:09:04 -0400398 extmap[int32(tag)] = ext
Russ Coxd4ce3f12012-09-12 10:36:26 +1000399 }
400 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700401 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700402 }
David Symonds59b73b32015-08-24 13:22:02 +1000403 // Maybe it's a oneof?
404 if prop.oneofUnmarshaler != nil {
405 m := structPointer_Interface(base, st).(Message)
406 // First return value indicates whether tag is a oneof field.
407 ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
408 if err == ErrInternalBadWireType {
409 // Map the error to something more descriptive.
410 // Do the formatting here to save generated code space.
411 err = fmt.Errorf("bad wiretype for oneof field in %T", m)
412 }
413 if ok {
414 continue
415 }
416 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000417 err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700418 continue
419 }
420 p := prop.Prop[fieldnum]
421
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700422 if p.dec == nil {
David Symonds6a6f82c2012-08-22 09:18:54 +1000423 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700424 continue
425 }
David Symonds5b7775e2010-12-01 10:09:04 +1100426 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700427 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100428 if wire == WireBytes && p.packedDec != nil {
429 // a packable field
430 dec = p.packedDec
431 } else {
David Symondsbaeae8b2014-06-23 09:41:27 +1000432 err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
David Symonds5b7775e2010-12-01 10:09:04 +1100433 continue
434 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700435 }
David Symonds4646c372013-09-09 13:18:58 +1000436 decErr := dec(o, p, base)
437 if decErr != nil && !state.shouldContinue(decErr, p) {
438 err = decErr
439 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700440 if err == nil && p.Required {
441 // Successfully decoded a required field.
442 if tag <= 64 {
443 // use bitmap for fields 1-64 to catch field reuse.
444 var mask uint64 = 1 << uint64(tag-1)
445 if reqFields&mask == 0 {
446 // new required field
447 reqFields |= mask
448 required--
449 }
450 } else {
451 // This is imprecise. It can be fooled by a required field
452 // with a tag > 64 that is encoded twice; that's very rare.
453 // A fully correct implementation would require allocating
454 // a data structure, which we would like to avoid.
455 required--
456 }
457 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700458 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700459 if err == nil {
460 if is_group {
461 return io.ErrUnexpectedEOF
462 }
David Symonds4646c372013-09-09 13:18:58 +1000463 if state.err != nil {
464 return state.err
465 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700466 if required > 0 {
David Symonds4646c372013-09-09 13:18:58 +1000467 // Not enough information to determine the exact field. If we use extra
468 // CPU, we could determine the field only if the missing required field
469 // has a tag <= 64 and we check reqFields.
David Symondse583a5f2013-09-27 10:02:37 +1000470 return &RequiredNotSetError{"{Unknown}"}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700471 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700472 }
473 return err
474}
475
Rob Pikeaaa3a622010-03-20 22:32:34 -0700476// Individual type decoders
477// For each,
478// u is the decoded value,
479// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700480
481// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700482// The goal is modest amortization and allocation
483// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700484const (
Russ Coxd4ce3f12012-09-12 10:36:26 +1000485 boolPoolSize = 16
486 uint32PoolSize = 8
487 uint64PoolSize = 4
Rob Pike76f6ee52011-10-20 12:58:28 -0700488)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700489
490// Decode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000491func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700492 u, err := p.valDec(o)
493 if err != nil {
494 return err
495 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700496 if len(o.bools) == 0 {
497 o.bools = make([]bool, boolPoolSize)
498 }
499 o.bools[0] = u != 0
Russ Coxd4ce3f12012-09-12 10:36:26 +1000500 *structPointer_Bool(base, p.field) = &o.bools[0]
Rob Pike76f6ee52011-10-20 12:58:28 -0700501 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700502 return nil
503}
504
David Symondsabd3b412014-11-28 11:43:44 +1100505func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
506 u, err := p.valDec(o)
507 if err != nil {
508 return err
509 }
510 *structPointer_BoolVal(base, p.field) = u != 0
511 return nil
512}
513
Rob Pikeaaa3a622010-03-20 22:32:34 -0700514// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000515func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700516 u, err := p.valDec(o)
517 if err != nil {
518 return err
519 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000520 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700521 return nil
522}
523
David Symondsabd3b412014-11-28 11:43:44 +1100524func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
525 u, err := p.valDec(o)
526 if err != nil {
527 return err
528 }
529 word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
530 return nil
531}
532
Rob Pikeaaa3a622010-03-20 22:32:34 -0700533// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000534func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700535 u, err := p.valDec(o)
536 if err != nil {
537 return err
538 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000539 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700540 return nil
541}
542
David Symondsabd3b412014-11-28 11:43:44 +1100543func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
544 u, err := p.valDec(o)
545 if err != nil {
546 return err
547 }
548 word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
549 return nil
550}
551
Rob Pikeaaa3a622010-03-20 22:32:34 -0700552// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000553func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700554 s, err := o.DecodeStringBytes()
555 if err != nil {
556 return err
557 }
David Symondsa11b6342015-01-28 17:07:47 +1100558 *structPointer_String(base, p.field) = &s
Rob Pikeaaa3a622010-03-20 22:32:34 -0700559 return nil
560}
561
David Symondsabd3b412014-11-28 11:43:44 +1100562func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
563 s, err := o.DecodeStringBytes()
564 if err != nil {
565 return err
566 }
567 *structPointer_StringVal(base, p.field) = s
568 return nil
569}
570
Rob Pikeaaa3a622010-03-20 22:32:34 -0700571// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000572func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700573 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700574 if err != nil {
575 return err
576 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000577 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700578 return nil
579}
580
581// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000582func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700583 u, err := p.valDec(o)
584 if err != nil {
585 return err
586 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000587 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700588 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100589 return nil
590}
591
592// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000593func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
594 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100595
596 nn, err := o.DecodeVarint()
597 if err != nil {
598 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700599 }
David Symonds5b7775e2010-12-01 10:09:04 +1100600 nb := int(nn) // number of bytes of encoded bools
David Symonds0c959e82015-09-28 15:17:00 +1000601 fin := o.index + nb
602 if fin < o.index {
603 return errOverflow
604 }
David Symonds5b7775e2010-12-01 10:09:04 +1100605
Rob Pike76f6ee52011-10-20 12:58:28 -0700606 y := *v
David Symonds0c959e82015-09-28 15:17:00 +1000607 for o.index < fin {
David Symonds5b7775e2010-12-01 10:09:04 +1100608 u, err := p.valDec(o)
609 if err != nil {
610 return err
611 }
612 y = append(y, u != 0)
613 }
614
Rob Pike76f6ee52011-10-20 12:58:28 -0700615 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700616 return nil
617}
618
619// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000620func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700621 u, err := p.valDec(o)
622 if err != nil {
623 return err
624 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000625 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100626 return nil
627}
628
629// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000630func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
631 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100632
633 nn, err := o.DecodeVarint()
634 if err != nil {
635 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700636 }
David Symonds5b7775e2010-12-01 10:09:04 +1100637 nb := int(nn) // number of bytes of encoded int32s
638
David Symonds5b7775e2010-12-01 10:09:04 +1100639 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000640 if fin < o.index {
641 return errOverflow
642 }
David Symonds5b7775e2010-12-01 10:09:04 +1100643 for o.index < fin {
644 u, err := p.valDec(o)
645 if err != nil {
646 return err
647 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000648 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100649 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700650 return nil
651}
652
653// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000654func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700655 u, err := p.valDec(o)
656 if err != nil {
657 return err
658 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700659
Russ Coxd4ce3f12012-09-12 10:36:26 +1000660 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100661 return nil
662}
663
664// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000665func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
666 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100667
668 nn, err := o.DecodeVarint()
669 if err != nil {
670 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700671 }
David Symonds5b7775e2010-12-01 10:09:04 +1100672 nb := int(nn) // number of bytes of encoded int64s
673
David Symonds5b7775e2010-12-01 10:09:04 +1100674 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000675 if fin < o.index {
676 return errOverflow
677 }
David Symonds5b7775e2010-12-01 10:09:04 +1100678 for o.index < fin {
679 u, err := p.valDec(o)
680 if err != nil {
681 return err
682 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000683 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100684 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700685 return nil
686}
687
688// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000689func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700690 s, err := o.DecodeStringBytes()
691 if err != nil {
692 return err
693 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000694 v := structPointer_StringSlice(base, p.field)
695 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700696 return nil
697}
698
699// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000700func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700701 b, err := o.DecodeRawBytes(true)
702 if err != nil {
703 return err
704 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000705 v := structPointer_BytesSlice(base, p.field)
706 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700707 return nil
708}
709
David Symonds3ea3e052014-12-22 16:15:28 +1100710// Decode a map field.
711func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
712 raw, err := o.DecodeRawBytes(false)
713 if err != nil {
714 return err
715 }
716 oi := o.index // index at the end of this map entry
717 o.index -= len(raw) // move buffer back to start of map entry
718
David Symonds0f7a9ca2015-07-20 16:00:00 +1000719 mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +1100720 if mptr.Elem().IsNil() {
721 mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
722 }
723 v := mptr.Elem() // map[K]V
724
725 // Prepare addressable doubly-indirect placeholders for the key and value types.
726 // See enc_new_map for why.
727 keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
728 keybase := toStructPointer(keyptr.Addr()) // **K
729
730 var valbase structPointer
731 var valptr reflect.Value
732 switch p.mtype.Elem().Kind() {
733 case reflect.Slice:
734 // []byte
735 var dummy []byte
736 valptr = reflect.ValueOf(&dummy) // *[]byte
737 valbase = toStructPointer(valptr) // *[]byte
738 case reflect.Ptr:
739 // message; valptr is **Msg; need to allocate the intermediate pointer
740 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
741 valptr.Set(reflect.New(valptr.Type().Elem()))
742 valbase = toStructPointer(valptr)
743 default:
744 // everything else
745 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
746 valbase = toStructPointer(valptr.Addr()) // **V
747 }
748
749 // Decode.
750 // This parses a restricted wire format, namely the encoding of a message
751 // with two fields. See enc_new_map for the format.
752 for o.index < oi {
753 // tagcode for key and value properties are always a single byte
754 // because they have tags 1 and 2.
755 tagcode := o.buf[o.index]
756 o.index++
757 switch tagcode {
758 case p.mkeyprop.tagcode[0]:
759 if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
760 return err
761 }
762 case p.mvalprop.tagcode[0]:
763 if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
764 return err
765 }
766 default:
767 // TODO: Should we silently skip this instead?
768 return fmt.Errorf("proto: bad map data tag %d", raw[0])
769 }
770 }
David Symondsefd74762015-04-30 09:19:26 +1000771 keyelem, valelem := keyptr.Elem(), valptr.Elem()
Michael Matloobcde632b2016-03-08 09:25:00 +1100772 if !keyelem.IsValid() {
773 keyelem = reflect.Zero(p.mtype.Key())
774 }
775 if !valelem.IsValid() {
776 valelem = reflect.Zero(p.mtype.Elem())
David Symondsefd74762015-04-30 09:19:26 +1000777 }
David Symonds3ea3e052014-12-22 16:15:28 +1100778
David Symondsefd74762015-04-30 09:19:26 +1000779 v.SetMapIndex(keyelem, valelem)
David Symonds3ea3e052014-12-22 16:15:28 +1100780 return nil
781}
782
Rob Pikeaaa3a622010-03-20 22:32:34 -0700783// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000784func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
David Symonds2ce8ed42013-06-20 13:22:17 +1000785 bas := structPointer_GetStructPointer(base, p.field)
786 if structPointer_IsNil(bas) {
787 // allocate new nested message
788 bas = toStructPointer(reflect.New(p.stype))
789 structPointer_SetStructPointer(base, p.field, bas)
790 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000791 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700792}
793
794// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000795func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700796 raw, e := o.DecodeRawBytes(false)
797 if e != nil {
798 return e
799 }
800
David Symonds2ce8ed42013-06-20 13:22:17 +1000801 bas := structPointer_GetStructPointer(base, p.field)
802 if structPointer_IsNil(bas) {
803 // allocate new nested message
804 bas = toStructPointer(reflect.New(p.stype))
805 structPointer_SetStructPointer(base, p.field, bas)
806 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700807
808 // If the object can unmarshal itself, let it.
David Symonds4f8da862013-06-24 10:57:29 +1000809 if p.isUnmarshaler {
David Symonds2ce8ed42013-06-20 13:22:17 +1000810 iv := structPointer_Interface(bas, p.stype)
David Symondsa80b2822012-03-14 14:31:25 +1100811 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700812 }
813
814 obuf := o.buf
815 oi := o.index
816 o.buf = raw
817 o.index = 0
818
David Symondsc0287172012-08-15 11:10:30 +1000819 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700820 o.buf = obuf
821 o.index = oi
822
823 return err
824}
825
826// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000827func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700828 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700829}
830
831// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000832func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700833 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700834}
835
836// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000837func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
838 v := reflect.New(p.stype)
839 bas := toStructPointer(v)
840 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700841
842 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000843 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700844 return err
845 }
846
David Symondsc0287172012-08-15 11:10:30 +1000847 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700848 if err != nil {
849 return err
850 }
851
852 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100853 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000854 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100855 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700856 }
857
858 obuf := o.buf
859 oi := o.index
860 o.buf = raw
861 o.index = 0
862
David Symondsc0287172012-08-15 11:10:30 +1000863 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700864
865 o.buf = obuf
866 o.index = oi
867
868 return err
869}