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 H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/wire_format_lite.h
Line Count 1915
Domains Serialization, Wire_Protocol
Key Class WireFormatLite
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Lite wire format header with field encoding/decoding declarations. Defines the WireFormatLite class containing all wire type constants, field type enums, and inline serialization/deserialization methods for the protobuf-lite runtime.

Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.

Description

This is the largest header in the wire format subsystem and serves as the foundation for all protobuf binary encoding. WireFormatLite is a utility class with only static methods and constants:

  • Wire type constants -- WIRETYPE_VARINT (0), WIRETYPE_FIXED64 (1), WIRETYPE_LENGTH_DELIMITED (2), WIRETYPE_START_GROUP (3), WIRETYPE_END_GROUP (4), WIRETYPE_FIXED32 (5).
  • Field type enum -- FieldType enumerating all protobuf field types (TYPE_INT32, TYPE_STRING, TYPE_MESSAGE, etc.) with mappings to their wire types.
  • Tag manipulation -- MakeTag, GetTagFieldNumber, GetTagWireType for constructing and decomposing field tags.
  • Read methods -- Templated and type-specific ReadPrimitive, ReadString, ReadMessage, ReadGroup for deserializing fields from input.
  • Write methods -- WriteInt32, WriteString, WriteMessage, etc., plus "ToArray" variants that write directly to a byte buffer.
  • Size methods -- Int32Size, StringSize, MessageSize, etc., for computing the serialized size of each field type.
  • ZigZag encoding -- ZigZagEncode32/64 and ZigZagDecode32/64 for signed integer types.

Most methods are inline for maximum serialization throughput.

Usage

#include "google/protobuf/wire_format_lite.h"

Included by all generated protobuf code and internal runtime files.

Code Reference

class PROTOBUF_EXPORT WireFormatLite {
 public:
  // Wire types.
  enum WireType {
    WIRETYPE_VARINT           = 0,
    WIRETYPE_FIXED64          = 1,
    WIRETYPE_LENGTH_DELIMITED = 2,
    WIRETYPE_START_GROUP      = 3,
    WIRETYPE_END_GROUP        = 4,
    WIRETYPE_FIXED32          = 5,
  };

  // Field types.
  enum FieldType {
    TYPE_INT32      = 1,
    TYPE_INT64      = 2,
    TYPE_UINT32     = 3,
    TYPE_UINT64     = 4,
    TYPE_SINT32     = 5,
    TYPE_SINT64     = 6,
    TYPE_FIXED32    = 7,
    TYPE_FIXED64    = 8,
    TYPE_SFIXED32   = 9,
    TYPE_SFIXED64   = 10,
    TYPE_FLOAT      = 11,
    TYPE_DOUBLE     = 12,
    TYPE_BOOL       = 13,
    TYPE_STRING     = 14,
    TYPE_BYTES      = 15,
    TYPE_MESSAGE    = 16,
    TYPE_GROUP      = 17,
    TYPE_ENUM       = 18,
    MAX_FIELD_TYPE  = 18,
  };

  // Tag operations.
  static uint32_t MakeTag(int field_number, WireType type);
  static int GetTagFieldNumber(uint32_t tag);
  static WireType GetTagWireType(uint32_t tag);

  // ZigZag encoding for signed integers.
  static uint32_t ZigZagEncode32(int32_t n);
  static int32_t ZigZagDecode32(uint32_t n);
  static uint64_t ZigZagEncode64(int64_t n);
  static int64_t ZigZagDecode64(uint64_t n);

  // Read methods.
  template <typename CType, enum FieldType DeclaredType>
  static bool ReadPrimitive(io::CodedInputStream* input, CType* value);
  static bool ReadString(io::CodedInputStream* input, std::string* value);
  static bool ReadMessage(io::CodedInputStream* input,
                          MessageLite* value);

  // Write methods.
  static void WriteInt32(int field_number, int32_t value,
                         io::CodedOutputStream* output);
  static void WriteString(int field_number, const std::string& value,
                          io::CodedOutputStream* output);
  static void WriteMessage(int field_number, const MessageLite& value,
                           io::CodedOutputStream* output);

  // Write-to-array variants (returns advanced pointer).
  static uint8_t* WriteInt32ToArray(int field_number, int32_t value,
                                     uint8_t* target);
  static uint8_t* WriteStringToArray(int field_number,
                                      const std::string& value,
                                      uint8_t* target);

  // Size computation.
  static size_t Int32Size(int32_t value);
  static size_t StringSize(const std::string& value);
  static size_t MessageSize(const MessageLite& value);
};

I/O Contract

Direction Type Description
Input CodedInputStream / const char* Wire-format bytes to decode
Output CodedOutputStream / uint8_t* Wire-format bytes produced by encoding
Constants Wire types, field types Compile-time mappings between protobuf types and wire representations

Usage Examples

// Making a tag for field number 3, varint type
uint32_t tag = WireFormatLite::MakeTag(
    3, WireFormatLite::WIRETYPE_VARINT);

// Decomposing a tag
int field_num = WireFormatLite::GetTagFieldNumber(tag);  // 3
auto wire_type = WireFormatLite::GetTagWireType(tag);    // WIRETYPE_VARINT

// Computing serialized size
size_t sz = WireFormatLite::Int32Size(42);  // varint size of 42

Related Pages

Page Connections

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