coded_stream.h 70 KB

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