1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: kenton@google.com (Kenton Varda)
32
// Based on original Protocol Buffers design by
33
// Sanjay Ghemawat, Jeff Dean, and others.
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
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.
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
52
// int fd = open("myfile", O_WRONLY);
53
// ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
54
// CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
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));
62
// delete coded_output;
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);
72
// coded_input->ReadLittleEndian32(&magic_number);
73
// if (magic_number != 1234) {
74
// cerr << "File not in expected format." << endl;
79
// coded_input->ReadVarint32(&size);
81
// char* text = new char[size + 1];
82
// coded_input->ReadRaw(buffer, size);
85
// delete coded_input;
89
// cout << "Text is: " << text << endl;
92
// For those who are interested, varint encoding is defined as follows:
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
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".
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.
109
#ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
110
#define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
114
#if defined(_M_IX86) && \
115
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
116
#define PROTOBUF_LITTLE_ENDIAN 1
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)
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
130
#include <google/protobuf/stubs/common.h>
136
class DescriptorPool;
137
class MessageFactory;
141
// Defined in this file.
142
class CodedInputStream;
143
class CodedOutputStream;
145
// Defined in other files.
146
class ZeroCopyInputStream; // zero_copy_stream.h
147
class ZeroCopyOutputStream; // zero_copy_stream.h
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.
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 {
158
// Create a CodedInputStream that reads from the given ZeroCopyInputStream.
159
explicit CodedInputStream(ZeroCopyInputStream* input);
161
// Create a CodedInputStream that reads from the given flat array. This is
162
// faster than using an ArrayInputStream. PushLimit(size) is implied by
164
explicit CodedInputStream(const uint8* buffer, int size);
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.
174
// Skips a number of bytes. Returns false if an underlying read error
176
bool Skip(int count);
178
// Sets *data to point directly at the unread part of the CodedInputStream's
179
// underlying buffer, and *size to the size of that buffer, but does not
180
// advance the stream's current position. This will always either produce
181
// a non-empty buffer or return false. If the caller consumes any of
182
// this data, it should then call Skip() to skip over the consumed bytes.
183
// This may be useful for implementing external fast parsing routines for
184
// types of data not covered by the CodedInputStream interface.
185
bool GetDirectBufferPointer(const void** data, int* size);
187
// Like GetDirectBufferPointer, but this method is inlined, and does not
188
// attempt to Refresh() if the buffer is currently empty.
189
inline void GetDirectBufferPointerInline(const void** data,
190
int* size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
192
// Read raw bytes, copying them into the given buffer.
193
bool ReadRaw(void* buffer, int size);
195
// Like ReadRaw, but reads into a string.
197
// Implementation Note: ReadString() grows the string gradually as it
198
// reads in the data, rather than allocating the entire requested size
199
// upfront. This prevents denial-of-service attacks in which a client
200
// could claim that a string is going to be MAX_INT bytes long in order to
201
// crash the server because it can't allocate this much space at once.
202
bool ReadString(string* buffer, int size);
203
// Like the above, with inlined optimizations. This should only be used
204
// by the protobuf implementation.
205
inline bool InternalReadStringInline(string* buffer,
206
int size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
209
// Read a 32-bit little-endian integer.
210
bool ReadLittleEndian32(uint32* value);
211
// Read a 64-bit little-endian integer.
212
bool ReadLittleEndian64(uint64* value);
214
// These methods read from an externally provided buffer. The caller is
215
// responsible for ensuring that the buffer has sufficient space.
216
// Read a 32-bit little-endian integer.
217
static const uint8* ReadLittleEndian32FromArray(const uint8* buffer,
219
// Read a 64-bit little-endian integer.
220
static const uint8* ReadLittleEndian64FromArray(const uint8* buffer,
223
// Read an unsigned integer with Varint encoding, truncating to 32 bits.
224
// Reading a 32-bit value is equivalent to reading a 64-bit one and casting
225
// it to uint32, but may be more efficient.
226
bool ReadVarint32(uint32* value);
227
// Read an unsigned integer with Varint encoding.
228
bool ReadVarint64(uint64* value);
230
// Read a tag. This calls ReadVarint32() and returns the result, or returns
231
// zero (which is not a valid tag) if ReadVarint32() fails. Also, it updates
232
// the last tag value, which can be checked with LastTagWas().
233
// Always inline because this is only called in once place per parse loop
234
// but it is called for every iteration of said loop, so it should be fast.
235
// GCC doesn't want to inline this by default.
236
uint32 ReadTag() GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
238
// Usually returns true if calling ReadVarint32() now would produce the given
239
// value. Will always return false if ReadVarint32() would not return the
240
// given value. If ExpectTag() returns true, it also advances past
241
// the varint. For best performance, use a compile-time constant as the
243
// Always inline because this collapses to a small number of instructions
244
// when given a constant parameter, but GCC doesn't want to inline by default.
245
bool ExpectTag(uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
247
// Like above, except this reads from the specified buffer. The caller is
248
// responsible for ensuring that the buffer is large enough to read a varint
249
// of the expected size. For best performance, use a compile-time constant as
250
// the expected tag parameter.
252
// Returns a pointer beyond the expected tag if it was found, or NULL if it
254
static const uint8* ExpectTagFromArray(
256
uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
258
// Usually returns true if no more bytes can be read. Always returns false
259
// if more bytes can be read. If ExpectAtEnd() returns true, a subsequent
260
// call to LastTagWas() will act as if ReadTag() had been called and returned
261
// zero, and ConsumedEntireMessage() will return true.
264
// If the last call to ReadTag() returned the given value, returns true.
265
// Otherwise, returns false;
267
// This is needed because parsers for some types of embedded messages
268
// (with field type TYPE_GROUP) don't actually know that they've reached the
269
// end of a message until they see an ENDGROUP tag, which was actually part
270
// of the enclosing message. The enclosing message would like to check that
271
// tag to make sure it had the right number, so it calls LastTagWas() on
272
// return from the embedded parser to check.
273
bool LastTagWas(uint32 expected);
275
// When parsing message (but NOT a group), this method must be called
276
// immediately after MergeFromCodedStream() returns (if it returns true)
277
// to further verify that the message ended in a legitimate way. For
278
// example, this verifies that parsing did not end on an end-group tag.
279
// It also checks for some cases where, due to optimizations,
280
// MergeFromCodedStream() can incorrectly return true.
281
bool ConsumedEntireMessage();
283
// Limits ----------------------------------------------------------
284
// Limits are used when parsing length-delimited embedded messages.
285
// After the message's length is read, PushLimit() is used to prevent
286
// the CodedInputStream from reading beyond that length. Once the
287
// embedded message has been parsed, PopLimit() is called to undo the
290
// Opaque type used with PushLimit() and PopLimit(). Do not modify
291
// values of this type yourself. The only reason that this isn't a
292
// struct with private internals is for efficiency.
295
// Places a limit on the number of bytes that the stream may read,
296
// starting from the current position. Once the stream hits this limit,
297
// it will act like the end of the input has been reached until PopLimit()
300
// As the names imply, the stream conceptually has a stack of limits. The
301
// shortest limit on the stack is always enforced, even if it is not the
304
// The value returned by PushLimit() is opaque to the caller, and must
305
// be passed unchanged to the corresponding call to PopLimit().
306
Limit PushLimit(int byte_limit);
308
// Pops the last limit pushed by PushLimit(). The input must be the value
309
// returned by that call to PushLimit().
310
void PopLimit(Limit limit);
312
// Returns the number of bytes left until the nearest limit on the
313
// stack is hit, or -1 if no limits are in place.
314
int BytesUntilLimit();
316
// Total Bytes Limit -----------------------------------------------
317
// To prevent malicious users from sending excessively large messages
318
// and causing integer overflows or memory exhaustion, CodedInputStream
319
// imposes a hard limit on the total number of bytes it will read.
321
// Sets the maximum number of bytes that this CodedInputStream will read
322
// before refusing to continue. To prevent integer overflows in the
323
// protocol buffers implementation, as well as to prevent servers from
324
// allocating enormous amounts of memory to hold parsed messages, the
325
// maximum message length should be limited to the shortest length that
326
// will not harm usability. The theoretical shortest message that could
327
// cause integer overflows is 512MB. The default limit is 64MB. Apps
328
// should set shorter limits if possible. If warning_threshold is not -1,
329
// a warning will be printed to stderr after warning_threshold bytes are
330
// read. An error will always be printed to stderr if the limit is
333
// This is unrelated to PushLimit()/PopLimit().
335
// Hint: If you are reading this because your program is printing a
336
// warning about dangerously large protocol messages, you may be
337
// confused about what to do next. The best option is to change your
338
// design such that excessively large messages are not necessary.
339
// For example, try to design file formats to consist of many small
340
// messages rather than a single large one. If this is infeasible,
341
// you will need to increase the limit. Chances are, though, that
342
// your code never constructs a CodedInputStream on which the limit
343
// can be set. You probably parse messages by calling things like
344
// Message::ParseFromString(). In this case, you will need to change
345
// your code to instead construct some sort of ZeroCopyInputStream
346
// (e.g. an ArrayInputStream), construct a CodedInputStream around
347
// that, then call Message::ParseFromCodedStream() instead. Then
348
// you can adjust the limit. Yes, it's more work, but you're doing
349
// something unusual.
350
void SetTotalBytesLimit(int total_bytes_limit, int warning_threshold);
352
// Recursion Limit -------------------------------------------------
353
// To prevent corrupt or malicious messages from causing stack overflows,
354
// we must keep track of the depth of recursion when parsing embedded
355
// messages and groups. CodedInputStream keeps track of this because it
356
// is the only object that is passed down the stack during parsing.
358
// Sets the maximum recursion depth. The default is 64.
359
void SetRecursionLimit(int limit);
361
// Increments the current recursion depth. Returns true if the depth is
362
// under the limit, false if it has gone over.
363
bool IncrementRecursionDepth();
365
// Decrements the recursion depth.
366
void DecrementRecursionDepth();
368
// Extension Registry ----------------------------------------------
369
// ADVANCED USAGE: 99.9% of people can ignore this section.
371
// By default, when parsing extensions, the parser looks for extension
372
// definitions in the pool which owns the outer message's Descriptor.
373
// However, you may call SetExtensionRegistry() to provide an alternative
374
// pool instead. This makes it possible, for example, to parse a message
375
// using a generated class, but represent some extensions using
378
// Set the pool used to look up extensions. Most users do not need to call
379
// this as the correct pool will be chosen automatically.
381
// WARNING: It is very easy to misuse this. Carefully read the requirements
382
// below. Do not use this unless you are sure you need it. Almost no one
385
// Let's say you are parsing a message into message object m, and you want
386
// to take advantage of SetExtensionRegistry(). You must follow these
389
// The given DescriptorPool must contain m->GetDescriptor(). It is not
390
// sufficient for it to simply contain a descriptor that has the same name
391
// and content -- it must be the *exact object*. In other words:
392
// assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
393
// m->GetDescriptor());
394
// There are two ways to satisfy this requirement:
395
// 1) Use m->GetDescriptor()->pool() as the pool. This is generally useless
396
// because this is the pool that would be used anyway if you didn't call
397
// SetExtensionRegistry() at all.
398
// 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
399
// "underlay". Read the documentation for DescriptorPool for more
400
// information about underlays.
402
// You must also provide a MessageFactory. This factory will be used to
403
// construct Message objects representing extensions. The factory's
404
// GetPrototype() MUST return non-NULL for any Descriptor which can be found
405
// through the provided pool.
407
// If the provided factory might return instances of protocol-compiler-
408
// generated (i.e. compiled-in) types, or if the outer message object m is
409
// a generated type, then the given factory MUST have this property: If
410
// GetPrototype() is given a Descriptor which resides in
411
// DescriptorPool::generated_pool(), the factory MUST return the same
412
// prototype which MessageFactory::generated_factory() would return. That
413
// is, given a descriptor for a generated type, the factory must return an
414
// instance of the generated class (NOT DynamicMessage). However, when
415
// given a descriptor for a type that is NOT in generated_pool, the factory
416
// is free to return any implementation.
418
// The reason for this requirement is that generated sub-objects may be
419
// accessed via the standard (non-reflection) extension accessor methods,
420
// and these methods will down-cast the object to the generated class type.
421
// If the object is not actually of that type, the results would be undefined.
422
// On the other hand, if an extension is not compiled in, then there is no
423
// way the code could end up accessing it via the standard accessors -- the
424
// only way to access the extension is via reflection. When using reflection,
425
// DynamicMessage and generated messages are indistinguishable, so it's fine
426
// if these objects are represented using DynamicMessage.
428
// Using DynamicMessageFactory on which you have called
429
// SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
430
// above requirement.
432
// If either pool or factory is NULL, both must be NULL.
434
// Note that this feature is ignored when parsing "lite" messages as they do
435
// not have descriptors.
436
void SetExtensionRegistry(DescriptorPool* pool, MessageFactory* factory);
438
// Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
439
// has been provided.
440
const DescriptorPool* GetExtensionPool();
442
// Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
443
// factory has been provided.
444
MessageFactory* GetExtensionFactory();
447
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
449
ZeroCopyInputStream* input_;
450
const uint8* buffer_;
451
const uint8* buffer_end_; // pointer to the end of the buffer.
452
int total_bytes_read_; // total bytes read from input_, including
453
// the current buffer
455
// If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
456
// so that we can BackUp() on destruction.
459
// LastTagWas() stuff.
460
uint32 last_tag_; // result of last ReadTag().
462
// This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
463
// at EOF, or by ExpectAtEnd() when it returns true. This happens when we
464
// reach the end of a message and attempt to read another tag.
465
bool legitimate_message_end_;
467
// See EnableAliasing().
468
bool aliasing_enabled_;
471
Limit current_limit_; // if position = -1, no limit is applied
473
// For simplicity, if the current buffer crosses a limit (either a normal
474
// limit created by PushLimit() or the total bytes limit), buffer_size_
475
// only tracks the number of bytes before that limit. This field
476
// contains the number of bytes after it. Note that this implies that if
477
// buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
478
// hit a limit. However, if both are zero, it doesn't necessarily mean
479
// we aren't at a limit -- the buffer may have ended exactly at the limit.
480
int buffer_size_after_limit_;
482
// Maximum number of bytes to read, period. This is unrelated to
483
// current_limit_. Set using SetTotalBytesLimit().
484
int total_bytes_limit_;
485
int total_bytes_warning_threshold_;
487
// Current recursion depth, controlled by IncrementRecursionDepth() and
488
// DecrementRecursionDepth().
489
int recursion_depth_;
490
// Recursion depth limit, set by SetRecursionLimit().
491
int recursion_limit_;
493
// See SetExtensionRegistry().
494
const DescriptorPool* extension_pool_;
495
MessageFactory* extension_factory_;
497
// Private member functions.
499
// Advance the buffer by a given number of bytes.
500
void Advance(int amount);
502
// Back up input_ to the current buffer position.
503
void BackUpInputToCurrentPosition();
505
// Recomputes the value of buffer_size_after_limit_. Must be called after
506
// current_limit_ or total_bytes_limit_ changes.
507
void RecomputeBufferLimits();
509
// Writes an error message saying that we hit total_bytes_limit_.
510
void PrintTotalBytesLimitError();
512
// Called when the buffer runs out to request more data. Implies an
513
// Advance(BufferSize()).
516
// When parsing varints, we optimize for the common case of small values, and
517
// then optimize for the case when the varint fits within the current buffer
518
// piece. The Fallback method is used when we can't use the one-byte
519
// optimization. The Slow method is yet another fallback when the buffer is
520
// not large enough. Making the slow path out-of-line speeds up the common
521
// case by 10-15%. The slow path is fairly uncommon: it only triggers when a
522
// message crosses multiple buffers.
523
bool ReadVarint32Fallback(uint32* value);
524
bool ReadVarint64Fallback(uint64* value);
525
bool ReadVarint32Slow(uint32* value);
526
bool ReadVarint64Slow(uint64* value);
527
bool ReadLittleEndian32Fallback(uint32* value);
528
bool ReadLittleEndian64Fallback(uint64* value);
529
// Fallback/slow methods for reading tags. These do not update last_tag_,
530
// but will set legitimate_message_end_ if we are at the end of the input
532
uint32 ReadTagFallback();
533
uint32 ReadTagSlow();
534
bool ReadStringFallback(string* buffer, int size);
536
// Return the size of the buffer.
537
int BufferSize() const;
539
static const int kDefaultTotalBytesLimit = 64 << 20; // 64MB
541
static const int kDefaultTotalBytesWarningThreshold = 32 << 20; // 32MB
542
static const int kDefaultRecursionLimit = 64;
545
// Class which encodes and writes binary data which is composed of varint-
546
// encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream.
547
// Most users will not need to deal with CodedOutputStream.
549
// Most methods of CodedOutputStream which return a bool return false if an
550
// underlying I/O error occurs. Once such a failure occurs, the
551
// CodedOutputStream is broken and is no longer useful. The Write* methods do
552
// not return the stream status, but will invalidate the stream if an error
553
// occurs. The client can probe HadError() to determine the status.
555
// Note that every method of CodedOutputStream which writes some data has
556
// a corresponding static "ToArray" version. These versions write directly
557
// to the provided buffer, returning a pointer past the last written byte.
558
// They require that the buffer has sufficient capacity for the encoded data.
559
// This allows an optimization where we check if an output stream has enough
560
// space for an entire message before we start writing and, if there is, we
561
// call only the ToArray methods to avoid doing bound checks for each
563
// i.e., in the example above:
565
// CodedOutputStream coded_output = new CodedOutputStream(raw_output);
566
// int magic_number = 1234;
567
// char text[] = "Hello world!";
569
// int coded_size = sizeof(magic_number) +
570
// CodedOutputStream::Varint32Size(strlen(text)) +
574
// coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
575
// if (buffer != NULL) {
576
// // The output stream has enough space in the buffer: write directly to
578
// buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
580
// buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
581
// buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
583
// // Make bound-checked writes, which will ask the underlying stream for
584
// // more space as needed.
585
// coded_output->WriteLittleEndian32(magic_number);
586
// coded_output->WriteVarint32(strlen(text));
587
// coded_output->WriteRaw(text, strlen(text));
590
// delete coded_output;
591
class LIBPROTOBUF_EXPORT CodedOutputStream {
593
// Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
594
explicit CodedOutputStream(ZeroCopyOutputStream* output);
596
// Destroy the CodedOutputStream and position the underlying
597
// ZeroCopyOutputStream immediately after the last byte written.
598
~CodedOutputStream();
600
// Skips a number of bytes, leaving the bytes unmodified in the underlying
601
// buffer. Returns false if an underlying write error occurs. This is
602
// mainly useful with GetDirectBufferPointer().
603
bool Skip(int count);
605
// Sets *data to point directly at the unwritten part of the
606
// CodedOutputStream's underlying buffer, and *size to the size of that
607
// buffer, but does not advance the stream's current position. This will
608
// always either produce a non-empty buffer or return false. If the caller
609
// writes any data to this buffer, it should then call Skip() to skip over
610
// the consumed bytes. This may be useful for implementing external fast
611
// serialization routines for types of data not covered by the
612
// CodedOutputStream interface.
613
bool GetDirectBufferPointer(void** data, int* size);
615
// If there are at least "size" bytes available in the current buffer,
616
// returns a pointer directly into the buffer and advances over these bytes.
617
// The caller may then write directly into this buffer (e.g. using the
618
// *ToArray static methods) rather than go through CodedOutputStream. If
619
// there are not enough bytes available, returns NULL. The return pointer is
620
// invalidated as soon as any other non-const method of CodedOutputStream
622
inline uint8* GetDirectBufferForNBytesAndAdvance(int size);
624
// Write raw bytes, copying them from the given buffer.
625
void WriteRaw(const void* buffer, int size);
626
// Like WriteRaw() but writing directly to the target array.
627
// This is _not_ inlined, as the compiler often optimizes memcpy into inline
628
// copy loops. Since this gets called by every field with string or bytes
629
// type, inlining may lead to a significant amount of code bloat, with only a
630
// minor performance gain.
631
static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
633
// Equivalent to WriteRaw(str.data(), str.size()).
634
void WriteString(const string& str);
635
// Like WriteString() but writing directly to the target array.
636
static uint8* WriteStringToArray(const string& str, uint8* target);
639
// Write a 32-bit little-endian integer.
640
void WriteLittleEndian32(uint32 value);
641
// Like WriteLittleEndian32() but writing directly to the target array.
642
static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
643
// Write a 64-bit little-endian integer.
644
void WriteLittleEndian64(uint64 value);
645
// Like WriteLittleEndian64() but writing directly to the target array.
646
static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
648
// Write an unsigned integer with Varint encoding. Writing a 32-bit value
649
// is equivalent to casting it to uint64 and writing it as a 64-bit value,
650
// but may be more efficient.
651
void WriteVarint32(uint32 value);
652
// Like WriteVarint32() but writing directly to the target array.
653
static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
654
// Write an unsigned integer with Varint encoding.
655
void WriteVarint64(uint64 value);
656
// Like WriteVarint64() but writing directly to the target array.
657
static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
659
// Equivalent to WriteVarint32() except when the value is negative,
660
// in which case it must be sign-extended to a full 10 bytes.
661
void WriteVarint32SignExtended(int32 value);
662
// Like WriteVarint32SignExtended() but writing directly to the target array.
663
static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
665
// This is identical to WriteVarint32(), but optimized for writing tags.
666
// In particular, if the input is a compile-time constant, this method
667
// compiles down to a couple instructions.
668
// Always inline because otherwise the aformentioned optimization can't work,
669
// but GCC by default doesn't want to inline this.
670
void WriteTag(uint32 value);
671
// Like WriteTag() but writing directly to the target array.
672
static uint8* WriteTagToArray(
673
uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
675
// Returns the number of bytes needed to encode the given value as a varint.
676
static int VarintSize32(uint32 value);
677
// Returns the number of bytes needed to encode the given value as a varint.
678
static int VarintSize64(uint64 value);
680
// If negative, 10 bytes. Otheriwse, same as VarintSize32().
681
static int VarintSize32SignExtended(int32 value);
683
// Returns the total number of bytes written since this object was created.
684
inline int ByteCount() const;
686
// Returns true if there was an underlying I/O error since this object was
688
bool HadError() const { return had_error_; }
691
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
693
ZeroCopyOutputStream* output_;
696
int total_bytes_; // Sum of sizes of all buffers seen so far.
697
bool had_error_; // Whether an error occurred during output.
699
// Advance the buffer by a given number of bytes.
700
void Advance(int amount);
702
// Called when the buffer runs out to request more data. Implies an
703
// Advance(buffer_size_).
706
static uint8* WriteVarint32FallbackToArray(uint32 value, uint8* target);
708
// Always-inlined versions of WriteVarint* functions so that code can be
709
// reused, while still controlling size. For instance, WriteVarint32ToArray()
710
// should not directly call this: since it is inlined itself, doing so
711
// would greatly increase the size of generated code. Instead, it should call
712
// WriteVarint32FallbackToArray. Meanwhile, WriteVarint32() is already
713
// out-of-line, so it should just invoke this directly to avoid any extra
714
// function call overhead.
715
static uint8* WriteVarint32FallbackToArrayInline(
716
uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
717
static uint8* WriteVarint64ToArrayInline(
718
uint64 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
720
static int VarintSize32Fallback(uint32 value);
723
// inline methods ====================================================
724
// The vast majority of varints are only one byte. These inline
725
// methods optimize for that case.
727
inline bool CodedInputStream::ReadVarint32(uint32* value) {
728
if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
733
return ReadVarint32Fallback(value);
737
inline bool CodedInputStream::ReadVarint64(uint64* value) {
738
if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
743
return ReadVarint64Fallback(value);
748
inline const uint8* CodedInputStream::ReadLittleEndian32FromArray(
751
#if defined(PROTOBUF_LITTLE_ENDIAN)
752
memcpy(value, buffer, sizeof(*value));
753
return buffer + sizeof(*value);
755
*value = (static_cast<uint32>(buffer[0]) ) |
756
(static_cast<uint32>(buffer[1]) << 8) |
757
(static_cast<uint32>(buffer[2]) << 16) |
758
(static_cast<uint32>(buffer[3]) << 24);
759
return buffer + sizeof(*value);
763
inline const uint8* CodedInputStream::ReadLittleEndian64FromArray(
766
#if defined(PROTOBUF_LITTLE_ENDIAN)
767
memcpy(value, buffer, sizeof(*value));
768
return buffer + sizeof(*value);
770
uint32 part0 = (static_cast<uint32>(buffer[0]) ) |
771
(static_cast<uint32>(buffer[1]) << 8) |
772
(static_cast<uint32>(buffer[2]) << 16) |
773
(static_cast<uint32>(buffer[3]) << 24);
774
uint32 part1 = (static_cast<uint32>(buffer[4]) ) |
775
(static_cast<uint32>(buffer[5]) << 8) |
776
(static_cast<uint32>(buffer[6]) << 16) |
777
(static_cast<uint32>(buffer[7]) << 24);
778
*value = static_cast<uint64>(part0) |
779
(static_cast<uint64>(part1) << 32);
780
return buffer + sizeof(*value);
784
inline bool CodedInputStream::ReadLittleEndian32(uint32* value) {
785
#if defined(PROTOBUF_LITTLE_ENDIAN)
786
if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
787
memcpy(value, buffer_, sizeof(*value));
788
Advance(sizeof(*value));
791
return ReadLittleEndian32Fallback(value);
794
return ReadLittleEndian32Fallback(value);
798
inline bool CodedInputStream::ReadLittleEndian64(uint64* value) {
799
#if defined(PROTOBUF_LITTLE_ENDIAN)
800
if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
801
memcpy(value, buffer_, sizeof(*value));
802
Advance(sizeof(*value));
805
return ReadLittleEndian64Fallback(value);
808
return ReadLittleEndian64Fallback(value);
812
inline uint32 CodedInputStream::ReadTag() {
813
if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] < 0x80) {
814
last_tag_ = buffer_[0];
818
last_tag_ = ReadTagFallback();
823
inline bool CodedInputStream::LastTagWas(uint32 expected) {
824
return last_tag_ == expected;
827
inline bool CodedInputStream::ConsumedEntireMessage() {
828
return legitimate_message_end_;
831
inline bool CodedInputStream::ExpectTag(uint32 expected) {
832
if (expected < (1 << 7)) {
833
if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) {
839
} else if (expected < (1 << 14)) {
840
if (GOOGLE_PREDICT_TRUE(BufferSize() >= 2) &&
841
buffer_[0] == static_cast<uint8>(expected | 0x80) &&
842
buffer_[1] == static_cast<uint8>(expected >> 7)) {
849
// Don't bother optimizing for larger values.
854
inline const uint8* CodedInputStream::ExpectTagFromArray(
855
const uint8* buffer, uint32 expected) {
856
if (expected < (1 << 7)) {
857
if (buffer[0] == expected) {
860
} else if (expected < (1 << 14)) {
861
if (buffer[0] == static_cast<uint8>(expected | 0x80) &&
862
buffer[1] == static_cast<uint8>(expected >> 7)) {
869
inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
872
*size = buffer_end_ - buffer_;
875
inline bool CodedInputStream::ExpectAtEnd() {
876
// If we are at a limit we know no more bytes can be read. Otherwise, it's
877
// hard to say without calling Refresh(), and we'd rather not do that.
879
if (buffer_ == buffer_end_ && buffer_size_after_limit_ != 0) {
880
last_tag_ = 0; // Pretend we called ReadTag()...
881
legitimate_message_end_ = true; // ... and it hit EOF.
888
inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) {
889
if (buffer_size_ < size) {
892
uint8* result = buffer_;
898
inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
904
return WriteVarint32FallbackToArray(value, target);
908
inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
910
WriteVarint64(static_cast<uint64>(value));
912
WriteVarint32(static_cast<uint32>(value));
916
inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
917
int32 value, uint8* target) {
919
return WriteVarint64ToArray(static_cast<uint64>(value), target);
921
return WriteVarint32ToArray(static_cast<uint32>(value), target);
925
inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
927
#if defined(PROTOBUF_LITTLE_ENDIAN)
928
memcpy(target, &value, sizeof(value));
930
target[0] = static_cast<uint8>(value);
931
target[1] = static_cast<uint8>(value >> 8);
932
target[2] = static_cast<uint8>(value >> 16);
933
target[3] = static_cast<uint8>(value >> 24);
935
return target + sizeof(value);
938
inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
940
#if defined(PROTOBUF_LITTLE_ENDIAN)
941
memcpy(target, &value, sizeof(value));
943
uint32 part0 = static_cast<uint32>(value);
944
uint32 part1 = static_cast<uint32>(value >> 32);
946
target[0] = static_cast<uint8>(part0);
947
target[1] = static_cast<uint8>(part0 >> 8);
948
target[2] = static_cast<uint8>(part0 >> 16);
949
target[3] = static_cast<uint8>(part0 >> 24);
950
target[4] = static_cast<uint8>(part1);
951
target[5] = static_cast<uint8>(part1 >> 8);
952
target[6] = static_cast<uint8>(part1 >> 16);
953
target[7] = static_cast<uint8>(part1 >> 24);
955
return target + sizeof(value);
958
inline void CodedOutputStream::WriteTag(uint32 value) {
959
WriteVarint32(value);
962
inline uint8* CodedOutputStream::WriteTagToArray(
963
uint32 value, uint8* target) {
964
if (value < (1 << 7)) {
967
} else if (value < (1 << 14)) {
968
target[0] = static_cast<uint8>(value | 0x80);
969
target[1] = static_cast<uint8>(value >> 7);
972
return WriteVarint32FallbackToArray(value, target);
976
inline int CodedOutputStream::VarintSize32(uint32 value) {
977
if (value < (1 << 7)) {
980
return VarintSize32Fallback(value);
984
inline int CodedOutputStream::VarintSize32SignExtended(int32 value) {
986
return 10; // TODO(kenton): Make this a symbolic constant.
988
return VarintSize32(static_cast<uint32>(value));
992
inline void CodedOutputStream::WriteString(const string& str) {
993
WriteRaw(str.data(), str.size());
996
inline uint8* CodedOutputStream::WriteStringToArray(
997
const string& str, uint8* target) {
998
return WriteRawToArray(str.data(), str.size(), target);
1001
inline int CodedOutputStream::ByteCount() const {
1002
return total_bytes_ - buffer_size_;
1005
inline void CodedInputStream::Advance(int amount) {
1009
inline void CodedOutputStream::Advance(int amount) {
1011
buffer_size_ -= amount;
1014
inline void CodedInputStream::SetRecursionLimit(int limit) {
1015
recursion_limit_ = limit;
1018
inline bool CodedInputStream::IncrementRecursionDepth() {
1020
return recursion_depth_ <= recursion_limit_;
1023
inline void CodedInputStream::DecrementRecursionDepth() {
1024
if (recursion_depth_ > 0) --recursion_depth_;
1027
inline void CodedInputStream::SetExtensionRegistry(DescriptorPool* pool,
1028
MessageFactory* factory) {
1029
extension_pool_ = pool;
1030
extension_factory_ = factory;
1033
inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
1034
return extension_pool_;
1037
inline MessageFactory* CodedInputStream::GetExtensionFactory() {
1038
return extension_factory_;
1041
inline int CodedInputStream::BufferSize() const {
1042
return buffer_end_ - buffer_;
1045
inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
1049
total_bytes_read_(0),
1052
legitimate_message_end_(false),
1053
aliasing_enabled_(false),
1054
current_limit_(kint32max),
1055
buffer_size_after_limit_(0),
1056
total_bytes_limit_(kDefaultTotalBytesLimit),
1057
total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
1058
recursion_depth_(0),
1059
recursion_limit_(kDefaultRecursionLimit),
1060
extension_pool_(NULL),
1061
extension_factory_(NULL) {
1062
// Eagerly Refresh() so buffer space is immediately available.
1066
inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
1069
buffer_end_(buffer + size),
1070
total_bytes_read_(size),
1073
legitimate_message_end_(false),
1074
aliasing_enabled_(false),
1075
current_limit_(size),
1076
buffer_size_after_limit_(0),
1077
total_bytes_limit_(kDefaultTotalBytesLimit),
1078
total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
1079
recursion_depth_(0),
1080
recursion_limit_(kDefaultRecursionLimit),
1081
extension_pool_(NULL),
1082
extension_factory_(NULL) {
1083
// Note that setting current_limit_ == size is important to prevent some
1084
// code paths from trying to access input_ and segfaulting.
1087
inline CodedInputStream::~CodedInputStream() {
1088
if (input_ != NULL) {
1089
BackUpInputToCurrentPosition();
1094
} // namespace protobuf
1096
} // namespace google
1098
#if defined(_MSC_VER) && _MSC_VER >= 1300
1099
#pragma runtime_checks("c", restore)
1102
#endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__