blob: 0c237e11d3e403c23ac526fbc7ec5c47283f3b47 [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 (
39 "bytes"
40 "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.
51var ErrWrongType = os.NewError("field/encoding mismatch: wrong type for field")
52
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.
83func (p *Buffer) DecodeVarint() (x uint64, err os.Error) {
84 // 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.
108func (p *Buffer) DecodeFixed64() (x uint64, err os.Error) {
109 // 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.
131func (p *Buffer) DecodeFixed32() (x uint64, err os.Error) {
132 // 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.
150func (p *Buffer) DecodeZigzag64() (x uint64, err os.Error) {
151 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.
162func (p *Buffer) DecodeZigzag32() (x uint64, err os.Error) {
163 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.
177func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err os.Error) {
178 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.
204func (p *Buffer) DecodeStringBytes() (s string, err os.Error) {
205 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.
215func (o *Buffer) skipAndSave(t *reflect.StructType, tag, wire int, base uintptr) os.Error {
216
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))
243 *ptr = bytes.Add(o.buf, obuf[oi:o.index])
244
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.
251func (o *Buffer) skip(t *reflect.StructType, tag, wire int) os.Error {
252
253 var u uint64
254 var err os.Error
255
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 {
289 Unmarshal([]byte) os.Error
290}
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.
295func Unmarshal(buf []byte, pb interface{}) os.Error {
296 // 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.
308func (p *Buffer) Unmarshal(pb interface{}) os.Error {
309 // 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.
333func (o *Buffer) unmarshalType(t *reflect.PtrType, is_group bool, base uintptr) os.Error {
334 st := t.Elem().(*reflect.StructType)
335 prop := GetProperties(st)
336 sbase := getsbase(prop) // scratch area for data items
337
338 var err os.Error
339 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?
357 o.ptr = base
358 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 {
361 e.ExtensionMap()[int32(tag)] = bytes.Add(nil, o.buf[oi:o.index])
362 }
363 continue
364 }
365 err = o.skipAndSave(st, tag, wire, base)
366 continue
367 }
368 p := prop.Prop[fieldnum]
369
370 if p.dec != nil {
371 if wire != WireStartGroup && wire != p.WireType {
372 err = ErrWrongType
373 continue
374 }
375 err = p.dec(o, p, base, sbase)
376 continue
377 }
378
379 fmt.Fprintf(os.Stderr, "no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
380 }
381 if err == nil && is_group {
382 return io.ErrUnexpectedEOF
383 }
384 return err
385}
386
387// Make *pslice have base address base, length 0, and capacity startSize.
388func initSlice(pslice unsafe.Pointer, base uintptr) {
389 sp := (*reflect.SliceHeader)(pslice)
390 sp.Data = base
391 sp.Len = 0
392 sp.Cap = startSize
393}
394
395// Individual type decoders
396// For each,
397// u is the decoded value,
398// v is a pointer to the field (pointer) in the struct
399// x is a pointer to the preallocated scratch space to hold the decoded value.
400
401// Decode a bool.
402func (o *Buffer) dec_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
403 u, err := p.valDec(o)
404 if err != nil {
405 return err
406 }
407 v := (**uint8)(unsafe.Pointer(base + p.offset))
408 x := (*uint8)(unsafe.Pointer(sbase + p.scratch))
409 *x = uint8(u)
410 *v = x
411 return nil
412}
413
414// Decode an int32.
415func (o *Buffer) dec_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
416 u, err := p.valDec(o)
417 if err != nil {
418 return err
419 }
420 v := (**int32)(unsafe.Pointer(base + p.offset))
421 x := (*int32)(unsafe.Pointer(sbase + p.scratch))
422 *x = int32(u)
423 *v = x
424 return nil
425}
426
427// Decode an int64.
428func (o *Buffer) dec_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
429 u, err := p.valDec(o)
430 if err != nil {
431 return err
432 }
433 v := (**int64)(unsafe.Pointer(base + p.offset))
434 x := (*int64)(unsafe.Pointer(sbase + p.scratch))
435 *x = int64(u)
436 *v = x
437 return nil
438}
439
440// Decode a string.
441func (o *Buffer) dec_string(p *Properties, base uintptr, sbase uintptr) os.Error {
442 s, err := o.DecodeStringBytes()
443 if err != nil {
444 return err
445 }
446 v := (**string)(unsafe.Pointer(base + p.offset))
447 x := (*string)(unsafe.Pointer(sbase + p.scratch))
448 *x = s
449 *v = x
450 return nil
451}
452
453// Decode a slice of bytes ([]byte).
454func (o *Buffer) dec_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
455 b, err := o.DecodeRawBytes(false)
456 if err != nil {
457 return err
458 }
459 lb := len(b)
460 if lb == 0 {
461 return nil
462 }
463 x := (*[]uint8)(unsafe.Pointer(base + p.offset))
464
465 y := *x
466 c := cap(y)
467 if c == 0 {
468 initSlice(unsafe.Pointer(x), sbase+p.scratch)
469 y = *x
470 c = cap(y)
471 }
472
473 l := len(y)
474 if l+lb > c {
475 // incremental growth is max(len(slice)*1.5, len(slice)+len(bytes))
476 g := l * 3 / 2
477 if l+lb > g {
478 g = l + lb
479 }
480 z := make([]uint8, l, g)
481 copy(z, y)
482 y = z
483 }
484
485 y = y[0 : l+lb]
486 copy(y[l:l+lb], b)
487
488 *x = y
489 return nil
490}
491
492// Decode a slice of bools ([]bool).
493func (o *Buffer) dec_slice_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
494 u, err := p.valDec(o)
495 if err != nil {
496 return err
497 }
498 x := (*[]bool)(unsafe.Pointer(base + p.offset))
499
500 y := *x
501 c := cap(y)
502 if c == 0 {
503 initSlice(unsafe.Pointer(x), sbase+p.scratch)
504 y = *x
505 c = cap(y)
506 }
507 l := len(y)
508 if l >= c {
509 g := l * 3 / 2
510 z := make([]bool, l, g)
511 copy(z, y)
512 y = z
513 }
514 y = y[0 : l+1]
515 y[l] = u != 0
516 *x = y
517 return nil
518}
519
520// Decode a slice of int32s ([]int32).
521func (o *Buffer) dec_slice_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
522 u, err := p.valDec(o)
523 if err != nil {
524 return err
525 }
526 x := (*[]int32)(unsafe.Pointer(base + p.offset))
527
528 y := *x
529 c := cap(y)
530 if c == 0 {
531 initSlice(unsafe.Pointer(x), sbase+p.scratch)
532 y = *x
533 c = cap(y)
534 }
535 l := len(y)
536 if l >= c {
537 g := l * 3 / 2
538 z := make([]int32, l, g)
539 copy(z, y)
540 y = z
541 }
542 y = y[0 : l+1]
543 y[l] = int32(u)
544 *x = y
545 return nil
546}
547
548// Decode a slice of int64s ([]int64).
549func (o *Buffer) dec_slice_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
550 u, err := p.valDec(o)
551 if err != nil {
552 return err
553 }
554 x := (*[]int64)(unsafe.Pointer(base + p.offset))
555
556 y := *x
557 c := cap(y)
558 if c == 0 {
559 initSlice(unsafe.Pointer(x), sbase+p.scratch)
560 y = *x
561 c = cap(y)
562 }
563 l := len(y)
564 if l >= c {
565 g := l * 3 / 2
566 z := make([]int64, l, g)
567 copy(z, y)
568 y = z
569 }
570 y = y[0 : l+1]
571 y[l] = int64(u)
572 *x = y
573 return nil
574}
575
576// Decode a slice of strings ([]string).
577func (o *Buffer) dec_slice_string(p *Properties, base uintptr, sbase uintptr) os.Error {
578 s, err := o.DecodeStringBytes()
579 if err != nil {
580 return err
581 }
582 x := (*[]string)(unsafe.Pointer(base + p.offset))
583
584 y := *x
585 c := cap(y)
586 if c == 0 {
587 initSlice(unsafe.Pointer(x), sbase+p.scratch)
588 y = *x
589 c = cap(y)
590 }
591 l := len(y)
592 if l >= c {
593 g := l * 3 / 2
594 z := make([]string, l, g)
595 copy(z, y)
596 y = z
597 }
598 y = y[0 : l+1]
599 y[l] = s
600 *x = y
601 return nil
602}
603
604// Decode a slice of slice of bytes ([][]byte).
605func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
606 b, err := o.DecodeRawBytes(true)
607 if err != nil {
608 return err
609 }
610 x := (*[][]byte)(unsafe.Pointer(base + p.offset))
611
612 y := *x
613 c := cap(y)
614 if c == 0 {
615 initSlice(unsafe.Pointer(x), sbase+p.scratch)
616 y = *x
617 c = cap(y)
618 }
619 l := len(y)
620 if l >= c {
621 g := l * 3 / 2
622 z := make([][]byte, l, g)
623 copy(z, y)
624 y = z
625 }
626 y = y[0 : l+1]
627 y[l] = b
628 *x = y
629 return nil
630}
631
632// Decode a group.
633func (o *Buffer) dec_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
634 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
635 typ := p.stype.Elem().(*reflect.StructType)
636 structv := unsafe.New(typ)
637 bas := uintptr(structv)
638 *ptr = (*struct{})(structv)
639
640 err := o.unmarshalType(p.stype, true, bas)
641
642 return err
643}
644
645// Decode an embedded message.
646func (o *Buffer) dec_struct_message(p *Properties, base uintptr, sbase uintptr) (err os.Error) {
647 raw, e := o.DecodeRawBytes(false)
648 if e != nil {
649 return e
650 }
651
652 ptr := (**struct{})(unsafe.Pointer(base + p.offset))
653 typ := p.stype.Elem().(*reflect.StructType)
654 structv := unsafe.New(typ)
655 bas := uintptr(structv)
656 *ptr = (*struct{})(structv)
657
658 // If the object can unmarshal itself, let it.
659 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr))
660 if u, ok := iv.(Unmarshaler); ok {
661 return u.Unmarshal(raw)
662 }
663
664 obuf := o.buf
665 oi := o.index
666 o.buf = raw
667 o.index = 0
668
669 err = o.unmarshalType(p.stype, false, bas)
670 o.buf = obuf
671 o.index = oi
672
673 return err
674}
675
676// Decode a slice of embedded messages.
677func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr, sbase uintptr) os.Error {
678 return o.dec_slice_struct(p, false, base, sbase)
679}
680
681// Decode a slice of embedded groups.
682func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
683 return o.dec_slice_struct(p, true, base, sbase)
684}
685
686// Decode a slice of structs ([]*struct).
687func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr, sbase uintptr) os.Error {
688
689 x := (*[]*struct{})(unsafe.Pointer(base + p.offset))
690 y := *x
691 c := cap(y)
692 if c == 0 {
693 initSlice(unsafe.Pointer(x), sbase+p.scratch)
694 y = *x
695 c = cap(y)
696 }
697
698 l := len(y)
699 if l >= c {
700 // Create a new slice with 1.5X the capacity.
701 g := l * 3 / 2
702 z := make([]*struct{}, l, g)
703 copy(z, y)
704 y = z
705 }
706 y = y[0 : l+1]
707 *x = y
708
709 typ := p.stype.Elem().(*reflect.StructType)
710 structv := unsafe.New(typ)
711 bas := uintptr(structv)
712 y[l] = (*struct{})(structv)
713
714 if is_group {
715 err := o.unmarshalType(p.stype, is_group, bas)
716 return err
717 }
718
719 raw, err := o.DecodeRawBytes(true)
720 if err != nil {
721 return err
722 }
723
724 // If the object can unmarshal itself, let it.
725 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[l]))
726 if u, ok := iv.(Unmarshaler); ok {
727 return u.Unmarshal(raw)
728 }
729
730 obuf := o.buf
731 oi := o.index
732 o.buf = raw
733 o.index = 0
734
735 err = o.unmarshalType(p.stype, is_group, bas)
736
737 o.buf = obuf
738 o.index = oi
739
740 return err
741}