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, _ := extendable(structPointer_Interface(base, st)); isExtensionField(e, int32(tag)) {
  347. if err = o.skip(st, tag, wire); err == nil {
  348. extmap := e.extensionsWrite()
  349. ext := extmap[int32(tag)] // may be missing
  350. ext.enc = append(ext.enc, o.buf[oi:o.index]...)
  351. extmap[int32(tag)] = ext
  352. }
  353. continue
  354. }
  355. }
  356. // Maybe it's a oneof?
  357. if prop.oneofUnmarshaler != nil {
  358. m := structPointer_Interface(base, st).(Message)
  359. // First return value indicates whether tag is a oneof field.
  360. ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
  361. if err == ErrInternalBadWireType {
  362. // Map the error to something more descriptive.
  363. // Do the formatting here to save generated code space.
  364. err = fmt.Errorf("bad wiretype for oneof field in %T", m)
  365. }
  366. if ok {
  367. continue
  368. }
  369. }
  370. err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
  371. continue
  372. }
  373. p := prop.Prop[fieldnum]
  374. if p.dec == nil {
  375. fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
  376. continue
  377. }
  378. dec := p.dec
  379. if wire != WireStartGroup && wire != p.WireType {
  380. if wire == WireBytes && p.packedDec != nil {
  381. // a packable field
  382. dec = p.packedDec
  383. } else {
  384. err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
  385. continue
  386. }
  387. }
  388. decErr := dec(o, p, base)
  389. if decErr != nil && !state.shouldContinue(decErr, p) {
  390. err = decErr
  391. }
  392. if err == nil && p.Required {
  393. // Successfully decoded a required field.
  394. if tag <= 64 {
  395. // use bitmap for fields 1-64 to catch field reuse.
  396. var mask uint64 = 1 << uint64(tag-1)
  397. if reqFields&mask == 0 {
  398. // new required field
  399. reqFields |= mask
  400. required--
  401. }
  402. } else {
  403. // This is imprecise. It can be fooled by a required field
  404. // with a tag > 64 that is encoded twice; that's very rare.
  405. // A fully correct implementation would require allocating
  406. // a data structure, which we would like to avoid.
  407. required--
  408. }
  409. }
  410. }
  411. if err == nil {
  412. if is_group {
  413. return io.ErrUnexpectedEOF
  414. }
  415. if state.err != nil {
  416. return state.err
  417. }
  418. if required > 0 {
  419. // Not enough information to determine the exact field. If we use extra
  420. // CPU, we could determine the field only if the missing required field
  421. // has a tag <= 64 and we check reqFields.
  422. return &RequiredNotSetError{"{Unknown}"}
  423. }
  424. }
  425. return err
  426. }
  427. // Individual type decoders
  428. // For each,
  429. // u is the decoded value,
  430. // v is a pointer to the field (pointer) in the struct
  431. // Sizes of the pools to allocate inside the Buffer.
  432. // The goal is modest amortization and allocation
  433. // on at least 16-byte boundaries.
  434. const (
  435. boolPoolSize = 16
  436. uint32PoolSize = 8
  437. uint64PoolSize = 4
  438. )
  439. // Decode a bool.
  440. func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
  441. u, err := p.valDec(o)
  442. if err != nil {
  443. return err
  444. }
  445. if len(o.bools) == 0 {
  446. o.bools = make([]bool, boolPoolSize)
  447. }
  448. o.bools[0] = u != 0
  449. *structPointer_Bool(base, p.field) = &o.bools[0]
  450. o.bools = o.bools[1:]
  451. return nil
  452. }
  453. func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
  454. u, err := p.valDec(o)
  455. if err != nil {
  456. return err
  457. }
  458. *structPointer_BoolVal(base, p.field) = u != 0
  459. return nil
  460. }
  461. // Decode an int32.
  462. func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
  463. u, err := p.valDec(o)
  464. if err != nil {
  465. return err
  466. }
  467. word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
  468. return nil
  469. }
  470. func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
  471. u, err := p.valDec(o)
  472. if err != nil {
  473. return err
  474. }
  475. word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
  476. return nil
  477. }
  478. // Decode an int64.
  479. func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
  480. u, err := p.valDec(o)
  481. if err != nil {
  482. return err
  483. }
  484. word64_Set(structPointer_Word64(base, p.field), o, u)
  485. return nil
  486. }
  487. func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
  488. u, err := p.valDec(o)
  489. if err != nil {
  490. return err
  491. }
  492. word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
  493. return nil
  494. }
  495. // Decode a string.
  496. func (o *Buffer) dec_string(p *Properties, base structPointer) error {
  497. s, err := o.DecodeStringBytes()
  498. if err != nil {
  499. return err
  500. }
  501. *structPointer_String(base, p.field) = &s
  502. return nil
  503. }
  504. func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
  505. s, err := o.DecodeStringBytes()
  506. if err != nil {
  507. return err
  508. }
  509. *structPointer_StringVal(base, p.field) = s
  510. return nil
  511. }
  512. // Decode a slice of bytes ([]byte).
  513. func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
  514. b, err := o.DecodeRawBytes(true)
  515. if err != nil {
  516. return err
  517. }
  518. *structPointer_Bytes(base, p.field) = b
  519. return nil
  520. }
  521. // Decode a slice of bools ([]bool).
  522. func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
  523. u, err := p.valDec(o)
  524. if err != nil {
  525. return err
  526. }
  527. v := structPointer_BoolSlice(base, p.field)
  528. *v = append(*v, u != 0)
  529. return nil
  530. }
  531. // Decode a slice of bools ([]bool) in packed format.
  532. func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
  533. v := structPointer_BoolSlice(base, p.field)
  534. nn, err := o.DecodeVarint()
  535. if err != nil {
  536. return err
  537. }
  538. nb := int(nn) // number of bytes of encoded bools
  539. fin := o.index + nb
  540. if fin < o.index {
  541. return errOverflow
  542. }
  543. y := *v
  544. for o.index < fin {
  545. u, err := p.valDec(o)
  546. if err != nil {
  547. return err
  548. }
  549. y = append(y, u != 0)
  550. }
  551. *v = y
  552. return nil
  553. }
  554. // Decode a slice of int32s ([]int32).
  555. func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
  556. u, err := p.valDec(o)
  557. if err != nil {
  558. return err
  559. }
  560. structPointer_Word32Slice(base, p.field).Append(uint32(u))
  561. return nil
  562. }
  563. // Decode a slice of int32s ([]int32) in packed format.
  564. func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
  565. v := structPointer_Word32Slice(base, p.field)
  566. nn, err := o.DecodeVarint()
  567. if err != nil {
  568. return err
  569. }
  570. nb := int(nn) // number of bytes of encoded int32s
  571. fin := o.index + nb
  572. if fin < o.index {
  573. return errOverflow
  574. }
  575. for o.index < fin {
  576. u, err := p.valDec(o)
  577. if err != nil {
  578. return err
  579. }
  580. v.Append(uint32(u))
  581. }
  582. return nil
  583. }
  584. // Decode a slice of int64s ([]int64).
  585. func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
  586. u, err := p.valDec(o)
  587. if err != nil {
  588. return err
  589. }
  590. structPointer_Word64Slice(base, p.field).Append(u)
  591. return nil
  592. }
  593. // Decode a slice of int64s ([]int64) in packed format.
  594. func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
  595. v := structPointer_Word64Slice(base, p.field)
  596. nn, err := o.DecodeVarint()
  597. if err != nil {
  598. return err
  599. }
  600. nb := int(nn) // number of bytes of encoded int64s
  601. fin := o.index + nb
  602. if fin < o.index {
  603. return errOverflow
  604. }
  605. for o.index < fin {
  606. u, err := p.valDec(o)
  607. if err != nil {
  608. return err
  609. }
  610. v.Append(u)
  611. }
  612. return nil
  613. }
  614. // Decode a slice of strings ([]string).
  615. func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
  616. s, err := o.DecodeStringBytes()
  617. if err != nil {
  618. return err
  619. }
  620. v := structPointer_StringSlice(base, p.field)
  621. *v = append(*v, s)
  622. return nil
  623. }
  624. // Decode a slice of slice of bytes ([][]byte).
  625. func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
  626. b, err := o.DecodeRawBytes(true)
  627. if err != nil {
  628. return err
  629. }
  630. v := structPointer_BytesSlice(base, p.field)
  631. *v = append(*v, b)
  632. return nil
  633. }
  634. // Decode a map field.
  635. func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
  636. raw, err := o.DecodeRawBytes(false)
  637. if err != nil {
  638. return err
  639. }
  640. oi := o.index // index at the end of this map entry
  641. o.index -= len(raw) // move buffer back to start of map entry
  642. mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
  643. if mptr.Elem().IsNil() {
  644. mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
  645. }
  646. v := mptr.Elem() // map[K]V
  647. // Prepare addressable doubly-indirect placeholders for the key and value types.
  648. // See enc_new_map for why.
  649. keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
  650. keybase := toStructPointer(keyptr.Addr()) // **K
  651. var valbase structPointer
  652. var valptr reflect.Value
  653. switch p.mtype.Elem().Kind() {
  654. case reflect.Slice:
  655. // []byte
  656. var dummy []byte
  657. valptr = reflect.ValueOf(&dummy) // *[]byte
  658. valbase = toStructPointer(valptr) // *[]byte
  659. case reflect.Ptr:
  660. // message; valptr is **Msg; need to allocate the intermediate pointer
  661. valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
  662. valptr.Set(reflect.New(valptr.Type().Elem()))
  663. valbase = toStructPointer(valptr)
  664. default:
  665. // everything else
  666. valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
  667. valbase = toStructPointer(valptr.Addr()) // **V
  668. }
  669. // Decode.
  670. // This parses a restricted wire format, namely the encoding of a message
  671. // with two fields. See enc_new_map for the format.
  672. for o.index < oi {
  673. // tagcode for key and value properties are always a single byte
  674. // because they have tags 1 and 2.
  675. tagcode := o.buf[o.index]
  676. o.index++
  677. switch tagcode {
  678. case p.mkeyprop.tagcode[0]:
  679. if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
  680. return err
  681. }
  682. case p.mvalprop.tagcode[0]:
  683. if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
  684. return err
  685. }
  686. default:
  687. // TODO: Should we silently skip this instead?
  688. return fmt.Errorf("proto: bad map data tag %d", raw[0])
  689. }
  690. }
  691. keyelem, valelem := keyptr.Elem(), valptr.Elem()
  692. if !keyelem.IsValid() {
  693. keyelem = reflect.Zero(p.mtype.Key())
  694. }
  695. if !valelem.IsValid() {
  696. valelem = reflect.Zero(p.mtype.Elem())
  697. }
  698. v.SetMapIndex(keyelem, valelem)
  699. return nil
  700. }
  701. // Decode a group.
  702. func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
  703. bas := structPointer_GetStructPointer(base, p.field)
  704. if structPointer_IsNil(bas) {
  705. // allocate new nested message
  706. bas = toStructPointer(reflect.New(p.stype))
  707. structPointer_SetStructPointer(base, p.field, bas)
  708. }
  709. return o.unmarshalType(p.stype, p.sprop, true, bas)
  710. }
  711. // Decode an embedded message.
  712. func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
  713. raw, e := o.DecodeRawBytes(false)
  714. if e != nil {
  715. return e
  716. }
  717. bas := structPointer_GetStructPointer(base, p.field)
  718. if structPointer_IsNil(bas) {
  719. // allocate new nested message
  720. bas = toStructPointer(reflect.New(p.stype))
  721. structPointer_SetStructPointer(base, p.field, bas)
  722. }
  723. // If the object can unmarshal itself, let it.
  724. if p.isUnmarshaler {
  725. iv := structPointer_Interface(bas, p.stype)
  726. return iv.(Unmarshaler).Unmarshal(raw)
  727. }
  728. obuf := o.buf
  729. oi := o.index
  730. o.buf = raw
  731. o.index = 0
  732. err = o.unmarshalType(p.stype, p.sprop, false, bas)
  733. o.buf = obuf
  734. o.index = oi
  735. return err
  736. }
  737. // Decode a slice of embedded messages.
  738. func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
  739. return o.dec_slice_struct(p, false, base)
  740. }
  741. // Decode a slice of embedded groups.
  742. func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
  743. return o.dec_slice_struct(p, true, base)
  744. }
  745. // Decode a slice of structs ([]*struct).
  746. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
  747. v := reflect.New(p.stype)
  748. bas := toStructPointer(v)
  749. structPointer_StructPointerSlice(base, p.field).Append(bas)
  750. if is_group {
  751. err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
  752. return err
  753. }
  754. raw, err := o.DecodeRawBytes(false)
  755. if err != nil {
  756. return err
  757. }
  758. // If the object can unmarshal itself, let it.
  759. if p.isUnmarshaler {
  760. iv := v.Interface()
  761. return iv.(Unmarshaler).Unmarshal(raw)
  762. }
  763. obuf := o.buf
  764. oi := o.index
  765. o.buf = raw
  766. o.index = 0
  767. err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
  768. o.buf = obuf
  769. o.index = oi
  770. return err
  771. }