blob: 3c58cfb9b2a22960e61c0520e283912eead15752 [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.
Rob Pikeaaa3a622010-03-20 22:32:34 -07004// http://code.google.com/p/goprotobuf/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto
33
34/*
35 * Routines for 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
46// ErrWrongType occurs when the wire encoding for the field disagrees with
47// that specified in the type being decoded. This is usually caused by attempting
48// to convert an encoded protocol buffer into a struct of the wrong type.
David Symondsa7f3a0f2013-09-09 13:32:33 +100049var ErrWrongType = errors.New("proto: field/encoding mismatch: wrong type for field")
Rob Pikeaaa3a622010-03-20 22:32:34 -070050
Adam Langley28c83cb2013-07-13 14:54:54 +100051// errOverflow is returned when an integer is too large to be represented.
52var errOverflow = errors.New("proto: integer overflow")
53
Rob Pikeaaa3a622010-03-20 22:32:34 -070054// The fundamental decoders that interpret bytes on the wire.
55// Those that take integer types all return uint64 and are
56// therefore of type valueDecoder.
57
58// DecodeVarint reads a varint-encoded integer from the slice.
59// It returns the integer and the number of bytes consumed, or
60// zero if there is not enough.
61// This is the format for the
62// int32, int64, uint32, uint64, bool, and enum
63// protocol buffer types.
64func DecodeVarint(buf []byte) (x uint64, n int) {
65 // x, n already 0
Adam Langley28c83cb2013-07-13 14:54:54 +100066 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070067 if n >= len(buf) {
68 return 0, 0
69 }
70 b := uint64(buf[n])
71 n++
72 x |= (b & 0x7F) << shift
73 if (b & 0x80) == 0 {
Adam Langley28c83cb2013-07-13 14:54:54 +100074 return x, n
Rob Pikeaaa3a622010-03-20 22:32:34 -070075 }
76 }
Adam Langley28c83cb2013-07-13 14:54:54 +100077
78 // The number is too large to represent in a 64-bit value.
79 return 0, 0
Rob Pikeaaa3a622010-03-20 22:32:34 -070080}
81
82// DecodeVarint reads a varint-encoded integer from the Buffer.
83// This is the format for the
84// int32, int64, uint32, uint64, bool, and enum
85// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070086func (p *Buffer) DecodeVarint() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070087 // x, err already 0
88
89 i := p.index
90 l := len(p.buf)
91
Adam Langley28c83cb2013-07-13 14:54:54 +100092 for shift := uint(0); shift < 64; shift += 7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070093 if i >= l {
94 err = io.ErrUnexpectedEOF
95 return
96 }
97 b := p.buf[i]
98 i++
99 x |= (uint64(b) & 0x7F) << shift
100 if b < 0x80 {
Adam Langley28c83cb2013-07-13 14:54:54 +1000101 p.index = i
102 return
Rob Pikeaaa3a622010-03-20 22:32:34 -0700103 }
104 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000105
106 // The number is too large to represent in a 64-bit value.
107 err = errOverflow
Rob Pikeaaa3a622010-03-20 22:32:34 -0700108 return
109}
110
111// DecodeFixed64 reads a 64-bit integer from the Buffer.
112// This is the format for the
113// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700114func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700115 // x, err already 0
116 i := p.index + 8
Adam Langley28c83cb2013-07-13 14:54:54 +1000117 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700118 err = io.ErrUnexpectedEOF
119 return
120 }
121 p.index = i
122
123 x = uint64(p.buf[i-8])
124 x |= uint64(p.buf[i-7]) << 8
125 x |= uint64(p.buf[i-6]) << 16
126 x |= uint64(p.buf[i-5]) << 24
127 x |= uint64(p.buf[i-4]) << 32
128 x |= uint64(p.buf[i-3]) << 40
129 x |= uint64(p.buf[i-2]) << 48
130 x |= uint64(p.buf[i-1]) << 56
131 return
132}
133
134// DecodeFixed32 reads a 32-bit integer from the Buffer.
135// This is the format for the
136// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700137func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700138 // x, err already 0
139 i := p.index + 4
Adam Langley28c83cb2013-07-13 14:54:54 +1000140 if i < 0 || i > len(p.buf) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700141 err = io.ErrUnexpectedEOF
142 return
143 }
144 p.index = i
145
146 x = uint64(p.buf[i-4])
147 x |= uint64(p.buf[i-3]) << 8
148 x |= uint64(p.buf[i-2]) << 16
149 x |= uint64(p.buf[i-1]) << 24
150 return
151}
152
153// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
154// from the Buffer.
155// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700156func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700157 x, err = p.DecodeVarint()
158 if err != nil {
159 return
160 }
161 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
162 return
163}
164
165// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
166// from the Buffer.
167// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700168func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700169 x, err = p.DecodeVarint()
170 if err != nil {
171 return
172 }
173 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
174 return
175}
176
177// These are not ValueDecoders: they produce an array of bytes or a string.
178// bytes, embedded messages
179
180// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
181// This is the format used for the bytes protocol buffer
182// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700183func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700184 n, err := p.DecodeVarint()
185 if err != nil {
186 return
187 }
188
189 nb := int(n)
David Symonds22ac1502012-01-18 12:37:12 +1100190 if nb < 0 {
191 return nil, fmt.Errorf("proto: bad byte length %d", nb)
192 }
Adam Langley28c83cb2013-07-13 14:54:54 +1000193 end := p.index + nb
194 if end < p.index || end > len(p.buf) {
David Symonds22ac1502012-01-18 12:37:12 +1100195 return nil, io.ErrUnexpectedEOF
Rob Pikeaaa3a622010-03-20 22:32:34 -0700196 }
197
198 if !alloc {
199 // todo: check if can get more uses of alloc=false
Adam Langley28c83cb2013-07-13 14:54:54 +1000200 buf = p.buf[p.index:end]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700201 p.index += nb
202 return
203 }
204
205 buf = make([]byte, nb)
206 copy(buf, p.buf[p.index:])
207 p.index += nb
208 return
209}
210
211// DecodeStringBytes reads an encoded string from the Buffer.
212// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700213func (p *Buffer) DecodeStringBytes() (s string, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700214 buf, err := p.DecodeRawBytes(false)
215 if err != nil {
216 return
217 }
218 return string(buf), nil
219}
220
221// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
222// If the protocol buffer has extensions, and the field matches, add it as an extension.
223// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000224func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700225 oi := o.index
226
227 err := o.skip(t, tag, wire)
228 if err != nil {
229 return err
230 }
231
Russ Coxd4ce3f12012-09-12 10:36:26 +1000232 if !unrecField.IsValid() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700233 return nil
234 }
235
Russ Coxd4ce3f12012-09-12 10:36:26 +1000236 ptr := structPointer_Bytes(base, unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700237
238 if *ptr == nil {
239 // This is the first skipped element,
240 // allocate a new buffer.
241 *ptr = o.bufalloc()
242 }
243
244 // Add the skipped field to struct field
245 obuf := o.buf
246
247 o.buf = *ptr
248 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800249 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700250
251 o.buf = obuf
252
253 return nil
254}
255
256// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700257func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700258
259 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700260 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700261
262 switch wire {
263 case WireVarint:
264 _, err = o.DecodeVarint()
265 case WireFixed64:
266 _, err = o.DecodeFixed64()
267 case WireBytes:
268 _, err = o.DecodeRawBytes(false)
269 case WireFixed32:
270 _, err = o.DecodeFixed32()
271 case WireStartGroup:
272 for {
273 u, err = o.DecodeVarint()
274 if err != nil {
275 break
276 }
277 fwire := int(u & 0x7)
278 if fwire == WireEndGroup {
279 break
280 }
281 ftag := int(u >> 3)
282 err = o.skip(t, ftag, fwire)
283 if err != nil {
284 break
285 }
286 }
287 default:
David Symonds22ac1502012-01-18 12:37:12 +1100288 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700289 }
290 return err
291}
292
David Symonds7d3c6802012-11-08 08:20:18 +1100293// Unmarshaler is the interface representing objects that can
294// unmarshal themselves. The method should reset the receiver before
295// decoding starts. The argument points to data that may be
296// overwritten, so implementations should not keep references to the
297// buffer.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700298type Unmarshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700299 Unmarshal([]byte) error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700300}
301
302// Unmarshal parses the protocol buffer representation in buf and places the
303// decoded result in pb. If the struct underlying pb does not match
304// the data in buf, the results can be unpredictable.
David Symonds19285602012-07-02 16:04:28 -0700305//
David Symonds525838c2012-07-20 15:42:49 +1000306// Unmarshal resets pb before starting to unmarshal, so any
David Symondsd4b52d02013-01-15 14:30:26 +1100307// existing data in pb is always removed. Use UnmarshalMerge
David Symonds525838c2012-07-20 15:42:49 +1000308// to preserve and append to existing data.
David Symonds9f60f432012-06-14 09:45:25 +1000309func Unmarshal(buf []byte, pb Message) error {
David Symonds525838c2012-07-20 15:42:49 +1000310 pb.Reset()
David Symondsd4b52d02013-01-15 14:30:26 +1100311 return UnmarshalMerge(buf, pb)
David Symonds19285602012-07-02 16:04:28 -0700312}
313
David Symondsd4b52d02013-01-15 14:30:26 +1100314// UnmarshalMerge parses the protocol buffer representation in buf and
David Symonds19285602012-07-02 16:04:28 -0700315// writes the decoded result to pb. If the struct underlying pb does not match
316// the data in buf, the results can be unpredictable.
317//
David Symondsd4b52d02013-01-15 14:30:26 +1100318// UnmarshalMerge merges into existing data in pb.
David Symonds525838c2012-07-20 15:42:49 +1000319// Most code should use Unmarshal instead.
David Symondsd4b52d02013-01-15 14:30:26 +1100320func UnmarshalMerge(buf []byte, pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700321 // If the object can unmarshal itself, let it.
322 if u, ok := pb.(Unmarshaler); ok {
323 return u.Unmarshal(buf)
324 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700325 return NewBuffer(buf).Unmarshal(pb)
326}
327
328// Unmarshal parses the protocol buffer representation in the
329// Buffer and places the decoded result in pb. If the struct
330// underlying pb does not match the data in the buffer, the results can be
331// unpredictable.
David Symonds9f60f432012-06-14 09:45:25 +1000332func (p *Buffer) Unmarshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700333 // If the object can unmarshal itself, let it.
334 if u, ok := pb.(Unmarshaler); ok {
335 err := u.Unmarshal(p.buf[p.index:])
336 p.index = len(p.buf)
337 return err
338 }
339
Rob Pikeaaa3a622010-03-20 22:32:34 -0700340 typ, base, err := getbase(pb)
341 if err != nil {
342 return err
343 }
344
David Symonds6a6f82c2012-08-22 09:18:54 +1000345 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700346
David Symonds9f60f432012-06-14 09:45:25 +1000347 if collectStats {
348 stats.Decode++
349 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700350
351 return err
352}
353
354// unmarshalType does the work of unmarshaling a structure.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000355func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000356 var state errorState
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700357 required, reqFields := prop.reqCount, uint64(0)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700358
Rob Pikea17fdd92011-11-02 12:43:05 -0700359 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700360 for err == nil && o.index < len(o.buf) {
361 oi := o.index
362 var u uint64
363 u, err = o.DecodeVarint()
364 if err != nil {
365 break
366 }
367 wire := int(u & 0x7)
368 if wire == WireEndGroup {
369 if is_group {
370 return nil // input is satisfied
371 }
372 return ErrWrongType
373 }
374 tag := int(u >> 3)
David Symonds6e50db52012-02-11 15:56:22 +1100375 if tag <= 0 {
376 return fmt.Errorf("proto: illegal tag %d", tag)
377 }
David Symonds2bba1b22012-09-26 14:53:08 +1000378 fieldnum, ok := prop.decoderTags.get(tag)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700379 if !ok {
380 // Maybe it's an extension?
Russ Coxd4ce3f12012-09-12 10:36:26 +1000381 if prop.extendable {
382 if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
383 if err = o.skip(st, tag, wire); err == nil {
384 ext := e.ExtensionMap()[int32(tag)] // may be missing
385 ext.enc = append(ext.enc, o.buf[oi:o.index]...)
386 e.ExtensionMap()[int32(tag)] = ext
387 }
388 continue
Rob Pikeaaa3a622010-03-20 22:32:34 -0700389 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700390 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000391 err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700392 continue
393 }
394 p := prop.Prop[fieldnum]
395
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700396 if p.dec == nil {
David Symonds6a6f82c2012-08-22 09:18:54 +1000397 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700398 continue
399 }
David Symonds5b7775e2010-12-01 10:09:04 +1100400 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700401 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100402 if wire == WireBytes && p.packedDec != nil {
403 // a packable field
404 dec = p.packedDec
405 } else {
406 err = ErrWrongType
407 continue
408 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700409 }
David Symonds4646c372013-09-09 13:18:58 +1000410 decErr := dec(o, p, base)
411 if decErr != nil && !state.shouldContinue(decErr, p) {
412 err = decErr
413 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700414 if err == nil && p.Required {
415 // Successfully decoded a required field.
416 if tag <= 64 {
417 // use bitmap for fields 1-64 to catch field reuse.
418 var mask uint64 = 1 << uint64(tag-1)
419 if reqFields&mask == 0 {
420 // new required field
421 reqFields |= mask
422 required--
423 }
424 } else {
425 // This is imprecise. It can be fooled by a required field
426 // with a tag > 64 that is encoded twice; that's very rare.
427 // A fully correct implementation would require allocating
428 // a data structure, which we would like to avoid.
429 required--
430 }
431 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700432 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700433 if err == nil {
434 if is_group {
435 return io.ErrUnexpectedEOF
436 }
David Symonds4646c372013-09-09 13:18:58 +1000437 if state.err != nil {
438 return state.err
439 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700440 if required > 0 {
David Symonds4646c372013-09-09 13:18:58 +1000441 // Not enough information to determine the exact field. If we use extra
442 // CPU, we could determine the field only if the missing required field
443 // has a tag <= 64 and we check reqFields.
444 return &ErrRequiredNotSet{"{Unknown}"}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700445 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700446 }
447 return err
448}
449
Rob Pikeaaa3a622010-03-20 22:32:34 -0700450// Individual type decoders
451// For each,
452// u is the decoded value,
453// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700454
455// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700456// The goal is modest amortization and allocation
457// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700458const (
Russ Coxd4ce3f12012-09-12 10:36:26 +1000459 boolPoolSize = 16
460 uint32PoolSize = 8
461 uint64PoolSize = 4
Rob Pike76f6ee52011-10-20 12:58:28 -0700462)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700463
464// Decode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000465func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700466 u, err := p.valDec(o)
467 if err != nil {
468 return err
469 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700470 if len(o.bools) == 0 {
471 o.bools = make([]bool, boolPoolSize)
472 }
473 o.bools[0] = u != 0
Russ Coxd4ce3f12012-09-12 10:36:26 +1000474 *structPointer_Bool(base, p.field) = &o.bools[0]
Rob Pike76f6ee52011-10-20 12:58:28 -0700475 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700476 return nil
477}
478
479// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000480func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700481 u, err := p.valDec(o)
482 if err != nil {
483 return err
484 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000485 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700486 return nil
487}
488
489// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000490func (o *Buffer) dec_int64(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 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000495 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700496 return nil
497}
498
499// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000500func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700501 s, err := o.DecodeStringBytes()
502 if err != nil {
503 return err
504 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700505 sp := new(string)
506 *sp = s
Russ Coxd4ce3f12012-09-12 10:36:26 +1000507 *structPointer_String(base, p.field) = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700508 return nil
509}
510
511// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000512func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700513 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700514 if err != nil {
515 return err
516 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000517 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700518 return nil
519}
520
521// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000522func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700523 u, err := p.valDec(o)
524 if err != nil {
525 return err
526 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000527 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700528 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100529 return nil
530}
531
532// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000533func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
534 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100535
536 nn, err := o.DecodeVarint()
537 if err != nil {
538 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700539 }
David Symonds5b7775e2010-12-01 10:09:04 +1100540 nb := int(nn) // number of bytes of encoded bools
541
Rob Pike76f6ee52011-10-20 12:58:28 -0700542 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100543 for i := 0; i < nb; i++ {
544 u, err := p.valDec(o)
545 if err != nil {
546 return err
547 }
548 y = append(y, u != 0)
549 }
550
Rob Pike76f6ee52011-10-20 12:58:28 -0700551 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700552 return nil
553}
554
555// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000556func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700557 u, err := p.valDec(o)
558 if err != nil {
559 return err
560 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000561 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100562 return nil
563}
564
565// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000566func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
567 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100568
569 nn, err := o.DecodeVarint()
570 if err != nil {
571 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700572 }
David Symonds5b7775e2010-12-01 10:09:04 +1100573 nb := int(nn) // number of bytes of encoded int32s
574
David Symonds5b7775e2010-12-01 10:09:04 +1100575 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000576 if fin < o.index {
577 return errOverflow
578 }
David Symonds5b7775e2010-12-01 10:09:04 +1100579 for o.index < fin {
580 u, err := p.valDec(o)
581 if err != nil {
582 return err
583 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000584 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100585 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700586 return nil
587}
588
589// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000590func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700591 u, err := p.valDec(o)
592 if err != nil {
593 return err
594 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700595
Russ Coxd4ce3f12012-09-12 10:36:26 +1000596 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100597 return nil
598}
599
600// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000601func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
602 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100603
604 nn, err := o.DecodeVarint()
605 if err != nil {
606 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700607 }
David Symonds5b7775e2010-12-01 10:09:04 +1100608 nb := int(nn) // number of bytes of encoded int64s
609
David Symonds5b7775e2010-12-01 10:09:04 +1100610 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000611 if fin < o.index {
612 return errOverflow
613 }
David Symonds5b7775e2010-12-01 10:09:04 +1100614 for o.index < fin {
615 u, err := p.valDec(o)
616 if err != nil {
617 return err
618 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000619 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100620 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700621 return nil
622}
623
624// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000625func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700626 s, err := o.DecodeStringBytes()
627 if err != nil {
628 return err
629 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000630 v := structPointer_StringSlice(base, p.field)
631 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700632 return nil
633}
634
635// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000636func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700637 b, err := o.DecodeRawBytes(true)
638 if err != nil {
639 return err
640 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000641 v := structPointer_BytesSlice(base, p.field)
642 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700643 return nil
644}
645
646// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000647func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
David Symonds2ce8ed42013-06-20 13:22:17 +1000648 bas := structPointer_GetStructPointer(base, p.field)
649 if structPointer_IsNil(bas) {
650 // allocate new nested message
651 bas = toStructPointer(reflect.New(p.stype))
652 structPointer_SetStructPointer(base, p.field, bas)
653 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000654 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700655}
656
657// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000658func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700659 raw, e := o.DecodeRawBytes(false)
660 if e != nil {
661 return e
662 }
663
David Symonds2ce8ed42013-06-20 13:22:17 +1000664 bas := structPointer_GetStructPointer(base, p.field)
665 if structPointer_IsNil(bas) {
666 // allocate new nested message
667 bas = toStructPointer(reflect.New(p.stype))
668 structPointer_SetStructPointer(base, p.field, bas)
669 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700670
671 // If the object can unmarshal itself, let it.
David Symonds4f8da862013-06-24 10:57:29 +1000672 if p.isUnmarshaler {
David Symonds2ce8ed42013-06-20 13:22:17 +1000673 iv := structPointer_Interface(bas, p.stype)
David Symondsa80b2822012-03-14 14:31:25 +1100674 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700675 }
676
677 obuf := o.buf
678 oi := o.index
679 o.buf = raw
680 o.index = 0
681
David Symondsc0287172012-08-15 11:10:30 +1000682 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700683 o.buf = obuf
684 o.index = oi
685
686 return err
687}
688
689// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000690func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700691 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700692}
693
694// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000695func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700696 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700697}
698
699// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000700func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
701 v := reflect.New(p.stype)
702 bas := toStructPointer(v)
703 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700704
705 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000706 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700707 return err
708 }
709
David Symondsc0287172012-08-15 11:10:30 +1000710 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700711 if err != nil {
712 return err
713 }
714
715 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100716 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000717 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100718 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700719 }
720
721 obuf := o.buf
722 oi := o.index
723 o.buf = raw
724 o.index = 0
725
David Symondsc0287172012-08-15 11:10:30 +1000726 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700727
728 o.buf = obuf
729 o.index = oi
730
731 return err
732}