blob: 4571d74a32915f509bb76ed4f8ced0f1e9fbbc02 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 Google Inc. All rights reserved.
4// 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.
Rob Pikea17fdd92011-11-02 12:43:05 -070049var ErrWrongType = errors.New("field/encoding mismatch: wrong type for field")
Rob Pikeaaa3a622010-03-20 22:32:34 -070050
51// The fundamental decoders that interpret bytes on the wire.
52// Those that take integer types all return uint64 and are
53// therefore of type valueDecoder.
54
55// DecodeVarint reads a varint-encoded integer from the slice.
56// It returns the integer and the number of bytes consumed, or
57// zero if there is not enough.
58// This is the format for the
59// int32, int64, uint32, uint64, bool, and enum
60// protocol buffer types.
61func DecodeVarint(buf []byte) (x uint64, n int) {
62 // x, n already 0
63 for shift := uint(0); ; shift += 7 {
64 if n >= len(buf) {
65 return 0, 0
66 }
67 b := uint64(buf[n])
68 n++
69 x |= (b & 0x7F) << shift
70 if (b & 0x80) == 0 {
71 break
72 }
73 }
74 return x, n
75}
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
87 for shift := uint(0); ; shift += 7 {
88 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 {
96 break
97 }
98 }
99 p.index = i
100 return
101}
102
103// DecodeFixed64 reads a 64-bit integer from the Buffer.
104// This is the format for the
105// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700106func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700107 // x, err already 0
108 i := p.index + 8
109 if i > len(p.buf) {
110 err = io.ErrUnexpectedEOF
111 return
112 }
113 p.index = i
114
115 x = uint64(p.buf[i-8])
116 x |= uint64(p.buf[i-7]) << 8
117 x |= uint64(p.buf[i-6]) << 16
118 x |= uint64(p.buf[i-5]) << 24
119 x |= uint64(p.buf[i-4]) << 32
120 x |= uint64(p.buf[i-3]) << 40
121 x |= uint64(p.buf[i-2]) << 48
122 x |= uint64(p.buf[i-1]) << 56
123 return
124}
125
126// DecodeFixed32 reads a 32-bit integer from the Buffer.
127// This is the format for the
128// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700129func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700130 // x, err already 0
131 i := p.index + 4
132 if i > len(p.buf) {
133 err = io.ErrUnexpectedEOF
134 return
135 }
136 p.index = i
137
138 x = uint64(p.buf[i-4])
139 x |= uint64(p.buf[i-3]) << 8
140 x |= uint64(p.buf[i-2]) << 16
141 x |= uint64(p.buf[i-1]) << 24
142 return
143}
144
145// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
146// from the Buffer.
147// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700148func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700149 x, err = p.DecodeVarint()
150 if err != nil {
151 return
152 }
153 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
154 return
155}
156
157// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
158// from the Buffer.
159// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700160func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700161 x, err = p.DecodeVarint()
162 if err != nil {
163 return
164 }
165 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
166 return
167}
168
169// These are not ValueDecoders: they produce an array of bytes or a string.
170// bytes, embedded messages
171
172// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
173// This is the format used for the bytes protocol buffer
174// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700175func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700176 n, err := p.DecodeVarint()
177 if err != nil {
178 return
179 }
180
181 nb := int(n)
David Symonds22ac1502012-01-18 12:37:12 +1100182 if nb < 0 {
183 return nil, fmt.Errorf("proto: bad byte length %d", nb)
184 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700185 if p.index+nb > len(p.buf) {
David Symonds22ac1502012-01-18 12:37:12 +1100186 return nil, io.ErrUnexpectedEOF
Rob Pikeaaa3a622010-03-20 22:32:34 -0700187 }
188
189 if !alloc {
190 // todo: check if can get more uses of alloc=false
191 buf = p.buf[p.index : p.index+nb]
192 p.index += nb
193 return
194 }
195
196 buf = make([]byte, nb)
197 copy(buf, p.buf[p.index:])
198 p.index += nb
199 return
200}
201
202// DecodeStringBytes reads an encoded string from the Buffer.
203// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700204func (p *Buffer) DecodeStringBytes() (s string, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700205 buf, err := p.DecodeRawBytes(false)
206 if err != nil {
207 return
208 }
209 return string(buf), nil
210}
211
212// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
213// If the protocol buffer has extensions, and the field matches, add it as an extension.
214// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000215func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700216
217 oi := o.index
218
219 err := o.skip(t, tag, wire)
220 if err != nil {
221 return err
222 }
223
Russ Coxd4ce3f12012-09-12 10:36:26 +1000224 if !unrecField.IsValid() {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700225 return nil
226 }
227
Russ Coxd4ce3f12012-09-12 10:36:26 +1000228 ptr := structPointer_Bytes(base, unrecField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700229
230 if *ptr == nil {
231 // This is the first skipped element,
232 // allocate a new buffer.
233 *ptr = o.bufalloc()
234 }
235
236 // Add the skipped field to struct field
237 obuf := o.buf
238
239 o.buf = *ptr
240 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800241 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700242
243 o.buf = obuf
244
245 return nil
246}
247
248// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700249func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700250
251 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700252 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700253
254 switch wire {
255 case WireVarint:
256 _, err = o.DecodeVarint()
257 case WireFixed64:
258 _, err = o.DecodeFixed64()
259 case WireBytes:
260 _, err = o.DecodeRawBytes(false)
261 case WireFixed32:
262 _, err = o.DecodeFixed32()
263 case WireStartGroup:
264 for {
265 u, err = o.DecodeVarint()
266 if err != nil {
267 break
268 }
269 fwire := int(u & 0x7)
270 if fwire == WireEndGroup {
271 break
272 }
273 ftag := int(u >> 3)
274 err = o.skip(t, ftag, fwire)
275 if err != nil {
276 break
277 }
278 }
279 default:
David Symonds22ac1502012-01-18 12:37:12 +1100280 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700281 }
282 return err
283}
284
285// Unmarshaler is the interface representing objects that can unmarshal themselves.
David Symondsc0287172012-08-15 11:10:30 +1000286// The argument points to data that may be overwritten, so implementations should
287// not keep references to the buffer.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700288type Unmarshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700289 Unmarshal([]byte) error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700290}
291
292// Unmarshal parses the protocol buffer representation in buf and places the
293// decoded result in pb. If the struct underlying pb does not match
294// the data in buf, the results can be unpredictable.
David Symonds19285602012-07-02 16:04:28 -0700295//
David Symonds525838c2012-07-20 15:42:49 +1000296// Unmarshal resets pb before starting to unmarshal, so any
297// existing data in pb is always removed. Use UnmarshalAppend
298// to preserve and append to existing data.
David Symonds9f60f432012-06-14 09:45:25 +1000299func Unmarshal(buf []byte, pb Message) error {
David Symonds525838c2012-07-20 15:42:49 +1000300 pb.Reset()
David Symonds19285602012-07-02 16:04:28 -0700301 return UnmarshalAppend(buf, pb)
302}
303
304// UnmarshalAppend parses the protocol buffer representation in buf and
305// writes the decoded result to pb. If the struct underlying pb does not match
306// the data in buf, the results can be unpredictable.
307//
David Symonds525838c2012-07-20 15:42:49 +1000308// UnmarshalAppend preserves and appends to existing data in pb.
309// Most code should use Unmarshal instead.
David Symonds19285602012-07-02 16:04:28 -0700310func UnmarshalAppend(buf []byte, pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700311 // If the object can unmarshal itself, let it.
312 if u, ok := pb.(Unmarshaler); ok {
313 return u.Unmarshal(buf)
314 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700315 return NewBuffer(buf).Unmarshal(pb)
316}
317
318// Unmarshal parses the protocol buffer representation in the
319// Buffer and places the decoded result in pb. If the struct
320// underlying pb does not match the data in the buffer, the results can be
321// unpredictable.
David Symonds9f60f432012-06-14 09:45:25 +1000322func (p *Buffer) Unmarshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700323 // If the object can unmarshal itself, let it.
324 if u, ok := pb.(Unmarshaler); ok {
325 err := u.Unmarshal(p.buf[p.index:])
326 p.index = len(p.buf)
327 return err
328 }
329
Rob Pikeaaa3a622010-03-20 22:32:34 -0700330 typ, base, err := getbase(pb)
331 if err != nil {
332 return err
333 }
334
David Symonds6a6f82c2012-08-22 09:18:54 +1000335 err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700336
David Symonds9f60f432012-06-14 09:45:25 +1000337 if collectStats {
338 stats.Decode++
339 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700340
341 return err
342}
343
344// unmarshalType does the work of unmarshaling a structure.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000345func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
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 }
361 return ErrWrongType
362 }
363 tag := int(u >> 3)
David Symonds6e50db52012-02-11 15:56:22 +1100364 if tag <= 0 {
365 return fmt.Errorf("proto: illegal tag %d", tag)
366 }
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 {
395 err = ErrWrongType
396 continue
397 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700398 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700399 err = dec(o, p, base)
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700400 if err == nil && p.Required {
401 // Successfully decoded a required field.
402 if tag <= 64 {
403 // use bitmap for fields 1-64 to catch field reuse.
404 var mask uint64 = 1 << uint64(tag-1)
405 if reqFields&mask == 0 {
406 // new required field
407 reqFields |= mask
408 required--
409 }
410 } else {
411 // This is imprecise. It can be fooled by a required field
412 // with a tag > 64 that is encoded twice; that's very rare.
413 // A fully correct implementation would require allocating
414 // a data structure, which we would like to avoid.
415 required--
416 }
417 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700418 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700419 if err == nil {
420 if is_group {
421 return io.ErrUnexpectedEOF
422 }
423 if required > 0 {
David Symonds5b7775e2010-12-01 10:09:04 +1100424 return &ErrRequiredNotSet{st}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700425 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700426 }
427 return err
428}
429
Rob Pikeaaa3a622010-03-20 22:32:34 -0700430// Individual type decoders
431// For each,
432// u is the decoded value,
433// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700434
435// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700436// The goal is modest amortization and allocation
437// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700438const (
Russ Coxd4ce3f12012-09-12 10:36:26 +1000439 boolPoolSize = 16
440 uint32PoolSize = 8
441 uint64PoolSize = 4
Rob Pike76f6ee52011-10-20 12:58:28 -0700442)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700443
444// Decode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000445func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700446 u, err := p.valDec(o)
447 if err != nil {
448 return err
449 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700450 if len(o.bools) == 0 {
451 o.bools = make([]bool, boolPoolSize)
452 }
453 o.bools[0] = u != 0
Russ Coxd4ce3f12012-09-12 10:36:26 +1000454 *structPointer_Bool(base, p.field) = &o.bools[0]
Rob Pike76f6ee52011-10-20 12:58:28 -0700455 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700456 return nil
457}
458
459// Decode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000460func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700461 u, err := p.valDec(o)
462 if err != nil {
463 return err
464 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000465 word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700466 return nil
467}
468
469// Decode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000470func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700471 u, err := p.valDec(o)
472 if err != nil {
473 return err
474 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000475 word64_Set(structPointer_Word64(base, p.field), o, u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700476 return nil
477}
478
479// Decode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000480func (o *Buffer) dec_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700481 s, err := o.DecodeStringBytes()
482 if err != nil {
483 return err
484 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700485 sp := new(string)
486 *sp = s
Russ Coxd4ce3f12012-09-12 10:36:26 +1000487 *structPointer_String(base, p.field) = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700488 return nil
489}
490
491// Decode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000492func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700493 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700494 if err != nil {
495 return err
496 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000497 *structPointer_Bytes(base, p.field) = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700498 return nil
499}
500
501// Decode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000502func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700503 u, err := p.valDec(o)
504 if err != nil {
505 return err
506 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000507 v := structPointer_BoolSlice(base, p.field)
Rob Pike76f6ee52011-10-20 12:58:28 -0700508 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100509 return nil
510}
511
512// Decode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000513func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
514 v := structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100515
516 nn, err := o.DecodeVarint()
517 if err != nil {
518 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700519 }
David Symonds5b7775e2010-12-01 10:09:04 +1100520 nb := int(nn) // number of bytes of encoded bools
521
Rob Pike76f6ee52011-10-20 12:58:28 -0700522 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100523 for i := 0; i < nb; i++ {
524 u, err := p.valDec(o)
525 if err != nil {
526 return err
527 }
528 y = append(y, u != 0)
529 }
530
Rob Pike76f6ee52011-10-20 12:58:28 -0700531 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700532 return nil
533}
534
535// Decode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000536func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700537 u, err := p.valDec(o)
538 if err != nil {
539 return err
540 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000541 structPointer_Word32Slice(base, p.field).Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100542 return nil
543}
544
545// Decode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000546func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
547 v := structPointer_Word32Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100548
549 nn, err := o.DecodeVarint()
550 if err != nil {
551 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700552 }
David Symonds5b7775e2010-12-01 10:09:04 +1100553 nb := int(nn) // number of bytes of encoded int32s
554
David Symonds5b7775e2010-12-01 10:09:04 +1100555 fin := o.index + nb
556 for o.index < fin {
557 u, err := p.valDec(o)
558 if err != nil {
559 return err
560 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000561 v.Append(uint32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100562 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700563 return nil
564}
565
566// Decode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000567func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700568 u, err := p.valDec(o)
569 if err != nil {
570 return err
571 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700572
Russ Coxd4ce3f12012-09-12 10:36:26 +1000573 structPointer_Word64Slice(base, p.field).Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100574 return nil
575}
576
577// Decode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000578func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
579 v := structPointer_Word64Slice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100580
581 nn, err := o.DecodeVarint()
582 if err != nil {
583 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700584 }
David Symonds5b7775e2010-12-01 10:09:04 +1100585 nb := int(nn) // number of bytes of encoded int64s
586
David Symonds5b7775e2010-12-01 10:09:04 +1100587 fin := o.index + nb
588 for o.index < fin {
589 u, err := p.valDec(o)
590 if err != nil {
591 return err
592 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000593 v.Append(u)
David Symonds5b7775e2010-12-01 10:09:04 +1100594 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700595 return nil
596}
597
598// Decode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000599func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700600 s, err := o.DecodeStringBytes()
601 if err != nil {
602 return err
603 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000604 v := structPointer_StringSlice(base, p.field)
605 *v = append(*v, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700606 return nil
607}
608
609// Decode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000610func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700611 b, err := o.DecodeRawBytes(true)
612 if err != nil {
613 return err
614 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000615 v := structPointer_BytesSlice(base, p.field)
616 *v = append(*v, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700617 return nil
618}
619
620// Decode a group.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000621func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
622 bas := toStructPointer(reflect.New(p.stype))
623 structPointer_SetStructPointer(base, p.field, bas)
624 return o.unmarshalType(p.stype, p.sprop, true, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700625}
626
627// Decode an embedded message.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000628func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700629 raw, e := o.DecodeRawBytes(false)
630 if e != nil {
631 return e
632 }
633
Russ Coxd4ce3f12012-09-12 10:36:26 +1000634 v := reflect.New(p.stype)
635 bas := toStructPointer(v)
636 structPointer_SetStructPointer(base, p.field, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700637
638 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100639 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000640 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100641 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700642 }
643
644 obuf := o.buf
645 oi := o.index
646 o.buf = raw
647 o.index = 0
648
David Symondsc0287172012-08-15 11:10:30 +1000649 err = o.unmarshalType(p.stype, p.sprop, false, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700650 o.buf = obuf
651 o.index = oi
652
653 return err
654}
655
656// Decode a slice of embedded messages.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000657func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700658 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700659}
660
661// Decode a slice of embedded groups.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000662func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700663 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700664}
665
666// Decode a slice of structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000667func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
668 v := reflect.New(p.stype)
669 bas := toStructPointer(v)
670 structPointer_StructPointerSlice(base, p.field).Append(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700671
672 if is_group {
David Symondsc0287172012-08-15 11:10:30 +1000673 err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700674 return err
675 }
676
David Symondsc0287172012-08-15 11:10:30 +1000677 raw, err := o.DecodeRawBytes(false)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700678 if err != nil {
679 return err
680 }
681
682 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100683 if p.isUnmarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000684 iv := v.Interface()
David Symondsa80b2822012-03-14 14:31:25 +1100685 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700686 }
687
688 obuf := o.buf
689 oi := o.index
690 o.buf = raw
691 o.index = 0
692
David Symondsc0287172012-08-15 11:10:30 +1000693 err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700694
695 o.buf = obuf
696 o.index = oi
697
698 return err
699}