Skip to content

API Reference

medoomem edited this page Jul 23, 2026 · 3 revisions

API Reference

moezip offers three distinct integration interfaces depending on your programming language and runtime environment:

  1. Native Python Module (import moezip): Compiled with PyBind11 for high-performance Python applications.
  2. C-API Shared Library (moezip.dll / libmoezip.so): Exports C-linkage functions for ctypes, C, C++, Rust, or Go.
  3. Native C++ Headers: Direct C++20 header inclusion for native C++ projects.

1. Native Python Extension (import moezip)

When built with PyBind11 (moezip.pyd on Windows / moezip.so on Linux), the C++ engine exposes thread-safe, high-level functions directly to Python. Embedded assets load automatically on first execution.

moezip.compress(text: str) -> bytes

Compresses a UTF-8 string into a binary bytes buffer using embedded router weights and dictionary tables.

  • Parameters: text (str) – Raw input string.
  • Returns: bytes – Compressed binary data payload.
  • Raises: std::runtime_error if encoding fails.

moezip.decompress(packed_bytes: bytes) -> str

Decompresses a moezip binary bytes buffer back into its exact UTF-8 string representation.

  • Parameters: packed_bytes (bytes) – Compressed binary data buffer.
  • Returns: str – Restored UTF-8 text string.
  • Raises: std::runtime_error if payload structure is invalid or corrupt.

Python Example

import moezip

# Direct string-to-bytes compression
data = "Sample string to compress using embedded assets."
compressed = moezip.compress(data)

# Bytes-to-string decompression
restored = moezip.decompress(compressed)

assert data == restored
print(f"Compressed size: {len(compressed)} bytes")

2. C-API Shared Library (moezip.dll / libmoezip.so)

For integration via Python ctypes, C, C++, Rust, or Go, moezip exports C-linkage functions (extern "C") through api.cpp.

Functions

void init_engine(const char* vocab_path, const char* router_path)

Pre-loads search tables and state transition matrices into memory.

  • vocab_path: File path to custom vocabulary text file, or NULL / "" to load embedded C++ binary assets.
  • router_path: File path to custom router JSON file, or NULL / "" to load embedded C++ binary assets.

int compress_text(const char* text, uint8_t* out_buf, int max_out_len)

Compresses a null-terminated UTF-8 text string into an output byte buffer.

  • Returns: Integer length of written bytes in out_buf, or -1 if output buffer size is insufficient.

int decompress_bytes(const uint8_t* in_buf, int in_len, char* out_buf, int max_out_len)

Decompresses raw binary bytes back into a null-terminated UTF-8 character buffer.

  • Returns: Integer character length written to out_buf, or -1 if output buffer size is insufficient.

Python ctypes Integration Example

import os
import sys
import ctypes

# Select platform shared library
lib_name = "moezip.dll" if sys.platform == "win32" else "libmoezip.so"
lib_dir = "build/Release" if sys.platform == "win32" else "build_linux"
lib_path = os.path.abspath(os.path.join(lib_dir, lib_name))

moezip = ctypes.CDLL(lib_path)

# Configure C function signatures
moezip.init_engine.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
moezip.compress_text.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_int]
moezip.compress_text.restype = ctypes.c_int
moezip.decompress_bytes.argtypes = [ctypes.POINTER(ctypes.c_uint8), ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
moezip.decompress_bytes.restype = ctypes.c_int

# Initialize engine with embedded binary assets (pass empty strings)
moezip.init_engine(b"", b"")

def compress(text: str) -> bytes:
    raw_bytes = text.encode('utf-8')
    buf_size = len(raw_bytes) * 2 + 2048
    out_buf = (ctypes.c_uint8 * buf_size)()
    comp_len = moezip.compress_text(raw_bytes, out_buf, buf_size)
    if comp_len <= 0:
        raise RuntimeError("Compression error")
    return bytes(out_buf[:comp_len])

def decompress(comp_bytes: bytes) -> str:
    in_buf = (ctypes.c_uint8 * len(comp_bytes)).from_buffer_copy(comp_bytes)
    buf_size = len(comp_bytes) * 10 + 4096
    out_buf = ctypes.create_string_buffer(buf_size)
    decomp_len = moezip.decompress_bytes(in_buf, len(comp_bytes), out_buf, buf_size)
    if decomp_len <= 0:
        raise RuntimeError("Decompression error")
    return out_buf.value.decode('utf-8')

3. Native C++ Integration

For native C++20 projects, include the core headers directly into your build system.

Core Headers

  • #include "vocab.hpp": Vocabulary partitioning and dictionary data structures.
  • #include "router.hpp": $32 \times 32$ Markov transition matrix and predictor routines.
  • #include "codec.hpp": Primary adaptive compression and decompression entry points.

Primary C++ Functions

// Load vocabulary assets (or embedded defaults if empty string passed)
VocabData vd = load_and_partition_wordlist("");

// Load router transition matrix (or embedded defaults if empty string passed)
TransitionMatrix matrix = load_router("", vd.expert_count);

// Compress text to binary byte vector
std::vector<uint8_t> packed = compress_adaptive_moe(
    text, vd.vocab_to_idx, matrix, vd.expert_count, vd.expert_size
);

// Decompress binary byte vector back to string
std::string restored = decompress_adaptive_moe(
    packed, vd.vocab, vd.vocab_to_idx, matrix, vd.expert_count, vd.expert_size
);

Native C++ Code Example

#include <iostream>
#include <string>
#include <vector>
#include "vocab.hpp"
#include "router.hpp"
#include "codec.hpp"

int main() {
    // Initialize search engine assets
    VocabData vd = load_and_partition_wordlist("");
    TransitionMatrix matrix = load_router("", vd.expert_count);

    std::string input = "Sample text for native C++ compression test.";

    // Compress
    auto compressed = compress_adaptive_moe(
        input, vd.vocab_to_idx, matrix, vd.expert_count, vd.expert_size
    );

    // Decompress
    std::string decompressed = decompress_adaptive_moe(
        compressed, vd.vocab, vd.vocab_to_idx, matrix, vd.expert_count, vd.expert_size
    );

    std::cout << "Original size  : " << input.size() << " bytes\n";
    std::cout << "Compressed size: " << compressed.size() << " bytes\n";
    std::cout << "Restored string: " << decompressed << "\n";

    return 0;
}

🗜️ moezip Wiki


📦 Target Artifacts

  • Windows (MSVC):
    • moezip.exe (CLI)
    • moezip.dll (Shared Lib)
    • moezip.pyd (Python)
  • Linux (GCC):
    • moezip (CLI)
    • libmoezip.so (Shared Lib)
    • moezip.so (Python)

🔗 Quick Links

Clone this wiki locally