decode.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. package proto
  32. /*
  33. * Routines for decoding protocol buffer data to construct in-memory representations.
  34. */
  35. import (
  36. "bytes"
  37. "fmt"
  38. "io"
  39. "os"
  40. "reflect"
  41. "runtime"
  42. "unsafe"
  43. )
  44. // ErrWrongType occurs when the wire encoding for the field disagrees with
  45. // that specified in the type being decoded. This is usually caused by attempting
  46. // to convert an encoded protocol buffer into a struct of the wrong type.
  47. var ErrWrongType = os.NewError("field/encoding mismatch: wrong type for field")
  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 += 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. break
  68. }
  69. }
  70. return x, n
  71. }
  72. // DecodeVarint reads a varint-encoded integer from the Buffer.
  73. // This is the format for the
  74. // int32, int64, uint32, uint64, bool, and enum
  75. // protocol buffer types.
  76. func (p *Buffer) DecodeVarint() (x uint64, err os.Error) {
  77. // x, err already 0
  78. i := p.index
  79. l := len(p.buf)
  80. for shift := uint(0); ; shift += 7 {
  81. if i >= l {
  82. err = io.ErrUnexpectedEOF
  83. return
  84. }
  85. b := p.buf[i]
  86. i++
  87. x |= (uint64(b) & 0x7F) << shift
  88. if b < 0x80 {
  89. break
  90. }
  91. }
  92. p.index = i
  93. return
  94. }
  95. // DecodeFixed64 reads a 64-bit integer from the Buffer.
  96. // This is the format for the
  97. // fixed64, sfixed64, and double protocol buffer types.
  98. func (p *Buffer) DecodeFixed64() (x uint64, err os.Error) {
  99. // x, err already 0
  100. i := p.index + 8
  101. if i > len(p.buf) {
  102. err = io.ErrUnexpectedEOF
  103. return
  104. }
  105. p.index = i
  106. x = uint64(p.buf[i-8])
  107. x |= uint64(p.buf[i-7]) << 8
  108. x |= uint64(p.buf[i-6]) << 16
  109. x |= uint64(p.buf[i-5]) << 24
  110. x |= uint64(p.buf[i-4]) << 32
  111. x |= uint64(p.buf[i-3]) << 40
  112. x |= uint64(p.buf[i-2]) << 48
  113. x |= uint64(p.buf[i-1]) << 56
  114. return
  115. }
  116. // DecodeFixed32 reads a 32-bit integer from the Buffer.
  117. // This is the format for the
  118. // fixed32, sfixed32, and float protocol buffer types.
  119. func (p *Buffer) DecodeFixed32() (x uint64, err os.Error) {
  120. // x, err already 0
  121. i := p.index + 4
  122. if i > len(p.buf) {
  123. err = io.ErrUnexpectedEOF
  124. return
  125. }
  126. p.index = i
  127. x = uint64(p.buf[i-4])
  128. x |= uint64(p.buf[i-3]) << 8
  129. x |= uint64(p.buf[i-2]) << 16
  130. x |= uint64(p.buf[i-1]) << 24
  131. return
  132. }
  133. // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
  134. // from the Buffer.
  135. // This is the format used for the sint64 protocol buffer type.
  136. func (p *Buffer) DecodeZigzag64() (x uint64, err os.Error) {
  137. x, err = p.DecodeVarint()
  138. if err != nil {
  139. return
  140. }
  141. x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
  142. return
  143. }
  144. // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
  145. // from the Buffer.
  146. // This is the format used for the sint32 protocol buffer type.
  147. func (p *Buffer) DecodeZigzag32() (x uint64, err os.Error) {
  148. x, err = p.DecodeVarint()
  149. if err != nil {
  150. return
  151. }
  152. x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
  153. return
  154. }
  155. // These are not ValueDecoders: they produce an array of bytes or a string.
  156. // bytes, embedded messages
  157. // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
  158. // This is the format used for the bytes protocol buffer
  159. // type and for embedded messages.
  160. func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err os.Error) {
  161. n, err := p.DecodeVarint()
  162. if err != nil {
  163. return
  164. }
  165. nb := int(n)
  166. if p.index+nb > len(p.buf) {
  167. err = io.ErrUnexpectedEOF
  168. return
  169. }
  170. if !alloc {
  171. // todo: check if can get more uses of alloc=false
  172. buf = p.buf[p.index : p.index+nb]
  173. p.index += nb
  174. return
  175. }
  176. buf = make([]byte, nb)
  177. copy(buf, p.buf[p.index:])
  178. p.index += nb
  179. return
  180. }
  181. // DecodeStringBytes reads an encoded string from the Buffer.
  182. // This is the format used for the proto2 string type.
  183. func (p *Buffer) DecodeStringBytes() (s string, err os.Error) {
  184. buf, err := p.DecodeRawBytes(false)
  185. if err != nil {
  186. return
  187. }
  188. return string(buf), nil
  189. }
  190. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  191. // If the protocol buffer has extensions, and the field matches, add it as an extension.
  192. // Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
  193. func (o *Buffer) skipAndSave(t *reflect.StructType, tag, wire int, base uintptr) os.Error {
  194. oi := o.index
  195. err := o.skip(t, tag, wire)
  196. if err != nil {
  197. return err
  198. }
  199. x := fieldIndex(t, "XXX_unrecognized")
  200. if x == nil {
  201. return nil
  202. }
  203. p := propByIndex(t, x)
  204. ptr := (*[]byte)(unsafe.Pointer(base + p.offset))
  205. if *ptr == nil {
  206. // This is the first skipped element,
  207. // allocate a new buffer.
  208. *ptr = o.bufalloc()
  209. }
  210. // Add the skipped field to struct field
  211. obuf := o.buf
  212. o.buf = *ptr
  213. o.EncodeVarint(uint64(tag<<3 | wire))
  214. *ptr = bytes.Add(o.buf, obuf[oi:o.index])
  215. o.buf = obuf
  216. return nil
  217. }
  218. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  219. func (o *Buffer) skip(t *reflect.StructType, tag, wire int) os.Error {
  220. var u uint64
  221. var err os.Error
  222. switch wire {
  223. case WireVarint:
  224. _, err = o.DecodeVarint()
  225. case WireFixed64:
  226. _, err = o.DecodeFixed64()
  227. case WireBytes:
  228. _, err = o.DecodeRawBytes(false)
  229. case WireFixed32:
  230. _, err = o.DecodeFixed32()
  231. case WireStartGroup:
  232. for {
  233. u, err = o.DecodeVarint()
  234. if err != nil {
  235. break
  236. }
  237. fwire := int(u & 0x7)
  238. if fwire == WireEndGroup {
  239. break
  240. }
  241. ftag := int(u >> 3)
  242. err = o.skip(t, ftag, fwire)
  243. if err != nil {
  244. break
  245. }
  246. }
  247. default:
  248. fmt.Fprintf(os.Stderr, "proto: can't skip wire type %d for %s\n", wire, t)
  249. }
  250. return err
  251. }
  252. // Unmarshaler is the interface representing objects that can unmarshal themselves.
  253. type Unmarshaler interface {
  254. Unmarshal([]byte) os.Error
  255. }
  256. // Unmarshal parses the protocol buffer representation in buf and places the
  257. // decoded result in pb. If the struct underlying pb does not match
  258. // the data in buf, the results can be unpredictable.
  259. func Unmarshal(buf []byte, pb interface{}) os.Error {
  260. // If the object can unmarshal itself, let it.
  261. if u, ok := pb.(Unmarshaler); ok {
  262. return u.Unmarshal(buf)
  263. }
  264. return NewBuffer(buf).Unmarshal(pb)
  265. }
  266. // Unmarshal parses the protocol buffer representation in the
  267. // Buffer and places the decoded result in pb. If the struct
  268. // underlying pb does not match the data in the buffer, the results can be
  269. // unpredictable.
  270. func (p *Buffer) Unmarshal(pb interface{}) os.Error {
  271. // If the object can unmarshal itself, let it.
  272. if u, ok := pb.(Unmarshaler); ok {
  273. err := u.Unmarshal(p.buf[p.index:])
  274. p.index = len(p.buf)
  275. return err
  276. }
  277. mstat := runtime.MemStats.Mallocs
  278. typ, base, err := getbase(pb)
  279. if err != nil {
  280. return err
  281. }
  282. err = p.unmarshalType(typ, false, base)
  283. mstat = runtime.MemStats.Mallocs - mstat
  284. stats.Dmalloc += mstat
  285. stats.Decode++
  286. return err
  287. }
  288. // unmarshalType does the work of unmarshaling a structure.
  289. func (o *Buffer) unmarshalType(t *reflect.PtrType, is_group bool, base uintptr) os.Error {
  290. st := t.Elem().(*reflect.StructType)
  291. prop := GetProperties(st)
  292. sbase := getsbase(prop) // scratch area for data items
  293. var err os.Error
  294. for err == nil && o.index < len(o.buf) {
  295. oi := o.index
  296. var u uint64
  297. u, err = o.DecodeVarint()
  298. if err != nil {
  299. break
  300. }
  301. wire := int(u & 0x7)
  302. if wire == WireEndGroup {
  303. if is_group {
  304. return nil // input is satisfied
  305. }
  306. return ErrWrongType
  307. }
  308. tag := int(u >> 3)
  309. fieldnum, ok := prop.tags[tag]
  310. if !ok {
  311. // Maybe it's an extension?
  312. o.ptr = base
  313. iv := unsafe.Unreflect(t, unsafe.Pointer(&o.ptr))
  314. if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) {
  315. if err = o.skip(st, tag, wire); err == nil {
  316. e.ExtensionMap()[int32(tag)] = bytes.Add(nil, o.buf[oi:o.index])
  317. }
  318. continue
  319. }
  320. err = o.skipAndSave(st, tag, wire, base)
  321. continue
  322. }
  323. p := prop.Prop[fieldnum]
  324. if p.dec != nil {
  325. if wire != WireStartGroup && wire != p.WireType {
  326. err = ErrWrongType
  327. continue
  328. }
  329. err = p.dec(o, p, base, sbase)
  330. continue
  331. }
  332. fmt.Fprintf(os.Stderr, "no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
  333. }
  334. if err == nil && is_group {
  335. return io.ErrUnexpectedEOF
  336. }
  337. return err
  338. }
  339. // Make *pslice have base address base, length 0, and capacity startSize.
  340. func initSlice(pslice unsafe.Pointer, base uintptr) {
  341. sp := (*reflect.SliceHeader)(pslice)
  342. sp.Data = base
  343. sp.Len = 0
  344. sp.Cap = startSize
  345. }
  346. // Individual type decoders
  347. // For each,
  348. // u is the decoded value,
  349. // v is a pointer to the field (pointer) in the struct
  350. // x is a pointer to the preallocated scratch space to hold the decoded value.
  351. // Decode a bool.
  352. func (o *Buffer) dec_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
  353. u, err := p.valDec(o)
  354. if err != nil {
  355. return err
  356. }
  357. v := (**uint8)(unsafe.Pointer(base + p.offset))
  358. x := (*uint8)(unsafe.Pointer(sbase + p.scratch))
  359. *x = uint8(u)
  360. *v = x
  361. return nil
  362. }
  363. // Decode an int32.
  364. func (o *Buffer) dec_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
  365. u, err := p.valDec(o)
  366. if err != nil {
  367. return err
  368. }
  369. v := (**int32)(unsafe.Pointer(base + p.offset))
  370. x := (*int32)(unsafe.Pointer(sbase + p.scratch))
  371. *x = int32(u)
  372. *v = x
  373. return nil
  374. }
  375. // Decode an int64.
  376. func (o *Buffer) dec_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
  377. u, err := p.valDec(o)
  378. if err != nil {
  379. return err
  380. }
  381. v := (**int64)(unsafe.Pointer(base + p.offset))
  382. x := (*int64)(unsafe.Pointer(sbase + p.scratch))
  383. *x = int64(u)
  384. *v = x
  385. return nil
  386. }
  387. // Decode a string.
  388. func (o *Buffer) dec_string(p *Properties, base uintptr, sbase uintptr) os.Error {
  389. s, err := o.DecodeStringBytes()
  390. if err != nil {
  391. return err
  392. }
  393. v := (**string)(unsafe.Pointer(base + p.offset))
  394. x := (*string)(unsafe.Pointer(sbase + p.scratch))
  395. *x = s
  396. *v = x
  397. return nil
  398. }
  399. // Decode a slice of bytes ([]byte).
  400. func (o *Buffer) dec_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
  401. b, err := o.DecodeRawBytes(false)
  402. if err != nil {
  403. return err
  404. }
  405. lb := len(b)
  406. if lb == 0 {
  407. return nil
  408. }
  409. x := (*[]uint8)(unsafe.Pointer(base + p.offset))
  410. y := *x
  411. c := cap(y)
  412. if c == 0 {
  413. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  414. y = *x
  415. c = cap(y)
  416. }
  417. l := len(y)
  418. if l+lb > c {
  419. // incremental growth is max(len(slice)*1.5, len(slice)+len(bytes))
  420. g := l * 3 / 2
  421. if l+lb > g {
  422. g = l + lb
  423. }
  424. z := make([]uint8, l, g)
  425. copy(z, y)
  426. y = z
  427. }
  428. y = y[0 : l+lb]
  429. copy(y[l:l+lb], b)
  430. *x = y
  431. return nil
  432. }
  433. // Decode a slice of bools ([]bool).
  434. func (o *Buffer) dec_slice_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
  435. u, err := p.valDec(o)
  436. if err != nil {
  437. return err
  438. }
  439. x := (*[]bool)(unsafe.Pointer(base + p.offset))
  440. y := *x
  441. c := cap(y)
  442. if c == 0 {
  443. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  444. y = *x
  445. c = cap(y)
  446. }
  447. l := len(y)
  448. if l >= c {
  449. g := l * 3 / 2
  450. z := make([]bool, l, g)
  451. copy(z, y)
  452. y = z
  453. }
  454. y = y[0 : l+1]
  455. y[l] = u != 0
  456. *x = y
  457. return nil
  458. }
  459. // Decode a slice of int32s ([]int32).
  460. func (o *Buffer) dec_slice_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
  461. u, err := p.valDec(o)
  462. if err != nil {
  463. return err
  464. }
  465. x := (*[]int32)(unsafe.Pointer(base + p.offset))
  466. y := *x
  467. c := cap(y)
  468. if c == 0 {
  469. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  470. y = *x
  471. c = cap(y)
  472. }
  473. l := len(y)
  474. if l >= c {
  475. g := l * 3 / 2
  476. z := make([]int32, l, g)
  477. copy(z, y)
  478. y = z
  479. }
  480. y = y[0 : l+1]
  481. y[l] = int32(u)
  482. *x = y
  483. return nil
  484. }
  485. // Decode a slice of int64s ([]int64).
  486. func (o *Buffer) dec_slice_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
  487. u, err := p.valDec(o)
  488. if err != nil {
  489. return err
  490. }
  491. x := (*[]int64)(unsafe.Pointer(base + p.offset))
  492. y := *x
  493. c := cap(y)
  494. if c == 0 {
  495. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  496. y = *x
  497. c = cap(y)
  498. }
  499. l := len(y)
  500. if l >= c {
  501. g := l * 3 / 2
  502. z := make([]int64, l, g)
  503. copy(z, y)
  504. y = z
  505. }
  506. y = y[0 : l+1]
  507. y[l] = int64(u)
  508. *x = y
  509. return nil
  510. }
  511. // Decode a slice of strings ([]string).
  512. func (o *Buffer) dec_slice_string(p *Properties, base uintptr, sbase uintptr) os.Error {
  513. s, err := o.DecodeStringBytes()
  514. if err != nil {
  515. return err
  516. }
  517. x := (*[]string)(unsafe.Pointer(base + p.offset))
  518. y := *x
  519. c := cap(y)
  520. if c == 0 {
  521. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  522. y = *x
  523. c = cap(y)
  524. }
  525. l := len(y)
  526. if l >= c {
  527. g := l * 3 / 2
  528. z := make([]string, l, g)
  529. copy(z, y)
  530. y = z
  531. }
  532. y = y[0 : l+1]
  533. y[l] = s
  534. *x = y
  535. return nil
  536. }
  537. // Decode a slice of slice of bytes ([][]byte).
  538. func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
  539. b, err := o.DecodeRawBytes(true)
  540. if err != nil {
  541. return err
  542. }
  543. x := (*[][]byte)(unsafe.Pointer(base + p.offset))
  544. y := *x
  545. c := cap(y)
  546. if c == 0 {
  547. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  548. y = *x
  549. c = cap(y)
  550. }
  551. l := len(y)
  552. if l >= c {
  553. g := l * 3 / 2
  554. z := make([][]byte, l, g)
  555. copy(z, y)
  556. y = z
  557. }
  558. y = y[0 : l+1]
  559. y[l] = b
  560. *x = y
  561. return nil
  562. }
  563. // Decode a group.
  564. func (o *Buffer) dec_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
  565. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  566. typ := p.stype.Elem().(*reflect.StructType)
  567. structv := unsafe.New(typ)
  568. bas := uintptr(structv)
  569. *ptr = (*struct{})(structv)
  570. err := o.unmarshalType(p.stype, true, bas)
  571. return err
  572. }
  573. // Decode an embedded message.
  574. func (o *Buffer) dec_struct_message(p *Properties, base uintptr, sbase uintptr) (err os.Error) {
  575. raw, e := o.DecodeRawBytes(false)
  576. if e != nil {
  577. return e
  578. }
  579. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  580. typ := p.stype.Elem().(*reflect.StructType)
  581. structv := unsafe.New(typ)
  582. bas := uintptr(structv)
  583. *ptr = (*struct{})(structv)
  584. // If the object can unmarshal itself, let it.
  585. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr))
  586. if u, ok := iv.(Unmarshaler); ok {
  587. return u.Unmarshal(raw)
  588. }
  589. obuf := o.buf
  590. oi := o.index
  591. o.buf = raw
  592. o.index = 0
  593. err = o.unmarshalType(p.stype, false, bas)
  594. o.buf = obuf
  595. o.index = oi
  596. return err
  597. }
  598. // Decode a slice of embedded messages.
  599. func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr, sbase uintptr) os.Error {
  600. return o.dec_slice_struct(p, false, base, sbase)
  601. }
  602. // Decode a slice of embedded groups.
  603. func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
  604. return o.dec_slice_struct(p, true, base, sbase)
  605. }
  606. // Decode a slice of structs ([]*struct).
  607. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr, sbase uintptr) os.Error {
  608. x := (*[]*struct{})(unsafe.Pointer(base + p.offset))
  609. y := *x
  610. c := cap(y)
  611. if c == 0 {
  612. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  613. y = *x
  614. c = cap(y)
  615. }
  616. l := len(y)
  617. if l >= c {
  618. // Create a new slice with 1.5X the capacity.
  619. g := l * 3 / 2
  620. z := make([]*struct{}, l, g)
  621. copy(z, y)
  622. y = z
  623. }
  624. y = y[0 : l+1]
  625. *x = y
  626. typ := p.stype.Elem().(*reflect.StructType)
  627. structv := unsafe.New(typ)
  628. bas := uintptr(structv)
  629. y[l] = (*struct{})(structv)
  630. if is_group {
  631. err := o.unmarshalType(p.stype, is_group, bas)
  632. return err
  633. }
  634. raw, err := o.DecodeRawBytes(true)
  635. if err != nil {
  636. return err
  637. }
  638. // If the object can unmarshal itself, let it.
  639. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[l]))
  640. if u, ok := iv.(Unmarshaler); ok {
  641. return u.Unmarshal(raw)
  642. }
  643. obuf := o.buf
  644. oi := o.index
  645. o.buf = raw
  646. o.index = 0
  647. err = o.unmarshalType(p.stype, is_group, bas)
  648. o.buf = obuf
  649. o.index = oi
  650. return err
  651. }