wire.go 13 KB

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