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