wire_format_lite.h 84 KB

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