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