blob: 4fbf02e769c20682ee544147a4e287b1ba10895f [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 encoding data into the wire format for protocol buffers.
36 */
37
38import (
39 "bytes"
40 "os"
41 "reflect"
42 "runtime"
43 "unsafe"
44)
45
46// ErrRequiredNotSet is the error returned if Marshal is called with
47// a protocol buffer struct whose required fields have not
Rob Pikec6d8e4a2010-07-28 15:34:32 -070048// all been initialized. It is also the error returned if Unmarshal is
49// called with an encoded protocol buffer that does not include all the
50// required fields.
Rob Pikeaaa3a622010-03-20 22:32:34 -070051var ErrRequiredNotSet = os.NewError("required fields not set")
52
53// ErrRepeatedHasNil is the error returned if Marshal is called with
54// a protocol buffer struct with a repeated field containing a nil element.
55var ErrRepeatedHasNil = os.NewError("repeated field has nil")
56
57// ErrNil is the error returned if Marshal is called with nil.
58var ErrNil = os.NewError("marshal called with nil")
59
60// The fundamental encoders that put bytes on the wire.
61// Those that take integer types all accept uint64 and are
62// therefore of type valueEncoder.
63
David Symonds4fee3b12010-11-11 10:00:13 +110064const maxVarintBytes = 10 // maximum length of a varint
65
Rob Pikeaaa3a622010-03-20 22:32:34 -070066// EncodeVarint returns the varint encoding of x.
67// This is the format for the
68// int32, int64, uint32, uint64, bool, and enum
69// protocol buffer types.
70// Not used by the package itself, but helpful to clients
71// wishing to use the same encoding.
72func EncodeVarint(x uint64) []byte {
David Symonds4fee3b12010-11-11 10:00:13 +110073 var buf [maxVarintBytes]byte
Rob Pikeaaa3a622010-03-20 22:32:34 -070074 var n int
75 for n = 0; x > 127; n++ {
76 buf[n] = 0x80 | uint8(x&0x7F)
77 x >>= 7
78 }
79 buf[n] = uint8(x)
80 n++
81 return buf[0:n]
82}
83
David Symonds4fee3b12010-11-11 10:00:13 +110084var emptyBytes [maxVarintBytes]byte
85
Rob Pikeaaa3a622010-03-20 22:32:34 -070086// EncodeVarint writes a varint-encoded integer to the Buffer.
87// This is the format for the
88// int32, int64, uint32, uint64, bool, and enum
89// protocol buffer types.
90func (p *Buffer) EncodeVarint(x uint64) os.Error {
91 l := len(p.buf)
David Symonds4fee3b12010-11-11 10:00:13 +110092 if l+maxVarintBytes > cap(p.buf) { // not necessary except for performance
93 p.buf = append(p.buf, emptyBytes[:]...)
94 } else {
95 p.buf = p.buf[:l+maxVarintBytes]
Rob Pikeaaa3a622010-03-20 22:32:34 -070096 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070097
David Symonds4fee3b12010-11-11 10:00:13 +110098 for x >= 1<<7 {
Rob Pikeaaa3a622010-03-20 22:32:34 -070099 p.buf[l] = uint8(x&0x7f | 0x80)
100 l++
101 x >>= 7
102 }
103 p.buf[l] = uint8(x)
David Symonds4fee3b12010-11-11 10:00:13 +1100104 p.buf = p.buf[:l+1]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700105 return nil
106}
107
108// EncodeFixed64 writes a 64-bit integer to the Buffer.
109// This is the format for the
110// fixed64, sfixed64, and double protocol buffer types.
111func (p *Buffer) EncodeFixed64(x uint64) os.Error {
David Symonds4fee3b12010-11-11 10:00:13 +1100112 const fixed64Bytes = 8
Rob Pikeaaa3a622010-03-20 22:32:34 -0700113 l := len(p.buf)
David Symonds4fee3b12010-11-11 10:00:13 +1100114 if l+fixed64Bytes > cap(p.buf) { // not necessary except for performance
115 p.buf = append(p.buf, emptyBytes[:fixed64Bytes]...)
116 } else {
117 p.buf = p.buf[:l+fixed64Bytes]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700118 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700119
120 p.buf[l] = uint8(x)
121 p.buf[l+1] = uint8(x >> 8)
122 p.buf[l+2] = uint8(x >> 16)
123 p.buf[l+3] = uint8(x >> 24)
124 p.buf[l+4] = uint8(x >> 32)
125 p.buf[l+5] = uint8(x >> 40)
126 p.buf[l+6] = uint8(x >> 48)
127 p.buf[l+7] = uint8(x >> 56)
128 return nil
129}
130
131// EncodeFixed32 writes a 32-bit integer to the Buffer.
132// This is the format for the
133// fixed32, sfixed32, and float protocol buffer types.
134func (p *Buffer) EncodeFixed32(x uint64) os.Error {
David Symonds4fee3b12010-11-11 10:00:13 +1100135 const fixed32Bytes = 4
Rob Pikeaaa3a622010-03-20 22:32:34 -0700136 l := len(p.buf)
David Symonds4fee3b12010-11-11 10:00:13 +1100137 if l+fixed32Bytes > cap(p.buf) { // not necessary except for performance
138 p.buf = append(p.buf, emptyBytes[:fixed32Bytes]...)
139 } else {
140 p.buf = p.buf[:l+fixed32Bytes]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700141 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700142
143 p.buf[l] = uint8(x)
144 p.buf[l+1] = uint8(x >> 8)
145 p.buf[l+2] = uint8(x >> 16)
146 p.buf[l+3] = uint8(x >> 24)
147 return nil
148}
149
150// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
151// to the Buffer.
152// This is the format used for the sint64 protocol buffer type.
153func (p *Buffer) EncodeZigzag64(x uint64) os.Error {
154 // use signed number to get arithmetic right shift.
155 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
156}
157
158// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
159// to the Buffer.
160// This is the format used for the sint32 protocol buffer type.
161func (p *Buffer) EncodeZigzag32(x uint64) os.Error {
162 // use signed number to get arithmetic right shift.
163 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
164}
165
166// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
167// This is the format used for the bytes protocol buffer
168// type and for embedded messages.
169func (p *Buffer) EncodeRawBytes(b []byte) os.Error {
170 lb := len(b)
171 p.EncodeVarint(uint64(lb))
172 p.buf = bytes.Add(p.buf, b)
173 return nil
174}
175
176// EncodeStringBytes writes an encoded string to the Buffer.
177// This is the format used for the proto2 string type.
178func (p *Buffer) EncodeStringBytes(s string) os.Error {
179
180 // this works because strings and slices are the same.
181 y := *(*[]byte)(unsafe.Pointer(&s))
182 p.EncodeRawBytes(y)
183 return nil
184}
185
186// Marshaler is the interface representing objects that can marshal themselves.
187type Marshaler interface {
188 Marshal() ([]byte, os.Error)
189}
190
191// Marshal takes the protocol buffer struct represented by pb
192// and encodes it into the wire format, returning the data.
193func Marshal(pb interface{}) ([]byte, os.Error) {
194 // Can the object marshal itself?
195 if m, ok := pb.(Marshaler); ok {
196 return m.Marshal()
197 }
198 p := NewBuffer(nil)
199 err := p.Marshal(pb)
200 if err != nil {
201 return nil, err
202 }
203 return p.buf, err
204}
205
206// Marshal takes the protocol buffer struct represented by pb
207// and encodes it into the wire format, writing the result to the
208// Buffer.
209func (p *Buffer) Marshal(pb interface{}) os.Error {
210 // Can the object marshal itself?
211 if m, ok := pb.(Marshaler); ok {
212 data, err := m.Marshal()
213 if err != nil {
214 return err
215 }
216 p.buf = bytes.Add(p.buf, data)
217 return nil
218 }
219
220 mstat := runtime.MemStats.Mallocs
221
222 t, b, err := getbase(pb)
223 if err == nil {
224 err = p.enc_struct(t.Elem().(*reflect.StructType), b)
225 }
226
227 mstat = runtime.MemStats.Mallocs - mstat
228 stats.Emalloc += mstat
229 stats.Encode++
230
231 return err
232}
233
234// Individual type encoders.
235
236// Encode a bool.
237func (o *Buffer) enc_bool(p *Properties, base uintptr) os.Error {
238 v := *(**uint8)(unsafe.Pointer(base + p.offset))
239 if v == nil {
240 return ErrNil
241 }
242 x := *v
243 if x != 0 {
244 x = 1
245 }
246 o.buf = bytes.Add(o.buf, p.tagcode)
247 p.valEnc(o, uint64(x))
248 return nil
249}
250
251// Encode an int32.
252func (o *Buffer) enc_int32(p *Properties, base uintptr) os.Error {
253 v := *(**uint32)(unsafe.Pointer(base + p.offset))
254 if v == nil {
255 return ErrNil
256 }
257 x := *v
258 o.buf = bytes.Add(o.buf, p.tagcode)
259 p.valEnc(o, uint64(x))
260 return nil
261}
262
263// Encode an int64.
264func (o *Buffer) enc_int64(p *Properties, base uintptr) os.Error {
265 v := *(**uint64)(unsafe.Pointer(base + p.offset))
266 if v == nil {
267 return ErrNil
268 }
269 x := *v
270 o.buf = bytes.Add(o.buf, p.tagcode)
271 p.valEnc(o, uint64(x))
272 return nil
273}
274
275// Encode a string.
276func (o *Buffer) enc_string(p *Properties, base uintptr) os.Error {
277 v := *(**string)(unsafe.Pointer(base + p.offset))
278 if v == nil {
279 return ErrNil
280 }
281 x := *v
282 o.buf = bytes.Add(o.buf, p.tagcode)
283 o.EncodeStringBytes(x)
284 return nil
285}
286
287// Encode a message struct.
288func (o *Buffer) enc_struct_message(p *Properties, base uintptr) os.Error {
289 // Can the object marshal itself?
290 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(base+p.offset))
291 if m, ok := iv.(Marshaler); ok {
David Symonds03c9d412010-08-26 14:23:18 +1000292 if n, ok := reflect.NewValue(iv).(nillable); ok && n.IsNil() {
293 return ErrNil
294 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700295 data, err := m.Marshal()
296 if err != nil {
297 return err
298 }
299 o.buf = bytes.Add(o.buf, p.tagcode)
300 o.EncodeRawBytes(data)
301 return nil
302 }
303 v := *(**struct{})(unsafe.Pointer(base + p.offset))
304 if v == nil {
305 return ErrNil
306 }
307
308 // need the length before we can write out the message itself,
309 // so marshal into a separate byte buffer first.
310 obuf := o.buf
311 o.buf = o.bufalloc()
312
313 b := uintptr(unsafe.Pointer(v))
314 typ := p.stype.Elem().(*reflect.StructType)
315 err := o.enc_struct(typ, b)
316
317 nbuf := o.buf
318 o.buf = obuf
319 if err != nil {
320 o.buffree(nbuf)
321 return err
322 }
323 o.buf = bytes.Add(o.buf, p.tagcode)
324 o.EncodeRawBytes(nbuf)
325 o.buffree(nbuf)
326 return nil
327}
328
329// Encode a group struct.
330func (o *Buffer) enc_struct_group(p *Properties, base uintptr) os.Error {
331 v := *(**struct{})(unsafe.Pointer(base + p.offset))
332 if v == nil {
333 return ErrNil
334 }
335
336 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
337 b := uintptr(unsafe.Pointer(v))
338 typ := p.stype.Elem().(*reflect.StructType)
339 err := o.enc_struct(typ, b)
340 if err != nil {
341 return err
342 }
343 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
344 return nil
345}
346
347// Encode a slice of bools ([]bool).
348func (o *Buffer) enc_slice_bool(p *Properties, base uintptr) os.Error {
349 s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
350 l := len(s)
351 if l == 0 {
352 return ErrNil
353 }
354 for _, x := range s {
355 o.buf = bytes.Add(o.buf, p.tagcode)
356 if x != 0 {
357 x = 1
358 }
359 p.valEnc(o, uint64(x))
360 }
361 return nil
362}
363
364// Encode a slice of bytes ([]byte).
365func (o *Buffer) enc_slice_byte(p *Properties, base uintptr) os.Error {
366 s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
367 // if the field is required, we must send something, even if it's an empty array.
368 if !p.Required {
369 l := len(s)
370 if l == 0 {
371 return ErrNil
372 }
373 // check default
374 if l == len(p.Default) {
375 same := true
376 for i := 0; i < len(p.Default); i++ {
377 if p.Default[i] != s[i] {
378 same = false
379 break
380 }
381 }
382 if same {
383 return ErrNil
384 }
385 }
386 }
387 o.buf = bytes.Add(o.buf, p.tagcode)
388 o.EncodeRawBytes(s)
389 return nil
390}
391
392// Encode a slice of int32s ([]int32).
393func (o *Buffer) enc_slice_int32(p *Properties, base uintptr) os.Error {
394 s := *(*[]uint32)(unsafe.Pointer(base + p.offset))
395 l := len(s)
396 if l == 0 {
397 return ErrNil
398 }
399 for i := 0; i < l; i++ {
400 o.buf = bytes.Add(o.buf, p.tagcode)
401 x := s[i]
402 p.valEnc(o, uint64(x))
403 }
404 return nil
405}
406
407// Encode a slice of int64s ([]int64).
408func (o *Buffer) enc_slice_int64(p *Properties, base uintptr) os.Error {
409 s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
410 l := len(s)
411 if l == 0 {
412 return ErrNil
413 }
414 for i := 0; i < l; i++ {
415 o.buf = bytes.Add(o.buf, p.tagcode)
416 x := s[i]
417 p.valEnc(o, uint64(x))
418 }
419 return nil
420}
421
422// Encode a slice of slice of bytes ([][]byte).
423func (o *Buffer) enc_slice_slice_byte(p *Properties, base uintptr) os.Error {
424 ss := *(*[][]uint8)(unsafe.Pointer(base + p.offset))
425 l := len(ss)
426 if l == 0 {
427 return ErrNil
428 }
429 for i := 0; i < l; i++ {
430 o.buf = bytes.Add(o.buf, p.tagcode)
431 s := ss[i]
432 o.EncodeRawBytes(s)
433 }
434 return nil
435}
436
437// Encode a slice of strings ([]string).
438func (o *Buffer) enc_slice_string(p *Properties, base uintptr) os.Error {
439 ss := *(*[]string)(unsafe.Pointer(base + p.offset))
440 l := len(ss)
441 for i := 0; i < l; i++ {
442 o.buf = bytes.Add(o.buf, p.tagcode)
443 s := ss[i]
444 o.EncodeStringBytes(s)
445 }
446 return nil
447}
448
449// Encode a slice of message structs ([]*struct).
450func (o *Buffer) enc_slice_struct_message(p *Properties, base uintptr) os.Error {
451 s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
452 l := len(s)
453 typ := p.stype.Elem().(*reflect.StructType)
454
455 for i := 0; i < l; i++ {
456 v := s[i]
457 if v == nil {
458 return ErrRepeatedHasNil
459 }
460
461 // Can the object marshal itself?
462 iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&s[i]))
463 if m, ok := iv.(Marshaler); ok {
David Symonds03c9d412010-08-26 14:23:18 +1000464 if n, ok := reflect.NewValue(iv).(nillable); ok && n.IsNil() {
465 return ErrNil
466 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700467 data, err := m.Marshal()
468 if err != nil {
469 return err
470 }
471 o.buf = bytes.Add(o.buf, p.tagcode)
472 o.EncodeRawBytes(data)
473 continue
474 }
475
476 obuf := o.buf
477 o.buf = o.bufalloc()
478
479 b := uintptr(unsafe.Pointer(v))
480 err := o.enc_struct(typ, b)
481
482 nbuf := o.buf
483 o.buf = obuf
484 if err != nil {
485 o.buffree(nbuf)
486 if err == ErrNil {
487 return ErrRepeatedHasNil
488 }
489 return err
490 }
491 o.buf = bytes.Add(o.buf, p.tagcode)
492 o.EncodeRawBytes(nbuf)
493
494 o.buffree(nbuf)
495 }
496 return nil
497}
498
499// Encode a slice of group structs ([]*struct).
500func (o *Buffer) enc_slice_struct_group(p *Properties, base uintptr) os.Error {
501 s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
502 l := len(s)
503 typ := p.stype.Elem().(*reflect.StructType)
504
505 for i := 0; i < l; i++ {
506 v := s[i]
507 if v == nil {
508 return ErrRepeatedHasNil
509 }
510
511 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
512
513 b := uintptr(unsafe.Pointer(v))
514 err := o.enc_struct(typ, b)
515
516 if err != nil {
517 if err == ErrNil {
518 return ErrRepeatedHasNil
519 }
520 return err
521 }
522
523 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
524 }
525 return nil
526}
527
528// Encode an extension map.
529func (o *Buffer) enc_map(p *Properties, base uintptr) os.Error {
530 v := *(*map[int32][]byte)(unsafe.Pointer(base + p.offset))
531 for _, b := range v {
532 o.buf = bytes.Add(o.buf, b)
533 }
534 return nil
535}
536
537// Encode a struct.
538func (o *Buffer) enc_struct(t *reflect.StructType, base uintptr) os.Error {
539 prop := GetProperties(t)
540 required := prop.reqCount
541 for _, p := range prop.Prop {
542 if p.enc != nil {
543 err := p.enc(o, p, base)
544 if err != nil {
545 if err != ErrNil {
546 return err
547 }
548 } else if p.Required {
549 required--
550 }
551 }
552 }
553 // See if we encoded all required fields.
554 if required > 0 {
555 return ErrRequiredNotSet
556 }
557
558 return nil
559}