decode.go 23 KB

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