class CodedInputStream

Defined at line 149 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

Class which reads and decodes binary data which is composed of varint-

encoded integers and fixed-width pieces. Wraps a ZeroCopyInputStream.

Most users will not need to deal with CodedInputStream.

Most methods of CodedInputStream that return a bool return false if an

underlying I/O error occurs or if the data is malformed. Once such a

failure occurs, the CodedInputStream is broken and is no longer useful.

After a failure, callers also should assume writes to "out" args may have

occurred, though nothing useful can be determined from those writes.

Public Methods

void CodedInputStream (const CodedInputStream & )

Defined at line 158 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool GetDirectBufferPointer (const void ** data, int * size)

Sets *data to point directly at the unread part of the CodedInputStream's

underlying buffer, and *size to the size of that buffer, but does not

advance the stream's current position. This will always either produce

a non-empty buffer or return false. If the caller consumes any of

this data, it should then call Skip() to skip over the consumed bytes.

This may be useful for implementing external fast parsing routines for

types of data not covered by the CodedInputStream interface.

bool ReadRaw (void * buffer, int size)

Read raw bytes, copying them into the given buffer.

bool ReadString (std::string * buffer, int size)

Like ReadRaw, but reads into a string.

bool ReadCord (absl::Cord * output, int size)

Like ReadString(), but reads to a Cord.

CodedInputStream & operator= (const CodedInputStream & )

Defined at line 159 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

uint32_t ReadTag ()

Read a tag. This calls ReadVarint32() and returns the result, or returns

zero (which is not a valid tag) if ReadVarint32() fails. Also, ReadTag

(but not ReadTagNoLastTag) updates the last tag value, which can be checked

with LastTagWas().

Always inline because this is only called in one place per parse loop

but it is called for every iteration of said loop, so it should be fast.

GCC doesn't want to inline this by default.

Defined at line 245 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

std::pair<uint32_t, bool> ReadTagWithCutoff (uint32_t cutoff)

This usually a faster alternative to ReadTag() when cutoff is a manifest

constant. It does particularly well for cutoff >= 127. The first part

of the return value is the tag that was read, though it can also be 0 in

the cases where ReadTag() would return 0. If the second part is true

then the tag is known to be in [0, cutoff]. If not, the tag either is

above cutoff or is 0. (There's intentional wiggle room when tag is 0,

because that can arise in several ways, and for best performance we want

to avoid an extra "is tag == 0?" check here.)

Defined at line 259 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void SetLastTag (uint32_t tag)

Defined at line 307 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void SetConsumed ()

Defined at line 316 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

int RecursionBudget ()

Defined at line 385 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

int GetDefaultRecursionLimit ()

Defined at line 387 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void CodedInputStream (ZeroCopyInputStream * input)

Create a CodedInputStream that reads from the given ZeroCopyInputStream.

Defined at line 1557 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void CodedInputStream (const uint8_t * buffer, int size)

Create a CodedInputStream that reads from the given flat array. This is

faster than using an ArrayInputStream. PushLimit(size) is implied by

this constructor.

Defined at line 1578 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void ~CodedInputStream ()

Destroy the CodedInputStream and position the underlying

ZeroCopyInputStream at the first unread byte. If an error occurred while

reading (causing a method to return false), then the exact position of

the input stream may be anywhere between the last value that was read

successfully and the stream's byte limit.

bool IsFlat ()

Return true if this CodedInputStream reads from a flat array instead of

a ZeroCopyInputStream.

Defined at line 1599 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool Skip (int count)

Skips a number of bytes. Returns false if an underlying read error

occurs.

Defined at line 1601 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void GetDirectBufferPointerInline (const void ** data, int * size)

Like GetDirectBufferPointer, but this method is inlined, and does not

attempt to Refresh() if the buffer is currently empty.

Defined at line 1494 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadLittleEndian16 (uint16_t * value)

Read a 16-bit little-endian integer.

Defined at line 1357 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadLittleEndian32 (uint32_t * value)

Read a 32-bit little-endian integer.

Defined at line 1366 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadLittleEndian64 (uint64_t * value)

Read a 64-bit little-endian integer.

Defined at line 1380 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

const uint8_t * ReadLittleEndian16FromArray (const uint8_t * buffer, uint16_t * value)

These methods read from an externally provided buffer. The caller is

responsible for ensuring that the buffer has sufficient space.

Read a 16-bit little-endian integer.

Defined at line 1336 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

const uint8_t * ReadLittleEndian32FromArray (const uint8_t * buffer, uint32_t * value)

Read a 32-bit little-endian integer.

Defined at line 1343 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

const uint8_t * ReadLittleEndian64FromArray (const uint8_t * buffer, uint64_t * value)

Read a 64-bit little-endian integer.

Defined at line 1350 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadVarint32 (uint32_t * value)

Read an unsigned integer with Varint encoding, truncating to 32 bits.

Reading a 32-bit value is equivalent to reading a 64-bit one and casting

it to uint32_t, but may be more efficient.

Defined at line 1296 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadVarint64 (uint64_t * value)

Read an unsigned integer with Varint encoding.

Defined at line 1311 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ReadVarintSizeAsInt (int * value)

Reads a varint off the wire into an "int". This should be used for reading

sizes off the wire (sizes of strings, submessages, bytes fields, etc).

The value from the wire is interpreted as unsigned. If its value exceeds

the representable value of an integer on this platform, instead of

truncating we return false. Truncating (as performed by ReadVarint32()

above) is an acceptable approach for fields representing an integer, but

when we are parsing a size from the wire, truncating the value would result

in us misparsing the payload.

Defined at line 1322 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

uint32_t ReadTagNoLastTag ()

Defined at line 1394 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

std::pair<uint32_t, bool> ReadTagWithCutoffNoLastTag (uint32_t cutoff)

Defined at line 1407 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ExpectTag (uint32_t expected)

Usually returns true if calling ReadVarint32() now would produce the given

value. Will always return false if ReadVarint32() would not return the

given value. If ExpectTag() returns true, it also advances past

the varint. For best performance, use a compile-time constant as the

parameter.

Always inline because this collapses to a small number of instructions

when given a constant parameter, but GCC doesn't want to inline by default.

Defined at line 1455 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

const uint8_t * ExpectTagFromArray (const uint8_t * buffer, uint32_t expected)

Like above, except this reads from the specified buffer. The caller is

responsible for ensuring that the buffer is large enough to read a varint

of the expected size. For best performance, use a compile-time constant as

the expected tag parameter.

Returns a pointer beyond the expected tag if it was found, or NULL if it

was not.

Defined at line 1479 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ExpectAtEnd ()

Usually returns true if no more bytes can be read. Always returns false

if more bytes can be read. If ExpectAtEnd() returns true, a subsequent

call to LastTagWas() will act as if ReadTag() had been called and returned

zero, and ConsumedEntireMessage() will return true.

Defined at line 1500 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool LastTagWas (uint32_t expected)

If the last call to ReadTag() or ReadTagWithCutoff() returned the given

value, returns true. Otherwise, returns false.

ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last

returned value.

This is needed because parsers for some types of embedded messages

(with field type TYPE_GROUP) don't actually know that they've reached the

end of a message until they see an ENDGROUP tag, which was actually part

of the enclosing message. The enclosing message would like to check that

tag to make sure it had the right number, so it calls LastTagWas() on

return from the embedded parser to check.

Defined at line 1447 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool ConsumedEntireMessage ()

When parsing message (but NOT a group), this method must be called

immediately after MergeFromCodedStream() returns (if it returns true)

to further verify that the message ended in a legitimate way. For

example, this verifies that parsing did not end on an end-group tag.

It also checks for some cases where, due to optimizations,

MergeFromCodedStream() can incorrectly return true.

Defined at line 1451 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

Limit PushLimit (int byte_limit)

Places a limit on the number of bytes that the stream may read,

starting from the current position. Once the stream hits this limit,

it will act like the end of the input has been reached until PopLimit()

is called.

As the names imply, the stream conceptually has a stack of limits. The

shortest limit on the stack is always enforced, even if it is not the

top limit.

The value returned by PushLimit() is opaque to the caller, and must

be passed unchanged to the corresponding call to PopLimit().

void PopLimit (Limit limit)

Pops the last limit pushed by PushLimit(). The input must be the value

returned by that call to PushLimit().

int BytesUntilLimit ()

Returns the number of bytes left until the nearest limit on the

stack is hit, or -1 if no limits are in place.

int CurrentPosition ()

Returns current position relative to the beginning of the input stream.

Defined at line 1514 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void SetTotalBytesLimit (int total_bytes_limit)

Sets the maximum number of bytes that this CodedInputStream will read

before refusing to continue. To prevent servers from allocating enormous

amounts of memory to hold parsed messages, the maximum message length

should be limited to the shortest length that will not harm usability.

The default limit is INT_MAX (~2GB) and apps should set shorter limits

if possible. An error will always be printed to stderr if the limit is

reached.

Note: setting a limit less than the current read position is interpreted

as a limit on the current position.

This is unrelated to PushLimit()/PopLimit().

int BytesUntilTotalBytesLimit ()

The Total Bytes Limit minus the Current Position, or -1 if the total bytes

limit is INT_MAX.

void SetRecursionLimit (int limit)

Sets the maximum recursion depth. The default is 100.

Defined at line 1520 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

bool IncrementRecursionDepth ()

Increments the current recursion depth. Returns true if the depth is

under the limit, false if it has gone over.

Defined at line 1525 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void DecrementRecursionDepth ()

Decrements the recursion depth if possible.

Defined at line 1530 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

void UnsafeDecrementRecursionDepth ()

Decrements the recursion depth blindly. This is faster than

DecrementRecursionDepth(). It should be used only if all previous

increments to recursion depth were successful.

Defined at line 1534 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

std::pair<CodedInputStream::Limit, int> IncrementRecursionDepthAndPushLimit (int byte_limit)

Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_).

Using this can reduce code size and complexity in some cases. The caller

is expected to check that the second part of the result is non-negative (to

bail out if the depth of recursion is too high) and, if all is well, to

later pass the first part of the result to PopLimit() or similar.

Limit ReadLengthAndPushLimit ()

Shorthand for PushLimit(ReadVarint32(

&length

) ? length : 0).

bool DecrementRecursionDepthAndPopLimit (Limit limit)

Helper that is equivalent to: {

bool result = ConsumedEntireMessage();

PopLimit(limit);

UnsafeDecrementRecursionDepth();

return result; }

Using this can reduce code size and complexity in some cases.

Do not use unless the current recursion depth is greater than zero.

bool CheckEntireMessageConsumedAndPopLimit (Limit limit)

Helper that is equivalent to: {

bool result = ConsumedEntireMessage();

PopLimit(limit);

return result; }

Using this can reduce code size and complexity in some cases.

void SetExtensionRegistry (const DescriptorPool * pool, MessageFactory * factory)

Set the pool used to look up extensions. Most users do not need to call

this as the correct pool will be chosen automatically.

WARNING: It is very easy to misuse this. Carefully read the requirements

below. Do not use this unless you are sure you need it. Almost no one

does.

Let's say you are parsing a message into message object m, and you want

to take advantage of SetExtensionRegistry(). You must follow these

requirements:

The given DescriptorPool must contain m->GetDescriptor(). It is not

sufficient for it to simply contain a descriptor that has the same name

and content -- it must be the *exact object*. In other words:

assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==

m->GetDescriptor());

There are two ways to satisfy this requirement:

1) Use m->GetDescriptor()->pool() as the pool. This is generally useless

because this is the pool that would be used anyway if you didn't call

SetExtensionRegistry() at all.

2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an

"underlay". Read the documentation for DescriptorPool for more

information about underlays.

You must also provide a MessageFactory. This factory will be used to

construct Message objects representing extensions. The factory's

GetPrototype() MUST return non-NULL for any Descriptor which can be found

through the provided pool.

If the provided factory might return instances of protocol-compiler-

generated (i.e. compiled-in) types, or if the outer message object m is

a generated type, then the given factory MUST have this property: If

GetPrototype() is given a Descriptor which resides in

DescriptorPool::generated_pool(), the factory MUST return the same

prototype which MessageFactory::generated_factory() would return. That

is, given a descriptor for a generated type, the factory must return an

instance of the generated class (NOT DynamicMessage). However, when

given a descriptor for a type that is NOT in generated_pool, the factory

is free to return any implementation.

The reason for this requirement is that generated sub-objects may be

accessed via the standard (non-reflection) extension accessor methods,

and these methods will down-cast the object to the generated class type.

If the object is not actually of that type, the results would be undefined.

On the other hand, if an extension is not compiled in, then there is no

way the code could end up accessing it via the standard accessors -- the

only way to access the extension is via reflection. When using reflection,

DynamicMessage and generated messages are indistinguishable, so it's fine

if these objects are represented using DynamicMessage.

Using DynamicMessageFactory on which you have called

SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the

above requirement.

If either pool or factory is NULL, both must be NULL.

Note that this feature is ignored when parsing "lite" messages as they do

not have descriptors.

Defined at line 1539 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

const DescriptorPool * GetExtensionPool ()

Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool

has been provided.

Defined at line 1545 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

MessageFactory * GetExtensionFactory ()

Get the MessageFactory set via SetExtensionRegistry(), or NULL if no

factory has been provided.

Defined at line 1549 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h

Friends

class EpsCopyByteStream
class ZeroCopyCodedInputStream