wire_format_lite.h 82 KB

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