blob: de885f6fd84b2ac588abcdc9731fb474458d605d [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 "unsafe"
45)
46
47// ErrWrongType occurs when the wire encoding for the field disagrees with
48// that specified in the type being decoded. This is usually caused by attempting
49// to convert an encoded protocol buffer into a struct of the wrong type.
Rob Pikea17fdd92011-11-02 12:43:05 -070050var ErrWrongType = errors.New("field/encoding mismatch: wrong type for field")
Rob Pikeaaa3a622010-03-20 22:32:34 -070051
52// The fundamental decoders that interpret bytes on the wire.
53// Those that take integer types all return uint64 and are
54// therefore of type valueDecoder.
55
56// DecodeVarint reads a varint-encoded integer from the slice.
57// It returns the integer and the number of bytes consumed, or
58// zero if there is not enough.
59// This is the format for the
60// int32, int64, uint32, uint64, bool, and enum
61// protocol buffer types.
62func DecodeVarint(buf []byte) (x uint64, n int) {
63 // x, n already 0
64 for shift := uint(0); ; shift += 7 {
65 if n >= len(buf) {
66 return 0, 0
67 }
68 b := uint64(buf[n])
69 n++
70 x |= (b & 0x7F) << shift
71 if (b & 0x80) == 0 {
72 break
73 }
74 }
75 return x, n
76}
77
78// DecodeVarint reads a varint-encoded integer from the Buffer.
79// This is the format for the
80// int32, int64, uint32, uint64, bool, and enum
81// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070082func (p *Buffer) DecodeVarint() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070083 // x, err already 0
84
85 i := p.index
86 l := len(p.buf)
87
88 for shift := uint(0); ; shift += 7 {
89 if i >= l {
90 err = io.ErrUnexpectedEOF
91 return
92 }
93 b := p.buf[i]
94 i++
95 x |= (uint64(b) & 0x7F) << shift
96 if b < 0x80 {
97 break
98 }
99 }
100 p.index = i
101 return
102}
103
104// DecodeFixed64 reads a 64-bit integer from the Buffer.
105// This is the format for the
106// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700107func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700108 // x, err already 0
109 i := p.index + 8
110 if i > len(p.buf) {
111 err = io.ErrUnexpectedEOF
112 return
113 }
114 p.index = i
115
116 x = uint64(p.buf[i-8])
117 x |= uint64(p.buf[i-7]) << 8
118 x |= uint64(p.buf[i-6]) << 16
119 x |= uint64(p.buf[i-5]) << 24
120 x |= uint64(p.buf[i-4]) << 32
121 x |= uint64(p.buf[i-3]) << 40
122 x |= uint64(p.buf[i-2]) << 48
123 x |= uint64(p.buf[i-1]) << 56
124 return
125}
126
127// DecodeFixed32 reads a 32-bit integer from the Buffer.
128// This is the format for the
129// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700130func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700131 // x, err already 0
132 i := p.index + 4
133 if i > len(p.buf) {
134 err = io.ErrUnexpectedEOF
135 return
136 }
137 p.index = i
138
139 x = uint64(p.buf[i-4])
140 x |= uint64(p.buf[i-3]) << 8
141 x |= uint64(p.buf[i-2]) << 16
142 x |= uint64(p.buf[i-1]) << 24
143 return
144}
145
146// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
147// from the Buffer.
148// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700149func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700150 x, err = p.DecodeVarint()
151 if err != nil {
152 return
153 }
154 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
155 return
156}
157
158// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
159// from the Buffer.
160// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700161func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700162 x, err = p.DecodeVarint()
163 if err != nil {
164 return
165 }
166 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
167 return
168}
169
170// These are not ValueDecoders: they produce an array of bytes or a string.
171// bytes, embedded messages
172
173// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
174// This is the format used for the bytes protocol buffer
175// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700176func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700177 n, err := p.DecodeVarint()
178 if err != nil {
179 return
180 }
181
182 nb := int(n)
David Symonds22ac1502012-01-18 12:37:12 +1100183 if nb < 0 {
184 return nil, fmt.Errorf("proto: bad byte length %d", nb)
185 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700186 if p.index+nb > len(p.buf) {
David Symonds22ac1502012-01-18 12:37:12 +1100187 return nil, io.ErrUnexpectedEOF
Rob Pikeaaa3a622010-03-20 22:32:34 -0700188 }
189
190 if !alloc {
191 // todo: check if can get more uses of alloc=false
192 buf = p.buf[p.index : p.index+nb]
193 p.index += nb
194 return
195 }
196
197 buf = make([]byte, nb)
198 copy(buf, p.buf[p.index:])
199 p.index += nb
200 return
201}
202
203// DecodeStringBytes reads an encoded string from the Buffer.
204// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700205func (p *Buffer) DecodeStringBytes() (s string, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700206 buf, err := p.DecodeRawBytes(false)
207 if err != nil {
208 return
209 }
210 return string(buf), nil
211}
212
213// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
214// If the protocol buffer has extensions, and the field matches, add it as an extension.
215// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
Rob Pikea17fdd92011-11-02 12:43:05 -0700216func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700217
218 oi := o.index
219
220 err := o.skip(t, tag, wire)
221 if err != nil {
222 return err
223 }
224
225 x := fieldIndex(t, "XXX_unrecognized")
226 if x == nil {
227 return nil
228 }
229
230 p := propByIndex(t, x)
231 ptr := (*[]byte)(unsafe.Pointer(base + p.offset))
232
233 if *ptr == nil {
234 // This is the first skipped element,
235 // allocate a new buffer.
236 *ptr = o.bufalloc()
237 }
238
239 // Add the skipped field to struct field
240 obuf := o.buf
241
242 o.buf = *ptr
243 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800244 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700245
246 o.buf = obuf
247
248 return nil
249}
250
251// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700252func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700253
254 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700255 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700256
257 switch wire {
258 case WireVarint:
259 _, err = o.DecodeVarint()
260 case WireFixed64:
261 _, err = o.DecodeFixed64()
262 case WireBytes:
263 _, err = o.DecodeRawBytes(false)
264 case WireFixed32:
265 _, err = o.DecodeFixed32()
266 case WireStartGroup:
267 for {
268 u, err = o.DecodeVarint()
269 if err != nil {
270 break
271 }
272 fwire := int(u & 0x7)
273 if fwire == WireEndGroup {
274 break
275 }
276 ftag := int(u >> 3)
277 err = o.skip(t, ftag, fwire)
278 if err != nil {
279 break
280 }
281 }
282 default:
David Symonds22ac1502012-01-18 12:37:12 +1100283 err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700284 }
285 return err
286}
287
288// Unmarshaler is the interface representing objects that can unmarshal themselves.
289type Unmarshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700290 Unmarshal([]byte) error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700291}
292
293// Unmarshal parses the protocol buffer representation in buf and places the
294// decoded result in pb. If the struct underlying pb does not match
295// the data in buf, the results can be unpredictable.
Rob Pikea17fdd92011-11-02 12:43:05 -0700296func Unmarshal(buf []byte, pb interface{}) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700297 // If the object can unmarshal itself, let it.
298 if u, ok := pb.(Unmarshaler); ok {
299 return u.Unmarshal(buf)
300 }
301
302 return NewBuffer(buf).Unmarshal(pb)
303}
304
305// Unmarshal parses the protocol buffer representation in the
306// Buffer and places the decoded result in pb. If the struct
307// underlying pb does not match the data in the buffer, the results can be
308// unpredictable.
Rob Pikea17fdd92011-11-02 12:43:05 -0700309func (p *Buffer) Unmarshal(pb interface{}) 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 err := u.Unmarshal(p.buf[p.index:])
313 p.index = len(p.buf)
314 return err
315 }
316
Rob Pikeaaa3a622010-03-20 22:32:34 -0700317 typ, base, err := getbase(pb)
318 if err != nil {
319 return err
320 }
321
322 err = p.unmarshalType(typ, false, base)
323
Rob Pikeaaa3a622010-03-20 22:32:34 -0700324 stats.Decode++
325
326 return err
327}
328
329// unmarshalType does the work of unmarshaling a structure.
Rob Pikea17fdd92011-11-02 12:43:05 -0700330func (o *Buffer) unmarshalType(t reflect.Type, is_group bool, base uintptr) error {
Rob Pike97e934d2011-04-11 12:52:49 -0700331 st := t.Elem()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700332 prop := GetProperties(st)
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700333 required, reqFields := prop.reqCount, uint64(0)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700334
Rob Pikea17fdd92011-11-02 12:43:05 -0700335 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700336 for err == nil && o.index < len(o.buf) {
337 oi := o.index
338 var u uint64
339 u, err = o.DecodeVarint()
340 if err != nil {
341 break
342 }
343 wire := int(u & 0x7)
344 if wire == WireEndGroup {
345 if is_group {
346 return nil // input is satisfied
347 }
348 return ErrWrongType
349 }
350 tag := int(u >> 3)
David Symonds6e50db52012-02-11 15:56:22 +1100351 if tag <= 0 {
352 return fmt.Errorf("proto: illegal tag %d", tag)
353 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700354 fieldnum, ok := prop.tags[tag]
355 if !ok {
356 // Maybe it's an extension?
David Symondsa80b2822012-03-14 14:31:25 +1100357 iv := reflect.NewAt(st, unsafe.Pointer(base)).Interface()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700358 if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) {
359 if err = o.skip(st, tag, wire); err == nil {
David Symonds61826da2012-05-05 09:31:28 +1000360 ext := e.ExtensionMap()[int32(tag)] // may be missing
361 ext.enc = append(ext.enc, o.buf[oi:o.index]...)
362 e.ExtensionMap()[int32(tag)] = ext
Rob Pikeaaa3a622010-03-20 22:32:34 -0700363 }
364 continue
365 }
366 err = o.skipAndSave(st, tag, wire, base)
367 continue
368 }
369 p := prop.Prop[fieldnum]
370
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700371 if p.dec == nil {
David Symonds6e50db52012-02-11 15:56:22 +1100372 fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700373 continue
374 }
David Symonds5b7775e2010-12-01 10:09:04 +1100375 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700376 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100377 if wire == WireBytes && p.packedDec != nil {
378 // a packable field
379 dec = p.packedDec
380 } else {
381 err = ErrWrongType
382 continue
383 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700384 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700385 err = dec(o, p, base)
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700386 if err == nil && p.Required {
387 // Successfully decoded a required field.
388 if tag <= 64 {
389 // use bitmap for fields 1-64 to catch field reuse.
390 var mask uint64 = 1 << uint64(tag-1)
391 if reqFields&mask == 0 {
392 // new required field
393 reqFields |= mask
394 required--
395 }
396 } else {
397 // This is imprecise. It can be fooled by a required field
398 // with a tag > 64 that is encoded twice; that's very rare.
399 // A fully correct implementation would require allocating
400 // a data structure, which we would like to avoid.
401 required--
402 }
403 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700404 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700405 if err == nil {
406 if is_group {
407 return io.ErrUnexpectedEOF
408 }
409 if required > 0 {
David Symonds5b7775e2010-12-01 10:09:04 +1100410 return &ErrRequiredNotSet{st}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700411 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700412 }
413 return err
414}
415
Rob Pikeaaa3a622010-03-20 22:32:34 -0700416// Individual type decoders
417// For each,
418// u is the decoded value,
419// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700420
421// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700422// The goal is modest amortization and allocation
423// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700424const (
David Symonds049646b2011-10-21 11:13:45 +1100425 boolPoolSize = 16
Rob Pike76f6ee52011-10-20 12:58:28 -0700426 int32PoolSize = 8
427 int64PoolSize = 4
428)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700429
430// Decode a bool.
Rob Pikea17fdd92011-11-02 12:43:05 -0700431func (o *Buffer) dec_bool(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700432 u, err := p.valDec(o)
433 if err != nil {
434 return err
435 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700436 if len(o.bools) == 0 {
437 o.bools = make([]bool, boolPoolSize)
438 }
439 o.bools[0] = u != 0
440 v := (**bool)(unsafe.Pointer(base + p.offset))
441 *v = &o.bools[0]
442 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700443 return nil
444}
445
446// Decode an int32.
Rob Pikea17fdd92011-11-02 12:43:05 -0700447func (o *Buffer) dec_int32(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700448 u, err := p.valDec(o)
449 if err != nil {
450 return err
451 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700452 if len(o.int32s) == 0 {
453 o.int32s = make([]int32, int32PoolSize)
454 }
455 o.int32s[0] = int32(u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700456 v := (**int32)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700457 *v = &o.int32s[0]
458 o.int32s = o.int32s[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700459 return nil
460}
461
462// Decode an int64.
Rob Pikea17fdd92011-11-02 12:43:05 -0700463func (o *Buffer) dec_int64(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700464 u, err := p.valDec(o)
465 if err != nil {
466 return err
467 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700468 if len(o.int64s) == 0 {
469 o.int64s = make([]int64, int64PoolSize)
470 }
471 o.int64s[0] = int64(u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700472 v := (**int64)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700473 *v = &o.int64s[0]
474 o.int64s = o.int64s[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700475 return nil
476}
477
478// Decode a string.
Rob Pikea17fdd92011-11-02 12:43:05 -0700479func (o *Buffer) dec_string(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700480 s, err := o.DecodeStringBytes()
481 if err != nil {
482 return err
483 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700484 sp := new(string)
485 *sp = s
Rob Pikeaaa3a622010-03-20 22:32:34 -0700486 v := (**string)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700487 *v = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700488 return nil
489}
490
491// Decode a slice of bytes ([]byte).
Rob Pikea17fdd92011-11-02 12:43:05 -0700492func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) 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 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700497 v := (*[]byte)(unsafe.Pointer(base + p.offset))
498 *v = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700499 return nil
500}
501
502// Decode a slice of bools ([]bool).
Rob Pikea17fdd92011-11-02 12:43:05 -0700503func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700504 u, err := p.valDec(o)
505 if err != nil {
506 return err
507 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700508 v := (*[]bool)(unsafe.Pointer(base + p.offset))
509 *v = append(*v, u != 0)
David Symonds5b7775e2010-12-01 10:09:04 +1100510 return nil
511}
512
513// Decode a slice of bools ([]bool) in packed format.
Rob Pikea17fdd92011-11-02 12:43:05 -0700514func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700515 v := (*[]bool)(unsafe.Pointer(base + p.offset))
David Symonds5b7775e2010-12-01 10:09:04 +1100516
517 nn, err := o.DecodeVarint()
518 if err != nil {
519 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700520 }
David Symonds5b7775e2010-12-01 10:09:04 +1100521 nb := int(nn) // number of bytes of encoded bools
522
Rob Pike76f6ee52011-10-20 12:58:28 -0700523 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100524 for i := 0; i < nb; i++ {
525 u, err := p.valDec(o)
526 if err != nil {
527 return err
528 }
529 y = append(y, u != 0)
530 }
531
Rob Pike76f6ee52011-10-20 12:58:28 -0700532 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700533 return nil
534}
535
536// Decode a slice of int32s ([]int32).
Rob Pikea17fdd92011-11-02 12:43:05 -0700537func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700538 u, err := p.valDec(o)
539 if err != nil {
540 return err
541 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700542 v := (*[]int32)(unsafe.Pointer(base + p.offset))
543 *v = append(*v, int32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100544 return nil
545}
546
547// Decode a slice of int32s ([]int32) in packed format.
Rob Pikea17fdd92011-11-02 12:43:05 -0700548func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700549 v := (*[]int32)(unsafe.Pointer(base + p.offset))
David Symonds5b7775e2010-12-01 10:09:04 +1100550
551 nn, err := o.DecodeVarint()
552 if err != nil {
553 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700554 }
David Symonds5b7775e2010-12-01 10:09:04 +1100555 nb := int(nn) // number of bytes of encoded int32s
556
Rob Pike76f6ee52011-10-20 12:58:28 -0700557 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100558
559 fin := o.index + nb
560 for o.index < fin {
561 u, err := p.valDec(o)
562 if err != nil {
563 return err
564 }
565 y = append(y, int32(u))
566 }
567
Rob Pike76f6ee52011-10-20 12:58:28 -0700568 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700569 return nil
570}
571
572// Decode a slice of int64s ([]int64).
Rob Pikea17fdd92011-11-02 12:43:05 -0700573func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700574 u, err := p.valDec(o)
575 if err != nil {
576 return err
577 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700578 v := (*[]int64)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700579
Rob Pike76f6ee52011-10-20 12:58:28 -0700580 y := *v
581 *v = append(y, int64(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100582 return nil
583}
584
585// Decode a slice of int64s ([]int64) in packed format.
Rob Pikea17fdd92011-11-02 12:43:05 -0700586func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700587 v := (*[]int64)(unsafe.Pointer(base + p.offset))
David Symonds5b7775e2010-12-01 10:09:04 +1100588
589 nn, err := o.DecodeVarint()
590 if err != nil {
591 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700592 }
David Symonds5b7775e2010-12-01 10:09:04 +1100593 nb := int(nn) // number of bytes of encoded int64s
594
Rob Pike76f6ee52011-10-20 12:58:28 -0700595 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100596 fin := o.index + nb
597 for o.index < fin {
598 u, err := p.valDec(o)
599 if err != nil {
600 return err
601 }
602 y = append(y, int64(u))
603 }
604
Rob Pike76f6ee52011-10-20 12:58:28 -0700605 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700606 return nil
607}
608
609// Decode a slice of strings ([]string).
Rob Pikea17fdd92011-11-02 12:43:05 -0700610func (o *Buffer) dec_slice_string(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700611 s, err := o.DecodeStringBytes()
612 if err != nil {
613 return err
614 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700615 v := (*[]string)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700616
Rob Pike76f6ee52011-10-20 12:58:28 -0700617 y := *v
618 *v = append(y, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700619 return nil
620}
621
622// Decode a slice of slice of bytes ([][]byte).
Rob Pikea17fdd92011-11-02 12:43:05 -0700623func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700624 b, err := o.DecodeRawBytes(true)
625 if err != nil {
626 return err
627 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700628 v := (*[][]byte)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700629
Rob Pike76f6ee52011-10-20 12:58:28 -0700630 y := *v
631 *v = append(y, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700632 return nil
633}
634
635// Decode a group.
Rob Pikea17fdd92011-11-02 12:43:05 -0700636func (o *Buffer) dec_struct_group(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700637 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
Rob Pike97e934d2011-04-11 12:52:49 -0700638 typ := p.stype.Elem()
Rob Pike7a788132012-02-14 08:14:14 +1100639 bas := reflect.New(typ).Pointer()
640 structv := unsafe.Pointer(bas)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700641 *ptr = (*struct{})(structv)
642
643 err := o.unmarshalType(p.stype, true, bas)
644
645 return err
646}
647
648// Decode an embedded message.
Rob Pikea17fdd92011-11-02 12:43:05 -0700649func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700650 raw, e := o.DecodeRawBytes(false)
651 if e != nil {
652 return e
653 }
654
655 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
Rob Pike97e934d2011-04-11 12:52:49 -0700656 typ := p.stype.Elem()
Rob Pike7a788132012-02-14 08:14:14 +1100657 bas := reflect.New(typ).Pointer()
David Symondsa80b2822012-03-14 14:31:25 +1100658 structp := unsafe.Pointer(bas)
659 *ptr = (*struct{})(structp)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700660
661 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100662 if p.isMarshaler {
663 iv := reflect.NewAt(p.stype.Elem(), structp).Interface()
664 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700665 }
666
667 obuf := o.buf
668 oi := o.index
669 o.buf = raw
670 o.index = 0
671
672 err = o.unmarshalType(p.stype, false, bas)
673 o.buf = obuf
674 o.index = oi
675
676 return err
677}
678
679// Decode a slice of embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700680func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700681 return o.dec_slice_struct(p, false, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700682}
683
684// Decode a slice of embedded groups.
Rob Pikea17fdd92011-11-02 12:43:05 -0700685func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700686 return o.dec_slice_struct(p, true, base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700687}
688
689// Decode a slice of structs ([]*struct).
Rob Pikea17fdd92011-11-02 12:43:05 -0700690func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700691
Rob Pike76f6ee52011-10-20 12:58:28 -0700692 v := (*[]*struct{})(unsafe.Pointer(base + p.offset))
693 y := *v
Rob Pikeaaa3a622010-03-20 22:32:34 -0700694
Rob Pike97e934d2011-04-11 12:52:49 -0700695 typ := p.stype.Elem()
Rob Pike7a788132012-02-14 08:14:14 +1100696 bas := reflect.New(typ).Pointer()
David Symondsa80b2822012-03-14 14:31:25 +1100697 structp := unsafe.Pointer(bas)
698 y = append(y, (*struct{})(structp))
Rob Pike76f6ee52011-10-20 12:58:28 -0700699 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700700
701 if is_group {
702 err := o.unmarshalType(p.stype, is_group, bas)
703 return err
704 }
705
706 raw, err := o.DecodeRawBytes(true)
707 if err != nil {
708 return err
709 }
710
711 // If the object can unmarshal itself, let it.
David Symondsa80b2822012-03-14 14:31:25 +1100712 if p.isUnmarshaler {
713 iv := reflect.NewAt(typ, structp).Interface()
714 return iv.(Unmarshaler).Unmarshal(raw)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700715 }
716
717 obuf := o.buf
718 oi := o.index
719 o.buf = raw
720 o.index = 0
721
722 err = o.unmarshalType(p.stype, is_group, bas)
723
724 o.buf = obuf
725 o.index = oi
726
727 return err
728}