C Libraries for LZMA Derived from 7-Zip

The Lempel-Ziv-Markov chain algorithm (LZMA), originally developed by Igor Pavlov for the 7-Zip file archiver, delivers exceptionally high compression ratios and fast decompression speeds. Because 7-Zip was originally authored in a mix of C and C++, several open-source projects have adapted its core compression and decompression logic into standalone C libraries. This article outlines the prominent open-source C libraries and tools directly derived from or packaging 7-Zip’s source code for LZMA and LZMA2 support.

1. The LZMA SDK (C Implementation)

The most direct C adaptation of 7-Zip's algorithm is the LZMA SDK, maintained by Igor Pavlov himself. While the full 7-Zip software package relies heavily on C++, Pavlov decoupled the core algorithmic routines into an ANSI C implementation distributed within the SDK.

  • Components: The SDK includes source files such as LzmaDec.c (LZMA decoder), LzmaEnc.c (LZMA encoder), Lzma2Dec.c, and Lzma2Enc.c.
  • Characteristics: It is designed to be self-contained, highly portable, and dependency-free, making it easy to drop directly into embedded systems, bootloaders, and firmware.
  • Licensing: The core C code in the LZMA SDK is placed in the public domain.

2. liblzma (XZ Utils)

liblzma is part of the XZ Utils package and represents the most widely deployed C library derived from 7-Zip's LZMA codebase.

  • Origins: It evolved from the older "LZMA Utils" project, which initially wrapped the LZMA SDK's C implementation. Over time, the developers refactored and expanded the code to create the .xz container format.
  • Capabilities: liblzma offers native support for both raw LZMA streams and the modernized .xz container format (which features LZMA2, integrity checks, and multithreading).
  • Usage: It serves as the standard decompression backend for package managers (such as dpkg and rpm), the Linux kernel build system, and general UNIX utility software.

3. p7zip

p7zip is the POSIX port of 7-Zip's command-line utilities.

  • Structure: While the broader architecture of p7zip is implemented in C++ to mirror the COM-like internal structure of 7-Zip, the underlying compression algorithms remain the C routines imported straight from Pavlov's codebase.
  • Role: Developers requiring a build system tailored for Linux, macOS, and BSD environments often extract the C/C++ core routines directly from p7zip rather than the Windows-centric upstream 7-Zip repository.

4. In-Tree Kernel and Bootloader Implementations

Several low-level infrastructure projects contain internal C libraries adapted directly from the 7-Zip LZMA SDK:

  • Linux Kernel (lib/decompress_unlzma.c): The Linux kernel contains an in-tree C decompressor derived directly from Pavlov’s LZMA SDK C decoder. It is utilized during the early boot process to unpack compressed kernel images (vmlinuz) and initial ramdisks (initramfs).
  • Das U-Boot: The popular bootloader for embedded devices features a minimal, standalone C port of LzmaDecode.c imported from the LZMA SDK to decompress payloads on memory-constrained systems.
  • GRUB 2: The GRUB bootloader includes bundled LZMA decompression code sourced from the LZMA SDK C library to reduce binary size on disk.