decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. required, reqFields := prop.reqCount, uint64(0)
  293. sbase := getsbase(prop) // scratch area for data items
  294. var err os.Error
  295. for err == nil && o.index < len(o.buf) {
  296. oi := o.index
  297. var u uint64
  298. u, err = o.DecodeVarint()
  299. if err != nil {
  300. break
  301. }
  302. wire := int(u & 0x7)
  303. if wire == WireEndGroup {
  304. if is_group {
  305. return nil // input is satisfied
  306. }
  307. return ErrWrongType
  308. }
  309. tag := int(u >> 3)
  310. fieldnum, ok := prop.tags[tag]
  311. if !ok {
  312. // Maybe it's an extension?
  313. o.ptr = base
  314. iv := unsafe.Unreflect(t, unsafe.Pointer(&o.ptr))
  315. if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) {
  316. if err = o.skip(st, tag, wire); err == nil {
  317. e.ExtensionMap()[int32(tag)] = bytes.Add(nil, o.buf[oi:o.index])
  318. }
  319. continue
  320. }
  321. err = o.skipAndSave(st, tag, wire, base)
  322. continue
  323. }
  324. p := prop.Prop[fieldnum]
  325. if p.dec == nil {
  326. fmt.Fprintf(os.Stderr, "no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
  327. continue
  328. }
  329. if wire != WireStartGroup && wire != p.WireType {
  330. err = ErrWrongType
  331. continue
  332. }
  333. err = p.dec(o, p, base, sbase)
  334. if err == nil && p.Required {
  335. // Successfully decoded a required field.
  336. if tag <= 64 {
  337. // use bitmap for fields 1-64 to catch field reuse.
  338. var mask uint64 = 1 << uint64(tag-1)
  339. if reqFields&mask == 0 {
  340. // new required field
  341. reqFields |= mask
  342. required--
  343. }
  344. } else {
  345. // This is imprecise. It can be fooled by a required field
  346. // with a tag > 64 that is encoded twice; that's very rare.
  347. // A fully correct implementation would require allocating
  348. // a data structure, which we would like to avoid.
  349. required--
  350. }
  351. }
  352. }
  353. if err == nil {
  354. if is_group {
  355. return io.ErrUnexpectedEOF
  356. }
  357. if required > 0 {
  358. return ErrRequiredNotSet
  359. }
  360. }
  361. return err
  362. }
  363. // Make *pslice have base address base, length 0, and capacity startSize.
  364. func initSlice(pslice unsafe.Pointer, base uintptr) {
  365. sp := (*reflect.SliceHeader)(pslice)
  366. sp.Data = base
  367. sp.Len = 0
  368. sp.Cap = startSize
  369. }
  370. // Individual type decoders
  371. // For each,
  372. // u is the decoded value,
  373. // v is a pointer to the field (pointer) in the struct
  374. // x is a pointer to the preallocated scratch space to hold the decoded value.
  375. // Decode a bool.
  376. func (o *Buffer) dec_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
  377. u, err := p.valDec(o)
  378. if err != nil {
  379. return err
  380. }
  381. v := (**uint8)(unsafe.Pointer(base + p.offset))
  382. x := (*uint8)(unsafe.Pointer(sbase + p.scratch))
  383. *x = uint8(u)
  384. *v = x
  385. return nil
  386. }
  387. // Decode an int32.
  388. func (o *Buffer) dec_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
  389. u, err := p.valDec(o)
  390. if err != nil {
  391. return err
  392. }
  393. v := (**int32)(unsafe.Pointer(base + p.offset))
  394. x := (*int32)(unsafe.Pointer(sbase + p.scratch))
  395. *x = int32(u)
  396. *v = x
  397. return nil
  398. }
  399. // Decode an int64.
  400. func (o *Buffer) dec_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
  401. u, err := p.valDec(o)
  402. if err != nil {
  403. return err
  404. }
  405. v := (**int64)(unsafe.Pointer(base + p.offset))
  406. x := (*int64)(unsafe.Pointer(sbase + p.scratch))
  407. *x = int64(u)
  408. *v = x
  409. return nil
  410. }
  411. // Decode a string.
  412. func (o *Buffer) dec_string(p *Properties, base uintptr, sbase uintptr) os.Error {
  413. s, err := o.DecodeStringBytes()
  414. if err != nil {
  415. return err
  416. }
  417. v := (**string)(unsafe.Pointer(base + p.offset))
  418. x := (*string)(unsafe.Pointer(sbase + p.scratch))
  419. *x = s
  420. *v = x
  421. return nil
  422. }
  423. // Decode a slice of bytes ([]byte).
  424. func (o *Buffer) dec_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
  425. b, err := o.DecodeRawBytes(false)
  426. if err != nil {
  427. return err
  428. }
  429. lb := len(b)
  430. if lb == 0 {
  431. return nil
  432. }
  433. x := (*[]uint8)(unsafe.Pointer(base + p.offset))
  434. y := *x
  435. c := cap(y)
  436. if c == 0 {
  437. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  438. y = *x
  439. c = cap(y)
  440. }
  441. l := len(y)
  442. if l+lb > c {
  443. // incremental growth is max(len(slice)*1.5, len(slice)+len(bytes))
  444. g := l * 3 / 2
  445. if l+lb > g {
  446. g = l + lb
  447. }
  448. z := make([]uint8, l, g)
  449. copy(z, y)
  450. y = z
  451. }
  452. y = y[0 : l+lb]
  453. copy(y[l:l+lb], b)
  454. *x = y
  455. return nil
  456. }
  457. // Decode a slice of bools ([]bool).
  458. func (o *Buffer) dec_slice_bool(p *Properties, base uintptr, sbase uintptr) os.Error {
  459. u, err := p.valDec(o)
  460. if err != nil {
  461. return err
  462. }
  463. x := (*[]bool)(unsafe.Pointer(base + p.offset))
  464. y := *x
  465. c := cap(y)
  466. if c == 0 {
  467. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  468. y = *x
  469. c = cap(y)
  470. }
  471. l := len(y)
  472. if l >= c {
  473. g := l * 3 / 2
  474. z := make([]bool, l, g)
  475. copy(z, y)
  476. y = z
  477. }
  478. y = y[0 : l+1]
  479. y[l] = u != 0
  480. *x = y
  481. return nil
  482. }
  483. // Decode a slice of int32s ([]int32).
  484. func (o *Buffer) dec_slice_int32(p *Properties, base uintptr, sbase uintptr) os.Error {
  485. u, err := p.valDec(o)
  486. if err != nil {
  487. return err
  488. }
  489. x := (*[]int32)(unsafe.Pointer(base + p.offset))
  490. y := *x
  491. c := cap(y)
  492. if c == 0 {
  493. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  494. y = *x
  495. c = cap(y)
  496. }
  497. l := len(y)
  498. if l >= c {
  499. g := l * 3 / 2
  500. z := make([]int32, l, g)
  501. copy(z, y)
  502. y = z
  503. }
  504. y = y[0 : l+1]
  505. y[l] = int32(u)
  506. *x = y
  507. return nil
  508. }
  509. // Decode a slice of int64s ([]int64).
  510. func (o *Buffer) dec_slice_int64(p *Properties, base uintptr, sbase uintptr) os.Error {
  511. u, err := p.valDec(o)
  512. if err != nil {
  513. return err
  514. }
  515. x := (*[]int64)(unsafe.Pointer(base + p.offset))
  516. y := *x
  517. c := cap(y)
  518. if c == 0 {
  519. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  520. y = *x
  521. c = cap(y)
  522. }
  523. l := len(y)
  524. if l >= c {
  525. g := l * 3 / 2
  526. z := make([]int64, l, g)
  527. copy(z, y)
  528. y = z
  529. }
  530. y = y[0 : l+1]
  531. y[l] = int64(u)
  532. *x = y
  533. return nil
  534. }
  535. // Decode a slice of strings ([]string).
  536. func (o *Buffer) dec_slice_string(p *Properties, base uintptr, sbase uintptr) os.Error {
  537. s, err := o.DecodeStringBytes()
  538. if err != nil {
  539. return err
  540. }
  541. x := (*[]string)(unsafe.Pointer(base + p.offset))
  542. y := *x
  543. c := cap(y)
  544. if c == 0 {
  545. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  546. y = *x
  547. c = cap(y)
  548. }
  549. l := len(y)
  550. if l >= c {
  551. g := l * 3 / 2
  552. z := make([]string, l, g)
  553. copy(z, y)
  554. y = z
  555. }
  556. y = y[0 : l+1]
  557. y[l] = s
  558. *x = y
  559. return nil
  560. }
  561. // Decode a slice of slice of bytes ([][]byte).
  562. func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr, sbase uintptr) os.Error {
  563. b, err := o.DecodeRawBytes(true)
  564. if err != nil {
  565. return err
  566. }
  567. x := (*[][]byte)(unsafe.Pointer(base + p.offset))
  568. y := *x
  569. c := cap(y)
  570. if c == 0 {
  571. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  572. y = *x
  573. c = cap(y)
  574. }
  575. l := len(y)
  576. if l >= c {
  577. g := l * 3 / 2
  578. z := make([][]byte, l, g)
  579. copy(z, y)
  580. y = z
  581. }
  582. y = y[0 : l+1]
  583. y[l] = b
  584. *x = y
  585. return nil
  586. }
  587. // Decode a group.
  588. func (o *Buffer) dec_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
  589. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  590. typ := p.stype.Elem().(*reflect.StructType)
  591. structv := unsafe.New(typ)
  592. bas := uintptr(structv)
  593. *ptr = (*struct{})(structv)
  594. err := o.unmarshalType(p.stype, true, bas)
  595. return err
  596. }
  597. // Decode an embedded message.
  598. func (o *Buffer) dec_struct_message(p *Properties, base uintptr, sbase uintptr) (err os.Error) {
  599. raw, e := o.DecodeRawBytes(false)
  600. if e != nil {
  601. return e
  602. }
  603. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  604. typ := p.stype.Elem().(*reflect.StructType)
  605. structv := unsafe.New(typ)
  606. bas := uintptr(structv)
  607. *ptr = (*struct{})(structv)
  608. // If the object can unmarshal itself, let it.
  609. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(ptr))
  610. if u, ok := iv.(Unmarshaler); ok {
  611. return u.Unmarshal(raw)
  612. }
  613. obuf := o.buf
  614. oi := o.index
  615. o.buf = raw
  616. o.index = 0
  617. err = o.unmarshalType(p.stype, false, bas)
  618. o.buf = obuf
  619. o.index = oi
  620. return err
  621. }
  622. // Decode a slice of embedded messages.
  623. func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr, sbase uintptr) os.Error {
  624. return o.dec_slice_struct(p, false, base, sbase)
  625. }
  626. // Decode a slice of embedded groups.
  627. func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr, sbase uintptr) os.Error {
  628. return o.dec_slice_struct(p, true, base, sbase)
  629. }
  630. // Decode a slice of structs ([]*struct).
  631. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr, sbase uintptr) os.Error {
  632. x := (*[]*struct{})(unsafe.Pointer(base + p.offset))
  633. y := *x
  634. c := cap(y)
  635. if c == 0 {
  636. initSlice(unsafe.Pointer(x), sbase+p.scratch)
  637. y = *x
  638. c = cap(y)
  639. }
  640. l := len(y)
  641. if l >= c {
  642. // Create a new slice with 1.5X the capacity.
  643. g := l * 3 / 2
  644. z := make([]*struct{}, l, g)
  645. copy(z, y)
  646. y = z
  647. }
  648. y = y[0 : l+1]
  649. *x = y
  650. typ := p.stype.Elem().(*reflect.StructType)
  651. structv := unsafe.New(typ)
  652. bas := uintptr(structv)
  653. y[l] = (*struct{})(structv)
  654. if is_group {
  655. err := o.unmarshalType(p.stype, is_group, bas)
  656. return err
  657. }
  658. raw, err := o.DecodeRawBytes(true)
  659. if err != nil {
  660. return err
  661. }
  662. // If the object can unmarshal itself, let it.
  663. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&y[l]))
  664. if u, ok := iv.(Unmarshaler); ok {
  665. return u.Unmarshal(raw)
  666. }
  667. obuf := o.buf
  668. oi := o.index
  669. o.buf = raw
  670. o.index = 0
  671. err = o.unmarshalType(p.stype, is_group, bas)
  672. o.buf = obuf
  673. o.index = oi
  674. return err
  675. }