You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

427 lines
11 KiB

  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. return 0, errOverflow
  168. done:
  169. p.index = i
  170. return x, nil
  171. }
  172. // DecodeFixed64 reads a 64-bit integer from the Buffer.
  173. // This is the format for the
  174. // fixed64, sfixed64, and double protocol buffer types.
  175. func (p *Buffer) DecodeFixed64() (x uint64, err error) {
  176. // x, err already 0
  177. i := p.index + 8
  178. if i < 0 || i > len(p.buf) {
  179. err = io.ErrUnexpectedEOF
  180. return
  181. }
  182. p.index = i
  183. x = uint64(p.buf[i-8])
  184. x |= uint64(p.buf[i-7]) << 8
  185. x |= uint64(p.buf[i-6]) << 16
  186. x |= uint64(p.buf[i-5]) << 24
  187. x |= uint64(p.buf[i-4]) << 32
  188. x |= uint64(p.buf[i-3]) << 40
  189. x |= uint64(p.buf[i-2]) << 48
  190. x |= uint64(p.buf[i-1]) << 56
  191. return
  192. }
  193. // DecodeFixed32 reads a 32-bit integer from the Buffer.
  194. // This is the format for the
  195. // fixed32, sfixed32, and float protocol buffer types.
  196. func (p *Buffer) DecodeFixed32() (x uint64, err error) {
  197. // x, err already 0
  198. i := p.index + 4
  199. if i < 0 || i > len(p.buf) {
  200. err = io.ErrUnexpectedEOF
  201. return
  202. }
  203. p.index = i
  204. x = uint64(p.buf[i-4])
  205. x |= uint64(p.buf[i-3]) << 8
  206. x |= uint64(p.buf[i-2]) << 16
  207. x |= uint64(p.buf[i-1]) << 24
  208. return
  209. }
  210. // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
  211. // from the Buffer.
  212. // This is the format used for the sint64 protocol buffer type.
  213. func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
  214. x, err = p.DecodeVarint()
  215. if err != nil {
  216. return
  217. }
  218. x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
  219. return
  220. }
  221. // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
  222. // from the Buffer.
  223. // This is the format used for the sint32 protocol buffer type.
  224. func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
  225. x, err = p.DecodeVarint()
  226. if err != nil {
  227. return
  228. }
  229. x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
  230. return
  231. }
  232. // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
  233. // This is the format used for the bytes protocol buffer
  234. // type and for embedded messages.
  235. func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
  236. n, err := p.DecodeVarint()
  237. if err != nil {
  238. return nil, err
  239. }
  240. nb := int(n)
  241. if nb < 0 {
  242. return nil, fmt.Errorf("proto: bad byte length %d", nb)
  243. }
  244. end := p.index + nb
  245. if end < p.index || end > len(p.buf) {
  246. return nil, io.ErrUnexpectedEOF
  247. }
  248. if !alloc {
  249. // todo: check if can get more uses of alloc=false
  250. buf = p.buf[p.index:end]
  251. p.index += nb
  252. return
  253. }
  254. buf = make([]byte, nb)
  255. copy(buf, p.buf[p.index:])
  256. p.index += nb
  257. return
  258. }
  259. // DecodeStringBytes reads an encoded string from the Buffer.
  260. // This is the format used for the proto2 string type.
  261. func (p *Buffer) DecodeStringBytes() (s string, err error) {
  262. buf, err := p.DecodeRawBytes(false)
  263. if err != nil {
  264. return
  265. }
  266. return string(buf), nil
  267. }
  268. // Unmarshaler is the interface representing objects that can
  269. // unmarshal themselves. The argument points to data that may be
  270. // overwritten, so implementations should not keep references to the
  271. // buffer.
  272. // Unmarshal implementations should not clear the receiver.
  273. // Any unmarshaled data should be merged into the receiver.
  274. // Callers of Unmarshal that do not want to retain existing data
  275. // should Reset the receiver before calling Unmarshal.
  276. type Unmarshaler interface {
  277. Unmarshal([]byte) error
  278. }
  279. // newUnmarshaler is the interface representing objects that can
  280. // unmarshal themselves. The semantics are identical to Unmarshaler.
  281. //
  282. // This exists to support protoc-gen-go generated messages.
  283. // The proto package will stop type-asserting to this interface in the future.
  284. //
  285. // DO NOT DEPEND ON THIS.
  286. type newUnmarshaler interface {
  287. XXX_Unmarshal([]byte) error
  288. }
  289. // Unmarshal parses the protocol buffer representation in buf and places the
  290. // decoded result in pb. If the struct underlying pb does not match
  291. // the data in buf, the results can be unpredictable.
  292. //
  293. // Unmarshal resets pb before starting to unmarshal, so any
  294. // existing data in pb is always removed. Use UnmarshalMerge
  295. // to preserve and append to existing data.
  296. func Unmarshal(buf []byte, pb Message) error {
  297. pb.Reset()
  298. if u, ok := pb.(newUnmarshaler); ok {
  299. return u.XXX_Unmarshal(buf)
  300. }
  301. if u, ok := pb.(Unmarshaler); ok {
  302. return u.Unmarshal(buf)
  303. }
  304. return NewBuffer(buf).Unmarshal(pb)
  305. }
  306. // UnmarshalMerge parses the protocol buffer representation in buf and
  307. // writes the decoded result to pb. If the struct underlying pb does not match
  308. // the data in buf, the results can be unpredictable.
  309. //
  310. // UnmarshalMerge merges into existing data in pb.
  311. // Most code should use Unmarshal instead.
  312. func UnmarshalMerge(buf []byte, pb Message) error {
  313. if u, ok := pb.(newUnmarshaler); ok {
  314. return u.XXX_Unmarshal(buf)
  315. }
  316. if u, ok := pb.(Unmarshaler); ok {
  317. // NOTE: The history of proto have unfortunately been inconsistent
  318. // whether Unmarshaler should or should not implicitly clear itself.
  319. // Some implementations do, most do not.
  320. // Thus, calling this here may or may not do what people want.
  321. //
  322. // See https://github.com/golang/protobuf/issues/424
  323. return u.Unmarshal(buf)
  324. }
  325. return NewBuffer(buf).Unmarshal(pb)
  326. }
  327. // DecodeMessage reads a count-delimited message from the Buffer.
  328. func (p *Buffer) DecodeMessage(pb Message) error {
  329. enc, err := p.DecodeRawBytes(false)
  330. if err != nil {
  331. return err
  332. }
  333. return NewBuffer(enc).Unmarshal(pb)
  334. }
  335. // DecodeGroup reads a tag-delimited group from the Buffer.
  336. // StartGroup tag is already consumed. This function consumes
  337. // EndGroup tag.
  338. func (p *Buffer) DecodeGroup(pb Message) error {
  339. b := p.buf[p.index:]
  340. x, y := findEndGroup(b)
  341. if x < 0 {
  342. return io.ErrUnexpectedEOF
  343. }
  344. err := Unmarshal(b[:x], pb)
  345. p.index += y
  346. return err
  347. }
  348. // Unmarshal parses the protocol buffer representation in the
  349. // Buffer and places the decoded result in pb. If the struct
  350. // underlying pb does not match the data in the buffer, the results can be
  351. // unpredictable.
  352. //
  353. // Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
  354. func (p *Buffer) Unmarshal(pb Message) error {
  355. // If the object can unmarshal itself, let it.
  356. if u, ok := pb.(newUnmarshaler); ok {
  357. err := u.XXX_Unmarshal(p.buf[p.index:])
  358. p.index = len(p.buf)
  359. return err
  360. }
  361. if u, ok := pb.(Unmarshaler); ok {
  362. // NOTE: The history of proto have unfortunately been inconsistent
  363. // whether Unmarshaler should or should not implicitly clear itself.
  364. // Some implementations do, most do not.
  365. // Thus, calling this here may or may not do what people want.
  366. //
  367. // See https://github.com/golang/protobuf/issues/424
  368. err := u.Unmarshal(p.buf[p.index:])
  369. p.index = len(p.buf)
  370. return err
  371. }
  372. // Slow workaround for messages that aren't Unmarshalers.
  373. // This includes some hand-coded .pb.go files and
  374. // bootstrap protos.
  375. // TODO: fix all of those and then add Unmarshal to
  376. // the Message interface. Then:
  377. // The cast above and code below can be deleted.
  378. // The old unmarshaler can be deleted.
  379. // Clients can call Unmarshal directly (can already do that, actually).
  380. var info InternalMessageInfo
  381. err := info.Unmarshal(pb, p.buf[p.index:])
  382. p.index = len(p.buf)
  383. return err
  384. }