wire.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // Copyright 2018 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 wire parses and formats the protobuf wire encoding.
  5. //
  6. // See https://developers.google.com/protocol-buffers/docs/encoding.
  7. package wire
  8. import (
  9. "errors"
  10. "io"
  11. "math"
  12. "math/bits"
  13. "google.golang.org/protobuf/reflect/protoreflect"
  14. )
  15. // Number represents the field number.
  16. type Number = protoreflect.FieldNumber
  17. const (
  18. MinValidNumber Number = 1
  19. FirstReservedNumber Number = 19000
  20. LastReservedNumber Number = 19999
  21. MaxValidNumber Number = 1<<29 - 1
  22. )
  23. // Type represents the wire type.
  24. type Type int8
  25. const (
  26. VarintType Type = 0
  27. Fixed32Type Type = 5
  28. Fixed64Type Type = 1
  29. BytesType Type = 2
  30. StartGroupType Type = 3
  31. EndGroupType Type = 4
  32. )
  33. const (
  34. _ = -iota
  35. errCodeTruncated
  36. errCodeFieldNumber
  37. errCodeOverflow
  38. errCodeReserved
  39. errCodeEndGroup
  40. )
  41. var (
  42. errFieldNumber = errors.New("invalid field number")
  43. errOverflow = errors.New("variable length integer overflow")
  44. errReserved = errors.New("cannot parse reserved wire type")
  45. errEndGroup = errors.New("mismatching end group marker")
  46. errParse = errors.New("parse error")
  47. )
  48. // ParseError converts an error code into an error value.
  49. // This returns nil if n is a non-negative number.
  50. func ParseError(n int) error {
  51. if n >= 0 {
  52. return nil
  53. }
  54. switch n {
  55. case errCodeTruncated:
  56. return io.ErrUnexpectedEOF
  57. case errCodeFieldNumber:
  58. return errFieldNumber
  59. case errCodeOverflow:
  60. return errOverflow
  61. case errCodeReserved:
  62. return errReserved
  63. case errCodeEndGroup:
  64. return errEndGroup
  65. default:
  66. return errParse
  67. }
  68. }
  69. // ConsumeField parses an entire field record (both tag and value) and returns
  70. // the field number, the wire type, and the total length.
  71. // This returns a negative length upon an error (see ParseError).
  72. //
  73. // The total length includes the tag header and the end group marker (if the
  74. // field is a group).
  75. func ConsumeField(b []byte) (Number, Type, int) {
  76. num, typ, n := ConsumeTag(b)
  77. if n < 0 {
  78. return 0, 0, n // forward error code
  79. }
  80. m := ConsumeFieldValue(num, typ, b[n:])
  81. if m < 0 {
  82. return 0, 0, m // forward error code
  83. }
  84. return num, typ, n + m
  85. }
  86. // ConsumeFieldValue parses a field value and returns its length.
  87. // This assumes that the field Number and wire Type have already been parsed.
  88. // This returns a negative length upon an error (see ParseError).
  89. //
  90. // When parsing a group, the length includes the end group marker and
  91. // the end group is verified to match the starting field number.
  92. func ConsumeFieldValue(num Number, typ Type, b []byte) (n int) {
  93. switch typ {
  94. case VarintType:
  95. _, n = ConsumeVarint(b)
  96. return n
  97. case Fixed32Type:
  98. _, n = ConsumeFixed32(b)
  99. return n
  100. case Fixed64Type:
  101. _, n = ConsumeFixed64(b)
  102. return n
  103. case BytesType:
  104. _, n = ConsumeBytes(b)
  105. return n
  106. case StartGroupType:
  107. n0 := len(b)
  108. for {
  109. num2, typ2, n := ConsumeTag(b)
  110. if n < 0 {
  111. return n // forward error code
  112. }
  113. b = b[n:]
  114. if typ2 == EndGroupType {
  115. if num != num2 {
  116. return errCodeEndGroup
  117. }
  118. return n0 - len(b)
  119. }
  120. n = ConsumeFieldValue(num2, typ2, b)
  121. if n < 0 {
  122. return n // forward error code
  123. }
  124. b = b[n:]
  125. }
  126. case EndGroupType:
  127. return errCodeEndGroup
  128. default:
  129. return errCodeReserved
  130. }
  131. }
  132. // AppendTag encodes num and typ as a varint-encoded tag and appends it to b.
  133. func AppendTag(b []byte, num Number, typ Type) []byte {
  134. return AppendVarint(b, EncodeTag(num, typ))
  135. }
  136. // ConsumeTag parses b as a varint-encoded tag, reporting its length.
  137. // This returns a negative length upon an error (see ParseError).
  138. func ConsumeTag(b []byte) (Number, Type, int) {
  139. v, n := ConsumeVarint(b)
  140. if n < 0 {
  141. return 0, 0, n // forward error code
  142. }
  143. num, typ := DecodeTag(v)
  144. if num < MinValidNumber {
  145. return 0, 0, errCodeFieldNumber
  146. }
  147. return num, typ, n
  148. }
  149. func SizeTag(num Number) int {
  150. return SizeVarint(EncodeTag(num, 0)) // wire type has no effect on size
  151. }
  152. // AppendVarint appends v to b as a varint-encoded uint64.
  153. func AppendVarint(b []byte, v uint64) []byte {
  154. // TODO: Specialize for sizes 1 and 2 with mid-stack inlining.
  155. switch {
  156. case v < 1<<7:
  157. b = append(b, byte(v))
  158. case v < 1<<14:
  159. b = append(b,
  160. byte((v>>0)&0x7f|0x80),
  161. byte(v>>7))
  162. case v < 1<<21:
  163. b = append(b,
  164. byte((v>>0)&0x7f|0x80),
  165. byte((v>>7)&0x7f|0x80),
  166. byte(v>>14))
  167. case v < 1<<28:
  168. b = append(b,
  169. byte((v>>0)&0x7f|0x80),
  170. byte((v>>7)&0x7f|0x80),
  171. byte((v>>14)&0x7f|0x80),
  172. byte(v>>21))
  173. case v < 1<<35:
  174. b = append(b,
  175. byte((v>>0)&0x7f|0x80),
  176. byte((v>>7)&0x7f|0x80),
  177. byte((v>>14)&0x7f|0x80),
  178. byte((v>>21)&0x7f|0x80),
  179. byte(v>>28))
  180. case v < 1<<42:
  181. b = append(b,
  182. byte((v>>0)&0x7f|0x80),
  183. byte((v>>7)&0x7f|0x80),
  184. byte((v>>14)&0x7f|0x80),
  185. byte((v>>21)&0x7f|0x80),
  186. byte((v>>28)&0x7f|0x80),
  187. byte(v>>35))
  188. case v < 1<<49:
  189. b = append(b,
  190. byte((v>>0)&0x7f|0x80),
  191. byte((v>>7)&0x7f|0x80),
  192. byte((v>>14)&0x7f|0x80),
  193. byte((v>>21)&0x7f|0x80),
  194. byte((v>>28)&0x7f|0x80),
  195. byte((v>>35)&0x7f|0x80),
  196. byte(v>>42))
  197. case v < 1<<56:
  198. b = append(b,
  199. byte((v>>0)&0x7f|0x80),
  200. byte((v>>7)&0x7f|0x80),
  201. byte((v>>14)&0x7f|0x80),
  202. byte((v>>21)&0x7f|0x80),
  203. byte((v>>28)&0x7f|0x80),
  204. byte((v>>35)&0x7f|0x80),
  205. byte((v>>42)&0x7f|0x80),
  206. byte(v>>49))
  207. case v < 1<<63:
  208. b = append(b,
  209. byte((v>>0)&0x7f|0x80),
  210. byte((v>>7)&0x7f|0x80),
  211. byte((v>>14)&0x7f|0x80),
  212. byte((v>>21)&0x7f|0x80),
  213. byte((v>>28)&0x7f|0x80),
  214. byte((v>>35)&0x7f|0x80),
  215. byte((v>>42)&0x7f|0x80),
  216. byte((v>>49)&0x7f|0x80),
  217. byte(v>>56))
  218. default:
  219. b = append(b,
  220. byte((v>>0)&0x7f|0x80),
  221. byte((v>>7)&0x7f|0x80),
  222. byte((v>>14)&0x7f|0x80),
  223. byte((v>>21)&0x7f|0x80),
  224. byte((v>>28)&0x7f|0x80),
  225. byte((v>>35)&0x7f|0x80),
  226. byte((v>>42)&0x7f|0x80),
  227. byte((v>>49)&0x7f|0x80),
  228. byte((v>>56)&0x7f|0x80),
  229. 1)
  230. }
  231. return b
  232. }
  233. // ConsumeVarint parses b as a varint-encoded uint64, reporting its length.
  234. // This returns a negative length upon an error (see ParseError).
  235. func ConsumeVarint(b []byte) (v uint64, n int) {
  236. // TODO: Specialize for sizes 1 and 2 with mid-stack inlining.
  237. var y uint64
  238. if len(b) <= 0 {
  239. return 0, errCodeTruncated
  240. }
  241. v = uint64(b[0])
  242. if v < 0x80 {
  243. return v, 1
  244. }
  245. v -= 0x80
  246. if len(b) <= 1 {
  247. return 0, errCodeTruncated
  248. }
  249. y = uint64(b[1])
  250. v += y << 7
  251. if y < 0x80 {
  252. return v, 2
  253. }
  254. v -= 0x80 << 7
  255. if len(b) <= 2 {
  256. return 0, errCodeTruncated
  257. }
  258. y = uint64(b[2])
  259. v += y << 14
  260. if y < 0x80 {
  261. return v, 3
  262. }
  263. v -= 0x80 << 14
  264. if len(b) <= 3 {
  265. return 0, errCodeTruncated
  266. }
  267. y = uint64(b[3])
  268. v += y << 21
  269. if y < 0x80 {
  270. return v, 4
  271. }
  272. v -= 0x80 << 21
  273. if len(b) <= 4 {
  274. return 0, errCodeTruncated
  275. }
  276. y = uint64(b[4])
  277. v += y << 28
  278. if y < 0x80 {
  279. return v, 5
  280. }
  281. v -= 0x80 << 28
  282. if len(b) <= 5 {
  283. return 0, errCodeTruncated
  284. }
  285. y = uint64(b[5])
  286. v += y << 35
  287. if y < 0x80 {
  288. return v, 6
  289. }
  290. v -= 0x80 << 35
  291. if len(b) <= 6 {
  292. return 0, errCodeTruncated
  293. }
  294. y = uint64(b[6])
  295. v += y << 42
  296. if y < 0x80 {
  297. return v, 7
  298. }
  299. v -= 0x80 << 42
  300. if len(b) <= 7 {
  301. return 0, errCodeTruncated
  302. }
  303. y = uint64(b[7])
  304. v += y << 49
  305. if y < 0x80 {
  306. return v, 8
  307. }
  308. v -= 0x80 << 49
  309. if len(b) <= 8 {
  310. return 0, errCodeTruncated
  311. }
  312. y = uint64(b[8])
  313. v += y << 56
  314. if y < 0x80 {
  315. return v, 9
  316. }
  317. v -= 0x80 << 56
  318. if len(b) <= 9 {
  319. return 0, errCodeTruncated
  320. }
  321. y = uint64(b[9])
  322. v += y << 63
  323. if y < 2 {
  324. return v, 10
  325. }
  326. return 0, errCodeOverflow
  327. }
  328. // SizeVarint returns the encoded size of a varint.
  329. // The size is guaranteed to be within 1 and 10, inclusive.
  330. func SizeVarint(v uint64) int {
  331. return 1 + (bits.Len64(v)-1)/7
  332. }
  333. // AppendFixed32 appends v to b as a little-endian uint32.
  334. func AppendFixed32(b []byte, v uint32) []byte {
  335. return append(b,
  336. byte(v>>0),
  337. byte(v>>8),
  338. byte(v>>16),
  339. byte(v>>24))
  340. }
  341. // ConsumeFixed32 parses b as a little-endian uint32, reporting its length.
  342. // This returns a negative length upon an error (see ParseError).
  343. func ConsumeFixed32(b []byte) (v uint32, n int) {
  344. if len(b) < 4 {
  345. return 0, errCodeTruncated
  346. }
  347. v = uint32(b[0])<<0 | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  348. return v, 4
  349. }
  350. // SizeFixed32 returns the encoded size of a fixed32; which is always 4.
  351. func SizeFixed32() int {
  352. return 4
  353. }
  354. // AppendFixed64 appends v to b as a little-endian uint64.
  355. func AppendFixed64(b []byte, v uint64) []byte {
  356. return append(b,
  357. byte(v>>0),
  358. byte(v>>8),
  359. byte(v>>16),
  360. byte(v>>24),
  361. byte(v>>32),
  362. byte(v>>40),
  363. byte(v>>48),
  364. byte(v>>56))
  365. }
  366. // ConsumeFixed64 parses b as a little-endian uint64, reporting its length.
  367. // This returns a negative length upon an error (see ParseError).
  368. func ConsumeFixed64(b []byte) (v uint64, n int) {
  369. if len(b) < 8 {
  370. return 0, errCodeTruncated
  371. }
  372. v = uint64(b[0])<<0 | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  373. return v, 8
  374. }
  375. // SizeFixed64 returns the encoded size of a fixed64; which is always 8.
  376. func SizeFixed64() int {
  377. return 8
  378. }
  379. // AppendBytes appends v to b as a length-prefixed bytes value.
  380. func AppendBytes(b []byte, v []byte) []byte {
  381. return append(AppendVarint(b, uint64(len(v))), v...)
  382. }
  383. // ConsumeBytes parses b as a length-prefixed bytes value, reporting its length.
  384. // This returns a negative length upon an error (see ParseError).
  385. func ConsumeBytes(b []byte) (v []byte, n int) {
  386. m, n := ConsumeVarint(b)
  387. if n < 0 {
  388. return nil, n // forward error code
  389. }
  390. if m > uint64(len(b[n:])) {
  391. return nil, errCodeTruncated
  392. }
  393. return b[n:][:m], n + int(m)
  394. }
  395. // SizeBytes returns the encoded size of a length-prefixed bytes value,
  396. // given only the length.
  397. func SizeBytes(n int) int {
  398. return SizeVarint(uint64(n)) + n
  399. }
  400. // AppendGroup appends v to b as group value, with a trailing end group marker.
  401. // The value v must not contain the end marker.
  402. func AppendGroup(b []byte, num Number, v []byte) []byte {
  403. return AppendVarint(append(b, v...), EncodeTag(num, EndGroupType))
  404. }
  405. // ConsumeGroup parses b as a group value until the trailing end group marker,
  406. // and verifies that the end marker matches the provided num. The value v
  407. // does not contain the end marker, while the length does contain the end marker.
  408. // This returns a negative length upon an error (see ParseError).
  409. func ConsumeGroup(num Number, b []byte) (v []byte, n int) {
  410. n = ConsumeFieldValue(num, StartGroupType, b)
  411. if n < 0 {
  412. return nil, n // forward error code
  413. }
  414. b = b[:n]
  415. // Truncate off end group marker, but need to handle denormalized varints.
  416. // Assuming end marker is never 0 (which is always the case since
  417. // EndGroupType is non-zero), we can truncate all trailing bytes where the
  418. // lower 7 bits are all zero (implying that the varint is denormalized).
  419. for len(b) > 0 && b[len(b)-1]&0x7f == 0 {
  420. b = b[:len(b)-1]
  421. }
  422. b = b[:len(b)-SizeTag(num)]
  423. return b, n
  424. }
  425. // SizeGroup returns the encoded size of a group, given only the length.
  426. func SizeGroup(num Number, n int) int {
  427. return n + SizeTag(num)
  428. }
  429. // DecodeTag decodes the field Number and wire Type from its unified form.
  430. // The Number is -1 if the decoded field number overflows.
  431. // Other than overflow, this does not check for field number validity.
  432. func DecodeTag(x uint64) (Number, Type) {
  433. // NOTE: MessageSet allows for larger field numbers than normal.
  434. if x>>3 > uint64(math.MaxInt32) {
  435. return -1, 0
  436. }
  437. return Number(x >> 3), Type(x & 7)
  438. }
  439. // EncodeTag encodes the field Number and wire Type into its unified form.
  440. func EncodeTag(num Number, typ Type) uint64 {
  441. return uint64(num)<<3 | uint64(typ&7)
  442. }
  443. // DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.
  444. // Input: {…, 5, 3, 1, 0, 2, 4, 6, …}
  445. // Output: {…, -3, -2, -1, 0, +1, +2, +3, …}
  446. func DecodeZigZag(x uint64) int64 {
  447. return int64(x>>1) ^ int64(x)<<63>>63
  448. }
  449. // EncodeZigZag encodes an int64 as a zig-zag-encoded uint64.
  450. // Input: {…, -3, -2, -1, 0, +1, +2, +3, …}
  451. // Output: {…, 5, 3, 1, 0, 2, 4, 6, …}
  452. func EncodeZigZag(x int64) uint64 {
  453. return uint64(x<<1) ^ uint64(x>>63)
  454. }
  455. // DecodeBool decodes a uint64 as a bool.
  456. // Input: { 0, 1, 2, …}
  457. // Output: {false, true, true, …}
  458. func DecodeBool(x uint64) bool {
  459. return x != 0
  460. }
  461. // EncodeBool encodes a bool as a uint64.
  462. // Input: {false, true}
  463. // Output: { 0, 1}
  464. func EncodeBool(x bool) uint64 {
  465. if x {
  466. return 1
  467. }
  468. return 0
  469. }