Implementation:Alibaba MNN Protobuf Parse Context CC
Appearance
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/parse_context.cc
|
| Line Count | 559 |
| Domains | Serialization, Parsing |
| Key Class | ParseContext
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Streaming parse context implementation. Contains the runtime logic for the high-performance parsing pipeline used to deserialize protobuf wire-format data.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
This source file implements the ParseContext class and its helper EpsCopyInputStream. The parsing pipeline is designed for zero-copy performance where possible:
- EpsCopyInputStream -- Manages a stream buffer with an epsilon (safety margin) region. It guarantees that at least
kSlopBytesare available past the current read position, eliminating per-byte bounds checks during varint and fixed-width reads. - ParseContext -- Wraps
EpsCopyInputStreamwith depth tracking and group-end detection. It coordinates recursive message parsing while enforcing the maximum nesting depth to prevent stack overflow attacks. - Buffer management -- Handles transitions between flat buffer segments and streaming input, copying tail bytes into a patch buffer when a segment boundary is reached.
- Error handling -- Propagates parse errors via a null-pointer return convention rather than exceptions.
Usage
#include "google/protobuf/parse_context.h"
Used by generated _InternalParse methods and the core protobuf runtime during deserialization.
Code Reference
// EpsCopyInputStream manages buffered reading with safety margin.
const char* EpsCopyInputStream::Next(int overrun, int chunk_size) {
// Refills the buffer, copying leftover bytes into the patch buffer.
// Returns nullptr on EOF or error.
}
// ParseContext tracks recursion depth during nested message parsing.
const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
int old_depth = depth_;
// Push depth limit, parse sub-message, restore depth.
ptr = msg->_InternalParse(ptr, this);
depth_ = old_depth;
return ptr;
}
// Done check for end-of-group or end-of-message.
bool ParseContext::Done(const char** ptr) {
return DoneNoSlopCheck(ptr);
}
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | const char* buffer |
Wire-format bytes provided as a flat buffer or stream |
| Output | const char* advanced pointer |
Updated read position after consuming fields; nullptr on error
|
| State | Recursion depth, buffer bounds | Maintained across nested ParseMessage calls
|
Usage Examples
// Internal parse loop pattern used by generated code
const char* MyMessage::_InternalParse(const char* ptr,
ParseContext* ctx) {
while (!ctx->Done(&ptr)) {
uint32_t tag;
ptr = ReadTag(ptr, &tag);
switch (tag >> 3) {
case 1:
ptr = ctx->ParseMessage(mutable_sub_msg(), ptr);
break;
default:
ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx);
break;
}
}
return ptr;
}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment