wire_format_lite.h 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // atenasio@google.com (Chris Atenasio) (ZigZag transform)
  32. // wink@google.com (Wink Saville) (refactored from wire_format.h)
  33. // Based on original Protocol Buffers design by
  34. // Sanjay Ghemawat, Jeff Dean, and others.
  35. //
  36. // This header is logically internal, but is made public because it is used
  37. // from protocol-compiler-generated code, which may reside in other components.
  38. #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
  39. #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
  40. #include <string>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/stubs/logging.h>
  43. #include <google/protobuf/io/coded_stream.h>
  44. #include <google/protobuf/arenastring.h>
  45. #include <google/protobuf/message_lite.h>
  46. #include <google/protobuf/port.h>
  47. #include <google/protobuf/repeated_field.h>
  48. #include <google/protobuf/stubs/casts.h>
  49. // Do UTF-8 validation on string type in Debug build only
  50. #ifndef NDEBUG
  51. #define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  52. #endif
  53. // Avoid conflict with iOS where <ConditionalMacros.h> #defines TYPE_BOOL.
  54. //
  55. // If some one needs the macro TYPE_BOOL in a file that includes this header,
  56. // it's possible to bring it back using push/pop_macro as follows.
  57. //
  58. // #pragma push_macro("TYPE_BOOL")
  59. // #include this header and/or all headers that need the macro to be undefined.
  60. // #pragma pop_macro("TYPE_BOOL")
  61. #undef TYPE_BOOL
  62. #include <google/protobuf/port_def.inc>
  63. namespace google {
  64. namespace protobuf {
  65. namespace internal {
  66. // This class is for internal use by the protocol buffer library and by
  67. // protocol-compiler-generated message classes. It must not be called
  68. // directly by clients.
  69. //
  70. // This class contains helpers for implementing the binary protocol buffer
  71. // wire format without the need for reflection. Use WireFormat when using
  72. // reflection.
  73. //
  74. // This class is really a namespace that contains only static methods.
  75. class PROTOBUF_EXPORT WireFormatLite {
  76. public:
  77. // -----------------------------------------------------------------
  78. // Helper constants and functions related to the format. These are
  79. // mostly meant for internal and generated code to use.
  80. // The wire format is composed of a sequence of tag/value pairs, each
  81. // of which contains the value of one field (or one element of a repeated
  82. // field). Each tag is encoded as a varint. The lower bits of the tag
  83. // identify its wire type, which specifies the format of the data to follow.
  84. // The rest of the bits contain the field number. Each type of field (as
  85. // declared by FieldDescriptor::Type, in descriptor.h) maps to one of
  86. // these wire types. Immediately following each tag is the field's value,
  87. // encoded in the format specified by the wire type. Because the tag
  88. // identifies the encoding of this data, it is possible to skip
  89. // unrecognized fields for forwards compatibility.
  90. enum WireType {
  91. WIRETYPE_VARINT = 0,
  92. WIRETYPE_FIXED64 = 1,
  93. WIRETYPE_LENGTH_DELIMITED = 2,
  94. WIRETYPE_START_GROUP = 3,
  95. WIRETYPE_END_GROUP = 4,
  96. WIRETYPE_FIXED32 = 5,
  97. };
  98. // Lite alternative to FieldDescriptor::Type. Must be kept in sync.
  99. enum FieldType {
  100. TYPE_DOUBLE = 1,
  101. TYPE_FLOAT = 2,
  102. TYPE_INT64 = 3,
  103. TYPE_UINT64 = 4,
  104. TYPE_INT32 = 5,
  105. TYPE_FIXED64 = 6,
  106. TYPE_FIXED32 = 7,
  107. TYPE_BOOL = 8,
  108. TYPE_STRING = 9,
  109. TYPE_GROUP = 10,
  110. TYPE_MESSAGE = 11,
  111. TYPE_BYTES = 12,
  112. TYPE_UINT32 = 13,
  113. TYPE_ENUM = 14,
  114. TYPE_SFIXED32 = 15,
  115. TYPE_SFIXED64 = 16,
  116. TYPE_SINT32 = 17,
  117. TYPE_SINT64 = 18,
  118. MAX_FIELD_TYPE = 18,
  119. };
  120. // Lite alternative to FieldDescriptor::CppType. Must be kept in sync.
  121. enum CppType {
  122. CPPTYPE_INT32 = 1,
  123. CPPTYPE_INT64 = 2,
  124. CPPTYPE_UINT32 = 3,
  125. CPPTYPE_UINT64 = 4,
  126. CPPTYPE_DOUBLE = 5,
  127. CPPTYPE_FLOAT = 6,
  128. CPPTYPE_BOOL = 7,
  129. CPPTYPE_ENUM = 8,
  130. CPPTYPE_STRING = 9,
  131. CPPTYPE_MESSAGE = 10,
  132. MAX_CPPTYPE = 10,
  133. };
  134. // Helper method to get the CppType for a particular Type.
  135. static CppType FieldTypeToCppType(FieldType type);
  136. // Given a FieldDescriptor::Type return its WireType
  137. static inline WireFormatLite::WireType WireTypeForFieldType(
  138. WireFormatLite::FieldType type) {
  139. return kWireTypeForFieldType[type];
  140. }
  141. // Number of bits in a tag which identify the wire type.
  142. static constexpr int kTagTypeBits = 3;
  143. // Mask for those bits.
  144. static constexpr uint32_t kTagTypeMask = (1 << kTagTypeBits) - 1;
  145. // Helper functions for encoding and decoding tags. (Inlined below and in
  146. // _inl.h)
  147. //
  148. // This is different from MakeTag(field->number(), field->type()) in the
  149. // case of packed repeated fields.
  150. constexpr static uint32_t MakeTag(int field_number, WireType type);
  151. static WireType GetTagWireType(uint32_t tag);
  152. static int GetTagFieldNumber(uint32_t tag);
  153. // Compute the byte size of a tag. For groups, this includes both the start
  154. // and end tags.
  155. static inline size_t TagSize(int field_number,
  156. WireFormatLite::FieldType type);
  157. // Skips a field value with the given tag. The input should start
  158. // positioned immediately after the tag. Skipped values are simply
  159. // discarded, not recorded anywhere. See WireFormat::SkipField() for a
  160. // version that records to an UnknownFieldSet.
  161. static bool SkipField(io::CodedInputStream* input, uint32_t tag);
  162. // Skips a field value with the given tag. The input should start
  163. // positioned immediately after the tag. Skipped values are recorded to a
  164. // CodedOutputStream.
  165. static bool SkipField(io::CodedInputStream* input, uint32_t tag,
  166. io::CodedOutputStream* output);
  167. // Reads and ignores a message from the input. Skipped values are simply
  168. // discarded, not recorded anywhere. See WireFormat::SkipMessage() for a
  169. // version that records to an UnknownFieldSet.
  170. static bool SkipMessage(io::CodedInputStream* input);
  171. // Reads and ignores a message from the input. Skipped values are recorded
  172. // to a CodedOutputStream.
  173. static bool SkipMessage(io::CodedInputStream* input,
  174. io::CodedOutputStream* output);
  175. // This macro does the same thing as WireFormatLite::MakeTag(), but the
  176. // result is usable as a compile-time constant, which makes it usable
  177. // as a switch case or a template input. WireFormatLite::MakeTag() is more
  178. // type-safe, though, so prefer it if possible.
  179. #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \
  180. static_cast<uint32_t>((static_cast<uint32_t>(FIELD_NUMBER) << 3) | (TYPE))
  181. // These are the tags for the old MessageSet format, which was defined as:
  182. // message MessageSet {
  183. // repeated group Item = 1 {
  184. // required int32 type_id = 2;
  185. // required string message = 3;
  186. // }
  187. // }
  188. static constexpr int kMessageSetItemNumber = 1;
  189. static constexpr int kMessageSetTypeIdNumber = 2;
  190. static constexpr int kMessageSetMessageNumber = 3;
  191. static const int kMessageSetItemStartTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
  192. kMessageSetItemNumber, WireFormatLite::WIRETYPE_START_GROUP);
  193. static const int kMessageSetItemEndTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
  194. kMessageSetItemNumber, WireFormatLite::WIRETYPE_END_GROUP);
  195. static const int kMessageSetTypeIdTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
  196. kMessageSetTypeIdNumber, WireFormatLite::WIRETYPE_VARINT);
  197. static const int kMessageSetMessageTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
  198. kMessageSetMessageNumber, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  199. // Byte size of all tags of a MessageSet::Item combined.
  200. static const size_t kMessageSetItemTagsSize;
  201. // Helper functions for converting between floats/doubles and IEEE-754
  202. // uint32s/uint64s so that they can be written. (Assumes your platform
  203. // uses IEEE-754 floats.)
  204. static uint32_t EncodeFloat(float value);
  205. static float DecodeFloat(uint32_t value);
  206. static uint64_t EncodeDouble(double value);
  207. static double DecodeDouble(uint64_t value);
  208. // Helper functions for mapping signed integers to unsigned integers in
  209. // such a way that numbers with small magnitudes will encode to smaller
  210. // varints. If you simply static_cast a negative number to an unsigned
  211. // number and varint-encode it, it will always take 10 bytes, defeating
  212. // the purpose of varint. So, for the "sint32" and "sint64" field types,
  213. // we ZigZag-encode the values.
  214. static uint32_t ZigZagEncode32(int32_t n);
  215. static int32_t ZigZagDecode32(uint32_t n);
  216. static uint64_t ZigZagEncode64(int64_t n);
  217. static int64_t ZigZagDecode64(uint64_t n);
  218. // =================================================================
  219. // Methods for reading/writing individual field.
  220. // Read fields, not including tags. The assumption is that you already
  221. // read the tag to determine what field to read.
  222. // For primitive fields, we just use a templatized routine parameterized by
  223. // the represented type and the FieldType. These are specialized with the
  224. // appropriate definition for each declared type.
  225. template <typename CType, enum FieldType DeclaredType>
  226. PROTOBUF_NDEBUG_INLINE static bool ReadPrimitive(io::CodedInputStream* input,
  227. CType* value);
  228. // Reads repeated primitive values, with optimizations for repeats.
  229. // tag_size and tag should both be compile-time constants provided by the
  230. // protocol compiler.
  231. template <typename CType, enum FieldType DeclaredType>
  232. PROTOBUF_NDEBUG_INLINE static bool ReadRepeatedPrimitive(
  233. int tag_size, uint32_t tag, io::CodedInputStream* input,
  234. RepeatedField<CType>* value);
  235. // Identical to ReadRepeatedPrimitive, except will not inline the
  236. // implementation.
  237. template <typename CType, enum FieldType DeclaredType>
  238. static bool ReadRepeatedPrimitiveNoInline(int tag_size, uint32_t tag,
  239. io::CodedInputStream* input,
  240. RepeatedField<CType>* value);
  241. // Reads a primitive value directly from the provided buffer. It returns a
  242. // pointer past the segment of data that was read.
  243. //
  244. // This is only implemented for the types with fixed wire size, e.g.
  245. // float, double, and the (s)fixed* types.
  246. template <typename CType, enum FieldType DeclaredType>
  247. PROTOBUF_NDEBUG_INLINE static const uint8_t* ReadPrimitiveFromArray(
  248. const uint8_t* buffer, CType* value);
  249. // Reads a primitive packed field.
  250. //
  251. // This is only implemented for packable types.
  252. template <typename CType, enum FieldType DeclaredType>
  253. PROTOBUF_NDEBUG_INLINE static bool ReadPackedPrimitive(
  254. io::CodedInputStream* input, RepeatedField<CType>* value);
  255. // Identical to ReadPackedPrimitive, except will not inline the
  256. // implementation.
  257. template <typename CType, enum FieldType DeclaredType>
  258. static bool ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
  259. RepeatedField<CType>* value);
  260. // Read a packed enum field. If the is_valid function is not nullptr, values
  261. // for which is_valid(value) returns false are silently dropped.
  262. static bool ReadPackedEnumNoInline(io::CodedInputStream* input,
  263. bool (*is_valid)(int),
  264. RepeatedField<int>* values);
  265. // Read a packed enum field. If the is_valid function is not nullptr, values
  266. // for which is_valid(value) returns false are appended to
  267. // unknown_fields_stream.
  268. static bool ReadPackedEnumPreserveUnknowns(
  269. io::CodedInputStream* input, int field_number, bool (*is_valid)(int),
  270. io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values);
  271. // Read a string. ReadString(..., std::string* value) requires an
  272. // existing std::string.
  273. static inline bool ReadString(io::CodedInputStream* input,
  274. std::string* value);
  275. // ReadString(..., std::string** p) is internal-only, and should only be
  276. // called from generated code. It starts by setting *p to "new std::string" if
  277. // *p == &GetEmptyStringAlreadyInited(). It then invokes
  278. // ReadString(io::CodedInputStream* input, *p). This is useful for reducing
  279. // code size.
  280. static inline bool ReadString(io::CodedInputStream* input, std::string** p);
  281. // Analogous to ReadString().
  282. static bool ReadBytes(io::CodedInputStream* input, std::string* value);
  283. static bool ReadBytes(io::CodedInputStream* input, std::string** p);
  284. enum Operation {
  285. PARSE = 0,
  286. SERIALIZE = 1,
  287. };
  288. // Returns true if the data is valid UTF-8.
  289. static bool VerifyUtf8String(const char* data, int size, Operation op,
  290. const char* field_name);
  291. template <typename MessageType>
  292. static inline bool ReadGroup(int field_number, io::CodedInputStream* input,
  293. MessageType* value);
  294. template <typename MessageType>
  295. static inline bool ReadMessage(io::CodedInputStream* input,
  296. MessageType* value);
  297. template <typename MessageType>
  298. static inline bool ReadMessageNoVirtual(io::CodedInputStream* input,
  299. MessageType* value) {
  300. return ReadMessage(input, value);
  301. }
  302. // Write a tag. The Write*() functions typically include the tag, so
  303. // normally there's no need to call this unless using the Write*NoTag()
  304. // variants.
  305. PROTOBUF_NDEBUG_INLINE static void WriteTag(int field_number, WireType type,
  306. io::CodedOutputStream* output);
  307. // Write fields, without tags.
  308. PROTOBUF_NDEBUG_INLINE static void WriteInt32NoTag(
  309. int32_t value, io::CodedOutputStream* output);
  310. PROTOBUF_NDEBUG_INLINE static void WriteInt64NoTag(
  311. int64_t value, io::CodedOutputStream* output);
  312. PROTOBUF_NDEBUG_INLINE static void WriteUInt32NoTag(
  313. uint32_t value, io::CodedOutputStream* output);
  314. PROTOBUF_NDEBUG_INLINE static void WriteUInt64NoTag(
  315. uint64_t value, io::CodedOutputStream* output);
  316. PROTOBUF_NDEBUG_INLINE static void WriteSInt32NoTag(
  317. int32_t value, io::CodedOutputStream* output);
  318. PROTOBUF_NDEBUG_INLINE static void WriteSInt64NoTag(
  319. int64_t value, io::CodedOutputStream* output);
  320. PROTOBUF_NDEBUG_INLINE static void WriteFixed32NoTag(
  321. uint32_t value, io::CodedOutputStream* output);
  322. PROTOBUF_NDEBUG_INLINE static void WriteFixed64NoTag(
  323. uint64_t value, io::CodedOutputStream* output);
  324. PROTOBUF_NDEBUG_INLINE static void WriteSFixed32NoTag(
  325. int32_t value, io::CodedOutputStream* output);
  326. PROTOBUF_NDEBUG_INLINE static void WriteSFixed64NoTag(
  327. int64_t value, io::CodedOutputStream* output);
  328. PROTOBUF_NDEBUG_INLINE static void WriteFloatNoTag(
  329. float value, io::CodedOutputStream* output);
  330. PROTOBUF_NDEBUG_INLINE static void WriteDoubleNoTag(
  331. double value, io::CodedOutputStream* output);
  332. PROTOBUF_NDEBUG_INLINE static void WriteBoolNoTag(
  333. bool value, io::CodedOutputStream* output);
  334. PROTOBUF_NDEBUG_INLINE static void WriteEnumNoTag(
  335. int value, io::CodedOutputStream* output);
  336. // Write array of primitive fields, without tags
  337. static void WriteFloatArray(const float* a, int n,
  338. io::CodedOutputStream* output);
  339. static void WriteDoubleArray(const double* a, int n,
  340. io::CodedOutputStream* output);
  341. static void WriteFixed32Array(const uint32_t* a, int n,
  342. io::CodedOutputStream* output);
  343. static void WriteFixed64Array(const uint64_t* a, int n,
  344. io::CodedOutputStream* output);
  345. static void WriteSFixed32Array(const int32_t* a, int n,
  346. io::CodedOutputStream* output);
  347. static void WriteSFixed64Array(const int64_t* a, int n,
  348. io::CodedOutputStream* output);
  349. static void WriteBoolArray(const bool* a, int n,
  350. io::CodedOutputStream* output);
  351. // Write fields, including tags.
  352. static void WriteInt32(int field_number, int32_t value,
  353. io::CodedOutputStream* output);
  354. static void WriteInt64(int field_number, int64_t value,
  355. io::CodedOutputStream* output);
  356. static void WriteUInt32(int field_number, uint32_t value,
  357. io::CodedOutputStream* output);
  358. static void WriteUInt64(int field_number, uint64_t value,
  359. io::CodedOutputStream* output);
  360. static void WriteSInt32(int field_number, int32_t value,
  361. io::CodedOutputStream* output);
  362. static void WriteSInt64(int field_number, int64_t value,
  363. io::CodedOutputStream* output);
  364. static void WriteFixed32(int field_number, uint32_t value,
  365. io::CodedOutputStream* output);
  366. static void WriteFixed64(int field_number, uint64_t value,
  367. io::CodedOutputStream* output);
  368. static void WriteSFixed32(int field_number, int32_t value,
  369. io::CodedOutputStream* output);
  370. static void WriteSFixed64(int field_number, int64_t value,
  371. io::CodedOutputStream* output);
  372. static void WriteFloat(int field_number, float value,
  373. io::CodedOutputStream* output);
  374. static void WriteDouble(int field_number, double value,
  375. io::CodedOutputStream* output);
  376. static void WriteBool(int field_number, bool value,
  377. io::CodedOutputStream* output);
  378. static void WriteEnum(int field_number, int value,
  379. io::CodedOutputStream* output);
  380. static void WriteString(int field_number, const std::string& value,
  381. io::CodedOutputStream* output);
  382. static void WriteBytes(int field_number, const std::string& value,
  383. io::CodedOutputStream* output);
  384. static void WriteStringMaybeAliased(int field_number,
  385. const std::string& value,
  386. io::CodedOutputStream* output);
  387. static void WriteBytesMaybeAliased(int field_number, const std::string& value,
  388. io::CodedOutputStream* output);
  389. static void WriteGroup(int field_number, const MessageLite& value,
  390. io::CodedOutputStream* output);
  391. static void WriteMessage(int field_number, const MessageLite& value,
  392. io::CodedOutputStream* output);
  393. // Like above, but these will check if the output stream has enough
  394. // space to write directly to a flat array.
  395. static void WriteGroupMaybeToArray(int field_number, const MessageLite& value,
  396. io::CodedOutputStream* output);
  397. static void WriteMessageMaybeToArray(int field_number,
  398. const MessageLite& value,
  399. io::CodedOutputStream* output);
  400. // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
  401. // pointer must point at an instance of MessageType, *not* a subclass (or
  402. // the subclass must not override SerializeWithCachedSizes()).
  403. template <typename MessageType>
  404. static inline void WriteGroupNoVirtual(int field_number,
  405. const MessageType& value,
  406. io::CodedOutputStream* output);
  407. template <typename MessageType>
  408. static inline void WriteMessageNoVirtual(int field_number,
  409. const MessageType& value,
  410. io::CodedOutputStream* output);
  411. // Like above, but use only *ToArray methods of CodedOutputStream.
  412. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteTagToArray(int field_number,
  413. WireType type,
  414. uint8_t* target);
  415. // Write fields, without tags.
  416. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt32NoTagToArray(
  417. int32_t value, uint8_t* target);
  418. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt64NoTagToArray(
  419. int64_t value, uint8_t* target);
  420. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt32NoTagToArray(
  421. uint32_t value, uint8_t* target);
  422. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt64NoTagToArray(
  423. uint64_t value, uint8_t* target);
  424. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt32NoTagToArray(
  425. int32_t value, uint8_t* target);
  426. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt64NoTagToArray(
  427. int64_t value, uint8_t* target);
  428. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed32NoTagToArray(
  429. uint32_t value, uint8_t* target);
  430. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed64NoTagToArray(
  431. uint64_t value, uint8_t* target);
  432. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed32NoTagToArray(
  433. int32_t value, uint8_t* target);
  434. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed64NoTagToArray(
  435. int64_t value, uint8_t* target);
  436. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFloatNoTagToArray(
  437. float value, uint8_t* target);
  438. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteDoubleNoTagToArray(
  439. double value, uint8_t* target);
  440. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteBoolNoTagToArray(bool value,
  441. uint8_t* target);
  442. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteEnumNoTagToArray(int value,
  443. uint8_t* target);
  444. // Write fields, without tags. These require that value.size() > 0.
  445. template <typename T>
  446. PROTOBUF_NDEBUG_INLINE static uint8_t* WritePrimitiveNoTagToArray(
  447. const RepeatedField<T>& value, uint8_t* (*Writer)(T, uint8_t*),
  448. uint8_t* target);
  449. template <typename T>
  450. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixedNoTagToArray(
  451. const RepeatedField<T>& value, uint8_t* (*Writer)(T, uint8_t*),
  452. uint8_t* target);
  453. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt32NoTagToArray(
  454. const RepeatedField<int32_t>& value, uint8_t* output);
  455. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt64NoTagToArray(
  456. const RepeatedField<int64_t>& value, uint8_t* output);
  457. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt32NoTagToArray(
  458. const RepeatedField<uint32_t>& value, uint8_t* output);
  459. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt64NoTagToArray(
  460. const RepeatedField<uint64_t>& value, uint8_t* output);
  461. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt32NoTagToArray(
  462. const RepeatedField<int32_t>& value, uint8_t* output);
  463. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt64NoTagToArray(
  464. const RepeatedField<int64_t>& value, uint8_t* output);
  465. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed32NoTagToArray(
  466. const RepeatedField<uint32_t>& value, uint8_t* output);
  467. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed64NoTagToArray(
  468. const RepeatedField<uint64_t>& value, uint8_t* output);
  469. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed32NoTagToArray(
  470. const RepeatedField<int32_t>& value, uint8_t* output);
  471. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed64NoTagToArray(
  472. const RepeatedField<int64_t>& value, uint8_t* output);
  473. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFloatNoTagToArray(
  474. const RepeatedField<float>& value, uint8_t* output);
  475. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteDoubleNoTagToArray(
  476. const RepeatedField<double>& value, uint8_t* output);
  477. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteBoolNoTagToArray(
  478. const RepeatedField<bool>& value, uint8_t* output);
  479. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteEnumNoTagToArray(
  480. const RepeatedField<int>& value, uint8_t* output);
  481. // Write fields, including tags.
  482. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt32ToArray(int field_number,
  483. int32_t value,
  484. uint8_t* target);
  485. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt64ToArray(int field_number,
  486. int64_t value,
  487. uint8_t* target);
  488. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt32ToArray(int field_number,
  489. uint32_t value,
  490. uint8_t* target);
  491. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt64ToArray(int field_number,
  492. uint64_t value,
  493. uint8_t* target);
  494. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt32ToArray(int field_number,
  495. int32_t value,
  496. uint8_t* target);
  497. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt64ToArray(int field_number,
  498. int64_t value,
  499. uint8_t* target);
  500. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed32ToArray(int field_number,
  501. uint32_t value,
  502. uint8_t* target);
  503. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed64ToArray(int field_number,
  504. uint64_t value,
  505. uint8_t* target);
  506. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed32ToArray(int field_number,
  507. int32_t value,
  508. uint8_t* target);
  509. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed64ToArray(int field_number,
  510. int64_t value,
  511. uint8_t* target);
  512. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFloatToArray(int field_number,
  513. float value,
  514. uint8_t* target);
  515. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteDoubleToArray(int field_number,
  516. double value,
  517. uint8_t* target);
  518. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteBoolToArray(int field_number,
  519. bool value,
  520. uint8_t* target);
  521. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteEnumToArray(int field_number,
  522. int value,
  523. uint8_t* target);
  524. template <typename T>
  525. PROTOBUF_NDEBUG_INLINE static uint8_t* WritePrimitiveToArray(
  526. int field_number, const RepeatedField<T>& value,
  527. uint8_t* (*Writer)(int, T, uint8_t*), uint8_t* target);
  528. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt32ToArray(
  529. int field_number, const RepeatedField<int32_t>& value, uint8_t* output);
  530. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteInt64ToArray(
  531. int field_number, const RepeatedField<int64_t>& value, uint8_t* output);
  532. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt32ToArray(
  533. int field_number, const RepeatedField<uint32_t>& value, uint8_t* output);
  534. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteUInt64ToArray(
  535. int field_number, const RepeatedField<uint64_t>& value, uint8_t* output);
  536. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt32ToArray(
  537. int field_number, const RepeatedField<int32_t>& value, uint8_t* output);
  538. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSInt64ToArray(
  539. int field_number, const RepeatedField<int64_t>& value, uint8_t* output);
  540. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed32ToArray(
  541. int field_number, const RepeatedField<uint32_t>& value, uint8_t* output);
  542. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFixed64ToArray(
  543. int field_number, const RepeatedField<uint64_t>& value, uint8_t* output);
  544. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed32ToArray(
  545. int field_number, const RepeatedField<int32_t>& value, uint8_t* output);
  546. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteSFixed64ToArray(
  547. int field_number, const RepeatedField<int64_t>& value, uint8_t* output);
  548. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteFloatToArray(
  549. int field_number, const RepeatedField<float>& value, uint8_t* output);
  550. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteDoubleToArray(
  551. int field_number, const RepeatedField<double>& value, uint8_t* output);
  552. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteBoolToArray(
  553. int field_number, const RepeatedField<bool>& value, uint8_t* output);
  554. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteEnumToArray(
  555. int field_number, const RepeatedField<int>& value, uint8_t* output);
  556. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteStringToArray(
  557. int field_number, const std::string& value, uint8_t* target);
  558. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteBytesToArray(
  559. int field_number, const std::string& value, uint8_t* target);
  560. // Whether to serialize deterministically (e.g., map keys are
  561. // sorted) is a property of a CodedOutputStream, and in the process
  562. // of serialization, the "ToArray" variants may be invoked. But they don't
  563. // have a CodedOutputStream available, so they get an additional parameter
  564. // telling them whether to serialize deterministically.
  565. template <typename MessageType>
  566. PROTOBUF_NDEBUG_INLINE static uint8_t* InternalWriteGroup(
  567. int field_number, const MessageType& value, uint8_t* target,
  568. io::EpsCopyOutputStream* stream);
  569. template <typename MessageType>
  570. PROTOBUF_NDEBUG_INLINE static uint8_t* InternalWriteMessage(
  571. int field_number, const MessageType& value, uint8_t* target,
  572. io::EpsCopyOutputStream* stream);
  573. // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
  574. // pointer must point at an instance of MessageType, *not* a subclass (or
  575. // the subclass must not override SerializeWithCachedSizes()).
  576. template <typename MessageType>
  577. PROTOBUF_NDEBUG_INLINE static uint8_t* InternalWriteGroupNoVirtualToArray(
  578. int field_number, const MessageType& value, uint8_t* target);
  579. template <typename MessageType>
  580. PROTOBUF_NDEBUG_INLINE static uint8_t* InternalWriteMessageNoVirtualToArray(
  581. int field_number, const MessageType& value, uint8_t* target);
  582. // For backward-compatibility, the last four methods also have versions
  583. // that are non-deterministic always.
  584. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteGroupToArray(
  585. int field_number, const MessageLite& value, uint8_t* target) {
  586. io::EpsCopyOutputStream stream(
  587. target,
  588. value.GetCachedSize() +
  589. static_cast<int>(2 * io::CodedOutputStream::VarintSize32(
  590. static_cast<uint32_t>(field_number) << 3)),
  591. io::CodedOutputStream::IsDefaultSerializationDeterministic());
  592. return InternalWriteGroup(field_number, value, target, &stream);
  593. }
  594. PROTOBUF_NDEBUG_INLINE static uint8_t* WriteMessageToArray(
  595. int field_number, const MessageLite& value, uint8_t* target) {
  596. int size = value.GetCachedSize();
  597. io::EpsCopyOutputStream stream(
  598. target,
  599. size + static_cast<int>(io::CodedOutputStream::VarintSize32(
  600. static_cast<uint32_t>(field_number) << 3) +
  601. io::CodedOutputStream::VarintSize32(size)),
  602. io::CodedOutputStream::IsDefaultSerializationDeterministic());
  603. return InternalWriteMessage(field_number, value, target, &stream);
  604. }
  605. // Compute the byte size of a field. The XxSize() functions do NOT include
  606. // the tag, so you must also call TagSize(). (This is because, for repeated
  607. // fields, you should only call TagSize() once and multiply it by the element
  608. // count, but you may have to call XxSize() for each individual element.)
  609. static inline size_t Int32Size(int32_t value);
  610. static inline size_t Int64Size(int64_t value);
  611. static inline size_t UInt32Size(uint32_t value);
  612. static inline size_t UInt64Size(uint64_t value);
  613. static inline size_t SInt32Size(int32_t value);
  614. static inline size_t SInt64Size(int64_t value);
  615. static inline size_t EnumSize(int value);
  616. static inline size_t Int32SizePlusOne(int32_t value);
  617. static inline size_t Int64SizePlusOne(int64_t value);
  618. static inline size_t UInt32SizePlusOne(uint32_t value);
  619. static inline size_t UInt64SizePlusOne(uint64_t value);
  620. static inline size_t SInt32SizePlusOne(int32_t value);
  621. static inline size_t SInt64SizePlusOne(int64_t value);
  622. static inline size_t EnumSizePlusOne(int value);
  623. static size_t Int32Size(const RepeatedField<int32_t>& value);
  624. static size_t Int64Size(const RepeatedField<int64_t>& value);
  625. static size_t UInt32Size(const RepeatedField<uint32_t>& value);
  626. static size_t UInt64Size(const RepeatedField<uint64_t>& value);
  627. static size_t SInt32Size(const RepeatedField<int32_t>& value);
  628. static size_t SInt64Size(const RepeatedField<int64_t>& value);
  629. static size_t EnumSize(const RepeatedField<int>& value);
  630. // These types always have the same size.
  631. static constexpr size_t kFixed32Size = 4;
  632. static constexpr size_t kFixed64Size = 8;
  633. static constexpr size_t kSFixed32Size = 4;
  634. static constexpr size_t kSFixed64Size = 8;
  635. static constexpr size_t kFloatSize = 4;
  636. static constexpr size_t kDoubleSize = 8;
  637. static constexpr size_t kBoolSize = 1;
  638. static inline size_t StringSize(const std::string& value);
  639. static inline size_t BytesSize(const std::string& value);
  640. template <typename MessageType>
  641. static inline size_t GroupSize(const MessageType& value);
  642. template <typename MessageType>
  643. static inline size_t MessageSize(const MessageType& value);
  644. // Like above, but de-virtualize the call to ByteSize(). The
  645. // pointer must point at an instance of MessageType, *not* a subclass (or
  646. // the subclass must not override ByteSize()).
  647. template <typename MessageType>
  648. static inline size_t GroupSizeNoVirtual(const MessageType& value);
  649. template <typename MessageType>
  650. static inline size_t MessageSizeNoVirtual(const MessageType& value);
  651. // Given the length of data, calculate the byte size of the data on the
  652. // wire if we encode the data as a length delimited field.
  653. static inline size_t LengthDelimitedSize(size_t length);
  654. private:
  655. // A helper method for the repeated primitive reader. This method has
  656. // optimizations for primitive types that have fixed size on the wire, and
  657. // can be read using potentially faster paths.
  658. template <typename CType, enum FieldType DeclaredType>
  659. PROTOBUF_NDEBUG_INLINE static bool ReadRepeatedFixedSizePrimitive(
  660. int tag_size, uint32_t tag, io::CodedInputStream* input,
  661. RepeatedField<CType>* value);
  662. // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields.
  663. template <typename CType, enum FieldType DeclaredType>
  664. PROTOBUF_NDEBUG_INLINE static bool ReadPackedFixedSizePrimitive(
  665. io::CodedInputStream* input, RepeatedField<CType>* value);
  666. static const CppType kFieldTypeToCppTypeMap[];
  667. static const WireFormatLite::WireType kWireTypeForFieldType[];
  668. static void WriteSubMessageMaybeToArray(int size, const MessageLite& value,
  669. io::CodedOutputStream* output);
  670. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite);
  671. };
  672. // A class which deals with unknown values. The default implementation just
  673. // discards them. WireFormat defines a subclass which writes to an
  674. // UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since
  675. // ExtensionSet is part of the lite library but UnknownFieldSet is not.
  676. class PROTOBUF_EXPORT FieldSkipper {
  677. public:
  678. FieldSkipper() {}
  679. virtual ~FieldSkipper() {}
  680. // Skip a field whose tag has already been consumed.
  681. virtual bool SkipField(io::CodedInputStream* input, uint32_t tag);
  682. // Skip an entire message or group, up to an end-group tag (which is consumed)
  683. // or end-of-stream.
  684. virtual bool SkipMessage(io::CodedInputStream* input);
  685. // Deal with an already-parsed unrecognized enum value. The default
  686. // implementation does nothing, but the UnknownFieldSet-based implementation
  687. // saves it as an unknown varint.
  688. virtual void SkipUnknownEnum(int field_number, int value);
  689. };
  690. // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream.
  691. class PROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper {
  692. public:
  693. explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields)
  694. : unknown_fields_(unknown_fields) {}
  695. ~CodedOutputStreamFieldSkipper() override {}
  696. // implements FieldSkipper -----------------------------------------
  697. bool SkipField(io::CodedInputStream* input, uint32_t tag) override;
  698. bool SkipMessage(io::CodedInputStream* input) override;
  699. void SkipUnknownEnum(int field_number, int value) override;
  700. protected:
  701. io::CodedOutputStream* unknown_fields_;
  702. };
  703. // inline methods ====================================================
  704. inline WireFormatLite::CppType WireFormatLite::FieldTypeToCppType(
  705. FieldType type) {
  706. return kFieldTypeToCppTypeMap[type];
  707. }
  708. constexpr inline uint32_t WireFormatLite::MakeTag(int field_number,
  709. WireType type) {
  710. return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type);
  711. }
  712. inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32_t tag) {
  713. return static_cast<WireType>(tag & kTagTypeMask);
  714. }
  715. inline int WireFormatLite::GetTagFieldNumber(uint32_t tag) {
  716. return static_cast<int>(tag >> kTagTypeBits);
  717. }
  718. inline size_t WireFormatLite::TagSize(int field_number,
  719. WireFormatLite::FieldType type) {
  720. size_t result = io::CodedOutputStream::VarintSize32(
  721. static_cast<uint32_t>(field_number << kTagTypeBits));
  722. if (type == TYPE_GROUP) {
  723. // Groups have both a start and an end tag.
  724. return result * 2;
  725. } else {
  726. return result;
  727. }
  728. }
  729. inline uint32_t WireFormatLite::EncodeFloat(float value) {
  730. return bit_cast<uint32_t>(value);
  731. }
  732. inline float WireFormatLite::DecodeFloat(uint32_t value) {
  733. return bit_cast<float>(value);
  734. }
  735. inline uint64_t WireFormatLite::EncodeDouble(double value) {
  736. return bit_cast<uint64_t>(value);
  737. }
  738. inline double WireFormatLite::DecodeDouble(uint64_t value) {
  739. return bit_cast<double>(value);
  740. }
  741. // ZigZag Transform: Encodes signed integers so that they can be
  742. // effectively used with varint encoding.
  743. //
  744. // varint operates on unsigned integers, encoding smaller numbers into
  745. // fewer bytes. If you try to use it on a signed integer, it will treat
  746. // this number as a very large unsigned integer, which means that even
  747. // small signed numbers like -1 will take the maximum number of bytes
  748. // (10) to encode. ZigZagEncode() maps signed integers to unsigned
  749. // in such a way that those with a small absolute value will have smaller
  750. // encoded values, making them appropriate for encoding using varint.
  751. //
  752. // int32_t -> uint32_t
  753. // -------------------------
  754. // 0 -> 0
  755. // -1 -> 1
  756. // 1 -> 2
  757. // -2 -> 3
  758. // ... -> ...
  759. // 2147483647 -> 4294967294
  760. // -2147483648 -> 4294967295
  761. //
  762. // >> encode >>
  763. // << decode <<
  764. inline uint32_t WireFormatLite::ZigZagEncode32(int32_t n) {
  765. // Note: the right-shift must be arithmetic
  766. // Note: left shift must be unsigned because of overflow
  767. return (static_cast<uint32_t>(n) << 1) ^ static_cast<uint32_t>(n >> 31);
  768. }
  769. inline int32_t WireFormatLite::ZigZagDecode32(uint32_t n) {
  770. // Note: Using unsigned types prevent undefined behavior
  771. return static_cast<int32_t>((n >> 1) ^ (~(n & 1) + 1));
  772. }
  773. inline uint64_t WireFormatLite::ZigZagEncode64(int64_t n) {
  774. // Note: the right-shift must be arithmetic
  775. // Note: left shift must be unsigned because of overflow
  776. return (static_cast<uint64_t>(n) << 1) ^ static_cast<uint64_t>(n >> 63);
  777. }
  778. inline int64_t WireFormatLite::ZigZagDecode64(uint64_t n) {
  779. // Note: Using unsigned types prevent undefined behavior
  780. return static_cast<int64_t>((n >> 1) ^ (~(n & 1) + 1));
  781. }
  782. // String is for UTF-8 text only, but, even so, ReadString() can simply
  783. // call ReadBytes().
  784. inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
  785. std::string* value) {
  786. return ReadBytes(input, value);
  787. }
  788. inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
  789. std::string** p) {
  790. return ReadBytes(input, p);
  791. }
  792. inline uint8_t* InternalSerializeUnknownMessageSetItemsToArray(
  793. const std::string& unknown_fields, uint8_t* target,
  794. io::EpsCopyOutputStream* stream) {
  795. return stream->WriteRaw(unknown_fields.data(),
  796. static_cast<int>(unknown_fields.size()), target);
  797. }
  798. inline size_t ComputeUnknownMessageSetItemsSize(
  799. const std::string& unknown_fields) {
  800. return unknown_fields.size();
  801. }
  802. // Implementation details of ReadPrimitive.
  803. template <>
  804. inline bool WireFormatLite::ReadPrimitive<int32_t, WireFormatLite::TYPE_INT32>(
  805. io::CodedInputStream* input, int32_t* value) {
  806. uint32_t temp;
  807. if (!input->ReadVarint32(&temp)) return false;
  808. *value = static_cast<int32_t>(temp);
  809. return true;
  810. }
  811. template <>
  812. inline bool WireFormatLite::ReadPrimitive<int64_t, WireFormatLite::TYPE_INT64>(
  813. io::CodedInputStream* input, int64_t* value) {
  814. uint64_t temp;
  815. if (!input->ReadVarint64(&temp)) return false;
  816. *value = static_cast<int64_t>(temp);
  817. return true;
  818. }
  819. template <>
  820. inline bool
  821. WireFormatLite::ReadPrimitive<uint32_t, WireFormatLite::TYPE_UINT32>(
  822. io::CodedInputStream* input, uint32_t* value) {
  823. return input->ReadVarint32(value);
  824. }
  825. template <>
  826. inline bool
  827. WireFormatLite::ReadPrimitive<uint64_t, WireFormatLite::TYPE_UINT64>(
  828. io::CodedInputStream* input, uint64_t* value) {
  829. return input->ReadVarint64(value);
  830. }
  831. template <>
  832. inline bool WireFormatLite::ReadPrimitive<int32_t, WireFormatLite::TYPE_SINT32>(
  833. io::CodedInputStream* input, int32_t* value) {
  834. uint32_t temp;
  835. if (!input->ReadVarint32(&temp)) return false;
  836. *value = ZigZagDecode32(temp);
  837. return true;
  838. }
  839. template <>
  840. inline bool WireFormatLite::ReadPrimitive<int64_t, WireFormatLite::TYPE_SINT64>(
  841. io::CodedInputStream* input, int64_t* value) {
  842. uint64_t temp;
  843. if (!input->ReadVarint64(&temp)) return false;
  844. *value = ZigZagDecode64(temp);
  845. return true;
  846. }
  847. template <>
  848. inline bool
  849. WireFormatLite::ReadPrimitive<uint32_t, WireFormatLite::TYPE_FIXED32>(
  850. io::CodedInputStream* input, uint32_t* value) {
  851. return input->ReadLittleEndian32(value);
  852. }
  853. template <>
  854. inline bool
  855. WireFormatLite::ReadPrimitive<uint64_t, WireFormatLite::TYPE_FIXED64>(
  856. io::CodedInputStream* input, uint64_t* value) {
  857. return input->ReadLittleEndian64(value);
  858. }
  859. template <>
  860. inline bool
  861. WireFormatLite::ReadPrimitive<int32_t, WireFormatLite::TYPE_SFIXED32>(
  862. io::CodedInputStream* input, int32_t* value) {
  863. uint32_t temp;
  864. if (!input->ReadLittleEndian32(&temp)) return false;
  865. *value = static_cast<int32_t>(temp);
  866. return true;
  867. }
  868. template <>
  869. inline bool
  870. WireFormatLite::ReadPrimitive<int64_t, WireFormatLite::TYPE_SFIXED64>(
  871. io::CodedInputStream* input, int64_t* value) {
  872. uint64_t temp;
  873. if (!input->ReadLittleEndian64(&temp)) return false;
  874. *value = static_cast<int64_t>(temp);
  875. return true;
  876. }
  877. template <>
  878. inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
  879. io::CodedInputStream* input, float* value) {
  880. uint32_t temp;
  881. if (!input->ReadLittleEndian32(&temp)) return false;
  882. *value = DecodeFloat(temp);
  883. return true;
  884. }
  885. template <>
  886. inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
  887. io::CodedInputStream* input, double* value) {
  888. uint64_t temp;
  889. if (!input->ReadLittleEndian64(&temp)) return false;
  890. *value = DecodeDouble(temp);
  891. return true;
  892. }
  893. template <>
  894. inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
  895. io::CodedInputStream* input, bool* value) {
  896. uint64_t temp;
  897. if (!input->ReadVarint64(&temp)) return false;
  898. *value = temp != 0;
  899. return true;
  900. }
  901. template <>
  902. inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  903. io::CodedInputStream* input, int* value) {
  904. uint32_t temp;
  905. if (!input->ReadVarint32(&temp)) return false;
  906. *value = static_cast<int>(temp);
  907. return true;
  908. }
  909. template <>
  910. inline const uint8_t*
  911. WireFormatLite::ReadPrimitiveFromArray<uint32_t, WireFormatLite::TYPE_FIXED32>(
  912. const uint8_t* buffer, uint32_t* value) {
  913. return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
  914. }
  915. template <>
  916. inline const uint8_t*
  917. WireFormatLite::ReadPrimitiveFromArray<uint64_t, WireFormatLite::TYPE_FIXED64>(
  918. const uint8_t* buffer, uint64_t* value) {
  919. return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
  920. }
  921. template <>
  922. inline const uint8_t*
  923. WireFormatLite::ReadPrimitiveFromArray<int32_t, WireFormatLite::TYPE_SFIXED32>(
  924. const uint8_t* buffer, int32_t* value) {
  925. uint32_t temp;
  926. buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
  927. *value = static_cast<int32_t>(temp);
  928. return buffer;
  929. }
  930. template <>
  931. inline const uint8_t*
  932. WireFormatLite::ReadPrimitiveFromArray<int64_t, WireFormatLite::TYPE_SFIXED64>(
  933. const uint8_t* buffer, int64_t* value) {
  934. uint64_t temp;
  935. buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
  936. *value = static_cast<int64_t>(temp);
  937. return buffer;
  938. }
  939. template <>
  940. inline const uint8_t*
  941. WireFormatLite::ReadPrimitiveFromArray<float, WireFormatLite::TYPE_FLOAT>(
  942. const uint8_t* buffer, float* value) {
  943. uint32_t temp;
  944. buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
  945. *value = DecodeFloat(temp);
  946. return buffer;
  947. }
  948. template <>
  949. inline const uint8_t*
  950. WireFormatLite::ReadPrimitiveFromArray<double, WireFormatLite::TYPE_DOUBLE>(
  951. const uint8_t* buffer, double* value) {
  952. uint64_t temp;
  953. buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
  954. *value = DecodeDouble(temp);
  955. return buffer;
  956. }
  957. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  958. inline bool WireFormatLite::ReadRepeatedPrimitive(
  959. int, // tag_size, unused.
  960. uint32_t tag, io::CodedInputStream* input, RepeatedField<CType>* values) {
  961. CType value;
  962. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  963. values->Add(value);
  964. int elements_already_reserved = values->Capacity() - values->size();
  965. while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
  966. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  967. values->AddAlreadyReserved(value);
  968. elements_already_reserved--;
  969. }
  970. return true;
  971. }
  972. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  973. inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
  974. int tag_size, uint32_t tag, io::CodedInputStream* input,
  975. RepeatedField<CType>* values) {
  976. GOOGLE_DCHECK_EQ(UInt32Size(tag), static_cast<size_t>(tag_size));
  977. CType value;
  978. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  979. values->Add(value);
  980. // For fixed size values, repeated values can be read more quickly by
  981. // reading directly from a raw array.
  982. //
  983. // We can get a tight loop by only reading as many elements as can be
  984. // added to the RepeatedField without having to do any resizing. Additionally,
  985. // we only try to read as many elements as are available from the current
  986. // buffer space. Doing so avoids having to perform boundary checks when
  987. // reading the value: the maximum number of elements that can be read is
  988. // known outside of the loop.
  989. const void* void_pointer;
  990. int size;
  991. input->GetDirectBufferPointerInline(&void_pointer, &size);
  992. if (size > 0) {
  993. const uint8_t* buffer = reinterpret_cast<const uint8_t*>(void_pointer);
  994. // The number of bytes each type occupies on the wire.
  995. const int per_value_size = tag_size + static_cast<int>(sizeof(value));
  996. // parentheses around (std::min) prevents macro expansion of min(...)
  997. int elements_available =
  998. (std::min)(values->Capacity() - values->size(), size / per_value_size);
  999. int num_read = 0;
  1000. while (num_read < elements_available &&
  1001. (buffer = io::CodedInputStream::ExpectTagFromArray(buffer, tag)) !=
  1002. nullptr) {
  1003. buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
  1004. values->AddAlreadyReserved(value);
  1005. ++num_read;
  1006. }
  1007. const int read_bytes = num_read * per_value_size;
  1008. if (read_bytes > 0) {
  1009. input->Skip(read_bytes);
  1010. }
  1011. }
  1012. return true;
  1013. }
  1014. // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
  1015. // the optimized code path.
  1016. #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
  1017. template <> \
  1018. inline bool WireFormatLite::ReadRepeatedPrimitive< \
  1019. CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
  1020. int tag_size, uint32_t tag, io::CodedInputStream* input, \
  1021. RepeatedField<CPPTYPE>* values) { \
  1022. return ReadRepeatedFixedSizePrimitive<CPPTYPE, \
  1023. WireFormatLite::DECLARED_TYPE>( \
  1024. tag_size, tag, input, values); \
  1025. }
  1026. READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32_t, TYPE_FIXED32)
  1027. READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64_t, TYPE_FIXED64)
  1028. READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32_t, TYPE_SFIXED32)
  1029. READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64_t, TYPE_SFIXED64)
  1030. READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
  1031. READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
  1032. #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
  1033. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  1034. bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
  1035. int tag_size, uint32_t tag, io::CodedInputStream* input,
  1036. RepeatedField<CType>* value) {
  1037. return ReadRepeatedPrimitive<CType, DeclaredType>(tag_size, tag, input,
  1038. value);
  1039. }
  1040. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  1041. inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
  1042. RepeatedField<CType>* values) {
  1043. int length;
  1044. if (!input->ReadVarintSizeAsInt(&length)) return false;
  1045. io::CodedInputStream::Limit limit = input->PushLimit(length);
  1046. while (input->BytesUntilLimit() > 0) {
  1047. CType value;
  1048. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  1049. values->Add(value);
  1050. }
  1051. input->PopLimit(limit);
  1052. return true;
  1053. }
  1054. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  1055. inline bool WireFormatLite::ReadPackedFixedSizePrimitive(
  1056. io::CodedInputStream* input, RepeatedField<CType>* values) {
  1057. int length;
  1058. if (!input->ReadVarintSizeAsInt(&length)) return false;
  1059. const int old_entries = values->size();
  1060. const int new_entries = length / static_cast<int>(sizeof(CType));
  1061. const int new_bytes = new_entries * static_cast<int>(sizeof(CType));
  1062. if (new_bytes != length) return false;
  1063. // We would *like* to pre-allocate the buffer to write into (for
  1064. // speed), but *must* avoid performing a very large allocation due
  1065. // to a malicious user-supplied "length" above. So we have a fast
  1066. // path that pre-allocates when the "length" is less than a bound.
  1067. // We determine the bound by calling BytesUntilTotalBytesLimit() and
  1068. // BytesUntilLimit(). These return -1 to mean "no limit set".
  1069. // There are four cases:
  1070. // TotalBytesLimit Limit
  1071. // -1 -1 Use slow path.
  1072. // -1 >= 0 Use fast path if length <= Limit.
  1073. // >= 0 -1 Use slow path.
  1074. // >= 0 >= 0 Use fast path if length <= min(both limits).
  1075. int64_t bytes_limit = input->BytesUntilTotalBytesLimit();
  1076. if (bytes_limit == -1) {
  1077. bytes_limit = input->BytesUntilLimit();
  1078. } else {
  1079. // parentheses around (std::min) prevents macro expansion of min(...)
  1080. bytes_limit =
  1081. (std::min)(bytes_limit, static_cast<int64_t>(input->BytesUntilLimit()));
  1082. }
  1083. if (bytes_limit >= new_bytes) {
  1084. // Fast-path that pre-allocates *values to the final size.
  1085. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1086. values->Resize(old_entries + new_entries, 0);
  1087. // values->mutable_data() may change after Resize(), so do this after:
  1088. void* dest = reinterpret_cast<void*>(values->mutable_data() + old_entries);
  1089. if (!input->ReadRaw(dest, new_bytes)) {
  1090. values->Truncate(old_entries);
  1091. return false;
  1092. }
  1093. #else
  1094. values->Reserve(old_entries + new_entries);
  1095. CType value;
  1096. for (int i = 0; i < new_entries; ++i) {
  1097. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  1098. values->AddAlreadyReserved(value);
  1099. }
  1100. #endif
  1101. } else {
  1102. // This is the slow-path case where "length" may be too large to
  1103. // safely allocate. We read as much as we can into *values
  1104. // without pre-allocating "length" bytes.
  1105. CType value;
  1106. for (int i = 0; i < new_entries; ++i) {
  1107. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  1108. values->Add(value);
  1109. }
  1110. }
  1111. return true;
  1112. }
  1113. // Specializations of ReadPackedPrimitive for the fixed size types, which use
  1114. // an optimized code path.
  1115. #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
  1116. template <> \
  1117. inline bool \
  1118. WireFormatLite::ReadPackedPrimitive<CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
  1119. io::CodedInputStream * input, RepeatedField<CPPTYPE> * values) { \
  1120. return ReadPackedFixedSizePrimitive<CPPTYPE, \
  1121. WireFormatLite::DECLARED_TYPE>( \
  1122. input, values); \
  1123. }
  1124. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32_t, TYPE_FIXED32)
  1125. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64_t, TYPE_FIXED64)
  1126. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int32_t, TYPE_SFIXED32)
  1127. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int64_t, TYPE_SFIXED64)
  1128. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
  1129. READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
  1130. #undef READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE
  1131. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  1132. bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
  1133. RepeatedField<CType>* values) {
  1134. return ReadPackedPrimitive<CType, DeclaredType>(input, values);
  1135. }
  1136. template <typename MessageType>
  1137. inline bool WireFormatLite::ReadGroup(int field_number,
  1138. io::CodedInputStream* input,
  1139. MessageType* value) {
  1140. if (!input->IncrementRecursionDepth()) return false;
  1141. if (!value->MergePartialFromCodedStream(input)) return false;
  1142. input->UnsafeDecrementRecursionDepth();
  1143. // Make sure the last thing read was an end tag for this group.
  1144. if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. template <typename MessageType>
  1150. inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
  1151. MessageType* value) {
  1152. int length;
  1153. if (!input->ReadVarintSizeAsInt(&length)) return false;
  1154. std::pair<io::CodedInputStream::Limit, int> p =
  1155. input->IncrementRecursionDepthAndPushLimit(length);
  1156. if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false;
  1157. // Make sure that parsing stopped when the limit was hit, not at an endgroup
  1158. // tag.
  1159. return input->DecrementRecursionDepthAndPopLimit(p.first);
  1160. }
  1161. // ===================================================================
  1162. inline void WireFormatLite::WriteTag(int field_number, WireType type,
  1163. io::CodedOutputStream* output) {
  1164. output->WriteTag(MakeTag(field_number, type));
  1165. }
  1166. inline void WireFormatLite::WriteInt32NoTag(int32_t value,
  1167. io::CodedOutputStream* output) {
  1168. output->WriteVarint32SignExtended(value);
  1169. }
  1170. inline void WireFormatLite::WriteInt64NoTag(int64_t value,
  1171. io::CodedOutputStream* output) {
  1172. output->WriteVarint64(static_cast<uint64_t>(value));
  1173. }
  1174. inline void WireFormatLite::WriteUInt32NoTag(uint32_t value,
  1175. io::CodedOutputStream* output) {
  1176. output->WriteVarint32(value);
  1177. }
  1178. inline void WireFormatLite::WriteUInt64NoTag(uint64_t value,
  1179. io::CodedOutputStream* output) {
  1180. output->WriteVarint64(value);
  1181. }
  1182. inline void WireFormatLite::WriteSInt32NoTag(int32_t value,
  1183. io::CodedOutputStream* output) {
  1184. output->WriteVarint32(ZigZagEncode32(value));
  1185. }
  1186. inline void WireFormatLite::WriteSInt64NoTag(int64_t value,
  1187. io::CodedOutputStream* output) {
  1188. output->WriteVarint64(ZigZagEncode64(value));
  1189. }
  1190. inline void WireFormatLite::WriteFixed32NoTag(uint32_t value,
  1191. io::CodedOutputStream* output) {
  1192. output->WriteLittleEndian32(value);
  1193. }
  1194. inline void WireFormatLite::WriteFixed64NoTag(uint64_t value,
  1195. io::CodedOutputStream* output) {
  1196. output->WriteLittleEndian64(value);
  1197. }
  1198. inline void WireFormatLite::WriteSFixed32NoTag(int32_t value,
  1199. io::CodedOutputStream* output) {
  1200. output->WriteLittleEndian32(static_cast<uint32_t>(value));
  1201. }
  1202. inline void WireFormatLite::WriteSFixed64NoTag(int64_t value,
  1203. io::CodedOutputStream* output) {
  1204. output->WriteLittleEndian64(static_cast<uint64_t>(value));
  1205. }
  1206. inline void WireFormatLite::WriteFloatNoTag(float value,
  1207. io::CodedOutputStream* output) {
  1208. output->WriteLittleEndian32(EncodeFloat(value));
  1209. }
  1210. inline void WireFormatLite::WriteDoubleNoTag(double value,
  1211. io::CodedOutputStream* output) {
  1212. output->WriteLittleEndian64(EncodeDouble(value));
  1213. }
  1214. inline void WireFormatLite::WriteBoolNoTag(bool value,
  1215. io::CodedOutputStream* output) {
  1216. output->WriteVarint32(value ? 1 : 0);
  1217. }
  1218. inline void WireFormatLite::WriteEnumNoTag(int value,
  1219. io::CodedOutputStream* output) {
  1220. output->WriteVarint32SignExtended(value);
  1221. }
  1222. // See comment on ReadGroupNoVirtual to understand the need for this template
  1223. // parameter name.
  1224. template <typename MessageType_WorkAroundCppLookupDefect>
  1225. inline void WireFormatLite::WriteGroupNoVirtual(
  1226. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  1227. io::CodedOutputStream* output) {
  1228. WriteTag(field_number, WIRETYPE_START_GROUP, output);
  1229. value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
  1230. WriteTag(field_number, WIRETYPE_END_GROUP, output);
  1231. }
  1232. template <typename MessageType_WorkAroundCppLookupDefect>
  1233. inline void WireFormatLite::WriteMessageNoVirtual(
  1234. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  1235. io::CodedOutputStream* output) {
  1236. WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
  1237. output->WriteVarint32(
  1238. value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
  1239. value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
  1240. }
  1241. // ===================================================================
  1242. inline uint8_t* WireFormatLite::WriteTagToArray(int field_number, WireType type,
  1243. uint8_t* target) {
  1244. return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
  1245. target);
  1246. }
  1247. inline uint8_t* WireFormatLite::WriteInt32NoTagToArray(int32_t value,
  1248. uint8_t* target) {
  1249. return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
  1250. }
  1251. inline uint8_t* WireFormatLite::WriteInt64NoTagToArray(int64_t value,
  1252. uint8_t* target) {
  1253. return io::CodedOutputStream::WriteVarint64ToArray(
  1254. static_cast<uint64_t>(value), target);
  1255. }
  1256. inline uint8_t* WireFormatLite::WriteUInt32NoTagToArray(uint32_t value,
  1257. uint8_t* target) {
  1258. return io::CodedOutputStream::WriteVarint32ToArray(value, target);
  1259. }
  1260. inline uint8_t* WireFormatLite::WriteUInt64NoTagToArray(uint64_t value,
  1261. uint8_t* target) {
  1262. return io::CodedOutputStream::WriteVarint64ToArray(value, target);
  1263. }
  1264. inline uint8_t* WireFormatLite::WriteSInt32NoTagToArray(int32_t value,
  1265. uint8_t* target) {
  1266. return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
  1267. target);
  1268. }
  1269. inline uint8_t* WireFormatLite::WriteSInt64NoTagToArray(int64_t value,
  1270. uint8_t* target) {
  1271. return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
  1272. target);
  1273. }
  1274. inline uint8_t* WireFormatLite::WriteFixed32NoTagToArray(uint32_t value,
  1275. uint8_t* target) {
  1276. return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
  1277. }
  1278. inline uint8_t* WireFormatLite::WriteFixed64NoTagToArray(uint64_t value,
  1279. uint8_t* target) {
  1280. return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
  1281. }
  1282. inline uint8_t* WireFormatLite::WriteSFixed32NoTagToArray(int32_t value,
  1283. uint8_t* target) {
  1284. return io::CodedOutputStream::WriteLittleEndian32ToArray(
  1285. static_cast<uint32_t>(value), target);
  1286. }
  1287. inline uint8_t* WireFormatLite::WriteSFixed64NoTagToArray(int64_t value,
  1288. uint8_t* target) {
  1289. return io::CodedOutputStream::WriteLittleEndian64ToArray(
  1290. static_cast<uint64_t>(value), target);
  1291. }
  1292. inline uint8_t* WireFormatLite::WriteFloatNoTagToArray(float value,
  1293. uint8_t* target) {
  1294. return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
  1295. target);
  1296. }
  1297. inline uint8_t* WireFormatLite::WriteDoubleNoTagToArray(double value,
  1298. uint8_t* target) {
  1299. return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
  1300. target);
  1301. }
  1302. inline uint8_t* WireFormatLite::WriteBoolNoTagToArray(bool value,
  1303. uint8_t* target) {
  1304. return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
  1305. }
  1306. inline uint8_t* WireFormatLite::WriteEnumNoTagToArray(int value,
  1307. uint8_t* target) {
  1308. return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
  1309. }
  1310. template <typename T>
  1311. inline uint8_t* WireFormatLite::WritePrimitiveNoTagToArray(
  1312. const RepeatedField<T>& value, uint8_t* (*Writer)(T, uint8_t*),
  1313. uint8_t* target) {
  1314. const int n = value.size();
  1315. GOOGLE_DCHECK_GT(n, 0);
  1316. const T* ii = value.data();
  1317. int i = 0;
  1318. do {
  1319. target = Writer(ii[i], target);
  1320. } while (++i < n);
  1321. return target;
  1322. }
  1323. template <typename T>
  1324. inline uint8_t* WireFormatLite::WriteFixedNoTagToArray(
  1325. const RepeatedField<T>& value, uint8_t* (*Writer)(T, uint8_t*),
  1326. uint8_t* target) {
  1327. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1328. (void)Writer;
  1329. const int n = value.size();
  1330. GOOGLE_DCHECK_GT(n, 0);
  1331. const T* ii = value.data();
  1332. const int bytes = n * static_cast<int>(sizeof(ii[0]));
  1333. memcpy(target, ii, static_cast<size_t>(bytes));
  1334. return target + bytes;
  1335. #else
  1336. return WritePrimitiveNoTagToArray(value, Writer, target);
  1337. #endif
  1338. }
  1339. inline uint8_t* WireFormatLite::WriteInt32NoTagToArray(
  1340. const RepeatedField<int32_t>& value, uint8_t* target) {
  1341. return WritePrimitiveNoTagToArray(value, WriteInt32NoTagToArray, target);
  1342. }
  1343. inline uint8_t* WireFormatLite::WriteInt64NoTagToArray(
  1344. const RepeatedField<int64_t>& value, uint8_t* target) {
  1345. return WritePrimitiveNoTagToArray(value, WriteInt64NoTagToArray, target);
  1346. }
  1347. inline uint8_t* WireFormatLite::WriteUInt32NoTagToArray(
  1348. const RepeatedField<uint32_t>& value, uint8_t* target) {
  1349. return WritePrimitiveNoTagToArray(value, WriteUInt32NoTagToArray, target);
  1350. }
  1351. inline uint8_t* WireFormatLite::WriteUInt64NoTagToArray(
  1352. const RepeatedField<uint64_t>& value, uint8_t* target) {
  1353. return WritePrimitiveNoTagToArray(value, WriteUInt64NoTagToArray, target);
  1354. }
  1355. inline uint8_t* WireFormatLite::WriteSInt32NoTagToArray(
  1356. const RepeatedField<int32_t>& value, uint8_t* target) {
  1357. return WritePrimitiveNoTagToArray(value, WriteSInt32NoTagToArray, target);
  1358. }
  1359. inline uint8_t* WireFormatLite::WriteSInt64NoTagToArray(
  1360. const RepeatedField<int64_t>& value, uint8_t* target) {
  1361. return WritePrimitiveNoTagToArray(value, WriteSInt64NoTagToArray, target);
  1362. }
  1363. inline uint8_t* WireFormatLite::WriteFixed32NoTagToArray(
  1364. const RepeatedField<uint32_t>& value, uint8_t* target) {
  1365. return WriteFixedNoTagToArray(value, WriteFixed32NoTagToArray, target);
  1366. }
  1367. inline uint8_t* WireFormatLite::WriteFixed64NoTagToArray(
  1368. const RepeatedField<uint64_t>& value, uint8_t* target) {
  1369. return WriteFixedNoTagToArray(value, WriteFixed64NoTagToArray, target);
  1370. }
  1371. inline uint8_t* WireFormatLite::WriteSFixed32NoTagToArray(
  1372. const RepeatedField<int32_t>& value, uint8_t* target) {
  1373. return WriteFixedNoTagToArray(value, WriteSFixed32NoTagToArray, target);
  1374. }
  1375. inline uint8_t* WireFormatLite::WriteSFixed64NoTagToArray(
  1376. const RepeatedField<int64_t>& value, uint8_t* target) {
  1377. return WriteFixedNoTagToArray(value, WriteSFixed64NoTagToArray, target);
  1378. }
  1379. inline uint8_t* WireFormatLite::WriteFloatNoTagToArray(
  1380. const RepeatedField<float>& value, uint8_t* target) {
  1381. return WriteFixedNoTagToArray(value, WriteFloatNoTagToArray, target);
  1382. }
  1383. inline uint8_t* WireFormatLite::WriteDoubleNoTagToArray(
  1384. const RepeatedField<double>& value, uint8_t* target) {
  1385. return WriteFixedNoTagToArray(value, WriteDoubleNoTagToArray, target);
  1386. }
  1387. inline uint8_t* WireFormatLite::WriteBoolNoTagToArray(
  1388. const RepeatedField<bool>& value, uint8_t* target) {
  1389. return WritePrimitiveNoTagToArray(value, WriteBoolNoTagToArray, target);
  1390. }
  1391. inline uint8_t* WireFormatLite::WriteEnumNoTagToArray(
  1392. const RepeatedField<int>& value, uint8_t* target) {
  1393. return WritePrimitiveNoTagToArray(value, WriteEnumNoTagToArray, target);
  1394. }
  1395. inline uint8_t* WireFormatLite::WriteInt32ToArray(int field_number,
  1396. int32_t value,
  1397. uint8_t* target) {
  1398. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1399. return WriteInt32NoTagToArray(value, target);
  1400. }
  1401. inline uint8_t* WireFormatLite::WriteInt64ToArray(int field_number,
  1402. int64_t value,
  1403. uint8_t* target) {
  1404. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1405. return WriteInt64NoTagToArray(value, target);
  1406. }
  1407. inline uint8_t* WireFormatLite::WriteUInt32ToArray(int field_number,
  1408. uint32_t value,
  1409. uint8_t* target) {
  1410. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1411. return WriteUInt32NoTagToArray(value, target);
  1412. }
  1413. inline uint8_t* WireFormatLite::WriteUInt64ToArray(int field_number,
  1414. uint64_t value,
  1415. uint8_t* target) {
  1416. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1417. return WriteUInt64NoTagToArray(value, target);
  1418. }
  1419. inline uint8_t* WireFormatLite::WriteSInt32ToArray(int field_number,
  1420. int32_t value,
  1421. uint8_t* target) {
  1422. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1423. return WriteSInt32NoTagToArray(value, target);
  1424. }
  1425. inline uint8_t* WireFormatLite::WriteSInt64ToArray(int field_number,
  1426. int64_t value,
  1427. uint8_t* target) {
  1428. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1429. return WriteSInt64NoTagToArray(value, target);
  1430. }
  1431. inline uint8_t* WireFormatLite::WriteFixed32ToArray(int field_number,
  1432. uint32_t value,
  1433. uint8_t* target) {
  1434. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  1435. return WriteFixed32NoTagToArray(value, target);
  1436. }
  1437. inline uint8_t* WireFormatLite::WriteFixed64ToArray(int field_number,
  1438. uint64_t value,
  1439. uint8_t* target) {
  1440. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  1441. return WriteFixed64NoTagToArray(value, target);
  1442. }
  1443. inline uint8_t* WireFormatLite::WriteSFixed32ToArray(int field_number,
  1444. int32_t value,
  1445. uint8_t* target) {
  1446. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  1447. return WriteSFixed32NoTagToArray(value, target);
  1448. }
  1449. inline uint8_t* WireFormatLite::WriteSFixed64ToArray(int field_number,
  1450. int64_t value,
  1451. uint8_t* target) {
  1452. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  1453. return WriteSFixed64NoTagToArray(value, target);
  1454. }
  1455. inline uint8_t* WireFormatLite::WriteFloatToArray(int field_number, float value,
  1456. uint8_t* target) {
  1457. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  1458. return WriteFloatNoTagToArray(value, target);
  1459. }
  1460. inline uint8_t* WireFormatLite::WriteDoubleToArray(int field_number,
  1461. double value,
  1462. uint8_t* target) {
  1463. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  1464. return WriteDoubleNoTagToArray(value, target);
  1465. }
  1466. inline uint8_t* WireFormatLite::WriteBoolToArray(int field_number, bool value,
  1467. uint8_t* target) {
  1468. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1469. return WriteBoolNoTagToArray(value, target);
  1470. }
  1471. inline uint8_t* WireFormatLite::WriteEnumToArray(int field_number, int value,
  1472. uint8_t* target) {
  1473. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  1474. return WriteEnumNoTagToArray(value, target);
  1475. }
  1476. template <typename T>
  1477. inline uint8_t* WireFormatLite::WritePrimitiveToArray(
  1478. int field_number, const RepeatedField<T>& value,
  1479. uint8_t* (*Writer)(int, T, uint8_t*), uint8_t* target) {
  1480. const int n = value.size();
  1481. if (n == 0) {
  1482. return target;
  1483. }
  1484. const T* ii = value.data();
  1485. int i = 0;
  1486. do {
  1487. target = Writer(field_number, ii[i], target);
  1488. } while (++i < n);
  1489. return target;
  1490. }
  1491. inline uint8_t* WireFormatLite::WriteInt32ToArray(
  1492. int field_number, const RepeatedField<int32_t>& value, uint8_t* target) {
  1493. return WritePrimitiveToArray(field_number, value, WriteInt32ToArray, target);
  1494. }
  1495. inline uint8_t* WireFormatLite::WriteInt64ToArray(
  1496. int field_number, const RepeatedField<int64_t>& value, uint8_t* target) {
  1497. return WritePrimitiveToArray(field_number, value, WriteInt64ToArray, target);
  1498. }
  1499. inline uint8_t* WireFormatLite::WriteUInt32ToArray(
  1500. int field_number, const RepeatedField<uint32_t>& value, uint8_t* target) {
  1501. return WritePrimitiveToArray(field_number, value, WriteUInt32ToArray, target);
  1502. }
  1503. inline uint8_t* WireFormatLite::WriteUInt64ToArray(
  1504. int field_number, const RepeatedField<uint64_t>& value, uint8_t* target) {
  1505. return WritePrimitiveToArray(field_number, value, WriteUInt64ToArray, target);
  1506. }
  1507. inline uint8_t* WireFormatLite::WriteSInt32ToArray(
  1508. int field_number, const RepeatedField<int32_t>& value, uint8_t* target) {
  1509. return WritePrimitiveToArray(field_number, value, WriteSInt32ToArray, target);
  1510. }
  1511. inline uint8_t* WireFormatLite::WriteSInt64ToArray(
  1512. int field_number, const RepeatedField<int64_t>& value, uint8_t* target) {
  1513. return WritePrimitiveToArray(field_number, value, WriteSInt64ToArray, target);
  1514. }
  1515. inline uint8_t* WireFormatLite::WriteFixed32ToArray(
  1516. int field_number, const RepeatedField<uint32_t>& value, uint8_t* target) {
  1517. return WritePrimitiveToArray(field_number, value, WriteFixed32ToArray,
  1518. target);
  1519. }
  1520. inline uint8_t* WireFormatLite::WriteFixed64ToArray(
  1521. int field_number, const RepeatedField<uint64_t>& value, uint8_t* target) {
  1522. return WritePrimitiveToArray(field_number, value, WriteFixed64ToArray,
  1523. target);
  1524. }
  1525. inline uint8_t* WireFormatLite::WriteSFixed32ToArray(
  1526. int field_number, const RepeatedField<int32_t>& value, uint8_t* target) {
  1527. return WritePrimitiveToArray(field_number, value, WriteSFixed32ToArray,
  1528. target);
  1529. }
  1530. inline uint8_t* WireFormatLite::WriteSFixed64ToArray(
  1531. int field_number, const RepeatedField<int64_t>& value, uint8_t* target) {
  1532. return WritePrimitiveToArray(field_number, value, WriteSFixed64ToArray,
  1533. target);
  1534. }
  1535. inline uint8_t* WireFormatLite::WriteFloatToArray(
  1536. int field_number, const RepeatedField<float>& value, uint8_t* target) {
  1537. return WritePrimitiveToArray(field_number, value, WriteFloatToArray, target);
  1538. }
  1539. inline uint8_t* WireFormatLite::WriteDoubleToArray(
  1540. int field_number, const RepeatedField<double>& value, uint8_t* target) {
  1541. return WritePrimitiveToArray(field_number, value, WriteDoubleToArray, target);
  1542. }
  1543. inline uint8_t* WireFormatLite::WriteBoolToArray(
  1544. int field_number, const RepeatedField<bool>& value, uint8_t* target) {
  1545. return WritePrimitiveToArray(field_number, value, WriteBoolToArray, target);
  1546. }
  1547. inline uint8_t* WireFormatLite::WriteEnumToArray(
  1548. int field_number, const RepeatedField<int>& value, uint8_t* target) {
  1549. return WritePrimitiveToArray(field_number, value, WriteEnumToArray, target);
  1550. }
  1551. inline uint8_t* WireFormatLite::WriteStringToArray(int field_number,
  1552. const std::string& value,
  1553. uint8_t* target) {
  1554. // String is for UTF-8 text only
  1555. // WARNING: In wire_format.cc, both strings and bytes are handled by
  1556. // WriteString() to avoid code duplication. If the implementations become
  1557. // different, you will need to update that usage.
  1558. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  1559. return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
  1560. }
  1561. inline uint8_t* WireFormatLite::WriteBytesToArray(int field_number,
  1562. const std::string& value,
  1563. uint8_t* target) {
  1564. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  1565. return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
  1566. }
  1567. template <typename MessageType>
  1568. inline uint8_t* WireFormatLite::InternalWriteGroup(
  1569. int field_number, const MessageType& value, uint8_t* target,
  1570. io::EpsCopyOutputStream* stream) {
  1571. target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
  1572. target = value._InternalSerialize(target, stream);
  1573. target = stream->EnsureSpace(target);
  1574. return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
  1575. }
  1576. template <typename MessageType>
  1577. inline uint8_t* WireFormatLite::InternalWriteMessage(
  1578. int field_number, const MessageType& value, uint8_t* target,
  1579. io::EpsCopyOutputStream* stream) {
  1580. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  1581. target = io::CodedOutputStream::WriteVarint32ToArrayOutOfLine(
  1582. static_cast<uint32_t>(value.GetCachedSize()), target);
  1583. return value._InternalSerialize(target, stream);
  1584. }
  1585. // See comment on ReadGroupNoVirtual to understand the need for this template
  1586. // parameter name.
  1587. template <typename MessageType_WorkAroundCppLookupDefect>
  1588. inline uint8_t* WireFormatLite::InternalWriteGroupNoVirtualToArray(
  1589. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  1590. uint8_t* target) {
  1591. target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
  1592. target = value.MessageType_WorkAroundCppLookupDefect::
  1593. SerializeWithCachedSizesToArray(target);
  1594. return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
  1595. }
  1596. template <typename MessageType_WorkAroundCppLookupDefect>
  1597. inline uint8_t* WireFormatLite::InternalWriteMessageNoVirtualToArray(
  1598. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  1599. uint8_t* target) {
  1600. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  1601. target = io::CodedOutputStream::WriteVarint32ToArray(
  1602. static_cast<uint32_t>(
  1603. value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()),
  1604. target);
  1605. return value
  1606. .MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizesToArray(
  1607. target);
  1608. }
  1609. // ===================================================================
  1610. inline size_t WireFormatLite::Int32Size(int32_t value) {
  1611. return io::CodedOutputStream::VarintSize32SignExtended(value);
  1612. }
  1613. inline size_t WireFormatLite::Int64Size(int64_t value) {
  1614. return io::CodedOutputStream::VarintSize64(static_cast<uint64_t>(value));
  1615. }
  1616. inline size_t WireFormatLite::UInt32Size(uint32_t value) {
  1617. return io::CodedOutputStream::VarintSize32(value);
  1618. }
  1619. inline size_t WireFormatLite::UInt64Size(uint64_t value) {
  1620. return io::CodedOutputStream::VarintSize64(value);
  1621. }
  1622. inline size_t WireFormatLite::SInt32Size(int32_t value) {
  1623. return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
  1624. }
  1625. inline size_t WireFormatLite::SInt64Size(int64_t value) {
  1626. return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
  1627. }
  1628. inline size_t WireFormatLite::EnumSize(int value) {
  1629. return io::CodedOutputStream::VarintSize32SignExtended(value);
  1630. }
  1631. inline size_t WireFormatLite::Int32SizePlusOne(int32_t value) {
  1632. return io::CodedOutputStream::VarintSize32SignExtendedPlusOne(value);
  1633. }
  1634. inline size_t WireFormatLite::Int64SizePlusOne(int64_t value) {
  1635. return io::CodedOutputStream::VarintSize64PlusOne(
  1636. static_cast<uint64_t>(value));
  1637. }
  1638. inline size_t WireFormatLite::UInt32SizePlusOne(uint32_t value) {
  1639. return io::CodedOutputStream::VarintSize32PlusOne(value);
  1640. }
  1641. inline size_t WireFormatLite::UInt64SizePlusOne(uint64_t value) {
  1642. return io::CodedOutputStream::VarintSize64PlusOne(value);
  1643. }
  1644. inline size_t WireFormatLite::SInt32SizePlusOne(int32_t value) {
  1645. return io::CodedOutputStream::VarintSize32PlusOne(ZigZagEncode32(value));
  1646. }
  1647. inline size_t WireFormatLite::SInt64SizePlusOne(int64_t value) {
  1648. return io::CodedOutputStream::VarintSize64PlusOne(ZigZagEncode64(value));
  1649. }
  1650. inline size_t WireFormatLite::EnumSizePlusOne(int value) {
  1651. return io::CodedOutputStream::VarintSize32SignExtendedPlusOne(value);
  1652. }
  1653. inline size_t WireFormatLite::StringSize(const std::string& value) {
  1654. return LengthDelimitedSize(value.size());
  1655. }
  1656. inline size_t WireFormatLite::BytesSize(const std::string& value) {
  1657. return LengthDelimitedSize(value.size());
  1658. }
  1659. template <typename MessageType>
  1660. inline size_t WireFormatLite::GroupSize(const MessageType& value) {
  1661. return value.ByteSizeLong();
  1662. }
  1663. template <typename MessageType>
  1664. inline size_t WireFormatLite::MessageSize(const MessageType& value) {
  1665. return LengthDelimitedSize(value.ByteSizeLong());
  1666. }
  1667. // See comment on ReadGroupNoVirtual to understand the need for this template
  1668. // parameter name.
  1669. template <typename MessageType_WorkAroundCppLookupDefect>
  1670. inline size_t WireFormatLite::GroupSizeNoVirtual(
  1671. const MessageType_WorkAroundCppLookupDefect& value) {
  1672. return value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong();
  1673. }
  1674. template <typename MessageType_WorkAroundCppLookupDefect>
  1675. inline size_t WireFormatLite::MessageSizeNoVirtual(
  1676. const MessageType_WorkAroundCppLookupDefect& value) {
  1677. return LengthDelimitedSize(
  1678. value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong());
  1679. }
  1680. inline size_t WireFormatLite::LengthDelimitedSize(size_t length) {
  1681. // The static_cast here prevents an error in certain compiler configurations
  1682. // but is not technically correct--if length is too large to fit in a uint32_t
  1683. // then it will be silently truncated. We will need to fix this if we ever
  1684. // decide to start supporting serialized messages greater than 2 GiB in size.
  1685. return length +
  1686. io::CodedOutputStream::VarintSize32(static_cast<uint32_t>(length));
  1687. }
  1688. template <typename MS>
  1689. bool ParseMessageSetItemImpl(io::CodedInputStream* input, MS ms) {
  1690. // This method parses a group which should contain two fields:
  1691. // required int32 type_id = 2;
  1692. // required data message = 3;
  1693. uint32_t last_type_id = 0;
  1694. // If we see message data before the type_id, we'll append it to this so
  1695. // we can parse it later.
  1696. std::string message_data;
  1697. while (true) {
  1698. const uint32_t tag = input->ReadTagNoLastTag();
  1699. if (tag == 0) return false;
  1700. switch (tag) {
  1701. case WireFormatLite::kMessageSetTypeIdTag: {
  1702. uint32_t type_id;
  1703. if (!input->ReadVarint32(&type_id)) return false;
  1704. last_type_id = type_id;
  1705. if (!message_data.empty()) {
  1706. // We saw some message data before the type_id. Have to parse it
  1707. // now.
  1708. io::CodedInputStream sub_input(
  1709. reinterpret_cast<const uint8_t*>(message_data.data()),
  1710. static_cast<int>(message_data.size()));
  1711. sub_input.SetRecursionLimit(input->RecursionBudget());
  1712. if (!ms.ParseField(last_type_id, &sub_input)) {
  1713. return false;
  1714. }
  1715. message_data.clear();
  1716. }
  1717. break;
  1718. }
  1719. case WireFormatLite::kMessageSetMessageTag: {
  1720. if (last_type_id == 0) {
  1721. // We haven't seen a type_id yet. Append this data to message_data.
  1722. uint32_t length;
  1723. if (!input->ReadVarint32(&length)) return false;
  1724. if (static_cast<int32_t>(length) < 0) return false;
  1725. uint32_t size = static_cast<uint32_t>(
  1726. length + io::CodedOutputStream::VarintSize32(length));
  1727. message_data.resize(size);
  1728. auto ptr = reinterpret_cast<uint8_t*>(&message_data[0]);
  1729. ptr = io::CodedOutputStream::WriteVarint32ToArray(length, ptr);
  1730. if (!input->ReadRaw(ptr, length)) return false;
  1731. } else {
  1732. // Already saw type_id, so we can parse this directly.
  1733. if (!ms.ParseField(last_type_id, input)) {
  1734. return false;
  1735. }
  1736. }
  1737. break;
  1738. }
  1739. case WireFormatLite::kMessageSetItemEndTag: {
  1740. return true;
  1741. }
  1742. default: {
  1743. if (!ms.SkipField(tag, input)) return false;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. } // namespace internal
  1749. } // namespace protobuf
  1750. } // namespace google
  1751. #include <google/protobuf/port_undef.inc>
  1752. #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__