| 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 decoding protocol buffer data to construct in-memory representations. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 39 | "fmt" |
| 40 | "io" |
| 41 | "os" |
| 42 | "reflect" |
| 43 | "runtime" |
| 44 | "unsafe" |
| 45 | ) |
| 46 | |
| 47 | // ErrWrongType occurs when the wire encoding for the field disagrees with |
| 48 | // that specified in the type being decoded. This is usually caused by attempting |
| 49 | // to convert an encoded protocol buffer into a struct of the wrong type. |
| 50 | var ErrWrongType = os.NewError("field/encoding mismatch: wrong type for field") |
| 51 | |
| 52 | // The fundamental decoders that interpret bytes on the wire. |
| 53 | // Those that take integer types all return uint64 and are |
| 54 | // therefore of type valueDecoder. |
| 55 | |
| 56 | // DecodeVarint reads a varint-encoded integer from the slice. |
| 57 | // It returns the integer and the number of bytes consumed, or |
| 58 | // zero if there is not enough. |
| 59 | // This is the format for the |
| 60 | // int32, int64, uint32, uint64, bool, and enum |
| 61 | // protocol buffer types. |
| 62 | func DecodeVarint(buf []byte) (x uint64, n int) { |
| 63 | // x, n already 0 |
| 64 | for shift := uint(0); ; shift += 7 { |
| 65 | if n >= len(buf) { |
| 66 | return 0, 0 |
| 67 | } |
| 68 | b := uint64(buf[n]) |
| 69 | n++ |
| 70 | x |= (b & 0x7F) << shift |
| 71 | if (b & 0x80) == 0 { |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | return x, n |
| 76 | } |
| 77 | |
| 78 | // DecodeVarint reads a varint-encoded integer from the Buffer. |
| 79 | // This is the format for the |
| 80 | // int32, int64, uint32, uint64, bool, and enum |
| 81 | // protocol buffer types. |
| 82 | func (p *Buffer) DecodeVarint() (x uint64, err os.Error) { |
| 83 | // x, err already 0 |
| 84 | |
| 85 | i := p.index |
| 86 | l := len(p.buf) |
| 87 | |
| 88 | for shift := uint(0); ; shift += 7 { |
| 89 | if i >= l { |
| 90 | err = io.ErrUnexpectedEOF |
| 91 | return |
| 92 | } |
| 93 | b := p.buf[i] |
| 94 | i++ |
| 95 | x |= (uint64(b) & 0x7F) << shift |
| 96 | if b < 0x80 { |
| 97 | break |
| 98 | } |
| 99 | } |
| 100 | p.index = i |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | // DecodeFixed64 reads a 64-bit integer from the Buffer. |
| 105 | // This is the format for the |
| 106 | // fixed64, sfixed64, and double protocol buffer types. |
| 107 | func (p *Buffer) DecodeFixed64() (x uint64, err os.Error) { |
| 108 | // x, err already 0 |
| 109 | i := p.index + 8 |
| 110 | if i > len(p.buf) { |
| 111 | err = io.ErrUnexpectedEOF |
| 112 | return |
| 113 | } |
| 114 | p.index = i |
| 115 | |
| 116 | x = uint64(p.buf[i-8]) |
| 117 | x |= uint64(p.buf[i-7]) << 8 |
| 118 | x |= uint64(p.buf[i-6]) << 16 |
| 119 | x |= uint64(p.buf[i-5]) << 24 |
| 120 | x |= uint64(p.buf[i-4]) << 32 |
| 121 | x |= uint64(p.buf[i-3]) << 40 |
| 122 | x |= uint64(p.buf[i-2]) << 48 |
| 123 | x |= uint64(p.buf[i-1]) << 56 |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | // DecodeFixed32 reads a 32-bit integer from the Buffer. |
| 128 | // This is the format for the |
| 129 | // fixed32, sfixed32, and float protocol buffer types. |
| 130 | func (p *Buffer) DecodeFixed32() (x uint64, err os.Error) { |
| 131 | // x, err already 0 |
| 132 | i := p.index + 4 |
| 133 | if i > len(p.buf) { |
| 134 | err = io.ErrUnexpectedEOF |
| 135 | return |
| 136 | } |
| 137 | p.index = i |
| 138 | |
| 139 | x = uint64(p.buf[i-4]) |
| 140 | x |= uint64(p.buf[i-3]) << 8 |
| 141 | x |= uint64(p.buf[i-2]) << 16 |
| 142 | x |= uint64(p.buf[i-1]) << 24 |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | // DecodeZigzag64 reads a zigzag-encoded 64-bit integer |
| 147 | // from the Buffer. |
| 148 | // This is the format used for the sint64 protocol buffer type. |
| 149 | func (p *Buffer) DecodeZigzag64() (x uint64, err os.Error) { |
| 150 | x, err = p.DecodeVarint() |
| 151 | if err != nil { |
| 152 | return |
| 153 | } |
| 154 | x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | // DecodeZigzag32 reads a zigzag-encoded 32-bit integer |
| 159 | // from the Buffer. |
| 160 | // This is the format used for the sint32 protocol buffer type. |
| 161 | func (p *Buffer) DecodeZigzag32() (x uint64, err os.Error) { |
| 162 | x, err = p.DecodeVarint() |
| 163 | if err != nil { |
| 164 | return |
| 165 | } |
| 166 | x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31)) |
| 167 | return |
| 168 | } |
| 169 | |
| 170 | // These are not ValueDecoders: they produce an array of bytes or a string. |
| 171 | // bytes, embedded messages |
| 172 | |
| 173 | // DecodeRawBytes reads a count-delimited byte buffer from the Buffer. |
| 174 | // This is the format used for the bytes protocol buffer |
| 175 | // type and for embedded messages. |
| 176 | func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err os.Error) { |
| 177 | n, err := p.DecodeVarint() |
| 178 | if err != nil { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | nb := int(n) |
| 183 | if p.index+nb > len(p.buf) { |
| 184 | err = io.ErrUnexpectedEOF |
| 185 | return |
| 186 | } |
| 187 | |
| 188 | if !alloc { |
| 189 | // todo: check if can get more uses of alloc=false |
| 190 | buf = p.buf[p.index : p.index+nb] |
| 191 | p.index += nb |
| 192 | return |
| 193 | } |
| 194 | |
| 195 | buf = make([]byte, nb) |
| 196 | copy(buf, p.buf[p.index:]) |
| 197 | p.index += nb |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | // DecodeStringBytes reads an encoded string from the Buffer. |
| 202 | // This is the format used for the proto2 string type. |
| 203 | func (p *Buffer) DecodeStringBytes() (s string, err os.Error) { |
| 204 | buf, err := p.DecodeRawBytes(false) |
| 205 | if err != nil { |
| 206 | return |
| 207 | } |
| 208 | return string(buf), nil |
| 209 | } |
| 210 | |
| 211 | // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. |
| 212 | // If the protocol buffer has extensions, and the field matches, add it as an extension. |
| 213 | // Otherwise, if the XXX_unrecognized field exists, append the skipped data there. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 214 | func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 215 | |
| 216 | oi := o.index |
| 217 | |
| 218 | err := o.skip(t, tag, wire) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | |
| 223 | x := fieldIndex(t, "XXX_unrecognized") |
| 224 | if x == nil { |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | p := propByIndex(t, x) |
| 229 | ptr := (*[]byte)(unsafe.Pointer(base + p.offset)) |
| 230 | |
| 231 | if *ptr == nil { |
| 232 | // This is the first skipped element, |
| 233 | // allocate a new buffer. |
| 234 | *ptr = o.bufalloc() |
| 235 | } |
| 236 | |
| 237 | // Add the skipped field to struct field |
| 238 | obuf := o.buf |
| 239 | |
| 240 | o.buf = *ptr |
| 241 | o.EncodeVarint(uint64(tag<<3 | wire)) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 242 | *ptr = append(o.buf, obuf[oi:o.index]...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 243 | |
| 244 | o.buf = obuf |
| 245 | |
| 246 | return nil |
| 247 | } |
| 248 | |
| 249 | // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 250 | func (o *Buffer) skip(t reflect.Type, tag, wire int) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 251 | |
| 252 | var u uint64 |
| 253 | var err os.Error |
| 254 | |
| 255 | switch wire { |
| 256 | case WireVarint: |
| 257 | _, err = o.DecodeVarint() |
| 258 | case WireFixed64: |
| 259 | _, err = o.DecodeFixed64() |
| 260 | case WireBytes: |
| 261 | _, err = o.DecodeRawBytes(false) |
| 262 | case WireFixed32: |
| 263 | _, err = o.DecodeFixed32() |
| 264 | case WireStartGroup: |
| 265 | for { |
| 266 | u, err = o.DecodeVarint() |
| 267 | if err != nil { |
| 268 | break |
| 269 | } |
| 270 | fwire := int(u & 0x7) |
| 271 | if fwire == WireEndGroup { |
| 272 | break |
| 273 | } |
| 274 | ftag := int(u >> 3) |
| 275 | err = o.skip(t, ftag, fwire) |
| 276 | if err != nil { |
| 277 | break |
| 278 | } |
| 279 | } |
| 280 | default: |
| 281 | fmt.Fprintf(os.Stderr, "proto: can't skip wire type %d for %s\n", wire, t) |
| 282 | } |
| 283 | return err |
| 284 | } |
| 285 | |
| 286 | // Unmarshaler is the interface representing objects that can unmarshal themselves. |
| 287 | type Unmarshaler interface { |
| 288 | Unmarshal([]byte) os.Error |
| 289 | } |
| 290 | |
| 291 | // Unmarshal parses the protocol buffer representation in buf and places the |
| 292 | // decoded result in pb. If the struct underlying pb does not match |
| 293 | // the data in buf, the results can be unpredictable. |
| 294 | func Unmarshal(buf []byte, pb interface{}) os.Error { |
| 295 | // If the object can unmarshal itself, let it. |
| 296 | if u, ok := pb.(Unmarshaler); ok { |
| 297 | return u.Unmarshal(buf) |
| 298 | } |
| 299 | |
| 300 | return NewBuffer(buf).Unmarshal(pb) |
| 301 | } |
| 302 | |
| 303 | // Unmarshal parses the protocol buffer representation in the |
| 304 | // Buffer and places the decoded result in pb. If the struct |
| 305 | // underlying pb does not match the data in the buffer, the results can be |
| 306 | // unpredictable. |
| 307 | func (p *Buffer) Unmarshal(pb interface{}) os.Error { |
| 308 | // If the object can unmarshal itself, let it. |
| 309 | if u, ok := pb.(Unmarshaler); ok { |
| 310 | err := u.Unmarshal(p.buf[p.index:]) |
| 311 | p.index = len(p.buf) |
| 312 | return err |
| 313 | } |
| 314 | |
| 315 | mstat := runtime.MemStats.Mallocs |
| 316 | |
| 317 | typ, base, err := getbase(pb) |
| 318 | if err != nil { |
| 319 | return err |
| 320 | } |
| 321 | |
| 322 | err = p.unmarshalType(typ, false, base) |
| 323 | |
| 324 | mstat = runtime.MemStats.Mallocs - mstat |
| 325 | stats.Dmalloc += mstat |
| 326 | stats.Decode++ |
| 327 | |
| 328 | return err |
| 329 | } |
| 330 | |
| 331 | // unmarshalType does the work of unmarshaling a structure. |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 332 | func (o *Buffer) unmarshalType(t reflect.Type, is_group bool, base uintptr) os.Error { |
| 333 | st := t.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 334 | prop := GetProperties(st) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 335 | required, reqFields := prop.reqCount, uint64(0) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 336 | |
| 337 | var err os.Error |
| 338 | for err == nil && o.index < len(o.buf) { |
| 339 | oi := o.index |
| 340 | var u uint64 |
| 341 | u, err = o.DecodeVarint() |
| 342 | if err != nil { |
| 343 | break |
| 344 | } |
| 345 | wire := int(u & 0x7) |
| 346 | if wire == WireEndGroup { |
| 347 | if is_group { |
| 348 | return nil // input is satisfied |
| 349 | } |
| 350 | return ErrWrongType |
| 351 | } |
| 352 | tag := int(u >> 3) |
| 353 | fieldnum, ok := prop.tags[tag] |
| 354 | if !ok { |
| 355 | // Maybe it's an extension? |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 356 | o.ptr = base // copy the address here to avoid a heap allocation. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 357 | iv := unsafe.Unreflect(t, unsafe.Pointer(&o.ptr)) |
| 358 | if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) { |
| 359 | if err = o.skip(st, tag, wire); err == nil { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 360 | e.ExtensionMap()[int32(tag)] = Extension{enc: append([]byte(nil), o.buf[oi:o.index]...)} |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 361 | } |
| 362 | continue |
| 363 | } |
| 364 | err = o.skipAndSave(st, tag, wire, base) |
| 365 | continue |
| 366 | } |
| 367 | p := prop.Prop[fieldnum] |
| 368 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 369 | if p.dec == nil { |
| 370 | fmt.Fprintf(os.Stderr, "no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 371 | continue |
| 372 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 373 | dec := p.dec |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 374 | if wire != WireStartGroup && wire != p.WireType { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 375 | if wire == WireBytes && p.packedDec != nil { |
| 376 | // a packable field |
| 377 | dec = p.packedDec |
| 378 | } else { |
| 379 | err = ErrWrongType |
| 380 | continue |
| 381 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 382 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 383 | err = dec(o, p, base) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 384 | if err == nil && p.Required { |
| 385 | // Successfully decoded a required field. |
| 386 | if tag <= 64 { |
| 387 | // use bitmap for fields 1-64 to catch field reuse. |
| 388 | var mask uint64 = 1 << uint64(tag-1) |
| 389 | if reqFields&mask == 0 { |
| 390 | // new required field |
| 391 | reqFields |= mask |
| 392 | required-- |
| 393 | } |
| 394 | } else { |
| 395 | // This is imprecise. It can be fooled by a required field |
| 396 | // with a tag > 64 that is encoded twice; that's very rare. |
| 397 | // A fully correct implementation would require allocating |
| 398 | // a data structure, which we would like to avoid. |
| 399 | required-- |
| 400 | } |
| 401 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 402 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 403 | if err == nil { |
| 404 | if is_group { |
| 405 | return io.ErrUnexpectedEOF |
| 406 | } |
| 407 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 408 | return &ErrRequiredNotSet{st} |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 409 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 410 | } |
| 411 | return err |
| 412 | } |
| 413 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 414 | // Individual type decoders |
| 415 | // For each, |
| 416 | // u is the decoded value, |
| 417 | // v is a pointer to the field (pointer) in the struct |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 418 | |
| 419 | // Sizes of the pools to allocate inside the Buffer. |
| Rob Pike | 97edc7e | 2011-10-20 15:53:19 -0700 | [diff] [blame] | 420 | // The goal is modest amortization and allocation |
| 421 | // on at least 16-byte boundaries. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 422 | const ( |
| David Symonds | 049646b | 2011-10-21 11:13:45 +1100 | [diff] [blame^] | 423 | boolPoolSize = 16 |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 424 | int32PoolSize = 8 |
| 425 | int64PoolSize = 4 |
| 426 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 427 | |
| 428 | // Decode a bool. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 429 | func (o *Buffer) dec_bool(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 430 | u, err := p.valDec(o) |
| 431 | if err != nil { |
| 432 | return err |
| 433 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 434 | if len(o.bools) == 0 { |
| 435 | o.bools = make([]bool, boolPoolSize) |
| 436 | } |
| 437 | o.bools[0] = u != 0 |
| 438 | v := (**bool)(unsafe.Pointer(base + p.offset)) |
| 439 | *v = &o.bools[0] |
| 440 | o.bools = o.bools[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 441 | return nil |
| 442 | } |
| 443 | |
| 444 | // Decode an int32. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 445 | func (o *Buffer) dec_int32(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 446 | u, err := p.valDec(o) |
| 447 | if err != nil { |
| 448 | return err |
| 449 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 450 | if len(o.int32s) == 0 { |
| 451 | o.int32s = make([]int32, int32PoolSize) |
| 452 | } |
| 453 | o.int32s[0] = int32(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 454 | v := (**int32)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 455 | *v = &o.int32s[0] |
| 456 | o.int32s = o.int32s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 457 | return nil |
| 458 | } |
| 459 | |
| 460 | // Decode an int64. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 461 | func (o *Buffer) dec_int64(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 462 | u, err := p.valDec(o) |
| 463 | if err != nil { |
| 464 | return err |
| 465 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 466 | if len(o.int64s) == 0 { |
| 467 | o.int64s = make([]int64, int64PoolSize) |
| 468 | } |
| 469 | o.int64s[0] = int64(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 470 | v := (**int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 471 | *v = &o.int64s[0] |
| 472 | o.int64s = o.int64s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 473 | return nil |
| 474 | } |
| 475 | |
| 476 | // Decode a string. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 477 | func (o *Buffer) dec_string(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 478 | s, err := o.DecodeStringBytes() |
| 479 | if err != nil { |
| 480 | return err |
| 481 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 482 | sp := new(string) |
| 483 | *sp = s |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 484 | v := (**string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 485 | *v = sp |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 486 | return nil |
| 487 | } |
| 488 | |
| 489 | // Decode a slice of bytes ([]byte). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 490 | func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) os.Error { |
| 491 | b, err := o.DecodeRawBytes(true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 492 | if err != nil { |
| 493 | return err |
| 494 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 495 | v := (*[]byte)(unsafe.Pointer(base + p.offset)) |
| 496 | *v = b |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | return nil |
| 498 | } |
| 499 | |
| 500 | // Decode a slice of bools ([]bool). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 501 | func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 502 | u, err := p.valDec(o) |
| 503 | if err != nil { |
| 504 | return err |
| 505 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 506 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 507 | *v = append(*v, u != 0) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 508 | return nil |
| 509 | } |
| 510 | |
| 511 | // Decode a slice of bools ([]bool) in packed format. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 512 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) os.Error { |
| 513 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 514 | |
| 515 | nn, err := o.DecodeVarint() |
| 516 | if err != nil { |
| 517 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 518 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 519 | nb := int(nn) // number of bytes of encoded bools |
| 520 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 521 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 522 | for i := 0; i < nb; i++ { |
| 523 | u, err := p.valDec(o) |
| 524 | if err != nil { |
| 525 | return err |
| 526 | } |
| 527 | y = append(y, u != 0) |
| 528 | } |
| 529 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 530 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 531 | return nil |
| 532 | } |
| 533 | |
| 534 | // Decode a slice of int32s ([]int32). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 535 | func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 536 | u, err := p.valDec(o) |
| 537 | if err != nil { |
| 538 | return err |
| 539 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 540 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 541 | *v = append(*v, int32(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 542 | return nil |
| 543 | } |
| 544 | |
| 545 | // Decode a slice of int32s ([]int32) in packed format. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 546 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) os.Error { |
| 547 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 548 | |
| 549 | nn, err := o.DecodeVarint() |
| 550 | if err != nil { |
| 551 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 552 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 553 | nb := int(nn) // number of bytes of encoded int32s |
| 554 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 555 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 556 | |
| 557 | fin := o.index + nb |
| 558 | for o.index < fin { |
| 559 | u, err := p.valDec(o) |
| 560 | if err != nil { |
| 561 | return err |
| 562 | } |
| 563 | y = append(y, int32(u)) |
| 564 | } |
| 565 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 566 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 567 | return nil |
| 568 | } |
| 569 | |
| 570 | // Decode a slice of int64s ([]int64). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 571 | func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 572 | u, err := p.valDec(o) |
| 573 | if err != nil { |
| 574 | return err |
| 575 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 576 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 577 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 578 | y := *v |
| 579 | *v = append(y, int64(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 580 | return nil |
| 581 | } |
| 582 | |
| 583 | // Decode a slice of int64s ([]int64) in packed format. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 584 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) os.Error { |
| 585 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 586 | |
| 587 | nn, err := o.DecodeVarint() |
| 588 | if err != nil { |
| 589 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 590 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 591 | nb := int(nn) // number of bytes of encoded int64s |
| 592 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 593 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 594 | fin := o.index + nb |
| 595 | for o.index < fin { |
| 596 | u, err := p.valDec(o) |
| 597 | if err != nil { |
| 598 | return err |
| 599 | } |
| 600 | y = append(y, int64(u)) |
| 601 | } |
| 602 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 603 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 604 | return nil |
| 605 | } |
| 606 | |
| 607 | // Decode a slice of strings ([]string). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 608 | func (o *Buffer) dec_slice_string(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 609 | s, err := o.DecodeStringBytes() |
| 610 | if err != nil { |
| 611 | return err |
| 612 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 613 | v := (*[]string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 614 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 615 | y := *v |
| 616 | *v = append(y, s) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 617 | return nil |
| 618 | } |
| 619 | |
| 620 | // Decode a slice of slice of bytes ([][]byte). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 621 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 622 | b, err := o.DecodeRawBytes(true) |
| 623 | if err != nil { |
| 624 | return err |
| 625 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 626 | v := (*[][]byte)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 627 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 628 | y := *v |
| 629 | *v = append(y, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 630 | return nil |
| 631 | } |
| 632 | |
| 633 | // Decode a group. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 634 | func (o *Buffer) dec_struct_group(p *Properties, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 635 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 636 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 637 | structv := unsafe.New(typ) |
| 638 | bas := uintptr(structv) |
| 639 | *ptr = (*struct{})(structv) |
| 640 | |
| 641 | err := o.unmarshalType(p.stype, true, bas) |
| 642 | |
| 643 | return err |
| 644 | } |
| 645 | |
| 646 | // Decode an embedded message. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 647 | func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err os.Error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 648 | raw, e := o.DecodeRawBytes(false) |
| 649 | if e != nil { |
| 650 | return e |
| 651 | } |
| 652 | |
| 653 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 654 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 655 | structv := unsafe.New(typ) |
| 656 | bas := uintptr(structv) |
| 657 | *ptr = (*struct{})(structv) |
| 658 | |
| 659 | // If the object can unmarshal itself, let it. |
| 660 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr)) |
| 661 | if u, ok := iv.(Unmarshaler); ok { |
| 662 | return u.Unmarshal(raw) |
| 663 | } |
| 664 | |
| 665 | obuf := o.buf |
| 666 | oi := o.index |
| 667 | o.buf = raw |
| 668 | o.index = 0 |
| 669 | |
| 670 | err = o.unmarshalType(p.stype, false, bas) |
| 671 | o.buf = obuf |
| 672 | o.index = oi |
| 673 | |
| 674 | return err |
| 675 | } |
| 676 | |
| 677 | // Decode a slice of embedded messages. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 678 | func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) os.Error { |
| 679 | return o.dec_slice_struct(p, false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Decode a slice of embedded groups. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 683 | func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) os.Error { |
| 684 | return o.dec_slice_struct(p, true, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // Decode a slice of structs ([]*struct). |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 688 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr) os.Error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 689 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 690 | v := (*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 691 | y := *v |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 692 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 693 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 694 | structv := unsafe.New(typ) |
| 695 | bas := uintptr(structv) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 696 | y = append(y, (*struct{})(structv)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 697 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 698 | |
| 699 | if is_group { |
| 700 | err := o.unmarshalType(p.stype, is_group, bas) |
| 701 | return err |
| 702 | } |
| 703 | |
| 704 | raw, err := o.DecodeRawBytes(true) |
| 705 | if err != nil { |
| 706 | return err |
| 707 | } |
| 708 | |
| 709 | // If the object can unmarshal itself, let it. |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 710 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[len(y)-1])) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 711 | if u, ok := iv.(Unmarshaler); ok { |
| 712 | return u.Unmarshal(raw) |
| 713 | } |
| 714 | |
| 715 | obuf := o.buf |
| 716 | oi := o.index |
| 717 | o.buf = raw |
| 718 | o.index = 0 |
| 719 | |
| 720 | err = o.unmarshalType(p.stype, is_group, bas) |
| 721 | |
| 722 | o.buf = obuf |
| 723 | o.index = oi |
| 724 | |
| 725 | return err |
| 726 | } |