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 | } |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 375 | if wire != WireStartGroup && wire != p.WireType { |
| 376 | err = ErrWrongType |
| 377 | continue |
| 378 | } |
| 379 | err = p.dec(o, p, base, sbase) |
| 380 | if err == nil && p.Required { |
| 381 | // Successfully decoded a required field. |
| 382 | if tag <= 64 { |
| 383 | // use bitmap for fields 1-64 to catch field reuse. |
| 384 | var mask uint64 = 1 << uint64(tag-1) |
| 385 | if reqFields&mask == 0 { |
| 386 | // new required field |
| 387 | reqFields |= mask |
| 388 | required-- |
| 389 | } |
| 390 | } else { |
| 391 | // This is imprecise. It can be fooled by a required field |
| 392 | // with a tag > 64 that is encoded twice; that's very rare. |
| 393 | // A fully correct implementation would require allocating |
| 394 | // a data structure, which we would like to avoid. |
| 395 | required-- |
| 396 | } |
| 397 | } |
Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 398 | } |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 399 | if err == nil { |
| 400 | if is_group { |
| 401 | return io.ErrUnexpectedEOF |
| 402 | } |
| 403 | if required > 0 { |
| 404 | return ErrRequiredNotSet |
| 405 | } |
Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 406 | } |
| 407 | return err |
| 408 | } |
| 409 | |
| 410 | // Make *pslice have base address base, length 0, and capacity startSize. |
| 411 | func initSlice(pslice unsafe.Pointer, base uintptr) { |
| 412 | sp := (*reflect.SliceHeader)(pslice) |
| 413 | sp.Data = base |
| 414 | sp.Len = 0 |
| 415 | sp.Cap = startSize |
| 416 | } |
| 417 | |
| 418 | // Individual type decoders |
| 419 | // For each, |
| 420 | // u is the decoded value, |
| 421 | // v is a pointer to the field (pointer) in the struct |
| 422 | // x is a pointer to the preallocated scratch space to hold the decoded value. |
| 423 | |
| 424 | // Decode a bool. |
| 425 | func (o *Buffer) dec_bool(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 426 | u, err := p.valDec(o) |
| 427 | if err != nil { |
| 428 | return err |
| 429 | } |
| 430 | v := (**uint8)(unsafe.Pointer(base + p.offset)) |
| 431 | x := (*uint8)(unsafe.Pointer(sbase + p.scratch)) |
| 432 | *x = uint8(u) |
| 433 | *v = x |
| 434 | return nil |
| 435 | } |
| 436 | |
| 437 | // Decode an int32. |
| 438 | func (o *Buffer) dec_int32(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 439 | u, err := p.valDec(o) |
| 440 | if err != nil { |
| 441 | return err |
| 442 | } |
| 443 | v := (**int32)(unsafe.Pointer(base + p.offset)) |
| 444 | x := (*int32)(unsafe.Pointer(sbase + p.scratch)) |
| 445 | *x = int32(u) |
| 446 | *v = x |
| 447 | return nil |
| 448 | } |
| 449 | |
| 450 | // Decode an int64. |
| 451 | func (o *Buffer) dec_int64(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 452 | u, err := p.valDec(o) |
| 453 | if err != nil { |
| 454 | return err |
| 455 | } |
| 456 | v := (**int64)(unsafe.Pointer(base + p.offset)) |
| 457 | x := (*int64)(unsafe.Pointer(sbase + p.scratch)) |
| 458 | *x = int64(u) |
| 459 | *v = x |
| 460 | return nil |
| 461 | } |
| 462 | |
| 463 | // Decode a string. |
| 464 | func (o *Buffer) dec_string(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 465 | s, err := o.DecodeStringBytes() |
| 466 | if err != nil { |
| 467 | return err |
| 468 | } |
| 469 | v := (**string)(unsafe.Pointer(base + p.offset)) |
| 470 | x := (*string)(unsafe.Pointer(sbase + p.scratch)) |
| 471 | *x = s |
| 472 | *v = x |
| 473 | return nil |
| 474 | } |
| 475 | |
| 476 | // Decode a slice of bytes ([]byte). |
| 477 | func (o *Buffer) dec_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 478 | b, err := o.DecodeRawBytes(false) |
| 479 | if err != nil { |
| 480 | return err |
| 481 | } |
| 482 | lb := len(b) |
| 483 | if lb == 0 { |
| 484 | return nil |
| 485 | } |
| 486 | x := (*[]uint8)(unsafe.Pointer(base + p.offset)) |
| 487 | |
| 488 | y := *x |
| 489 | c := cap(y) |
| 490 | if c == 0 { |
| 491 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 492 | y = *x |
| 493 | c = cap(y) |
| 494 | } |
| 495 | |
| 496 | l := len(y) |
| 497 | if l+lb > c { |
| 498 | // incremental growth is max(len(slice)*1.5, len(slice)+len(bytes)) |
| 499 | g := l * 3 / 2 |
| 500 | if l+lb > g { |
| 501 | g = l + lb |
| 502 | } |
| 503 | z := make([]uint8, l, g) |
| 504 | copy(z, y) |
| 505 | y = z |
| 506 | } |
| 507 | |
| 508 | y = y[0 : l+lb] |
| 509 | copy(y[l:l+lb], b) |
| 510 | |
| 511 | *x = y |
| 512 | return nil |
| 513 | } |
| 514 | |
| 515 | // Decode a slice of bools ([]bool). |
| 516 | func (o *Buffer) dec_slice_bool(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 517 | u, err := p.valDec(o) |
| 518 | if err != nil { |
| 519 | return err |
| 520 | } |
| 521 | x := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 522 | |
| 523 | y := *x |
| 524 | c := cap(y) |
| 525 | if c == 0 { |
| 526 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 527 | y = *x |
| 528 | c = cap(y) |
| 529 | } |
| 530 | l := len(y) |
| 531 | if l >= c { |
| 532 | g := l * 3 / 2 |
| 533 | z := make([]bool, l, g) |
| 534 | copy(z, y) |
| 535 | y = z |
| 536 | } |
| 537 | y = y[0 : l+1] |
| 538 | y[l] = u != 0 |
| 539 | *x = y |
| 540 | return nil |
| 541 | } |
| 542 | |
| 543 | // Decode a slice of int32s ([]int32). |
| 544 | func (o *Buffer) dec_slice_int32(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 545 | u, err := p.valDec(o) |
| 546 | if err != nil { |
| 547 | return err |
| 548 | } |
| 549 | x := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 550 | |
| 551 | y := *x |
| 552 | c := cap(y) |
| 553 | if c == 0 { |
| 554 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 555 | y = *x |
| 556 | c = cap(y) |
| 557 | } |
| 558 | l := len(y) |
| 559 | if l >= c { |
| 560 | g := l * 3 / 2 |
| 561 | z := make([]int32, l, g) |
| 562 | copy(z, y) |
| 563 | y = z |
| 564 | } |
| 565 | y = y[0 : l+1] |
| 566 | y[l] = int32(u) |
| 567 | *x = y |
| 568 | return nil |
| 569 | } |
| 570 | |
| 571 | // Decode a slice of int64s ([]int64). |
| 572 | func (o *Buffer) dec_slice_int64(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 573 | u, err := p.valDec(o) |
| 574 | if err != nil { |
| 575 | return err |
| 576 | } |
| 577 | x := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| 578 | |
| 579 | y := *x |
| 580 | c := cap(y) |
| 581 | if c == 0 { |
| 582 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 583 | y = *x |
| 584 | c = cap(y) |
| 585 | } |
| 586 | l := len(y) |
| 587 | if l >= c { |
| 588 | g := l * 3 / 2 |
| 589 | z := make([]int64, l, g) |
| 590 | copy(z, y) |
| 591 | y = z |
| 592 | } |
| 593 | y = y[0 : l+1] |
| 594 | y[l] = int64(u) |
| 595 | *x = y |
| 596 | return nil |
| 597 | } |
| 598 | |
| 599 | // Decode a slice of strings ([]string). |
| 600 | func (o *Buffer) dec_slice_string(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 601 | s, err := o.DecodeStringBytes() |
| 602 | if err != nil { |
| 603 | return err |
| 604 | } |
| 605 | x := (*[]string)(unsafe.Pointer(base + p.offset)) |
| 606 | |
| 607 | y := *x |
| 608 | c := cap(y) |
| 609 | if c == 0 { |
| 610 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 611 | y = *x |
| 612 | c = cap(y) |
| 613 | } |
| 614 | l := len(y) |
| 615 | if l >= c { |
| 616 | g := l * 3 / 2 |
| 617 | z := make([]string, l, g) |
| 618 | copy(z, y) |
| 619 | y = z |
| 620 | } |
| 621 | y = y[0 : l+1] |
| 622 | y[l] = s |
| 623 | *x = y |
| 624 | return nil |
| 625 | } |
| 626 | |
| 627 | // Decode a slice of slice of bytes ([][]byte). |
| 628 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 629 | b, err := o.DecodeRawBytes(true) |
| 630 | if err != nil { |
| 631 | return err |
| 632 | } |
| 633 | x := (*[][]byte)(unsafe.Pointer(base + p.offset)) |
| 634 | |
| 635 | y := *x |
| 636 | c := cap(y) |
| 637 | if c == 0 { |
| 638 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 639 | y = *x |
| 640 | c = cap(y) |
| 641 | } |
| 642 | l := len(y) |
| 643 | if l >= c { |
| 644 | g := l * 3 / 2 |
| 645 | z := make([][]byte, l, g) |
| 646 | copy(z, y) |
| 647 | y = z |
| 648 | } |
| 649 | y = y[0 : l+1] |
| 650 | y[l] = b |
| 651 | *x = y |
| 652 | return nil |
| 653 | } |
| 654 | |
| 655 | // Decode a group. |
| 656 | func (o *Buffer) dec_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 657 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| 658 | typ := p.stype.Elem().(*reflect.StructType) |
| 659 | structv := unsafe.New(typ) |
| 660 | bas := uintptr(structv) |
| 661 | *ptr = (*struct{})(structv) |
| 662 | |
| 663 | err := o.unmarshalType(p.stype, true, bas) |
| 664 | |
| 665 | return err |
| 666 | } |
| 667 | |
| 668 | // Decode an embedded message. |
| 669 | func (o *Buffer) dec_struct_message(p *Properties, base uintptr, sbase uintptr) (err os.Error) { |
| 670 | raw, e := o.DecodeRawBytes(false) |
| 671 | if e != nil { |
| 672 | return e |
| 673 | } |
| 674 | |
| 675 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| 676 | typ := p.stype.Elem().(*reflect.StructType) |
| 677 | structv := unsafe.New(typ) |
| 678 | bas := uintptr(structv) |
| 679 | *ptr = (*struct{})(structv) |
| 680 | |
| 681 | // If the object can unmarshal itself, let it. |
| 682 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr)) |
| 683 | if u, ok := iv.(Unmarshaler); ok { |
| 684 | return u.Unmarshal(raw) |
| 685 | } |
| 686 | |
| 687 | obuf := o.buf |
| 688 | oi := o.index |
| 689 | o.buf = raw |
| 690 | o.index = 0 |
| 691 | |
| 692 | err = o.unmarshalType(p.stype, false, bas) |
| 693 | o.buf = obuf |
| 694 | o.index = oi |
| 695 | |
| 696 | return err |
| 697 | } |
| 698 | |
| 699 | // Decode a slice of embedded messages. |
| 700 | func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 701 | return o.dec_slice_struct(p, false, base, sbase) |
| 702 | } |
| 703 | |
| 704 | // Decode a slice of embedded groups. |
| 705 | func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 706 | return o.dec_slice_struct(p, true, base, sbase) |
| 707 | } |
| 708 | |
| 709 | // Decode a slice of structs ([]*struct). |
| 710 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr, sbase uintptr) os.Error { |
| 711 | |
| 712 | x := (*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 713 | y := *x |
| 714 | c := cap(y) |
| 715 | if c == 0 { |
| 716 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 717 | y = *x |
| 718 | c = cap(y) |
| 719 | } |
| 720 | |
| 721 | l := len(y) |
| 722 | if l >= c { |
| 723 | // Create a new slice with 1.5X the capacity. |
| 724 | g := l * 3 / 2 |
| 725 | z := make([]*struct{}, l, g) |
| 726 | copy(z, y) |
| 727 | y = z |
| 728 | } |
| 729 | y = y[0 : l+1] |
| 730 | *x = y |
| 731 | |
| 732 | typ := p.stype.Elem().(*reflect.StructType) |
| 733 | structv := unsafe.New(typ) |
| 734 | bas := uintptr(structv) |
| 735 | y[l] = (*struct{})(structv) |
| 736 | |
| 737 | if is_group { |
| 738 | err := o.unmarshalType(p.stype, is_group, bas) |
| 739 | return err |
| 740 | } |
| 741 | |
| 742 | raw, err := o.DecodeRawBytes(true) |
| 743 | if err != nil { |
| 744 | return err |
| 745 | } |
| 746 | |
| 747 | // If the object can unmarshal itself, let it. |
| 748 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[l])) |
| 749 | if u, ok := iv.(Unmarshaler); ok { |
| 750 | return u.Unmarshal(raw) |
| 751 | } |
| 752 | |
| 753 | obuf := o.buf |
| 754 | oi := o.index |
| 755 | o.buf = raw |
| 756 | o.index = 0 |
| 757 | |
| 758 | err = o.unmarshalType(p.stype, is_group, bas) |
| 759 | |
| 760 | o.buf = obuf |
| 761 | o.index = oi |
| 762 | |
| 763 | return err |
| 764 | } |