decode.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. // errOverflow is returned when an integer is too large to be represented.
  43. var errOverflow = errors.New("proto: integer overflow")
  44. // ErrInternalBadWireType is returned by generated code when an incorrect
  45. // wire type is encountered. It does not get returned to user code.
  46. var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
  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 < 64; 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. return x, n
  67. }
  68. }
  69. // The number is too large to represent in a 64-bit value.
  70. return 0, 0
  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 error) {
  77. // x, err already 0
  78. i := p.index
  79. l := len(p.buf)
  80. for shift := uint(0); shift < 64; 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. p.index = i
  90. return
  91. }
  92. }
  93. // The number is too large to represent in a 64-bit value.
  94. err = errOverflow
  95. return
  96. }
  97. // DecodeFixed64 reads a 64-bit integer from the Buffer.
  98. // This is the format for the
  99. // fixed64, sfixed64, and double protocol buffer types.
  100. func (p *Buffer) DecodeFixed64() (x uint64, err error) {
  101. // x, err already 0
  102. i := p.index + 8
  103. if i < 0 || i > len(p.buf) {
  104. err = io.ErrUnexpectedEOF
  105. return
  106. }
  107. p.index = i
  108. x = uint64(p.buf[i-8])
  109. x |= uint64(p.buf[i-7]) << 8
  110. x |= uint64(p.buf[i-6]) << 16
  111. x |= uint64(p.buf[i-5]) << 24
  112. x |= uint64(p.buf[i-4]) << 32
  113. x |= uint64(p.buf[i-3]) << 40
  114. x |= uint64(p.buf[i-2]) << 48
  115. x |= uint64(p.buf[i-1]) << 56
  116. return
  117. }
  118. // DecodeFixed32 reads a 32-bit integer from the Buffer.
  119. // This is the format for the
  120. // fixed32, sfixed32, and float protocol buffer types.
  121. func (p *Buffer) DecodeFixed32() (x uint64, err error) {
  122. // x, err already 0
  123. i := p.index + 4
  124. if i < 0 || i > len(p.buf) {
  125. err = io.ErrUnexpectedEOF
  126. return
  127. }
  128. p.index = i
  129. x = uint64(p.buf[i-4])
  130. x |= uint64(p.buf[i-3]) << 8
  131. x |= uint64(p.buf[i-2]) << 16
  132. x |= uint64(p.buf[i-1]) << 24
  133. return
  134. }
  135. // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
  136. // from the Buffer.
  137. // This is the format used for the sint64 protocol buffer type.
  138. func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
  139. x, err = p.DecodeVarint()
  140. if err != nil {
  141. return
  142. }
  143. x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
  144. return
  145. }
  146. // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
  147. // from the Buffer.
  148. // This is the format used for the sint32 protocol buffer type.
  149. func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
  150. x, err = p.DecodeVarint()
  151. if err != nil {
  152. return
  153. }
  154. x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
  155. return
  156. }
  157. // These are not ValueDecoders: they produce an array of bytes or a string.
  158. // bytes, embedded messages
  159. // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
  160. // This is the format used for the bytes protocol buffer
  161. // type and for embedded messages.
  162. func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
  163. n, err := p.DecodeVarint()
  164. if err != nil {
  165. return nil, err
  166. }
  167. nb := int(n)
  168. if nb < 0 {
  169. return nil, fmt.Errorf("proto: bad byte length %d", nb)
  170. }
  171. end := p.index + nb
  172. if end < p.index || end > len(p.buf) {
  173. return nil, io.ErrUnexpectedEOF
  174. }
  175. if !alloc {
  176. // todo: check if can get more uses of alloc=false
  177. buf = p.buf[p.index:end]
  178. p.index += nb
  179. return
  180. }
  181. buf = make([]byte, nb)
  182. copy(buf, p.buf[p.index:])
  183. p.index += nb
  184. return
  185. }
  186. // DecodeStringBytes reads an encoded string from the Buffer.
  187. // This is the format used for the proto2 string type.
  188. func (p *Buffer) DecodeStringBytes() (s string, err error) {
  189. buf, err := p.DecodeRawBytes(false)
  190. if err != nil {
  191. return
  192. }
  193. return string(buf), nil
  194. }
  195. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  196. // If the protocol buffer has extensions, and the field matches, add it as an extension.
  197. // Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
  198. func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
  199. oi := o.index
  200. err := o.skip(t, tag, wire)
  201. if err != nil {
  202. return err
  203. }
  204. if !unrecField.IsValid() {
  205. return nil
  206. }
  207. ptr := structPointer_Bytes(base, unrecField)
  208. // Add the skipped field to struct field
  209. obuf := o.buf
  210. o.buf = *ptr
  211. o.EncodeVarint(uint64(tag<<3 | wire))
  212. *ptr = append(o.buf, obuf[oi:o.index]...)
  213. o.buf = obuf
  214. return nil
  215. }
  216. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  217. func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
  218. var u uint64
  219. var err error
  220. switch wire {
  221. case WireVarint:
  222. _, err = o.DecodeVarint()
  223. case WireFixed64:
  224. _, err = o.DecodeFixed64()
  225. case WireBytes:
  226. _, err = o.DecodeRawBytes(false)
  227. case WireFixed32:
  228. _, err = o.DecodeFixed32()
  229. case WireStartGroup:
  230. for {
  231. u, err = o.DecodeVarint()
  232. if err != nil {
  233. break
  234. }
  235. fwire := int(u & 0x7)
  236. if fwire == WireEndGroup {
  237. break
  238. }
  239. ftag := int(u >> 3)
  240. err = o.skip(t, ftag, fwire)
  241. if err != nil {
  242. break
  243. }
  244. }
  245. default:
  246. err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
  247. }
  248. return err
  249. }
  250. // Unmarshaler is the interface representing objects that can
  251. // unmarshal themselves. The method should reset the receiver before
  252. // decoding starts. The argument points to data that may be
  253. // overwritten, so implementations should not keep references to the
  254. // buffer.
  255. type Unmarshaler interface {
  256. Unmarshal([]byte) error
  257. }
  258. // Unmarshal parses the protocol buffer representation in buf and places the
  259. // decoded result in pb. If the struct underlying pb does not match
  260. // the data in buf, the results can be unpredictable.
  261. //
  262. // Unmarshal resets pb before starting to unmarshal, so any
  263. // existing data in pb is always removed. Use UnmarshalMerge
  264. // to preserve and append to existing data.
  265. func Unmarshal(buf []byte, pb Message) error {
  266. pb.Reset()
  267. return UnmarshalMerge(buf, pb)
  268. }
  269. // UnmarshalMerge parses the protocol buffer representation in buf and
  270. // writes the decoded result to pb. If the struct underlying pb does not match
  271. // the data in buf, the results can be unpredictable.
  272. //
  273. // UnmarshalMerge merges into existing data in pb.
  274. // Most code should use Unmarshal instead.
  275. func UnmarshalMerge(buf []byte, pb Message) error {
  276. // If the object can unmarshal itself, let it.
  277. if u, ok := pb.(Unmarshaler); ok {
  278. return u.Unmarshal(buf)
  279. }
  280. return NewBuffer(buf).Unmarshal(pb)
  281. }
  282. // DecodeMessage reads a count-delimited message from the Buffer.
  283. func (p *Buffer) DecodeMessage(pb Message) error {
  284. enc, err := p.DecodeRawBytes(false)
  285. if err != nil {
  286. return err
  287. }
  288. return NewBuffer(enc).Unmarshal(pb)
  289. }
  290. // DecodeGroup reads a tag-delimited group from the Buffer.
  291. func (p *Buffer) DecodeGroup(pb Message) error {
  292. typ, base, err := getbase(pb)
  293. if err != nil {
  294. return err
  295. }
  296. return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), true, base)
  297. }
  298. // Unmarshal parses the protocol buffer representation in the
  299. // Buffer and places the decoded result in pb. If the struct
  300. // underlying pb does not match the data in the buffer, the results can be
  301. // unpredictable.
  302. func (p *Buffer) Unmarshal(pb Message) error {
  303. // If the object can unmarshal itself, let it.
  304. if u, ok := pb.(Unmarshaler); ok {
  305. err := u.Unmarshal(p.buf[p.index:])
  306. p.index = len(p.buf)
  307. return err
  308. }
  309. typ, base, err := getbase(pb)
  310. if err != nil {
  311. return err
  312. }
  313. err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
  314. if collectStats {
  315. stats.Decode++
  316. }
  317. return err
  318. }
  319. // unmarshalType does the work of unmarshaling a structure.
  320. func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
  321. var state errorState
  322. required, reqFields := prop.reqCount, uint64(0)
  323. var err error
  324. for err == nil && o.index < len(o.buf) {
  325. oi := o.index
  326. var u uint64
  327. u, err = o.DecodeVarint()
  328. if err != nil {
  329. break
  330. }
  331. wire := int(u & 0x7)
  332. if wire == WireEndGroup {
  333. if is_group {
  334. return nil // input is satisfied
  335. }
  336. return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
  337. }
  338. tag := int(u >> 3)
  339. if tag <= 0 {
  340. return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
  341. }
  342. fieldnum, ok := prop.decoderTags.get(tag)
  343. if !ok {
  344. // Maybe it's an extension?
  345. if prop.extendable {
  346. if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
  347. if err = o.skip(st, tag, wire); err == nil {
  348. ext := e.ExtensionMap()[int32(tag)] // may be missing
  349. ext.enc = append(ext.enc, o.buf[oi:o.index]...)
  350. e.ExtensionMap()[int32(tag)] = ext
  351. }
  352. continue
  353. }
  354. }
  355. // Maybe it's a oneof?
  356. if prop.oneofUnmarshaler != nil {
  357. m := structPointer_Interface(base, st).(Message)
  358. // First return value indicates whether tag is a oneof field.
  359. ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
  360. if err == ErrInternalBadWireType {
  361. // Map the error to something more descriptive.
  362. // Do the formatting here to save generated code space.
  363. err = fmt.Errorf("bad wiretype for oneof field in %T", m)
  364. }
  365. if ok {
  366. continue
  367. }
  368. }
  369. err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
  370. continue
  371. }
  372. p := prop.Prop[fieldnum]
  373. if p.dec == nil {
  374. fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
  375. continue
  376. }
  377. dec := p.dec
  378. if wire != WireStartGroup && wire != p.WireType {
  379. if wire == WireBytes && p.packedDec != nil {
  380. // a packable field
  381. dec = p.packedDec
  382. } else {
  383. err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
  384. continue
  385. }
  386. }
  387. decErr := dec(o, p, base)
  388. if decErr != nil && !state.shouldContinue(decErr, p) {
  389. err = decErr
  390. }
  391. if err == nil && p.Required {
  392. // Successfully decoded a required field.
  393. if tag <= 64 {
  394. // use bitmap for fields 1-64 to catch field reuse.
  395. var mask uint64 = 1 << uint64(tag-1)
  396. if reqFields&mask == 0 {
  397. // new required field
  398. reqFields |= mask
  399. required--
  400. }
  401. } else {
  402. // This is imprecise. It can be fooled by a required field
  403. // with a tag > 64 that is encoded twice; that's very rare.
  404. // A fully correct implementation would require allocating
  405. // a data structure, which we would like to avoid.
  406. required--
  407. }
  408. }
  409. }
  410. if err == nil {
  411. if is_group {
  412. return io.ErrUnexpectedEOF
  413. }
  414. if state.err != nil {
  415. return state.err
  416. }
  417. if required > 0 {
  418. // Not enough information to determine the exact field. If we use extra
  419. // CPU, we could determine the field only if the missing required field
  420. // has a tag <= 64 and we check reqFields.
  421. return &RequiredNotSetError{"{Unknown}"}
  422. }
  423. }
  424. return err
  425. }
  426. // Individual type decoders
  427. // For each,
  428. // u is the decoded value,
  429. // v is a pointer to the field (pointer) in the struct
  430. // Sizes of the pools to allocate inside the Buffer.
  431. // The goal is modest amortization and allocation
  432. // on at least 16-byte boundaries.
  433. const (
  434. boolPoolSize = 16
  435. uint32PoolSize = 8
  436. uint64PoolSize = 4
  437. )
  438. // Decode a bool.
  439. func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
  440. u, err := p.valDec(o)
  441. if err != nil {
  442. return err
  443. }
  444. if len(o.bools) == 0 {
  445. o.bools = make([]bool, boolPoolSize)
  446. }
  447. o.bools[0] = u != 0
  448. *structPointer_Bool(base, p.field) = &o.bools[0]
  449. o.bools = o.bools[1:]
  450. return nil
  451. }
  452. func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
  453. u, err := p.valDec(o)
  454. if err != nil {
  455. return err
  456. }
  457. *structPointer_BoolVal(base, p.field) = u != 0
  458. return nil
  459. }
  460. // Decode an int32.
  461. func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
  462. u, err := p.valDec(o)
  463. if err != nil {
  464. return err
  465. }
  466. word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
  467. return nil
  468. }
  469. func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
  470. u, err := p.valDec(o)
  471. if err != nil {
  472. return err
  473. }
  474. word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
  475. return nil
  476. }
  477. // Decode an int64.
  478. func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
  479. u, err := p.valDec(o)
  480. if err != nil {
  481. return err
  482. }
  483. word64_Set(structPointer_Word64(base, p.field), o, u)
  484. return nil
  485. }
  486. func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
  487. u, err := p.valDec(o)
  488. if err != nil {
  489. return err
  490. }
  491. word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
  492. return nil
  493. }
  494. // Decode a string.
  495. func (o *Buffer) dec_string(p *Properties, base structPointer) error {
  496. s, err := o.DecodeStringBytes()
  497. if err != nil {
  498. return err
  499. }
  500. *structPointer_String(base, p.field) = &s
  501. return nil
  502. }
  503. func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
  504. s, err := o.DecodeStringBytes()
  505. if err != nil {
  506. return err
  507. }
  508. *structPointer_StringVal(base, p.field) = s
  509. return nil
  510. }
  511. // Decode a slice of bytes ([]byte).
  512. func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
  513. b, err := o.DecodeRawBytes(true)
  514. if err != nil {
  515. return err
  516. }
  517. *structPointer_Bytes(base, p.field) = b
  518. return nil
  519. }
  520. // Decode a slice of bools ([]bool).
  521. func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
  522. u, err := p.valDec(o)
  523. if err != nil {
  524. return err
  525. }
  526. v := structPointer_BoolSlice(base, p.field)
  527. *v = append(*v, u != 0)
  528. return nil
  529. }
  530. // Decode a slice of bools ([]bool) in packed format.
  531. func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
  532. v := structPointer_BoolSlice(base, p.field)
  533. nn, err := o.DecodeVarint()
  534. if err != nil {
  535. return err
  536. }
  537. nb := int(nn) // number of bytes of encoded bools
  538. fin := o.index + nb
  539. if fin < o.index {
  540. return errOverflow
  541. }
  542. y := *v
  543. for o.index < fin {
  544. u, err := p.valDec(o)
  545. if err != nil {
  546. return err
  547. }
  548. y = append(y, u != 0)
  549. }
  550. *v = y
  551. return nil
  552. }
  553. // Decode a slice of int32s ([]int32).
  554. func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
  555. u, err := p.valDec(o)
  556. if err != nil {
  557. return err
  558. }
  559. structPointer_Word32Slice(base, p.field).Append(uint32(u))
  560. return nil
  561. }
  562. // Decode a slice of int32s ([]int32) in packed format.
  563. func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
  564. v := structPointer_Word32Slice(base, p.field)
  565. nn, err := o.DecodeVarint()
  566. if err != nil {
  567. return err
  568. }
  569. nb := int(nn) // number of bytes of encoded int32s
  570. fin := o.index + nb
  571. if fin < o.index {
  572. return errOverflow
  573. }
  574. for o.index < fin {
  575. u, err := p.valDec(o)
  576. if err != nil {
  577. return err
  578. }
  579. v.Append(uint32(u))
  580. }
  581. return nil
  582. }
  583. // Decode a slice of int64s ([]int64).
  584. func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
  585. u, err := p.valDec(o)
  586. if err != nil {
  587. return err
  588. }
  589. structPointer_Word64Slice(base, p.field).Append(u)
  590. return nil
  591. }
  592. // Decode a slice of int64s ([]int64) in packed format.
  593. func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
  594. v := structPointer_Word64Slice(base, p.field)
  595. nn, err := o.DecodeVarint()
  596. if err != nil {
  597. return err
  598. }
  599. nb := int(nn) // number of bytes of encoded int64s
  600. fin := o.index + nb
  601. if fin < o.index {
  602. return errOverflow
  603. }
  604. for o.index < fin {
  605. u, err := p.valDec(o)
  606. if err != nil {
  607. return err
  608. }
  609. v.Append(u)
  610. }
  611. return nil
  612. }
  613. // Decode a slice of strings ([]string).
  614. func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
  615. s, err := o.DecodeStringBytes()
  616. if err != nil {
  617. return err
  618. }
  619. v := structPointer_StringSlice(base, p.field)
  620. *v = append(*v, s)
  621. return nil
  622. }
  623. // Decode a slice of slice of bytes ([][]byte).
  624. func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
  625. b, err := o.DecodeRawBytes(true)
  626. if err != nil {
  627. return err
  628. }
  629. v := structPointer_BytesSlice(base, p.field)
  630. *v = append(*v, b)
  631. return nil
  632. }
  633. // Decode a map field.
  634. func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
  635. raw, err := o.DecodeRawBytes(false)
  636. if err != nil {
  637. return err
  638. }
  639. oi := o.index // index at the end of this map entry
  640. o.index -= len(raw) // move buffer back to start of map entry
  641. mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
  642. if mptr.Elem().IsNil() {
  643. mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
  644. }
  645. v := mptr.Elem() // map[K]V
  646. // Prepare addressable doubly-indirect placeholders for the key and value types.
  647. // See enc_new_map for why.
  648. keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
  649. keybase := toStructPointer(keyptr.Addr()) // **K
  650. var valbase structPointer
  651. var valptr reflect.Value
  652. switch p.mtype.Elem().Kind() {
  653. case reflect.Slice:
  654. // []byte
  655. var dummy []byte
  656. valptr = reflect.ValueOf(&dummy) // *[]byte
  657. valbase = toStructPointer(valptr) // *[]byte
  658. case reflect.Ptr:
  659. // message; valptr is **Msg; need to allocate the intermediate pointer
  660. valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
  661. valptr.Set(reflect.New(valptr.Type().Elem()))
  662. valbase = toStructPointer(valptr)
  663. default:
  664. // everything else
  665. valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
  666. valbase = toStructPointer(valptr.Addr()) // **V
  667. }
  668. // Decode.
  669. // This parses a restricted wire format, namely the encoding of a message
  670. // with two fields. See enc_new_map for the format.
  671. for o.index < oi {
  672. // tagcode for key and value properties are always a single byte
  673. // because they have tags 1 and 2.
  674. tagcode := o.buf[o.index]
  675. o.index++
  676. switch tagcode {
  677. case p.mkeyprop.tagcode[0]:
  678. if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
  679. return err
  680. }
  681. case p.mvalprop.tagcode[0]:
  682. if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
  683. return err
  684. }
  685. default:
  686. // TODO: Should we silently skip this instead?
  687. return fmt.Errorf("proto: bad map data tag %d", raw[0])
  688. }
  689. }
  690. keyelem, valelem := keyptr.Elem(), valptr.Elem()
  691. if !keyelem.IsValid() {
  692. keyelem = reflect.Zero(p.mtype.Key())
  693. }
  694. if !valelem.IsValid() {
  695. valelem = reflect.Zero(p.mtype.Elem())
  696. }
  697. v.SetMapIndex(keyelem, valelem)
  698. return nil
  699. }
  700. // Decode a group.
  701. func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
  702. bas := structPointer_GetStructPointer(base, p.field)
  703. if structPointer_IsNil(bas) {
  704. // allocate new nested message
  705. bas = toStructPointer(reflect.New(p.stype))
  706. structPointer_SetStructPointer(base, p.field, bas)
  707. }
  708. return o.unmarshalType(p.stype, p.sprop, true, bas)
  709. }
  710. // Decode an embedded message.
  711. func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
  712. raw, e := o.DecodeRawBytes(false)
  713. if e != nil {
  714. return e
  715. }
  716. bas := structPointer_GetStructPointer(base, p.field)
  717. if structPointer_IsNil(bas) {
  718. // allocate new nested message
  719. bas = toStructPointer(reflect.New(p.stype))
  720. structPointer_SetStructPointer(base, p.field, bas)
  721. }
  722. // If the object can unmarshal itself, let it.
  723. if p.isUnmarshaler {
  724. iv := structPointer_Interface(bas, p.stype)
  725. return iv.(Unmarshaler).Unmarshal(raw)
  726. }
  727. obuf := o.buf
  728. oi := o.index
  729. o.buf = raw
  730. o.index = 0
  731. err = o.unmarshalType(p.stype, p.sprop, false, bas)
  732. o.buf = obuf
  733. o.index = oi
  734. return err
  735. }
  736. // Decode a slice of embedded messages.
  737. func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
  738. return o.dec_slice_struct(p, false, base)
  739. }
  740. // Decode a slice of embedded groups.
  741. func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
  742. return o.dec_slice_struct(p, true, base)
  743. }
  744. // Decode a slice of structs ([]*struct).
  745. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
  746. v := reflect.New(p.stype)
  747. bas := toStructPointer(v)
  748. structPointer_StructPointerSlice(base, p.field).Append(bas)
  749. if is_group {
  750. err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
  751. return err
  752. }
  753. raw, err := o.DecodeRawBytes(false)
  754. if err != nil {
  755. return err
  756. }
  757. // If the object can unmarshal itself, let it.
  758. if p.isUnmarshaler {
  759. iv := v.Interface()
  760. return iv.(Unmarshaler).Unmarshal(raw)
  761. }
  762. obuf := o.buf
  763. oi := o.index
  764. o.buf = raw
  765. o.index = 0
  766. err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
  767. o.buf = obuf
  768. o.index = oi
  769. return err
  770. }