coded_stream.h 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 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* 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* value);
  212. // Read a 64-bit little-endian integer.
  213. bool ReadLittleEndian64(uint64* 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* ReadLittleEndian32FromArray(const uint8* buffer,
  218. uint32* value);
  219. // Read a 64-bit little-endian integer.
  220. static const uint8* ReadLittleEndian64FromArray(const uint8* buffer,
  221. uint64* 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, but may be more efficient.
  225. bool ReadVarint32(uint32* value);
  226. // Read an unsigned integer with Varint encoding.
  227. bool ReadVarint64(uint64* 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 ReadTag() {
  247. return last_tag_ = ReadTagNoLastTag();
  248. }
  249. PROTOBUF_ALWAYS_INLINE uint32 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, bool> ReadTagWithCutoff(uint32 cutoff) {
  260. std::pair<uint32, bool> result = ReadTagWithCutoffNoLastTag(cutoff);
  261. last_tag_ = result.first;
  262. return result;
  263. }
  264. PROTOBUF_ALWAYS_INLINE
  265. std::pair<uint32, bool> ReadTagWithCutoffNoLastTag(uint32 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 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* ExpectTagFromArray(const uint8* buffer, uint32 expected);
  283. // Usually returns true if no more bytes can be read. Always returns false
  284. // if more bytes can be read. If ExpectAtEnd() returns true, a subsequent
  285. // call to LastTagWas() will act as if ReadTag() had been called and returned
  286. // zero, and ConsumedEntireMessage() will return true.
  287. bool ExpectAtEnd();
  288. // If the last call to ReadTag() or ReadTagWithCutoff() returned the given
  289. // value, returns true. Otherwise, returns false.
  290. // ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last
  291. // returned value.
  292. //
  293. // This is needed because parsers for some types of embedded messages
  294. // (with field type TYPE_GROUP) don't actually know that they've reached the
  295. // end of a message until they see an ENDGROUP tag, which was actually part
  296. // of the enclosing message. The enclosing message would like to check that
  297. // tag to make sure it had the right number, so it calls LastTagWas() on
  298. // return from the embedded parser to check.
  299. bool LastTagWas(uint32 expected);
  300. void SetLastTag(uint32 tag) { last_tag_ = tag; }
  301. // When parsing message (but NOT a group), this method must be called
  302. // immediately after MergeFromCodedStream() returns (if it returns true)
  303. // to further verify that the message ended in a legitimate way. For
  304. // example, this verifies that parsing did not end on an end-group tag.
  305. // It also checks for some cases where, due to optimizations,
  306. // MergeFromCodedStream() can incorrectly return true.
  307. bool ConsumedEntireMessage();
  308. void SetConsumed() { legitimate_message_end_ = true; }
  309. // Limits ----------------------------------------------------------
  310. // Limits are used when parsing length-delimited embedded messages.
  311. // After the message's length is read, PushLimit() is used to prevent
  312. // the CodedInputStream from reading beyond that length. Once the
  313. // embedded message has been parsed, PopLimit() is called to undo the
  314. // limit.
  315. // Opaque type used with PushLimit() and PopLimit(). Do not modify
  316. // values of this type yourself. The only reason that this isn't a
  317. // struct with private internals is for efficiency.
  318. typedef int Limit;
  319. // Places a limit on the number of bytes that the stream may read,
  320. // starting from the current position. Once the stream hits this limit,
  321. // it will act like the end of the input has been reached until PopLimit()
  322. // is called.
  323. //
  324. // As the names imply, the stream conceptually has a stack of limits. The
  325. // shortest limit on the stack is always enforced, even if it is not the
  326. // top limit.
  327. //
  328. // The value returned by PushLimit() is opaque to the caller, and must
  329. // be passed unchanged to the corresponding call to PopLimit().
  330. Limit PushLimit(int byte_limit);
  331. // Pops the last limit pushed by PushLimit(). The input must be the value
  332. // returned by that call to PushLimit().
  333. void PopLimit(Limit limit);
  334. // Returns the number of bytes left until the nearest limit on the
  335. // stack is hit, or -1 if no limits are in place.
  336. int BytesUntilLimit() const;
  337. // Returns current position relative to the beginning of the input stream.
  338. int CurrentPosition() const;
  339. // Total Bytes Limit -----------------------------------------------
  340. // To prevent malicious users from sending excessively large messages
  341. // and causing memory exhaustion, CodedInputStream imposes a hard limit on
  342. // the total number of bytes it will read.
  343. // Sets the maximum number of bytes that this CodedInputStream will read
  344. // before refusing to continue. To prevent servers from allocating enormous
  345. // amounts of memory to hold parsed messages, the maximum message length
  346. // should be limited to the shortest length that will not harm usability.
  347. // The default limit is INT_MAX (~2GB) and apps should set shorter limits
  348. // if possible. An error will always be printed to stderr if the limit is
  349. // reached.
  350. //
  351. // Note: setting a limit less than the current read position is interpreted
  352. // as a limit on the current position.
  353. //
  354. // This is unrelated to PushLimit()/PopLimit().
  355. void SetTotalBytesLimit(int total_bytes_limit);
  356. PROTOBUF_DEPRECATED_MSG(
  357. "Please use the single parameter version of SetTotalBytesLimit(). The "
  358. "second parameter is ignored.")
  359. void SetTotalBytesLimit(int total_bytes_limit, int) {
  360. SetTotalBytesLimit(total_bytes_limit);
  361. }
  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* buffer_;
  484. const uint8* 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 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 it read otherwise. The latter has a bool
  546. // indicating success or failure as part of its return type.
  547. int64 ReadVarint32Fallback(uint32 first_byte_or_zero);
  548. int ReadVarintSizeAsIntFallback();
  549. std::pair<uint64, bool> ReadVarint64Fallback();
  550. bool ReadVarint32Slow(uint32* value);
  551. bool ReadVarint64Slow(uint64* value);
  552. int ReadVarintSizeAsIntSlow();
  553. bool ReadLittleEndian32Fallback(uint32* value);
  554. bool ReadLittleEndian64Fallback(uint64* 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 ReadTagFallback(uint32 first_byte_or_zero);
  559. uint32 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** 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*>(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** 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* Trim(uint8* 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_MUST_USE_RESULT uint8* EnsureSpace(uint8* ptr) {
  606. if (PROTOBUF_PREDICT_FALSE(ptr >= end_)) {
  607. return EnsureSpaceFallback(ptr);
  608. }
  609. return ptr;
  610. }
  611. uint8* WriteRaw(const void* data, int size, uint8* 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. uint8* WriteRawMaybeAliased(const void* data, int size, uint8* ptr) {
  623. if (aliasing_enabled_) {
  624. return WriteAliasedRaw(data, size, ptr);
  625. } else {
  626. return WriteRaw(data, size, ptr);
  627. }
  628. }
  629. uint8* WriteStringMaybeAliased(uint32 num, const std::string& s, uint8* ptr) {
  630. std::ptrdiff_t size = s.size();
  631. if (PROTOBUF_PREDICT_FALSE(
  632. size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
  633. return WriteStringMaybeAliasedOutline(num, s, ptr);
  634. }
  635. ptr = UnsafeVarint((num << 3) | 2, ptr);
  636. *ptr++ = static_cast<uint8>(size);
  637. std::memcpy(ptr, s.data(), size);
  638. return ptr + size;
  639. }
  640. uint8* WriteBytesMaybeAliased(uint32 num, const std::string& s, uint8* ptr) {
  641. return WriteStringMaybeAliased(num, s, ptr);
  642. }
  643. template <typename T>
  644. PROTOBUF_ALWAYS_INLINE uint8* WriteString(uint32 num, const T& s,
  645. uint8* ptr) {
  646. std::ptrdiff_t size = s.size();
  647. if (PROTOBUF_PREDICT_FALSE(
  648. size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
  649. return WriteStringOutline(num, s, ptr);
  650. }
  651. ptr = UnsafeVarint((num << 3) | 2, ptr);
  652. *ptr++ = static_cast<uint8>(size);
  653. std::memcpy(ptr, s.data(), size);
  654. return ptr + size;
  655. }
  656. template <typename T>
  657. uint8* WriteBytes(uint32 num, const T& s, uint8* ptr) {
  658. return WriteString(num, s, ptr);
  659. }
  660. template <typename T>
  661. PROTOBUF_ALWAYS_INLINE uint8* WriteInt32Packed(int num, const T& r, int size,
  662. uint8* ptr) {
  663. return WriteVarintPacked(num, r, size, ptr, Encode64);
  664. }
  665. template <typename T>
  666. PROTOBUF_ALWAYS_INLINE uint8* WriteUInt32Packed(int num, const T& r, int size,
  667. uint8* ptr) {
  668. return WriteVarintPacked(num, r, size, ptr, Encode32);
  669. }
  670. template <typename T>
  671. PROTOBUF_ALWAYS_INLINE uint8* WriteSInt32Packed(int num, const T& r, int size,
  672. uint8* ptr) {
  673. return WriteVarintPacked(num, r, size, ptr, ZigZagEncode32);
  674. }
  675. template <typename T>
  676. PROTOBUF_ALWAYS_INLINE uint8* WriteInt64Packed(int num, const T& r, int size,
  677. uint8* ptr) {
  678. return WriteVarintPacked(num, r, size, ptr, Encode64);
  679. }
  680. template <typename T>
  681. PROTOBUF_ALWAYS_INLINE uint8* WriteUInt64Packed(int num, const T& r, int size,
  682. uint8* ptr) {
  683. return WriteVarintPacked(num, r, size, ptr, Encode64);
  684. }
  685. template <typename T>
  686. PROTOBUF_ALWAYS_INLINE uint8* WriteSInt64Packed(int num, const T& r, int size,
  687. uint8* ptr) {
  688. return WriteVarintPacked(num, r, size, ptr, ZigZagEncode64);
  689. }
  690. template <typename T>
  691. PROTOBUF_ALWAYS_INLINE uint8* WriteEnumPacked(int num, const T& r, int size,
  692. uint8* ptr) {
  693. return WriteVarintPacked(num, r, size, ptr, Encode64);
  694. }
  695. template <typename T>
  696. PROTOBUF_ALWAYS_INLINE uint8* WriteFixedPacked(int num, const T& r,
  697. uint8* ptr) {
  698. ptr = EnsureSpace(ptr);
  699. constexpr auto element_size = sizeof(typename T::value_type);
  700. auto size = r.size() * element_size;
  701. ptr = WriteLengthDelim(num, size, ptr);
  702. return WriteRawLittleEndian<element_size>(r.data(), static_cast<int>(size),
  703. ptr);
  704. }
  705. // Returns true if there was an underlying I/O error since this object was
  706. // created.
  707. bool HadError() const { return had_error_; }
  708. // Instructs the EpsCopyOutputStream to allow the underlying
  709. // ZeroCopyOutputStream to hold pointers to the original structure instead of
  710. // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
  711. // underlying stream does not support aliasing, then enabling it has no
  712. // affect. For now, this only affects the behavior of
  713. // WriteRawMaybeAliased().
  714. //
  715. // NOTE: It is caller's responsibility to ensure that the chunk of memory
  716. // remains live until all of the data has been consumed from the stream.
  717. void EnableAliasing(bool enabled);
  718. // See documentation on CodedOutputStream::SetSerializationDeterministic.
  719. void SetSerializationDeterministic(bool value) {
  720. is_serialization_deterministic_ = value;
  721. }
  722. // See documentation on CodedOutputStream::IsSerializationDeterministic.
  723. bool IsSerializationDeterministic() const {
  724. return is_serialization_deterministic_;
  725. }
  726. // The number of bytes written to the stream at position ptr, relative to the
  727. // stream's overall position.
  728. int64 ByteCount(uint8* ptr) const;
  729. private:
  730. uint8* end_;
  731. uint8* buffer_end_ = buffer_;
  732. uint8 buffer_[2 * kSlopBytes];
  733. ZeroCopyOutputStream* stream_;
  734. bool had_error_ = false;
  735. bool aliasing_enabled_ = false; // See EnableAliasing().
  736. bool is_serialization_deterministic_;
  737. uint8* EnsureSpaceFallback(uint8* ptr);
  738. inline uint8* Next();
  739. int Flush(uint8* ptr);
  740. std::ptrdiff_t GetSize(uint8* ptr) const {
  741. GOOGLE_DCHECK(ptr <= end_ + kSlopBytes); // NOLINT
  742. return end_ + kSlopBytes - ptr;
  743. }
  744. uint8* Error() {
  745. had_error_ = true;
  746. // We use the patch buffer to always guarantee space to write to.
  747. end_ = buffer_ + kSlopBytes;
  748. return buffer_;
  749. }
  750. static constexpr int TagSize(uint32 tag) {
  751. return (tag < (1 << 7)) ? 1
  752. : (tag < (1 << 14)) ? 2
  753. : (tag < (1 << 21)) ? 3
  754. : (tag < (1 << 28)) ? 4
  755. : 5;
  756. }
  757. PROTOBUF_ALWAYS_INLINE uint8* WriteTag(uint32 num, uint32 wt, uint8* ptr) {
  758. GOOGLE_DCHECK(ptr < end_); // NOLINT
  759. return UnsafeVarint((num << 3) | wt, ptr);
  760. }
  761. PROTOBUF_ALWAYS_INLINE uint8* WriteLengthDelim(int num, uint32 size,
  762. uint8* ptr) {
  763. ptr = WriteTag(num, 2, ptr);
  764. return UnsafeWriteSize(size, ptr);
  765. }
  766. uint8* WriteRawFallback(const void* data, int size, uint8* ptr);
  767. uint8* WriteAliasedRaw(const void* data, int size, uint8* ptr);
  768. uint8* WriteStringMaybeAliasedOutline(uint32 num, const std::string& s,
  769. uint8* ptr);
  770. uint8* WriteStringOutline(uint32 num, const std::string& s, uint8* ptr);
  771. template <typename T, typename E>
  772. PROTOBUF_ALWAYS_INLINE uint8* WriteVarintPacked(int num, const T& r, int size,
  773. uint8* ptr, const E& encode) {
  774. ptr = EnsureSpace(ptr);
  775. ptr = WriteLengthDelim(num, size, ptr);
  776. auto it = r.data();
  777. auto end = it + r.size();
  778. do {
  779. ptr = EnsureSpace(ptr);
  780. ptr = UnsafeVarint(encode(*it++), ptr);
  781. } while (it < end);
  782. return ptr;
  783. }
  784. static uint32 Encode32(uint32 v) { return v; }
  785. static uint64 Encode64(uint64 v) { return v; }
  786. static uint32 ZigZagEncode32(int32 v) {
  787. return (static_cast<uint32>(v) << 1) ^ static_cast<uint32>(v >> 31);
  788. }
  789. static uint64 ZigZagEncode64(int64 v) {
  790. return (static_cast<uint64>(v) << 1) ^ static_cast<uint64>(v >> 63);
  791. }
  792. template <typename T>
  793. PROTOBUF_ALWAYS_INLINE static uint8* UnsafeVarint(T value, uint8* ptr) {
  794. static_assert(std::is_unsigned<T>::value,
  795. "Varint serialization must be unsigned");
  796. ptr[0] = static_cast<uint8>(value);
  797. if (value < 0x80) {
  798. return ptr + 1;
  799. }
  800. // Turn on continuation bit in the byte we just wrote.
  801. ptr[0] |= static_cast<uint8>(0x80);
  802. value >>= 7;
  803. ptr[1] = static_cast<uint8>(value);
  804. if (value < 0x80) {
  805. return ptr + 2;
  806. }
  807. ptr += 2;
  808. do {
  809. // Turn on continuation bit in the byte we just wrote.
  810. ptr[-1] |= static_cast<uint8>(0x80);
  811. value >>= 7;
  812. *ptr = static_cast<uint8>(value);
  813. ++ptr;
  814. } while (value >= 0x80);
  815. return ptr;
  816. }
  817. PROTOBUF_ALWAYS_INLINE static uint8* UnsafeWriteSize(uint32 value,
  818. uint8* ptr) {
  819. while (PROTOBUF_PREDICT_FALSE(value >= 0x80)) {
  820. *ptr = static_cast<uint8>(value | 0x80);
  821. value >>= 7;
  822. ++ptr;
  823. }
  824. *ptr++ = static_cast<uint8>(value);
  825. return ptr;
  826. }
  827. template <int S>
  828. uint8* WriteRawLittleEndian(const void* data, int size, uint8* ptr);
  829. #ifndef PROTOBUF_LITTLE_ENDIAN
  830. uint8* WriteRawLittleEndian32(const void* data, int size, uint8* ptr);
  831. uint8* WriteRawLittleEndian64(const void* data, int size, uint8* ptr);
  832. #endif
  833. // These methods are for CodedOutputStream. Ideally they should be private
  834. // but to match current behavior of CodedOutputStream as close as possible
  835. // we allow it some functionality.
  836. public:
  837. uint8* SetInitialBuffer(void* data, int size) {
  838. auto ptr = static_cast<uint8*>(data);
  839. if (size > kSlopBytes) {
  840. end_ = ptr + size - kSlopBytes;
  841. buffer_end_ = nullptr;
  842. return ptr;
  843. } else {
  844. end_ = buffer_ + size;
  845. buffer_end_ = ptr;
  846. return buffer_;
  847. }
  848. }
  849. private:
  850. // Needed by CodedOutputStream HadError. HadError needs to flush the patch
  851. // buffers to ensure there is no error as of yet.
  852. uint8* FlushAndResetBuffer(uint8*);
  853. // The following functions mimic the old CodedOutputStream behavior as close
  854. // as possible. They flush the current state to the stream, behave as
  855. // the old CodedOutputStream and then return to normal operation.
  856. bool Skip(int count, uint8** pp);
  857. bool GetDirectBufferPointer(void** data, int* size, uint8** pp);
  858. uint8* GetDirectBufferForNBytesAndAdvance(int size, uint8** pp);
  859. friend class CodedOutputStream;
  860. };
  861. template <>
  862. inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<1>(const void* data,
  863. int size,
  864. uint8* ptr) {
  865. return WriteRaw(data, size, ptr);
  866. }
  867. template <>
  868. inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<4>(const void* data,
  869. int size,
  870. uint8* ptr) {
  871. #ifdef PROTOBUF_LITTLE_ENDIAN
  872. return WriteRaw(data, size, ptr);
  873. #else
  874. return WriteRawLittleEndian32(data, size, ptr);
  875. #endif
  876. }
  877. template <>
  878. inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<8>(const void* data,
  879. int size,
  880. uint8* ptr) {
  881. #ifdef PROTOBUF_LITTLE_ENDIAN
  882. return WriteRaw(data, size, ptr);
  883. #else
  884. return WriteRawLittleEndian64(data, size, ptr);
  885. #endif
  886. }
  887. // Class which encodes and writes binary data which is composed of varint-
  888. // encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream.
  889. // Most users will not need to deal with CodedOutputStream.
  890. //
  891. // Most methods of CodedOutputStream which return a bool return false if an
  892. // underlying I/O error occurs. Once such a failure occurs, the
  893. // CodedOutputStream is broken and is no longer useful. The Write* methods do
  894. // not return the stream status, but will invalidate the stream if an error
  895. // occurs. The client can probe HadError() to determine the status.
  896. //
  897. // Note that every method of CodedOutputStream which writes some data has
  898. // a corresponding static "ToArray" version. These versions write directly
  899. // to the provided buffer, returning a pointer past the last written byte.
  900. // They require that the buffer has sufficient capacity for the encoded data.
  901. // This allows an optimization where we check if an output stream has enough
  902. // space for an entire message before we start writing and, if there is, we
  903. // call only the ToArray methods to avoid doing bound checks for each
  904. // individual value.
  905. // i.e., in the example above:
  906. //
  907. // CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  908. // int magic_number = 1234;
  909. // char text[] = "Hello world!";
  910. //
  911. // int coded_size = sizeof(magic_number) +
  912. // CodedOutputStream::VarintSize32(strlen(text)) +
  913. // strlen(text);
  914. //
  915. // uint8* buffer =
  916. // coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
  917. // if (buffer != nullptr) {
  918. // // The output stream has enough space in the buffer: write directly to
  919. // // the array.
  920. // buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
  921. // buffer);
  922. // buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
  923. // buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
  924. // } else {
  925. // // Make bound-checked writes, which will ask the underlying stream for
  926. // // more space as needed.
  927. // coded_output->WriteLittleEndian32(magic_number);
  928. // coded_output->WriteVarint32(strlen(text));
  929. // coded_output->WriteRaw(text, strlen(text));
  930. // }
  931. //
  932. // delete coded_output;
  933. class PROTOBUF_EXPORT CodedOutputStream {
  934. public:
  935. // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
  936. explicit CodedOutputStream(ZeroCopyOutputStream* stream)
  937. : CodedOutputStream(stream, true) {}
  938. CodedOutputStream(ZeroCopyOutputStream* stream, bool do_eager_refresh);
  939. // Destroy the CodedOutputStream and position the underlying
  940. // ZeroCopyOutputStream immediately after the last byte written.
  941. ~CodedOutputStream();
  942. // Returns true if there was an underlying I/O error since this object was
  943. // created. On should call Trim before this function in order to catch all
  944. // errors.
  945. bool HadError() {
  946. cur_ = impl_.FlushAndResetBuffer(cur_);
  947. GOOGLE_DCHECK(cur_);
  948. return impl_.HadError();
  949. }
  950. // Trims any unused space in the underlying buffer so that its size matches
  951. // the number of bytes written by this stream. The underlying buffer will
  952. // automatically be trimmed when this stream is destroyed; this call is only
  953. // necessary if the underlying buffer is accessed *before* the stream is
  954. // destroyed.
  955. void Trim() { cur_ = impl_.Trim(cur_); }
  956. // Skips a number of bytes, leaving the bytes unmodified in the underlying
  957. // buffer. Returns false if an underlying write error occurs. This is
  958. // mainly useful with GetDirectBufferPointer().
  959. // Note of caution, the skipped bytes may contain uninitialized data. The
  960. // caller must make sure that the skipped bytes are properly initialized,
  961. // otherwise you might leak bytes from your heap.
  962. bool Skip(int count) { return impl_.Skip(count, &cur_); }
  963. // Sets *data to point directly at the unwritten part of the
  964. // CodedOutputStream's underlying buffer, and *size to the size of that
  965. // buffer, but does not advance the stream's current position. This will
  966. // always either produce a non-empty buffer or return false. If the caller
  967. // writes any data to this buffer, it should then call Skip() to skip over
  968. // the consumed bytes. This may be useful for implementing external fast
  969. // serialization routines for types of data not covered by the
  970. // CodedOutputStream interface.
  971. bool GetDirectBufferPointer(void** data, int* size) {
  972. return impl_.GetDirectBufferPointer(data, size, &cur_);
  973. }
  974. // If there are at least "size" bytes available in the current buffer,
  975. // returns a pointer directly into the buffer and advances over these bytes.
  976. // The caller may then write directly into this buffer (e.g. using the
  977. // *ToArray static methods) rather than go through CodedOutputStream. If
  978. // there are not enough bytes available, returns NULL. The return pointer is
  979. // invalidated as soon as any other non-const method of CodedOutputStream
  980. // is called.
  981. inline uint8* GetDirectBufferForNBytesAndAdvance(int size) {
  982. return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_);
  983. }
  984. // Write raw bytes, copying them from the given buffer.
  985. void WriteRaw(const void* buffer, int size) {
  986. cur_ = impl_.WriteRaw(buffer, size, cur_);
  987. }
  988. // Like WriteRaw() but will try to write aliased data if aliasing is
  989. // turned on.
  990. void WriteRawMaybeAliased(const void* data, int size);
  991. // Like WriteRaw() but writing directly to the target array.
  992. // This is _not_ inlined, as the compiler often optimizes memcpy into inline
  993. // copy loops. Since this gets called by every field with string or bytes
  994. // type, inlining may lead to a significant amount of code bloat, with only a
  995. // minor performance gain.
  996. static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
  997. // Equivalent to WriteRaw(str.data(), str.size()).
  998. void WriteString(const std::string& str);
  999. // Like WriteString() but writing directly to the target array.
  1000. static uint8* WriteStringToArray(const std::string& str, uint8* target);
  1001. // Write the varint-encoded size of str followed by str.
  1002. static uint8* WriteStringWithSizeToArray(const std::string& str,
  1003. uint8* target);
  1004. // Write a 32-bit little-endian integer.
  1005. void WriteLittleEndian32(uint32 value) {
  1006. cur_ = impl_.EnsureSpace(cur_);
  1007. SetCur(WriteLittleEndian32ToArray(value, Cur()));
  1008. }
  1009. // Like WriteLittleEndian32() but writing directly to the target array.
  1010. static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
  1011. // Write a 64-bit little-endian integer.
  1012. void WriteLittleEndian64(uint64 value) {
  1013. cur_ = impl_.EnsureSpace(cur_);
  1014. SetCur(WriteLittleEndian64ToArray(value, Cur()));
  1015. }
  1016. // Like WriteLittleEndian64() but writing directly to the target array.
  1017. static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
  1018. // Write an unsigned integer with Varint encoding. Writing a 32-bit value
  1019. // is equivalent to casting it to uint64 and writing it as a 64-bit value,
  1020. // but may be more efficient.
  1021. void WriteVarint32(uint32 value);
  1022. // Like WriteVarint32() but writing directly to the target array.
  1023. static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
  1024. // Like WriteVarint32() but writing directly to the target array, and with the
  1025. // less common-case paths being out of line rather than inlined.
  1026. static uint8* WriteVarint32ToArrayOutOfLine(uint32 value, uint8* target);
  1027. // Write an unsigned integer with Varint encoding.
  1028. void WriteVarint64(uint64 value);
  1029. // Like WriteVarint64() but writing directly to the target array.
  1030. static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
  1031. // Equivalent to WriteVarint32() except when the value is negative,
  1032. // in which case it must be sign-extended to a full 10 bytes.
  1033. void WriteVarint32SignExtended(int32 value);
  1034. // Like WriteVarint32SignExtended() but writing directly to the target array.
  1035. static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
  1036. // This is identical to WriteVarint32(), but optimized for writing tags.
  1037. // In particular, if the input is a compile-time constant, this method
  1038. // compiles down to a couple instructions.
  1039. // Always inline because otherwise the aforementioned optimization can't work,
  1040. // but GCC by default doesn't want to inline this.
  1041. void WriteTag(uint32 value);
  1042. // Like WriteTag() but writing directly to the target array.
  1043. PROTOBUF_ALWAYS_INLINE
  1044. static uint8* WriteTagToArray(uint32 value, uint8* target);
  1045. // Returns the number of bytes needed to encode the given value as a varint.
  1046. static size_t VarintSize32(uint32 value);
  1047. // Returns the number of bytes needed to encode the given value as a varint.
  1048. static size_t VarintSize64(uint64 value);
  1049. // If negative, 10 bytes. Otherwise, same as VarintSize32().
  1050. static size_t VarintSize32SignExtended(int32 value);
  1051. // Compile-time equivalent of VarintSize32().
  1052. template <uint32 Value>
  1053. struct StaticVarintSize32 {
  1054. static const size_t value = (Value < (1 << 7)) ? 1
  1055. : (Value < (1 << 14)) ? 2
  1056. : (Value < (1 << 21)) ? 3
  1057. : (Value < (1 << 28)) ? 4
  1058. : 5;
  1059. };
  1060. // Returns the total number of bytes written since this object was created.
  1061. int ByteCount() const {
  1062. return static_cast<int>(impl_.ByteCount(cur_) - start_count_);
  1063. }
  1064. // Instructs the CodedOutputStream to allow the underlying
  1065. // ZeroCopyOutputStream to hold pointers to the original structure instead of
  1066. // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
  1067. // underlying stream does not support aliasing, then enabling it has no
  1068. // affect. For now, this only affects the behavior of
  1069. // WriteRawMaybeAliased().
  1070. //
  1071. // NOTE: It is caller's responsibility to ensure that the chunk of memory
  1072. // remains live until all of the data has been consumed from the stream.
  1073. void EnableAliasing(bool enabled) { impl_.EnableAliasing(enabled); }
  1074. // Indicate to the serializer whether the user wants derministic
  1075. // serialization. The default when this is not called comes from the global
  1076. // default, controlled by SetDefaultSerializationDeterministic.
  1077. //
  1078. // What deterministic serialization means is entirely up to the driver of the
  1079. // serialization process (i.e. the caller of methods like WriteVarint32). In
  1080. // the case of serializing a proto buffer message using one of the methods of
  1081. // MessageLite, this means that for a given binary equal messages will always
  1082. // be serialized to the same bytes. This implies:
  1083. //
  1084. // * Repeated serialization of a message will return the same bytes.
  1085. //
  1086. // * Different processes running the same binary (including on different
  1087. // machines) will serialize equal messages to the same bytes.
  1088. //
  1089. // Note that this is *not* canonical across languages. It is also unstable
  1090. // across different builds with intervening message definition changes, due to
  1091. // unknown fields. Users who need canonical serialization (e.g. persistent
  1092. // storage in a canonical form, fingerprinting) should define their own
  1093. // canonicalization specification and implement the serializer using
  1094. // reflection APIs rather than relying on this API.
  1095. void SetSerializationDeterministic(bool value) {
  1096. impl_.SetSerializationDeterministic(value);
  1097. }
  1098. // Return whether the user wants deterministic serialization. See above.
  1099. bool IsSerializationDeterministic() const {
  1100. return impl_.IsSerializationDeterministic();
  1101. }
  1102. static bool IsDefaultSerializationDeterministic() {
  1103. return default_serialization_deterministic_.load(
  1104. std::memory_order_relaxed) != 0;
  1105. }
  1106. template <typename Func>
  1107. void Serialize(const Func& func);
  1108. uint8* Cur() const { return cur_; }
  1109. void SetCur(uint8* ptr) { cur_ = ptr; }
  1110. EpsCopyOutputStream* EpsCopy() { return &impl_; }
  1111. private:
  1112. EpsCopyOutputStream impl_;
  1113. uint8* cur_;
  1114. int64 start_count_;
  1115. static std::atomic<bool> default_serialization_deterministic_;
  1116. // See above. Other projects may use "friend" to allow them to call this.
  1117. // After SetDefaultSerializationDeterministic() completes, all protocol
  1118. // buffer serializations will be deterministic by default. Thread safe.
  1119. // However, the meaning of "after" is subtle here: to be safe, each thread
  1120. // that wants deterministic serialization by default needs to call
  1121. // SetDefaultSerializationDeterministic() or ensure on its own that another
  1122. // thread has done so.
  1123. friend void internal::MapTestForceDeterministic();
  1124. static void SetDefaultSerializationDeterministic() {
  1125. default_serialization_deterministic_.store(true, std::memory_order_relaxed);
  1126. }
  1127. // REQUIRES: value >= 0x80, and that (value & 7f) has been written to *target.
  1128. static uint8* WriteVarint32ToArrayOutOfLineHelper(uint32 value, uint8* target);
  1129. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
  1130. };
  1131. // inline methods ====================================================
  1132. // The vast majority of varints are only one byte. These inline
  1133. // methods optimize for that case.
  1134. inline bool CodedInputStream::ReadVarint32(uint32* value) {
  1135. uint32 v = 0;
  1136. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1137. v = *buffer_;
  1138. if (v < 0x80) {
  1139. *value = v;
  1140. Advance(1);
  1141. return true;
  1142. }
  1143. }
  1144. int64 result = ReadVarint32Fallback(v);
  1145. *value = static_cast<uint32>(result);
  1146. return result >= 0;
  1147. }
  1148. inline bool CodedInputStream::ReadVarint64(uint64* value) {
  1149. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
  1150. *value = *buffer_;
  1151. Advance(1);
  1152. return true;
  1153. }
  1154. std::pair<uint64, bool> p = ReadVarint64Fallback();
  1155. *value = p.first;
  1156. return p.second;
  1157. }
  1158. inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) {
  1159. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1160. int v = *buffer_;
  1161. if (v < 0x80) {
  1162. *value = v;
  1163. Advance(1);
  1164. return true;
  1165. }
  1166. }
  1167. *value = ReadVarintSizeAsIntFallback();
  1168. return *value >= 0;
  1169. }
  1170. // static
  1171. inline const uint8* CodedInputStream::ReadLittleEndian32FromArray(
  1172. const uint8* buffer, uint32* value) {
  1173. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1174. memcpy(value, buffer, sizeof(*value));
  1175. return buffer + sizeof(*value);
  1176. #else
  1177. *value = (static_cast<uint32>(buffer[0])) |
  1178. (static_cast<uint32>(buffer[1]) << 8) |
  1179. (static_cast<uint32>(buffer[2]) << 16) |
  1180. (static_cast<uint32>(buffer[3]) << 24);
  1181. return buffer + sizeof(*value);
  1182. #endif
  1183. }
  1184. // static
  1185. inline const uint8* CodedInputStream::ReadLittleEndian64FromArray(
  1186. const uint8* buffer, uint64* value) {
  1187. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1188. memcpy(value, buffer, sizeof(*value));
  1189. return buffer + sizeof(*value);
  1190. #else
  1191. uint32 part0 = (static_cast<uint32>(buffer[0])) |
  1192. (static_cast<uint32>(buffer[1]) << 8) |
  1193. (static_cast<uint32>(buffer[2]) << 16) |
  1194. (static_cast<uint32>(buffer[3]) << 24);
  1195. uint32 part1 = (static_cast<uint32>(buffer[4])) |
  1196. (static_cast<uint32>(buffer[5]) << 8) |
  1197. (static_cast<uint32>(buffer[6]) << 16) |
  1198. (static_cast<uint32>(buffer[7]) << 24);
  1199. *value = static_cast<uint64>(part0) | (static_cast<uint64>(part1) << 32);
  1200. return buffer + sizeof(*value);
  1201. #endif
  1202. }
  1203. inline bool CodedInputStream::ReadLittleEndian32(uint32* value) {
  1204. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1205. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  1206. buffer_ = ReadLittleEndian32FromArray(buffer_, value);
  1207. return true;
  1208. } else {
  1209. return ReadLittleEndian32Fallback(value);
  1210. }
  1211. #else
  1212. return ReadLittleEndian32Fallback(value);
  1213. #endif
  1214. }
  1215. inline bool CodedInputStream::ReadLittleEndian64(uint64* value) {
  1216. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1217. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  1218. buffer_ = ReadLittleEndian64FromArray(buffer_, value);
  1219. return true;
  1220. } else {
  1221. return ReadLittleEndian64Fallback(value);
  1222. }
  1223. #else
  1224. return ReadLittleEndian64Fallback(value);
  1225. #endif
  1226. }
  1227. inline uint32 CodedInputStream::ReadTagNoLastTag() {
  1228. uint32 v = 0;
  1229. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1230. v = *buffer_;
  1231. if (v < 0x80) {
  1232. Advance(1);
  1233. return v;
  1234. }
  1235. }
  1236. v = ReadTagFallback(v);
  1237. return v;
  1238. }
  1239. inline std::pair<uint32, bool> CodedInputStream::ReadTagWithCutoffNoLastTag(
  1240. uint32 cutoff) {
  1241. // In performance-sensitive code we can expect cutoff to be a compile-time
  1242. // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at
  1243. // compile time.
  1244. uint32 first_byte_or_zero = 0;
  1245. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
  1246. // Hot case: buffer_ non_empty, buffer_[0] in [1, 128).
  1247. // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields
  1248. // is large enough then is it better to check for the two-byte case first?
  1249. first_byte_or_zero = buffer_[0];
  1250. if (static_cast<int8>(buffer_[0]) > 0) {
  1251. const uint32 kMax1ByteVarint = 0x7f;
  1252. uint32 tag = buffer_[0];
  1253. Advance(1);
  1254. return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff);
  1255. }
  1256. // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available,
  1257. // and tag is two bytes. The latter is tested by bitwise-and-not of the
  1258. // first byte and the second byte.
  1259. if (cutoff >= 0x80 && PROTOBUF_PREDICT_TRUE(buffer_ + 1 < buffer_end_) &&
  1260. PROTOBUF_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) {
  1261. const uint32 kMax2ByteVarint = (0x7f << 7) + 0x7f;
  1262. uint32 tag = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80);
  1263. Advance(2);
  1264. // It might make sense to test for tag == 0 now, but it is so rare that
  1265. // that we don't bother. A varint-encoded 0 should be one byte unless
  1266. // the encoder lost its mind. The second part of the return value of
  1267. // this function is allowed to be either true or false if the tag is 0,
  1268. // so we don't have to check for tag == 0. We may need to check whether
  1269. // it exceeds cutoff.
  1270. bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff;
  1271. return std::make_pair(tag, at_or_below_cutoff);
  1272. }
  1273. }
  1274. // Slow path
  1275. const uint32 tag = ReadTagFallback(first_byte_or_zero);
  1276. return std::make_pair(tag, static_cast<uint32>(tag - 1) < cutoff);
  1277. }
  1278. inline bool CodedInputStream::LastTagWas(uint32 expected) {
  1279. return last_tag_ == expected;
  1280. }
  1281. inline bool CodedInputStream::ConsumedEntireMessage() {
  1282. return legitimate_message_end_;
  1283. }
  1284. inline bool CodedInputStream::ExpectTag(uint32 expected) {
  1285. if (expected < (1 << 7)) {
  1286. if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) &&
  1287. buffer_[0] == expected) {
  1288. Advance(1);
  1289. return true;
  1290. } else {
  1291. return false;
  1292. }
  1293. } else if (expected < (1 << 14)) {
  1294. if (PROTOBUF_PREDICT_TRUE(BufferSize() >= 2) &&
  1295. buffer_[0] == static_cast<uint8>(expected | 0x80) &&
  1296. buffer_[1] == static_cast<uint8>(expected >> 7)) {
  1297. Advance(2);
  1298. return true;
  1299. } else {
  1300. return false;
  1301. }
  1302. } else {
  1303. // Don't bother optimizing for larger values.
  1304. return false;
  1305. }
  1306. }
  1307. inline const uint8* CodedInputStream::ExpectTagFromArray(const uint8* buffer,
  1308. uint32 expected) {
  1309. if (expected < (1 << 7)) {
  1310. if (buffer[0] == expected) {
  1311. return buffer + 1;
  1312. }
  1313. } else if (expected < (1 << 14)) {
  1314. if (buffer[0] == static_cast<uint8>(expected | 0x80) &&
  1315. buffer[1] == static_cast<uint8>(expected >> 7)) {
  1316. return buffer + 2;
  1317. }
  1318. }
  1319. return nullptr;
  1320. }
  1321. inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
  1322. int* size) {
  1323. *data = buffer_;
  1324. *size = static_cast<int>(buffer_end_ - buffer_);
  1325. }
  1326. inline bool CodedInputStream::ExpectAtEnd() {
  1327. // If we are at a limit we know no more bytes can be read. Otherwise, it's
  1328. // hard to say without calling Refresh(), and we'd rather not do that.
  1329. if (buffer_ == buffer_end_ && ((buffer_size_after_limit_ != 0) ||
  1330. (total_bytes_read_ == current_limit_))) {
  1331. last_tag_ = 0; // Pretend we called ReadTag()...
  1332. legitimate_message_end_ = true; // ... and it hit EOF.
  1333. return true;
  1334. } else {
  1335. return false;
  1336. }
  1337. }
  1338. inline int CodedInputStream::CurrentPosition() const {
  1339. return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_);
  1340. }
  1341. inline void CodedInputStream::Advance(int amount) { buffer_ += amount; }
  1342. inline void CodedInputStream::SetRecursionLimit(int limit) {
  1343. recursion_budget_ += limit - recursion_limit_;
  1344. recursion_limit_ = limit;
  1345. }
  1346. inline bool CodedInputStream::IncrementRecursionDepth() {
  1347. --recursion_budget_;
  1348. return recursion_budget_ >= 0;
  1349. }
  1350. inline void CodedInputStream::DecrementRecursionDepth() {
  1351. if (recursion_budget_ < recursion_limit_) ++recursion_budget_;
  1352. }
  1353. inline void CodedInputStream::UnsafeDecrementRecursionDepth() {
  1354. assert(recursion_budget_ < recursion_limit_);
  1355. ++recursion_budget_;
  1356. }
  1357. inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
  1358. MessageFactory* factory) {
  1359. extension_pool_ = pool;
  1360. extension_factory_ = factory;
  1361. }
  1362. inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
  1363. return extension_pool_;
  1364. }
  1365. inline MessageFactory* CodedInputStream::GetExtensionFactory() {
  1366. return extension_factory_;
  1367. }
  1368. inline int CodedInputStream::BufferSize() const {
  1369. return static_cast<int>(buffer_end_ - buffer_);
  1370. }
  1371. inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
  1372. : buffer_(nullptr),
  1373. buffer_end_(nullptr),
  1374. input_(input),
  1375. total_bytes_read_(0),
  1376. overflow_bytes_(0),
  1377. last_tag_(0),
  1378. legitimate_message_end_(false),
  1379. aliasing_enabled_(false),
  1380. current_limit_(kint32max),
  1381. buffer_size_after_limit_(0),
  1382. total_bytes_limit_(kDefaultTotalBytesLimit),
  1383. recursion_budget_(default_recursion_limit_),
  1384. recursion_limit_(default_recursion_limit_),
  1385. extension_pool_(nullptr),
  1386. extension_factory_(nullptr) {
  1387. // Eagerly Refresh() so buffer space is immediately available.
  1388. Refresh();
  1389. }
  1390. inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
  1391. : buffer_(buffer),
  1392. buffer_end_(buffer + size),
  1393. input_(nullptr),
  1394. total_bytes_read_(size),
  1395. overflow_bytes_(0),
  1396. last_tag_(0),
  1397. legitimate_message_end_(false),
  1398. aliasing_enabled_(false),
  1399. current_limit_(size),
  1400. buffer_size_after_limit_(0),
  1401. total_bytes_limit_(kDefaultTotalBytesLimit),
  1402. recursion_budget_(default_recursion_limit_),
  1403. recursion_limit_(default_recursion_limit_),
  1404. extension_pool_(nullptr),
  1405. extension_factory_(nullptr) {
  1406. // Note that setting current_limit_ == size is important to prevent some
  1407. // code paths from trying to access input_ and segfaulting.
  1408. }
  1409. inline bool CodedInputStream::IsFlat() const { return input_ == nullptr; }
  1410. inline bool CodedInputStream::Skip(int count) {
  1411. if (count < 0) return false; // security: count is often user-supplied
  1412. const int original_buffer_size = BufferSize();
  1413. if (count <= original_buffer_size) {
  1414. // Just skipping within the current buffer. Easy.
  1415. Advance(count);
  1416. return true;
  1417. }
  1418. return SkipFallback(count, original_buffer_size);
  1419. }
  1420. inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
  1421. uint8* target) {
  1422. return EpsCopyOutputStream::UnsafeVarint(value, target);
  1423. }
  1424. inline uint8* CodedOutputStream::WriteVarint32ToArrayOutOfLine(uint32 value,
  1425. uint8* target) {
  1426. target[0] = static_cast<uint8>(value);
  1427. if (value < 0x80) {
  1428. return target + 1;
  1429. } else {
  1430. return WriteVarint32ToArrayOutOfLineHelper(value, target);
  1431. }
  1432. }
  1433. inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value,
  1434. uint8* target) {
  1435. return EpsCopyOutputStream::UnsafeVarint(value, target);
  1436. }
  1437. inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
  1438. WriteVarint64(static_cast<uint64>(value));
  1439. }
  1440. inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
  1441. int32 value, uint8* target) {
  1442. return WriteVarint64ToArray(static_cast<uint64>(value), target);
  1443. }
  1444. inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
  1445. uint8* target) {
  1446. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1447. memcpy(target, &value, sizeof(value));
  1448. #else
  1449. target[0] = static_cast<uint8>(value);
  1450. target[1] = static_cast<uint8>(value >> 8);
  1451. target[2] = static_cast<uint8>(value >> 16);
  1452. target[3] = static_cast<uint8>(value >> 24);
  1453. #endif
  1454. return target + sizeof(value);
  1455. }
  1456. inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
  1457. uint8* target) {
  1458. #if defined(PROTOBUF_LITTLE_ENDIAN)
  1459. memcpy(target, &value, sizeof(value));
  1460. #else
  1461. uint32 part0 = static_cast<uint32>(value);
  1462. uint32 part1 = static_cast<uint32>(value >> 32);
  1463. target[0] = static_cast<uint8>(part0);
  1464. target[1] = static_cast<uint8>(part0 >> 8);
  1465. target[2] = static_cast<uint8>(part0 >> 16);
  1466. target[3] = static_cast<uint8>(part0 >> 24);
  1467. target[4] = static_cast<uint8>(part1);
  1468. target[5] = static_cast<uint8>(part1 >> 8);
  1469. target[6] = static_cast<uint8>(part1 >> 16);
  1470. target[7] = static_cast<uint8>(part1 >> 24);
  1471. #endif
  1472. return target + sizeof(value);
  1473. }
  1474. inline void CodedOutputStream::WriteVarint32(uint32 value) {
  1475. cur_ = impl_.EnsureSpace(cur_);
  1476. SetCur(WriteVarint32ToArray(value, Cur()));
  1477. }
  1478. inline void CodedOutputStream::WriteVarint64(uint64 value) {
  1479. cur_ = impl_.EnsureSpace(cur_);
  1480. SetCur(WriteVarint64ToArray(value, Cur()));
  1481. }
  1482. inline void CodedOutputStream::WriteTag(uint32 value) { WriteVarint32(value); }
  1483. inline uint8* CodedOutputStream::WriteTagToArray(uint32 value, uint8* target) {
  1484. return WriteVarint32ToArray(value, target);
  1485. }
  1486. inline size_t CodedOutputStream::VarintSize32(uint32 value) {
  1487. // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
  1488. // Use an explicit multiplication to implement the divide of
  1489. // a number in the 1..31 range.
  1490. // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
  1491. // undefined.
  1492. uint32 log2value = Bits::Log2FloorNonZero(value | 0x1);
  1493. return static_cast<size_t>((log2value * 9 + 73) / 64);
  1494. }
  1495. inline size_t CodedOutputStream::VarintSize64(uint64 value) {
  1496. // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
  1497. // Use an explicit multiplication to implement the divide of
  1498. // a number in the 1..63 range.
  1499. // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
  1500. // undefined.
  1501. uint32 log2value = Bits::Log2FloorNonZero64(value | 0x1);
  1502. return static_cast<size_t>((log2value * 9 + 73) / 64);
  1503. }
  1504. inline size_t CodedOutputStream::VarintSize32SignExtended(int32 value) {
  1505. if (value < 0) {
  1506. return 10; // TODO(kenton): Make this a symbolic constant.
  1507. } else {
  1508. return VarintSize32(static_cast<uint32>(value));
  1509. }
  1510. }
  1511. inline void CodedOutputStream::WriteString(const std::string& str) {
  1512. WriteRaw(str.data(), static_cast<int>(str.size()));
  1513. }
  1514. inline void CodedOutputStream::WriteRawMaybeAliased(const void* data,
  1515. int size) {
  1516. cur_ = impl_.WriteRawMaybeAliased(data, size, cur_);
  1517. }
  1518. inline uint8* CodedOutputStream::WriteRawToArray(const void* data, int size,
  1519. uint8* target) {
  1520. memcpy(target, data, size);
  1521. return target + size;
  1522. }
  1523. inline uint8* CodedOutputStream::WriteStringToArray(const std::string& str,
  1524. uint8* target) {
  1525. return WriteRawToArray(str.data(), static_cast<int>(str.size()), target);
  1526. }
  1527. } // namespace io
  1528. } // namespace protobuf
  1529. } // namespace google
  1530. #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
  1531. #pragma runtime_checks("c", restore)
  1532. #endif // _MSC_VER && !defined(__INTEL_COMPILER)
  1533. #include <google/protobuf/port_undef.inc>
  1534. #endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__