Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
build:
name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- name: linux-gcc
os: ubuntu-latest
compiler: gcc
flags: ""
- name: linux-clang
os: ubuntu-latest
compiler: clang
flags: ""
- name: macos-clang
os: macos-latest
compiler: clang
flags: ""
- name: windows-msvc
os: windows-latest
compiler: ""
flags: ""

steps:
- uses: actions/checkout@v4

- name: Configure
run: |
if [ "${{ matrix.compiler }}" != "" ]; then
export CC=${{ matrix.compiler }}
fi
cmake -B build -DCMAKE_BUILD_TYPE=Release -DLEAFLITTER_WERROR=ON ${{ matrix.flags }}
shell: bash

- name: Build
run: cmake --build build --config Release

- name: Test
run: ctest --test-dir build -C Release --output-on-failure

- name: Install
run: |
cmake --install build --config Release --prefix install-dir
test -f install-dir/include/lf_stream.h
test -f install-dir/lib/libleaflitter.a -o -f install-dir/lib/leaflitter.lib
shell: bash

sanitize:
name: sanitize
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Configure
run: >
cmake -B build
-DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_COMPILER=clang
-DLEAFLITTER_WERROR=ON
-DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer"

- name: Build
run: cmake --build build

- name: Test
run: ctest --test-dir build --output-on-failure
env:
ASAN_OPTIONS: detect_leaks=1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ build/
build-*/
out/
cmake-build-*/
install*/
CMakeUserPresets.json

# Compiled objects and libraries
*.o
Expand Down
106 changes: 106 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.16)

project(leaflitter
VERSION 1.1.0
DESCRIPTION "A lossless byte-stream compressor that uses LZ77 matching and canonical Huffman coding"
LANGUAGES C)

include(CTest)

option(LEAFLITTER_BUILD_TESTS "Build the unit tests" ON)
option(LEAFLITTER_BUILD_TOOLS "Build the command line tools" ON)
option(LEAFLITTER_BUILD_SHARED "Build a shared library in addition to the static library" OFF)
option(LEAFLITTER_WERROR "Treat compiler warnings as errors" OFF)

set(LEAFLITTER_SOURCES
src/lf_adler32.c
src/lf_bitio.c
src/lf_block.c
src/lf_buf.c
src/lf_common.c
src/lf_huffman.c
src/lf_match.c
src/lf_stream.c)

add_library(leaflitter STATIC ${LEAFLITTER_SOURCES})
add_library(leaflitter::leaflitter ALIAS leaflitter)

target_include_directories(leaflitter
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)

target_compile_features(leaflitter PUBLIC c_std_11)

if(LEAFLITTER_BUILD_SHARED)
add_library(leaflitter_shared SHARED ${LEAFLITTER_SOURCES})
target_include_directories(leaflitter_shared
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)
target_compile_features(leaflitter_shared PUBLIC c_std_11)
endif()

if(MSVC)
target_compile_options(leaflitter PRIVATE /W4)
if(LEAFLITTER_WERROR)
target_compile_options(leaflitter PRIVATE /WX)
endif()
if(LEAFLITTER_BUILD_SHARED)
target_compile_options(leaflitter_shared PRIVATE /W4)
endif()
else()
target_compile_options(leaflitter PRIVATE -Wall -Wextra -Wpedantic)
if(LEAFLITTER_WERROR)
target_compile_options(leaflitter PRIVATE -Werror)
endif()
if(LEAFLITTER_BUILD_SHARED)
target_compile_options(leaflitter_shared PRIVATE -Wall -Wextra -Wpedantic)
endif()
endif()

if(LEAFLITTER_BUILD_TESTS AND BUILD_TESTING)
add_subdirectory(tests)
endif()

if(LEAFLITTER_BUILD_TOOLS)
add_subdirectory(tools)
add_subdirectory(samples)
endif()

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

install(TARGETS leaflitter
EXPORT leaflitterTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(LEAFLITTER_BUILD_SHARED)
install(TARGETS leaflitter_shared
EXPORT leaflitterTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

install(DIRECTORY src/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.h")

install(EXPORT leaflitterTargets
FILE leaflitterTargets.cmake
NAMESPACE leaflitter::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/leaflitter)

configure_package_config_file(
cmake/leaflitterConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/leaflitterConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/leaflitter)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/leaflitterConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/leaflitterConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/leaflitterConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/leaflitter)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 leaflitter contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# leaflitter

leaflitter compresses byte streams with LZ77 matching and canonical Huffman
coding. The design follows DEFLATE but stays simpler. The library is small,
portable, and free of external dependencies.

## Features

- LZ77 hash-chain match finder with a sliding window
- Canonical Huffman coding with a 15-bit length limit
- Stored and Huffman block types
- Adler-32 integrity check on decompression
- C11 API and a command line tool
- Deterministic output for a given input
- No third-party runtime dependencies

## Build

leaflitter uses CMake. Configure, build, and test from the project root.

```
cmake -B build
cmake --build build
ctest --test-dir build
```

Install the library and headers with the following command.

```
cmake --install build
```

The tests use the vendored Unity framework. Set `BUILD_TESTING=OFF` to skip
the tests. Set `LEAFLITTER_BUILD_TOOLS=OFF` to skip the tool and the sample.

## Use the library

Compress and decompress a byte buffer in memory.

```c
#include "lf_buf.h"
#include "lf_stream.h"

uint8_t input[] = "hello hello hello";
lf_buf packed;
lf_buf plain;
lf_result r;

lf_buf_init(&packed);
lf_buf_init(&plain);

r = lf_compress(NULL, input, sizeof(input) - 1, &packed);
if (r != LF_OK) {
return r;
}

r = lf_decompress(packed.data, packed.len, &plain);
if (r != LF_OK) {
return r;
}
```

Pass `NULL` for options to use the defaults. Build a custom `lf_options`
struct to change the block size, window size, or match chain length. Call
`lf_options_default` to fill that struct first.

Compression accepts inputs up to 4 GiB. Decompression accepts any valid
stream. Use `lf_compress_bound` to size an output buffer before compression.

## Use the command line tool

The tool compresses and decompresses files.

```
leaflitter c <in> <out>
leaflitter d <in> <out>
leaflitter i <in>
```

Command `c` compresses `<in>` to `<out>`. Command `d` restores the original
bytes. Command `i` prints the stream header.

## Format

The container format is documented in `docs/FORMAT.md`. The header stores the
original size, an Adler-32 checksum, and the window size. A stream ends with
a final block flag.

The format is new. It is not compatible with DEFLATE or gzip.

## Roadmap

See `docs/ROADMAP.md` for the completed work and the planned work.

## Limitations

- Compression inputs are limited to 4 GiB.
- The code-length tree is fixed, not adaptive.
- The encoder searches one hash chain per position.
- Compression is single-threaded.

## Testing

The tests use Unity and run through CTest. Every module has a test file.
Round-trip tests cover small, large, and random inputs. Golden vectors lock
the byte format. Corruption tests check that bad streams fail cleanly.

## License

This project is licensed under the MIT License. See `LICENSE` for details.
5 changes: 5 additions & 0 deletions cmake/leaflitterConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/leaflitterTargets.cmake")

check_required_components(leaflitter)
Loading
Loading