| 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 | sbase := getsbase(prop) // scratch area for data items |
| 337 | |
| 338 | var err os.Error |
| 339 | for err == nil && o.index < len(o.buf) { |
| 340 | oi := o.index |
| 341 | var u uint64 |
| 342 | u, err = o.DecodeVarint() |
| 343 | if err != nil { |
| 344 | break |
| 345 | } |
| 346 | wire := int(u & 0x7) |
| 347 | if wire == WireEndGroup { |
| 348 | if is_group { |
| 349 | return nil // input is satisfied |
| 350 | } |
| 351 | return ErrWrongType |
| 352 | } |
| 353 | tag := int(u >> 3) |
| 354 | fieldnum, ok := prop.tags[tag] |
| 355 | if !ok { |
| 356 | // Maybe it's an extension? |
| 357 | o.ptr = base |
| 358 | iv := unsafe.Unreflect(t, unsafe.Pointer(&o.ptr)) |
| 359 | if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) { |
| 360 | if err = o.skip(st, tag, wire); err == nil { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 361 | e.ExtensionMap()[int32(tag)] = append([]byte(nil), o.buf[oi:o.index]...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 362 | } |
| 363 | continue |
| 364 | } |
| 365 | err = o.skipAndSave(st, tag, wire, base) |
| 366 | continue |
| 367 | } |
| 368 | p := prop.Prop[fieldnum] |
| 369 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 370 | if p.dec == nil { |
| 371 | 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] | 372 | continue |
| 373 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 374 | dec := p.dec |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 375 | if wire != WireStartGroup && wire != p.WireType { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 376 | if wire == WireBytes && p.packedDec != nil { |
| 377 | // a packable field |
| 378 | dec = p.packedDec |
| 379 | } else { |
| 380 | err = ErrWrongType |
| 381 | continue |
| 382 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 383 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 384 | err = dec(o, p, base, sbase) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 385 | if err == nil && p.Required { |
| 386 | // Successfully decoded a required field. |
| 387 | if tag <= 64 { |
| 388 | // use bitmap for fields 1-64 to catch field reuse. |
| 389 | var mask uint64 = 1 << uint64(tag-1) |
| 390 | if reqFields&mask == 0 { |
| 391 | // new required field |
| 392 | reqFields |= mask |
| 393 | required-- |
| 394 | } |
| 395 | } else { |
| 396 | // This is imprecise. It can be fooled by a required field |
| 397 | // with a tag > 64 that is encoded twice; that's very rare. |
| 398 | // A fully correct implementation would require allocating |
| 399 | // a data structure, which we would like to avoid. |
| 400 | required-- |
| 401 | } |
| 402 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 403 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 404 | if err == nil { |
| 405 | if is_group { |
| 406 | return io.ErrUnexpectedEOF |
| 407 | } |
| 408 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 409 | return &ErrRequiredNotSet{st} |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 410 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 411 | } |
| 412 | return err |
| 413 | } |
| 414 | |
| 415 | // Make *pslice have base address base, length 0, and capacity startSize. |
| 416 | func initSlice(pslice unsafe.Pointer, base uintptr) { |
| 417 | sp := (*reflect.SliceHeader)(pslice) |
| 418 | sp.Data = base |
| 419 | sp.Len = 0 |
| 420 | sp.Cap = startSize |
| 421 | } |
| 422 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 423 | // Make *pslice have base address base, length 0, and capacity max(startSize, n). |
| 424 | func initSlicen(pslice unsafe.Pointer, base uintptr, n int) { |
| 425 | if n < startSize { |
| 426 | n = startSize |
| 427 | } |
| 428 | sp := (*reflect.SliceHeader)(pslice) |
| 429 | sp.Data = base |
| 430 | sp.Len = 0 |
| 431 | sp.Cap = n |
| 432 | } |
| 433 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 434 | // Individual type decoders |
| 435 | // For each, |
| 436 | // u is the decoded value, |
| 437 | // v is a pointer to the field (pointer) in the struct |
| 438 | // x is a pointer to the preallocated scratch space to hold the decoded value. |
| 439 | |
| 440 | // Decode a bool. |
| 441 | func (o *Buffer) dec_bool(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 442 | u, err := p.valDec(o) |
| 443 | if err != nil { |
| 444 | return err |
| 445 | } |
| 446 | v := (**uint8)(unsafe.Pointer(base + p.offset)) |
| 447 | x := (*uint8)(unsafe.Pointer(sbase + p.scratch)) |
| 448 | *x = uint8(u) |
| 449 | *v = x |
| 450 | return nil |
| 451 | } |
| 452 | |
| 453 | // Decode an int32. |
| 454 | func (o *Buffer) dec_int32(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 455 | u, err := p.valDec(o) |
| 456 | if err != nil { |
| 457 | return err |
| 458 | } |
| 459 | v := (**int32)(unsafe.Pointer(base + p.offset)) |
| 460 | x := (*int32)(unsafe.Pointer(sbase + p.scratch)) |
| 461 | *x = int32(u) |
| 462 | *v = x |
| 463 | return nil |
| 464 | } |
| 465 | |
| 466 | // Decode an int64. |
| 467 | func (o *Buffer) dec_int64(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 468 | u, err := p.valDec(o) |
| 469 | if err != nil { |
| 470 | return err |
| 471 | } |
| 472 | v := (**int64)(unsafe.Pointer(base + p.offset)) |
| 473 | x := (*int64)(unsafe.Pointer(sbase + p.scratch)) |
| 474 | *x = int64(u) |
| 475 | *v = x |
| 476 | return nil |
| 477 | } |
| 478 | |
| 479 | // Decode a string. |
| 480 | func (o *Buffer) dec_string(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 481 | s, err := o.DecodeStringBytes() |
| 482 | if err != nil { |
| 483 | return err |
| 484 | } |
| 485 | v := (**string)(unsafe.Pointer(base + p.offset)) |
| 486 | x := (*string)(unsafe.Pointer(sbase + p.scratch)) |
| 487 | *x = s |
| 488 | *v = x |
| 489 | return nil |
| 490 | } |
| 491 | |
| 492 | // Decode a slice of bytes ([]byte). |
| 493 | func (o *Buffer) dec_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 494 | b, err := o.DecodeRawBytes(false) |
| 495 | if err != nil { |
| 496 | return err |
| 497 | } |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 498 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 499 | x := (*[]uint8)(unsafe.Pointer(base + p.offset)) |
| 500 | |
| 501 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 502 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 503 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 504 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 507 | *x = append(y, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 508 | return nil |
| 509 | } |
| 510 | |
| 511 | // Decode a slice of bools ([]bool). |
| 512 | func (o *Buffer) dec_slice_bool(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 513 | u, err := p.valDec(o) |
| 514 | if err != nil { |
| 515 | return err |
| 516 | } |
| 517 | x := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 518 | |
| 519 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 520 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 521 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 522 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 523 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 524 | |
| 525 | *x = append(y, u != 0) |
| 526 | return nil |
| 527 | } |
| 528 | |
| 529 | // Decode a slice of bools ([]bool) in packed format. |
| 530 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 531 | x := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 532 | |
| 533 | nn, err := o.DecodeVarint() |
| 534 | if err != nil { |
| 535 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 536 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 537 | nb := int(nn) // number of bytes of encoded bools |
| 538 | |
| 539 | y := *x |
| 540 | if cap(y) == 0 { |
| 541 | // Packed fields are usually only encoded once, |
| 542 | // so this branch is almost always executed. |
| 543 | // The append in the loop below takes care of other cases. |
| 544 | initSlicen(unsafe.Pointer(x), sbase+p.scratch, nb) |
| 545 | y = *x |
| 546 | } |
| 547 | |
| 548 | for i := 0; i < nb; i++ { |
| 549 | u, err := p.valDec(o) |
| 550 | if err != nil { |
| 551 | return err |
| 552 | } |
| 553 | y = append(y, u != 0) |
| 554 | } |
| 555 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 556 | *x = y |
| 557 | return nil |
| 558 | } |
| 559 | |
| 560 | // Decode a slice of int32s ([]int32). |
| 561 | func (o *Buffer) dec_slice_int32(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 562 | u, err := p.valDec(o) |
| 563 | if err != nil { |
| 564 | return err |
| 565 | } |
| 566 | x := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 567 | |
| 568 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 569 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 570 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 571 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 572 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 573 | |
| 574 | *x = append(y, int32(u)) |
| 575 | return nil |
| 576 | } |
| 577 | |
| 578 | // Decode a slice of int32s ([]int32) in packed format. |
| 579 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 580 | x := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 581 | |
| 582 | nn, err := o.DecodeVarint() |
| 583 | if err != nil { |
| 584 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 585 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 586 | nb := int(nn) // number of bytes of encoded int32s |
| 587 | |
| 588 | y := *x |
| 589 | if cap(y) == 0 { |
| 590 | // Packed fields are usually only encoded once, |
| 591 | // so this branch is almost always executed. |
| 592 | // The append in the loop below takes care of other cases. |
| 593 | initSlicen(unsafe.Pointer(x), sbase+p.scratch, nb) |
| 594 | y = *x |
| 595 | } |
| 596 | |
| 597 | fin := o.index + nb |
| 598 | for o.index < fin { |
| 599 | u, err := p.valDec(o) |
| 600 | if err != nil { |
| 601 | return err |
| 602 | } |
| 603 | y = append(y, int32(u)) |
| 604 | } |
| 605 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 606 | *x = y |
| 607 | return nil |
| 608 | } |
| 609 | |
| 610 | // Decode a slice of int64s ([]int64). |
| 611 | func (o *Buffer) dec_slice_int64(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 612 | u, err := p.valDec(o) |
| 613 | if err != nil { |
| 614 | return err |
| 615 | } |
| 616 | x := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| 617 | |
| 618 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 619 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 620 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 621 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 622 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 623 | |
| 624 | *x = append(y, int64(u)) |
| 625 | return nil |
| 626 | } |
| 627 | |
| 628 | // Decode a slice of int64s ([]int64) in packed format. |
| 629 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 630 | x := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| 631 | |
| 632 | nn, err := o.DecodeVarint() |
| 633 | if err != nil { |
| 634 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 635 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 636 | nb := int(nn) // number of bytes of encoded int64s |
| 637 | |
| 638 | y := *x |
| 639 | if cap(y) == 0 { |
| 640 | // Packed fields are usually only encoded once, |
| 641 | // so this branch is almost always executed. |
| 642 | // The append in the loop below takes care of other cases. |
| 643 | initSlicen(unsafe.Pointer(x), sbase+p.scratch, nb) |
| 644 | y = *x |
| 645 | } |
| 646 | |
| 647 | fin := o.index + nb |
| 648 | for o.index < fin { |
| 649 | u, err := p.valDec(o) |
| 650 | if err != nil { |
| 651 | return err |
| 652 | } |
| 653 | y = append(y, int64(u)) |
| 654 | } |
| 655 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 656 | *x = y |
| 657 | return nil |
| 658 | } |
| 659 | |
| 660 | // Decode a slice of strings ([]string). |
| 661 | func (o *Buffer) dec_slice_string(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 662 | s, err := o.DecodeStringBytes() |
| 663 | if err != nil { |
| 664 | return err |
| 665 | } |
| 666 | x := (*[]string)(unsafe.Pointer(base + p.offset)) |
| 667 | |
| 668 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 669 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 670 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 671 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 672 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 673 | |
| 674 | *x = append(y, s) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 675 | return nil |
| 676 | } |
| 677 | |
| 678 | // Decode a slice of slice of bytes ([][]byte). |
| 679 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 680 | b, err := o.DecodeRawBytes(true) |
| 681 | if err != nil { |
| 682 | return err |
| 683 | } |
| 684 | x := (*[][]byte)(unsafe.Pointer(base + p.offset)) |
| 685 | |
| 686 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 687 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 688 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 689 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 690 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 691 | |
| 692 | *x = append(y, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 693 | return nil |
| 694 | } |
| 695 | |
| 696 | // Decode a group. |
| 697 | func (o *Buffer) dec_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 698 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame^] | 699 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 700 | structv := unsafe.New(typ) |
| 701 | bas := uintptr(structv) |
| 702 | *ptr = (*struct{})(structv) |
| 703 | |
| 704 | err := o.unmarshalType(p.stype, true, bas) |
| 705 | |
| 706 | return err |
| 707 | } |
| 708 | |
| 709 | // Decode an embedded message. |
| 710 | func (o *Buffer) dec_struct_message(p *Properties, base uintptr, sbase uintptr) (err os.Error) { |
| 711 | raw, e := o.DecodeRawBytes(false) |
| 712 | if e != nil { |
| 713 | return e |
| 714 | } |
| 715 | |
| 716 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame^] | 717 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 718 | structv := unsafe.New(typ) |
| 719 | bas := uintptr(structv) |
| 720 | *ptr = (*struct{})(structv) |
| 721 | |
| 722 | // If the object can unmarshal itself, let it. |
| 723 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr)) |
| 724 | if u, ok := iv.(Unmarshaler); ok { |
| 725 | return u.Unmarshal(raw) |
| 726 | } |
| 727 | |
| 728 | obuf := o.buf |
| 729 | oi := o.index |
| 730 | o.buf = raw |
| 731 | o.index = 0 |
| 732 | |
| 733 | err = o.unmarshalType(p.stype, false, bas) |
| 734 | o.buf = obuf |
| 735 | o.index = oi |
| 736 | |
| 737 | return err |
| 738 | } |
| 739 | |
| 740 | // Decode a slice of embedded messages. |
| 741 | func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 742 | return o.dec_slice_struct(p, false, base, sbase) |
| 743 | } |
| 744 | |
| 745 | // Decode a slice of embedded groups. |
| 746 | func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error { |
| 747 | return o.dec_slice_struct(p, true, base, sbase) |
| 748 | } |
| 749 | |
| 750 | // Decode a slice of structs ([]*struct). |
| 751 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr, sbase uintptr) os.Error { |
| 752 | |
| 753 | x := (*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 754 | y := *x |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 755 | if cap(y) == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 756 | initSlice(unsafe.Pointer(x), sbase+p.scratch) |
| 757 | y = *x |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame^] | 760 | typ := p.stype.Elem() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 761 | structv := unsafe.New(typ) |
| 762 | bas := uintptr(structv) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 763 | y = append(y, (*struct{})(structv)) |
| 764 | *x = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 765 | |
| 766 | if is_group { |
| 767 | err := o.unmarshalType(p.stype, is_group, bas) |
| 768 | return err |
| 769 | } |
| 770 | |
| 771 | raw, err := o.DecodeRawBytes(true) |
| 772 | if err != nil { |
| 773 | return err |
| 774 | } |
| 775 | |
| 776 | // If the object can unmarshal itself, let it. |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 777 | iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[len(y)-1])) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 778 | if u, ok := iv.(Unmarshaler); ok { |
| 779 | return u.Unmarshal(raw) |
| 780 | } |
| 781 | |
| 782 | obuf := o.buf |
| 783 | oi := o.index |
| 784 | o.buf = raw |
| 785 | o.index = 0 |
| 786 | |
| 787 | err = o.unmarshalType(p.stype, is_group, bas) |
| 788 | |
| 789 | o.buf = obuf |
| 790 | o.index = oi |
| 791 | |
| 792 | return err |
| 793 | } |