decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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("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. required, reqFields := prop.reqCount, uint64(0)
  312. var err error
  313. for err == nil && o.index < len(o.buf) {
  314. oi := o.index
  315. var u uint64
  316. u, err = o.DecodeVarint()
  317. if err != nil {
  318. break
  319. }
  320. wire := int(u & 0x7)
  321. if wire == WireEndGroup {
  322. if is_group {
  323. return nil // input is satisfied
  324. }
  325. return ErrWrongType
  326. }
  327. tag := int(u >> 3)
  328. if tag <= 0 {
  329. return fmt.Errorf("proto: illegal tag %d", tag)
  330. }
  331. fieldnum, ok := prop.decoderTags.get(tag)
  332. if !ok {
  333. // Maybe it's an extension?
  334. if prop.extendable {
  335. if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
  336. if err = o.skip(st, tag, wire); err == nil {
  337. ext := e.ExtensionMap()[int32(tag)] // may be missing
  338. ext.enc = append(ext.enc, o.buf[oi:o.index]...)
  339. e.ExtensionMap()[int32(tag)] = ext
  340. }
  341. continue
  342. }
  343. }
  344. err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
  345. continue
  346. }
  347. p := prop.Prop[fieldnum]
  348. if p.dec == nil {
  349. fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
  350. continue
  351. }
  352. dec := p.dec
  353. if wire != WireStartGroup && wire != p.WireType {
  354. if wire == WireBytes && p.packedDec != nil {
  355. // a packable field
  356. dec = p.packedDec
  357. } else {
  358. err = ErrWrongType
  359. continue
  360. }
  361. }
  362. err = dec(o, p, base)
  363. if err == nil && p.Required {
  364. // Successfully decoded a required field.
  365. if tag <= 64 {
  366. // use bitmap for fields 1-64 to catch field reuse.
  367. var mask uint64 = 1 << uint64(tag-1)
  368. if reqFields&mask == 0 {
  369. // new required field
  370. reqFields |= mask
  371. required--
  372. }
  373. } else {
  374. // This is imprecise. It can be fooled by a required field
  375. // with a tag > 64 that is encoded twice; that's very rare.
  376. // A fully correct implementation would require allocating
  377. // a data structure, which we would like to avoid.
  378. required--
  379. }
  380. }
  381. }
  382. if err == nil {
  383. if is_group {
  384. return io.ErrUnexpectedEOF
  385. }
  386. if required > 0 {
  387. return &ErrRequiredNotSet{st}
  388. }
  389. }
  390. return err
  391. }
  392. // Individual type decoders
  393. // For each,
  394. // u is the decoded value,
  395. // v is a pointer to the field (pointer) in the struct
  396. // Sizes of the pools to allocate inside the Buffer.
  397. // The goal is modest amortization and allocation
  398. // on at least 16-byte boundaries.
  399. const (
  400. boolPoolSize = 16
  401. uint32PoolSize = 8
  402. uint64PoolSize = 4
  403. )
  404. // Decode a bool.
  405. func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
  406. u, err := p.valDec(o)
  407. if err != nil {
  408. return err
  409. }
  410. if len(o.bools) == 0 {
  411. o.bools = make([]bool, boolPoolSize)
  412. }
  413. o.bools[0] = u != 0
  414. *structPointer_Bool(base, p.field) = &o.bools[0]
  415. o.bools = o.bools[1:]
  416. return nil
  417. }
  418. // Decode an int32.
  419. func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
  420. u, err := p.valDec(o)
  421. if err != nil {
  422. return err
  423. }
  424. word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
  425. return nil
  426. }
  427. // Decode an int64.
  428. func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
  429. u, err := p.valDec(o)
  430. if err != nil {
  431. return err
  432. }
  433. word64_Set(structPointer_Word64(base, p.field), o, u)
  434. return nil
  435. }
  436. // Decode a string.
  437. func (o *Buffer) dec_string(p *Properties, base structPointer) error {
  438. s, err := o.DecodeStringBytes()
  439. if err != nil {
  440. return err
  441. }
  442. sp := new(string)
  443. *sp = s
  444. *structPointer_String(base, p.field) = sp
  445. return nil
  446. }
  447. // Decode a slice of bytes ([]byte).
  448. func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
  449. b, err := o.DecodeRawBytes(true)
  450. if err != nil {
  451. return err
  452. }
  453. *structPointer_Bytes(base, p.field) = b
  454. return nil
  455. }
  456. // Decode a slice of bools ([]bool).
  457. func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
  458. u, err := p.valDec(o)
  459. if err != nil {
  460. return err
  461. }
  462. v := structPointer_BoolSlice(base, p.field)
  463. *v = append(*v, u != 0)
  464. return nil
  465. }
  466. // Decode a slice of bools ([]bool) in packed format.
  467. func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
  468. v := structPointer_BoolSlice(base, p.field)
  469. nn, err := o.DecodeVarint()
  470. if err != nil {
  471. return err
  472. }
  473. nb := int(nn) // number of bytes of encoded bools
  474. y := *v
  475. for i := 0; i < nb; i++ {
  476. u, err := p.valDec(o)
  477. if err != nil {
  478. return err
  479. }
  480. y = append(y, u != 0)
  481. }
  482. *v = y
  483. return nil
  484. }
  485. // Decode a slice of int32s ([]int32).
  486. func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
  487. u, err := p.valDec(o)
  488. if err != nil {
  489. return err
  490. }
  491. structPointer_Word32Slice(base, p.field).Append(uint32(u))
  492. return nil
  493. }
  494. // Decode a slice of int32s ([]int32) in packed format.
  495. func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
  496. v := structPointer_Word32Slice(base, p.field)
  497. nn, err := o.DecodeVarint()
  498. if err != nil {
  499. return err
  500. }
  501. nb := int(nn) // number of bytes of encoded int32s
  502. fin := o.index + nb
  503. if fin < o.index {
  504. return errOverflow
  505. }
  506. for o.index < fin {
  507. u, err := p.valDec(o)
  508. if err != nil {
  509. return err
  510. }
  511. v.Append(uint32(u))
  512. }
  513. return nil
  514. }
  515. // Decode a slice of int64s ([]int64).
  516. func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
  517. u, err := p.valDec(o)
  518. if err != nil {
  519. return err
  520. }
  521. structPointer_Word64Slice(base, p.field).Append(u)
  522. return nil
  523. }
  524. // Decode a slice of int64s ([]int64) in packed format.
  525. func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
  526. v := structPointer_Word64Slice(base, p.field)
  527. nn, err := o.DecodeVarint()
  528. if err != nil {
  529. return err
  530. }
  531. nb := int(nn) // number of bytes of encoded int64s
  532. fin := o.index + nb
  533. if fin < o.index {
  534. return errOverflow
  535. }
  536. for o.index < fin {
  537. u, err := p.valDec(o)
  538. if err != nil {
  539. return err
  540. }
  541. v.Append(u)
  542. }
  543. return nil
  544. }
  545. // Decode a slice of strings ([]string).
  546. func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
  547. s, err := o.DecodeStringBytes()
  548. if err != nil {
  549. return err
  550. }
  551. v := structPointer_StringSlice(base, p.field)
  552. *v = append(*v, s)
  553. return nil
  554. }
  555. // Decode a slice of slice of bytes ([][]byte).
  556. func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
  557. b, err := o.DecodeRawBytes(true)
  558. if err != nil {
  559. return err
  560. }
  561. v := structPointer_BytesSlice(base, p.field)
  562. *v = append(*v, b)
  563. return nil
  564. }
  565. // Decode a group.
  566. func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
  567. bas := structPointer_GetStructPointer(base, p.field)
  568. if structPointer_IsNil(bas) {
  569. // allocate new nested message
  570. bas = toStructPointer(reflect.New(p.stype))
  571. structPointer_SetStructPointer(base, p.field, bas)
  572. }
  573. return o.unmarshalType(p.stype, p.sprop, true, bas)
  574. }
  575. // Decode an embedded message.
  576. func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
  577. raw, e := o.DecodeRawBytes(false)
  578. if e != nil {
  579. return e
  580. }
  581. bas := structPointer_GetStructPointer(base, p.field)
  582. if structPointer_IsNil(bas) {
  583. // allocate new nested message
  584. bas = toStructPointer(reflect.New(p.stype))
  585. structPointer_SetStructPointer(base, p.field, bas)
  586. }
  587. // If the object can unmarshal itself, let it.
  588. if p.isUnmarshaler {
  589. iv := structPointer_Interface(bas, p.stype)
  590. return iv.(Unmarshaler).Unmarshal(raw)
  591. }
  592. obuf := o.buf
  593. oi := o.index
  594. o.buf = raw
  595. o.index = 0
  596. err = o.unmarshalType(p.stype, p.sprop, false, bas)
  597. o.buf = obuf
  598. o.index = oi
  599. return err
  600. }
  601. // Decode a slice of embedded messages.
  602. func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
  603. return o.dec_slice_struct(p, false, base)
  604. }
  605. // Decode a slice of embedded groups.
  606. func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
  607. return o.dec_slice_struct(p, true, base)
  608. }
  609. // Decode a slice of structs ([]*struct).
  610. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
  611. v := reflect.New(p.stype)
  612. bas := toStructPointer(v)
  613. structPointer_StructPointerSlice(base, p.field).Append(bas)
  614. if is_group {
  615. err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
  616. return err
  617. }
  618. raw, err := o.DecodeRawBytes(false)
  619. if err != nil {
  620. return err
  621. }
  622. // If the object can unmarshal itself, let it.
  623. if p.isUnmarshaler {
  624. iv := v.Interface()
  625. return iv.(Unmarshaler).Unmarshal(raw)
  626. }
  627. obuf := o.buf
  628. oi := o.index
  629. o.buf = raw
  630. o.index = 0
  631. err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
  632. o.buf = obuf
  633. o.index = oi
  634. return err
  635. }