decode.go 9.5 KB

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