# imagemagick/lzma architecture

> Architecture diagram of the GitHub repository imagemagick/lzma, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/imagemagick/lzma
- Repository: https://github.com/imagemagick/lzma

## Overview

1. **Repository purpose and top-level shape**
   XZ Utils is split into a reusable compression library, `liblzma`, and a set of command-line tools built on top of it. The repo is organized around three major concerns: core compression/decompression code, user-facing executables, and the build/test/documentation machinery around them.

2. **Core library boundary: `src/liblzma`**
   `liblzma` is the central subsystem and the main API boundary for external consumers. Public headers live under `src/liblzma/api/lzma/*.h`, while the implementation is split into internal modules by function and codec family. This is the library that applications link against for `.xz` and legacy `.lzma` support.

3. **Codec pipeline architecture inside `liblzma`**
   The library implements a layered pipeline: container/stream handling, block encoding/decoding, filters, and lower-level entropy/coder primitives. Major internal areas include:
   - `common/` for stream, block, index, and container orchestration
   - `lzma/` for LZMA/LZMA2-specific encoder and decoder logic
   - `delta/` for delta filter support
   - `simple/` for BCJ-style architecture-specific filters
   - `rangecoder/` for range coding primitives
   - `check/` for integrity checks like CRC32, CRC64, and SHA-256

4. **Data flow for compression and decompression**
   Compression flows from filter selection into block encoding, then stream/container assembly, with optional multithreaded encoding in `stream_encoder_mt.c`. Decompression reverses that path through stream decoding, block decoding, filter reconstruction, and integrity verification. The library also supports parsing and producing indexes and stream flags, which are part of the `.xz` container format.

5. **Command-line tool layer: `src/xz`, `src/xzdec`, `src/lzmainfo`**
   The main user-facing tool is `xz`, which provides compression, decompression, listing, testing, and file management behavior similar in spirit to `gzip`. `xzdec` is a smaller decompression-only utility, and `lzmainfo` reports metadata about `.lzma` files. These tools are thin wrappers over `liblzma` plus their own option parsing, I/O handling, messaging, sandboxing, and signal logic.

6. **`xz` application subsystems**
   Inside `src/xz`, the code is further split into operational concerns: argument parsing, file I/O, coder setup, listing/info output, suffix handling, hardware capability detection, sandbox support, messages, signals, and utility routines. This indicates a classic CLI architecture where the executable orchestrates library calls and system interactions rather than implementing compression logic itself.

7. **Platform and system abstraction layer: `src/common` and `lib/getopt*`**
   Shared portability code lives in `src/common` and `lib/`, covering thread abstractions, gettext integration, wide-string helpers, process name handling, physical memory detection, exit handling, and a bundled `getopt` implementation. This layer isolates OS-specific behavior so the library and tools can build across Unix-like systems and Windows.

8. **Build system and configuration**
   The project uses GNU Autotools as the primary build path (`configure.ac`, `Makefile.am`, `m4/`), with CMake support also present (`CMakeLists.txt`, `cmake/`). Configuration determines platform features, threading, symbol visibility, and generated headers like `config.h` and `src/liblzma/api/lzma/version.h`. This build layer is a significant architectural boundary because much of the code is conditionally compiled for portability and CPU-feature specialization.

9. **Testing and verification**
   Tests are extensive and split between unit tests, file-format fixtures, shell integration tests, and fuzzing harnesses. The `tests/files` corpus contains valid, invalid, and unsupported `.xz`/`.lzma` samples used to validate container parsing and error handling. `tests/ossfuzz` provides fuzz targets for stream and encoder/decoder paths, reflecting that the library’s attack surface is primarily in parsing and decoding untrusted compressed data.

10. **Documentation, examples, and localization**
    Documentation is part of the repo architecture, not an afterthought: `doc/examples` demonstrates library usage, `doc/man` and `src/scripts/*.in` cover CLI behavior, and `po/` plus `po4a/` handle translations for messages and man pages. The translated CLI/manpage layer is built through gettext and po4a tooling, which matters because user-visible text is generated alongside the binaries.

11. **Platform packaging and Windows-specific support**
    The `windows/` and `dos/` directories contain platform-specific build and integration assets, including alternative build instructions and resource files for Windows binaries. These are not separate implementations of compression, but they form an important deployment boundary for how the same core library and tools are packaged on non-POSIX systems.

12. **External-facing architectural summary**
    The repo’s overall architecture is: applications and scripts call into `liblzma`; `liblzma` composes codecs, filters, checks, and container logic; portability layers adapt the code to different OSes and CPUs; and the build/test/localization infrastructure ensures the same functionality ships across platforms with strong format-validation coverage.

Main entry points: [`configure.ac`](https://github.com/imagemagick/lzma/blob/main/configure.ac) (Autotools), [`src/xz`](https://github.com/imagemagick/lzma/tree/main/src/xz) (xz), [`src/liblzma/api/lzma`](https://github.com/imagemagick/lzma/tree/main/src/liblzma/api/lzma) (Public API), [`src/liblzma/common`](https://github.com/imagemagick/lzma/tree/main/src/liblzma/common) (Container Core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_build["Build & Portability"]
  node_build_autotools["Autotools<br/>build-system<br/>[configure.ac]"]
  node_build_cmake["CMake<br/>build-system<br/>[CMakeLists.txt]"]
  node_build_portability["Portability<br/>compat layer"]
  node_build_getopt["Getopt<br/>cli parsing"]
  node_build_windows["Windows<br/>platform packaging"]
end

subgraph group_lib["liblzma"]
  node_lib_api["Public API<br/>headers"]
  node_lib_common["Container Core<br/>stream/block"]
  node_lib_lzma["LZMA Codec"]
  node_lib_filters["Filters"]
  node_lib_entropy["Entropy Core<br/>range coder"]
  node_lib_checks["Integrity Checks<br/>checksums"]
end

subgraph group_cli["Command-line tools"]
  node_cli_xz["xz<br/>main CLI"]
  node_cli_xzdec["xzdec<br/>decompress CLI"]
  node_cli_lzmainfo["lzmainfo<br/>metadata CLI"]
  node_cli_scripts["Helper scripts<br/>shell tooling"]
end

subgraph group_quality["Quality & Docs"]
  node_quality_tests["Tests<br/>verification"]
  node_quality_fuzz["Fuzzing<br/>harnesses"]
  node_quality_docs["Docs<br/>documentation"]
  node_quality_translations["Translations<br/>localization"]
end

node_build_autotools -->|"configures"| node_build_portability
node_build_cmake -->|"configures"| node_build_portability
node_build_autotools -->|"generates"| node_lib_api
node_build_autotools -->|"builds"| node_cli_xz
node_build_autotools -->|"builds"| node_cli_xzdec
node_build_autotools -->|"builds"| node_cli_lzmainfo
node_build_autotools -->|"builds"| node_lib_common
node_build_autotools -->|"builds"| node_lib_lzma
node_build_autotools -->|"builds"| node_lib_filters
node_build_getopt -->|"parses args"| node_cli_xz
node_build_getopt -->|"parses args"| node_cli_xzdec
node_build_getopt -->|"parses args"| node_cli_lzmainfo
node_build_portability -->|"supports"| node_cli_xz
node_build_portability -->|"supports"| node_cli_xzdec
node_build_portability -->|"supports"| node_lib_common
node_lib_api -->|"exposes"| node_lib_common
node_lib_common -->|"orchestrates"| node_lib_lzma
node_lib_common -->|"chains"| node_lib_filters
node_lib_lzma -->|"uses"| node_lib_entropy
node_lib_lzma -->|"verifies"| node_lib_checks
node_lib_filters -->|"feeds"| node_lib_common
node_cli_xz -->|"calls"| node_lib_api
node_cli_xzdec -->|"calls"| node_lib_api
node_cli_lzmainfo -->|"calls"| node_lib_api
node_cli_xz -->|"uses"| node_build_portability
node_cli_scripts -->|"generates"| node_quality_docs
node_quality_tests -->|"validates"| node_lib_api
node_quality_tests -->|"validates"| node_cli_xz
node_quality_fuzz -->|"fuzzes"| node_lib_common
node_quality_fuzz -->|"fuzzes"| node_lib_lzma
node_quality_docs -->|"documents"| node_cli_xz
node_quality_docs -->|"documents"| node_lib_api
node_quality_translations -->|"localizes"| node_cli_xz
node_quality_translations -->|"localizes"| node_quality_docs
node_build_windows -->|"packages"| node_cli_xz
node_build_windows -->|"packages"| node_lib_api

click node_build_autotools "https://github.com/imagemagick/lzma/blob/main/configure.ac"
click node_build_cmake "https://github.com/imagemagick/lzma/blob/main/CMakeLists.txt"
click node_build_portability "https://github.com/imagemagick/lzma/tree/main/src/common"
click node_build_getopt "https://github.com/imagemagick/lzma/tree/main/lib"
click node_build_windows "https://github.com/imagemagick/lzma/tree/main/windows"
click node_lib_api "https://github.com/imagemagick/lzma/tree/main/src/liblzma/api/lzma"
click node_lib_common "https://github.com/imagemagick/lzma/tree/main/src/liblzma/common"
click node_lib_lzma "https://github.com/imagemagick/lzma/tree/main/src/liblzma/lzma"
click node_lib_filters "https://github.com/imagemagick/lzma/tree/main/src/liblzma/delta"
click node_lib_entropy "https://github.com/imagemagick/lzma/tree/main/src/liblzma/rangecoder"
click node_lib_checks "https://github.com/imagemagick/lzma/tree/main/src/liblzma/check"
click node_cli_xz "https://github.com/imagemagick/lzma/tree/main/src/xz"
click node_cli_xzdec "https://github.com/imagemagick/lzma/tree/main/src/xzdec"
click node_cli_lzmainfo "https://github.com/imagemagick/lzma/tree/main/src/lzmainfo"
click node_cli_scripts "https://github.com/imagemagick/lzma/tree/main/src/scripts"
click node_quality_tests "https://github.com/imagemagick/lzma/tree/main/tests"
click node_quality_fuzz "https://github.com/imagemagick/lzma/tree/main/tests/ossfuzz"
click node_quality_docs "https://github.com/imagemagick/lzma/tree/main/doc"
click node_quality_translations "https://github.com/imagemagick/lzma/tree/main/po"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_build_autotools,node_build_cmake,node_build_portability,node_build_getopt,node_build_windows toneBlue
class node_lib_api,node_lib_common,node_lib_lzma,node_lib_filters,node_lib_entropy,node_lib_checks toneAmber
class node_cli_xz,node_cli_xzdec,node_cli_lzmainfo,node_cli_scripts toneMint
class node_quality_tests,node_quality_fuzz,node_quality_docs,node_quality_translations toneRose
```

## Components

### Build & Portability

Autotools/CMake, portability shims, and platform-specific packaging.

- [Autotools](https://github.com/imagemagick/lzma/blob/main/configure.ac): build-system · Primary configure/Makefile-based build and feature detection path. · `configure.ac`
- [CMake](https://github.com/imagemagick/lzma/blob/main/CMakeLists.txt): build-system · Alternative build configuration for the same codebase. · `CMakeLists.txt`
- [Portability](https://github.com/imagemagick/lzma/tree/main/src/common): compat layer · Shared OS/thread/locale/process helpers used across library and tools. · `src/common`
- [Getopt](https://github.com/imagemagick/lzma/tree/main/lib): cli parsing · Bundled option parsing implementation for portable command-line handling. · `lib`
- [Windows](https://github.com/imagemagick/lzma/tree/main/windows): platform packaging · Windows and DOS integration assets, manifests, and build notes. · `windows`

### liblzma

Reusable compression library and public API.

- [Public API](https://github.com/imagemagick/lzma/tree/main/src/liblzma/api/lzma): headers · External liblzma headers for compression, container, filter, and check APIs. · `src/liblzma/api/lzma`
- [Container Core](https://github.com/imagemagick/lzma/tree/main/src/liblzma/common): stream/block · Stream, block, index, and format orchestration for .xz and legacy formats. · `src/liblzma/common`
- [LZMA Codec](https://github.com/imagemagick/lzma/tree/main/src/liblzma/lzma): codec · LZMA/LZMA2 encoder and decoder implementation. · `src/liblzma/lzma`
- [Filters](https://github.com/imagemagick/lzma/tree/main/src/liblzma/delta): codec filters · Delta and BCJ/simple filters that preprocess data before coding. · `src/liblzma/delta`
- [Entropy Core](https://github.com/imagemagick/lzma/tree/main/src/liblzma/rangecoder): range coder · Range-coding and pricing primitives used by the LZMA codec. · `src/liblzma/rangecoder`
- [Integrity Checks](https://github.com/imagemagick/lzma/tree/main/src/liblzma/check): checksums · CRC32, CRC64, and SHA-256 verification support. · `src/liblzma/check`

### Command-line tools

User-facing executables layered on top of liblzma.

- [xz](https://github.com/imagemagick/lzma/tree/main/src/xz): main CLI · Full-featured compression/decompression/listing tool built on liblzma. · `src/xz`
- [xzdec](https://github.com/imagemagick/lzma/tree/main/src/xzdec): decompress CLI · Small decompression-only utility. · `src/xzdec`
- [lzmainfo](https://github.com/imagemagick/lzma/tree/main/src/lzmainfo): metadata CLI · Reports metadata for .lzma files. · `src/lzmainfo`
- [Helper scripts](https://github.com/imagemagick/lzma/tree/main/src/scripts): shell tooling · User-facing wrapper scripts and generated manpage sources. · `src/scripts`

### Quality & Docs

Tests, fuzzing, examples, and documentation.

- [Tests](https://github.com/imagemagick/lzma/tree/main/tests): verification · Unit, integration, format-fixture, and platform regression tests. · `tests`
- [Fuzzing](https://github.com/imagemagick/lzma/tree/main/tests/ossfuzz): harnesses · OSS-Fuzz targets for stream and encoder/decoder paths. · `tests/ossfuzz`
- [Docs](https://github.com/imagemagick/lzma/tree/main/doc): documentation · Examples, manpages, and format documentation. · `doc`
- [Translations](https://github.com/imagemagick/lzma/tree/main/po): localization · gettext and po4a translation catalogs for messages and manpages. · `po`

## Connections

- Autotools → Portability: configures · Feature detection and portability settings feed shared compatibility code.
- CMake → Portability: configures · CMake builds the same portability layer and platform abstractions.
- Autotools → Public API: generates · Build system produces versioned headers and package metadata.
- Autotools → xz: builds · Compiles the main executable from CLI orchestration code.
- Autotools → xzdec: builds · Compiles the decompression-only utility.
- Autotools → lzmainfo: builds · Compiles the metadata reporting tool.
- Autotools → Container Core: builds · Builds the core library container and stream logic.
- Autotools → LZMA Codec: builds · Builds LZMA codec internals.
- Autotools → Filters: builds · Builds filter implementations.
- Getopt → xz: parses args · Portable option parsing for the main CLI.
- Getopt → xzdec: parses args · Portable option parsing for the decompression tool.
- Getopt → lzmainfo: parses args · Portable option parsing for the info tool.
- Portability → xz: supports · Threads, signals, sandboxing, memory, and system helpers.
- Portability → xzdec: supports · Shared runtime helpers for the smaller CLI.
- Portability → Container Core: supports · Cross-platform helpers used by stream and index handling.
- Public API → Container Core: exposes · Public API fronts the container and stream machinery.
- Container Core → LZMA Codec: orchestrates · Container/block handling invokes codec-specific LZMA logic.
- Container Core → Filters: chains · Filter setup and reconstruction flow through the common layer.
- LZMA Codec → Entropy Core: uses · LZMA encoder/decoder depends on range-coding primitives.
- LZMA Codec → Integrity Checks: verifies · Stream and block processing integrates integrity checks.
- Filters → Container Core: feeds · Delta and simple filters are assembled into the stream pipeline.
- xz → Public API: calls · Compression, decompression, listing, and testing are implemented via liblzma.
- xzdec → Public API: calls · Minimal decoder wrapper over the library API.
- lzmainfo → Public API: calls · Reads file metadata through liblzma container helpers.
- xz → Portability: uses · Optionally uses sandbox, signals, hardware and I/O helpers.
- Helper scripts → Docs: generates · Shell wrappers and manpage sources support user-facing documentation.
- Tests → Public API: validates · Unit and fixture tests exercise public APIs and format parsing.
- Tests → xz: validates · Integration tests cover the main CLI behavior.
- Fuzzing → Container Core: fuzzes · Fuzz targets focus on stream and decoder entry points.
- Fuzzing → LZMA Codec: fuzzes · Encoder/decoder harnesses stress codec internals.
- Docs → xz: documents · Manpages and examples describe CLI behavior and usage.
- Docs → Public API: documents · Examples show how to use the reusable compression library.
- Translations → xz: localizes · Translations cover CLI messages.
- Translations → Docs: localizes · po4a catalogs translate manpages and related documentation.
- Windows → xz: packages · Windows assets and manifests support the main executable.
- Windows → Public API: packages · Windows packaging also ships the library and its ABI metadata.
