| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | ) |
| 45 | |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 46 | // errOverflow is returned when an integer is too large to be represented. |
| 47 | var errOverflow = errors.New("proto: integer overflow") |
| 48 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 49 | // The fundamental decoders that interpret bytes on the wire. |
| 50 | // Those that take integer types all return uint64 and are |
| 51 | // therefore of type valueDecoder. |
| 52 | |
| 53 | // DecodeVarint reads a varint-encoded integer from the slice. |
| 54 | // It returns the integer and the number of bytes consumed, or |
| 55 | // zero if there is not enough. |
| 56 | // This is the format for the |
| 57 | // int32, int64, uint32, uint64, bool, and enum |
| 58 | // protocol buffer types. |
| 59 | func DecodeVarint(buf []byte) (x uint64, n int) { |
| 60 | // x, n already 0 |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 61 | for shift := uint(0); shift < 64; shift += 7 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 62 | if n >= len(buf) { |
| 63 | return 0, 0 |
| 64 | } |
| 65 | b := uint64(buf[n]) |
| 66 | n++ |
| 67 | x |= (b & 0x7F) << shift |
| 68 | if (b & 0x80) == 0 { |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 69 | return x, n |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 72 | |
| 73 | // The number is too large to represent in a 64-bit value. |
| 74 | return 0, 0 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // DecodeVarint reads a varint-encoded integer from the Buffer. |
| 78 | // This is the format for the |
| 79 | // int32, int64, uint32, uint64, bool, and enum |
| 80 | // protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 81 | func (p *Buffer) DecodeVarint() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 82 | // x, err already 0 |
| 83 | |
| 84 | i := p.index |
| 85 | l := len(p.buf) |
| 86 | |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 87 | for shift := uint(0); shift < 64; shift += 7 { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 88 | if i >= l { |
| 89 | err = io.ErrUnexpectedEOF |
| 90 | return |
| 91 | } |
| 92 | b := p.buf[i] |
| 93 | i++ |
| 94 | x |= (uint64(b) & 0x7F) << shift |
| 95 | if b < 0x80 { |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 96 | p.index = i |
| 97 | return |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 100 | |
| 101 | // The number is too large to represent in a 64-bit value. |
| 102 | err = errOverflow |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 103 | return |
| 104 | } |
| 105 | |
| 106 | // DecodeFixed64 reads a 64-bit integer from the Buffer. |
| 107 | // This is the format for the |
| 108 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 109 | func (p *Buffer) DecodeFixed64() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 110 | // x, err already 0 |
| 111 | i := p.index + 8 |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 112 | if i < 0 || i > len(p.buf) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 113 | err = io.ErrUnexpectedEOF |
| 114 | return |
| 115 | } |
| 116 | p.index = i |
| 117 | |
| 118 | x = uint64(p.buf[i-8]) |
| 119 | x |= uint64(p.buf[i-7]) << 8 |
| 120 | x |= uint64(p.buf[i-6]) << 16 |
| 121 | x |= uint64(p.buf[i-5]) << 24 |
| 122 | x |= uint64(p.buf[i-4]) << 32 |
| 123 | x |= uint64(p.buf[i-3]) << 40 |
| 124 | x |= uint64(p.buf[i-2]) << 48 |
| 125 | x |= uint64(p.buf[i-1]) << 56 |
| 126 | return |
| 127 | } |
| 128 | |
| 129 | // DecodeFixed32 reads a 32-bit integer from the Buffer. |
| 130 | // This is the format for the |
| 131 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 132 | func (p *Buffer) DecodeFixed32() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 133 | // x, err already 0 |
| 134 | i := p.index + 4 |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 135 | if i < 0 || i > len(p.buf) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 136 | err = io.ErrUnexpectedEOF |
| 137 | return |
| 138 | } |
| 139 | p.index = i |
| 140 | |
| 141 | x = uint64(p.buf[i-4]) |
| 142 | x |= uint64(p.buf[i-3]) << 8 |
| 143 | x |= uint64(p.buf[i-2]) << 16 |
| 144 | x |= uint64(p.buf[i-1]) << 24 |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | // DecodeZigzag64 reads a zigzag-encoded 64-bit integer |
| 149 | // from the Buffer. |
| 150 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 151 | func (p *Buffer) DecodeZigzag64() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 152 | x, err = p.DecodeVarint() |
| 153 | if err != nil { |
| 154 | return |
| 155 | } |
| 156 | x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63) |
| 157 | return |
| 158 | } |
| 159 | |
| 160 | // DecodeZigzag32 reads a zigzag-encoded 32-bit integer |
| 161 | // from the Buffer. |
| 162 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 163 | func (p *Buffer) DecodeZigzag32() (x uint64, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 164 | x, err = p.DecodeVarint() |
| 165 | if err != nil { |
| 166 | return |
| 167 | } |
| 168 | x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31)) |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | // These are not ValueDecoders: they produce an array of bytes or a string. |
| 173 | // bytes, embedded messages |
| 174 | |
| 175 | // DecodeRawBytes reads a count-delimited byte buffer from the Buffer. |
| 176 | // This is the format used for the bytes protocol buffer |
| 177 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 178 | func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 179 | n, err := p.DecodeVarint() |
| 180 | if err != nil { |
| 181 | return |
| 182 | } |
| 183 | |
| 184 | nb := int(n) |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 185 | if nb < 0 { |
| 186 | return nil, fmt.Errorf("proto: bad byte length %d", nb) |
| 187 | } |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 188 | end := p.index + nb |
| 189 | if end < p.index || end > len(p.buf) { |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 190 | return nil, io.ErrUnexpectedEOF |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if !alloc { |
| 194 | // todo: check if can get more uses of alloc=false |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 195 | buf = p.buf[p.index:end] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 196 | p.index += nb |
| 197 | return |
| 198 | } |
| 199 | |
| 200 | buf = make([]byte, nb) |
| 201 | copy(buf, p.buf[p.index:]) |
| 202 | p.index += nb |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | // DecodeStringBytes reads an encoded string from the Buffer. |
| 207 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 208 | func (p *Buffer) DecodeStringBytes() (s string, err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 209 | buf, err := p.DecodeRawBytes(false) |
| 210 | if err != nil { |
| 211 | return |
| 212 | } |
| 213 | return string(buf), nil |
| 214 | } |
| 215 | |
| 216 | // Skip the next item in the buffer. Its wire type is decoded and presented as an argument. |
| 217 | // If the protocol buffer has extensions, and the field matches, add it as an extension. |
| 218 | // Otherwise, if the XXX_unrecognized field exists, append the skipped data there. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 219 | func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 220 | oi := o.index |
| 221 | |
| 222 | err := o.skip(t, tag, wire) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 227 | if !unrecField.IsValid() { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 228 | return nil |
| 229 | } |
| 230 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 231 | ptr := structPointer_Bytes(base, unrecField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 232 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 233 | // Add the skipped field to struct field |
| 234 | obuf := o.buf |
| 235 | |
| 236 | o.buf = *ptr |
| 237 | o.EncodeVarint(uint64(tag<<3 | wire)) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 238 | *ptr = append(o.buf, obuf[oi:o.index]...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 239 | |
| 240 | o.buf = obuf |
| 241 | |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | // 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] | 246 | func (o *Buffer) skip(t reflect.Type, tag, wire int) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 247 | |
| 248 | var u uint64 |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 249 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 250 | |
| 251 | switch wire { |
| 252 | case WireVarint: |
| 253 | _, err = o.DecodeVarint() |
| 254 | case WireFixed64: |
| 255 | _, err = o.DecodeFixed64() |
| 256 | case WireBytes: |
| 257 | _, err = o.DecodeRawBytes(false) |
| 258 | case WireFixed32: |
| 259 | _, err = o.DecodeFixed32() |
| 260 | case WireStartGroup: |
| 261 | for { |
| 262 | u, err = o.DecodeVarint() |
| 263 | if err != nil { |
| 264 | break |
| 265 | } |
| 266 | fwire := int(u & 0x7) |
| 267 | if fwire == WireEndGroup { |
| 268 | break |
| 269 | } |
| 270 | ftag := int(u >> 3) |
| 271 | err = o.skip(t, ftag, fwire) |
| 272 | if err != nil { |
| 273 | break |
| 274 | } |
| 275 | } |
| 276 | default: |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 277 | 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] | 278 | } |
| 279 | return err |
| 280 | } |
| 281 | |
| David Symonds | 7d3c680 | 2012-11-08 08:20:18 +1100 | [diff] [blame] | 282 | // Unmarshaler is the interface representing objects that can |
| 283 | // unmarshal themselves. The method should reset the receiver before |
| 284 | // decoding starts. The argument points to data that may be |
| 285 | // overwritten, so implementations should not keep references to the |
| 286 | // buffer. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 287 | type Unmarshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 288 | Unmarshal([]byte) error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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. |
| David Symonds | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 294 | // |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 295 | // Unmarshal resets pb before starting to unmarshal, so any |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 296 | // existing data in pb is always removed. Use UnmarshalMerge |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 297 | // to preserve and append to existing data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 298 | func Unmarshal(buf []byte, pb Message) error { |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 299 | pb.Reset() |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 300 | return UnmarshalMerge(buf, pb) |
| David Symonds | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 303 | // UnmarshalMerge parses the protocol buffer representation in buf and |
| David Symonds | 1928560 | 2012-07-02 16:04:28 -0700 | [diff] [blame] | 304 | // writes the decoded result to pb. If the struct underlying pb does not match |
| 305 | // the data in buf, the results can be unpredictable. |
| 306 | // |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 307 | // UnmarshalMerge merges into existing data in pb. |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 308 | // Most code should use Unmarshal instead. |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 309 | func UnmarshalMerge(buf []byte, 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 | return u.Unmarshal(buf) |
| 313 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 314 | return NewBuffer(buf).Unmarshal(pb) |
| 315 | } |
| 316 | |
| 317 | // Unmarshal parses the protocol buffer representation in the |
| 318 | // Buffer and places the decoded result in pb. If the struct |
| 319 | // underlying pb does not match the data in the buffer, the results can be |
| 320 | // unpredictable. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 321 | func (p *Buffer) Unmarshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 322 | // If the object can unmarshal itself, let it. |
| 323 | if u, ok := pb.(Unmarshaler); ok { |
| 324 | err := u.Unmarshal(p.buf[p.index:]) |
| 325 | p.index = len(p.buf) |
| 326 | return err |
| 327 | } |
| 328 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 329 | typ, base, err := getbase(pb) |
| 330 | if err != nil { |
| 331 | return err |
| 332 | } |
| 333 | |
| David Symonds | 6a6f82c | 2012-08-22 09:18:54 +1000 | [diff] [blame] | 334 | err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 335 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 336 | if collectStats { |
| 337 | stats.Decode++ |
| 338 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 339 | |
| 340 | return err |
| 341 | } |
| 342 | |
| 343 | // unmarshalType does the work of unmarshaling a structure. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 344 | func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 345 | var state errorState |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 346 | required, reqFields := prop.reqCount, uint64(0) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 347 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 348 | var err error |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 349 | for err == nil && o.index < len(o.buf) { |
| 350 | oi := o.index |
| 351 | var u uint64 |
| 352 | u, err = o.DecodeVarint() |
| 353 | if err != nil { |
| 354 | break |
| 355 | } |
| 356 | wire := int(u & 0x7) |
| 357 | if wire == WireEndGroup { |
| 358 | if is_group { |
| 359 | return nil // input is satisfied |
| 360 | } |
| David Symonds | baeae8b | 2014-06-23 09:41:27 +1000 | [diff] [blame^] | 361 | return fmt.Errorf("proto: %s: wiretype end group for non-group", st) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 362 | } |
| 363 | tag := int(u >> 3) |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 364 | if tag <= 0 { |
| David Symonds | baeae8b | 2014-06-23 09:41:27 +1000 | [diff] [blame^] | 365 | return fmt.Errorf("proto: %s: illegal tag %d", st, tag) |
| David Symonds | 6e50db5 | 2012-02-11 15:56:22 +1100 | [diff] [blame] | 366 | } |
| David Symonds | 2bba1b2 | 2012-09-26 14:53:08 +1000 | [diff] [blame] | 367 | fieldnum, ok := prop.decoderTags.get(tag) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 368 | if !ok { |
| 369 | // Maybe it's an extension? |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 370 | if prop.extendable { |
| 371 | if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) { |
| 372 | if err = o.skip(st, tag, wire); err == nil { |
| 373 | ext := e.ExtensionMap()[int32(tag)] // may be missing |
| 374 | ext.enc = append(ext.enc, o.buf[oi:o.index]...) |
| 375 | e.ExtensionMap()[int32(tag)] = ext |
| 376 | } |
| 377 | continue |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 378 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 379 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 380 | err = o.skipAndSave(st, tag, wire, base, prop.unrecField) |
| 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 { |
| David Symonds | baeae8b | 2014-06-23 09:41:27 +1000 | [diff] [blame^] | 395 | err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 396 | continue |
| 397 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 398 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 399 | decErr := dec(o, p, base) |
| 400 | if decErr != nil && !state.shouldContinue(decErr, p) { |
| 401 | err = decErr |
| 402 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 403 | if err == nil && p.Required { |
| 404 | // Successfully decoded a required field. |
| 405 | if tag <= 64 { |
| 406 | // use bitmap for fields 1-64 to catch field reuse. |
| 407 | var mask uint64 = 1 << uint64(tag-1) |
| 408 | if reqFields&mask == 0 { |
| 409 | // new required field |
| 410 | reqFields |= mask |
| 411 | required-- |
| 412 | } |
| 413 | } else { |
| 414 | // This is imprecise. It can be fooled by a required field |
| 415 | // with a tag > 64 that is encoded twice; that's very rare. |
| 416 | // A fully correct implementation would require allocating |
| 417 | // a data structure, which we would like to avoid. |
| 418 | required-- |
| 419 | } |
| 420 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 421 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 422 | if err == nil { |
| 423 | if is_group { |
| 424 | return io.ErrUnexpectedEOF |
| 425 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 426 | if state.err != nil { |
| 427 | return state.err |
| 428 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 429 | if required > 0 { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 430 | // Not enough information to determine the exact field. If we use extra |
| 431 | // CPU, we could determine the field only if the missing required field |
| 432 | // has a tag <= 64 and we check reqFields. |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 433 | return &RequiredNotSetError{"{Unknown}"} |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 434 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 435 | } |
| 436 | return err |
| 437 | } |
| 438 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 439 | // Individual type decoders |
| 440 | // For each, |
| 441 | // u is the decoded value, |
| 442 | // v is a pointer to the field (pointer) in the struct |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 443 | |
| 444 | // Sizes of the pools to allocate inside the Buffer. |
| Rob Pike | 97edc7e | 2011-10-20 15:53:19 -0700 | [diff] [blame] | 445 | // The goal is modest amortization and allocation |
| 446 | // on at least 16-byte boundaries. |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 447 | const ( |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 448 | boolPoolSize = 16 |
| 449 | uint32PoolSize = 8 |
| 450 | uint64PoolSize = 4 |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 451 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 452 | |
| 453 | // Decode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 454 | func (o *Buffer) dec_bool(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 455 | u, err := p.valDec(o) |
| 456 | if err != nil { |
| 457 | return err |
| 458 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 459 | if len(o.bools) == 0 { |
| 460 | o.bools = make([]bool, boolPoolSize) |
| 461 | } |
| 462 | o.bools[0] = u != 0 |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 463 | *structPointer_Bool(base, p.field) = &o.bools[0] |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 464 | o.bools = o.bools[1:] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 465 | return nil |
| 466 | } |
| 467 | |
| 468 | // Decode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 469 | func (o *Buffer) dec_int32(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 470 | u, err := p.valDec(o) |
| 471 | if err != nil { |
| 472 | return err |
| 473 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 474 | word32_Set(structPointer_Word32(base, p.field), o, uint32(u)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 475 | return nil |
| 476 | } |
| 477 | |
| 478 | // Decode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 479 | func (o *Buffer) dec_int64(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 480 | u, err := p.valDec(o) |
| 481 | if err != nil { |
| 482 | return err |
| 483 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 484 | word64_Set(structPointer_Word64(base, p.field), o, u) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 485 | return nil |
| 486 | } |
| 487 | |
| 488 | // Decode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 489 | func (o *Buffer) dec_string(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 490 | s, err := o.DecodeStringBytes() |
| 491 | if err != nil { |
| 492 | return err |
| 493 | } |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 494 | sp := new(string) |
| 495 | *sp = s |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 496 | *structPointer_String(base, p.field) = sp |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | return nil |
| 498 | } |
| 499 | |
| 500 | // Decode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 501 | func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 502 | b, err := o.DecodeRawBytes(true) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 503 | if err != nil { |
| 504 | return err |
| 505 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 506 | *structPointer_Bytes(base, p.field) = b |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 507 | return nil |
| 508 | } |
| 509 | |
| 510 | // Decode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 511 | func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 512 | u, err := p.valDec(o) |
| 513 | if err != nil { |
| 514 | return err |
| 515 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 516 | v := structPointer_BoolSlice(base, p.field) |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 517 | *v = append(*v, u != 0) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 518 | return nil |
| 519 | } |
| 520 | |
| 521 | // Decode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 522 | func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error { |
| 523 | v := structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 524 | |
| 525 | nn, err := o.DecodeVarint() |
| 526 | if err != nil { |
| 527 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 528 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 529 | nb := int(nn) // number of bytes of encoded bools |
| 530 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 531 | y := *v |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 532 | for i := 0; i < nb; i++ { |
| 533 | u, err := p.valDec(o) |
| 534 | if err != nil { |
| 535 | return err |
| 536 | } |
| 537 | y = append(y, u != 0) |
| 538 | } |
| 539 | |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 540 | *v = y |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 541 | return nil |
| 542 | } |
| 543 | |
| 544 | // Decode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 545 | func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 546 | u, err := p.valDec(o) |
| 547 | if err != nil { |
| 548 | return err |
| 549 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 550 | structPointer_Word32Slice(base, p.field).Append(uint32(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 551 | return nil |
| 552 | } |
| 553 | |
| 554 | // Decode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 555 | func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error { |
| 556 | v := structPointer_Word32Slice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 557 | |
| 558 | nn, err := o.DecodeVarint() |
| 559 | if err != nil { |
| 560 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 561 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 562 | nb := int(nn) // number of bytes of encoded int32s |
| 563 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 564 | fin := o.index + nb |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 565 | if fin < o.index { |
| 566 | return errOverflow |
| 567 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 568 | for o.index < fin { |
| 569 | u, err := p.valDec(o) |
| 570 | if err != nil { |
| 571 | return err |
| 572 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 573 | v.Append(uint32(u)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 574 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 575 | return nil |
| 576 | } |
| 577 | |
| 578 | // Decode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 579 | func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 580 | u, err := p.valDec(o) |
| 581 | if err != nil { |
| 582 | return err |
| 583 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 584 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 585 | structPointer_Word64Slice(base, p.field).Append(u) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 586 | return nil |
| 587 | } |
| 588 | |
| 589 | // Decode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 590 | func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error { |
| 591 | v := structPointer_Word64Slice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 592 | |
| 593 | nn, err := o.DecodeVarint() |
| 594 | if err != nil { |
| 595 | return err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 596 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 597 | nb := int(nn) // number of bytes of encoded int64s |
| 598 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 599 | fin := o.index + nb |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 600 | if fin < o.index { |
| 601 | return errOverflow |
| 602 | } |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 603 | for o.index < fin { |
| 604 | u, err := p.valDec(o) |
| 605 | if err != nil { |
| 606 | return err |
| 607 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 608 | v.Append(u) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 609 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 610 | return nil |
| 611 | } |
| 612 | |
| 613 | // Decode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 614 | func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 615 | s, err := o.DecodeStringBytes() |
| 616 | if err != nil { |
| 617 | return err |
| 618 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 619 | v := structPointer_StringSlice(base, p.field) |
| 620 | *v = append(*v, 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). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 625 | func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) 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 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 630 | v := structPointer_BytesSlice(base, p.field) |
| 631 | *v = append(*v, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 632 | return nil |
| 633 | } |
| 634 | |
| 635 | // Decode a group. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 636 | func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 2ce8ed4 | 2013-06-20 13:22:17 +1000 | [diff] [blame] | 637 | bas := structPointer_GetStructPointer(base, p.field) |
| 638 | if structPointer_IsNil(bas) { |
| 639 | // allocate new nested message |
| 640 | bas = toStructPointer(reflect.New(p.stype)) |
| 641 | structPointer_SetStructPointer(base, p.field, bas) |
| 642 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 643 | return o.unmarshalType(p.stype, p.sprop, true, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | // Decode an embedded message. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 647 | func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 648 | raw, e := o.DecodeRawBytes(false) |
| 649 | if e != nil { |
| 650 | return e |
| 651 | } |
| 652 | |
| David Symonds | 2ce8ed4 | 2013-06-20 13:22:17 +1000 | [diff] [blame] | 653 | bas := structPointer_GetStructPointer(base, p.field) |
| 654 | if structPointer_IsNil(bas) { |
| 655 | // allocate new nested message |
| 656 | bas = toStructPointer(reflect.New(p.stype)) |
| 657 | structPointer_SetStructPointer(base, p.field, bas) |
| 658 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 659 | |
| 660 | // If the object can unmarshal itself, let it. |
| David Symonds | 4f8da86 | 2013-06-24 10:57:29 +1000 | [diff] [blame] | 661 | if p.isUnmarshaler { |
| David Symonds | 2ce8ed4 | 2013-06-20 13:22:17 +1000 | [diff] [blame] | 662 | iv := structPointer_Interface(bas, p.stype) |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 663 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | obuf := o.buf |
| 667 | oi := o.index |
| 668 | o.buf = raw |
| 669 | o.index = 0 |
| 670 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 671 | err = o.unmarshalType(p.stype, p.sprop, false, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 672 | o.buf = obuf |
| 673 | o.index = oi |
| 674 | |
| 675 | return err |
| 676 | } |
| 677 | |
| 678 | // Decode a slice of embedded messages. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 679 | func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 680 | return o.dec_slice_struct(p, false, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // Decode a slice of embedded groups. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 684 | func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 685 | return o.dec_slice_struct(p, true, base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // Decode a slice of structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 689 | func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error { |
| 690 | v := reflect.New(p.stype) |
| 691 | bas := toStructPointer(v) |
| 692 | structPointer_StructPointerSlice(base, p.field).Append(bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 693 | |
| 694 | if is_group { |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 695 | err := o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 696 | return err |
| 697 | } |
| 698 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 699 | raw, err := o.DecodeRawBytes(false) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 700 | if err != nil { |
| 701 | return err |
| 702 | } |
| 703 | |
| 704 | // If the object can unmarshal itself, let it. |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 705 | if p.isUnmarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 706 | iv := v.Interface() |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 707 | return iv.(Unmarshaler).Unmarshal(raw) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | obuf := o.buf |
| 711 | oi := o.index |
| 712 | o.buf = raw |
| 713 | o.index = 0 |
| 714 | |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 715 | err = o.unmarshalType(p.stype, p.sprop, is_group, bas) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 716 | |
| 717 | o.buf = obuf |
| 718 | o.index = oi |
| 719 | |
| 720 | return err |
| 721 | } |