blob: 6166dd4dd77e8e5bf2ea75085365d0dd6ff4dbd0 [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
Rob Pikeaaa3a622010-03-20 22:32:34 -070049// The fundamental decoders that interpret bytes on the wire.
50// Those that take integer types all return uint64 and are
51// therefore of type valueDecoder.
52
53// DecodeVarint reads a varint-encoded integer from the slice.
54// It returns the integer and the number of bytes consumed, or
55// zero if there is not enough.
56// This is the format for the
57// int32, int64, uint32, uint64, bool, and enum
58// protocol buffer types.
59func DecodeVarint(buf []byte) (x uint64, n int) {
60 // x, n already 0
Adam Langley28c83cb2013-07-13 14:54:54 +100061 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070062 if n >= len(buf) {
63 return 0, 0
64 }
65 b := uint64(buf[n])
66 n++
67 x |= (b & 0x7F) << shift
68 if (b & 0x80) == 0 {
Adam Langley28c83cb2013-07-13 14:54:54 +100069 return x, n
Rob Pikeaaa3a622010-03-20 22:32:34 -070070 }
71 }
Adam Langley28c83cb2013-07-13 14:54:54 +100072
73 // The number is too large to represent in a 64-bit value.
74 return 0, 0
Rob Pikeaaa3a622010-03-20 22:32:34 -070075}
76
77// DecodeVarint reads a varint-encoded integer from the Buffer.
78// This is the format for the
79// int32, int64, uint32, uint64, bool, and enum
80// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070081func (p *Buffer) DecodeVarint() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070082 // x, err already 0
83
84 i := p.index
85 l := len(p.buf)
86
Adam Langley28c83cb2013-07-13 14:54:54 +100087 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070088 if i >= l {
89 err = io.ErrUnexpectedEOF
90 return
91 }
92 b := p.buf[i]
93 i++
94 x |= (uint64(b) & 0x7F) << shift
95 if b < 0x80 {
Adam Langley28c83cb2013-07-13 14:54:54 +100096 p.index = i
97 return
Rob Pikeaaa3a622010-03-20 22:32:34 -070098 }
99 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000100
101 // The number is too large to represent in a 64-bit value.
102 err = errOverflow
Rob Pikeaaa3a622010-03-20 22:32:34 -0700103 return
104}
105
106// DecodeFixed64 reads a 64-bit integer from the Buffer.
107// This is the format for the
108// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700109func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700110 // x, err already 0
111 i := p.index + 8
Adam Langley28c83cb2013-07-13 14:54:54 +1000112 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700113 err = io.ErrUnexpectedEOF
114 return
115 }
116 p.index = i
117
118 x = uint64(p.buf[i-8])
119 x |= uint64(p.buf[i-7]) << 8
120 x |= uint64(p.buf[i-6]) << 16
121 x |= uint64(p.buf[i-5]) << 24
122 x |= uint64(p.buf[i-4]) << 32
123 x |= uint64(p.buf[i-3]) << 40
124 x |= uint64(p.buf[i-2]) << 48
125 x |= uint64(p.buf[i-1]) << 56
126 return
127}
128
129// DecodeFixed32 reads a 32-bit integer from the Buffer.
130// This is the format for the
131// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700132func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700133 // x, err already 0
134 i := p.index + 4
Adam Langley28c83cb2013-07-13 14:54:54 +1000135 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700136 err = io.ErrUnexpectedEOF
137 return
138 }
139 p.index = i
140
141 x = uint64(p.buf[i-4])
142 x |= uint64(p.buf[i-3]) << 8
143 x |= uint64(p.buf[i-2]) << 16
144 x |= uint64(p.buf[i-1]) << 24
145 return
146}
147
148// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
149// from the Buffer.
150// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700151func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700152 x, err = p.DecodeVarint()
153 if err != nil {
154 return
155 }
156 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
157 return
158}
159
160// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
161// from the Buffer.
162// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700163func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700164 x, err = p.DecodeVarint()
165 if err != nil {
166 return
167 }
168 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
169 return
170}
171
172// These are not ValueDecoders: they produce an array of bytes or a string.
173// bytes, embedded messages
174
175// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
176// This is the format used for the bytes protocol buffer
177// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700178func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700179 n, err := p.DecodeVarint()
180 if err != nil {
181 return
182 }
183
184 nb := int(n)
David Symonds22ac1502012-01-18 12:37:12 +1100185 if nb < 0 {
186 return nil, fmt.Errorf("proto: bad byte length %d", nb)
187 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000188 end := p.index + nb
189 if end < p.index || end > len(p.buf) {
David Symonds22ac1502012-01-18 12:37:12 +1100190 return nil, io.ErrUnexpectedEOF
Rob Pikeaaa3a622010-03-20 22:32:34 -0700191 }
192
193 if !alloc {
194 // todo: check if can get more uses of alloc=false
Adam Langley28c83cb2013-07-13 14:54:54 +1000195 buf = p.buf[p.index:end]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700196 p.index += nb
197 return
198 }
199
200 buf = make([]byte, nb)
201 copy(buf, p.buf[p.index:])
202 p.index += nb
203 return
204}
205
206// DecodeStringBytes reads an encoded string from the Buffer.
207// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700208func (p *Buffer) DecodeStringBytes() (s string, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700209 buf, err := p.DecodeRawBytes(false)
210 if err != nil {
211 return
212 }
213 return string(buf), nil
214}
215
216// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
217// If the protocol buffer has extensions, and the field matches, add it as an extension.
218// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000219func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700220 oi := o.index
221
222 err := o.skip(t, tag, wire)
223 if err != nil {
224 return err
225 }
226
Russ Coxd4ce3f12012-09-12 10:36:26 +1000227 if !unrecField.IsValid() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700228 return nil
229 }
230
Russ Coxd4ce3f12012-09-12 10:36:26 +1000231 ptr := structPointer_Bytes(base, unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700232
Rob Pikeaaa3a622010-03-20 22:32:34 -0700233 // Add the skipped field to struct field
234 obuf := o.buf
235
236 o.buf = *ptr
237 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800238 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700239
240 o.buf = obuf
241
242 return nil
243}
244
245// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700246func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700247
248 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700249 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700250
251 switch wire {
252 case WireVarint:
253 _, err = o.DecodeVarint()
254 case WireFixed64:
255 _, err = o.DecodeFixed64()
256 case WireBytes:
257 _, err = o.DecodeRawBytes(false)
258 case WireFixed32:
259 _, err = o.DecodeFixed32()
260 case WireStartGroup:
261 for {
262 u, err = o.DecodeVarint()
263 if err != nil {
264 break
265 }
266 fwire := int(u & 0x7)
267 if fwire == WireEndGroup {
268 break
269 }
270 ftag := int(u >> 3)
271 err = o.skip(t, ftag, fwire)
272 if err != nil {
273 break
274 }
275 }
276 default:
David Symonds22ac1502012-01-18 12:37:12 +1100277 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700278 }
279 return err
280}
281
David Symonds7d3c6802012-11-08 08:20:18 +1100282// Unmarshaler is the interface representing objects that can
283// unmarshal themselves. The method should reset the receiver before
284// decoding starts. The argument points to data that may be
285// overwritten, so implementations should not keep references to the
286// buffer.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700287type Unmarshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700288 Unmarshal([]byte) error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700289}
290
291// Unmarshal parses the protocol buffer representation in buf and places the
292// decoded result in pb. If the struct underlying pb does not match
293// the data in buf, the results can be unpredictable.
David Symonds19285602012-07-02 16:04:28 -0700294//
David Symonds525838c2012-07-20 15:42:49 +1000295// Unmarshal resets pb before starting to unmarshal, so any
David Symondsd4b52d02013-01-15 14:30:26 +1100296// existing data in pb is always removed. Use UnmarshalMerge
David Symonds525838c2012-07-20 15:42:49 +1000297// to preserve and append to existing data.
David Symonds9f60f432012-06-14 09:45:25 +1000298func Unmarshal(buf []byte, pb Message) error {
David Symonds525838c2012-07-20 15:42:49 +1000299 pb.Reset()
David Symondsd4b52d02013-01-15 14:30:26 +1100300 return UnmarshalMerge(buf, pb)
David Symonds19285602012-07-02 16:04:28 -0700301}
302
David Symondsd4b52d02013-01-15 14:30:26 +1100303// UnmarshalMerge parses the protocol buffer representation in buf and
David Symonds19285602012-07-02 16:04:28 -0700304// writes the decoded result to pb. If the struct underlying pb does not match
305// the data in buf, the results can be unpredictable.
306//
David Symondsd4b52d02013-01-15 14:30:26 +1100307// UnmarshalMerge merges into existing data in pb.
David Symonds525838c2012-07-20 15:42:49 +1000308// Most code should use Unmarshal instead.
David Symondsd4b52d02013-01-15 14:30:26 +1100309func UnmarshalMerge(buf []byte, pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700310 // If the object can unmarshal itself, let it.
311 if u, ok := pb.(Unmarshaler); ok {
312 return u.Unmarshal(buf)
313 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700314 return NewBuffer(buf).Unmarshal(pb)
315}
316
317// Unmarshal parses the protocol buffer representation in the
318// Buffer and places the decoded result in pb. If the struct
319// underlying pb does not match the data in the buffer, the results can be
320// unpredictable.
David Symonds9f60f432012-06-14 09:45:25 +1000321func (p *Buffer) Unmarshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700322 // If the object can unmarshal itself, let it.
323 if u, ok := pb.(Unmarshaler); ok {
324 err := u.Unmarshal(p.buf[p.index:])
325 p.index = len(p.buf)
326 return err
327 }
328
Rob Pikeaaa3a622010-03-20 22:32:34 -0700329 typ, base, err := getbase(pb)
330 if err != nil {
331 return err
332 }
333
David Symonds6a6f82c2012-08-22 09:18:54 +1000334 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700335
David Symonds9f60f432012-06-14 09:45:25 +1000336 if collectStats {
337 stats.Decode++
338 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700339
340 return err
341}
342
343// unmarshalType does the work of unmarshaling a structure.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000344func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000345 var state errorState
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700346 required, reqFields := prop.reqCount, uint64(0)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700347
Rob Pikea17fdd92011-11-02 12:43:05 -0700348 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700349 for err == nil && o.index < len(o.buf) {
350 oi := o.index
351 var u uint64
352 u, err = o.DecodeVarint()
353 if err != nil {
354 break
355 }
356 wire := int(u & 0x7)
357 if wire == WireEndGroup {
358 if is_group {
359 return nil // input is satisfied
360 }
David Symondsbaeae8b2014-06-23 09:41:27 +1000361 return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700362 }
363 tag := int(u >> 3)
David Symonds6e50db52012-02-11 15:56:22 +1100364 if tag <= 0 {
David Symonds50386d22014-10-28 11:00:50 +1100365 return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
David Symonds6e50db52012-02-11 15:56:22 +1100366 }
David Symonds2bba1b22012-09-26 14:53:08 +1000367 fieldnum, ok := prop.decoderTags.get(tag)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700368 if !ok {
369 // Maybe it's an extension?
Russ Coxd4ce3f12012-09-12 10:36:26 +1000370 if prop.extendable {
371 if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
372 if err = o.skip(st, tag, wire); err == nil {
373 ext := e.ExtensionMap()[int32(tag)] // may be missing
374 ext.enc = append(ext.enc, o.buf[oi:o.index]...)
375 e.ExtensionMap()[int32(tag)] = ext
376 }
377 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700378 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700379 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000380 err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700381 continue
382 }
383 p := prop.Prop[fieldnum]
384
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700385 if p.dec == nil {
David Symonds6a6f82c2012-08-22 09:18:54 +1000386 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700387 continue
388 }
David Symonds5b7775e2010-12-01 10:09:04 +1100389 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700390 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100391 if wire == WireBytes && p.packedDec != nil {
392 // a packable field
393 dec = p.packedDec
394 } else {
David Symondsbaeae8b2014-06-23 09:41:27 +1000395 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 +1100396 continue
397 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700398 }
David Symonds4646c372013-09-09 13:18:58 +1000399 decErr := dec(o, p, base)
400 if decErr != nil && !state.shouldContinue(decErr, p) {
401 err = decErr
402 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700403 if err == nil && p.Required {
404 // Successfully decoded a required field.
405 if tag <= 64 {
406 // use bitmap for fields 1-64 to catch field reuse.
407 var mask uint64 = 1 << uint64(tag-1)
408 if reqFields&mask == 0 {
409 // new required field
410 reqFields |= mask
411 required--
412 }
413 } else {
414 // This is imprecise. It can be fooled by a required field
415 // with a tag > 64 that is encoded twice; that's very rare.
416 // A fully correct implementation would require allocating
417 // a data structure, which we would like to avoid.
418 required--
419 }
420 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700421 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700422 if err == nil {
423 if is_group {
424 return io.ErrUnexpectedEOF
425 }
David Symonds4646c372013-09-09 13:18:58 +1000426 if state.err != nil {
427 return state.err
428 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700429 if required > 0 {
David Symonds4646c372013-09-09 13:18:58 +1000430 // Not enough information to determine the exact field. If we use extra
431 // CPU, we could determine the field only if the missing required field
432 // has a tag <= 64 and we check reqFields.
David Symondse583a5f2013-09-27 10:02:37 +1000433 return &RequiredNotSetError{"{Unknown}"}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700434 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700435 }
436 return err
437}
438
Rob Pikeaaa3a622010-03-20 22:32:34 -0700439// Individual type decoders
440// For each,
441// u is the decoded value,
442// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700443
444// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700445// The goal is modest amortization and allocation
446// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700447const (
Russ Coxd4ce3f12012-09-12 10:36:26 +1000448 boolPoolSize = 16
449 uint32PoolSize = 8
450 uint64PoolSize = 4
Rob Pike76f6ee52011-10-20 12:58:28 -0700451)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700452
453// Decode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000454func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700455 u, err := p.valDec(o)
456 if err != nil {
457 return err
458 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700459 if len(o.bools) == 0 {
460 o.bools = make([]bool, boolPoolSize)
461 }
462 o.bools[0] = u != 0
Russ Coxd4ce3f12012-09-12 10:36:26 +1000463 *structPointer_Bool(base, p.field) = &o.bools[0]
Rob Pike76f6ee52011-10-20 12:58:28 -0700464 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700465 return nil
466}
467
David Symondsabd3b412014-11-28 11:43:44 +1100468func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
469 u, err := p.valDec(o)
470 if err != nil {
471 return err
472 }
473 *structPointer_BoolVal(base, p.field) = u != 0
474 return nil
475}
476
Rob Pikeaaa3a622010-03-20 22:32:34 -0700477// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000478func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700479 u, err := p.valDec(o)
480 if err != nil {
481 return err
482 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000483 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700484 return nil
485}
486
David Symondsabd3b412014-11-28 11:43:44 +1100487func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
488 u, err := p.valDec(o)
489 if err != nil {
490 return err
491 }
492 word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
493 return nil
494}
495
Rob Pikeaaa3a622010-03-20 22:32:34 -0700496// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000497func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700498 u, err := p.valDec(o)
499 if err != nil {
500 return err
501 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000502 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700503 return nil
504}
505
David Symondsabd3b412014-11-28 11:43:44 +1100506func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
507 u, err := p.valDec(o)
508 if err != nil {
509 return err
510 }
511 word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
512 return nil
513}
514
Rob Pikeaaa3a622010-03-20 22:32:34 -0700515// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000516func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700517 s, err := o.DecodeStringBytes()
518 if err != nil {
519 return err
520 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700521 sp := new(string)
522 *sp = s
Russ Coxd4ce3f12012-09-12 10:36:26 +1000523 *structPointer_String(base, p.field) = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700524 return nil
525}
526
David Symondsabd3b412014-11-28 11:43:44 +1100527func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
528 s, err := o.DecodeStringBytes()
529 if err != nil {
530 return err
531 }
532 *structPointer_StringVal(base, p.field) = s
533 return nil
534}
535
Rob Pikeaaa3a622010-03-20 22:32:34 -0700536// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000537func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700538 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700539 if err != nil {
540 return err
541 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000542 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700543 return nil
544}
545
546// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000547func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700548 u, err := p.valDec(o)
549 if err != nil {
550 return err
551 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000552 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700553 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100554 return nil
555}
556
557// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000558func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
559 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100560
561 nn, err := o.DecodeVarint()
562 if err != nil {
563 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700564 }
David Symonds5b7775e2010-12-01 10:09:04 +1100565 nb := int(nn) // number of bytes of encoded bools
566
Rob Pike76f6ee52011-10-20 12:58:28 -0700567 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100568 for i := 0; i < nb; i++ {
569 u, err := p.valDec(o)
570 if err != nil {
571 return err
572 }
573 y = append(y, u != 0)
574 }
575
Rob Pike76f6ee52011-10-20 12:58:28 -0700576 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700577 return nil
578}
579
580// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000581func (o *Buffer) dec_slice_int32(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 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100587 return nil
588}
589
590// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000591func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
592 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100593
594 nn, err := o.DecodeVarint()
595 if err != nil {
596 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700597 }
David Symonds5b7775e2010-12-01 10:09:04 +1100598 nb := int(nn) // number of bytes of encoded int32s
599
David Symonds5b7775e2010-12-01 10:09:04 +1100600 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000601 if fin < o.index {
602 return errOverflow
603 }
David Symonds5b7775e2010-12-01 10:09:04 +1100604 for o.index < fin {
605 u, err := p.valDec(o)
606 if err != nil {
607 return err
608 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000609 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100610 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700611 return nil
612}
613
614// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000615func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700616 u, err := p.valDec(o)
617 if err != nil {
618 return err
619 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700620
Russ Coxd4ce3f12012-09-12 10:36:26 +1000621 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100622 return nil
623}
624
625// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000626func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
627 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100628
629 nn, err := o.DecodeVarint()
630 if err != nil {
631 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700632 }
David Symonds5b7775e2010-12-01 10:09:04 +1100633 nb := int(nn) // number of bytes of encoded int64s
634
David Symonds5b7775e2010-12-01 10:09:04 +1100635 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000636 if fin < o.index {
637 return errOverflow
638 }
David Symonds5b7775e2010-12-01 10:09:04 +1100639 for o.index < fin {
640 u, err := p.valDec(o)
641 if err != nil {
642 return err
643 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000644 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100645 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700646 return nil
647}
648
649// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000650func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700651 s, err := o.DecodeStringBytes()
652 if err != nil {
653 return err
654 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000655 v := structPointer_StringSlice(base, p.field)
656 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700657 return nil
658}
659
660// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000661func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700662 b, err := o.DecodeRawBytes(true)
663 if err != nil {
664 return err
665 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000666 v := structPointer_BytesSlice(base, p.field)
667 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700668 return nil
669}
670
671// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000672func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
David Symonds2ce8ed42013-06-20 13:22:17 +1000673 bas := structPointer_GetStructPointer(base, p.field)
674 if structPointer_IsNil(bas) {
675 // allocate new nested message
676 bas = toStructPointer(reflect.New(p.stype))
677 structPointer_SetStructPointer(base, p.field, bas)
678 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000679 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700680}
681
682// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000683func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700684 raw, e := o.DecodeRawBytes(false)
685 if e != nil {
686 return e
687 }
688
David Symonds2ce8ed42013-06-20 13:22:17 +1000689 bas := structPointer_GetStructPointer(base, p.field)
690 if structPointer_IsNil(bas) {
691 // allocate new nested message
692 bas = toStructPointer(reflect.New(p.stype))
693 structPointer_SetStructPointer(base, p.field, bas)
694 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700695
696 // If the object can unmarshal itself, let it.
David Symonds4f8da862013-06-24 10:57:29 +1000697 if p.isUnmarshaler {
David Symonds2ce8ed42013-06-20 13:22:17 +1000698 iv := structPointer_Interface(bas, p.stype)
David Symondsa80b2822012-03-14 14:31:25 +1100699 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700700 }
701
702 obuf := o.buf
703 oi := o.index
704 o.buf = raw
705 o.index = 0
706
David Symondsc0287172012-08-15 11:10:30 +1000707 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700708 o.buf = obuf
709 o.index = oi
710
711 return err
712}
713
714// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000715func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700716 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700717}
718
719// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000720func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700721 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700722}
723
724// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000725func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
726 v := reflect.New(p.stype)
727 bas := toStructPointer(v)
728 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700729
730 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000731 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700732 return err
733 }
734
David Symondsc0287172012-08-15 11:10:30 +1000735 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700736 if err != nil {
737 return err
738 }
739
740 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100741 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000742 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100743 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700744 }
745
746 obuf := o.buf
747 oi := o.index
748 o.buf = raw
749 o.index = 0
750
David Symondsc0287172012-08-15 11:10:30 +1000751 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700752
753 o.buf = obuf
754 o.index = oi
755
756 return err
757}