blob: f84d5713e282899fa36e8ad77b9c242f169238d0 [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"
44 "runtime"
45 "unsafe"
46)
47
48// ErrWrongType occurs when the wire encoding for the field disagrees with
49// that specified in the type being decoded. This is usually caused by attempting
50// to convert an encoded protocol buffer into a struct of the wrong type.
Rob Pikea17fdd92011-11-02 12:43:05 -070051var ErrWrongType = errors.New("field/encoding mismatch: wrong type for field")
Rob Pikeaaa3a622010-03-20 22:32:34 -070052
53// The fundamental decoders that interpret bytes on the wire.
54// Those that take integer types all return uint64 and are
55// therefore of type valueDecoder.
56
57// DecodeVarint reads a varint-encoded integer from the slice.
58// It returns the integer and the number of bytes consumed, or
59// zero if there is not enough.
60// This is the format for the
61// int32, int64, uint32, uint64, bool, and enum
62// protocol buffer types.
63func DecodeVarint(buf []byte) (x uint64, n int) {
64 // x, n already 0
65 for shift := uint(0); ; shift += 7 {
66 if n >= len(buf) {
67 return 0, 0
68 }
69 b := uint64(buf[n])
70 n++
71 x |= (b & 0x7F) << shift
72 if (b & 0x80) == 0 {
73 break
74 }
75 }
76 return x, n
77}
78
79// DecodeVarint reads a varint-encoded integer from the Buffer.
80// This is the format for the
81// int32, int64, uint32, uint64, bool, and enum
82// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070083func (p *Buffer) DecodeVarint() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -070084 // x, err already 0
85
86 i := p.index
87 l := len(p.buf)
88
89 for shift := uint(0); ; shift += 7 {
90 if i >= l {
91 err = io.ErrUnexpectedEOF
92 return
93 }
94 b := p.buf[i]
95 i++
96 x |= (uint64(b) & 0x7F) << shift
97 if b < 0x80 {
98 break
99 }
100 }
101 p.index = i
102 return
103}
104
105// DecodeFixed64 reads a 64-bit integer from the Buffer.
106// This is the format for the
107// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700108func (p *Buffer) DecodeFixed64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700109 // x, err already 0
110 i := p.index + 8
111 if i > len(p.buf) {
112 err = io.ErrUnexpectedEOF
113 return
114 }
115 p.index = i
116
117 x = uint64(p.buf[i-8])
118 x |= uint64(p.buf[i-7]) << 8
119 x |= uint64(p.buf[i-6]) << 16
120 x |= uint64(p.buf[i-5]) << 24
121 x |= uint64(p.buf[i-4]) << 32
122 x |= uint64(p.buf[i-3]) << 40
123 x |= uint64(p.buf[i-2]) << 48
124 x |= uint64(p.buf[i-1]) << 56
125 return
126}
127
128// DecodeFixed32 reads a 32-bit integer from the Buffer.
129// This is the format for the
130// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700131func (p *Buffer) DecodeFixed32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700132 // x, err already 0
133 i := p.index + 4
134 if i > len(p.buf) {
135 err = io.ErrUnexpectedEOF
136 return
137 }
138 p.index = i
139
140 x = uint64(p.buf[i-4])
141 x |= uint64(p.buf[i-3]) << 8
142 x |= uint64(p.buf[i-2]) << 16
143 x |= uint64(p.buf[i-1]) << 24
144 return
145}
146
147// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
148// from the Buffer.
149// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700150func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700151 x, err = p.DecodeVarint()
152 if err != nil {
153 return
154 }
155 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
156 return
157}
158
159// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
160// from the Buffer.
161// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700162func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700163 x, err = p.DecodeVarint()
164 if err != nil {
165 return
166 }
167 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
168 return
169}
170
171// These are not ValueDecoders: they produce an array of bytes or a string.
172// bytes, embedded messages
173
174// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
175// This is the format used for the bytes protocol buffer
176// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700177func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700178 n, err := p.DecodeVarint()
179 if err != nil {
180 return
181 }
182
183 nb := int(n)
184 if p.index+nb > len(p.buf) {
185 err = io.ErrUnexpectedEOF
186 return
187 }
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.
Rob Pikea17fdd92011-11-02 12:43:05 -0700215func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base uintptr) 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
224 x := fieldIndex(t, "XXX_unrecognized")
225 if x == nil {
226 return nil
227 }
228
229 p := propByIndex(t, x)
230 ptr := (*[]byte)(unsafe.Pointer(base + p.offset))
231
232 if *ptr == nil {
233 // This is the first skipped element,
234 // allocate a new buffer.
235 *ptr = o.bufalloc()
236 }
237
238 // Add the skipped field to struct field
239 obuf := o.buf
240
241 o.buf = *ptr
242 o.EncodeVarint(uint64(tag<<3 | wire))
Rob Pike99fa2b62010-12-02 10:39:42 -0800243 *ptr = append(o.buf, obuf[oi:o.index]...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700244
245 o.buf = obuf
246
247 return nil
248}
249
250// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
Rob Pikea17fdd92011-11-02 12:43:05 -0700251func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700252
253 var u uint64
Rob Pikea17fdd92011-11-02 12:43:05 -0700254 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700255
256 switch wire {
257 case WireVarint:
258 _, err = o.DecodeVarint()
259 case WireFixed64:
260 _, err = o.DecodeFixed64()
261 case WireBytes:
262 _, err = o.DecodeRawBytes(false)
263 case WireFixed32:
264 _, err = o.DecodeFixed32()
265 case WireStartGroup:
266 for {
267 u, err = o.DecodeVarint()
268 if err != nil {
269 break
270 }
271 fwire := int(u & 0x7)
272 if fwire == WireEndGroup {
273 break
274 }
275 ftag := int(u >> 3)
276 err = o.skip(t, ftag, fwire)
277 if err != nil {
278 break
279 }
280 }
281 default:
282 fmt.Fprintf(os.Stderr, "proto: can't skip wire type %d for %s\n", wire, t)
283 }
284 return err
285}
286
287// Unmarshaler is the interface representing objects that can unmarshal themselves.
288type 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.
Rob Pikea17fdd92011-11-02 12:43:05 -0700295func Unmarshal(buf []byte, pb interface{}) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700296 // If the object can unmarshal itself, let it.
297 if u, ok := pb.(Unmarshaler); ok {
298 return u.Unmarshal(buf)
299 }
300
301 return NewBuffer(buf).Unmarshal(pb)
302}
303
304// Unmarshal parses the protocol buffer representation in the
305// Buffer and places the decoded result in pb. If the struct
306// underlying pb does not match the data in the buffer, the results can be
307// unpredictable.
Rob Pikea17fdd92011-11-02 12:43:05 -0700308func (p *Buffer) Unmarshal(pb interface{}) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700309 // If the object can unmarshal itself, let it.
310 if u, ok := pb.(Unmarshaler); ok {
311 err := u.Unmarshal(p.buf[p.index:])
312 p.index = len(p.buf)
313 return err
314 }
315
316 mstat := runtime.MemStats.Mallocs
317
318 typ, base, err := getbase(pb)
319 if err != nil {
320 return err
321 }
322
323 err = p.unmarshalType(typ, false, base)
324
325 mstat = runtime.MemStats.Mallocs - mstat
326 stats.Dmalloc += mstat
327 stats.Decode++
328
329 return err
330}
331
332// unmarshalType does the work of unmarshaling a structure.
Rob Pikea17fdd92011-11-02 12:43:05 -0700333func (o *Buffer) unmarshalType(t reflect.Type, is_group bool, base uintptr) error {
Rob Pike97e934d2011-04-11 12:52:49 -0700334 st := t.Elem()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700335 prop := GetProperties(st)
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700336 required, reqFields := prop.reqCount, uint64(0)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700337
Rob Pikea17fdd92011-11-02 12:43:05 -0700338 var err error
Rob Pikeaaa3a622010-03-20 22:32:34 -0700339 for err == nil && o.index < len(o.buf) {
340 oi := o.index
341 var u uint64
342 u, err = o.DecodeVarint()
343 if err != nil {
344 break
345 }
346 wire := int(u & 0x7)
347 if wire == WireEndGroup {
348 if is_group {
349 return nil // input is satisfied
350 }
351 return ErrWrongType
352 }
353 tag := int(u >> 3)
354 fieldnum, ok := prop.tags[tag]
355 if !ok {
356 // Maybe it's an extension?
Rob Pike76f6ee52011-10-20 12:58:28 -0700357 o.ptr = base // copy the address here to avoid a heap allocation.
Rob Pikeaaa3a622010-03-20 22:32:34 -0700358 iv := unsafe.Unreflect(t, unsafe.Pointer(&o.ptr))
359 if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) {
360 if err = o.skip(st, tag, wire); err == nil {
David Symonds1d72f7a2011-08-19 18:28:52 +1000361 e.ExtensionMap()[int32(tag)] = Extension{enc: append([]byte(nil), o.buf[oi:o.index]...)}
Rob Pikeaaa3a622010-03-20 22:32:34 -0700362 }
363 continue
364 }
365 err = o.skipAndSave(st, tag, wire, base)
366 continue
367 }
368 p := prop.Prop[fieldnum]
369
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700370 if p.dec == nil {
371 fmt.Fprintf(os.Stderr, "no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700372 continue
373 }
David Symonds5b7775e2010-12-01 10:09:04 +1100374 dec := p.dec
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700375 if wire != WireStartGroup && wire != p.WireType {
David Symonds5b7775e2010-12-01 10:09:04 +1100376 if wire == WireBytes && p.packedDec != nil {
377 // a packable field
378 dec = p.packedDec
379 } else {
380 err = ErrWrongType
381 continue
382 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700383 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700384 err = dec(o, p, base)
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700385 if err == nil && p.Required {
386 // Successfully decoded a required field.
387 if tag <= 64 {
388 // use bitmap for fields 1-64 to catch field reuse.
389 var mask uint64 = 1 << uint64(tag-1)
390 if reqFields&mask == 0 {
391 // new required field
392 reqFields |= mask
393 required--
394 }
395 } else {
396 // This is imprecise. It can be fooled by a required field
397 // with a tag > 64 that is encoded twice; that's very rare.
398 // A fully correct implementation would require allocating
399 // a data structure, which we would like to avoid.
400 required--
401 }
402 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700403 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700404 if err == nil {
405 if is_group {
406 return io.ErrUnexpectedEOF
407 }
408 if required > 0 {
David Symonds5b7775e2010-12-01 10:09:04 +1100409 return &ErrRequiredNotSet{st}
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700410 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700411 }
412 return err
413}
414
Rob Pikeaaa3a622010-03-20 22:32:34 -0700415// Individual type decoders
416// For each,
417// u is the decoded value,
418// v is a pointer to the field (pointer) in the struct
Rob Pike76f6ee52011-10-20 12:58:28 -0700419
420// Sizes of the pools to allocate inside the Buffer.
Rob Pike97edc7e2011-10-20 15:53:19 -0700421// The goal is modest amortization and allocation
422// on at least 16-byte boundaries.
Rob Pike76f6ee52011-10-20 12:58:28 -0700423const (
David Symonds049646b2011-10-21 11:13:45 +1100424 boolPoolSize = 16
Rob Pike76f6ee52011-10-20 12:58:28 -0700425 int32PoolSize = 8
426 int64PoolSize = 4
427)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700428
429// Decode a bool.
Rob Pikea17fdd92011-11-02 12:43:05 -0700430func (o *Buffer) dec_bool(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700431 u, err := p.valDec(o)
432 if err != nil {
433 return err
434 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700435 if len(o.bools) == 0 {
436 o.bools = make([]bool, boolPoolSize)
437 }
438 o.bools[0] = u != 0
439 v := (**bool)(unsafe.Pointer(base + p.offset))
440 *v = &o.bools[0]
441 o.bools = o.bools[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700442 return nil
443}
444
445// Decode an int32.
Rob Pikea17fdd92011-11-02 12:43:05 -0700446func (o *Buffer) dec_int32(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700447 u, err := p.valDec(o)
448 if err != nil {
449 return err
450 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700451 if len(o.int32s) == 0 {
452 o.int32s = make([]int32, int32PoolSize)
453 }
454 o.int32s[0] = int32(u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700455 v := (**int32)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700456 *v = &o.int32s[0]
457 o.int32s = o.int32s[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700458 return nil
459}
460
461// Decode an int64.
Rob Pikea17fdd92011-11-02 12:43:05 -0700462func (o *Buffer) dec_int64(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700463 u, err := p.valDec(o)
464 if err != nil {
465 return err
466 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700467 if len(o.int64s) == 0 {
468 o.int64s = make([]int64, int64PoolSize)
469 }
470 o.int64s[0] = int64(u)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700471 v := (**int64)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700472 *v = &o.int64s[0]
473 o.int64s = o.int64s[1:]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700474 return nil
475}
476
477// Decode a string.
Rob Pikea17fdd92011-11-02 12:43:05 -0700478func (o *Buffer) dec_string(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700479 s, err := o.DecodeStringBytes()
480 if err != nil {
481 return err
482 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700483 sp := new(string)
484 *sp = s
Rob Pikeaaa3a622010-03-20 22:32:34 -0700485 v := (**string)(unsafe.Pointer(base + p.offset))
Rob Pike76f6ee52011-10-20 12:58:28 -0700486 *v = sp
Rob Pikeaaa3a622010-03-20 22:32:34 -0700487 return nil
488}
489
490// Decode a slice of bytes ([]byte).
Rob Pikea17fdd92011-11-02 12:43:05 -0700491func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700492 b, err := o.DecodeRawBytes(true)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700493 if err != nil {
494 return err
495 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700496 v := (*[]byte)(unsafe.Pointer(base + p.offset))
497 *v = b
Rob Pikeaaa3a622010-03-20 22:32:34 -0700498 return nil
499}
500
501// Decode a slice of bools ([]bool).
Rob Pikea17fdd92011-11-02 12:43:05 -0700502func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700503 u, err := p.valDec(o)
504 if err != nil {
505 return err
506 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700507 v := (*[]bool)(unsafe.Pointer(base + p.offset))
508 *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.
Rob Pikea17fdd92011-11-02 12:43:05 -0700513func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700514 v := (*[]bool)(unsafe.Pointer(base + p.offset))
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).
Rob Pikea17fdd92011-11-02 12:43:05 -0700536func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700537 u, err := p.valDec(o)
538 if err != nil {
539 return err
540 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700541 v := (*[]int32)(unsafe.Pointer(base + p.offset))
542 *v = append(*v, int32(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100543 return nil
544}
545
546// Decode a slice of int32s ([]int32) in packed format.
Rob Pikea17fdd92011-11-02 12:43:05 -0700547func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700548 v := (*[]int32)(unsafe.Pointer(base + p.offset))
David Symonds5b7775e2010-12-01 10:09:04 +1100549
550 nn, err := o.DecodeVarint()
551 if err != nil {
552 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700553 }
David Symonds5b7775e2010-12-01 10:09:04 +1100554 nb := int(nn) // number of bytes of encoded int32s
555
Rob Pike76f6ee52011-10-20 12:58:28 -0700556 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100557
558 fin := o.index + nb
559 for o.index < fin {
560 u, err := p.valDec(o)
561 if err != nil {
562 return err
563 }
564 y = append(y, int32(u))
565 }
566
Rob Pike76f6ee52011-10-20 12:58:28 -0700567 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700568 return nil
569}
570
571// Decode a slice of int64s ([]int64).
Rob Pikea17fdd92011-11-02 12:43:05 -0700572func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700573 u, err := p.valDec(o)
574 if err != nil {
575 return err
576 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700577 v := (*[]int64)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700578
Rob Pike76f6ee52011-10-20 12:58:28 -0700579 y := *v
580 *v = append(y, int64(u))
David Symonds5b7775e2010-12-01 10:09:04 +1100581 return nil
582}
583
584// Decode a slice of int64s ([]int64) in packed format.
Rob Pikea17fdd92011-11-02 12:43:05 -0700585func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700586 v := (*[]int64)(unsafe.Pointer(base + p.offset))
David Symonds5b7775e2010-12-01 10:09:04 +1100587
588 nn, err := o.DecodeVarint()
589 if err != nil {
590 return err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700591 }
David Symonds5b7775e2010-12-01 10:09:04 +1100592 nb := int(nn) // number of bytes of encoded int64s
593
Rob Pike76f6ee52011-10-20 12:58:28 -0700594 y := *v
David Symonds5b7775e2010-12-01 10:09:04 +1100595 fin := o.index + nb
596 for o.index < fin {
597 u, err := p.valDec(o)
598 if err != nil {
599 return err
600 }
601 y = append(y, int64(u))
602 }
603
Rob Pike76f6ee52011-10-20 12:58:28 -0700604 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700605 return nil
606}
607
608// Decode a slice of strings ([]string).
Rob Pikea17fdd92011-11-02 12:43:05 -0700609func (o *Buffer) dec_slice_string(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700610 s, err := o.DecodeStringBytes()
611 if err != nil {
612 return err
613 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700614 v := (*[]string)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700615
Rob Pike76f6ee52011-10-20 12:58:28 -0700616 y := *v
617 *v = append(y, s)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700618 return nil
619}
620
621// Decode a slice of slice of bytes ([][]byte).
Rob Pikea17fdd92011-11-02 12:43:05 -0700622func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700623 b, err := o.DecodeRawBytes(true)
624 if err != nil {
625 return err
626 }
Rob Pike76f6ee52011-10-20 12:58:28 -0700627 v := (*[][]byte)(unsafe.Pointer(base + p.offset))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700628
Rob Pike76f6ee52011-10-20 12:58:28 -0700629 y := *v
630 *v = append(y, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700631 return nil
632}
633
634// Decode a group.
Rob Pikea17fdd92011-11-02 12:43:05 -0700635func (o *Buffer) dec_struct_group(p *Properties, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700636 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
Rob Pike97e934d2011-04-11 12:52:49 -0700637 typ := p.stype.Elem()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700638 structv := unsafe.New(typ)
639 bas := uintptr(structv)
640 *ptr = (*struct{})(structv)
641
642 err := o.unmarshalType(p.stype, true, bas)
643
644 return err
645}
646
647// Decode an embedded message.
Rob Pikea17fdd92011-11-02 12:43:05 -0700648func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700649 raw, e := o.DecodeRawBytes(false)
650 if e != nil {
651 return e
652 }
653
654 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
Rob Pike97e934d2011-04-11 12:52:49 -0700655 typ := p.stype.Elem()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700656 structv := unsafe.New(typ)
657 bas := uintptr(structv)
658 *ptr = (*struct{})(structv)
659
660 // If the object can unmarshal itself, let it.
661 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr))
662 if u, ok := iv.(Unmarshaler); ok {
663 return u.Unmarshal(raw)
664 }
665
666 obuf := o.buf
667 oi := o.index
668 o.buf = raw
669 o.index = 0
670
671 err = o.unmarshalType(p.stype, false, bas)
672 o.buf = obuf
673 o.index = oi
674
675 return err
676}
677
678// Decode a slice of embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700679func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) 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.
Rob Pikea17fdd92011-11-02 12:43:05 -0700684func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) 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).
Rob Pikea17fdd92011-11-02 12:43:05 -0700689func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700690
Rob Pike76f6ee52011-10-20 12:58:28 -0700691 v := (*[]*struct{})(unsafe.Pointer(base + p.offset))
692 y := *v
Rob Pikeaaa3a622010-03-20 22:32:34 -0700693
Rob Pike97e934d2011-04-11 12:52:49 -0700694 typ := p.stype.Elem()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700695 structv := unsafe.New(typ)
696 bas := uintptr(structv)
David Symonds5b7775e2010-12-01 10:09:04 +1100697 y = append(y, (*struct{})(structv))
Rob Pike76f6ee52011-10-20 12:58:28 -0700698 *v = y
Rob Pikeaaa3a622010-03-20 22:32:34 -0700699
700 if is_group {
701 err := o.unmarshalType(p.stype, is_group, bas)
702 return err
703 }
704
705 raw, err := o.DecodeRawBytes(true)
706 if err != nil {
707 return err
708 }
709
710 // If the object can unmarshal itself, let it.
David Symonds5b7775e2010-12-01 10:09:04 +1100711 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[len(y)-1]))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700712 if u, ok := iv.(Unmarshaler); ok {
713 return u.Unmarshal(raw)
714 }
715
716 obuf := o.buf
717 oi := o.index
718 o.buf = raw
719 o.index = 0
720
721 err = o.unmarshalType(p.stype, is_group, bas)
722
723 o.buf = obuf
724 o.index = oi
725
726 return err
727}