decode.go 18 KB

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