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 74 kB

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