decode.go 23 KB

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