decode.go 24 KB

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