Counter Strike : Global Offensive Source Code
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.

769 lines
25 KiB

  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # http://code.google.com/p/protobuf/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Code for encoding protocol message primitives.
  31. Contains the logic for encoding every logical protocol field type
  32. into one of the 5 physical wire types.
  33. This code is designed to push the Python interpreter's performance to the
  34. limits.
  35. The basic idea is that at startup time, for every field (i.e. every
  36. FieldDescriptor) we construct two functions: a "sizer" and an "encoder". The
  37. sizer takes a value of this field's type and computes its byte size. The
  38. encoder takes a writer function and a value. It encodes the value into byte
  39. strings and invokes the writer function to write those strings. Typically the
  40. writer function is the write() method of a cStringIO.
  41. We try to do as much work as possible when constructing the writer and the
  42. sizer rather than when calling them. In particular:
  43. * We copy any needed global functions to local variables, so that we do not need
  44. to do costly global table lookups at runtime.
  45. * Similarly, we try to do any attribute lookups at startup time if possible.
  46. * Every field's tag is encoded to bytes at startup, since it can't change at
  47. runtime.
  48. * Whatever component of the field size we can compute at startup, we do.
  49. * We *avoid* sharing code if doing so would make the code slower and not sharing
  50. does not burden us too much. For example, encoders for repeated fields do
  51. not just call the encoders for singular fields in a loop because this would
  52. add an extra function call overhead for every loop iteration; instead, we
  53. manually inline the single-value encoder into the loop.
  54. * If a Python function lacks a return statement, Python actually generates
  55. instructions to pop the result of the last statement off the stack, push
  56. None onto the stack, and then return that. If we really don't care what
  57. value is returned, then we can save two instructions by returning the
  58. result of the last statement. It looks funny but it helps.
  59. * We assume that type and bounds checking has happened at a higher level.
  60. """
  61. __author__ = '[email protected] (Kenton Varda)'
  62. import struct
  63. from google.protobuf.internal import wire_format
  64. # This will overflow and thus become IEEE-754 "infinity". We would use
  65. # "float('inf')" but it doesn't work on Windows pre-Python-2.6.
  66. _POS_INF = 1e10000
  67. _NEG_INF = -_POS_INF
  68. def _VarintSize(value):
  69. """Compute the size of a varint value."""
  70. if value <= 0x7f: return 1
  71. if value <= 0x3fff: return 2
  72. if value <= 0x1fffff: return 3
  73. if value <= 0xfffffff: return 4
  74. if value <= 0x7ffffffff: return 5
  75. if value <= 0x3ffffffffff: return 6
  76. if value <= 0x1ffffffffffff: return 7
  77. if value <= 0xffffffffffffff: return 8
  78. if value <= 0x7fffffffffffffff: return 9
  79. return 10
  80. def _SignedVarintSize(value):
  81. """Compute the size of a signed varint value."""
  82. if value < 0: return 10
  83. if value <= 0x7f: return 1
  84. if value <= 0x3fff: return 2
  85. if value <= 0x1fffff: return 3
  86. if value <= 0xfffffff: return 4
  87. if value <= 0x7ffffffff: return 5
  88. if value <= 0x3ffffffffff: return 6
  89. if value <= 0x1ffffffffffff: return 7
  90. if value <= 0xffffffffffffff: return 8
  91. if value <= 0x7fffffffffffffff: return 9
  92. return 10
  93. def _TagSize(field_number):
  94. """Returns the number of bytes required to serialize a tag with this field
  95. number."""
  96. # Just pass in type 0, since the type won't affect the tag+type size.
  97. return _VarintSize(wire_format.PackTag(field_number, 0))
  98. # --------------------------------------------------------------------
  99. # In this section we define some generic sizers. Each of these functions
  100. # takes parameters specific to a particular field type, e.g. int32 or fixed64.
  101. # It returns another function which in turn takes parameters specific to a
  102. # particular field, e.g. the field number and whether it is repeated or packed.
  103. # Look at the next section to see how these are used.
  104. def _SimpleSizer(compute_value_size):
  105. """A sizer which uses the function compute_value_size to compute the size of
  106. each value. Typically compute_value_size is _VarintSize."""
  107. def SpecificSizer(field_number, is_repeated, is_packed):
  108. tag_size = _TagSize(field_number)
  109. if is_packed:
  110. local_VarintSize = _VarintSize
  111. def PackedFieldSize(value):
  112. result = 0
  113. for element in value:
  114. result += compute_value_size(element)
  115. return result + local_VarintSize(result) + tag_size
  116. return PackedFieldSize
  117. elif is_repeated:
  118. def RepeatedFieldSize(value):
  119. result = tag_size * len(value)
  120. for element in value:
  121. result += compute_value_size(element)
  122. return result
  123. return RepeatedFieldSize
  124. else:
  125. def FieldSize(value):
  126. return tag_size + compute_value_size(value)
  127. return FieldSize
  128. return SpecificSizer
  129. def _ModifiedSizer(compute_value_size, modify_value):
  130. """Like SimpleSizer, but modify_value is invoked on each value before it is
  131. passed to compute_value_size. modify_value is typically ZigZagEncode."""
  132. def SpecificSizer(field_number, is_repeated, is_packed):
  133. tag_size = _TagSize(field_number)
  134. if is_packed:
  135. local_VarintSize = _VarintSize
  136. def PackedFieldSize(value):
  137. result = 0
  138. for element in value:
  139. result += compute_value_size(modify_value(element))
  140. return result + local_VarintSize(result) + tag_size
  141. return PackedFieldSize
  142. elif is_repeated:
  143. def RepeatedFieldSize(value):
  144. result = tag_size * len(value)
  145. for element in value:
  146. result += compute_value_size(modify_value(element))
  147. return result
  148. return RepeatedFieldSize
  149. else:
  150. def FieldSize(value):
  151. return tag_size + compute_value_size(modify_value(value))
  152. return FieldSize
  153. return SpecificSizer
  154. def _FixedSizer(value_size):
  155. """Like _SimpleSizer except for a fixed-size field. The input is the size
  156. of one value."""
  157. def SpecificSizer(field_number, is_repeated, is_packed):
  158. tag_size = _TagSize(field_number)
  159. if is_packed:
  160. local_VarintSize = _VarintSize
  161. def PackedFieldSize(value):
  162. result = len(value) * value_size
  163. return result + local_VarintSize(result) + tag_size
  164. return PackedFieldSize
  165. elif is_repeated:
  166. element_size = value_size + tag_size
  167. def RepeatedFieldSize(value):
  168. return len(value) * element_size
  169. return RepeatedFieldSize
  170. else:
  171. field_size = value_size + tag_size
  172. def FieldSize(value):
  173. return field_size
  174. return FieldSize
  175. return SpecificSizer
  176. # ====================================================================
  177. # Here we declare a sizer constructor for each field type. Each "sizer
  178. # constructor" is a function that takes (field_number, is_repeated, is_packed)
  179. # as parameters and returns a sizer, which in turn takes a field value as
  180. # a parameter and returns its encoded size.
  181. Int32Sizer = Int64Sizer = EnumSizer = _SimpleSizer(_SignedVarintSize)
  182. UInt32Sizer = UInt64Sizer = _SimpleSizer(_VarintSize)
  183. SInt32Sizer = SInt64Sizer = _ModifiedSizer(
  184. _SignedVarintSize, wire_format.ZigZagEncode)
  185. Fixed32Sizer = SFixed32Sizer = FloatSizer = _FixedSizer(4)
  186. Fixed64Sizer = SFixed64Sizer = DoubleSizer = _FixedSizer(8)
  187. BoolSizer = _FixedSizer(1)
  188. def StringSizer(field_number, is_repeated, is_packed):
  189. """Returns a sizer for a string field."""
  190. tag_size = _TagSize(field_number)
  191. local_VarintSize = _VarintSize
  192. local_len = len
  193. assert not is_packed
  194. if is_repeated:
  195. def RepeatedFieldSize(value):
  196. result = tag_size * len(value)
  197. for element in value:
  198. l = local_len(element.encode('utf-8'))
  199. result += local_VarintSize(l) + l
  200. return result
  201. return RepeatedFieldSize
  202. else:
  203. def FieldSize(value):
  204. l = local_len(value.encode('utf-8'))
  205. return tag_size + local_VarintSize(l) + l
  206. return FieldSize
  207. def BytesSizer(field_number, is_repeated, is_packed):
  208. """Returns a sizer for a bytes field."""
  209. tag_size = _TagSize(field_number)
  210. local_VarintSize = _VarintSize
  211. local_len = len
  212. assert not is_packed
  213. if is_repeated:
  214. def RepeatedFieldSize(value):
  215. result = tag_size * len(value)
  216. for element in value:
  217. l = local_len(element)
  218. result += local_VarintSize(l) + l
  219. return result
  220. return RepeatedFieldSize
  221. else:
  222. def FieldSize(value):
  223. l = local_len(value)
  224. return tag_size + local_VarintSize(l) + l
  225. return FieldSize
  226. def GroupSizer(field_number, is_repeated, is_packed):
  227. """Returns a sizer for a group field."""
  228. tag_size = _TagSize(field_number) * 2
  229. assert not is_packed
  230. if is_repeated:
  231. def RepeatedFieldSize(value):
  232. result = tag_size * len(value)
  233. for element in value:
  234. result += element.ByteSize()
  235. return result
  236. return RepeatedFieldSize
  237. else:
  238. def FieldSize(value):
  239. return tag_size + value.ByteSize()
  240. return FieldSize
  241. def MessageSizer(field_number, is_repeated, is_packed):
  242. """Returns a sizer for a message field."""
  243. tag_size = _TagSize(field_number)
  244. local_VarintSize = _VarintSize
  245. assert not is_packed
  246. if is_repeated:
  247. def RepeatedFieldSize(value):
  248. result = tag_size * len(value)
  249. for element in value:
  250. l = element.ByteSize()
  251. result += local_VarintSize(l) + l
  252. return result
  253. return RepeatedFieldSize
  254. else:
  255. def FieldSize(value):
  256. l = value.ByteSize()
  257. return tag_size + local_VarintSize(l) + l
  258. return FieldSize
  259. # --------------------------------------------------------------------
  260. # MessageSet is special.
  261. def MessageSetItemSizer(field_number):
  262. """Returns a sizer for extensions of MessageSet.
  263. The message set message looks like this:
  264. message MessageSet {
  265. repeated group Item = 1 {
  266. required int32 type_id = 2;
  267. required string message = 3;
  268. }
  269. }
  270. """
  271. static_size = (_TagSize(1) * 2 + _TagSize(2) + _VarintSize(field_number) +
  272. _TagSize(3))
  273. local_VarintSize = _VarintSize
  274. def FieldSize(value):
  275. l = value.ByteSize()
  276. return static_size + local_VarintSize(l) + l
  277. return FieldSize
  278. # ====================================================================
  279. # Encoders!
  280. def _VarintEncoder():
  281. """Return an encoder for a basic varint value (does not include tag)."""
  282. local_chr = chr
  283. def EncodeVarint(write, value):
  284. bits = value & 0x7f
  285. value >>= 7
  286. while value:
  287. write(local_chr(0x80|bits))
  288. bits = value & 0x7f
  289. value >>= 7
  290. return write(local_chr(bits))
  291. return EncodeVarint
  292. def _SignedVarintEncoder():
  293. """Return an encoder for a basic signed varint value (does not include
  294. tag)."""
  295. local_chr = chr
  296. def EncodeSignedVarint(write, value):
  297. if value < 0:
  298. value += (1 << 64)
  299. bits = value & 0x7f
  300. value >>= 7
  301. while value:
  302. write(local_chr(0x80|bits))
  303. bits = value & 0x7f
  304. value >>= 7
  305. return write(local_chr(bits))
  306. return EncodeSignedVarint
  307. _EncodeVarint = _VarintEncoder()
  308. _EncodeSignedVarint = _SignedVarintEncoder()
  309. def _VarintBytes(value):
  310. """Encode the given integer as a varint and return the bytes. This is only
  311. called at startup time so it doesn't need to be fast."""
  312. pieces = []
  313. _EncodeVarint(pieces.append, value)
  314. return "".join(pieces)
  315. def TagBytes(field_number, wire_type):
  316. """Encode the given tag and return the bytes. Only called at startup."""
  317. return _VarintBytes(wire_format.PackTag(field_number, wire_type))
  318. # --------------------------------------------------------------------
  319. # As with sizers (see above), we have a number of common encoder
  320. # implementations.
  321. def _SimpleEncoder(wire_type, encode_value, compute_value_size):
  322. """Return a constructor for an encoder for fields of a particular type.
  323. Args:
  324. wire_type: The field's wire type, for encoding tags.
  325. encode_value: A function which encodes an individual value, e.g.
  326. _EncodeVarint().
  327. compute_value_size: A function which computes the size of an individual
  328. value, e.g. _VarintSize().
  329. """
  330. def SpecificEncoder(field_number, is_repeated, is_packed):
  331. if is_packed:
  332. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  333. local_EncodeVarint = _EncodeVarint
  334. def EncodePackedField(write, value):
  335. write(tag_bytes)
  336. size = 0
  337. for element in value:
  338. size += compute_value_size(element)
  339. local_EncodeVarint(write, size)
  340. for element in value:
  341. encode_value(write, element)
  342. return EncodePackedField
  343. elif is_repeated:
  344. tag_bytes = TagBytes(field_number, wire_type)
  345. def EncodeRepeatedField(write, value):
  346. for element in value:
  347. write(tag_bytes)
  348. encode_value(write, element)
  349. return EncodeRepeatedField
  350. else:
  351. tag_bytes = TagBytes(field_number, wire_type)
  352. def EncodeField(write, value):
  353. write(tag_bytes)
  354. return encode_value(write, value)
  355. return EncodeField
  356. return SpecificEncoder
  357. def _ModifiedEncoder(wire_type, encode_value, compute_value_size, modify_value):
  358. """Like SimpleEncoder but additionally invokes modify_value on every value
  359. before passing it to encode_value. Usually modify_value is ZigZagEncode."""
  360. def SpecificEncoder(field_number, is_repeated, is_packed):
  361. if is_packed:
  362. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  363. local_EncodeVarint = _EncodeVarint
  364. def EncodePackedField(write, value):
  365. write(tag_bytes)
  366. size = 0
  367. for element in value:
  368. size += compute_value_size(modify_value(element))
  369. local_EncodeVarint(write, size)
  370. for element in value:
  371. encode_value(write, modify_value(element))
  372. return EncodePackedField
  373. elif is_repeated:
  374. tag_bytes = TagBytes(field_number, wire_type)
  375. def EncodeRepeatedField(write, value):
  376. for element in value:
  377. write(tag_bytes)
  378. encode_value(write, modify_value(element))
  379. return EncodeRepeatedField
  380. else:
  381. tag_bytes = TagBytes(field_number, wire_type)
  382. def EncodeField(write, value):
  383. write(tag_bytes)
  384. return encode_value(write, modify_value(value))
  385. return EncodeField
  386. return SpecificEncoder
  387. def _StructPackEncoder(wire_type, format):
  388. """Return a constructor for an encoder for a fixed-width field.
  389. Args:
  390. wire_type: The field's wire type, for encoding tags.
  391. format: The format string to pass to struct.pack().
  392. """
  393. value_size = struct.calcsize(format)
  394. def SpecificEncoder(field_number, is_repeated, is_packed):
  395. local_struct_pack = struct.pack
  396. if is_packed:
  397. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  398. local_EncodeVarint = _EncodeVarint
  399. def EncodePackedField(write, value):
  400. write(tag_bytes)
  401. local_EncodeVarint(write, len(value) * value_size)
  402. for element in value:
  403. write(local_struct_pack(format, element))
  404. return EncodePackedField
  405. elif is_repeated:
  406. tag_bytes = TagBytes(field_number, wire_type)
  407. def EncodeRepeatedField(write, value):
  408. for element in value:
  409. write(tag_bytes)
  410. write(local_struct_pack(format, element))
  411. return EncodeRepeatedField
  412. else:
  413. tag_bytes = TagBytes(field_number, wire_type)
  414. def EncodeField(write, value):
  415. write(tag_bytes)
  416. return write(local_struct_pack(format, value))
  417. return EncodeField
  418. return SpecificEncoder
  419. def _FloatingPointEncoder(wire_type, format):
  420. """Return a constructor for an encoder for float fields.
  421. This is like StructPackEncoder, but catches errors that may be due to
  422. passing non-finite floating-point values to struct.pack, and makes a
  423. second attempt to encode those values.
  424. Args:
  425. wire_type: The field's wire type, for encoding tags.
  426. format: The format string to pass to struct.pack().
  427. """
  428. value_size = struct.calcsize(format)
  429. if value_size == 4:
  430. def EncodeNonFiniteOrRaise(write, value):
  431. # Remember that the serialized form uses little-endian byte order.
  432. if value == _POS_INF:
  433. write('\x00\x00\x80\x7F')
  434. elif value == _NEG_INF:
  435. write('\x00\x00\x80\xFF')
  436. elif value != value: # NaN
  437. write('\x00\x00\xC0\x7F')
  438. else:
  439. raise
  440. elif value_size == 8:
  441. def EncodeNonFiniteOrRaise(write, value):
  442. if value == _POS_INF:
  443. write('\x00\x00\x00\x00\x00\x00\xF0\x7F')
  444. elif value == _NEG_INF:
  445. write('\x00\x00\x00\x00\x00\x00\xF0\xFF')
  446. elif value != value: # NaN
  447. write('\x00\x00\x00\x00\x00\x00\xF8\x7F')
  448. else:
  449. raise
  450. else:
  451. raise ValueError('Can\'t encode floating-point values that are '
  452. '%d bytes long (only 4 or 8)' % value_size)
  453. def SpecificEncoder(field_number, is_repeated, is_packed):
  454. local_struct_pack = struct.pack
  455. if is_packed:
  456. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  457. local_EncodeVarint = _EncodeVarint
  458. def EncodePackedField(write, value):
  459. write(tag_bytes)
  460. local_EncodeVarint(write, len(value) * value_size)
  461. for element in value:
  462. # This try/except block is going to be faster than any code that
  463. # we could write to check whether element is finite.
  464. try:
  465. write(local_struct_pack(format, element))
  466. except SystemError:
  467. EncodeNonFiniteOrRaise(write, element)
  468. return EncodePackedField
  469. elif is_repeated:
  470. tag_bytes = TagBytes(field_number, wire_type)
  471. def EncodeRepeatedField(write, value):
  472. for element in value:
  473. write(tag_bytes)
  474. try:
  475. write(local_struct_pack(format, element))
  476. except SystemError:
  477. EncodeNonFiniteOrRaise(write, element)
  478. return EncodeRepeatedField
  479. else:
  480. tag_bytes = TagBytes(field_number, wire_type)
  481. def EncodeField(write, value):
  482. write(tag_bytes)
  483. try:
  484. write(local_struct_pack(format, value))
  485. except SystemError:
  486. EncodeNonFiniteOrRaise(write, value)
  487. return EncodeField
  488. return SpecificEncoder
  489. # ====================================================================
  490. # Here we declare an encoder constructor for each field type. These work
  491. # very similarly to sizer constructors, described earlier.
  492. Int32Encoder = Int64Encoder = EnumEncoder = _SimpleEncoder(
  493. wire_format.WIRETYPE_VARINT, _EncodeSignedVarint, _SignedVarintSize)
  494. UInt32Encoder = UInt64Encoder = _SimpleEncoder(
  495. wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize)
  496. SInt32Encoder = SInt64Encoder = _ModifiedEncoder(
  497. wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize,
  498. wire_format.ZigZagEncode)
  499. # Note that Python conveniently guarantees that when using the '<' prefix on
  500. # formats, they will also have the same size across all platforms (as opposed
  501. # to without the prefix, where their sizes depend on the C compiler's basic
  502. # type sizes).
  503. Fixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, '<I')
  504. Fixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, '<Q')
  505. SFixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, '<i')
  506. SFixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, '<q')
  507. FloatEncoder = _FloatingPointEncoder(wire_format.WIRETYPE_FIXED32, '<f')
  508. DoubleEncoder = _FloatingPointEncoder(wire_format.WIRETYPE_FIXED64, '<d')
  509. def BoolEncoder(field_number, is_repeated, is_packed):
  510. """Returns an encoder for a boolean field."""
  511. false_byte = chr(0)
  512. true_byte = chr(1)
  513. if is_packed:
  514. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  515. local_EncodeVarint = _EncodeVarint
  516. def EncodePackedField(write, value):
  517. write(tag_bytes)
  518. local_EncodeVarint(write, len(value))
  519. for element in value:
  520. if element:
  521. write(true_byte)
  522. else:
  523. write(false_byte)
  524. return EncodePackedField
  525. elif is_repeated:
  526. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT)
  527. def EncodeRepeatedField(write, value):
  528. for element in value:
  529. write(tag_bytes)
  530. if element:
  531. write(true_byte)
  532. else:
  533. write(false_byte)
  534. return EncodeRepeatedField
  535. else:
  536. tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT)
  537. def EncodeField(write, value):
  538. write(tag_bytes)
  539. if value:
  540. return write(true_byte)
  541. return write(false_byte)
  542. return EncodeField
  543. def StringEncoder(field_number, is_repeated, is_packed):
  544. """Returns an encoder for a string field."""
  545. tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  546. local_EncodeVarint = _EncodeVarint
  547. local_len = len
  548. assert not is_packed
  549. if is_repeated:
  550. def EncodeRepeatedField(write, value):
  551. for element in value:
  552. encoded = element.encode('utf-8')
  553. write(tag)
  554. local_EncodeVarint(write, local_len(encoded))
  555. write(encoded)
  556. return EncodeRepeatedField
  557. else:
  558. def EncodeField(write, value):
  559. encoded = value.encode('utf-8')
  560. write(tag)
  561. local_EncodeVarint(write, local_len(encoded))
  562. return write(encoded)
  563. return EncodeField
  564. def BytesEncoder(field_number, is_repeated, is_packed):
  565. """Returns an encoder for a bytes field."""
  566. tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  567. local_EncodeVarint = _EncodeVarint
  568. local_len = len
  569. assert not is_packed
  570. if is_repeated:
  571. def EncodeRepeatedField(write, value):
  572. for element in value:
  573. write(tag)
  574. local_EncodeVarint(write, local_len(element))
  575. write(element)
  576. return EncodeRepeatedField
  577. else:
  578. def EncodeField(write, value):
  579. write(tag)
  580. local_EncodeVarint(write, local_len(value))
  581. return write(value)
  582. return EncodeField
  583. def GroupEncoder(field_number, is_repeated, is_packed):
  584. """Returns an encoder for a group field."""
  585. start_tag = TagBytes(field_number, wire_format.WIRETYPE_START_GROUP)
  586. end_tag = TagBytes(field_number, wire_format.WIRETYPE_END_GROUP)
  587. assert not is_packed
  588. if is_repeated:
  589. def EncodeRepeatedField(write, value):
  590. for element in value:
  591. write(start_tag)
  592. element._InternalSerialize(write)
  593. write(end_tag)
  594. return EncodeRepeatedField
  595. else:
  596. def EncodeField(write, value):
  597. write(start_tag)
  598. value._InternalSerialize(write)
  599. return write(end_tag)
  600. return EncodeField
  601. def MessageEncoder(field_number, is_repeated, is_packed):
  602. """Returns an encoder for a message field."""
  603. tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  604. local_EncodeVarint = _EncodeVarint
  605. assert not is_packed
  606. if is_repeated:
  607. def EncodeRepeatedField(write, value):
  608. for element in value:
  609. write(tag)
  610. local_EncodeVarint(write, element.ByteSize())
  611. element._InternalSerialize(write)
  612. return EncodeRepeatedField
  613. else:
  614. def EncodeField(write, value):
  615. write(tag)
  616. local_EncodeVarint(write, value.ByteSize())
  617. return value._InternalSerialize(write)
  618. return EncodeField
  619. # --------------------------------------------------------------------
  620. # As before, MessageSet is special.
  621. def MessageSetItemEncoder(field_number):
  622. """Encoder for extensions of MessageSet.
  623. The message set message looks like this:
  624. message MessageSet {
  625. repeated group Item = 1 {
  626. required int32 type_id = 2;
  627. required string message = 3;
  628. }
  629. }
  630. """
  631. start_bytes = "".join([
  632. TagBytes(1, wire_format.WIRETYPE_START_GROUP),
  633. TagBytes(2, wire_format.WIRETYPE_VARINT),
  634. _VarintBytes(field_number),
  635. TagBytes(3, wire_format.WIRETYPE_LENGTH_DELIMITED)])
  636. end_bytes = TagBytes(1, wire_format.WIRETYPE_END_GROUP)
  637. local_EncodeVarint = _EncodeVarint
  638. def EncodeField(write, value):
  639. write(start_bytes)
  640. local_EncodeVarint(write, value.ByteSize())
  641. value._InternalSerialize(write)
  642. return write(end_bytes)
  643. return EncodeField