PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/proto/decode.go

https://code.google.com/p/goprotobuf/
Go | 726 lines | 505 code | 94 blank | 127 comment | 125 complexity | e77f8b078587b5db0088f42d6ce320fb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 Google Inc. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. "unsafe"
  42. )
  43. // ErrWrongType occurs when the wire encoding for the field disagrees with
  44. // that specified in the type being decoded. This is usually caused by attempting
  45. // to convert an encoded protocol buffer into a struct of the wrong type.
  46. var ErrWrongType = errors.New("field/encoding mismatch: wrong type for field")
  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 += 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. break
  67. }
  68. }
  69. return x, n
  70. }
  71. // DecodeVarint reads a varint-encoded integer from the Buffer.
  72. // This is the format for the
  73. // int32, int64, uint32, uint64, bool, and enum
  74. // protocol buffer types.
  75. func (p *Buffer) DecodeVarint() (x uint64, err error) {
  76. // x, err already 0
  77. i := p.index
  78. l := len(p.buf)
  79. for shift := uint(0); ; shift += 7 {
  80. if i >= l {
  81. err = io.ErrUnexpectedEOF
  82. return
  83. }
  84. b := p.buf[i]
  85. i++
  86. x |= (uint64(b) & 0x7F) << shift
  87. if b < 0x80 {
  88. break
  89. }
  90. }
  91. p.index = i
  92. return
  93. }
  94. // DecodeFixed64 reads a 64-bit integer from the Buffer.
  95. // This is the format for the
  96. // fixed64, sfixed64, and double protocol buffer types.
  97. func (p *Buffer) DecodeFixed64() (x uint64, err error) {
  98. // x, err already 0
  99. i := p.index + 8
  100. if i > len(p.buf) {
  101. err = io.ErrUnexpectedEOF
  102. return
  103. }
  104. p.index = i
  105. x = uint64(p.buf[i-8])
  106. x |= uint64(p.buf[i-7]) << 8
  107. x |= uint64(p.buf[i-6]) << 16
  108. x |= uint64(p.buf[i-5]) << 24
  109. x |= uint64(p.buf[i-4]) << 32
  110. x |= uint64(p.buf[i-3]) << 40
  111. x |= uint64(p.buf[i-2]) << 48
  112. x |= uint64(p.buf[i-1]) << 56
  113. return
  114. }
  115. // DecodeFixed32 reads a 32-bit integer from the Buffer.
  116. // This is the format for the
  117. // fixed32, sfixed32, and float protocol buffer types.
  118. func (p *Buffer) DecodeFixed32() (x uint64, err error) {
  119. // x, err already 0
  120. i := p.index + 4
  121. if i > len(p.buf) {
  122. err = io.ErrUnexpectedEOF
  123. return
  124. }
  125. p.index = i
  126. x = uint64(p.buf[i-4])
  127. x |= uint64(p.buf[i-3]) << 8
  128. x |= uint64(p.buf[i-2]) << 16
  129. x |= uint64(p.buf[i-1]) << 24
  130. return
  131. }
  132. // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
  133. // from the Buffer.
  134. // This is the format used for the sint64 protocol buffer type.
  135. func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
  136. x, err = p.DecodeVarint()
  137. if err != nil {
  138. return
  139. }
  140. x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
  141. return
  142. }
  143. // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
  144. // from the Buffer.
  145. // This is the format used for the sint32 protocol buffer type.
  146. func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
  147. x, err = p.DecodeVarint()
  148. if err != nil {
  149. return
  150. }
  151. x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
  152. return
  153. }
  154. // These are not ValueDecoders: they produce an array of bytes or a string.
  155. // bytes, embedded messages
  156. // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
  157. // This is the format used for the bytes protocol buffer
  158. // type and for embedded messages.
  159. func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
  160. n, err := p.DecodeVarint()
  161. if err != nil {
  162. return
  163. }
  164. nb := int(n)
  165. if nb < 0 {
  166. return nil, fmt.Errorf("proto: bad byte length %d", nb)
  167. }
  168. if p.index+nb > len(p.buf) {
  169. return nil, io.ErrUnexpectedEOF
  170. }
  171. if !alloc {
  172. // todo: check if can get more uses of alloc=false
  173. buf = p.buf[p.index : p.index+nb]
  174. p.index += nb
  175. return
  176. }
  177. buf = make([]byte, nb)
  178. copy(buf, p.buf[p.index:])
  179. p.index += nb
  180. return
  181. }
  182. // DecodeStringBytes reads an encoded string from the Buffer.
  183. // This is the format used for the proto2 string type.
  184. func (p *Buffer) DecodeStringBytes() (s string, err error) {
  185. buf, err := p.DecodeRawBytes(false)
  186. if err != nil {
  187. return
  188. }
  189. return string(buf), nil
  190. }
  191. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  192. // If the protocol buffer has extensions, and the field matches, add it as an extension.
  193. // Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
  194. func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base uintptr) error {
  195. oi := o.index
  196. err := o.skip(t, tag, wire)
  197. if err != nil {
  198. return err
  199. }
  200. x := fieldIndex(t, "XXX_unrecognized")
  201. if x == nil {
  202. return nil
  203. }
  204. p := propByIndex(t, x)
  205. ptr := (*[]byte)(unsafe.Pointer(base + p.offset))
  206. if *ptr == nil {
  207. // This is the first skipped element,
  208. // allocate a new buffer.
  209. *ptr = o.bufalloc()
  210. }
  211. // Add the skipped field to struct field
  212. obuf := o.buf
  213. o.buf = *ptr
  214. o.EncodeVarint(uint64(tag<<3 | wire))
  215. *ptr = append(o.buf, obuf[oi:o.index]...)
  216. o.buf = obuf
  217. return nil
  218. }
  219. // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
  220. func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
  221. var u uint64
  222. var err error
  223. switch wire {
  224. case WireVarint:
  225. _, err = o.DecodeVarint()
  226. case WireFixed64:
  227. _, err = o.DecodeFixed64()
  228. case WireBytes:
  229. _, err = o.DecodeRawBytes(false)
  230. case WireFixed32:
  231. _, err = o.DecodeFixed32()
  232. case WireStartGroup:
  233. for {
  234. u, err = o.DecodeVarint()
  235. if err != nil {
  236. break
  237. }
  238. fwire := int(u & 0x7)
  239. if fwire == WireEndGroup {
  240. break
  241. }
  242. ftag := int(u >> 3)
  243. err = o.skip(t, ftag, fwire)
  244. if err != nil {
  245. break
  246. }
  247. }
  248. default:
  249. err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
  250. }
  251. return err
  252. }
  253. // Unmarshaler is the interface representing objects that can unmarshal themselves.
  254. type Unmarshaler interface {
  255. Unmarshal([]byte) error
  256. }
  257. // Unmarshal parses the protocol buffer representation in buf and places the
  258. // decoded result in pb. If the struct underlying pb does not match
  259. // the data in buf, the results can be unpredictable.
  260. func Unmarshal(buf []byte, pb interface{}) error {
  261. // If the object can unmarshal itself, let it.
  262. if u, ok := pb.(Unmarshaler); ok {
  263. return u.Unmarshal(buf)
  264. }
  265. return NewBuffer(buf).Unmarshal(pb)
  266. }
  267. // Unmarshal parses the protocol buffer representation in the
  268. // Buffer and places the decoded result in pb. If the struct
  269. // underlying pb does not match the data in the buffer, the results can be
  270. // unpredictable.
  271. func (p *Buffer) Unmarshal(pb interface{}) error {
  272. // If the object can unmarshal itself, let it.
  273. if u, ok := pb.(Unmarshaler); ok {
  274. err := u.Unmarshal(p.buf[p.index:])
  275. p.index = len(p.buf)
  276. return err
  277. }
  278. typ, base, err := getbase(pb)
  279. if err != nil {
  280. return err
  281. }
  282. err = p.unmarshalType(typ, false, base)
  283. stats.Decode++
  284. return err
  285. }
  286. // unmarshalType does the work of unmarshaling a structure.
  287. func (o *Buffer) unmarshalType(t reflect.Type, is_group bool, base uintptr) error {
  288. st := t.Elem()
  289. prop := GetProperties(st)
  290. required, reqFields := prop.reqCount, uint64(0)
  291. var err error
  292. for err == nil && o.index < len(o.buf) {
  293. oi := o.index
  294. var u uint64
  295. u, err = o.DecodeVarint()
  296. if err != nil {
  297. break
  298. }
  299. wire := int(u & 0x7)
  300. if wire == WireEndGroup {
  301. if is_group {
  302. return nil // input is satisfied
  303. }
  304. return ErrWrongType
  305. }
  306. tag := int(u >> 3)
  307. if tag <= 0 {
  308. return fmt.Errorf("proto: illegal tag %d", tag)
  309. }
  310. fieldnum, ok := prop.tags[tag]
  311. if !ok {
  312. // Maybe it's an extension?
  313. iv := reflect.NewAt(st, unsafe.Pointer(base)).Interface()
  314. if e, ok := iv.(extendableProto); ok && isExtensionField(e, int32(tag)) {
  315. if err = o.skip(st, tag, wire); err == nil {
  316. e.ExtensionMap()[int32(tag)] = Extension{enc: append([]byte(nil), o.buf[oi:o.index]...)}
  317. }
  318. continue
  319. }
  320. err = o.skipAndSave(st, tag, wire, base)
  321. continue
  322. }
  323. p := prop.Prop[fieldnum]
  324. if p.dec == nil {
  325. fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", t, st.Field(fieldnum).Name)
  326. continue
  327. }
  328. dec := p.dec
  329. if wire != WireStartGroup && wire != p.WireType {
  330. if wire == WireBytes && p.packedDec != nil {
  331. // a packable field
  332. dec = p.packedDec
  333. } else {
  334. err = ErrWrongType
  335. continue
  336. }
  337. }
  338. err = dec(o, p, base)
  339. if err == nil && p.Required {
  340. // Successfully decoded a required field.
  341. if tag <= 64 {
  342. // use bitmap for fields 1-64 to catch field reuse.
  343. var mask uint64 = 1 << uint64(tag-1)
  344. if reqFields&mask == 0 {
  345. // new required field
  346. reqFields |= mask
  347. required--
  348. }
  349. } else {
  350. // This is imprecise. It can be fooled by a required field
  351. // with a tag > 64 that is encoded twice; that's very rare.
  352. // A fully correct implementation would require allocating
  353. // a data structure, which we would like to avoid.
  354. required--
  355. }
  356. }
  357. }
  358. if err == nil {
  359. if is_group {
  360. return io.ErrUnexpectedEOF
  361. }
  362. if required > 0 {
  363. return &ErrRequiredNotSet{st}
  364. }
  365. }
  366. return err
  367. }
  368. // Individual type decoders
  369. // For each,
  370. // u is the decoded value,
  371. // v is a pointer to the field (pointer) in the struct
  372. // Sizes of the pools to allocate inside the Buffer.
  373. // The goal is modest amortization and allocation
  374. // on at least 16-byte boundaries.
  375. const (
  376. boolPoolSize = 16
  377. int32PoolSize = 8
  378. int64PoolSize = 4
  379. )
  380. // Decode a bool.
  381. func (o *Buffer) dec_bool(p *Properties, base uintptr) error {
  382. u, err := p.valDec(o)
  383. if err != nil {
  384. return err
  385. }
  386. if len(o.bools) == 0 {
  387. o.bools = make([]bool, boolPoolSize)
  388. }
  389. o.bools[0] = u != 0
  390. v := (**bool)(unsafe.Pointer(base + p.offset))
  391. *v = &o.bools[0]
  392. o.bools = o.bools[1:]
  393. return nil
  394. }
  395. // Decode an int32.
  396. func (o *Buffer) dec_int32(p *Properties, base uintptr) error {
  397. u, err := p.valDec(o)
  398. if err != nil {
  399. return err
  400. }
  401. if len(o.int32s) == 0 {
  402. o.int32s = make([]int32, int32PoolSize)
  403. }
  404. o.int32s[0] = int32(u)
  405. v := (**int32)(unsafe.Pointer(base + p.offset))
  406. *v = &o.int32s[0]
  407. o.int32s = o.int32s[1:]
  408. return nil
  409. }
  410. // Decode an int64.
  411. func (o *Buffer) dec_int64(p *Properties, base uintptr) error {
  412. u, err := p.valDec(o)
  413. if err != nil {
  414. return err
  415. }
  416. if len(o.int64s) == 0 {
  417. o.int64s = make([]int64, int64PoolSize)
  418. }
  419. o.int64s[0] = int64(u)
  420. v := (**int64)(unsafe.Pointer(base + p.offset))
  421. *v = &o.int64s[0]
  422. o.int64s = o.int64s[1:]
  423. return nil
  424. }
  425. // Decode a string.
  426. func (o *Buffer) dec_string(p *Properties, base uintptr) error {
  427. s, err := o.DecodeStringBytes()
  428. if err != nil {
  429. return err
  430. }
  431. sp := new(string)
  432. *sp = s
  433. v := (**string)(unsafe.Pointer(base + p.offset))
  434. *v = sp
  435. return nil
  436. }
  437. // Decode a slice of bytes ([]byte).
  438. func (o *Buffer) dec_slice_byte(p *Properties, base uintptr) error {
  439. b, err := o.DecodeRawBytes(true)
  440. if err != nil {
  441. return err
  442. }
  443. v := (*[]byte)(unsafe.Pointer(base + p.offset))
  444. *v = b
  445. return nil
  446. }
  447. // Decode a slice of bools ([]bool).
  448. func (o *Buffer) dec_slice_bool(p *Properties, base uintptr) error {
  449. u, err := p.valDec(o)
  450. if err != nil {
  451. return err
  452. }
  453. v := (*[]bool)(unsafe.Pointer(base + p.offset))
  454. *v = append(*v, u != 0)
  455. return nil
  456. }
  457. // Decode a slice of bools ([]bool) in packed format.
  458. func (o *Buffer) dec_slice_packed_bool(p *Properties, base uintptr) error {
  459. v := (*[]bool)(unsafe.Pointer(base + p.offset))
  460. nn, err := o.DecodeVarint()
  461. if err != nil {
  462. return err
  463. }
  464. nb := int(nn) // number of bytes of encoded bools
  465. y := *v
  466. for i := 0; i < nb; i++ {
  467. u, err := p.valDec(o)
  468. if err != nil {
  469. return err
  470. }
  471. y = append(y, u != 0)
  472. }
  473. *v = y
  474. return nil
  475. }
  476. // Decode a slice of int32s ([]int32).
  477. func (o *Buffer) dec_slice_int32(p *Properties, base uintptr) error {
  478. u, err := p.valDec(o)
  479. if err != nil {
  480. return err
  481. }
  482. v := (*[]int32)(unsafe.Pointer(base + p.offset))
  483. *v = append(*v, int32(u))
  484. return nil
  485. }
  486. // Decode a slice of int32s ([]int32) in packed format.
  487. func (o *Buffer) dec_slice_packed_int32(p *Properties, base uintptr) error {
  488. v := (*[]int32)(unsafe.Pointer(base + p.offset))
  489. nn, err := o.DecodeVarint()
  490. if err != nil {
  491. return err
  492. }
  493. nb := int(nn) // number of bytes of encoded int32s
  494. y := *v
  495. fin := o.index + nb
  496. for o.index < fin {
  497. u, err := p.valDec(o)
  498. if err != nil {
  499. return err
  500. }
  501. y = append(y, int32(u))
  502. }
  503. *v = y
  504. return nil
  505. }
  506. // Decode a slice of int64s ([]int64).
  507. func (o *Buffer) dec_slice_int64(p *Properties, base uintptr) error {
  508. u, err := p.valDec(o)
  509. if err != nil {
  510. return err
  511. }
  512. v := (*[]int64)(unsafe.Pointer(base + p.offset))
  513. y := *v
  514. *v = append(y, int64(u))
  515. return nil
  516. }
  517. // Decode a slice of int64s ([]int64) in packed format.
  518. func (o *Buffer) dec_slice_packed_int64(p *Properties, base uintptr) error {
  519. v := (*[]int64)(unsafe.Pointer(base + p.offset))
  520. nn, err := o.DecodeVarint()
  521. if err != nil {
  522. return err
  523. }
  524. nb := int(nn) // number of bytes of encoded int64s
  525. y := *v
  526. fin := o.index + nb
  527. for o.index < fin {
  528. u, err := p.valDec(o)
  529. if err != nil {
  530. return err
  531. }
  532. y = append(y, int64(u))
  533. }
  534. *v = y
  535. return nil
  536. }
  537. // Decode a slice of strings ([]string).
  538. func (o *Buffer) dec_slice_string(p *Properties, base uintptr) error {
  539. s, err := o.DecodeStringBytes()
  540. if err != nil {
  541. return err
  542. }
  543. v := (*[]string)(unsafe.Pointer(base + p.offset))
  544. y := *v
  545. *v = append(y, s)
  546. return nil
  547. }
  548. // Decode a slice of slice of bytes ([][]byte).
  549. func (o *Buffer) dec_slice_slice_byte(p *Properties, base uintptr) error {
  550. b, err := o.DecodeRawBytes(true)
  551. if err != nil {
  552. return err
  553. }
  554. v := (*[][]byte)(unsafe.Pointer(base + p.offset))
  555. y := *v
  556. *v = append(y, b)
  557. return nil
  558. }
  559. // Decode a group.
  560. func (o *Buffer) dec_struct_group(p *Properties, base uintptr) error {
  561. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  562. typ := p.stype.Elem()
  563. bas := reflect.New(typ).Pointer()
  564. structv := unsafe.Pointer(bas)
  565. *ptr = (*struct{})(structv)
  566. err := o.unmarshalType(p.stype, true, bas)
  567. return err
  568. }
  569. // Decode an embedded message.
  570. func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) {
  571. raw, e := o.DecodeRawBytes(false)
  572. if e != nil {
  573. return e
  574. }
  575. ptr := (**struct{})(unsafe.Pointer(base + p.offset))
  576. typ := p.stype.Elem()
  577. bas := reflect.New(typ).Pointer()
  578. structp := unsafe.Pointer(bas)
  579. *ptr = (*struct{})(structp)
  580. // If the object can unmarshal itself, let it.
  581. if p.isMarshaler {
  582. iv := reflect.NewAt(p.stype.Elem(), structp).Interface()
  583. return iv.(Unmarshaler).Unmarshal(raw)
  584. }
  585. obuf := o.buf
  586. oi := o.index
  587. o.buf = raw
  588. o.index = 0
  589. err = o.unmarshalType(p.stype, false, bas)
  590. o.buf = obuf
  591. o.index = oi
  592. return err
  593. }
  594. // Decode a slice of embedded messages.
  595. func (o *Buffer) dec_slice_struct_message(p *Properties, base uintptr) error {
  596. return o.dec_slice_struct(p, false, base)
  597. }
  598. // Decode a slice of embedded groups.
  599. func (o *Buffer) dec_slice_struct_group(p *Properties, base uintptr) error {
  600. return o.dec_slice_struct(p, true, base)
  601. }
  602. // Decode a slice of structs ([]*struct).
  603. func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base uintptr) error {
  604. v := (*[]*struct{})(unsafe.Pointer(base + p.offset))
  605. y := *v
  606. typ := p.stype.Elem()
  607. bas := reflect.New(typ).Pointer()
  608. structp := unsafe.Pointer(bas)
  609. y = append(y, (*struct{})(structp))
  610. *v = y
  611. if is_group {
  612. err := o.unmarshalType(p.stype, is_group, bas)
  613. return err
  614. }
  615. raw, err := o.DecodeRawBytes(true)
  616. if err != nil {
  617. return err
  618. }
  619. // If the object can unmarshal itself, let it.
  620. if p.isUnmarshaler {
  621. iv := reflect.NewAt(typ, structp).Interface()
  622. return iv.(Unmarshaler).Unmarshal(raw)
  623. }
  624. obuf := o.buf
  625. oi := o.index
  626. o.buf = raw
  627. o.index = 0
  628. err = o.unmarshalType(p.stype, is_group, bas)
  629. o.buf = obuf
  630. o.index = oi
  631. return err
  632. }