| 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 | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 39 | "errors" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | "fmt" |
| 41 | "io" |
| 42 | "os" |
| 43 | "reflect" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 50 | var ErrWrongType = errors.New("field/encoding mismatch: wrong type for field") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 82 | func (p *Buffer) DecodeVarint() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 107 | func (p *Buffer) DecodeFixed64() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 130 | func (p *Buffer) DecodeFixed32() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 149 | func (p *Buffer) DecodeZigzag64() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 161 | func (p *Buffer) DecodeZigzag32() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 176 | func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 177 | n, err := p.DecodeVarint() |
| 178 | if err != nil { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | nb := int(n) |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 183 | if nb < 0 { |
| 184 | return nil, fmt.Errorf("proto: bad byte length %d", nb) |
| 185 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 186 | if p.index+nb > len(p.buf) { |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 187 | return nil, io.ErrUnexpectedEOF |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if !alloc { |
| 191 | // todo: check if can get more uses of alloc=false |
| 192 | buf = p.buf[p.index : p.index+nb] |
| 193 | p.index += nb |
| 194 | return |
| 195 | } |
| 196 | |
| 197 | buf = make([]byte, nb) |
| 198 | copy(buf, p.buf[p.index:]) |
| 199 | p.index += nb |
| 200 | return |
| 201 | } |
| 202 | |
| 203 | // DecodeStringBytes reads an encoded string from the Buffer. |
| 204 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 205 | func (p *Buffer) DecodeStringBytes() (s string, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 206 | buf, err := p.DecodeRawBytes(false) |
| 207 | if err != nil { |
| 208 | return |
| 209 | } |
| 210 | return string(buf), nil |
| 211 | } |
| 212 | |
| 213 | // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. |
| 214 | // If the protocol buffer has extensions, and the field matches, add it as an extension. |
| 215 | // Otherwise, if the XXX_unrecognized field exists, append the skipped data there. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 216 | func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 217 | |
| 218 | oi := o.index |
| 219 | |
| 220 | err := o.skip(t, tag, wire) |
| 221 | if err != nil { |
| 222 | return err |
| 223 | } |
| 224 | |
| 225 | x := fieldIndex(t, "XXX_unrecognized") |
| 226 | if x == nil { |
| 227 | return nil |
| 228 | } |
| 229 | |
| 230 | p := propByIndex(t, x) |
| 231 | ptr := (*[]byte)(unsafe.Pointer(base + p.offset)) |
| 232 | |
| 233 | if *ptr == nil { |
| 234 | // This is the first skipped element, |
| 235 | // allocate a new buffer. |
| 236 | *ptr = o.bufalloc() |
| 237 | } |
| 238 | |
| 239 | // Add the skipped field to struct field |
| 240 | obuf := o.buf |
| 241 | |
| 242 | o.buf = *ptr |
| 243 | o.EncodeVarint(uint64(tag<<3 | wire)) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 244 | *ptr = append(o.buf, obuf[oi:o.index]...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | |
| 246 | o.buf = obuf |
| 247 | |
| 248 | return nil |
| 249 | } |
| 250 | |
| 251 | // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 252 | func (o *Buffer) skip(t reflect.Type, tag, wire int) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 253 | |
| 254 | var u uint64 |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 255 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 256 | |
| 257 | switch wire { |
| 258 | case WireVarint: |
| 259 | _, err = o.DecodeVarint() |
| 260 | case WireFixed64: |
| 261 | _, err = o.DecodeFixed64() |
| 262 | case WireBytes: |
| 263 | _, err = o.DecodeRawBytes(false) |
| 264 | case WireFixed32: |
| 265 | _, err = o.DecodeFixed32() |
| 266 | case WireStartGroup: |
| 267 | for { |
| 268 | u, err = o.DecodeVarint() |
| 269 | if err != nil { |
| 270 | break |
| 271 | } |
| 272 | fwire := int(u & 0x7) |
| 273 | if fwire == WireEndGroup { |
| 274 | break |
| 275 | } |
| 276 | ftag := int(u >> 3) |
| 277 | err = o.skip(t, ftag, fwire) |
| 278 | if err != nil { |
| 279 | break |
| 280 | } |
| 281 | } |
| 282 | default: |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 283 | err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 284 | } |
| 285 | return err |
| 286 | } |
| 287 | |
| 288 | // Unmarshaler is the interface representing objects that can unmarshal themselves. |
| 289 | type Unmarshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 290 | Unmarshal([]byte) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Unmarshal parses the protocol buffer representation in buf and places the |
| 294 | // decoded result in pb. If the struct underlying pb does not match |
| 295 | // the data in buf, the results can be unpredictable. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 296 | func Unmarshal(buf []byte, pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 297 | // If the object can unmarshal itself, let it. |
| 298 | if u, ok := pb.(Unmarshaler); ok { |
| 299 | return u.Unmarshal(buf) |
| 300 | } |
| 301 | |
| 302 | return NewBuffer(buf).Unmarshal(pb) |
| 303 | } |
| 304 | |
| 305 | // Unmarshal parses the protocol buffer representation in the |
| 306 | // Buffer and places the decoded result in pb. If the struct |
| 307 | // underlying pb does not match the data in the buffer, the results can be |
| 308 | // unpredictable. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 309 | func (p *Buffer) Unmarshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 310 | // If the object can unmarshal itself, let it. |
| 311 | if u, ok := pb.(Unmarshaler); ok { |
| 312 | err := u.Unmarshal(p.buf[p.index:]) |
| 313 | p.index = len(p.buf) |
| 314 | return err |
| 315 | } |
| 316 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 317 | typ, base, err := getbase(pb) |
| 318 | if err != nil { |
| 319 | return err |
| 320 | } |
| 321 | |
| 322 | err = p.unmarshalType(typ, false, base) |
| 323 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame^] | 324 | if collectStats { |
| 325 | stats.Decode++ |
| 326 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 327 | |
| 328 | return err |
| 329 | } |
| 330 | |
| 331 | // unmarshalType does the work of unmarshaling a structure. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 332 | func (o *Buffer) unmarshalType(t reflect.Type, is_group bool, base uintptr) error { |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 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 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 337 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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) |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 353 | if tag <= 0 { |
| 354 | return fmt.Errorf("proto: illegal tag %d", tag) |
| 355 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 356 | fieldnum, ok := prop.tags[tag] |
| 357 | if !ok { |
| 358 | // Maybe it's an extension? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 359 | iv := reflect.NewAt(st, unsafe.Pointer(base)).Interface() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 360 | if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) { |
| 361 | if err = o.skip(st, tag, wire); err == nil { |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 362 | ext := e.ExtensionMap()[int32(tag)] // may be missing |
| 363 | ext.enc = append(ext.enc, o.buf[oi:o.index]...) |
| 364 | e.ExtensionMap()[int32(tag)] = ext |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 365 | } |
| 366 | continue |
| 367 | } |
| 368 | err = o.skipAndSave(st, tag, wire, base) |
| 369 | continue |
| 370 | } |
| 371 | p := prop.Prop[fieldnum] |
| 372 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 373 | if p.dec == nil { |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 374 | fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 375 | continue |
| 376 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 377 | dec := p.dec |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 378 | if wire != WireStartGroup && wire != p.WireType { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 379 | if wire == WireBytes && p.packedDec != nil { |
| 380 | // a packable field |
| 381 | dec = p.packedDec |
| 382 | } else { |
| 383 | err = ErrWrongType |
| 384 | continue |
| 385 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 386 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 387 | err = dec(o, p, base) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 388 | if err == nil && p.Required { |
| 389 | // Successfully decoded a required field. |
| 390 | if tag <= 64 { |
| 391 | // use bitmap for fields 1-64 to catch field reuse. |
| 392 | var mask uint64 = 1 << uint64(tag-1) |
| 393 | if reqFields&mask == 0 { |
| 394 | // new required field |
| 395 | reqFields |= mask |
| 396 | required-- |
| 397 | } |
| 398 | } else { |
| 399 | // This is imprecise. It can be fooled by a required field |
| 400 | // with a tag > 64 that is encoded twice; that's very rare. |
| 401 | // A fully correct implementation would require allocating |
| 402 | // a data structure, which we would like to avoid. |
| 403 | required-- |
| 404 | } |
| 405 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 406 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 407 | if err == nil { |
| 408 | if is_group { |
| 409 | return io.ErrUnexpectedEOF |
| 410 | } |
| 411 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 412 | return &ErrRequiredNotSet{st} |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 413 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 414 | } |
| 415 | return err |
| 416 | } |
| 417 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 422 | |
| 423 | // Sizes of the pools to allocate inside the Buffer. |
| Rob Pike | 97edc7e | 2011-10-20 15:53:19 -0700 | [diff] [blame] | 424 | // The goal is modest amortization and allocation |
| 425 | // on at least 16-byte boundaries. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 426 | const ( |
| David Symonds | 049646b | 2011-10-21 11:13:45 +1100 | [diff] [blame] | 427 | boolPoolSize = 16 |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 428 | int32PoolSize = 8 |
| 429 | int64PoolSize = 4 |
| 430 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 431 | |
| 432 | // Decode a bool. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 433 | func (o *Buffer) dec_bool(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 434 | u, err := p.valDec(o) |
| 435 | if err != nil { |
| 436 | return err |
| 437 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 438 | if len(o.bools) == 0 { |
| 439 | o.bools = make([]bool, boolPoolSize) |
| 440 | } |
| 441 | o.bools[0] = u != 0 |
| 442 | v := (**bool)(unsafe.Pointer(base + p.offset)) |
| 443 | *v = &o.bools[0] |
| 444 | o.bools = o.bools[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 445 | return nil |
| 446 | } |
| 447 | |
| 448 | // Decode an int32. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 449 | func (o *Buffer) dec_int32(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 450 | u, err := p.valDec(o) |
| 451 | if err != nil { |
| 452 | return err |
| 453 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 454 | if len(o.int32s) == 0 { |
| 455 | o.int32s = make([]int32, int32PoolSize) |
| 456 | } |
| 457 | o.int32s[0] = int32(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 458 | v := (**int32)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 459 | *v = &o.int32s[0] |
| 460 | o.int32s = o.int32s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 461 | return nil |
| 462 | } |
| 463 | |
| 464 | // Decode an int64. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 465 | func (o *Buffer) dec_int64(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 466 | u, err := p.valDec(o) |
| 467 | if err != nil { |
| 468 | return err |
| 469 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 470 | if len(o.int64s) == 0 { |
| 471 | o.int64s = make([]int64, int64PoolSize) |
| 472 | } |
| 473 | o.int64s[0] = int64(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 474 | v := (**int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 475 | *v = &o.int64s[0] |
| 476 | o.int64s = o.int64s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 477 | return nil |
| 478 | } |
| 479 | |
| 480 | // Decode a string. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 481 | func (o *Buffer) dec_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 482 | s, err := o.DecodeStringBytes() |
| 483 | if err != nil { |
| 484 | return err |
| 485 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 486 | sp := new(string) |
| 487 | *sp = s |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 488 | v := (**string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 489 | *v = sp |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 490 | return nil |
| 491 | } |
| 492 | |
| 493 | // Decode a slice of bytes ([]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 494 | func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 495 | b, err := o.DecodeRawBytes(true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 496 | if err != nil { |
| 497 | return err |
| 498 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 499 | v := (*[]byte)(unsafe.Pointer(base + p.offset)) |
| 500 | *v = b |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 501 | return nil |
| 502 | } |
| 503 | |
| 504 | // Decode a slice of bools ([]bool). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 505 | func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 506 | u, err := p.valDec(o) |
| 507 | if err != nil { |
| 508 | return err |
| 509 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 510 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 511 | *v = append(*v, u != 0) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 512 | return nil |
| 513 | } |
| 514 | |
| 515 | // Decode a slice of bools ([]bool) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 516 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 517 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 518 | |
| 519 | nn, err := o.DecodeVarint() |
| 520 | if err != nil { |
| 521 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 522 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 523 | nb := int(nn) // number of bytes of encoded bools |
| 524 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 525 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 526 | for i := 0; i < nb; i++ { |
| 527 | u, err := p.valDec(o) |
| 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | y = append(y, u != 0) |
| 532 | } |
| 533 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 534 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 535 | return nil |
| 536 | } |
| 537 | |
| 538 | // Decode a slice of int32s ([]int32). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 539 | func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 540 | u, err := p.valDec(o) |
| 541 | if err != nil { |
| 542 | return err |
| 543 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 544 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 545 | *v = append(*v, int32(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 546 | return nil |
| 547 | } |
| 548 | |
| 549 | // Decode a slice of int32s ([]int32) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 550 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 551 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 552 | |
| 553 | nn, err := o.DecodeVarint() |
| 554 | if err != nil { |
| 555 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 556 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 557 | nb := int(nn) // number of bytes of encoded int32s |
| 558 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 559 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 560 | |
| 561 | fin := o.index + nb |
| 562 | for o.index < fin { |
| 563 | u, err := p.valDec(o) |
| 564 | if err != nil { |
| 565 | return err |
| 566 | } |
| 567 | y = append(y, int32(u)) |
| 568 | } |
| 569 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 570 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 571 | return nil |
| 572 | } |
| 573 | |
| 574 | // Decode a slice of int64s ([]int64). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 575 | func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 576 | u, err := p.valDec(o) |
| 577 | if err != nil { |
| 578 | return err |
| 579 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 580 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 581 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 582 | y := *v |
| 583 | *v = append(y, int64(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 584 | return nil |
| 585 | } |
| 586 | |
| 587 | // Decode a slice of int64s ([]int64) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 588 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 589 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 590 | |
| 591 | nn, err := o.DecodeVarint() |
| 592 | if err != nil { |
| 593 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 594 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 595 | nb := int(nn) // number of bytes of encoded int64s |
| 596 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 597 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 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, int64(u)) |
| 605 | } |
| 606 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 607 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 608 | return nil |
| 609 | } |
| 610 | |
| 611 | // Decode a slice of strings ([]string). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 612 | func (o *Buffer) dec_slice_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 613 | s, err := o.DecodeStringBytes() |
| 614 | if err != nil { |
| 615 | return err |
| 616 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 617 | v := (*[]string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 618 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 619 | y := *v |
| 620 | *v = append(y, s) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 621 | return nil |
| 622 | } |
| 623 | |
| 624 | // Decode a slice of slice of bytes ([][]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 625 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 626 | b, err := o.DecodeRawBytes(true) |
| 627 | if err != nil { |
| 628 | return err |
| 629 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 630 | v := (*[][]byte)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 631 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 632 | y := *v |
| 633 | *v = append(y, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 634 | return nil |
| 635 | } |
| 636 | |
| 637 | // Decode a group. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 638 | func (o *Buffer) dec_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 639 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 640 | typ := p.stype.Elem() |
| Rob Pike | 7a78813 | 2012-02-14 08:14:14 +1100 | [diff] [blame] | 641 | bas := reflect.New(typ).Pointer() |
| 642 | structv := unsafe.Pointer(bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 643 | *ptr = (*struct{})(structv) |
| 644 | |
| 645 | err := o.unmarshalType(p.stype, true, bas) |
| 646 | |
| 647 | return err |
| 648 | } |
| 649 | |
| 650 | // Decode an embedded message. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 651 | func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 652 | raw, e := o.DecodeRawBytes(false) |
| 653 | if e != nil { |
| 654 | return e |
| 655 | } |
| 656 | |
| 657 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 658 | typ := p.stype.Elem() |
| Rob Pike | 7a78813 | 2012-02-14 08:14:14 +1100 | [diff] [blame] | 659 | bas := reflect.New(typ).Pointer() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 660 | structp := unsafe.Pointer(bas) |
| 661 | *ptr = (*struct{})(structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 662 | |
| 663 | // If the object can unmarshal itself, let it. |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 664 | if p.isMarshaler { |
| 665 | iv := reflect.NewAt(p.stype.Elem(), structp).Interface() |
| 666 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | obuf := o.buf |
| 670 | oi := o.index |
| 671 | o.buf = raw |
| 672 | o.index = 0 |
| 673 | |
| 674 | err = o.unmarshalType(p.stype, false, bas) |
| 675 | o.buf = obuf |
| 676 | o.index = oi |
| 677 | |
| 678 | return err |
| 679 | } |
| 680 | |
| 681 | // Decode a slice of embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 682 | func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 683 | return o.dec_slice_struct(p, false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Decode a slice of embedded groups. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 687 | func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 688 | return o.dec_slice_struct(p, true, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | // Decode a slice of structs ([]*struct). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 692 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 693 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 694 | v := (*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 695 | y := *v |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 696 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 697 | typ := p.stype.Elem() |
| Rob Pike | 7a78813 | 2012-02-14 08:14:14 +1100 | [diff] [blame] | 698 | bas := reflect.New(typ).Pointer() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 699 | structp := unsafe.Pointer(bas) |
| 700 | y = append(y, (*struct{})(structp)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 701 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 702 | |
| 703 | if is_group { |
| 704 | err := o.unmarshalType(p.stype, is_group, bas) |
| 705 | return err |
| 706 | } |
| 707 | |
| 708 | raw, err := o.DecodeRawBytes(true) |
| 709 | if err != nil { |
| 710 | return err |
| 711 | } |
| 712 | |
| 713 | // If the object can unmarshal itself, let it. |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 714 | if p.isUnmarshaler { |
| 715 | iv := reflect.NewAt(typ, structp).Interface() |
| 716 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | obuf := o.buf |
| 720 | oi := o.index |
| 721 | o.buf = raw |
| 722 | o.index = 0 |
| 723 | |
| 724 | err = o.unmarshalType(p.stype, is_group, bas) |
| 725 | |
| 726 | o.buf = obuf |
| 727 | o.index = oi |
| 728 | |
| 729 | return err |
| 730 | } |