mpack logo

mpack

Free

A C encoder/decoder for [MessagePack](http://msgpack.com) messages suitable for resource constriants embedded systems. Supports disabling dynamic memory allocation and overriding malloc, free, and realloc.

FreeFree tier
Type
Open Source

About mpack

MPack is a C implementation of an encoder and decoder for the MessagePack serialization format, designed to be lightweight, secure, and extremely fast. It provides a buffered reader/writer for streaming data, a tree-style parser that decodes into dynamically typed nodes, and an Expect API for high-performance schema-based parsing. MPack is suitable for resource-constrained embedded systems, supporting compile-time customization to disable dynamic memory allocation and override memory functions. It can be embedded directly into a project via a single-file amalgamation and runs on a wide range of platforms, including 8-bit microcontrollers (e.g., Arduino), WebAssembly, Linux kernel, and all major desktop and mobile OSes. Helper functions enable file I/O, automatic buffer growth, UTF-8 validation, and more.

Key Features

Buffered reader and writer for streaming serialization
Tree-style parser that builds an immutable tree of dynamically-typed nodes
Expect API for high-performance, schema-driven decoding
Compile-time feature customization (disable malloc, override allocators, etc.)
Secure against untrusted data with robust error handling
Extremely fast and optimized for minimal memory footprint
Single-file amalgamation for easy embedding into any C project
Supports 8-bit microcontrollers (Arduino), WebAssembly, Linux kernel, and all major OSes
Helper functions for files, buffer growth, UTF-8 checking, and string allocation

Pros & Cons

Pros
  • Lightweight and embeddable with a small binary size
  • Designed with security in mind against malicious or corrupt data
  • Highly portable across diverse platforms and compilers
  • Thoroughly documented with code examples and API reference
  • Customizable compile-time settings to fit exact requirements
  • Dual API (Node and Expect) offers flexibility for different use cases
Cons
  • Requires manual memory management in constrained environments when disabling dynamic allocation
  • Documentation and examples are technical, geared toward C developers
  • Limited to MessagePack format; no native JSON or other serialization support
  • Learning curve for selecting and using the appropriate API (tree vs. expect)
  • May be overkill for simple projects that only need basic serialization

Best For

Embedded systems data serialization with limited memory resourcesMessagePack encoding/decoding for IoT devices and microcontrollersCross-platform data exchange between heterogeneous systemsPerformance-critical applications requiring a small code footprintIntegration into firmware or kernel-level software (e.g., Linux kernel modules)

FAQ

What is MPack?
MPack is a C implementation of an encoder and decoder for the MessagePack serialization format. It is designed to be simple, secure, lightweight, and extremely fast, with a focus on embedded systems.
Can MPack run on microcontrollers?
Yes, MPack supports 8-bit microcontrollers such as Arduino. It offers compile-time options to disable dynamic memory allocation and override malloc/free, making it suitable for resource-constrained environments.
Is MPack secure against malicious data?
Yes, MPack is designed to be secure against untrusted data. Invalid or corrupt input results in nil nodes or zero/false values, and errors are reported via the tree or writer status, preventing buffer overflows or undefined behavior.
How do I include MPack in my project?
You can download the amalgamation package (single .h and .c file) from the GitHub releases and add mpack.h and mpack.c to your project. No build system setup is required.
What platforms does MPack support?
MPack supports all modern compilers, desktop and smartphone OSes (Windows, macOS, Linux, iOS, Android), WebAssembly, inside the Linux kernel, and even 8-bit microcontrollers like Arduino.