coded_stream.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file contains the CodedInputStream and CodedOutputStream classes,
  35. // which wrap a ZeroCopyInputStream or ZeroCopyOutputStream, respectively,
  36. // and allow you to read or write individual pieces of data in various
  37. // formats. In particular, these implement the varint encoding for
  38. // integers, a simple variable-length encoding in which smaller numbers
  39. // take fewer bytes.
  40. //
  41. // Typically these classes will only be used internally by the protocol
  42. // buffer library in order to encode and decode protocol buffers. Clients
  43. // of the library only need to know about this class if they wish to write
  44. // custom message parsing or serialization procedures.
  45. //
  46. // CodedOutputStream example:
  47. // // Write some data to "myfile". First we write a 4-byte "magic number"
  48. // // to identify the file type, then write a length-delimited string. The
  49. // // string is composed of a varint giving the length followed by the raw
  50. // // bytes.
  51. // int fd = open("myfile", O_CREAT | O_WRONLY);
  52. // ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
  53. // CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  54. //
  55. // int magic_number = 1234;
  56. // char text[] = "Hello world!";
  57. // coded_output->WriteLittleEndian32(magic_number);
  58. // coded_output->WriteVarint32(strlen(text));
  59. // coded_output->WriteRaw(text, strlen(text));
  60. //
  61. // delete coded_output;
  62. // delete raw_output;
  63. // close(fd);
  64. //
  65. // CodedInputStream example:
  66. // // Read a file created by the above code.
  67. // int fd = open("myfile", O_RDONLY);
  68. // ZeroCopyInputStream* raw_input = new FileInputStream(fd);
  69. // CodedInputStream* coded_input = new CodedInputStream(raw_input);
  70. //
  71. // coded_input->ReadLittleEndian32(&magic_number);
  72. // if (magic_number != 1234) {
  73. // cerr << "File not in expected format." << endl;
  74. // return;
  75. // }
  76. //
  77. // uint32_t size;
  78. // coded_input->ReadVarint32(&size);
  79. //
  80. // char* text = new char[size + 1];
  81. // coded_input->ReadRaw(buffer, size);
  82. // text[size] = '\0';
  83. //
  84. // delete coded_input;
  85. // delete raw_input;
  86. // close(fd);
  87. //
  88. // cout << "Text is: " << text << endl;
  89. // delete [] text;
  90. //
  91. // For those who are interested, varint encoding is defined as follows:
  92. //
  93. // The encoding operates on unsigned integers of up to 64 bits in length.
  94. // Each byte of the encoded value has the format:
  95. // * bits 0-6: Seven bits of the number being encoded.
  96. // * bit 7: Zero if this is the last byte in the encoding (in which
  97. // case all remaining bits of the number are zero) or 1 if
  98. // more bytes follow.
  99. // The first byte contains the least-significant 7 bits of the number, the
  100. // second byte (if present) contains the next-least-significant 7 bits,
  101. // and so on. So, the binary number 1011000101011 would be encoded in two
  102. // bytes as "10101011 00101100".
  103. //
  104. // In theory, varint could be used to encode integers of any length.
  105. // However, for practicality we set a limit at 64 bits. The maximum encoded
  106. // length of a number is thus 10 bytes.
  107. #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
  108. #define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
  109. #include <assert.h>
  110. #include <atomic>
  111. #include <climits>
  112. #include <cstddef>
  113. #include <cstring>
  114. #include <limits>
  115. #include <string>
  116. #include <type_traits>
  117. #include <utility>
  118. #ifdef _WIN32
  119. // Assuming windows is always little-endian.
  120. #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  121. #define PROTOBUF_LITTLE_ENDIAN 1
  122. #endif
  123. #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
  124. // If MSVC has "/RTCc" set, it will complain about truncating casts at
  125. // runtime. This file contains some intentional truncating casts.
  126. #pragma runtime_checks("c", off)
  127. #endif
  128. #else
  129. #ifdef __APPLE__
  130. #include <machine/endian.h> // __BYTE_ORDER
  131. #elif defined(__FreeBSD__)
  132. #include <sys/endian.h> // __BYTE_ORDER
  133. #elif (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
  134. #include <sys/isa_defs.h> // __BYTE_ORDER
  135. #elif defined(_AIX) || defined(__TOS_AIX__)
  136. #include <sys/machine.h> // BYTE_ORDER
  137. #else
  138. #if !defined(__QNX__)
  139. #include <endian.h> // __BYTE_ORDER
  140. #endif
  141. #endif
  142. #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
  143. (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
  144. !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  145. #define PROTOBUF_LITTLE_ENDIAN 1
  146. #endif
  147. #endif
  148. #include <google/protobuf/stubs/common.h>
  149. #include <google/protobuf/stubs/logging.h>
  150. #include <google/protobuf/stubs/strutil.h>
  151. #include <google/protobuf/port.h>
  152. #include <google/protobuf/stubs/port.h>
  153. #include <google/protobuf/port_def.inc>
  154. namespace google {
  155. namespace protobuf {
  156. class DescriptorPool;
  157. class MessageFactory;
  158. class ZeroCopyCodedInputStream;
  159. namespace internal {
  160. void MapTestForceDeterministic();
  161. class EpsCopyByteStream;
  162. } // namespace internal
  163. namespace io {
  164. // Defined in this file.
  165. class CodedInputStream;
  166. class CodedOutputStream;
  167. // Defined in other files.
  168. class ZeroCopyInputStream; // zero_copy_stream.h
  169. class ZeroCopyOutputStream; // zero_copy_stream.h
  170. // Class which reads and decodes binary data which is composed of varint-
  171. // encoded integers and fixed-width pieces. Wraps a ZeroCopyInputStream.
  172. // Most users will not need to deal with CodedInputStream.
  173. //
  174. // Most methods of CodedInputStream that return a bool return false if an
  175. // underlying I/O error occurs or if the data is malformed. Once such a
  176. // failure occurs, the CodedInputStream is broken and is no longer useful.
  177. // After a failure, callers also should assume writes to "out" args may have
  178. // occurred, though nothing useful can be determined from those writes.
  179. class PROTOBUF_EXPORT CodedInputStream {
  180. public:
  181. // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
  182. explicit CodedInputStream(ZeroCopyInputStream* input);
  183. // Create a CodedInputStream that reads from the given flat array. This is
  184. // faster than using an ArrayInputStream. PushLimit(size) is implied by
  185. // this constructor.
  186. explicit CodedInputStream(const uint8_t* buffer, int size);
  187. // Destroy the CodedInputStream and position the underlying
  188. // ZeroCopyInputStream at the first unread byte. If an error occurred while
  189. // reading (causing a method to return false), then the exact position of
  190. // the input stream may be anywhere between the last value that was read
  191. // successfully and the stream's byte limit.
  192. ~CodedInputStream();
  193. // Return true if this CodedInputStream reads from a flat array instead of
  194. // a ZeroCopyInputStream.
  195. inline bool IsFlat() const;
  196. // Skips a number of bytes. Returns false if an underlying read error
  197. // occurs.
  198. inline bool Skip(int count);
  199. // Sets *data to point directly at the unread part of the CodedInputStream's
  200. // underlying buffer, and *size to the size of that buffer, but does not
  201. // advance the stream's current position. This will always either produce
  202. // a non-empty buffer or return false. If the caller consumes any of
  203. // this data, it should then call Skip() to skip over the consumed bytes.
  204. // This may be useful for implementing external fast parsing routines for
  205. // types of data not covered by the CodedInputStream interface.
  206. bool GetDirectBufferPointer(const void** data, int* size);
  207. // Like GetDirectBufferPointer, but this method is inlined, and does not
  208. // attempt to Refresh() if the buffer is currently empty.
  209. PROTOBUF_ALWAYS_INLINE
  210. void GetDirectBufferPointerInline(const void** data, int* size);
  211. // Read raw bytes, copying them into the given buffer.
  212. bool ReadRaw(void* buffer, int size);
  213. // Like ReadRaw, but reads into a string.
  214. bool ReadString(std::string* buffer, int size);
  215. // Read a 32-bit little-endian integer.
  216. bool ReadLittleEndian32(uint32_t* value);
  217. // Read a 64-bit little-endian integer.
  218. bool ReadLittleEndian64(uint64_t* value);
  219. // These methods read from an externally provided buffer. The caller is
  220. // responsible for ensuring that the buffer has sufficient space.
  221. // Read a 32-bit little-endian integer.
  222. static const uint8_t* ReadLittleEndian32FromArray(const uint8_t* buffer,
  223. uint32_t* value);
  224. // Read a 64-bit little-endian integer.
  225. static const uint8_t* ReadLittleEndian64FromArray(const uint8_t* buffer,
  226. uint64_t* value);
  227. // Read an unsigned integer with Varint encoding, truncating to 32 bits.
  228. // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
  229. // it to uint32_t, but may be more efficient.
  230. bool ReadVarint32(uint32_t* value);
  231. // Read an unsigned integer with Varint encoding.
  232. bool ReadVarint64(uint64_t* value);
  233. // Reads a varint off the wire into an "int". This should be used for reading
  234. // sizes off the wire (sizes of strings, submessages, bytes fields, etc).
  235. //
  236. // The value from the wire is interpreted as unsigned. If its value exceeds
  237. // the representable value of an integer on this platform, instead of
  238. // truncating we return false. Truncating (as performed by ReadVarint32()
  239. // above) is an acceptable approach for fields representing an integer, but
  240. // when we are parsing a size from the wire, truncating the value would result
  241. // in us misparsing the payload.
  242. bool ReadVarintSizeAsInt(int* value);
  243. // Read a tag. This calls ReadVarint32() and returns the result, or returns
  244. // zero (which is not a valid tag) if ReadVarint32() fails. Also, ReadTag
  245. // (but not ReadTagNoLastTag) updates the last tag value, which can be checked
  246. // with LastTagWas().
  247. //
  248. // Always inline because this is only called in one place per parse loop
  249. // but it is called for every iteration of said loop, so it should be fast.
  250. // GCC doesn't want to inline this by default.
  251. PROTOBUF_ALWAYS_INLINE uint32_t ReadTag() {
  252. return last_tag_ = ReadTagNoLastTag();
  253. }
  254. PROTOBUF_ALWAYS_INLINE uint32_t ReadTagNoLastTag();
  255. // This usually a faster alternative to ReadTag() when cutoff is a manifest
  256. // constant. It does particularly well for cutoff >= 127. The first part
  257. // of the return value is the tag that was read, though it can also be 0 in
  258. // the cases where ReadTag() would return 0. If the second part is true
  259. // then the tag is known to be in [0, cutoff]. If not, the tag either is
  260. // above cutoff or is 0. (There's intentional wiggle room when tag is 0,
  261. // because that can arise in several ways, and for best performance we want
  262. // to avoid an extra "is tag == 0?" check here.)
  263. PROTOBUF_ALWAYS_INLINE
  264. std::pair<uint32_t, bool> ReadTagWithCutoff(uint32_t cutoff) {
  265. std::pair<uint32_t, bool> result = ReadTagWithCutoffNoLastTag(cutoff);
  266. last_tag_ = result.first;
  267. return result;
  268. }
  269. PROTOBUF_ALWAYS_INLINE
  270. std::pair<uint32_t, bool> ReadTagWithCutoffNoLastTag(uint32_t cutoff);
  271. // Usually returns true if calling ReadVarint32() now would produce the given
  272. // value. Will always return false if ReadVarint32() would not return the
  273. // given value. If ExpectTag() returns true, it also advances past
  274. // the varint. For best performance, use a compile-time constant as the
  275. // parameter.
  276. // Always inline because this collapses to a small number of instructions
  277. // when given a constant parameter, but GCC doesn't want to inline by default.
  278. PROTOBUF_ALWAYS_INLINE bool ExpectTag(uint32_t expected);
  279. // Like above, except this reads from the specified buffer. The caller is
  280. // responsible for ensuring that the buffer is large enough to read a varint
  281. // of the expected size. For best performance, use a compile-time constant as
  282. // the expected tag parameter.
  283. //
  284. // Returns a pointer beyond the expected tag if it was found, or NULL if it
  285. // was not.
  286. PROTOBUF_ALWAYS_INLINE
  287. static const uint8_t* ExpectTagFromArray(const uint8_t* buffer,
  288. uint32_t expected);
  289. // Usually returns true if no more bytes can be read. Always returns false
  290. // if more bytes can be read. If ExpectAtEnd() returns true, a subsequent
  291. // call to LastTagWas() will act as if ReadTag() had been called and returned
  292. // zero, and ConsumedEntireMessage() will return true.
  293. bool ExpectAtEnd();
  294. // If the last call to ReadTag() or ReadTagWithCutoff() returned the given
  295. // value, returns true. Otherwise, returns false.
  296. // ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last
  297. // returned value.
  298. //
  299. // This is needed because parsers for some types of embedded messages
  300. // (with field type TYPE_GROUP) don't actually know that they've reached the
  301. // end of a message until they see an ENDGROUP tag, which was actually part
  302. // of the enclosing message. The enclosing message would like to check that
  303. // tag to make sure it had the right number, so it calls LastTagWas() on
  304. // return from the embedded parser to check.
  305. bool LastTagWas(uint32_t expected);
  306. void SetLastTag(uint32_t tag) { last_tag_ = tag; }
  307. // When parsing message (but NOT a group), this method must be called
  308. // immediately after MergeFromCodedStream() returns (if it returns true)
  309. // to further verify that the message ended in a legitimate way. For
  310. // example, this verifies that parsing did not end on an end-group tag.
  311. // It also checks for some cases where, due to optimizations,
  312. // MergeFromCodedStream() can incorrectly return true.
  313. bool ConsumedEntireMessage();
  314. void SetConsumed() { legitimate_message_end_ = true; }
  315. // Limits ----------------------------------------------------------
  316. // Limits are used when parsing length-delimited embedded messages.
  317. // After the message's length is read, PushLimit() is used to prevent
  318. // the CodedInputStream from reading beyond that length. Once the
  319. // embedded message has been parsed, PopLimit() is called to undo the
  320. // limit.
  321. // Opaque type used with PushLimit() and PopLimit(). Do not modify
  322. // values of this type yourself. The only reason that this isn't a
  323. // struct with private internals is for efficiency.
  324. typedef int Limit;
  325. // Places a limit on the number of bytes that the stream may read,
  326. // starting from the current position. Once the stream hits this limit,
  327. // it will act like the end of the input has been reached until PopLimit()
  328. // is called.
  329. //
  330. // As the names imply, the stream conceptually has a stack of limits. The
  331. // shortest limit on the stack is always enforced, even if it is not the
  332. // top limit.
  333. //
  334. // The value returned by PushLimit() is opaque to the caller, and must
  335. // be passed unchanged to the corresponding call to PopLimit().
  336. Limit PushLimit(int byte_limit);
  337. // Pops the last limit pushed by PushLimit(). The input must be the value
  338. // returned by that call to PushLimit().
  339. void PopLimit(Limit limit);
  340. // Returns the number of bytes left until the nearest limit on the
  341. // stack is hit, or -1 if no limits are in place.
  342. int BytesUntilLimit() const;
  343. // Returns current position relative to the beginning of the input stream.
  344. int CurrentPosition() const;
  345. // Total Bytes Limit -----------------------------------------------
  346. // To prevent malicious users from sending excessively large messages
  347. // and causing memory exhaustion, CodedInputStream imposes a hard limit on
  348. // the total number of bytes it will read.
  349. // Sets the maximum number of bytes that this CodedInputStream will read
  350. // before refusing to continue. To prevent servers from allocating enormous
  351. // amounts of memory to hold parsed messages, the maximum message length
  352. // should be limited to the shortest length that will not harm usability.
  353. // The default limit is INT_MAX (~2GB) and apps should set shorter limits
  354. // if possible. An error will always be printed to stderr if the limit is
  355. // reached.
  356. //
  357. // Note: setting a limit less than the current read position is interpreted
  358. // as a limit on the current position.
  359. //
  360. // This is unrelated to PushLimit()/PopLimit().
  361. void SetTotalBytesLimit(int total_bytes_limit);
  362. // The Total Bytes Limit minus the Current Position, or -1 if the total bytes
  363. // limit is INT_MAX.
  364. int BytesUntilTotalBytesLimit() const;
  365. // Recursion Limit -------------------------------------------------
  366. // To prevent corrupt or malicious messages from causing stack overflows,
  367. // we must keep track of the depth of recursion when parsing embedded
  368. // messages and groups. CodedInputStream keeps track of this because it
  369. // is the only object that is passed down the stack during parsing.
  370. // Sets the maximum recursion depth. The default is 100.
  371. void SetRecursionLimit(int limit);
  372. int RecursionBudget() { return recursion_budget_; }
  373. static int GetDefaultRecursionLimit() { return default_recursion_limit_; }
  374. // Increments the current recursion depth. Returns true if the depth is
  375. // under the limit, false if it has gone over.
  376. bool IncrementRecursionDepth();
  377. // Decrements the recursion depth if possible.
  378. void DecrementRecursionDepth();
  379. // Decrements the recursion depth blindly. This is faster than
  380. // DecrementRecursionDepth(). It should be used only if all previous
  381. // increments to recursion depth were successful.
  382. void UnsafeDecrementRecursionDepth();
  383. // Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_).
  384. // Using this can reduce code size and complexity in some cases. The caller
  385. // is expected to check that the second part of the result is non-negative (to
  386. // bail out if the depth of recursion is too high) and, if all is well, to
  387. // later pass the first part of the result to PopLimit() or similar.
  388. std::pair<CodedInputStream::Limit, int> IncrementRecursionDepthAndPushLimit(
  389. int byte_limit);
  390. // Shorthand for PushLimit(ReadVarint32(&length) ? length : 0).
  391. Limit ReadLengthAndPushLimit();
  392. // Helper that is equivalent to: {
  393. // bool result = ConsumedEntireMessage();
  394. // PopLimit(limit);
  395. // UnsafeDecrementRecursionDepth();
  396. // return result; }
  397. // Using this can reduce code size and complexity in some cases.
  398. // Do not use unless the current recursion depth is greater than zero.
  399. bool DecrementRecursionDepthAndPopLimit(Limit limit);
  400. // Helper that is equivalent to: {
  401. // bool result = ConsumedEntireMessage();
  402. // PopLimit(limit);
  403. // return result; }
  404. // Using this can reduce code size and complexity in some cases.
  405. bool CheckEntireMessageConsumedAndPopLimit(Limit limit);
  406. // Extension Registry ----------------------------------------------
  407. // ADVANCED USAGE: 99.9% of people can ignore this section.
  408. //
  409. // By default, when parsing extensions, the parser looks for extension
  410. // definitions in the pool which owns the outer message's Descriptor.
  411. // However, you may call SetExtensionRegistry() to provide an alternative
  412. // pool instead. This makes it possible, for example, to parse a message
  413. // using a generated class, but represent some extensions using
  414. // DynamicMessage.
  415. // Set the pool used to look up extensions. Most users do not need to call
  416. // this as the correct pool will be chosen automatically.
  417. //
  418. // WARNING: It is very easy to misuse this. Carefully read the requirements
  419. // below. Do not use this unless you are sure you need it. Almost no one
  420. // does.
  421. //
  422. // Let's say you are parsing a message into message object m, and you want
  423. // to take advantage of SetExtensionRegistry(). You must follow these
  424. // requirements:
  425. //
  426. // The given DescriptorPool must contain m->GetDescriptor(). It is not
  427. // sufficient for it to simply contain a descriptor that has the same name
  428. // and content -- it must be the *exact object*. In other words:
  429. // assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
  430. // m->GetDescriptor());
  431. // There are two ways to satisfy this requirement:
  432. // 1) Use m->GetDescriptor()->pool() as the pool. This is generally useless
  433. // because this is the pool that would be used anyway if you didn't call
  434. // SetExtensionRegistry() at all.
  435. // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
  436. // "underlay". Read the documentation for DescriptorPool for more
  437. // information about underlays.
  438. //
  439. // You must also provide a MessageFactory. This factory will be used to
  440. // construct Message objects representing extensions. The factory's
  441. // GetPrototype() MUST return non-NULL for any Descriptor which can be found
  442. // through the provided pool.
  443. //
  444. // If the provided factory might return instances of protocol-compiler-
  445. // generated (i.e. compiled-in) types, or if the outer message object m is
  446. // a generated type, then the given factory MUST have this property: If
  447. // GetPrototype() is given a Descriptor which resides in
  448. // DescriptorPool::generated_pool(), the factory MUST return the same
  449. // prototype which MessageFactory::generated_factory() would return. That
  450. // is, given a descriptor for a generated type, the factory must return an
  451. // instance of the generated class (NOT DynamicMessage). However, when
  452. // given a descriptor for a type that is NOT in generated_pool, the factory
  453. // is free to return any implementation.
  454. //
  455. // The reason for this requirement is that generated sub-objects may be
  456. // accessed via the standard (non-reflection) extension accessor methods,
  457. // and these methods will down-cast the object to the generated class type.
  458. // If the object is not actually of that type, the results would be undefined.
  459. // On the other hand, if an extension is not compiled in, then there is no
  460. // way the code could end up accessing it via the standard accessors -- the
  461. // only way to access the extension is via reflection. When using reflection,
  462. // DynamicMessage and generated messages are indistinguishable, so it's fine
  463. // if these objects are represented using DynamicMessage.
  464. //
  465. // Using DynamicMessageFactory on which you have called
  466. // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
  467. // above requirement.
  468. //
  469. // If either pool or factory is NULL, both must be NULL.
  470. //
  471. // Note that this feature is ignored when parsing "lite" messages as they do
  472. // not have descriptors.
  473. void SetExtensionRegistry(const DescriptorPool* pool,
  474. MessageFactory* factory);
  475. // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
  476. // has been provided.
  477. const DescriptorPool* GetExtensionPool();
  478. // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
  479. // factory has been provided.
  480. MessageFactory* GetExtensionFactory();
  481. private:
  482. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
  483. const uint8_t* buffer_;
  484. const uint8_t* buffer_end_; // pointer to the end of the buffer.
  485. ZeroCopyInputStream* input_;
  486. int total_bytes_read_; // total bytes read from input_, including
  487. // the current buffer
  488. // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
  489. // so that we can BackUp() on destruction.
  490. int overflow_bytes_;
  491. // LastTagWas() stuff.
  492. uint32_t last_tag_; // result of last ReadTag() or ReadTagWithCutoff().
  493. // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
  494. // at EOF, or by ExpectAtEnd() when it returns true. This happens when we
  495. // reach the end of a message and attempt to read another tag.
  496. bool legitimate_message_end_;
  497. // See EnableAliasing().
  498. bool aliasing_enabled_;
  499. // Limits
  500. Limit current_limit_; // if position = -1, no limit is applied
  501. // For simplicity, if the current buffer crosses a limit (either a normal
  502. // limit created by PushLimit() or the total bytes limit), buffer_size_
  503. // only tracks the number of bytes before that limit. This field
  504. // contains the number of bytes after it. Note that this implies that if
  505. // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
  506. // hit a limit. However, if both are zero, it doesn't necessarily mean
  507. // we aren't at a limit -- the buffer may have ended exactly at the limit.
  508. int buffer_size_after_limit_;
  509. // Maximum number of bytes to read, period. This is unrelated to
  510. // current_limit_. Set using SetTotalBytesLimit().
  511. int total_bytes_limit_;
  512. // Current recursion budget, controlled by IncrementRecursionDepth() and
  513. // similar. Starts at recursion_limit_ and goes down: if this reaches
  514. // -1 we are over budget.
  515. int recursion_budget_;
  516. // Recursion depth limit, set by SetRecursionLimit().
  517. int recursion_limit_;
  518. // See SetExtensionRegistry().
  519. const DescriptorPool* extension_pool_;
  520. MessageFactory* extension_factory_;
  521. // Private member functions.
  522. // Fallback when Skip() goes past the end of the current buffer.
  523. bool SkipFallback(int count, int original_buffer_size);
  524. // Advance the buffer by a given number of bytes.
  525. void Advance(int amount);
  526. // Back up input_ to the current buffer position.
  527. void BackUpInputToCurrentPosition();
  528. // Recomputes the value of buffer_size_after_limit_. Must be called after
  529. // current_limit_ or total_bytes_limit_ changes.
  530. void RecomputeBufferLimits();
  531. // Writes an error message saying that we hit total_bytes_limit_.
  532. void PrintTotalBytesLimitError();
  533. // Called when the buffer runs out to request more data. Implies an
  534. // Advance(BufferSize()).
  535. bool Refresh();
  536. // When parsing varints, we optimize for the common case of small values, and
  537. // then optimize for the case when the varint fits within the current buffer
  538. // piece. The Fallback method is used when we can't use the one-byte
  539. // optimization. The Slow method is yet another fallback when the buffer is
  540. // not large enough. Making the slow path out-of-line speeds up the common
  541. // case by 10-15%. The slow path is fairly uncommon: it only triggers when a
  542. // message crosses multiple buffers. Note: ReadVarint32Fallback() and
  543. // ReadVarint64Fallback() are called frequently and generally not inlined, so
  544. // they have been optimized to avoid "out" parameters. The former returns -1
  545. // if it fails and the uint32_t it read otherwise. The latter has a bool
  546. // indicating success or failure as part of its return type.
  547. int64_t ReadVarint32Fallback(uint32_t first_byte_or_zero);
  548. int ReadVarintSizeAsIntFallback();
  549. std::pair<uint64_t, bool> ReadVarint64Fallback();
  550. bool ReadVarint32Slow(uint32_t* value);
  551. bool ReadVarint64Slow(uint64_t* value);
  552. int ReadVarintSizeAsIntSlow();
  553. bool ReadLittleEndian32Fallback(uint32_t* value);
  554. bool ReadLittleEndian64Fallback(uint64_t* value);
  555. // Fallback/slow methods for reading tags. These do not update last_tag_,
  556. // but will set legitimate_message_end_ if we are at the end of the input
  557. // stream.
  558. uint32_t ReadTagFallback(uint32_t first_byte_or_zero);
  559. uint32_t ReadTagSlow();
  560. bool ReadStringFallback(std::string* buffer, int size);
  561. // Return the size of the buffer.
  562. int BufferSize() const;
  563. static const int kDefaultTotalBytesLimit = INT_MAX;
  564. static int default_recursion_limit_; // 100 by default.
  565. friend class google::protobuf::ZeroCopyCodedInputStream;
  566. friend class google::protobuf::internal::EpsCopyByteStream;
  567. };
  568. // EpsCopyOutputStream wraps a ZeroCopyOutputStream and exposes a new stream,
  569. // which has the property you can write kSlopBytes (16 bytes) from the current
  570. // position without bounds checks. The cursor into the stream is managed by
  571. // the user of the class and is an explicit parameter in the methods. Careful
  572. // use of this class, ie. keep ptr a local variable, eliminates the need to
  573. // for the compiler to sync the ptr value between register and memory.
  574. class PROTOBUF_EXPORT EpsCopyOutputStream {
  575. public:
  576. enum { kSlopBytes = 16 };
  577. // Initialize from a stream.
  578. EpsCopyOutputStream(ZeroCopyOutputStream* stream, bool deterministic,
  579. uint8_t** pp)
  580. : end_(buffer_),
  581. stream_(stream),
  582. is_serialization_deterministic_(deterministic) {
  583. *pp = buffer_;
  584. }
  585. // Only for array serialization. No overflow protection, end_ will be the
  586. // pointed to the end of the array. When using this the total size is already
  587. // known, so no need to maintain the slop region.
  588. EpsCopyOutputStream(void* data, int size, bool deterministic)
  589. : end_(static_cast<uint8_t*>(data) + size),
  590. buffer_end_(nullptr),
  591. stream_(nullptr),
  592. is_serialization_deterministic_(deterministic) {}
  593. // Initialize from stream but with the first buffer already given (eager).
  594. EpsCopyOutputStream(void* data, int size, ZeroCopyOutputStream* stream,
  595. bool deterministic, uint8_t** pp)
  596. : stream_(stream), is_serialization_deterministic_(deterministic) {
  597. *pp = SetInitialBuffer(data, size);
  598. }
  599. // Flush everything that's written into the underlying ZeroCopyOutputStream
  600. // and trims the underlying stream to the location of ptr.
  601. uint8_t* Trim(uint8_t* ptr);
  602. // After this it's guaranteed you can safely write kSlopBytes to ptr. This
  603. // will never fail! The underlying stream can produce an error. Use HadError
  604. // to check for errors.
  605. PROTOBUF_NODISCARD uint8_t* EnsureSpace(uint8_t* ptr) {
  606. if (PROTOBUF_PREDICT_FALSE(ptr >= end_)) {
  607. return EnsureSpaceFallback(ptr);
  608. }
  609. return ptr;
  610. }
  611. uint8_t* WriteRaw(const void* data, int size, uint8_t* ptr) {
  612. if (PROTOBUF_PREDICT_FALSE(end_ - ptr < size)) {
  613. return WriteRawFallback(data, size, ptr);
  614. }
  615. std::memcpy(ptr, data, size);
  616. return ptr + size;
  617. }
  618. // Writes the buffer specified by data, size to the stream. Possibly by
  619. // aliasing the buffer (ie. not copying the data). The caller is responsible
  620. // to make sure the buffer is alive for the duration of the
  621. // ZeroCopyOutputStream.
  622. #ifndef NDEBUG
  623. PROTOBUF_NOINLINE
  624. #endif
  625. uint8_t* WriteRawMaybeAliased(const void* data, int size, uint8_t* ptr) {
  626. if (aliasing_enabled_) {
  627. return WriteAliasedRaw(data, size, ptr);
  628. } else {
  629. return WriteRaw(data, size, ptr);
  630. }
  631. }
  632. #ifndef NDEBUG
  633. PROTOBUF_NOINLINE
  634. #endif
  635. uint8_t* WriteStringMaybeAliased(uint32_t num, const std::string& s,
  636. uint8_t* ptr) {
  637. std::ptrdiff_t size = s.size();
  638. if (PROTOBUF_PREDICT_FALSE(
  639. size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
  640. return WriteStringMaybeAliasedOutline(num, s, ptr);
  641. }
  642. ptr = UnsafeVarint((num << 3) | 2, ptr);
  643. *ptr++ = static_cast<uint8_t>(size);
  644. std::memcpy(ptr, s.data(), size);
  645. return ptr + size;
  646. }
  647. uint8_t* WriteBytesMaybeAliased(uint32_t num, const std::string& s,
  648. uint8_t* ptr) {
  649. return WriteStringMaybeAliased(num, s, ptr);
  650. }
  651. template <typename T>
  652. PROTOBUF_ALWAYS_INLINE uint8_t* WriteString(uint32_t num, const T& s,
  653. uint8_t* ptr) {
  654. std::ptrdiff_t size = s.size();
  655. if (PROTOBUF_PREDICT_FALSE(
  656. size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
  657. return WriteStringOutline(num, s, ptr);
  658. }
  659. ptr = UnsafeVarint((num << 3) | 2, ptr);
  660. *ptr++ = static_cast<uint8_t>(size);
  661. std::memcpy(ptr, s.data(), size);
  662. return ptr + size;
  663. }
  664. template <typename T>
  665. #ifndef NDEBUG
  666. PROTOBUF_NOINLINE
  667. #endif
  668. uint8_t* WriteBytes(uint32_t num, const T& s, uint8_t* ptr) {
  669. return WriteString(num, s, ptr);
  670. }
  671. template <typename T>
  672. PROTOBUF_ALWAYS_INLINE uint8_t* WriteInt32Packed(int num, const T& r,
  673. int size, uint8_t* ptr) {
  674. return WriteVarintPacked(num, r, size, ptr, Encode64);
  675. }
  676. template <typename T>
  677. PROTOBUF_ALWAYS_INLINE uint8_t* WriteUInt32Packed(int num, const T& r,
  678. int size, uint8_t* ptr) {
  679. return WriteVarintPacked(num, r, size, ptr, Encode32);
  680. }
  681. template <typename T>
  682. PROTOBUF_ALWAYS_INLINE uint8_t* WriteSInt32Packed(int num, const T& r,
  683. int size, uint8_t* ptr) {
  684. return WriteVarintPacked(num, r, size, ptr, ZigZagEncode32);
  685. }
  686. template <typename T>
  687. PROTOBUF_ALWAYS_INLINE uint8_t* WriteInt64Packed(int num, const T& r,
  688. int size, uint8_t* ptr) {
  689. return WriteVarintPacked(num, r, size, ptr, Encode64);
  690. }
  691. template <typename T>
  692. PROTOBUF_ALWAYS_INLINE uint8_t* WriteUInt64Packed(int num, const T& r,
  693. int size, uint8_t* ptr) {
  694. return WriteVarintPacked(num, r, size, ptr, Encode64);
  695. }
  696. template <typename T>
  697. PROTOBUF_ALWAYS_INLINE uint8_t* WriteSInt64Packed(int num, const T& r,
  698. int size, uint8_t* ptr) {
  699. return WriteVarintPacked(num, r, size, ptr, ZigZagEncode64);
  700. }
  701. template <typename T>
  702. PROTOBUF_ALWAYS_INLINE uint8_t* WriteEnumPacked(int num, const T& r, int size,
  703. uint8_t* ptr) {
  704. return WriteVarintPacked(num, r, size, ptr, Encode64);
  705. }
  706. template <typename T>
  707. PROTOBUF_ALWAYS_INLINE uint8_t* WriteFixedPacked(int num, const T& r,
  708. uint8_t* ptr) {
  709. ptr = EnsureSpace(ptr);
  710. constexpr auto element_size = sizeof(typename T::value_type);
  711. auto size = r.size() * element_size;
  712. ptr = WriteLengthDelim(num, size, ptr);
  713. return WriteRawLittleEndian<element_size>(r.data(), static_cast<int>(size),
  714. ptr);
  715. }
  716. // Returns true if there was an underlying I/O error since this object was
  717. // created.
  718. bool HadError() const { return had_error_; }
  719. // Instructs the EpsCopyOutputStream to allow the underlying
  720. // ZeroCopyOutputStream to hold pointers to the original structure instead of
  721. // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
  722. // underlying stream does not support aliasing, then enabling it has no
  723. // affect. For now, this only affects the behavior of
  724. // WriteRawMaybeAliased().
  725. //
  726. // NOTE: It is caller's responsibility to ensure that the chunk of memory
  727. // remains live until all of the data has been consumed from the stream.
  728. void EnableAliasing(bool enabled);
  729. // See documentation on CodedOutputStream::SetSerializationDeterministic.
  730. void SetSerializationDeterministic(bool value) {
  731. is_serialization_deterministic_ = value;
  732. }
  733. // See documentation on CodedOutputStream::IsSerializationDeterministic.
  734. bool IsSerializationDeterministic() const {
  735. return is_serialization_deterministic_;
  736. }
  737. // The number of bytes written to the stream at position ptr, relative to the
  738. // stream's overall position.
  739. int64_t ByteCount(uint8_t* ptr) const;
  740. private:
  741. uint8_t* end_;
  742. uint8_t* buffer_end_ = buffer_;
  743. uint8_t buffer_[2 * kSlopBytes];
  744. ZeroCopyOutputStream* stream_;
  745. bool had_error_ = false;
  746. bool aliasing_enabled_ = false; // See EnableAliasing().
  747. bool is_serialization_deterministic_;
  748. uint8_t* EnsureSpaceFallback(uint8_t* ptr);
  749. inline uint8_t* Next();
  750. int Flush(uint8_t* ptr);
  751. std::ptrdiff_t GetSize(uint8_t* ptr) const {
  752. GOOGLE_DCHECK(ptr <= end_ + kSlopBytes); // NOLINT
  753. return end_ + kSlopBytes - ptr;
  754. }
  755. uint8_t* Error() {
  756. had_error_ = true;
  757. // We use the patch buffer to always guarantee space to write to.
  758. end_ = buffer_ + kSlopBytes;
  759. return buffer_;
  760. }
  761. static constexpr int TagSize(uint32_t tag) {
  762. return (tag < (1 << 7)) ? 1
  763. : (tag < (1 << 14)) ? 2
  764. : (tag < (1 << 21)) ? 3
  765. : (tag < (1 << 28)) ? 4
  766. : 5;
  767. }
  768. PROTOBUF_ALWAYS_INLINE uint8_t* WriteTag(uint32_t num, uint32_t wt,
  769. uint8_t* ptr) {
  770. GOOGLE_DCHECK(ptr < end_); // NOLINT
  771. return UnsafeVarint((num << 3) | wt, ptr);
  772. }
  773. PROTOBUF_ALWAYS_INLINE uint8_t* WriteLengthDelim(int num, uint32_t size,
  774. uint8_t* ptr) {
  775. ptr = WriteTag(num, 2, ptr);
  776. return UnsafeWriteSize(size, ptr);
  777. }
  778. uint8_t* WriteRawFallback(const void* data, int size, uint8_t* ptr);
  779. uint8_t* WriteAliasedRaw(const void* data, int size, uint8_t* ptr);
  780. uint8_t* WriteStringMaybeAliasedOutline(uint32_t num, const std::string& s,
  781. uint8_t* ptr);
  782. uint8_t* WriteStringOutline(uint32_t num, const std::string& s, uint8_t* ptr);
  783. template <typename T, typename E>
  784. PROTOBUF_ALWAYS_INLINE uint8_t* WriteVarintPacked(int num, const T& r,
  785. int size, uint8_t* ptr,
  786. const E& encode) {
  787. ptr = EnsureSpace(ptr);
  788. ptr = WriteLengthDelim(num, size, ptr);
  789. auto it = r.data();
  790. auto end = it + r.size();
  791. do {
  792. ptr = EnsureSpace(ptr);
  793. ptr = UnsafeVarint(encode(*it++), ptr);
  794. } while (it < end);
  795. return ptr;
  796. }
  797. static uint32_t Encode32(uint32_t v) { return v; }
  798. static uint64_t Encode64(uint64_t v) { return v; }
  799. static uint32_t ZigZagEncode32(int32_t v) {
  800. return (static_cast<uint32_t>(v) << 1) ^ static_cast<uint32_t>(v >> 31);
  801. }
  802. static uint64_t ZigZagEncode64(int64_t v) {
  803. return (static_cast<uint64_t>(v) << 1) ^ static_cast<uint64_t>(v >> 63);
  804. }
  805. template <typename T>
  806. PROTOBUF_ALWAYS_INLINE static uint8_t* UnsafeVarint(T value, uint8_t* ptr) {
  807. static_assert(std::is_unsigned<T>::value,
  808. "Varint serialization must be unsigned");
  809. ptr[0] = static_cast<uint8_t>(value);
  810. if (value < 0x80) {
  811. return ptr + 1;
  812. }
  813. // Turn on continuation bit in the byte we just wrote.
  814. ptr[0] |= static_cast<uint8_t>(0x80);
  815. value >>= 7;
  816. ptr[1] = static_cast<uint8_t>(value);
  817. if (value < 0x80) {
  818. return ptr + 2;
  819. }
  820. ptr += 2;
  821. do {
  822. // Turn on continuation bit in the byte we just wrote.
  823. ptr[-1] |= static_cast<uint8_t>(0x80);
  824. value >>= 7;
  825. *ptr = static_cast<uint8_t>(value);
  826. ++ptr;
  827. } while (value >= 0x80);
  828. return ptr;
  829. }
  830. PROTOBUF_ALWAYS_INLINE static uint8_t* UnsafeWriteSize(uint32_t value,
  831. uint8_t* ptr) {
  832. while (PROTOBUF_PREDICT_FALSE(value >= 0x80)) {
  833. *ptr = static_cast<uint8_t>(value | 0x80);
  834. value >>= 7;
  835. ++ptr;
  836. }
  837. *ptr++ = static_cast<uint8_t>(value);
  838. return ptr;
  839. }
  840. template <int S>
  841. uint8_t* WriteRawLittleEndian(const void* data, int size, uint8_t* ptr);
  842. #ifndef PROTOBUF_LITTLE_ENDIAN
  843. uint8_t* WriteRawLittleEndian32(const void* data, int size, uint8_t* ptr);
  844. uint8_t* WriteRawLittleEndian64(const void* data, int size, uint8_t* ptr);
  845. #endif
  846. // These methods are for CodedOutputStream. Ideally they should be private
  847. // but to match current behavior of CodedOutputStream as close as possible
  848. // we allow it some functionality.
  849. public:
  850. uint8_t* SetInitialBuffer(void* data, int size) {
  851. auto ptr = static_cast<uint8_t*>(data);
  852. if (size > kSlopBytes) {
  853. end_ = ptr + size - kSlopBytes;
  854. buffer_end_ = nullptr;
  855. return ptr;
  856. } else {
  857. end_ = buffer_ + size;
  858. buffer_end_ = ptr;
  859. return buffer_;
  860. }
  861. }
  862. private:
  863. // Needed by CodedOutputStream HadError. HadError needs to flush the patch
  864. // buffers to ensure there is no error as of yet.
  865. uint8_t* FlushAndResetBuffer(uint8_t*);
  866. // The following functions mimic the old CodedOutputStream behavior as close
  867. // as possible. They flush the current state to the stream, behave as
  868. // the old CodedOutputStream and then return to normal operation.
  869. bool Skip(int count, uint8_t** pp);
  870. bool GetDirectBufferPointer(void** data, int* size, uint8_t** pp);
  871. uint8_t* GetDirectBufferForNBytesAndAdvance(int size, uint8_t** pp);
  872. friend class CodedOutputStream;
  873. };
  874. template <>
  875. inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<1>(const void* data,
  876. int size,
  877. uint8_t* ptr) {
  878. return WriteRaw(data, size, ptr);
  879. }
  880. template <>
  881. inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<4>(const void* data,
  882. int size,
  883. uint8_t* ptr) {
  884. #ifdef PROTOBUF_LITTLE_ENDIAN
  885. return WriteRaw(data, size, ptr);
  886. #else
  887. return WriteRawLittleEndian32(data, size, ptr);
  888. #endif
  889. }
  890. template <>
  891. inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<8>(const void* data,
  892. int size,
  893. uint8_t* ptr) {
  894. #ifdef PROTOBUF_LITTLE_ENDIAN
  895. return WriteRaw(data, size, ptr);
  896. #else
  897. return WriteRawLittleEndian64(data, size, ptr);
  898. #endif
  899. }
  900. // Class which encodes and writes binary data which is composed of varint-
  901. // encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream.
  902. // Most users will not need to deal with CodedOutputStream.
  903. //
  904. // Most methods of CodedOutputStream which return a bool return false if an
  905. // underlying I/O error occurs. Once such a failure occurs, the
  906. // CodedOutputStream is broken and is no longer useful. The Write* methods do
  907. // not return the stream status, but will invalidate the stream if an error
  908. // occurs. The client can probe HadError() to determine the status.
  909. //
  910. // Note that every method of CodedOutputStream which writes some data has
  911. // a corresponding static "ToArray" version. These versions write directly
  912. // to the provided buffer, returning a pointer past the last written byte.
  913. // They require that the buffer has sufficient capacity for the encoded data.
  914. // This allows an optimization where we check if an output stream has enough
  915. // space for an entire message before we start writing and, if there is, we
  916. // call only the ToArray methods to avoid doing bound checks for each
  917. // individual value.
  918. // i.e., in the example above:
  919. //
  920. // CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  921. // int magic_number = 1234;
  922. // char text[] = "Hello world!";
  923. //
  924. // int coded_size = sizeof(magic_number) +
  925. // CodedOutputStream::VarintSize32(strlen(text)) +
  926. // strlen(text);
  927. //
  928. // uint8_t* buffer =
  929. // coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
  930. // if (buffer != nullptr) {
  931. // // The output stream has enough space in the buffer: write directly to
  932. // // the array.
  933. // buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
  934. // buffer);
  935. // buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
  936. // buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
  937. // } else {
  938. // // Make bound-checked writes, which will ask the underlying stream for
  939. // // more space as needed.
  940. // coded_output->WriteLittleEndian32(magic_number);
  941. // coded_output->WriteVarint32(strlen(text));
  942. // coded_output->WriteRaw(text, strlen(text));
  943. // }
  944. //
  945. // delete coded_output;
  946. class PROTOBUF_EXPORT CodedOutputStream {
  947. public:
  948. // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
  949. explicit CodedOutputStream(ZeroCopyOutputStream* stream)
  950. : CodedOutputStream(stream, true) {}
  951. CodedOutputStream(ZeroCopyOutputStream* stream, bool do_eager_refresh);
  952. // Destroy the CodedOutputStream and position the underlying
  953. // ZeroCopyOutputStream immediately after the last byte written.
  954. ~CodedOutputStream();
  955. // Returns true if there was an underlying I/O error since this object was
  956. // created. On should call Trim before this function in order to catch all
  957. // errors.
  958. bool HadError() {
  959. cur_ = impl_.FlushAndResetBuffer(cur_);
  960. GOOGLE_DCHECK(cur_);
  961. return impl_.HadError();
  962. }
  963. // Trims any unused space in the underlying buffer so that its size matches
  964. // the number of bytes written by this stream. The underlying buffer will
  965. // automatically be trimmed when this stream is destroyed; this call is only
  966. // necessary if the underlying buffer is accessed *before* the stream is
  967. // destroyed.
  968. void Trim() { cur_ = impl_.Trim(cur_); }
  969. // Skips a number of bytes, leaving the bytes unmodified in the underlying
  970. // buffer. Returns false if an underlying write error occurs. This is
  971. // mainly useful with GetDirectBufferPointer().
  972. // Note of caution, the skipped bytes may contain uninitialized data. The
  973. // caller must make sure that the skipped bytes are properly initialized,
  974. // otherwise you might leak bytes from your heap.
  975. bool Skip(int count) { return impl_.Skip(count, &cur_); }
  976. // Sets *data to point directly at the unwritten part of the
  977. // CodedOutputStream's underlying buffer, and *size to the size of that
  978. // buffer, but does not advance the stream's current position. This will
  979. // always either produce a non-empty buffer or return false. If the caller
  980. // writes any data to this buffer, it should then call Skip() to skip over
  981. // the consumed bytes. This may be useful for implementing external fast
  982. // serialization routines for types of data not covered by the
  983. // CodedOutputStream interface.
  984. bool GetDirectBufferPointer(void** data, int* size) {
  985. return impl_.GetDirectBufferPointer(data, size, &cur_);
  986. }
  987. // If there are at least "size" bytes available in the current buffer,
  988. // returns a pointer directly into the buffer and advances over these bytes.
  989. // The caller may then write directly into this buffer (e.g. using the
  990. // *ToArray static methods) rather than go through CodedOutputStream. If
  991. // there are not enough bytes available, returns NULL. The return pointer is
  992. // invalidated as soon as any other non-const method of CodedOutputStream
  993. // is called.
  994. inline uint8_t* GetDirectBufferForNBytesAndAdvance(int size) {
  995. return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_);
  996. }
  997. // Write raw bytes, copying them from the given buffer.
  998. void WriteRaw(const void* buffer, int size) {
  999. cur_ = impl_.WriteRaw(buffer, size, cur_);
  1000. }
  1001. // Like WriteRaw() but will try to write aliased data if aliasing is
  1002. // turned on.
  1003. void WriteRawMaybeAliased(const void* data, int size);
  1004. // Like WriteRaw() but writing directly to the target array.
  1005. // This is _not_ inlined, as the compiler often optimizes memcpy into inline
  1006. // copy loops. Since this gets called by every field with string or bytes
  1007. // type, inlining may lead to a significant amount of code bloat, with only a
  1008. // minor performance gain.
  1009. static uint8_t* WriteRawToArray(const void* buffer, int size,
  1010. uint8_t* target);
  1011. // Equivalent to WriteRaw(str.data(), str.size()).
  1012. void WriteString(const std::string& str);
  1013. // Like WriteString() but writing directly to the target array.
  1014. static uint8_t* WriteStringToArray(const std::string& str, uint8_t* target);
  1015. // Write the varint-encoded size of str followed by str.
  1016. static uint8_t* WriteStringWithSizeToArray(const std::string& str,
  1017. uint8_t* target);
  1018. // Write a 32-bit little-endian integer.
  1019. void WriteLittleEndian32(uint32_t value) {
  1020. cur_ = impl_.EnsureSpace(cur_);
  1021. SetCur(WriteLittleEndian32ToArray(value, Cur()));
  1022. }
  1023. // Like WriteLittleEndian32() but writing directly to the target array.
  1024. static uint8_t* WriteLittleEndian32ToArray(uint32_t value, uint8_t* target);
  1025. // Write a 64-bit little-endian integer.
  1026. void WriteLittleEndian64(uint64_t value) {
  1027. cur_ = impl_.EnsureSpace(cur_);
  1028. SetCur(WriteLittleEndian64ToArray(value, Cur()));
  1029. }
  1030. // Like WriteLittleEndian64() but writing directly to the target array.
  1031. static uint8_t* WriteLittleEndian64ToArray(uint64_t value, uint8_t* target);
  1032. // Write an unsigned integer with Varint encoding. Writing a 32-bit value
  1033. // is equivalent to casting it to uint64_t and writing it as a 64-bit value,
  1034. // but may be more efficient.
  1035. void WriteVarint32(uint32_t value);
  1036. // Like WriteVarint32() but writing directly to the target array.
  1037. static uint8_t* WriteVarint32ToArray(uint32_t value, uint8_t* target);
  1038. // Like WriteVarint32() but writing directly to the target array, and with
  1039. // the less common-case paths being out of line rather than inlined.
  1040. static uint8_t* WriteVarint32ToArrayOutOfLine(uint32_t value,
  1041. uint8_t* target);
  1042. // Write an unsigned integer with Varint encoding.
  1043. void WriteVarint64(uint64_t value);
  1044. // Like WriteVarint64() but writing directly to the target array.
  1045. static uint8_t* WriteVarint64ToArray(uint64_t value, uint8_t* target);
  1046. // Equivalent to WriteVarint32() except when the value is negative,
  1047. // in which case it must be sign-extended to a full 10 bytes.
  1048. void WriteVarint32SignExtended(int32_t value);
  1049. // Like WriteVarint32SignExtended() but writing directly to the target array.
  1050. static uint8_t* WriteVarint32SignExtendedToArray(int32_t value,
  1051. uint8_t* target);
  1052. // This is identical to WriteVarint32(), but optimized for writing tags.
  1053. // In particular, if the input is a compile-time constant, this method
  1054. // compiles down to a couple instructions.
  1055. // Always inline because otherwise the aforementioned optimization can't work,
  1056. // but GCC by default doesn't want to inline this.
  1057. void WriteTag(uint32_t value);
  1058. // Like WriteTag() but writing directly to the target array.
  1059. PROTOBUF_ALWAYS_INLINE
  1060. static uint8_t* WriteTagToArray(uint32_t value, uint8_t* target);
  1061. // Returns the number of bytes needed to encode the given value as a varint.
  1062. static size_t VarintSize32(uint32_t value);
  1063. // Returns the number of bytes needed to encode the given value as a varint.
  1064. static size_t VarintSize64(uint64_t value);
  1065. // If negative, 10 bytes. Otherwise, same as VarintSize32().
  1066. static size_t VarintSize32SignExtended(int32_t value);
  1067. // Same as above, plus one. The additional one comes at no compute cost.
  1068. static size_t VarintSize32PlusOne(uint32_t value);
  1069. static size_t VarintSize64PlusOne(uint64_t value);
  1070. static size_t VarintSize32SignExtendedPlusOne(int32_t value);
  1071. // Compile-time equivalent of VarintSize32().
  1072. template <uint32_t Value>
  1073. struct StaticVarintSize32 {
  1074. static const size_t value = (Value < (1 << 7)) ? 1
  1075. : (Value < (1 << 14)) ? 2
  1076. : (Value < (1 << 21)) ? 3
  1077. : (Value < (1 << 28)) ? 4
  1078. : 5;
  1079. };
  1080. // Returns the total number of bytes written since this object was created.
  1081. int ByteCount() const {
  1082. return static_cast<int>(impl_.ByteCount(cur_) - start_count_);
  1083. }
  1084. // Instructs the CodedOutputStream to allow the underlying
  1085. // ZeroCopyOutputStream to hold pointers to the original structure instead of
  1086. // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
  1087. // underlying stream does not support aliasing, then enabling it has no
  1088. // affect. For now, this only affects the behavior of
  1089. // WriteRawMaybeAliased().
  1090. //
  1091. // NOTE: It is caller's responsibility to ensure that the chunk of memory
  1092. // remains live until all of the data has been consumed from the stream.
  1093. void EnableAliasing(bool enabled) { impl_.EnableAliasing(enabled); }
  1094. // Indicate to the serializer whether the user wants derministic
  1095. // serialization. The default when this is not called comes from the global
  1096. // default, controlled by SetDefaultSerializationDeterministic.
  1097. //
  1098. // What deterministic serialization means is entirely up to the driver of the
  1099. // serialization process (i.e. the caller of methods like WriteVarint32). In
  1100. // the case of serializing a proto buffer message using one of the methods of
  1101. // MessageLite, this means that for a given binary equal messages will always
  1102. // be serialized to the same bytes. This implies:
  1103. //
  1104. // * Repeated serialization of a message will return the same bytes.
  1105. //
  1106. // * Different processes running the same binary (including on different
  1107. // machines) will serialize equal messages to the same bytes.
  1108. //
  1109. // Note that this is *not* canonical across languages. It is also unstable
  1110. // across different builds with intervening message definition changes, due to
  1111. // unknown fields. Users who need canonical serialization (e.g. persistent
  1112. // storage in a canonical form, fingerprinting) should define their own
  1113. // canonicalization specification and implement the serializer using
  1114. // reflection APIs rather than relying on this API.
  1115. void SetSerializationDeterministic(bool value) {
  1116. impl_.SetSerializationDeterministic(value);
  1117. }
  1118. // Return whether the user wants deterministic serialization. See above.
  1119. bool IsSerializationDeterministic() const {
  1120. return impl_.IsSerializationDeterministic();
  1121. }
  1122. static bool IsDefaultSerializationDeterministic() {
  1123. return default_serialization_deterministic_.load(
  1124. std::memory_order_relaxed) != 0;
  1125. }
  1126. template <typename Func>
  1127. void Serialize(const Func& func);
  1128. uint8_t* Cur() const { return cur_; }
  1129. void SetCur(uint8_t* ptr) { cur_ = ptr; }
  1130. EpsCopyOutputStream* EpsCopy() { return &impl_; }
  1131. private:
  1132. EpsCopyOutputStream impl_;
  1133. uint8_t* cur_;
  1134. int64_t start_count_;
  1135. static std::atomic<bool> default_serialization_deterministic_;
  1136. // See above. Other projects may use "friend" to allow them to call this.
  1137. // After SetDefaultSerializationDeterministic() completes, all protocol
  1138. // buffer serializations will be deterministic by default. Thread safe.
  1139. // However, the meaning of "after" is subtle here: to be safe, each thread
  1140. // that wants deterministic serialization by default needs to call
  1141. // SetDefaultSerializationDeterministic() or ensure on its own that another
  1142. // thread has done so.
  1143. friend void internal::MapTestForceDeterministic();
  1144. static void SetDefaultSerializationDeterministic() {
  1145. default_serialization_deterministic_.store(true, std::memory_order_relaxed);
  1146. }
  1147. // REQUIRES: value >= 0x80, and that (value & 7f) has been written to *target.
  1148. static uint8_t* WriteVarint32ToArrayOutOfLineHelper(uint32_t value,
  1149. uint8_t* target);
  1150. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
  1151. };
  1152. // inline methods ====================================================
  1153. // The vast majority of varints are only one byte. These inline
  1154. // methods optimize for that case.
  1155. inline bool CodedInputStream::ReadVarint32(uint32_t* value) {
  1156. uint32_t v = 0;
  1157. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1158. v = *buffer_;
  1159. if (v < 0x80) {
  1160. *value = v;
  1161. Advance(1);
  1162. return true;
  1163. }
  1164. }
  1165. int64_t result = ReadVarint32Fallback(v);
  1166. *value = static_cast<uint32_t>(result);
  1167. return result >= 0;
  1168. }
  1169. inline bool CodedInputStream::ReadVarint64(uint64_t* value) {
  1170. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
  1171. *value = *buffer_;
  1172. Advance(1);
  1173. return true;
  1174. }
  1175. std::pair<uint64_t, bool> p = ReadVarint64Fallback();
  1176. *value = p.first;
  1177. return p.second;
  1178. }
  1179. inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) {
  1180. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1181. int v = *buffer_;
  1182. if (v < 0x80) {
  1183. *value = v;
  1184. Advance(1);
  1185. return true;
  1186. }
  1187. }
  1188. *value = ReadVarintSizeAsIntFallback();
  1189. return *value >= 0;
  1190. }
  1191. // static
  1192. inline const uint8_t* CodedInputStream::ReadLittleEndian32FromArray(
  1193. const uint8_t* buffer, uint32_t* value) {
  1194. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1195. memcpy(value, buffer, sizeof(*value));
  1196. return buffer + sizeof(*value);
  1197. #else
  1198. *value = (static_cast<uint32_t>(buffer[0])) |
  1199. (static_cast<uint32_t>(buffer[1]) << 8) |
  1200. (static_cast<uint32_t>(buffer[2]) << 16) |
  1201. (static_cast<uint32_t>(buffer[3]) << 24);
  1202. return buffer + sizeof(*value);
  1203. #endif
  1204. }
  1205. // static
  1206. inline const uint8_t* CodedInputStream::ReadLittleEndian64FromArray(
  1207. const uint8_t* buffer, uint64_t* value) {
  1208. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1209. memcpy(value, buffer, sizeof(*value));
  1210. return buffer + sizeof(*value);
  1211. #else
  1212. uint32_t part0 = (static_cast<uint32_t>(buffer[0])) |
  1213. (static_cast<uint32_t>(buffer[1]) << 8) |
  1214. (static_cast<uint32_t>(buffer[2]) << 16) |
  1215. (static_cast<uint32_t>(buffer[3]) << 24);
  1216. uint32_t part1 = (static_cast<uint32_t>(buffer[4])) |
  1217. (static_cast<uint32_t>(buffer[5]) << 8) |
  1218. (static_cast<uint32_t>(buffer[6]) << 16) |
  1219. (static_cast<uint32_t>(buffer[7]) << 24);
  1220. *value = static_cast<uint64_t>(part0) | (static_cast<uint64_t>(part1) << 32);
  1221. return buffer + sizeof(*value);
  1222. #endif
  1223. }
  1224. inline bool CodedInputStream::ReadLittleEndian32(uint32_t* value) {
  1225. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1226. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  1227. buffer_ = ReadLittleEndian32FromArray(buffer_, value);
  1228. return true;
  1229. } else {
  1230. return ReadLittleEndian32Fallback(value);
  1231. }
  1232. #else
  1233. return ReadLittleEndian32Fallback(value);
  1234. #endif
  1235. }
  1236. inline bool CodedInputStream::ReadLittleEndian64(uint64_t* value) {
  1237. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1238. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  1239. buffer_ = ReadLittleEndian64FromArray(buffer_, value);
  1240. return true;
  1241. } else {
  1242. return ReadLittleEndian64Fallback(value);
  1243. }
  1244. #else
  1245. return ReadLittleEndian64Fallback(value);
  1246. #endif
  1247. }
  1248. inline uint32_t CodedInputStream::ReadTagNoLastTag() {
  1249. uint32_t v = 0;
  1250. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1251. v = *buffer_;
  1252. if (v < 0x80) {
  1253. Advance(1);
  1254. return v;
  1255. }
  1256. }
  1257. v = ReadTagFallback(v);
  1258. return v;
  1259. }
  1260. inline std::pair<uint32_t, bool> CodedInputStream::ReadTagWithCutoffNoLastTag(
  1261. uint32_t cutoff) {
  1262. // In performance-sensitive code we can expect cutoff to be a compile-time
  1263. // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at
  1264. // compile time.
  1265. uint32_t first_byte_or_zero = 0;
  1266. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1267. // Hot case: buffer_ non_empty, buffer_[0] in [1, 128).
  1268. // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields
  1269. // is large enough then is it better to check for the two-byte case first?
  1270. first_byte_or_zero = buffer_[0];
  1271. if (static_cast<int8_t>(buffer_[0]) > 0) {
  1272. const uint32_t kMax1ByteVarint = 0x7f;
  1273. uint32_t tag = buffer_[0];
  1274. Advance(1);
  1275. return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff);
  1276. }
  1277. // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available,
  1278. // and tag is two bytes. The latter is tested by bitwise-and-not of the
  1279. // first byte and the second byte.
  1280. if (cutoff >= 0x80 && PROTOBUF_PREDICT_TRUE(buffer_ + 1 < buffer_end_) &&
  1281. PROTOBUF_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) {
  1282. const uint32_t kMax2ByteVarint = (0x7f << 7) + 0x7f;
  1283. uint32_t tag = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80);
  1284. Advance(2);
  1285. // It might make sense to test for tag == 0 now, but it is so rare that
  1286. // that we don't bother. A varint-encoded 0 should be one byte unless
  1287. // the encoder lost its mind. The second part of the return value of
  1288. // this function is allowed to be either true or false if the tag is 0,
  1289. // so we don't have to check for tag == 0. We may need to check whether
  1290. // it exceeds cutoff.
  1291. bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff;
  1292. return std::make_pair(tag, at_or_below_cutoff);
  1293. }
  1294. }
  1295. // Slow path
  1296. const uint32_t tag = ReadTagFallback(first_byte_or_zero);
  1297. return std::make_pair(tag, static_cast<uint32_t>(tag - 1) < cutoff);
  1298. }
  1299. inline bool CodedInputStream::LastTagWas(uint32_t expected) {
  1300. return last_tag_ == expected;
  1301. }
  1302. inline bool CodedInputStream::ConsumedEntireMessage() {
  1303. return legitimate_message_end_;
  1304. }
  1305. inline bool CodedInputStream::ExpectTag(uint32_t expected) {
  1306. if (expected < (1 << 7)) {
  1307. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) &&
  1308. buffer_[0] == expected) {
  1309. Advance(1);
  1310. return true;
  1311. } else {
  1312. return false;
  1313. }
  1314. } else if (expected < (1 << 14)) {
  1315. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= 2) &&
  1316. buffer_[0] == static_cast<uint8_t>(expected | 0x80) &&
  1317. buffer_[1] == static_cast<uint8_t>(expected >> 7)) {
  1318. Advance(2);
  1319. return true;
  1320. } else {
  1321. return false;
  1322. }
  1323. } else {
  1324. // Don't bother optimizing for larger values.
  1325. return false;
  1326. }
  1327. }
  1328. inline const uint8_t* CodedInputStream::ExpectTagFromArray(
  1329. const uint8_t* buffer, uint32_t expected) {
  1330. if (expected < (1 << 7)) {
  1331. if (buffer[0] == expected) {
  1332. return buffer + 1;
  1333. }
  1334. } else if (expected < (1 << 14)) {
  1335. if (buffer[0] == static_cast<uint8_t>(expected | 0x80) &&
  1336. buffer[1] == static_cast<uint8_t>(expected >> 7)) {
  1337. return buffer + 2;
  1338. }
  1339. }
  1340. return nullptr;
  1341. }
  1342. inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
  1343. int* size) {
  1344. *data = buffer_;
  1345. *size = static_cast<int>(buffer_end_ - buffer_);
  1346. }
  1347. inline bool CodedInputStream::ExpectAtEnd() {
  1348. // If we are at a limit we know no more bytes can be read. Otherwise, it's
  1349. // hard to say without calling Refresh(), and we'd rather not do that.
  1350. if (buffer_ == buffer_end_ && ((buffer_size_after_limit_ != 0) ||
  1351. (total_bytes_read_ == current_limit_))) {
  1352. last_tag_ = 0; // Pretend we called ReadTag()...
  1353. legitimate_message_end_ = true; // ... and it hit EOF.
  1354. return true;
  1355. } else {
  1356. return false;
  1357. }
  1358. }
  1359. inline int CodedInputStream::CurrentPosition() const {
  1360. return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_);
  1361. }
  1362. inline void CodedInputStream::Advance(int amount) { buffer_ += amount; }
  1363. inline void CodedInputStream::SetRecursionLimit(int limit) {
  1364. recursion_budget_ += limit - recursion_limit_;
  1365. recursion_limit_ = limit;
  1366. }
  1367. inline bool CodedInputStream::IncrementRecursionDepth() {
  1368. --recursion_budget_;
  1369. return recursion_budget_ >= 0;
  1370. }
  1371. inline void CodedInputStream::DecrementRecursionDepth() {
  1372. if (recursion_budget_ < recursion_limit_) ++recursion_budget_;
  1373. }
  1374. inline void CodedInputStream::UnsafeDecrementRecursionDepth() {
  1375. assert(recursion_budget_ < recursion_limit_);
  1376. ++recursion_budget_;
  1377. }
  1378. inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
  1379. MessageFactory* factory) {
  1380. extension_pool_ = pool;
  1381. extension_factory_ = factory;
  1382. }
  1383. inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
  1384. return extension_pool_;
  1385. }
  1386. inline MessageFactory* CodedInputStream::GetExtensionFactory() {
  1387. return extension_factory_;
  1388. }
  1389. inline int CodedInputStream::BufferSize() const {
  1390. return static_cast<int>(buffer_end_ - buffer_);
  1391. }
  1392. inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
  1393. : buffer_(nullptr),
  1394. buffer_end_(nullptr),
  1395. input_(input),
  1396. total_bytes_read_(0),
  1397. overflow_bytes_(0),
  1398. last_tag_(0),
  1399. legitimate_message_end_(false),
  1400. aliasing_enabled_(false),
  1401. current_limit_(std::numeric_limits<int32_t>::max()),
  1402. buffer_size_after_limit_(0),
  1403. total_bytes_limit_(kDefaultTotalBytesLimit),
  1404. recursion_budget_(default_recursion_limit_),
  1405. recursion_limit_(default_recursion_limit_),
  1406. extension_pool_(nullptr),
  1407. extension_factory_(nullptr) {
  1408. // Eagerly Refresh() so buffer space is immediately available.
  1409. Refresh();
  1410. }
  1411. inline CodedInputStream::CodedInputStream(const uint8_t* buffer, int size)
  1412. : buffer_(buffer),
  1413. buffer_end_(buffer + size),
  1414. input_(nullptr),
  1415. total_bytes_read_(size),
  1416. overflow_bytes_(0),
  1417. last_tag_(0),
  1418. legitimate_message_end_(false),
  1419. aliasing_enabled_(false),
  1420. current_limit_(size),
  1421. buffer_size_after_limit_(0),
  1422. total_bytes_limit_(kDefaultTotalBytesLimit),
  1423. recursion_budget_(default_recursion_limit_),
  1424. recursion_limit_(default_recursion_limit_),
  1425. extension_pool_(nullptr),
  1426. extension_factory_(nullptr) {
  1427. // Note that setting current_limit_ == size is important to prevent some
  1428. // code paths from trying to access input_ and segfaulting.
  1429. }
  1430. inline bool CodedInputStream::IsFlat() const { return input_ == nullptr; }
  1431. inline bool CodedInputStream::Skip(int count) {
  1432. if (count < 0) return false; // security: count is often user-supplied
  1433. const int original_buffer_size = BufferSize();
  1434. if (count <= original_buffer_size) {
  1435. // Just skipping within the current buffer. Easy.
  1436. Advance(count);
  1437. return true;
  1438. }
  1439. return SkipFallback(count, original_buffer_size);
  1440. }
  1441. inline uint8_t* CodedOutputStream::WriteVarint32ToArray(uint32_t value,
  1442. uint8_t* target) {
  1443. return EpsCopyOutputStream::UnsafeVarint(value, target);
  1444. }
  1445. inline uint8_t* CodedOutputStream::WriteVarint32ToArrayOutOfLine(
  1446. uint32_t value, uint8_t* target) {
  1447. target[0] = static_cast<uint8_t>(value);
  1448. if (value < 0x80) {
  1449. return target + 1;
  1450. } else {
  1451. return WriteVarint32ToArrayOutOfLineHelper(value, target);
  1452. }
  1453. }
  1454. inline uint8_t* CodedOutputStream::WriteVarint64ToArray(uint64_t value,
  1455. uint8_t* target) {
  1456. return EpsCopyOutputStream::UnsafeVarint(value, target);
  1457. }
  1458. inline void CodedOutputStream::WriteVarint32SignExtended(int32_t value) {
  1459. WriteVarint64(static_cast<uint64_t>(value));
  1460. }
  1461. inline uint8_t* CodedOutputStream::WriteVarint32SignExtendedToArray(
  1462. int32_t value, uint8_t* target) {
  1463. return WriteVarint64ToArray(static_cast<uint64_t>(value), target);
  1464. }
  1465. inline uint8_t* CodedOutputStream::WriteLittleEndian32ToArray(uint32_t value,
  1466. uint8_t* target) {
  1467. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1468. memcpy(target, &value, sizeof(value));
  1469. #else
  1470. target[0] = static_cast<uint8_t>(value);
  1471. target[1] = static_cast<uint8_t>(value >> 8);
  1472. target[2] = static_cast<uint8_t>(value >> 16);
  1473. target[3] = static_cast<uint8_t>(value >> 24);
  1474. #endif
  1475. return target + sizeof(value);
  1476. }
  1477. inline uint8_t* CodedOutputStream::WriteLittleEndian64ToArray(uint64_t value,
  1478. uint8_t* target) {
  1479. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1480. memcpy(target, &value, sizeof(value));
  1481. #else
  1482. uint32_t part0 = static_cast<uint32_t>(value);
  1483. uint32_t part1 = static_cast<uint32_t>(value >> 32);
  1484. target[0] = static_cast<uint8_t>(part0);
  1485. target[1] = static_cast<uint8_t>(part0 >> 8);
  1486. target[2] = static_cast<uint8_t>(part0 >> 16);
  1487. target[3] = static_cast<uint8_t>(part0 >> 24);
  1488. target[4] = static_cast<uint8_t>(part1);
  1489. target[5] = static_cast<uint8_t>(part1 >> 8);
  1490. target[6] = static_cast<uint8_t>(part1 >> 16);
  1491. target[7] = static_cast<uint8_t>(part1 >> 24);
  1492. #endif
  1493. return target + sizeof(value);
  1494. }
  1495. inline void CodedOutputStream::WriteVarint32(uint32_t value) {
  1496. cur_ = impl_.EnsureSpace(cur_);
  1497. SetCur(WriteVarint32ToArray(value, Cur()));
  1498. }
  1499. inline void CodedOutputStream::WriteVarint64(uint64_t value) {
  1500. cur_ = impl_.EnsureSpace(cur_);
  1501. SetCur(WriteVarint64ToArray(value, Cur()));
  1502. }
  1503. inline void CodedOutputStream::WriteTag(uint32_t value) {
  1504. WriteVarint32(value);
  1505. }
  1506. inline uint8_t* CodedOutputStream::WriteTagToArray(uint32_t value,
  1507. uint8_t* target) {
  1508. return WriteVarint32ToArray(value, target);
  1509. }
  1510. inline size_t CodedOutputStream::VarintSize32(uint32_t value) {
  1511. // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
  1512. // Use an explicit multiplication to implement the divide of
  1513. // a number in the 1..31 range.
  1514. // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
  1515. // undefined.
  1516. uint32_t log2value = Bits::Log2FloorNonZero(value | 0x1);
  1517. return static_cast<size_t>((log2value * 9 + 73) / 64);
  1518. }
  1519. inline size_t CodedOutputStream::VarintSize32PlusOne(uint32_t value) {
  1520. // Same as above, but one more.
  1521. uint32_t log2value = Bits::Log2FloorNonZero(value | 0x1);
  1522. return static_cast<size_t>((log2value * 9 + 73 + 64) / 64);
  1523. }
  1524. inline size_t CodedOutputStream::VarintSize64(uint64_t value) {
  1525. // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
  1526. // Use an explicit multiplication to implement the divide of
  1527. // a number in the 1..63 range.
  1528. // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
  1529. // undefined.
  1530. uint32_t log2value = Bits::Log2FloorNonZero64(value | 0x1);
  1531. return static_cast<size_t>((log2value * 9 + 73) / 64);
  1532. }
  1533. inline size_t CodedOutputStream::VarintSize64PlusOne(uint64_t value) {
  1534. // Same as above, but one more.
  1535. uint32_t log2value = Bits::Log2FloorNonZero64(value | 0x1);
  1536. return static_cast<size_t>((log2value * 9 + 73 + 64) / 64);
  1537. }
  1538. inline size_t CodedOutputStream::VarintSize32SignExtended(int32_t value) {
  1539. return VarintSize64(static_cast<uint64_t>(int64_t{value}));
  1540. }
  1541. inline size_t CodedOutputStream::VarintSize32SignExtendedPlusOne(
  1542. int32_t value) {
  1543. return VarintSize64PlusOne(static_cast<uint64_t>(int64_t{value}));
  1544. }
  1545. inline void CodedOutputStream::WriteString(const std::string& str) {
  1546. WriteRaw(str.data(), static_cast<int>(str.size()));
  1547. }
  1548. inline void CodedOutputStream::WriteRawMaybeAliased(const void* data,
  1549. int size) {
  1550. cur_ = impl_.WriteRawMaybeAliased(data, size, cur_);
  1551. }
  1552. inline uint8_t* CodedOutputStream::WriteRawToArray(const void* data, int size,
  1553. uint8_t* target) {
  1554. memcpy(target, data, size);
  1555. return target + size;
  1556. }
  1557. inline uint8_t* CodedOutputStream::WriteStringToArray(const std::string& str,
  1558. uint8_t* target) {
  1559. return WriteRawToArray(str.data(), static_cast<int>(str.size()), target);
  1560. }
  1561. } // namespace io
  1562. } // namespace protobuf
  1563. } // namespace google
  1564. #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
  1565. #pragma runtime_checks("c", restore)
  1566. #endif // _MSC_VER && !defined(__INTEL_COMPILER)
  1567. #include <google/protobuf/port_undef.inc>
  1568. #endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__