blob: 49a487a375eb6fa26378f365edc85b173c25ffac [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
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 Symondsbaeae8b2014-06-23 09:41:27 +1000365 return fmt.Errorf("proto: %s: illegal tag %d", st, tag)
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
468// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000469func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700470 u, err := p.valDec(o)
471 if err != nil {
472 return err
473 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000474 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700475 return nil
476}
477
478// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000479func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700480 u, err := p.valDec(o)
481 if err != nil {
482 return err
483 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000484 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700485 return nil
486}
487
488// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000489func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700490 s, err := o.DecodeStringBytes()
491 if err != nil {
492 return err
493 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700494 sp := new(string)
495 *sp = s
Russ Coxd4ce3f12012-09-12 10:36:26 +1000496 *structPointer_String(base, p.field) = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700497 return nil
498}
499
500// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000501func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700502 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700503 if err != nil {
504 return err
505 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000506 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700507 return nil
508}
509
510// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000511func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700512 u, err := p.valDec(o)
513 if err != nil {
514 return err
515 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000516 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700517 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100518 return nil
519}
520
521// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000522func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
523 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100524
525 nn, err := o.DecodeVarint()
526 if err != nil {
527 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700528 }
David Symonds5b7775e2010-12-01 10:09:04 +1100529 nb := int(nn) // number of bytes of encoded bools
530
Rob Pike76f6ee52011-10-20 12:58:28 -0700531 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100532 for i := 0; i < nb; i++ {
533 u, err := p.valDec(o)
534 if err != nil {
535 return err
536 }
537 y = append(y, u != 0)
538 }
539
Rob Pike76f6ee52011-10-20 12:58:28 -0700540 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700541 return nil
542}
543
544// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000545func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700546 u, err := p.valDec(o)
547 if err != nil {
548 return err
549 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000550 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100551 return nil
552}
553
554// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000555func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
556 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100557
558 nn, err := o.DecodeVarint()
559 if err != nil {
560 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700561 }
David Symonds5b7775e2010-12-01 10:09:04 +1100562 nb := int(nn) // number of bytes of encoded int32s
563
David Symonds5b7775e2010-12-01 10:09:04 +1100564 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000565 if fin < o.index {
566 return errOverflow
567 }
David Symonds5b7775e2010-12-01 10:09:04 +1100568 for o.index < fin {
569 u, err := p.valDec(o)
570 if err != nil {
571 return err
572 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000573 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100574 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700575 return nil
576}
577
578// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000579func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700580 u, err := p.valDec(o)
581 if err != nil {
582 return err
583 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700584
Russ Coxd4ce3f12012-09-12 10:36:26 +1000585 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100586 return nil
587}
588
589// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000590func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
591 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100592
593 nn, err := o.DecodeVarint()
594 if err != nil {
595 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700596 }
David Symonds5b7775e2010-12-01 10:09:04 +1100597 nb := int(nn) // number of bytes of encoded int64s
598
David Symonds5b7775e2010-12-01 10:09:04 +1100599 fin := o.index + nb
Adam Langley28c83cb2013-07-13 14:54:54 +1000600 if fin < o.index {
601 return errOverflow
602 }
David Symonds5b7775e2010-12-01 10:09:04 +1100603 for o.index < fin {
604 u, err := p.valDec(o)
605 if err != nil {
606 return err
607 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000608 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100609 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700610 return nil
611}
612
613// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000614func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700615 s, err := o.DecodeStringBytes()
616 if err != nil {
617 return err
618 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000619 v := structPointer_StringSlice(base, p.field)
620 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700621 return nil
622}
623
624// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000625func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700626 b, err := o.DecodeRawBytes(true)
627 if err != nil {
628 return err
629 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000630 v := structPointer_BytesSlice(base, p.field)
631 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700632 return nil
633}
634
635// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000636func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
David Symonds2ce8ed42013-06-20 13:22:17 +1000637 bas := structPointer_GetStructPointer(base, p.field)
638 if structPointer_IsNil(bas) {
639 // allocate new nested message
640 bas = toStructPointer(reflect.New(p.stype))
641 structPointer_SetStructPointer(base, p.field, bas)
642 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000643 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700644}
645
646// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000647func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700648 raw, e := o.DecodeRawBytes(false)
649 if e != nil {
650 return e
651 }
652
David Symonds2ce8ed42013-06-20 13:22:17 +1000653 bas := structPointer_GetStructPointer(base, p.field)
654 if structPointer_IsNil(bas) {
655 // allocate new nested message
656 bas = toStructPointer(reflect.New(p.stype))
657 structPointer_SetStructPointer(base, p.field, bas)
658 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700659
660 // If the object can unmarshal itself, let it.
David Symonds4f8da862013-06-24 10:57:29 +1000661 if p.isUnmarshaler {
David Symonds2ce8ed42013-06-20 13:22:17 +1000662 iv := structPointer_Interface(bas, p.stype)
David Symondsa80b2822012-03-14 14:31:25 +1100663 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700664 }
665
666 obuf := o.buf
667 oi := o.index
668 o.buf = raw
669 o.index = 0
670
David Symondsc0287172012-08-15 11:10:30 +1000671 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700672 o.buf = obuf
673 o.index = oi
674
675 return err
676}
677
678// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000679func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700680 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700681}
682
683// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000684func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700685 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700686}
687
688// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000689func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
690 v := reflect.New(p.stype)
691 bas := toStructPointer(v)
692 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700693
694 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000695 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700696 return err
697 }
698
David Symondsc0287172012-08-15 11:10:30 +1000699 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700700 if err != nil {
701 return err
702 }
703
704 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100705 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000706 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100707 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700708 }
709
710 obuf := o.buf
711 oi := o.index
712 o.buf = raw
713 o.index = 0
714
David Symondsc0287172012-08-15 11:10:30 +1000715 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700716
717 o.buf = obuf
718 o.index = oi
719
720 return err
721}