decode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. )
  40. // errOverflow is returned when an integer is too large to be represented.
  41. var errOverflow = errors.New("proto: integer overflow")
  42. // ErrInternalBadWireType is returned by generated code when an incorrect
  43. // wire type is encountered. It does not get returned to user code.
  44. var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
  45. // DecodeVarint reads a varint-encoded integer from the slice.
  46. // It returns the integer and the number of bytes consumed, or
  47. // zero if there is not enough.
  48. // This is the format for the
  49. // int32, int64, uint32, uint64, bool, and enum
  50. // protocol buffer types.
  51. func DecodeVarint(buf []byte) (x uint64, n int) {
  52. for shift := uint(0); shift < 64; shift += 7 {
  53. if n >= len(buf) {
  54. return 0, 0
  55. }
  56. b := uint64(buf[n])
  57. n++
  58. x |= (b & 0x7F) << shift
  59. if (b & 0x80) == 0 {
  60. return x, n
  61. }
  62. }
  63. // The number is too large to represent in a 64-bit value.
  64. return 0, 0
  65. }
  66. func (p *Buffer) decodeVarintSlow() (x uint64, err error) {
  67. i := p.index
  68. l := len(p.buf)
  69. for shift := uint(0); shift < 64; shift += 7 {
  70. if i >= l {
  71. err = io.ErrUnexpectedEOF
  72. return
  73. }
  74. b := p.buf[i]
  75. i++
  76. x |= (uint64(b) & 0x7F) << shift
  77. if b < 0x80 {
  78. p.index = i
  79. return
  80. }
  81. }
  82. // The number is too large to represent in a 64-bit value.
  83. err = errOverflow
  84. return
  85. }
  86. // DecodeVarint reads a varint-encoded integer from the Buffer.
  87. // This is the format for the
  88. // int32, int64, uint32, uint64, bool, and enum
  89. // protocol buffer types.
  90. func (p *Buffer) DecodeVarint() (x uint64, err error) {
  91. i := p.index
  92. buf := p.buf
  93. if i >= len(buf) {
  94. return 0, io.ErrUnexpectedEOF
  95. } else if buf[i] < 0x80 {
  96. p.index++
  97. return uint64(buf[i]), nil
  98. } else if len(buf)-i < 10 {
  99. return p.decodeVarintSlow()
  100. }
  101. var b uint64
  102. // we already checked the first byte
  103. x = uint64(buf[i]) - 0x80
  104. i++
  105. b = uint64(buf[i])
  106. i++
  107. x += b << 7
  108. if b&0x80 == 0 {
  109. goto done
  110. }
  111. x -= 0x80 << 7
  112. b = uint64(buf[i])
  113. i++
  114. x += b << 14
  115. if b&0x80 == 0 {
  116. goto done
  117. }
  118. x -= 0x80 << 14
  119. b = uint64(buf[i])
  120. i++
  121. x += b << 21
  122. if b&0x80 == 0 {
  123. goto done
  124. }
  125. x -= 0x80 << 21
  126. b = uint64(buf[i])
  127. i++
  128. x += b << 28
  129. if b&0x80 == 0 {
  130. goto done
  131. }
  132. x -= 0x80 << 28
  133. b = uint64(buf[i])
  134. i++
  135. x += b << 35
  136. if b&0x80 == 0 {
  137. goto done
  138. }
  139. x -= 0x80 << 35
  140. b = uint64(buf[i])
  141. i++
  142. x += b << 42
  143. if b&0x80 == 0 {
  144. goto done
  145. }
  146. x -= 0x80 << 42
  147. b = uint64(buf[i])
  148. i++
  149. x += b << 49
  150. if b&0x80 == 0 {
  151. goto done
  152. }
  153. x -= 0x80 << 49
  154. b = uint64(buf[i])
  155. i++
  156. x += b << 56
  157. if b&0x80 == 0 {
  158. goto done
  159. }
  160. x -= 0x80 << 56
  161. b = uint64(buf[i])
  162. i++
  163. x += b << 63
  164. if b&0x80 == 0 {
  165. goto done
  166. }
  167. // x -= 0x80 << 63 // Always zero.
  168. return 0, errOverflow
  169. done:
  170. p.index = i
  171. return x, nil
  172. }
  173. // DecodeFixed64 reads a 64-bit integer from the Buffer.
  174. // This is the format for the
  175. // fixed64, sfixed64, and double protocol buffer types.
  176. func (p *Buffer) DecodeFixed64() (x uint64, err error) {
  177. // x, err already 0
  178. i := p.index + 8
  179. if i < 0 || i > len(p.buf) {
  180. err = io.ErrUnexpectedEOF
  181. return
  182. }
  183. p.index = i
  184. x = uint64(p.buf[i-8])
  185. x |= uint64(p.buf[i-7]) << 8
  186. x |= uint64(p.buf[i-6]) << 16
  187. x |= uint64(p.buf[i-5]) << 24
  188. x |= uint64(p.buf[i-4]) << 32
  189. x |= uint64(p.buf[i-3]) << 40
  190. x |= uint64(p.buf[i-2]) << 48
  191. x |= uint64(p.buf[i-1]) << 56
  192. return
  193. }
  194. // DecodeFixed32 reads a 32-bit integer from the Buffer.
  195. // This is the format for the
  196. // fixed32, sfixed32, and float protocol buffer types.
  197. func (p *Buffer) DecodeFixed32() (x uint64, err error) {
  198. // x, err already 0
  199. i := p.index + 4
  200. if i < 0 || i > len(p.buf) {
  201. err = io.ErrUnexpectedEOF
  202. return
  203. }
  204. p.index = i
  205. x = uint64(p.buf[i-4])
  206. x |= uint64(p.buf[i-3]) << 8
  207. x |= uint64(p.buf[i-2]) << 16
  208. x |= uint64(p.buf[i-1]) << 24
  209. return
  210. }
  211. // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
  212. // from the Buffer.
  213. // This is the format used for the sint64 protocol buffer type.
  214. func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
  215. x, err = p.DecodeVarint()
  216. if err != nil {
  217. return
  218. }
  219. x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
  220. return
  221. }
  222. // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
  223. // from the Buffer.
  224. // This is the format used for the sint32 protocol buffer type.
  225. func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
  226. x, err = p.DecodeVarint()
  227. if err != nil {
  228. return
  229. }
  230. x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
  231. return
  232. }
  233. // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
  234. // This is the format used for the bytes protocol buffer
  235. // type and for embedded messages.
  236. func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
  237. n, err := p.DecodeVarint()
  238. if err != nil {
  239. return nil, err
  240. }
  241. nb := int(n)
  242. if nb < 0 {
  243. return nil, fmt.Errorf("proto: bad byte length %d", nb)
  244. }
  245. end := p.index + nb
  246. if end < p.index || end > len(p.buf) {
  247. return nil, io.ErrUnexpectedEOF
  248. }
  249. if !alloc {
  250. // todo: check if can get more uses of alloc=false
  251. buf = p.buf[p.index:end]
  252. p.index += nb
  253. return
  254. }
  255. buf = make([]byte, nb)
  256. copy(buf, p.buf[p.index:])
  257. p.index += nb
  258. return
  259. }
  260. // DecodeStringBytes reads an encoded string from the Buffer.
  261. // This is the format used for the proto2 string type.
  262. func (p *Buffer) DecodeStringBytes() (s string, err error) {
  263. buf, err := p.DecodeRawBytes(false)
  264. if err != nil {
  265. return
  266. }
  267. return string(buf), nil
  268. }
  269. // Unmarshaler is the interface representing objects that can
  270. // unmarshal themselves. The argument points to data that may be
  271. // overwritten, so implementations should not keep references to the
  272. // buffer.
  273. // Unmarshal implementations should not clear the receiver.
  274. // Any unmarshaled data should be merged into the receiver.
  275. // Callers of Unmarshal that do not want to retain existing data
  276. // should Reset the receiver before calling Unmarshal.
  277. type Unmarshaler interface {
  278. Unmarshal([]byte) error
  279. }
  280. // newUnmarshaler is the interface representing objects that can
  281. // unmarshal themselves. The semantics are identical to Unmarshaler.
  282. //
  283. // This exists to support protoc-gen-go generated messages.
  284. // The proto package will stop type-asserting to this interface in the future.
  285. //
  286. // DO NOT DEPEND ON THIS.
  287. type newUnmarshaler interface {
  288. XXX_Unmarshal([]byte) error
  289. }
  290. // Unmarshal parses the protocol buffer representation in buf and places the
  291. // decoded result in pb. If the struct underlying pb does not match
  292. // the data in buf, the results can be unpredictable.
  293. //
  294. // Unmarshal resets pb before starting to unmarshal, so any
  295. // existing data in pb is always removed. Use UnmarshalMerge
  296. // to preserve and append to existing data.
  297. func Unmarshal(buf []byte, pb Message) error {
  298. pb.Reset()
  299. if u, ok := pb.(newUnmarshaler); ok {
  300. return u.XXX_Unmarshal(buf)
  301. }
  302. if u, ok := pb.(Unmarshaler); ok {
  303. return u.Unmarshal(buf)
  304. }
  305. return NewBuffer(buf).Unmarshal(pb)
  306. }
  307. // UnmarshalMerge parses the protocol buffer representation in buf and
  308. // writes the decoded result to pb. If the struct underlying pb does not match
  309. // the data in buf, the results can be unpredictable.
  310. //
  311. // UnmarshalMerge merges into existing data in pb.
  312. // Most code should use Unmarshal instead.
  313. func UnmarshalMerge(buf []byte, pb Message) error {
  314. if u, ok := pb.(newUnmarshaler); ok {
  315. return u.XXX_Unmarshal(buf)
  316. }
  317. if u, ok := pb.(Unmarshaler); ok {
  318. // NOTE: The history of proto have unfortunately been inconsistent
  319. // whether Unmarshaler should or should not implicitly clear itself.
  320. // Some implementations do, most do not.
  321. // Thus, calling this here may or may not do what people want.
  322. //
  323. // See https://github.com/golang/protobuf/issues/424
  324. return u.Unmarshal(buf)
  325. }
  326. return NewBuffer(buf).Unmarshal(pb)
  327. }
  328. // DecodeMessage reads a count-delimited message from the Buffer.
  329. func (p *Buffer) DecodeMessage(pb Message) error {
  330. enc, err := p.DecodeRawBytes(false)
  331. if err != nil {
  332. return err
  333. }
  334. return NewBuffer(enc).Unmarshal(pb)
  335. }
  336. // DecodeGroup reads a tag-delimited group from the Buffer.
  337. // StartGroup tag is already consumed. This function consumes
  338. // EndGroup tag.
  339. func (p *Buffer) DecodeGroup(pb Message) error {
  340. b := p.buf[p.index:]
  341. x, y := findEndGroup(b)
  342. if x < 0 {
  343. return io.ErrUnexpectedEOF
  344. }
  345. err := Unmarshal(b[:x], pb)
  346. p.index += y
  347. return err
  348. }
  349. // Unmarshal parses the protocol buffer representation in the
  350. // Buffer and places the decoded result in pb. If the struct
  351. // underlying pb does not match the data in the buffer, the results can be
  352. // unpredictable.
  353. //
  354. // Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
  355. func (p *Buffer) Unmarshal(pb Message) error {
  356. // If the object can unmarshal itself, let it.
  357. if u, ok := pb.(newUnmarshaler); ok {
  358. err := u.XXX_Unmarshal(p.buf[p.index:])
  359. p.index = len(p.buf)
  360. return err
  361. }
  362. if u, ok := pb.(Unmarshaler); ok {
  363. // NOTE: The history of proto have unfortunately been inconsistent
  364. // whether Unmarshaler should or should not implicitly clear itself.
  365. // Some implementations do, most do not.
  366. // Thus, calling this here may or may not do what people want.
  367. //
  368. // See https://github.com/golang/protobuf/issues/424
  369. err := u.Unmarshal(p.buf[p.index:])
  370. p.index = len(p.buf)
  371. return err
  372. }
  373. // Slow workaround for messages that aren't Unmarshalers.
  374. // This includes some hand-coded .pb.go files and
  375. // bootstrap protos.
  376. // TODO: fix all of those and then add Unmarshal to
  377. // the Message interface. Then:
  378. // The cast above and code below can be deleted.
  379. // The old unmarshaler can be deleted.
  380. // Clients can call Unmarshal directly (can already do that, actually).
  381. var info InternalMessageInfo
  382. err := info.Unmarshal(pb, p.buf[p.index:])
  383. p.index = len(p.buf)
  384. return err
  385. }