decode.go 23 KB

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