| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // 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 | |
| 32 | package proto |
| 33 | |
| 34 | /* |
| 35 | * Routines for encoding data into the wire format for protocol buffers. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 39 | "errors" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | "reflect" |
| 41 | "runtime" |
| 42 | "unsafe" |
| 43 | ) |
| 44 | |
| 45 | // ErrRequiredNotSet is the error returned if Marshal is called with |
| 46 | // a protocol buffer struct whose required fields have not |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 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 |
| 49 | // required fields. |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 50 | type ErrRequiredNotSet struct { |
| 51 | t reflect.Type |
| 52 | } |
| 53 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 54 | func (e *ErrRequiredNotSet) Error() string { |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 55 | return "proto: required fields not set in " + e.t.String() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 56 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 57 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 58 | var ( |
| 59 | // ErrRepeatedHasNil is the error returned if Marshal is called with |
| 60 | // a struct with a repeated field containing a nil element. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 61 | ErrRepeatedHasNil = errors.New("proto: repeated field has nil") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 62 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 63 | // ErrNil is the error returned if Marshal is called with nil. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 64 | ErrNil = errors.New("proto: Marshal called with nil") |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 65 | |
| 66 | // ErrNotPtr is the error returned if Marshal is called with a non-pointer. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 67 | ErrNotPtr = errors.New("proto: Marshal called with a non-pointer") |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 68 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 69 | |
| 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. |
| 73 | |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 74 | const maxVarintBytes = 10 // maximum length of a varint |
| 75 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 83 | var buf [maxVarintBytes]byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 84 | var n int |
| 85 | for n = 0; x > 127; n++ { |
| 86 | buf[n] = 0x80 | uint8(x&0x7F) |
| 87 | x >>= 7 |
| 88 | } |
| 89 | buf[n] = uint8(x) |
| 90 | n++ |
| 91 | return buf[0:n] |
| 92 | } |
| 93 | |
| 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 98 | func (p *Buffer) EncodeVarint(x uint64) error { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 99 | for x >= 1<<7 { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 100 | p.buf = append(p.buf, uint8(x&0x7f|0x80)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 101 | x >>= 7 |
| 102 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 103 | p.buf = append(p.buf, uint8(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 104 | return nil |
| 105 | } |
| 106 | |
| 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 110 | func (p *Buffer) EncodeFixed64(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 111 | p.buf = append(p.buf, |
| 112 | uint8(x), |
| 113 | uint8(x>>8), |
| 114 | uint8(x>>16), |
| 115 | uint8(x>>24), |
| 116 | uint8(x>>32), |
| 117 | uint8(x>>40), |
| 118 | uint8(x>>48), |
| 119 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 120 | return nil |
| 121 | } |
| 122 | |
| 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 126 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 127 | p.buf = append(p.buf, |
| 128 | uint8(x), |
| 129 | uint8(x>>8), |
| 130 | uint8(x>>16), |
| 131 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | return nil |
| 133 | } |
| 134 | |
| 135 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 136 | // to the Buffer. |
| 137 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 138 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 139 | // use signed number to get arithmetic right shift. |
| 140 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 141 | } |
| 142 | |
| 143 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 144 | // to the Buffer. |
| 145 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 146 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 147 | // use signed number to get arithmetic right shift. |
| 148 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 149 | } |
| 150 | |
| 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 154 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 155 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 156 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 157 | return nil |
| 158 | } |
| 159 | |
| 160 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 161 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 162 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 163 | p.EncodeVarint(uint64(len(s))) |
| 164 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 165 | return nil |
| 166 | } |
| 167 | |
| 168 | // Marshaler is the interface representing objects that can marshal themselves. |
| 169 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 170 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Marshal takes the protocol buffer struct represented by pb |
| 174 | // and encodes it into the wire format, returning the data. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 175 | func Marshal(pb interface{}) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | // Can the object marshal itself? |
| 177 | if m, ok := pb.(Marshaler); ok { |
| 178 | return m.Marshal() |
| 179 | } |
| 180 | p := NewBuffer(nil) |
| 181 | err := p.Marshal(pb) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | return p.buf, err |
| 186 | } |
| 187 | |
| 188 | // Marshal takes the protocol buffer struct represented by pb |
| 189 | // and encodes it into the wire format, writing the result to the |
| 190 | // Buffer. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 191 | func (p *Buffer) Marshal(pb interface{}) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 192 | // Can the object marshal itself? |
| 193 | if m, ok := pb.(Marshaler); ok { |
| 194 | data, err := m.Marshal() |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 198 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 199 | return nil |
| 200 | } |
| 201 | |
| 202 | mstat := runtime.MemStats.Mallocs |
| 203 | |
| 204 | t, b, err := getbase(pb) |
| 205 | if err == nil { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 206 | err = p.enc_struct(t.Elem(), b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | mstat = runtime.MemStats.Mallocs - mstat |
| 210 | stats.Emalloc += mstat |
| 211 | stats.Encode++ |
| 212 | |
| 213 | return err |
| 214 | } |
| 215 | |
| 216 | // Individual type encoders. |
| 217 | |
| 218 | // Encode a bool. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 219 | func (o *Buffer) enc_bool(p *Properties, base uintptr) error { |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 220 | v := *(**bool)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 221 | if v == nil { |
| 222 | return ErrNil |
| 223 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 224 | x := 0 |
| 225 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 226 | x = 1 |
| 227 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 228 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 229 | p.valEnc(o, uint64(x)) |
| 230 | return nil |
| 231 | } |
| 232 | |
| 233 | // Encode an int32. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 234 | func (o *Buffer) enc_int32(p *Properties, base uintptr) error { |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 235 | v := *(**int32)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 236 | if v == nil { |
| 237 | return ErrNil |
| 238 | } |
| 239 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 240 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 241 | p.valEnc(o, uint64(x)) |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | // Encode an int64. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 246 | func (o *Buffer) enc_int64(p *Properties, base uintptr) error { |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 247 | v := *(**int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 248 | if v == nil { |
| 249 | return ErrNil |
| 250 | } |
| 251 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 252 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 253 | p.valEnc(o, uint64(x)) |
| 254 | return nil |
| 255 | } |
| 256 | |
| 257 | // Encode a string. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 258 | func (o *Buffer) enc_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 259 | v := *(**string)(unsafe.Pointer(base + p.offset)) |
| 260 | if v == nil { |
| 261 | return ErrNil |
| 262 | } |
| 263 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 264 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 265 | o.EncodeStringBytes(x) |
| 266 | return nil |
| 267 | } |
| 268 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 269 | // All protocol buffer fields are nillable, but be careful. |
| 270 | func isNil(v reflect.Value) bool { |
| 271 | switch v.Kind() { |
| Rob Pike | ac8b1ce | 2011-04-11 16:14:54 -0700 | [diff] [blame] | 272 | case reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 273 | return v.IsNil() |
| 274 | } |
| 275 | return false |
| 276 | } |
| 277 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 278 | // Encode a message struct. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 279 | func (o *Buffer) enc_struct_message(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 280 | // Can the object marshal itself? |
| 281 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(base+p.offset)) |
| 282 | if m, ok := iv.(Marshaler); ok { |
| Nigel Tao | 4ede845 | 2011-04-28 11:27:25 +1000 | [diff] [blame] | 283 | if isNil(reflect.ValueOf(iv)) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 284 | return ErrNil |
| 285 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 286 | data, err := m.Marshal() |
| 287 | if err != nil { |
| 288 | return err |
| 289 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 290 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 291 | o.EncodeRawBytes(data) |
| 292 | return nil |
| 293 | } |
| 294 | v := *(**struct{})(unsafe.Pointer(base + p.offset)) |
| 295 | if v == nil { |
| 296 | return ErrNil |
| 297 | } |
| 298 | |
| 299 | // need the length before we can write out the message itself, |
| 300 | // so marshal into a separate byte buffer first. |
| 301 | obuf := o.buf |
| 302 | o.buf = o.bufalloc() |
| 303 | |
| 304 | b := uintptr(unsafe.Pointer(v)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 305 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 306 | err := o.enc_struct(typ, b) |
| 307 | |
| 308 | nbuf := o.buf |
| 309 | o.buf = obuf |
| 310 | if err != nil { |
| 311 | o.buffree(nbuf) |
| 312 | return err |
| 313 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 314 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 315 | o.EncodeRawBytes(nbuf) |
| 316 | o.buffree(nbuf) |
| 317 | return nil |
| 318 | } |
| 319 | |
| 320 | // Encode a group struct. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 321 | func (o *Buffer) enc_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 322 | v := *(**struct{})(unsafe.Pointer(base + p.offset)) |
| 323 | if v == nil { |
| 324 | return ErrNil |
| 325 | } |
| 326 | |
| 327 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 328 | b := uintptr(unsafe.Pointer(v)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 329 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 330 | err := o.enc_struct(typ, b) |
| 331 | if err != nil { |
| 332 | return err |
| 333 | } |
| 334 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 335 | return nil |
| 336 | } |
| 337 | |
| 338 | // Encode a slice of bools ([]bool). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 339 | func (o *Buffer) enc_slice_bool(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 340 | s := *(*[]uint8)(unsafe.Pointer(base + p.offset)) |
| 341 | l := len(s) |
| 342 | if l == 0 { |
| 343 | return ErrNil |
| 344 | } |
| 345 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 346 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 347 | if x != 0 { |
| 348 | x = 1 |
| 349 | } |
| 350 | p.valEnc(o, uint64(x)) |
| 351 | } |
| 352 | return nil |
| 353 | } |
| 354 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 355 | // Encode a slice of bools ([]bool) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 356 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base uintptr) error { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 357 | s := *(*[]uint8)(unsafe.Pointer(base + p.offset)) |
| 358 | l := len(s) |
| 359 | if l == 0 { |
| 360 | return ErrNil |
| 361 | } |
| 362 | o.buf = append(o.buf, p.tagcode...) |
| 363 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 364 | for _, x := range s { |
| 365 | if x != 0 { |
| 366 | x = 1 |
| 367 | } |
| 368 | p.valEnc(o, uint64(x)) |
| 369 | } |
| 370 | return nil |
| 371 | } |
| 372 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 373 | // Encode a slice of bytes ([]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 374 | func (o *Buffer) enc_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 375 | s := *(*[]uint8)(unsafe.Pointer(base + p.offset)) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 376 | if s == nil { |
| 377 | return ErrNil |
| 378 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 379 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 380 | o.EncodeRawBytes(s) |
| 381 | return nil |
| 382 | } |
| 383 | |
| 384 | // Encode a slice of int32s ([]int32). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 385 | func (o *Buffer) enc_slice_int32(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 386 | s := *(*[]uint32)(unsafe.Pointer(base + p.offset)) |
| 387 | l := len(s) |
| 388 | if l == 0 { |
| 389 | return ErrNil |
| 390 | } |
| 391 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 392 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 393 | x := s[i] |
| 394 | p.valEnc(o, uint64(x)) |
| 395 | } |
| 396 | return nil |
| 397 | } |
| 398 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 399 | // Encode a slice of int32s ([]int32) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 400 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base uintptr) error { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 401 | s := *(*[]uint32)(unsafe.Pointer(base + p.offset)) |
| 402 | l := len(s) |
| 403 | if l == 0 { |
| 404 | return ErrNil |
| 405 | } |
| 406 | // TODO: Reuse a Buffer. |
| 407 | buf := NewBuffer(nil) |
| 408 | for i := 0; i < l; i++ { |
| 409 | p.valEnc(buf, uint64(s[i])) |
| 410 | } |
| 411 | |
| 412 | o.buf = append(o.buf, p.tagcode...) |
| 413 | o.EncodeVarint(uint64(len(buf.buf))) |
| 414 | o.buf = append(o.buf, buf.buf...) |
| 415 | return nil |
| 416 | } |
| 417 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 418 | // Encode a slice of int64s ([]int64). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 419 | func (o *Buffer) enc_slice_int64(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 420 | s := *(*[]uint64)(unsafe.Pointer(base + p.offset)) |
| 421 | l := len(s) |
| 422 | if l == 0 { |
| 423 | return ErrNil |
| 424 | } |
| 425 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 426 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 427 | x := s[i] |
| 428 | p.valEnc(o, uint64(x)) |
| 429 | } |
| 430 | return nil |
| 431 | } |
| 432 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 433 | // Encode a slice of int64s ([]int64) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 434 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base uintptr) error { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 435 | s := *(*[]uint64)(unsafe.Pointer(base + p.offset)) |
| 436 | l := len(s) |
| 437 | if l == 0 { |
| 438 | return ErrNil |
| 439 | } |
| 440 | // TODO: Reuse a Buffer. |
| 441 | buf := NewBuffer(nil) |
| 442 | for i := 0; i < l; i++ { |
| 443 | p.valEnc(buf, s[i]) |
| 444 | } |
| 445 | |
| 446 | o.buf = append(o.buf, p.tagcode...) |
| 447 | o.EncodeVarint(uint64(len(buf.buf))) |
| 448 | o.buf = append(o.buf, buf.buf...) |
| 449 | return nil |
| 450 | } |
| 451 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 452 | // Encode a slice of slice of bytes ([][]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 453 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 454 | ss := *(*[][]uint8)(unsafe.Pointer(base + p.offset)) |
| 455 | l := len(ss) |
| 456 | if l == 0 { |
| 457 | return ErrNil |
| 458 | } |
| 459 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 460 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 461 | s := ss[i] |
| 462 | o.EncodeRawBytes(s) |
| 463 | } |
| 464 | return nil |
| 465 | } |
| 466 | |
| 467 | // Encode a slice of strings ([]string). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 468 | func (o *Buffer) enc_slice_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 469 | ss := *(*[]string)(unsafe.Pointer(base + p.offset)) |
| 470 | l := len(ss) |
| 471 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 472 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 473 | s := ss[i] |
| 474 | o.EncodeStringBytes(s) |
| 475 | } |
| 476 | return nil |
| 477 | } |
| 478 | |
| 479 | // Encode a slice of message structs ([]*struct). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 480 | func (o *Buffer) enc_slice_struct_message(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 481 | s := *(*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 482 | l := len(s) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 483 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 484 | |
| 485 | for i := 0; i < l; i++ { |
| 486 | v := s[i] |
| 487 | if v == nil { |
| 488 | return ErrRepeatedHasNil |
| 489 | } |
| 490 | |
| 491 | // Can the object marshal itself? |
| 492 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&s[i])) |
| 493 | if m, ok := iv.(Marshaler); ok { |
| David Symonds | 3fa055f | 2011-05-05 15:19:04 -0700 | [diff] [blame] | 494 | if isNil(reflect.ValueOf(iv)) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 495 | return ErrNil |
| 496 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | data, err := m.Marshal() |
| 498 | if err != nil { |
| 499 | return err |
| 500 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 501 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 502 | o.EncodeRawBytes(data) |
| 503 | continue |
| 504 | } |
| 505 | |
| 506 | obuf := o.buf |
| 507 | o.buf = o.bufalloc() |
| 508 | |
| 509 | b := uintptr(unsafe.Pointer(v)) |
| 510 | err := o.enc_struct(typ, b) |
| 511 | |
| 512 | nbuf := o.buf |
| 513 | o.buf = obuf |
| 514 | if err != nil { |
| 515 | o.buffree(nbuf) |
| 516 | if err == ErrNil { |
| 517 | return ErrRepeatedHasNil |
| 518 | } |
| 519 | return err |
| 520 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 521 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | o.EncodeRawBytes(nbuf) |
| 523 | |
| 524 | o.buffree(nbuf) |
| 525 | } |
| 526 | return nil |
| 527 | } |
| 528 | |
| 529 | // Encode a slice of group structs ([]*struct). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 530 | func (o *Buffer) enc_slice_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 531 | s := *(*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 532 | l := len(s) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 533 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 534 | |
| 535 | for i := 0; i < l; i++ { |
| 536 | v := s[i] |
| 537 | if v == nil { |
| 538 | return ErrRepeatedHasNil |
| 539 | } |
| 540 | |
| 541 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 542 | |
| 543 | b := uintptr(unsafe.Pointer(v)) |
| 544 | err := o.enc_struct(typ, b) |
| 545 | |
| 546 | if err != nil { |
| 547 | if err == ErrNil { |
| 548 | return ErrRepeatedHasNil |
| 549 | } |
| 550 | return err |
| 551 | } |
| 552 | |
| 553 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 554 | } |
| 555 | return nil |
| 556 | } |
| 557 | |
| 558 | // Encode an extension map. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 559 | func (o *Buffer) enc_map(p *Properties, base uintptr) error { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 560 | v := *(*map[int32]Extension)(unsafe.Pointer(base + p.offset)) |
| 561 | if err := encodeExtensionMap(v); err != nil { |
| 562 | return err |
| 563 | } |
| 564 | for _, e := range v { |
| 565 | o.buf = append(o.buf, e.enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 566 | } |
| 567 | return nil |
| 568 | } |
| 569 | |
| 570 | // Encode a struct. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame^] | 571 | func (o *Buffer) enc_struct(t reflect.Type, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 572 | prop := GetProperties(t) |
| 573 | required := prop.reqCount |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 574 | // Encode fields in tag order so that decoders may use optimizations |
| 575 | // that depend on the ordering. |
| 576 | // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order |
| 577 | for _, i := range prop.order { |
| 578 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 579 | if p.enc != nil { |
| 580 | err := p.enc(o, p, base) |
| 581 | if err != nil { |
| 582 | if err != ErrNil { |
| 583 | return err |
| 584 | } |
| 585 | } else if p.Required { |
| 586 | required-- |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | // See if we encoded all required fields. |
| 591 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 592 | return &ErrRequiredNotSet{t} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return nil |
| 596 | } |