coded_stream.h 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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_WRONLY);
  52. // ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
  53. // CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  54. //
  55. // int magic_number = 1234;
  56. // char text[] = "Hello world!";
  57. // coded_output->WriteLittleEndian32(magic_number);
  58. // coded_output->WriteVarint32(strlen(text));
  59. // coded_output->WriteRaw(text, strlen(text));
  60. //
  61. // delete coded_output;
  62. // delete raw_output;
  63. // close(fd);
  64. //
  65. // CodedInputStream example:
  66. // // Read a file created by the above code.
  67. // int fd = open("myfile", O_RDONLY);
  68. // ZeroCopyInputStream* raw_input = new FileInputStream(fd);
  69. // CodedInputStream coded_input = new CodedInputStream(raw_input);
  70. //
  71. // coded_input->ReadLittleEndian32(&magic_number);
  72. // if (magic_number != 1234) {
  73. // cerr << "File not in expected format." << endl;
  74. // return;
  75. // }
  76. //
  77. // uint32 size;
  78. // coded_input->ReadVarint32(&size);
  79. //
  80. // char* text = new char[size + 1];
  81. // coded_input->ReadRaw(buffer, size);
  82. // text[size] = '\0';
  83. //
  84. // delete coded_input;
  85. // delete raw_input;
  86. // close(fd);
  87. //
  88. // cout << "Text is: " << text << endl;
  89. // delete [] text;
  90. //
  91. // For those who are interested, varint encoding is defined as follows:
  92. //
  93. // The encoding operates on unsigned integers of up to 64 bits in length.
  94. // Each byte of the encoded value has the format:
  95. // * bits 0-6: Seven bits of the number being encoded.
  96. // * bit 7: Zero if this is the last byte in the encoding (in which
  97. // case all remaining bits of the number are zero) or 1 if
  98. // more bytes follow.
  99. // The first byte contains the least-significant 7 bits of the number, the
  100. // second byte (if present) contains the next-least-significant 7 bits,
  101. // and so on. So, the binary number 1011000101011 would be encoded in two
  102. // bytes as "10101011 00101100".
  103. //
  104. // In theory, varint could be used to encode integers of any length.
  105. // However, for practicality we set a limit at 64 bits. The maximum encoded
  106. // length of a number is thus 10 bytes.
  107. #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
  108. #define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
  109. #include <string>
  110. #ifdef _MSC_VER
  111. #if defined(_M_IX86) && \
  112. !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  113. #define PROTOBUF_LITTLE_ENDIAN 1
  114. #endif
  115. #if _MSC_VER >= 1300
  116. // If MSVC has "/RTCc" set, it will complain about truncating casts at
  117. // runtime. This file contains some intentional truncating casts.
  118. #pragma runtime_checks("c", off)
  119. #endif
  120. #else
  121. #include <sys/param.h> // __BYTE_ORDER
  122. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN && \
  123. !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  124. #define PROTOBUF_LITTLE_ENDIAN 1
  125. #endif
  126. #endif
  127. #include <google/protobuf/stubs/common.h>
  128. namespace google {
  129. namespace protobuf {
  130. class DescriptorPool;
  131. class MessageFactory;
  132. namespace io {
  133. // Defined in this file.
  134. class CodedInputStream;
  135. class CodedOutputStream;
  136. // Defined in other files.
  137. class ZeroCopyInputStream; // zero_copy_stream.h
  138. class ZeroCopyOutputStream; // zero_copy_stream.h
  139. // Class which reads and decodes binary data which is composed of varint-
  140. // encoded integers and fixed-width pieces. Wraps a ZeroCopyInputStream.
  141. // Most users will not need to deal with CodedInputStream.
  142. //
  143. // Most methods of CodedInputStream that return a bool return false if an
  144. // underlying I/O error occurs or if the data is malformed. Once such a
  145. // failure occurs, the CodedInputStream is broken and is no longer useful.
  146. class LIBPROTOBUF_EXPORT CodedInputStream {
  147. public:
  148. // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
  149. explicit CodedInputStream(ZeroCopyInputStream* input);
  150. // Create a CodedInputStream that reads from the given flat array. This is
  151. // faster than using an ArrayInputStream. PushLimit(size) is implied by
  152. // this constructor.
  153. explicit CodedInputStream(const uint8* buffer, int size);
  154. // Destroy the CodedInputStream and position the underlying
  155. // ZeroCopyInputStream at the first unread byte. If an error occurred while
  156. // reading (causing a method to return false), then the exact position of
  157. // the input stream may be anywhere between the last value that was read
  158. // successfully and the stream's byte limit.
  159. ~CodedInputStream();
  160. // Return true if this CodedInputStream reads from a flat array instead of
  161. // a ZeroCopyInputStream.
  162. inline bool IsFlat() const;
  163. // Skips a number of bytes. Returns false if an underlying read error
  164. // occurs.
  165. bool Skip(int count);
  166. // Sets *data to point directly at the unread part of the CodedInputStream's
  167. // underlying buffer, and *size to the size of that buffer, but does not
  168. // advance the stream's current position. This will always either produce
  169. // a non-empty buffer or return false. If the caller consumes any of
  170. // this data, it should then call Skip() to skip over the consumed bytes.
  171. // This may be useful for implementing external fast parsing routines for
  172. // types of data not covered by the CodedInputStream interface.
  173. bool GetDirectBufferPointer(const void** data, int* size);
  174. // Like GetDirectBufferPointer, but this method is inlined, and does not
  175. // attempt to Refresh() if the buffer is currently empty.
  176. inline void GetDirectBufferPointerInline(const void** data,
  177. int* size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  178. // Read raw bytes, copying them into the given buffer.
  179. bool ReadRaw(void* buffer, int size);
  180. // Like ReadRaw, but reads into a string.
  181. //
  182. // Implementation Note: ReadString() grows the string gradually as it
  183. // reads in the data, rather than allocating the entire requested size
  184. // upfront. This prevents denial-of-service attacks in which a client
  185. // could claim that a string is going to be MAX_INT bytes long in order to
  186. // crash the server because it can't allocate this much space at once.
  187. bool ReadString(string* buffer, int size);
  188. // Like the above, with inlined optimizations. This should only be used
  189. // by the protobuf implementation.
  190. inline bool InternalReadStringInline(string* buffer,
  191. int size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  192. // Read a 32-bit little-endian integer.
  193. bool ReadLittleEndian32(uint32* value);
  194. // Read a 64-bit little-endian integer.
  195. bool ReadLittleEndian64(uint64* value);
  196. // These methods read from an externally provided buffer. The caller is
  197. // responsible for ensuring that the buffer has sufficient space.
  198. // Read a 32-bit little-endian integer.
  199. static const uint8* ReadLittleEndian32FromArray(const uint8* buffer,
  200. uint32* value);
  201. // Read a 64-bit little-endian integer.
  202. static const uint8* ReadLittleEndian64FromArray(const uint8* buffer,
  203. uint64* value);
  204. // Read an unsigned integer with Varint encoding, truncating to 32 bits.
  205. // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
  206. // it to uint32, but may be more efficient.
  207. bool ReadVarint32(uint32* value);
  208. // Read an unsigned integer with Varint encoding.
  209. bool ReadVarint64(uint64* value);
  210. // Read a tag. This calls ReadVarint32() and returns the result, or returns
  211. // zero (which is not a valid tag) if ReadVarint32() fails. Also, it updates
  212. // the last tag value, which can be checked with LastTagWas().
  213. // Always inline because this is only called in one place per parse loop
  214. // but it is called for every iteration of said loop, so it should be fast.
  215. // GCC doesn't want to inline this by default.
  216. uint32 ReadTag() GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  217. // This usually a faster alternative to ReadTag() when cutoff is a manifest
  218. // constant. It does particularly well for cutoff >= 127. The first part
  219. // of the return value is the tag that was read, though it can also be 0 in
  220. // the cases where ReadTag() would return 0. If the second part is true
  221. // then the tag is known to be in [0, cutoff]. If not, the tag either is
  222. // above cutoff or is 0. (There's intentional wiggle room when tag is 0,
  223. // because that can arise in several ways, and for best performance we want
  224. // to avoid an extra "is tag == 0?" check here.)
  225. inline std::pair<uint32, bool> ReadTagWithCutoff(uint32 cutoff)
  226. GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  227. // Usually returns true if calling ReadVarint32() now would produce the given
  228. // value. Will always return false if ReadVarint32() would not return the
  229. // given value. If ExpectTag() returns true, it also advances past
  230. // the varint. For best performance, use a compile-time constant as the
  231. // parameter.
  232. // Always inline because this collapses to a small number of instructions
  233. // when given a constant parameter, but GCC doesn't want to inline by default.
  234. bool ExpectTag(uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  235. // Like above, except this reads from the specified buffer. The caller is
  236. // responsible for ensuring that the buffer is large enough to read a varint
  237. // of the expected size. For best performance, use a compile-time constant as
  238. // the expected tag parameter.
  239. //
  240. // Returns a pointer beyond the expected tag if it was found, or NULL if it
  241. // was not.
  242. static const uint8* ExpectTagFromArray(
  243. const uint8* buffer,
  244. uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  245. // Usually returns true if no more bytes can be read. Always returns false
  246. // if more bytes can be read. If ExpectAtEnd() returns true, a subsequent
  247. // call to LastTagWas() will act as if ReadTag() had been called and returned
  248. // zero, and ConsumedEntireMessage() will return true.
  249. bool ExpectAtEnd();
  250. // If the last call to ReadTag() or ReadTagWithCutoff() returned the
  251. // given value, returns true. Otherwise, returns false;
  252. //
  253. // This is needed because parsers for some types of embedded messages
  254. // (with field type TYPE_GROUP) don't actually know that they've reached the
  255. // end of a message until they see an ENDGROUP tag, which was actually part
  256. // of the enclosing message. The enclosing message would like to check that
  257. // tag to make sure it had the right number, so it calls LastTagWas() on
  258. // return from the embedded parser to check.
  259. bool LastTagWas(uint32 expected);
  260. // When parsing message (but NOT a group), this method must be called
  261. // immediately after MergeFromCodedStream() returns (if it returns true)
  262. // to further verify that the message ended in a legitimate way. For
  263. // example, this verifies that parsing did not end on an end-group tag.
  264. // It also checks for some cases where, due to optimizations,
  265. // MergeFromCodedStream() can incorrectly return true.
  266. bool ConsumedEntireMessage();
  267. // Limits ----------------------------------------------------------
  268. // Limits are used when parsing length-delimited embedded messages.
  269. // After the message's length is read, PushLimit() is used to prevent
  270. // the CodedInputStream from reading beyond that length. Once the
  271. // embedded message has been parsed, PopLimit() is called to undo the
  272. // limit.
  273. // Opaque type used with PushLimit() and PopLimit(). Do not modify
  274. // values of this type yourself. The only reason that this isn't a
  275. // struct with private internals is for efficiency.
  276. typedef int Limit;
  277. // Places a limit on the number of bytes that the stream may read,
  278. // starting from the current position. Once the stream hits this limit,
  279. // it will act like the end of the input has been reached until PopLimit()
  280. // is called.
  281. //
  282. // As the names imply, the stream conceptually has a stack of limits. The
  283. // shortest limit on the stack is always enforced, even if it is not the
  284. // top limit.
  285. //
  286. // The value returned by PushLimit() is opaque to the caller, and must
  287. // be passed unchanged to the corresponding call to PopLimit().
  288. Limit PushLimit(int byte_limit);
  289. // Pops the last limit pushed by PushLimit(). The input must be the value
  290. // returned by that call to PushLimit().
  291. void PopLimit(Limit limit);
  292. // Returns the number of bytes left until the nearest limit on the
  293. // stack is hit, or -1 if no limits are in place.
  294. int BytesUntilLimit() const;
  295. // Returns current position relative to the beginning of the input stream.
  296. int CurrentPosition() const;
  297. // Total Bytes Limit -----------------------------------------------
  298. // To prevent malicious users from sending excessively large messages
  299. // and causing integer overflows or memory exhaustion, CodedInputStream
  300. // imposes a hard limit on the total number of bytes it will read.
  301. // Sets the maximum number of bytes that this CodedInputStream will read
  302. // before refusing to continue. To prevent integer overflows in the
  303. // protocol buffers implementation, as well as to prevent servers from
  304. // allocating enormous amounts of memory to hold parsed messages, the
  305. // maximum message length should be limited to the shortest length that
  306. // will not harm usability. The theoretical shortest message that could
  307. // cause integer overflows is 512MB. The default limit is 64MB. Apps
  308. // should set shorter limits if possible. If warning_threshold is not -1,
  309. // a warning will be printed to stderr after warning_threshold bytes are
  310. // read. For backwards compatibility all negative values get squashed to -1,
  311. // as other negative values might have special internal meanings.
  312. // An error will always be printed to stderr if the limit is reached.
  313. //
  314. // This is unrelated to PushLimit()/PopLimit().
  315. //
  316. // Hint: If you are reading this because your program is printing a
  317. // warning about dangerously large protocol messages, you may be
  318. // confused about what to do next. The best option is to change your
  319. // design such that excessively large messages are not necessary.
  320. // For example, try to design file formats to consist of many small
  321. // messages rather than a single large one. If this is infeasible,
  322. // you will need to increase the limit. Chances are, though, that
  323. // your code never constructs a CodedInputStream on which the limit
  324. // can be set. You probably parse messages by calling things like
  325. // Message::ParseFromString(). In this case, you will need to change
  326. // your code to instead construct some sort of ZeroCopyInputStream
  327. // (e.g. an ArrayInputStream), construct a CodedInputStream around
  328. // that, then call Message::ParseFromCodedStream() instead. Then
  329. // you can adjust the limit. Yes, it's more work, but you're doing
  330. // something unusual.
  331. void SetTotalBytesLimit(int total_bytes_limit, int warning_threshold);
  332. // The Total Bytes Limit minus the Current Position, or -1 if there
  333. // is no Total Bytes Limit.
  334. int BytesUntilTotalBytesLimit() const;
  335. // Recursion Limit -------------------------------------------------
  336. // To prevent corrupt or malicious messages from causing stack overflows,
  337. // we must keep track of the depth of recursion when parsing embedded
  338. // messages and groups. CodedInputStream keeps track of this because it
  339. // is the only object that is passed down the stack during parsing.
  340. // Sets the maximum recursion depth. The default is 100.
  341. void SetRecursionLimit(int limit);
  342. // Increments the current recursion depth. Returns true if the depth is
  343. // under the limit, false if it has gone over.
  344. bool IncrementRecursionDepth();
  345. // Decrements the recursion depth.
  346. void DecrementRecursionDepth();
  347. // Extension Registry ----------------------------------------------
  348. // ADVANCED USAGE: 99.9% of people can ignore this section.
  349. //
  350. // By default, when parsing extensions, the parser looks for extension
  351. // definitions in the pool which owns the outer message's Descriptor.
  352. // However, you may call SetExtensionRegistry() to provide an alternative
  353. // pool instead. This makes it possible, for example, to parse a message
  354. // using a generated class, but represent some extensions using
  355. // DynamicMessage.
  356. // Set the pool used to look up extensions. Most users do not need to call
  357. // this as the correct pool will be chosen automatically.
  358. //
  359. // WARNING: It is very easy to misuse this. Carefully read the requirements
  360. // below. Do not use this unless you are sure you need it. Almost no one
  361. // does.
  362. //
  363. // Let's say you are parsing a message into message object m, and you want
  364. // to take advantage of SetExtensionRegistry(). You must follow these
  365. // requirements:
  366. //
  367. // The given DescriptorPool must contain m->GetDescriptor(). It is not
  368. // sufficient for it to simply contain a descriptor that has the same name
  369. // and content -- it must be the *exact object*. In other words:
  370. // assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
  371. // m->GetDescriptor());
  372. // There are two ways to satisfy this requirement:
  373. // 1) Use m->GetDescriptor()->pool() as the pool. This is generally useless
  374. // because this is the pool that would be used anyway if you didn't call
  375. // SetExtensionRegistry() at all.
  376. // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
  377. // "underlay". Read the documentation for DescriptorPool for more
  378. // information about underlays.
  379. //
  380. // You must also provide a MessageFactory. This factory will be used to
  381. // construct Message objects representing extensions. The factory's
  382. // GetPrototype() MUST return non-NULL for any Descriptor which can be found
  383. // through the provided pool.
  384. //
  385. // If the provided factory might return instances of protocol-compiler-
  386. // generated (i.e. compiled-in) types, or if the outer message object m is
  387. // a generated type, then the given factory MUST have this property: If
  388. // GetPrototype() is given a Descriptor which resides in
  389. // DescriptorPool::generated_pool(), the factory MUST return the same
  390. // prototype which MessageFactory::generated_factory() would return. That
  391. // is, given a descriptor for a generated type, the factory must return an
  392. // instance of the generated class (NOT DynamicMessage). However, when
  393. // given a descriptor for a type that is NOT in generated_pool, the factory
  394. // is free to return any implementation.
  395. //
  396. // The reason for this requirement is that generated sub-objects may be
  397. // accessed via the standard (non-reflection) extension accessor methods,
  398. // and these methods will down-cast the object to the generated class type.
  399. // If the object is not actually of that type, the results would be undefined.
  400. // On the other hand, if an extension is not compiled in, then there is no
  401. // way the code could end up accessing it via the standard accessors -- the
  402. // only way to access the extension is via reflection. When using reflection,
  403. // DynamicMessage and generated messages are indistinguishable, so it's fine
  404. // if these objects are represented using DynamicMessage.
  405. //
  406. // Using DynamicMessageFactory on which you have called
  407. // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
  408. // above requirement.
  409. //
  410. // If either pool or factory is NULL, both must be NULL.
  411. //
  412. // Note that this feature is ignored when parsing "lite" messages as they do
  413. // not have descriptors.
  414. void SetExtensionRegistry(const DescriptorPool* pool,
  415. MessageFactory* factory);
  416. // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
  417. // has been provided.
  418. const DescriptorPool* GetExtensionPool();
  419. // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
  420. // factory has been provided.
  421. MessageFactory* GetExtensionFactory();
  422. private:
  423. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
  424. ZeroCopyInputStream* input_;
  425. const uint8* buffer_;
  426. const uint8* buffer_end_; // pointer to the end of the buffer.
  427. int total_bytes_read_; // total bytes read from input_, including
  428. // the current buffer
  429. // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
  430. // so that we can BackUp() on destruction.
  431. int overflow_bytes_;
  432. // LastTagWas() stuff.
  433. uint32 last_tag_; // result of last ReadTag() or ReadTagWithCutoff().
  434. // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
  435. // at EOF, or by ExpectAtEnd() when it returns true. This happens when we
  436. // reach the end of a message and attempt to read another tag.
  437. bool legitimate_message_end_;
  438. // See EnableAliasing().
  439. bool aliasing_enabled_;
  440. // Limits
  441. Limit current_limit_; // if position = -1, no limit is applied
  442. // For simplicity, if the current buffer crosses a limit (either a normal
  443. // limit created by PushLimit() or the total bytes limit), buffer_size_
  444. // only tracks the number of bytes before that limit. This field
  445. // contains the number of bytes after it. Note that this implies that if
  446. // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
  447. // hit a limit. However, if both are zero, it doesn't necessarily mean
  448. // we aren't at a limit -- the buffer may have ended exactly at the limit.
  449. int buffer_size_after_limit_;
  450. // Maximum number of bytes to read, period. This is unrelated to
  451. // current_limit_. Set using SetTotalBytesLimit().
  452. int total_bytes_limit_;
  453. // If positive/0: Limit for bytes read after which a warning due to size
  454. // should be logged.
  455. // If -1: Printing of warning disabled. Can be set by client.
  456. // If -2: Internal: Limit has been reached, print full size when destructing.
  457. int total_bytes_warning_threshold_;
  458. // Current recursion depth, controlled by IncrementRecursionDepth() and
  459. // DecrementRecursionDepth().
  460. int recursion_depth_;
  461. // Recursion depth limit, set by SetRecursionLimit().
  462. int recursion_limit_;
  463. // See SetExtensionRegistry().
  464. const DescriptorPool* extension_pool_;
  465. MessageFactory* extension_factory_;
  466. // Private member functions.
  467. // Advance the buffer by a given number of bytes.
  468. void Advance(int amount);
  469. // Back up input_ to the current buffer position.
  470. void BackUpInputToCurrentPosition();
  471. // Recomputes the value of buffer_size_after_limit_. Must be called after
  472. // current_limit_ or total_bytes_limit_ changes.
  473. void RecomputeBufferLimits();
  474. // Writes an error message saying that we hit total_bytes_limit_.
  475. void PrintTotalBytesLimitError();
  476. // Called when the buffer runs out to request more data. Implies an
  477. // Advance(BufferSize()).
  478. bool Refresh();
  479. // When parsing varints, we optimize for the common case of small values, and
  480. // then optimize for the case when the varint fits within the current buffer
  481. // piece. The Fallback method is used when we can't use the one-byte
  482. // optimization. The Slow method is yet another fallback when the buffer is
  483. // not large enough. Making the slow path out-of-line speeds up the common
  484. // case by 10-15%. The slow path is fairly uncommon: it only triggers when a
  485. // message crosses multiple buffers.
  486. bool ReadVarint32Fallback(uint32* value);
  487. bool ReadVarint64Fallback(uint64* value);
  488. bool ReadVarint32Slow(uint32* value);
  489. bool ReadVarint64Slow(uint64* value);
  490. bool ReadLittleEndian32Fallback(uint32* value);
  491. bool ReadLittleEndian64Fallback(uint64* value);
  492. // Fallback/slow methods for reading tags. These do not update last_tag_,
  493. // but will set legitimate_message_end_ if we are at the end of the input
  494. // stream.
  495. uint32 ReadTagFallback();
  496. uint32 ReadTagSlow();
  497. bool ReadStringFallback(string* buffer, int size);
  498. // Return the size of the buffer.
  499. int BufferSize() const;
  500. static const int kDefaultTotalBytesLimit = 64 << 20; // 64MB
  501. static const int kDefaultTotalBytesWarningThreshold = 32 << 20; // 32MB
  502. static int default_recursion_limit_; // 100 by default.
  503. };
  504. // Class which encodes and writes binary data which is composed of varint-
  505. // encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream.
  506. // Most users will not need to deal with CodedOutputStream.
  507. //
  508. // Most methods of CodedOutputStream which return a bool return false if an
  509. // underlying I/O error occurs. Once such a failure occurs, the
  510. // CodedOutputStream is broken and is no longer useful. The Write* methods do
  511. // not return the stream status, but will invalidate the stream if an error
  512. // occurs. The client can probe HadError() to determine the status.
  513. //
  514. // Note that every method of CodedOutputStream which writes some data has
  515. // a corresponding static "ToArray" version. These versions write directly
  516. // to the provided buffer, returning a pointer past the last written byte.
  517. // They require that the buffer has sufficient capacity for the encoded data.
  518. // This allows an optimization where we check if an output stream has enough
  519. // space for an entire message before we start writing and, if there is, we
  520. // call only the ToArray methods to avoid doing bound checks for each
  521. // individual value.
  522. // i.e., in the example above:
  523. //
  524. // CodedOutputStream coded_output = new CodedOutputStream(raw_output);
  525. // int magic_number = 1234;
  526. // char text[] = "Hello world!";
  527. //
  528. // int coded_size = sizeof(magic_number) +
  529. // CodedOutputStream::VarintSize32(strlen(text)) +
  530. // strlen(text);
  531. //
  532. // uint8* buffer =
  533. // coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
  534. // if (buffer != NULL) {
  535. // // The output stream has enough space in the buffer: write directly to
  536. // // the array.
  537. // buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
  538. // buffer);
  539. // buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
  540. // buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
  541. // } else {
  542. // // Make bound-checked writes, which will ask the underlying stream for
  543. // // more space as needed.
  544. // coded_output->WriteLittleEndian32(magic_number);
  545. // coded_output->WriteVarint32(strlen(text));
  546. // coded_output->WriteRaw(text, strlen(text));
  547. // }
  548. //
  549. // delete coded_output;
  550. class LIBPROTOBUF_EXPORT CodedOutputStream {
  551. public:
  552. // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
  553. explicit CodedOutputStream(ZeroCopyOutputStream* output);
  554. // Destroy the CodedOutputStream and position the underlying
  555. // ZeroCopyOutputStream immediately after the last byte written.
  556. ~CodedOutputStream();
  557. // Skips a number of bytes, leaving the bytes unmodified in the underlying
  558. // buffer. Returns false if an underlying write error occurs. This is
  559. // mainly useful with GetDirectBufferPointer().
  560. bool Skip(int count);
  561. // Sets *data to point directly at the unwritten part of the
  562. // CodedOutputStream's underlying buffer, and *size to the size of that
  563. // buffer, but does not advance the stream's current position. This will
  564. // always either produce a non-empty buffer or return false. If the caller
  565. // writes any data to this buffer, it should then call Skip() to skip over
  566. // the consumed bytes. This may be useful for implementing external fast
  567. // serialization routines for types of data not covered by the
  568. // CodedOutputStream interface.
  569. bool GetDirectBufferPointer(void** data, int* size);
  570. // If there are at least "size" bytes available in the current buffer,
  571. // returns a pointer directly into the buffer and advances over these bytes.
  572. // The caller may then write directly into this buffer (e.g. using the
  573. // *ToArray static methods) rather than go through CodedOutputStream. If
  574. // there are not enough bytes available, returns NULL. The return pointer is
  575. // invalidated as soon as any other non-const method of CodedOutputStream
  576. // is called.
  577. inline uint8* GetDirectBufferForNBytesAndAdvance(int size);
  578. // Write raw bytes, copying them from the given buffer.
  579. void WriteRaw(const void* buffer, int size);
  580. // Like WriteRaw() but will try to write aliased data if aliasing is
  581. // turned on.
  582. void WriteRawMaybeAliased(const void* data, int size);
  583. // Like WriteRaw() but writing directly to the target array.
  584. // This is _not_ inlined, as the compiler often optimizes memcpy into inline
  585. // copy loops. Since this gets called by every field with string or bytes
  586. // type, inlining may lead to a significant amount of code bloat, with only a
  587. // minor performance gain.
  588. static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
  589. // Equivalent to WriteRaw(str.data(), str.size()).
  590. void WriteString(const string& str);
  591. // Like WriteString() but writing directly to the target array.
  592. static uint8* WriteStringToArray(const string& str, uint8* target);
  593. // Write the varint-encoded size of str followed by str.
  594. static uint8* WriteStringWithSizeToArray(const string& str, uint8* target);
  595. // Instructs the CodedOutputStream to allow the underlying
  596. // ZeroCopyOutputStream to hold pointers to the original structure instead of
  597. // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
  598. // underlying stream does not support aliasing, then enabling it has no
  599. // affect. For now, this only affects the behavior of
  600. // WriteRawMaybeAliased().
  601. //
  602. // NOTE: It is caller's responsibility to ensure that the chunk of memory
  603. // remains live until all of the data has been consumed from the stream.
  604. void EnableAliasing(bool enabled);
  605. // Write a 32-bit little-endian integer.
  606. void WriteLittleEndian32(uint32 value);
  607. // Like WriteLittleEndian32() but writing directly to the target array.
  608. static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
  609. // Write a 64-bit little-endian integer.
  610. void WriteLittleEndian64(uint64 value);
  611. // Like WriteLittleEndian64() but writing directly to the target array.
  612. static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
  613. // Write an unsigned integer with Varint encoding. Writing a 32-bit value
  614. // is equivalent to casting it to uint64 and writing it as a 64-bit value,
  615. // but may be more efficient.
  616. void WriteVarint32(uint32 value);
  617. // Like WriteVarint32() but writing directly to the target array.
  618. static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
  619. // Write an unsigned integer with Varint encoding.
  620. void WriteVarint64(uint64 value);
  621. // Like WriteVarint64() but writing directly to the target array.
  622. static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
  623. // Equivalent to WriteVarint32() except when the value is negative,
  624. // in which case it must be sign-extended to a full 10 bytes.
  625. void WriteVarint32SignExtended(int32 value);
  626. // Like WriteVarint32SignExtended() but writing directly to the target array.
  627. static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
  628. // This is identical to WriteVarint32(), but optimized for writing tags.
  629. // In particular, if the input is a compile-time constant, this method
  630. // compiles down to a couple instructions.
  631. // Always inline because otherwise the aformentioned optimization can't work,
  632. // but GCC by default doesn't want to inline this.
  633. void WriteTag(uint32 value);
  634. // Like WriteTag() but writing directly to the target array.
  635. static uint8* WriteTagToArray(
  636. uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  637. // Returns the number of bytes needed to encode the given value as a varint.
  638. static int VarintSize32(uint32 value);
  639. // Returns the number of bytes needed to encode the given value as a varint.
  640. static int VarintSize64(uint64 value);
  641. // If negative, 10 bytes. Otheriwse, same as VarintSize32().
  642. static int VarintSize32SignExtended(int32 value);
  643. // Compile-time equivalent of VarintSize32().
  644. template <uint32 Value>
  645. struct StaticVarintSize32 {
  646. static const int value =
  647. (Value < (1 << 7))
  648. ? 1
  649. : (Value < (1 << 14))
  650. ? 2
  651. : (Value < (1 << 21))
  652. ? 3
  653. : (Value < (1 << 28))
  654. ? 4
  655. : 5;
  656. };
  657. // Returns the total number of bytes written since this object was created.
  658. inline int ByteCount() const;
  659. // Returns true if there was an underlying I/O error since this object was
  660. // created.
  661. bool HadError() const { return had_error_; }
  662. private:
  663. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
  664. ZeroCopyOutputStream* output_;
  665. uint8* buffer_;
  666. int buffer_size_;
  667. int total_bytes_; // Sum of sizes of all buffers seen so far.
  668. bool had_error_; // Whether an error occurred during output.
  669. bool aliasing_enabled_; // See EnableAliasing().
  670. // Advance the buffer by a given number of bytes.
  671. void Advance(int amount);
  672. // Called when the buffer runs out to request more data. Implies an
  673. // Advance(buffer_size_).
  674. bool Refresh();
  675. // Like WriteRaw() but may avoid copying if the underlying
  676. // ZeroCopyOutputStream supports it.
  677. void WriteAliasedRaw(const void* buffer, int size);
  678. static uint8* WriteVarint32FallbackToArray(uint32 value, uint8* target);
  679. // Always-inlined versions of WriteVarint* functions so that code can be
  680. // reused, while still controlling size. For instance, WriteVarint32ToArray()
  681. // should not directly call this: since it is inlined itself, doing so
  682. // would greatly increase the size of generated code. Instead, it should call
  683. // WriteVarint32FallbackToArray. Meanwhile, WriteVarint32() is already
  684. // out-of-line, so it should just invoke this directly to avoid any extra
  685. // function call overhead.
  686. static uint8* WriteVarint32FallbackToArrayInline(
  687. uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  688. static uint8* WriteVarint64ToArrayInline(
  689. uint64 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
  690. static int VarintSize32Fallback(uint32 value);
  691. };
  692. // inline methods ====================================================
  693. // The vast majority of varints are only one byte. These inline
  694. // methods optimize for that case.
  695. inline bool CodedInputStream::ReadVarint32(uint32* value) {
  696. if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
  697. *value = *buffer_;
  698. Advance(1);
  699. return true;
  700. } else {
  701. return ReadVarint32Fallback(value);
  702. }
  703. }
  704. inline bool CodedInputStream::ReadVarint64(uint64* value) {
  705. if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
  706. *value = *buffer_;
  707. Advance(1);
  708. return true;
  709. } else {
  710. return ReadVarint64Fallback(value);
  711. }
  712. }
  713. // static
  714. inline const uint8* CodedInputStream::ReadLittleEndian32FromArray(
  715. const uint8* buffer,
  716. uint32* value) {
  717. #if defined(PROTOBUF_LITTLE_ENDIAN)
  718. memcpy(value, buffer, sizeof(*value));
  719. return buffer + sizeof(*value);
  720. #else
  721. *value = (static_cast<uint32>(buffer[0]) ) |
  722. (static_cast<uint32>(buffer[1]) << 8) |
  723. (static_cast<uint32>(buffer[2]) << 16) |
  724. (static_cast<uint32>(buffer[3]) << 24);
  725. return buffer + sizeof(*value);
  726. #endif
  727. }
  728. // static
  729. inline const uint8* CodedInputStream::ReadLittleEndian64FromArray(
  730. const uint8* buffer,
  731. uint64* value) {
  732. #if defined(PROTOBUF_LITTLE_ENDIAN)
  733. memcpy(value, buffer, sizeof(*value));
  734. return buffer + sizeof(*value);
  735. #else
  736. uint32 part0 = (static_cast<uint32>(buffer[0]) ) |
  737. (static_cast<uint32>(buffer[1]) << 8) |
  738. (static_cast<uint32>(buffer[2]) << 16) |
  739. (static_cast<uint32>(buffer[3]) << 24);
  740. uint32 part1 = (static_cast<uint32>(buffer[4]) ) |
  741. (static_cast<uint32>(buffer[5]) << 8) |
  742. (static_cast<uint32>(buffer[6]) << 16) |
  743. (static_cast<uint32>(buffer[7]) << 24);
  744. *value = static_cast<uint64>(part0) |
  745. (static_cast<uint64>(part1) << 32);
  746. return buffer + sizeof(*value);
  747. #endif
  748. }
  749. inline bool CodedInputStream::ReadLittleEndian32(uint32* value) {
  750. #if defined(PROTOBUF_LITTLE_ENDIAN)
  751. if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  752. memcpy(value, buffer_, sizeof(*value));
  753. Advance(sizeof(*value));
  754. return true;
  755. } else {
  756. return ReadLittleEndian32Fallback(value);
  757. }
  758. #else
  759. return ReadLittleEndian32Fallback(value);
  760. #endif
  761. }
  762. inline bool CodedInputStream::ReadLittleEndian64(uint64* value) {
  763. #if defined(PROTOBUF_LITTLE_ENDIAN)
  764. if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
  765. memcpy(value, buffer_, sizeof(*value));
  766. Advance(sizeof(*value));
  767. return true;
  768. } else {
  769. return ReadLittleEndian64Fallback(value);
  770. }
  771. #else
  772. return ReadLittleEndian64Fallback(value);
  773. #endif
  774. }
  775. inline uint32 CodedInputStream::ReadTag() {
  776. if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] < 0x80) {
  777. last_tag_ = buffer_[0];
  778. Advance(1);
  779. return last_tag_;
  780. } else {
  781. last_tag_ = ReadTagFallback();
  782. return last_tag_;
  783. }
  784. }
  785. inline std::pair<uint32, bool> CodedInputStream::ReadTagWithCutoff(
  786. uint32 cutoff) {
  787. // In performance-sensitive code we can expect cutoff to be a compile-time
  788. // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at
  789. // compile time.
  790. if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) {
  791. // Hot case: buffer_ non_empty, buffer_[0] in [1, 128).
  792. // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields
  793. // is large enough then is it better to check for the two-byte case first?
  794. if (static_cast<int8>(buffer_[0]) > 0) {
  795. const uint32 kMax1ByteVarint = 0x7f;
  796. uint32 tag = last_tag_ = buffer_[0];
  797. Advance(1);
  798. return make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff);
  799. }
  800. // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available,
  801. // and tag is two bytes. The latter is tested by bitwise-and-not of the
  802. // first byte and the second byte.
  803. if (cutoff >= 0x80 &&
  804. GOOGLE_PREDICT_TRUE(buffer_ + 1 < buffer_end_) &&
  805. GOOGLE_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) {
  806. const uint32 kMax2ByteVarint = (0x7f << 7) + 0x7f;
  807. uint32 tag = last_tag_ = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80);
  808. Advance(2);
  809. // It might make sense to test for tag == 0 now, but it is so rare that
  810. // that we don't bother. A varint-encoded 0 should be one byte unless
  811. // the encoder lost its mind. The second part of the return value of
  812. // this function is allowed to be either true or false if the tag is 0,
  813. // so we don't have to check for tag == 0. We may need to check whether
  814. // it exceeds cutoff.
  815. bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff;
  816. return make_pair(tag, at_or_below_cutoff);
  817. }
  818. }
  819. // Slow path
  820. last_tag_ = ReadTagFallback();
  821. return make_pair(last_tag_, static_cast<uint32>(last_tag_ - 1) < cutoff);
  822. }
  823. inline bool CodedInputStream::LastTagWas(uint32 expected) {
  824. return last_tag_ == expected;
  825. }
  826. inline bool CodedInputStream::ConsumedEntireMessage() {
  827. return legitimate_message_end_;
  828. }
  829. inline bool CodedInputStream::ExpectTag(uint32 expected) {
  830. if (expected < (1 << 7)) {
  831. if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) {
  832. Advance(1);
  833. return true;
  834. } else {
  835. return false;
  836. }
  837. } else if (expected < (1 << 14)) {
  838. if (GOOGLE_PREDICT_TRUE(BufferSize() >= 2) &&
  839. buffer_[0] == static_cast<uint8>(expected | 0x80) &&
  840. buffer_[1] == static_cast<uint8>(expected >> 7)) {
  841. Advance(2);
  842. return true;
  843. } else {
  844. return false;
  845. }
  846. } else {
  847. // Don't bother optimizing for larger values.
  848. return false;
  849. }
  850. }
  851. inline const uint8* CodedInputStream::ExpectTagFromArray(
  852. const uint8* buffer, uint32 expected) {
  853. if (expected < (1 << 7)) {
  854. if (buffer[0] == expected) {
  855. return buffer + 1;
  856. }
  857. } else if (expected < (1 << 14)) {
  858. if (buffer[0] == static_cast<uint8>(expected | 0x80) &&
  859. buffer[1] == static_cast<uint8>(expected >> 7)) {
  860. return buffer + 2;
  861. }
  862. }
  863. return NULL;
  864. }
  865. inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
  866. int* size) {
  867. *data = buffer_;
  868. *size = buffer_end_ - buffer_;
  869. }
  870. inline bool CodedInputStream::ExpectAtEnd() {
  871. // If we are at a limit we know no more bytes can be read. Otherwise, it's
  872. // hard to say without calling Refresh(), and we'd rather not do that.
  873. if (buffer_ == buffer_end_ &&
  874. ((buffer_size_after_limit_ != 0) ||
  875. (total_bytes_read_ == current_limit_))) {
  876. last_tag_ = 0; // Pretend we called ReadTag()...
  877. legitimate_message_end_ = true; // ... and it hit EOF.
  878. return true;
  879. } else {
  880. return false;
  881. }
  882. }
  883. inline int CodedInputStream::CurrentPosition() const {
  884. return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_);
  885. }
  886. inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) {
  887. if (buffer_size_ < size) {
  888. return NULL;
  889. } else {
  890. uint8* result = buffer_;
  891. Advance(size);
  892. return result;
  893. }
  894. }
  895. inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
  896. uint8* target) {
  897. if (value < 0x80) {
  898. *target = value;
  899. return target + 1;
  900. } else {
  901. return WriteVarint32FallbackToArray(value, target);
  902. }
  903. }
  904. inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
  905. if (value < 0) {
  906. WriteVarint64(static_cast<uint64>(value));
  907. } else {
  908. WriteVarint32(static_cast<uint32>(value));
  909. }
  910. }
  911. inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
  912. int32 value, uint8* target) {
  913. if (value < 0) {
  914. return WriteVarint64ToArray(static_cast<uint64>(value), target);
  915. } else {
  916. return WriteVarint32ToArray(static_cast<uint32>(value), target);
  917. }
  918. }
  919. inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
  920. uint8* target) {
  921. #if defined(PROTOBUF_LITTLE_ENDIAN)
  922. memcpy(target, &value, sizeof(value));
  923. #else
  924. target[0] = static_cast<uint8>(value);
  925. target[1] = static_cast<uint8>(value >> 8);
  926. target[2] = static_cast<uint8>(value >> 16);
  927. target[3] = static_cast<uint8>(value >> 24);
  928. #endif
  929. return target + sizeof(value);
  930. }
  931. inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
  932. uint8* target) {
  933. #if defined(PROTOBUF_LITTLE_ENDIAN)
  934. memcpy(target, &value, sizeof(value));
  935. #else
  936. uint32 part0 = static_cast<uint32>(value);
  937. uint32 part1 = static_cast<uint32>(value >> 32);
  938. target[0] = static_cast<uint8>(part0);
  939. target[1] = static_cast<uint8>(part0 >> 8);
  940. target[2] = static_cast<uint8>(part0 >> 16);
  941. target[3] = static_cast<uint8>(part0 >> 24);
  942. target[4] = static_cast<uint8>(part1);
  943. target[5] = static_cast<uint8>(part1 >> 8);
  944. target[6] = static_cast<uint8>(part1 >> 16);
  945. target[7] = static_cast<uint8>(part1 >> 24);
  946. #endif
  947. return target + sizeof(value);
  948. }
  949. inline void CodedOutputStream::WriteTag(uint32 value) {
  950. WriteVarint32(value);
  951. }
  952. inline uint8* CodedOutputStream::WriteTagToArray(
  953. uint32 value, uint8* target) {
  954. if (value < (1 << 7)) {
  955. target[0] = value;
  956. return target + 1;
  957. } else if (value < (1 << 14)) {
  958. target[0] = static_cast<uint8>(value | 0x80);
  959. target[1] = static_cast<uint8>(value >> 7);
  960. return target + 2;
  961. } else {
  962. return WriteVarint32FallbackToArray(value, target);
  963. }
  964. }
  965. inline int CodedOutputStream::VarintSize32(uint32 value) {
  966. if (value < (1 << 7)) {
  967. return 1;
  968. } else {
  969. return VarintSize32Fallback(value);
  970. }
  971. }
  972. inline int CodedOutputStream::VarintSize32SignExtended(int32 value) {
  973. if (value < 0) {
  974. return 10; // TODO(kenton): Make this a symbolic constant.
  975. } else {
  976. return VarintSize32(static_cast<uint32>(value));
  977. }
  978. }
  979. inline void CodedOutputStream::WriteString(const string& str) {
  980. WriteRaw(str.data(), static_cast<int>(str.size()));
  981. }
  982. inline void CodedOutputStream::WriteRawMaybeAliased(
  983. const void* data, int size) {
  984. if (aliasing_enabled_) {
  985. WriteAliasedRaw(data, size);
  986. } else {
  987. WriteRaw(data, size);
  988. }
  989. }
  990. inline uint8* CodedOutputStream::WriteStringToArray(
  991. const string& str, uint8* target) {
  992. return WriteRawToArray(str.data(), static_cast<int>(str.size()), target);
  993. }
  994. inline int CodedOutputStream::ByteCount() const {
  995. return total_bytes_ - buffer_size_;
  996. }
  997. inline void CodedInputStream::Advance(int amount) {
  998. buffer_ += amount;
  999. }
  1000. inline void CodedOutputStream::Advance(int amount) {
  1001. buffer_ += amount;
  1002. buffer_size_ -= amount;
  1003. }
  1004. inline void CodedInputStream::SetRecursionLimit(int limit) {
  1005. recursion_limit_ = limit;
  1006. }
  1007. inline bool CodedInputStream::IncrementRecursionDepth() {
  1008. ++recursion_depth_;
  1009. return recursion_depth_ <= recursion_limit_;
  1010. }
  1011. inline void CodedInputStream::DecrementRecursionDepth() {
  1012. if (recursion_depth_ > 0) --recursion_depth_;
  1013. }
  1014. inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
  1015. MessageFactory* factory) {
  1016. extension_pool_ = pool;
  1017. extension_factory_ = factory;
  1018. }
  1019. inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
  1020. return extension_pool_;
  1021. }
  1022. inline MessageFactory* CodedInputStream::GetExtensionFactory() {
  1023. return extension_factory_;
  1024. }
  1025. inline int CodedInputStream::BufferSize() const {
  1026. return buffer_end_ - buffer_;
  1027. }
  1028. inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
  1029. : input_(input),
  1030. buffer_(NULL),
  1031. buffer_end_(NULL),
  1032. total_bytes_read_(0),
  1033. overflow_bytes_(0),
  1034. last_tag_(0),
  1035. legitimate_message_end_(false),
  1036. aliasing_enabled_(false),
  1037. current_limit_(kint32max),
  1038. buffer_size_after_limit_(0),
  1039. total_bytes_limit_(kDefaultTotalBytesLimit),
  1040. total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
  1041. recursion_depth_(0),
  1042. recursion_limit_(default_recursion_limit_),
  1043. extension_pool_(NULL),
  1044. extension_factory_(NULL) {
  1045. // Eagerly Refresh() so buffer space is immediately available.
  1046. Refresh();
  1047. }
  1048. inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
  1049. : input_(NULL),
  1050. buffer_(buffer),
  1051. buffer_end_(buffer + size),
  1052. total_bytes_read_(size),
  1053. overflow_bytes_(0),
  1054. last_tag_(0),
  1055. legitimate_message_end_(false),
  1056. aliasing_enabled_(false),
  1057. current_limit_(size),
  1058. buffer_size_after_limit_(0),
  1059. total_bytes_limit_(kDefaultTotalBytesLimit),
  1060. total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
  1061. recursion_depth_(0),
  1062. recursion_limit_(default_recursion_limit_),
  1063. extension_pool_(NULL),
  1064. extension_factory_(NULL) {
  1065. // Note that setting current_limit_ == size is important to prevent some
  1066. // code paths from trying to access input_ and segfaulting.
  1067. }
  1068. inline bool CodedInputStream::IsFlat() const {
  1069. return input_ == NULL;
  1070. }
  1071. } // namespace io
  1072. } // namespace protobuf
  1073. #if defined(_MSC_VER) && _MSC_VER >= 1300
  1074. #pragma runtime_checks("c", restore)
  1075. #endif // _MSC_VER
  1076. } // namespace google
  1077. #endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__