decode.go 24 KB

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