blob: 5810782fd84d55364203111ac09baad7978e0884 [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 {
393 if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
394 if err = o.skip(st, tag, wire); err == nil {
395 ext := e.ExtensionMap()[int32(tag)] // may be missing
396 ext.enc = append(ext.enc, o.buf[oi:o.index]...)
397 e.ExtensionMap()[int32(tag)] = ext
398 }
399 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700400 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700401 }
David Symonds59b73b32015-08-24 13:22:02 +1000402 // Maybe it's a oneof?
403 if prop.oneofUnmarshaler != nil {
404 m := structPointer_Interface(base, st).(Message)
405 // First return value indicates whether tag is a oneof field.
406 ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
407 if err == ErrInternalBadWireType {
408 // Map the error to something more descriptive.
409 // Do the formatting here to save generated code space.
410 err = fmt.Errorf("bad wiretype for oneof field in %T", m)
411 }
412 if ok {
413 continue
414 }
415 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000416 err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700417 continue
418 }
419 p := prop.Prop[fieldnum]
420
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700421 if p.dec == nil {
David Symonds6a6f82c2012-08-22 09:18:54 +1000422 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700423 continue
424 }
David Symonds5b7775e2010-12-01 10:09:04 +1100425 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700426 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100427 if wire == WireBytes && p.packedDec != nil {
428 // a packable field
429 dec = p.packedDec
430 } else {
David Symondsbaeae8b2014-06-23 09:41:27 +1000431 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 +1100432 continue
433 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700434 }
David Symonds4646c372013-09-09 13:18:58 +1000435 decErr := dec(o, p, base)
436 if decErr != nil && !state.shouldContinue(decErr, p) {
437 err = decErr
438 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700439 if err == nil && p.Required {
440 // Successfully decoded a required field.
441 if tag <= 64 {
442 // use bitmap for fields 1-64 to catch field reuse.
443 var mask uint64 = 1 << uint64(tag-1)
444 if reqFields&mask == 0 {
445 // new required field
446 reqFields |= mask
447 required--
448 }
449 } else {
450 // This is imprecise. It can be fooled by a required field
451 // with a tag > 64 that is encoded twice; that's very rare.
452 // A fully correct implementation would require allocating
453 // a data structure, which we would like to avoid.
454 required--
455 }
456 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700457 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700458 if err == nil {
459 if is_group {
460 return io.ErrUnexpectedEOF
461 }
David Symonds4646c372013-09-09 13:18:58 +1000462 if state.err != nil {
463 return state.err
464 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700465 if required > 0 {
David Symonds4646c372013-09-09 13:18:58 +1000466 // Not enough information to determine the exact field. If we use extra
467 // CPU, we could determine the field only if the missing required field
468 // has a tag <= 64 and we check reqFields.
David Symondse583a5f2013-09-27 10:02:37 +1000469 return &RequiredNotSetError{"{Unknown}"}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700470 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700471 }
472 return err
473}
474
Rob Pikeaaa3a622010-03-20 22:32:34 -0700475// Individual type decoders
476// For each,
477// u is the decoded value,
478// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700479
480// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700481// The goal is modest amortization and allocation
482// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700483const (
Russ Coxd4ce3f12012-09-12 10:36:26 +1000484 boolPoolSize = 16
485 uint32PoolSize = 8
486 uint64PoolSize = 4
Rob Pike76f6ee52011-10-20 12:58:28 -0700487)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700488
489// Decode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000490func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700491 u, err := p.valDec(o)
492 if err != nil {
493 return err
494 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700495 if len(o.bools) == 0 {
496 o.bools = make([]bool, boolPoolSize)
497 }
498 o.bools[0] = u != 0
Russ Coxd4ce3f12012-09-12 10:36:26 +1000499 *structPointer_Bool(base, p.field) = &o.bools[0]
Rob Pike76f6ee52011-10-20 12:58:28 -0700500 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700501 return nil
502}
503
David Symondsabd3b412014-11-28 11:43:44 +1100504func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
505 u, err := p.valDec(o)
506 if err != nil {
507 return err
508 }
509 *structPointer_BoolVal(base, p.field) = u != 0
510 return nil
511}
512
Rob Pikeaaa3a622010-03-20 22:32:34 -0700513// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000514func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700515 u, err := p.valDec(o)
516 if err != nil {
517 return err
518 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000519 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700520 return nil
521}
522
David Symondsabd3b412014-11-28 11:43:44 +1100523func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
524 u, err := p.valDec(o)
525 if err != nil {
526 return err
527 }
528 word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
529 return nil
530}
531
Rob Pikeaaa3a622010-03-20 22:32:34 -0700532// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000533func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700534 u, err := p.valDec(o)
535 if err != nil {
536 return err
537 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000538 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700539 return nil
540}
541
David Symondsabd3b412014-11-28 11:43:44 +1100542func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
543 u, err := p.valDec(o)
544 if err != nil {
545 return err
546 }
547 word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
548 return nil
549}
550
Rob Pikeaaa3a622010-03-20 22:32:34 -0700551// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000552func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700553 s, err := o.DecodeStringBytes()
554 if err != nil {
555 return err
556 }
David Symondsa11b6342015-01-28 17:07:47 +1100557 *structPointer_String(base, p.field) = &s
Rob Pikeaaa3a622010-03-20 22:32:34 -0700558 return nil
559}
560
David Symondsabd3b412014-11-28 11:43:44 +1100561func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
562 s, err := o.DecodeStringBytes()
563 if err != nil {
564 return err
565 }
566 *structPointer_StringVal(base, p.field) = s
567 return nil
568}
569
Rob Pikeaaa3a622010-03-20 22:32:34 -0700570// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000571func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700572 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700573 if err != nil {
574 return err
575 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000576 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700577 return nil
578}
579
580// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000581func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700582 u, err := p.valDec(o)
583 if err != nil {
584 return err
585 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000586 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700587 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100588 return nil
589}
590
591// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000592func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
593 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100594
595 nn, err := o.DecodeVarint()
596 if err != nil {
597 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700598 }
David Symonds5b7775e2010-12-01 10:09:04 +1100599 nb := int(nn) // number of bytes of encoded bools
David Symonds0c959e82015-09-28 15:17:00 +1000600 fin := o.index + nb
601 if fin < o.index {
602 return errOverflow
603 }
David Symonds5b7775e2010-12-01 10:09:04 +1100604
Rob Pike76f6ee52011-10-20 12:58:28 -0700605 y := *v
David Symonds0c959e82015-09-28 15:17:00 +1000606 for o.index < fin {
David Symonds5b7775e2010-12-01 10:09:04 +1100607 u, err := p.valDec(o)
608 if err != nil {
609 return err
610 }
611 y = append(y, u != 0)
612 }
613
Rob Pike76f6ee52011-10-20 12:58:28 -0700614 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700615 return nil
616}
617
618// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000619func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700620 u, err := p.valDec(o)
621 if err != nil {
622 return err
623 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000624 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100625 return nil
626}
627
628// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000629func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
630 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100631
632 nn, err := o.DecodeVarint()
633 if err != nil {
634 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700635 }
David Symonds5b7775e2010-12-01 10:09:04 +1100636 nb := int(nn) // number of bytes of encoded int32s
637
David Symonds5b7775e2010-12-01 10:09:04 +1100638 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000639 if fin < o.index {
640 return errOverflow
641 }
David Symonds5b7775e2010-12-01 10:09:04 +1100642 for o.index < fin {
643 u, err := p.valDec(o)
644 if err != nil {
645 return err
646 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000647 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100648 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700649 return nil
650}
651
652// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000653func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700654 u, err := p.valDec(o)
655 if err != nil {
656 return err
657 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700658
Russ Coxd4ce3f12012-09-12 10:36:26 +1000659 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100660 return nil
661}
662
663// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000664func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
665 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100666
667 nn, err := o.DecodeVarint()
668 if err != nil {
669 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700670 }
David Symonds5b7775e2010-12-01 10:09:04 +1100671 nb := int(nn) // number of bytes of encoded int64s
672
David Symonds5b7775e2010-12-01 10:09:04 +1100673 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000674 if fin < o.index {
675 return errOverflow
676 }
David Symonds5b7775e2010-12-01 10:09:04 +1100677 for o.index < fin {
678 u, err := p.valDec(o)
679 if err != nil {
680 return err
681 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000682 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100683 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700684 return nil
685}
686
687// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000688func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700689 s, err := o.DecodeStringBytes()
690 if err != nil {
691 return err
692 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000693 v := structPointer_StringSlice(base, p.field)
694 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700695 return nil
696}
697
698// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000699func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700700 b, err := o.DecodeRawBytes(true)
701 if err != nil {
702 return err
703 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000704 v := structPointer_BytesSlice(base, p.field)
705 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700706 return nil
707}
708
David Symonds3ea3e052014-12-22 16:15:28 +1100709// Decode a map field.
710func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
711 raw, err := o.DecodeRawBytes(false)
712 if err != nil {
713 return err
714 }
715 oi := o.index // index at the end of this map entry
716 o.index -= len(raw) // move buffer back to start of map entry
717
David Symonds0f7a9ca2015-07-20 16:00:00 +1000718 mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
David Symonds3ea3e052014-12-22 16:15:28 +1100719 if mptr.Elem().IsNil() {
720 mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
721 }
722 v := mptr.Elem() // map[K]V
723
724 // Prepare addressable doubly-indirect placeholders for the key and value types.
725 // See enc_new_map for why.
726 keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
727 keybase := toStructPointer(keyptr.Addr()) // **K
728
729 var valbase structPointer
730 var valptr reflect.Value
731 switch p.mtype.Elem().Kind() {
732 case reflect.Slice:
733 // []byte
734 var dummy []byte
735 valptr = reflect.ValueOf(&dummy) // *[]byte
736 valbase = toStructPointer(valptr) // *[]byte
737 case reflect.Ptr:
738 // message; valptr is **Msg; need to allocate the intermediate pointer
739 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
740 valptr.Set(reflect.New(valptr.Type().Elem()))
741 valbase = toStructPointer(valptr)
742 default:
743 // everything else
744 valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
745 valbase = toStructPointer(valptr.Addr()) // **V
746 }
747
748 // Decode.
749 // This parses a restricted wire format, namely the encoding of a message
750 // with two fields. See enc_new_map for the format.
751 for o.index < oi {
752 // tagcode for key and value properties are always a single byte
753 // because they have tags 1 and 2.
754 tagcode := o.buf[o.index]
755 o.index++
756 switch tagcode {
757 case p.mkeyprop.tagcode[0]:
758 if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
759 return err
760 }
761 case p.mvalprop.tagcode[0]:
762 if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
763 return err
764 }
765 default:
766 // TODO: Should we silently skip this instead?
767 return fmt.Errorf("proto: bad map data tag %d", raw[0])
768 }
769 }
David Symondsefd74762015-04-30 09:19:26 +1000770 keyelem, valelem := keyptr.Elem(), valptr.Elem()
771 if !keyelem.IsValid() || !valelem.IsValid() {
772 // We did not decode the key or the value in the map entry.
773 // Either way, it's an invalid map entry.
774 return fmt.Errorf("proto: bad map data: missing key/val")
775 }
David Symonds3ea3e052014-12-22 16:15:28 +1100776
David Symondsefd74762015-04-30 09:19:26 +1000777 v.SetMapIndex(keyelem, valelem)
David Symonds3ea3e052014-12-22 16:15:28 +1100778 return nil
779}
780
Rob Pikeaaa3a622010-03-20 22:32:34 -0700781// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000782func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
David Symonds2ce8ed42013-06-20 13:22:17 +1000783 bas := structPointer_GetStructPointer(base, p.field)
784 if structPointer_IsNil(bas) {
785 // allocate new nested message
786 bas = toStructPointer(reflect.New(p.stype))
787 structPointer_SetStructPointer(base, p.field, bas)
788 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000789 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700790}
791
792// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000793func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700794 raw, e := o.DecodeRawBytes(false)
795 if e != nil {
796 return e
797 }
798
David Symonds2ce8ed42013-06-20 13:22:17 +1000799 bas := structPointer_GetStructPointer(base, p.field)
800 if structPointer_IsNil(bas) {
801 // allocate new nested message
802 bas = toStructPointer(reflect.New(p.stype))
803 structPointer_SetStructPointer(base, p.field, bas)
804 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700805
806 // If the object can unmarshal itself, let it.
David Symonds4f8da862013-06-24 10:57:29 +1000807 if p.isUnmarshaler {
David Symonds2ce8ed42013-06-20 13:22:17 +1000808 iv := structPointer_Interface(bas, p.stype)
David Symondsa80b2822012-03-14 14:31:25 +1100809 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700810 }
811
812 obuf := o.buf
813 oi := o.index
814 o.buf = raw
815 o.index = 0
816
David Symondsc0287172012-08-15 11:10:30 +1000817 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700818 o.buf = obuf
819 o.index = oi
820
821 return err
822}
823
824// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000825func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700826 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700827}
828
829// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000830func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700831 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700832}
833
834// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000835func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
836 v := reflect.New(p.stype)
837 bas := toStructPointer(v)
838 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700839
840 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000841 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700842 return err
843 }
844
David Symondsc0287172012-08-15 11:10:30 +1000845 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700846 if err != nil {
847 return err
848 }
849
850 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100851 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000852 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100853 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700854 }
855
856 obuf := o.buf
857 oi := o.index
858 o.buf = raw
859 o.index = 0
860
David Symondsc0287172012-08-15 11:10:30 +1000861 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700862
863 o.buf = obuf
864 o.index = oi
865
866 return err
867}