1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 Google Inc. All rights reserved.
4 // http://code.google.com/p/goprotobuf/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
35 * Routines for encoding data into the wire format for protocol buffers.
45 // ErrRequiredNotSet is the error returned if Marshal is called with
46 // a protocol buffer struct whose required fields have not
47 // all been initialized. It is also the error returned if Unmarshal is
48 // called with an encoded protocol buffer that does not include all the
50 type ErrRequiredNotSet struct {
54 func (e *ErrRequiredNotSet) String() string {
55 return "proto: required fields not set in " + e.t.String()
59 // ErrRepeatedHasNil is the error returned if Marshal is called with
60 // a struct with a repeated field containing a nil element.
61 ErrRepeatedHasNil = os.NewError("proto: repeated field has nil")
63 // ErrNil is the error returned if Marshal is called with nil.
64 ErrNil = os.NewError("proto: Marshal called with nil")
66 // ErrNotPtr is the error returned if Marshal is called with a non-pointer.
67 ErrNotPtr = os.NewError("proto: Marshal called with a non-pointer")
70 // The fundamental encoders that put bytes on the wire.
71 // Those that take integer types all accept uint64 and are
72 // therefore of type valueEncoder.
74 const maxVarintBytes = 10 // maximum length of a varint
76 // EncodeVarint returns the varint encoding of x.
77 // This is the format for the
78 // int32, int64, uint32, uint64, bool, and enum
79 // protocol buffer types.
80 // Not used by the package itself, but helpful to clients
81 // wishing to use the same encoding.
82 func EncodeVarint(x uint64) []byte {
83 var buf [maxVarintBytes]byte
85 for n = 0; x > 127; n++ {
86 buf[n] = 0x80 | uint8(x&0x7F)
94 // EncodeVarint writes a varint-encoded integer to the Buffer.
95 // This is the format for the
96 // int32, int64, uint32, uint64, bool, and enum
97 // protocol buffer types.
98 func (p *Buffer) EncodeVarint(x uint64) os.Error {
100 p.buf = append(p.buf, uint8(x&0x7f|0x80))
103 p.buf = append(p.buf, uint8(x))
107 // EncodeFixed64 writes a 64-bit integer to the Buffer.
108 // This is the format for the
109 // fixed64, sfixed64, and double protocol buffer types.
110 func (p *Buffer) EncodeFixed64(x uint64) os.Error {
111 p.buf = append(p.buf,
123 // EncodeFixed32 writes a 32-bit integer to the Buffer.
124 // This is the format for the
125 // fixed32, sfixed32, and float protocol buffer types.
126 func (p *Buffer) EncodeFixed32(x uint64) os.Error {
127 p.buf = append(p.buf,
135 // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
137 // This is the format used for the sint64 protocol buffer type.
138 func (p *Buffer) EncodeZigzag64(x uint64) os.Error {
139 // use signed number to get arithmetic right shift.
140 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
143 // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
145 // This is the format used for the sint32 protocol buffer type.
146 func (p *Buffer) EncodeZigzag32(x uint64) os.Error {
147 // use signed number to get arithmetic right shift.
148 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
151 // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
152 // This is the format used for the bytes protocol buffer
153 // type and for embedded messages.
154 func (p *Buffer) EncodeRawBytes(b []byte) os.Error {
156 p.EncodeVarint(uint64(lb))
157 p.buf = append(p.buf, b...)
161 // EncodeStringBytes writes an encoded string to the Buffer.
162 // This is the format used for the proto2 string type.
163 func (p *Buffer) EncodeStringBytes(s string) os.Error {
165 // this works because strings and slices are the same.
166 y := *(*[]byte)(unsafe.Pointer(&s))
171 // Marshaler is the interface representing objects that can marshal themselves.
172 type Marshaler interface {
173 Marshal() ([]byte, os.Error)
176 // Marshal takes the protocol buffer struct represented by pb
177 // and encodes it into the wire format, returning the data.
178 func Marshal(pb interface{}) ([]byte, os.Error) {
179 // Can the object marshal itself?
180 if m, ok := pb.(Marshaler); ok {
191 // Marshal takes the protocol buffer struct represented by pb
192 // and encodes it into the wire format, writing the result to the
194 func (p *Buffer) Marshal(pb interface{}) os.Error {
195 // Can the object marshal itself?
196 if m, ok := pb.(Marshaler); ok {
197 data, err := m.Marshal()
201 p.buf = append(p.buf, data...)
205 mstat := runtime.MemStats.Mallocs
207 t, b, err := getbase(pb)
208 if t.Kind() != reflect.Ptr {
212 err = p.enc_struct(t.Elem(), b)
215 mstat = runtime.MemStats.Mallocs - mstat
216 stats.Emalloc += mstat
222 // Individual type encoders.
225 func (o *Buffer) enc_bool(p *Properties, base uintptr) os.Error {
226 v := *(**uint8)(unsafe.Pointer(base + p.offset))
234 o.buf = append(o.buf, p.tagcode...)
235 p.valEnc(o, uint64(x))
240 func (o *Buffer) enc_int32(p *Properties, base uintptr) os.Error {
241 v := *(**uint32)(unsafe.Pointer(base + p.offset))
246 o.buf = append(o.buf, p.tagcode...)
247 p.valEnc(o, uint64(x))
252 func (o *Buffer) enc_int64(p *Properties, base uintptr) os.Error {
253 v := *(**uint64)(unsafe.Pointer(base + p.offset))
258 o.buf = append(o.buf, p.tagcode...)
259 p.valEnc(o, uint64(x))
264 func (o *Buffer) enc_string(p *Properties, base uintptr) os.Error {
265 v := *(**string)(unsafe.Pointer(base + p.offset))
270 o.buf = append(o.buf, p.tagcode...)
271 o.EncodeStringBytes(x)
275 // All protocol buffer fields are nillable, but be careful.
276 func isNil(v reflect.Value) bool {
278 case reflect.Map, reflect.Ptr, reflect.Slice:
284 // Encode a message struct.
285 func (o *Buffer) enc_struct_message(p *Properties, base uintptr) os.Error {
286 // Can the object marshal itself?
287 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(base+p.offset))
288 if m, ok := iv.(Marshaler); ok {
289 if isNil(reflect.ValueOf(iv)) {
292 data, err := m.Marshal()
296 o.buf = append(o.buf, p.tagcode...)
297 o.EncodeRawBytes(data)
300 v := *(**struct{})(unsafe.Pointer(base + p.offset))
305 // need the length before we can write out the message itself,
306 // so marshal into a separate byte buffer first.
310 b := uintptr(unsafe.Pointer(v))
311 typ := p.stype.Elem()
312 err := o.enc_struct(typ, b)
320 o.buf = append(o.buf, p.tagcode...)
321 o.EncodeRawBytes(nbuf)
326 // Encode a group struct.
327 func (o *Buffer) enc_struct_group(p *Properties, base uintptr) os.Error {
328 v := *(**struct{})(unsafe.Pointer(base + p.offset))
333 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
334 b := uintptr(unsafe.Pointer(v))
335 typ := p.stype.Elem()
336 err := o.enc_struct(typ, b)
340 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
344 // Encode a slice of bools ([]bool).
345 func (o *Buffer) enc_slice_bool(p *Properties, base uintptr) os.Error {
346 s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
351 for _, x := range s {
352 o.buf = append(o.buf, p.tagcode...)
356 p.valEnc(o, uint64(x))
361 // Encode a slice of bools ([]bool) in packed format.
362 func (o *Buffer) enc_slice_packed_bool(p *Properties, base uintptr) os.Error {
363 s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
368 o.buf = append(o.buf, p.tagcode...)
369 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
370 for _, x := range s {
374 p.valEnc(o, uint64(x))
379 // Encode a slice of bytes ([]byte).
380 func (o *Buffer) enc_slice_byte(p *Properties, base uintptr) os.Error {
381 s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
385 o.buf = append(o.buf, p.tagcode...)
390 // Encode a slice of int32s ([]int32).
391 func (o *Buffer) enc_slice_int32(p *Properties, base uintptr) os.Error {
392 s := *(*[]uint32)(unsafe.Pointer(base + p.offset))
397 for i := 0; i < l; i++ {
398 o.buf = append(o.buf, p.tagcode...)
400 p.valEnc(o, uint64(x))
405 // Encode a slice of int32s ([]int32) in packed format.
406 func (o *Buffer) enc_slice_packed_int32(p *Properties, base uintptr) os.Error {
407 s := *(*[]uint32)(unsafe.Pointer(base + p.offset))
412 // TODO: Reuse a Buffer.
413 buf := NewBuffer(nil)
414 for i := 0; i < l; i++ {
415 p.valEnc(buf, uint64(s[i]))
418 o.buf = append(o.buf, p.tagcode...)
419 o.EncodeVarint(uint64(len(buf.buf)))
420 o.buf = append(o.buf, buf.buf...)
424 // Encode a slice of int64s ([]int64).
425 func (o *Buffer) enc_slice_int64(p *Properties, base uintptr) os.Error {
426 s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
431 for i := 0; i < l; i++ {
432 o.buf = append(o.buf, p.tagcode...)
434 p.valEnc(o, uint64(x))
439 // Encode a slice of int64s ([]int64) in packed format.
440 func (o *Buffer) enc_slice_packed_int64(p *Properties, base uintptr) os.Error {
441 s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
446 // TODO: Reuse a Buffer.
447 buf := NewBuffer(nil)
448 for i := 0; i < l; i++ {
452 o.buf = append(o.buf, p.tagcode...)
453 o.EncodeVarint(uint64(len(buf.buf)))
454 o.buf = append(o.buf, buf.buf...)
458 // Encode a slice of slice of bytes ([][]byte).
459 func (o *Buffer) enc_slice_slice_byte(p *Properties, base uintptr) os.Error {
460 ss := *(*[][]uint8)(unsafe.Pointer(base + p.offset))
465 for i := 0; i < l; i++ {
466 o.buf = append(o.buf, p.tagcode...)
473 // Encode a slice of strings ([]string).
474 func (o *Buffer) enc_slice_string(p *Properties, base uintptr) os.Error {
475 ss := *(*[]string)(unsafe.Pointer(base + p.offset))
477 for i := 0; i < l; i++ {
478 o.buf = append(o.buf, p.tagcode...)
480 o.EncodeStringBytes(s)
485 // Encode a slice of message structs ([]*struct).
486 func (o *Buffer) enc_slice_struct_message(p *Properties, base uintptr) os.Error {
487 s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
489 typ := p.stype.Elem()
491 for i := 0; i < l; i++ {
494 return ErrRepeatedHasNil
497 // Can the object marshal itself?
498 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&s[i]))
499 if m, ok := iv.(Marshaler); ok {
500 if isNil(reflect.ValueOf(iv)) {
503 data, err := m.Marshal()
507 o.buf = append(o.buf, p.tagcode...)
508 o.EncodeRawBytes(data)
515 b := uintptr(unsafe.Pointer(v))
516 err := o.enc_struct(typ, b)
523 return ErrRepeatedHasNil
527 o.buf = append(o.buf, p.tagcode...)
528 o.EncodeRawBytes(nbuf)
535 // Encode a slice of group structs ([]*struct).
536 func (o *Buffer) enc_slice_struct_group(p *Properties, base uintptr) os.Error {
537 s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
539 typ := p.stype.Elem()
541 for i := 0; i < l; i++ {
544 return ErrRepeatedHasNil
547 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
549 b := uintptr(unsafe.Pointer(v))
550 err := o.enc_struct(typ, b)
554 return ErrRepeatedHasNil
559 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
564 // Encode an extension map.
565 func (o *Buffer) enc_map(p *Properties, base uintptr) os.Error {
566 v := *(*map[int32]Extension)(unsafe.Pointer(base + p.offset))
567 if err := encodeExtensionMap(v); err != nil {
570 for _, e := range v {
571 o.buf = append(o.buf, e.enc...)
577 func (o *Buffer) enc_struct(t reflect.Type, base uintptr) os.Error {
578 prop := GetProperties(t)
579 required := prop.reqCount
580 for _, p := range prop.Prop {
582 err := p.enc(o, p, base)
587 } else if p.Required {
592 // See if we encoded all required fields.
594 return &ErrRequiredNotSet{t}