Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN Protobuf Wire Format Lite CC

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/wire_format_lite.cc
Line Count 784
Domains Serialization, Wire_Protocol
Key Methods ReadTag, WriteTag, varint encoding
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Lite wire format implementation containing the core encoding and decoding routines for protobuf wire types. Implements varint encoding, tag reading/writing, and field-level serialization for all primitive types.

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 non-inline methods of WireFormatLite, providing the low-level binary encoding that underpins all protobuf serialization. Key implementations include:

  • Varint encoding/decoding -- Variable-length integer encoding used for field tags, int32, int64, uint32, uint64, sint32, sint64, and enum values. Uses the standard 7-bit-per-byte encoding with continuation bits.
  • ZigZag encoding -- Signed integer encoding that maps negative values to positive values for efficient varint representation (sint32, sint64).
  • Tag operations -- ReadTag reads a field tag (field number + wire type), WriteTag writes one. Tags are varint-encoded.
  • Fixed-width encoding -- Direct byte-order serialization for fixed32, fixed64, float, double.
  • Length-delimited fields -- Reading and writing strings, bytes, and embedded messages with a varint length prefix.
  • Group handling -- Legacy group wire format support (start/end group tags).
  • Field skipping -- SkipField advances past an unrecognized field without fully parsing it.

Usage

#include "google/protobuf/wire_format_lite.h"

Called by generated protobuf code and the runtime serialization pipeline.

Code Reference

// Varint encoding: write a 32-bit varint to output.
uint8_t* WireFormatLite::WriteVarint32ToArray(uint32_t value, uint8_t* target) {
  while (value >= 0x80) {
    *target++ = static_cast<uint8_t>(value | 0x80);
    value >>= 7;
  }
  *target++ = static_cast<uint8_t>(value);
  return target;
}

// ZigZag encoding for signed integers.
inline uint32_t WireFormatLite::ZigZagEncode32(int32_t n) {
  return static_cast<uint32_t>((n << 1) ^ (n >> 31));
}

// Read a tag from input stream.
bool WireFormatLite::ReadTag(io::CodedInputStream* input, uint32_t* tag) {
  return input->ReadVarint32(tag);
}

// Skip a field based on wire type.
bool WireFormatLite::SkipField(io::CodedInputStream* input,
                                uint32_t tag) {
  switch (WireFormatLite::GetTagWireType(tag)) {
    case WIRETYPE_VARINT:
      return input->Skip(input->ReadVarint64(nullptr));
    case WIRETYPE_FIXED64:
      return input->Skip(8);
    case WIRETYPE_LENGTH_DELIMITED: {
      uint32_t length;
      input->ReadVarint32(&length);
      return input->Skip(length);
    }
    case WIRETYPE_FIXED32:
      return input->Skip(4);
    default:
      return false;
  }
}

I/O Contract

Direction Type Description
Input Raw byte stream Wire-format bytes from CodedInputStream
Output Raw byte stream Wire-format bytes written to CodedOutputStream or uint8_t*
Encoding Varint, ZigZag, fixed Type-specific binary encodings per protobuf wire type

Usage Examples

// Writing a varint-encoded int32 field (field number 1)
uint8_t buffer[128];
uint8_t* ptr = buffer;
ptr = WireFormatLite::WriteInt32ToArray(
    1,    // field number
    42,   // value
    ptr);

// ZigZag encoding for signed values
uint32_t encoded = WireFormatLite::ZigZagEncode32(-1);
// encoded == 1
uint32_t encoded2 = WireFormatLite::ZigZagEncode32(1);
// encoded2 == 2

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment