decode.go 18 KB

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