You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

coded_stream.h 94 kB

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