| 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. |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 216 | func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base, unrecOffset 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 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 225 | if unrecOffset == 0 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 226 | return nil |
| 227 | } |
| 228 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 229 | ptr := (*[]byte)(unsafe.Pointer(base + unrecOffset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 250 | func (o *Buffer) skip(t reflect.Type, tag, wire int) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 251 | |
| 252 | var u uint64 |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 253 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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: |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 281 | 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] | 282 | } |
| 283 | return err |
| 284 | } |
| 285 | |
| 286 | // Unmarshaler is the interface representing objects that can unmarshal themselves. |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 287 | // The argument points to data that may be overwritten, so implementations should |
| 288 | // not keep references to the buffer. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 296 | // |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 297 | // Unmarshal resets pb before starting to unmarshal, so any |
| 298 | // existing data in pb is always removed. Use UnmarshalAppend |
| 299 | // to preserve and append to existing data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 300 | func Unmarshal(buf []byte, pb Message) error { |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 301 | pb.Reset() |
| David Symonds | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 302 | return UnmarshalAppend(buf, pb) |
| 303 | } |
| 304 | |
| 305 | // UnmarshalAppend parses the protocol buffer representation in buf and |
| 306 | // writes the decoded result to pb. If the struct underlying pb does not match |
| 307 | // the data in buf, the results can be unpredictable. |
| 308 | // |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 309 | // UnmarshalAppend preserves and appends to existing data in pb. |
| 310 | // Most code should use Unmarshal instead. |
| David Symonds | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 311 | func UnmarshalAppend(buf []byte, pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 312 | // If the object can unmarshal itself, let it. |
| 313 | if u, ok := pb.(Unmarshaler); ok { |
| 314 | return u.Unmarshal(buf) |
| 315 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 316 | return NewBuffer(buf).Unmarshal(pb) |
| 317 | } |
| 318 | |
| 319 | // Unmarshal parses the protocol buffer representation in the |
| 320 | // Buffer and places the decoded result in pb. If the struct |
| 321 | // underlying pb does not match the data in the buffer, the results can be |
| 322 | // unpredictable. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 323 | func (p *Buffer) Unmarshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 324 | // If the object can unmarshal itself, let it. |
| 325 | if u, ok := pb.(Unmarshaler); ok { |
| 326 | err := u.Unmarshal(p.buf[p.index:]) |
| 327 | p.index = len(p.buf) |
| 328 | return err |
| 329 | } |
| 330 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 331 | typ, base, err := getbase(pb) |
| 332 | if err != nil { |
| 333 | return err |
| 334 | } |
| 335 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 336 | err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 337 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 338 | if collectStats { |
| 339 | stats.Decode++ |
| 340 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 341 | |
| 342 | return err |
| 343 | } |
| 344 | |
| 345 | // unmarshalType does the work of unmarshaling a structure. |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 346 | func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base uintptr) error { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 347 | required, reqFields := prop.reqCount, uint64(0) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 348 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 349 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 350 | for err == nil && o.index < len(o.buf) { |
| 351 | oi := o.index |
| 352 | var u uint64 |
| 353 | u, err = o.DecodeVarint() |
| 354 | if err != nil { |
| 355 | break |
| 356 | } |
| 357 | wire := int(u & 0x7) |
| 358 | if wire == WireEndGroup { |
| 359 | if is_group { |
| 360 | return nil // input is satisfied |
| 361 | } |
| 362 | return ErrWrongType |
| 363 | } |
| 364 | tag := int(u >> 3) |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 365 | if tag <= 0 { |
| 366 | return fmt.Errorf("proto: illegal tag %d", tag) |
| 367 | } |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 368 | fieldnum, ok := prop.tags.get(tag) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 369 | if !ok { |
| 370 | // Maybe it's an extension? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 371 | iv := reflect.NewAt(st, unsafe.Pointer(base)).Interface() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 372 | if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) { |
| 373 | if err = o.skip(st, tag, wire); err == nil { |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 374 | ext := e.ExtensionMap()[int32(tag)] // may be missing |
| 375 | ext.enc = append(ext.enc, o.buf[oi:o.index]...) |
| 376 | e.ExtensionMap()[int32(tag)] = ext |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 377 | } |
| 378 | continue |
| 379 | } |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 380 | err = o.skipAndSave(st, tag, wire, base, prop.unrecOffset) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 381 | continue |
| 382 | } |
| 383 | p := prop.Prop[fieldnum] |
| 384 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 385 | if p.dec == nil { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 386 | fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 387 | continue |
| 388 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 389 | dec := p.dec |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 390 | if wire != WireStartGroup && wire != p.WireType { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 391 | if wire == WireBytes && p.packedDec != nil { |
| 392 | // a packable field |
| 393 | dec = p.packedDec |
| 394 | } else { |
| 395 | err = ErrWrongType |
| 396 | continue |
| 397 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 398 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 399 | err = dec(o, p, base) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 400 | if err == nil && p.Required { |
| 401 | // Successfully decoded a required field. |
| 402 | if tag <= 64 { |
| 403 | // use bitmap for fields 1-64 to catch field reuse. |
| 404 | var mask uint64 = 1 << uint64(tag-1) |
| 405 | if reqFields&mask == 0 { |
| 406 | // new required field |
| 407 | reqFields |= mask |
| 408 | required-- |
| 409 | } |
| 410 | } else { |
| 411 | // This is imprecise. It can be fooled by a required field |
| 412 | // with a tag > 64 that is encoded twice; that's very rare. |
| 413 | // A fully correct implementation would require allocating |
| 414 | // a data structure, which we would like to avoid. |
| 415 | required-- |
| 416 | } |
| 417 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 418 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 419 | if err == nil { |
| 420 | if is_group { |
| 421 | return io.ErrUnexpectedEOF |
| 422 | } |
| 423 | if required > 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 424 | return &ErrRequiredNotSet{st} |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 425 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 426 | } |
| 427 | return err |
| 428 | } |
| 429 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 430 | // Individual type decoders |
| 431 | // For each, |
| 432 | // u is the decoded value, |
| 433 | // v is a pointer to the field (pointer) in the struct |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 434 | |
| 435 | // Sizes of the pools to allocate inside the Buffer. |
| Rob Pike | 97edc7e | 2011-10-20 15:53:19 -0700 | [diff] [blame] | 436 | // The goal is modest amortization and allocation |
| 437 | // on at least 16-byte boundaries. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 438 | const ( |
| David Symonds | 049646b | 2011-10-21 11:13:45 +1100 | [diff] [blame] | 439 | boolPoolSize = 16 |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 440 | int32PoolSize = 8 |
| 441 | int64PoolSize = 4 |
| 442 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 443 | |
| 444 | // Decode a bool. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 445 | func (o *Buffer) dec_bool(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 446 | u, err := p.valDec(o) |
| 447 | if err != nil { |
| 448 | return err |
| 449 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 450 | if len(o.bools) == 0 { |
| 451 | o.bools = make([]bool, boolPoolSize) |
| 452 | } |
| 453 | o.bools[0] = u != 0 |
| 454 | v := (**bool)(unsafe.Pointer(base + p.offset)) |
| 455 | *v = &o.bools[0] |
| 456 | o.bools = o.bools[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 457 | return nil |
| 458 | } |
| 459 | |
| 460 | // Decode an int32. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 461 | func (o *Buffer) dec_int32(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 462 | u, err := p.valDec(o) |
| 463 | if err != nil { |
| 464 | return err |
| 465 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 466 | if len(o.int32s) == 0 { |
| 467 | o.int32s = make([]int32, int32PoolSize) |
| 468 | } |
| 469 | o.int32s[0] = int32(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 470 | v := (**int32)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 471 | *v = &o.int32s[0] |
| 472 | o.int32s = o.int32s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 473 | return nil |
| 474 | } |
| 475 | |
| 476 | // Decode an int64. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 477 | func (o *Buffer) dec_int64(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 478 | u, err := p.valDec(o) |
| 479 | if err != nil { |
| 480 | return err |
| 481 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 482 | if len(o.int64s) == 0 { |
| 483 | o.int64s = make([]int64, int64PoolSize) |
| 484 | } |
| 485 | o.int64s[0] = int64(u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 486 | v := (**int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 487 | *v = &o.int64s[0] |
| 488 | o.int64s = o.int64s[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 489 | return nil |
| 490 | } |
| 491 | |
| 492 | // Decode a string. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 493 | func (o *Buffer) dec_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 494 | s, err := o.DecodeStringBytes() |
| 495 | if err != nil { |
| 496 | return err |
| 497 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 498 | sp := new(string) |
| 499 | *sp = s |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 500 | v := (**string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 501 | *v = sp |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 502 | return nil |
| 503 | } |
| 504 | |
| 505 | // Decode a slice of bytes ([]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 506 | func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 507 | b, err := o.DecodeRawBytes(true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 508 | if err != nil { |
| 509 | return err |
| 510 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 511 | v := (*[]byte)(unsafe.Pointer(base + p.offset)) |
| 512 | *v = b |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 513 | return nil |
| 514 | } |
| 515 | |
| 516 | // Decode a slice of bools ([]bool). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 517 | func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 518 | u, err := p.valDec(o) |
| 519 | if err != nil { |
| 520 | return err |
| 521 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 522 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| 523 | *v = append(*v, u != 0) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 524 | return nil |
| 525 | } |
| 526 | |
| 527 | // Decode a slice of bools ([]bool) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 528 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 529 | v := (*[]bool)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 530 | |
| 531 | nn, err := o.DecodeVarint() |
| 532 | if err != nil { |
| 533 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 534 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 535 | nb := int(nn) // number of bytes of encoded bools |
| 536 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 537 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 538 | for i := 0; i < nb; i++ { |
| 539 | u, err := p.valDec(o) |
| 540 | if err != nil { |
| 541 | return err |
| 542 | } |
| 543 | y = append(y, u != 0) |
| 544 | } |
| 545 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 546 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 547 | return nil |
| 548 | } |
| 549 | |
| 550 | // Decode a slice of int32s ([]int32). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 551 | func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 552 | u, err := p.valDec(o) |
| 553 | if err != nil { |
| 554 | return err |
| 555 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 556 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| 557 | *v = append(*v, int32(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 558 | return nil |
| 559 | } |
| 560 | |
| 561 | // Decode a slice of int32s ([]int32) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 562 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 563 | v := (*[]int32)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 564 | |
| 565 | nn, err := o.DecodeVarint() |
| 566 | if err != nil { |
| 567 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 568 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 569 | nb := int(nn) // number of bytes of encoded int32s |
| 570 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 571 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 572 | |
| 573 | fin := o.index + nb |
| 574 | for o.index < fin { |
| 575 | u, err := p.valDec(o) |
| 576 | if err != nil { |
| 577 | return err |
| 578 | } |
| 579 | y = append(y, int32(u)) |
| 580 | } |
| 581 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 582 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 583 | return nil |
| 584 | } |
| 585 | |
| 586 | // Decode a slice of int64s ([]int64). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 587 | func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 588 | u, err := p.valDec(o) |
| 589 | if err != nil { |
| 590 | return err |
| 591 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 592 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 593 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 594 | y := *v |
| 595 | *v = append(y, int64(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 596 | return nil |
| 597 | } |
| 598 | |
| 599 | // Decode a slice of int64s ([]int64) in packed format. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 600 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 601 | v := (*[]int64)(unsafe.Pointer(base + p.offset)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 602 | |
| 603 | nn, err := o.DecodeVarint() |
| 604 | if err != nil { |
| 605 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 606 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 607 | nb := int(nn) // number of bytes of encoded int64s |
| 608 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 609 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 610 | fin := o.index + nb |
| 611 | for o.index < fin { |
| 612 | u, err := p.valDec(o) |
| 613 | if err != nil { |
| 614 | return err |
| 615 | } |
| 616 | y = append(y, int64(u)) |
| 617 | } |
| 618 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 619 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 620 | return nil |
| 621 | } |
| 622 | |
| 623 | // Decode a slice of strings ([]string). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 624 | func (o *Buffer) dec_slice_string(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 625 | s, err := o.DecodeStringBytes() |
| 626 | if err != nil { |
| 627 | return err |
| 628 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 629 | v := (*[]string)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 630 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 631 | y := *v |
| 632 | *v = append(y, s) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 633 | return nil |
| 634 | } |
| 635 | |
| 636 | // Decode a slice of slice of bytes ([][]byte). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 637 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 638 | b, err := o.DecodeRawBytes(true) |
| 639 | if err != nil { |
| 640 | return err |
| 641 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 642 | v := (*[][]byte)(unsafe.Pointer(base + p.offset)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 643 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 644 | y := *v |
| 645 | *v = append(y, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 646 | return nil |
| 647 | } |
| 648 | |
| 649 | // Decode a group. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 650 | func (o *Buffer) dec_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 651 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 652 | bas := reflect.New(p.stype).Pointer() |
| Rob Pike | 7a78813 | 2012-02-14 08:14:14 +1100 | [diff] [blame] | 653 | structv := unsafe.Pointer(bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 654 | *ptr = (*struct{})(structv) |
| 655 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 656 | err := o.unmarshalType(p.stype, p.sprop, true, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 657 | |
| 658 | return err |
| 659 | } |
| 660 | |
| 661 | // Decode an embedded message. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 662 | func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 663 | raw, e := o.DecodeRawBytes(false) |
| 664 | if e != nil { |
| 665 | return e |
| 666 | } |
| 667 | |
| 668 | ptr := (**struct{})(unsafe.Pointer(base + p.offset)) |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 669 | bas := reflect.New(p.stype).Pointer() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 670 | structp := unsafe.Pointer(bas) |
| 671 | *ptr = (*struct{})(structp) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 672 | |
| 673 | // If the object can unmarshal itself, let it. |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 674 | if p.isMarshaler { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 675 | iv := reflect.NewAt(p.stype, structp).Interface() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 676 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | obuf := o.buf |
| 680 | oi := o.index |
| 681 | o.buf = raw |
| 682 | o.index = 0 |
| 683 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 684 | err = o.unmarshalType(p.stype, p.sprop, false, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 685 | o.buf = obuf |
| 686 | o.index = oi |
| 687 | |
| 688 | return err |
| 689 | } |
| 690 | |
| 691 | // Decode a slice of embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 692 | func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 693 | return o.dec_slice_struct(p, false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | // Decode a slice of embedded groups. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 697 | func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 698 | return o.dec_slice_struct(p, true, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // Decode a slice of structs ([]*struct). |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 702 | 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] | 703 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 704 | v := (*[]*struct{})(unsafe.Pointer(base + p.offset)) |
| 705 | y := *v |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 706 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 707 | bas := reflect.New(p.stype).Pointer() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 708 | structp := unsafe.Pointer(bas) |
| 709 | y = append(y, (*struct{})(structp)) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 710 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 711 | |
| 712 | if is_group { |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 713 | err := o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 714 | return err |
| 715 | } |
| 716 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 717 | raw, err := o.DecodeRawBytes(false) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 718 | if err != nil { |
| 719 | return err |
| 720 | } |
| 721 | |
| 722 | // If the object can unmarshal itself, let it. |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 723 | if p.isUnmarshaler { |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame^] | 724 | iv := reflect.NewAt(p.stype, structp).Interface() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 725 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | obuf := o.buf |
| 729 | oi := o.index |
| 730 | o.buf = raw |
| 731 | o.index = 0 |
| 732 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 733 | err = o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 734 | |
| 735 | o.buf = obuf |
| 736 | o.index = oi |
| 737 | |
| 738 | return err |
| 739 | } |