Python bindings for libhuffman
Project description
libhuffman - The Huffman coding library
The Huffman library is a simple, pure C library for encoding and decoding data using a frequency-sorted binary tree. The implementation of this library is pretty straightforward, additional information regarding Huffman coding could be gained from the Wikipedia.
Installation
The build mechanism of the library is based on the CMake tool, so you could easily install it on your distribution in the following way:
$ sudo cmake install
By default the command above install the library into /usr/local/lib
and all
required headers into /usr/local/include
. The installation process is organized
using CMake. Just create a new directory build
and generate required makefiles:
$ mkdir -p build
$ cmake ..
After that run the install
target:
$ make install
Usage
Encoding
To encode the data, use either a file stream huf_fdopen
or huf_memopen
to use
an in-memory stream. Consider the following example, where the input is a memory
stream and output of the encoder is also memory buffer of 1MiB size.
void *bufin, *bufout = NULL;
huf_read_writer_t *input, *output = NULL;
// Allocate the necessary memory.
huf_memopen(&input, &bufin, HUF_1MIB_BUFFER);
huf_memopen(&output, &bufout, HUF_1MIB_BUFFER);
// Write the data for encoding to the input.
size_t input_len = 10;
input->write(input->stream, "0123456789", input_len);
Create a configuration used to encode the input string using Huffman algorithm:
huf_config_t config = {
.reader = input,
.length = input_len,
.writer = output,
.blocksize = HUF_64KIB_BUFFER,
};
huf_error_t err = huf_encode(&config);
printf("%s\n", huf_error_string(err));
reader
- input ready for the encoding.writer
- output for the encoded data.length
- length of the data in bytes to encode.blocksize
- the length of each chunk in bytes (instead of reading the file twicelibhuffman
reads and encodes data by blocks).reader_buffer_size
- this is opaque reader buffer size in bytes, if the buffer size is set to zero, all reads will be unbuffered.writer_buffer_size
- this is opaque writer buffer size ib bytes, if the buffer size is set to zero, all writes will be unbuffered.
After the encoding, the output memory buffer could be automatically scaled to fit all necessary encoded bytes. To retrieve a new length of the buffer, use the following:
size_t out_size = 0;
huf_memlen(output, &out_size);
// The data is accessible through the `bufout` variable or using `read` function:
uint8_t result[10] = {0};
size_t result_len = 10;
// result_len is inout parameter, and will contain the length of encoding
// after the reading from the stream.
output->read(output->stream, result, &result_len);
Decoding
Decoding is similar to the encoding, except that reader attribute of the configuration should contain the data used to decode:
input->write(input->stream, decoding, decoding_len);
huf_config_t config = {
.reader = input,
.length = input_len,
.writer = output,
.blockize = HUF_64KIB_BUFFER,
};
// After the decoding the original data will be writter to the `output`.
huf_decode(&config);
Resource Deallocation
Once the processing of the encoding is completed, consider freeing the allocated memory:
// This does not free underlying buffer, call free for the buffer.
huf_memclose(&mem_out);
free(buf);
For more examples, please, refer to the tests
directory.
Python Bindings
Python bindings for libhuffman
library are distributed as PyPI package, to install
that package, execute the following command:
pip install huffmanfile
You can use the libhuffman
for performant compression and decompression of Huffman
encoding. The interface of the Python library is similar to the interface of the
bz2
and
lzma
packages from Python's standard
library.
Examples of usage
Reading in a compressed file:
import huffmanfile
with huffmanfile.open("file.hm") as f:
file_content = f.read()
Creating a compressed file:
import huffmanfile
data = b"Insert Data Here"
with huffmanfile.open("file.hm", "w") as f:
f.write(data)
Compressing data in memory:
import huffmanfile
data_in = b"Insert Data Here"
data_out = huffmanfile.compress(data_in)
Incremental compression:
import huffmanfile
hfc = huffmanfile.HuffmanCompressor()
out1 = hfc.compress(b"Some data\n")
out2 = hfc.compress(b"Another piece of data\n")
out3 = hfc.compress(b"Even more data\n")
out4 = hfc.flush()
# Concatenate all the partial results:
result = b"".join([out1, out2, out3, out4])
Note, random data tends to compress poorly, while ordered, repetitive data usually yields a high compression ratio.
License
The Huffman library is distributed under MIT license, therefore you are free to do with code whatever you want. See the LICENSE file for full license text.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file huffmanfile-1.0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 95.7 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77b3833beefe3231e9167ab70858ebaf265f4bd07a1b03adc8fd30f13a612636 |
|
MD5 | 4bc38b9712889febe877764cba41b8e8 |
|
BLAKE2b-256 | e09b5eb2bee4915b8b9bb9b7f0633e18e9fed893a7fe7459c689b7e50b85218b |
File details
Details for the file huffmanfile-1.0.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 87.5 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94922e879a83c434ddd494d494087de18fe480d15ba71a62cb84e6ad86a633c7 |
|
MD5 | e42fb035d5920883bdc9d19ce7fcd10a |
|
BLAKE2b-256 | ae9a0c84ff2f208db774540be40063b0ad7236bbcdf42554da1a80354b5e57f0 |
File details
Details for the file huffmanfile-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b3434ca75064bd447b4a5b5053a133d30608bf0dac0d0f4d0420b3ecb86324c |
|
MD5 | d04041055eb7636445d5ddfaf40f3365 |
|
BLAKE2b-256 | c407beb3e7c01451d708ca45f31cad33a3f6e5c774e76aade122d81a50aa6961 |
File details
Details for the file huffmanfile-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 29.0 kB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bafce8e57e781d826df56ae807b09a872353b768bbe128629acb5477d0adf82 |
|
MD5 | ca7ff0173e29dc5cd6220bbffdbfa7e0 |
|
BLAKE2b-256 | 6d59a28eb250580bdc1797a97ea42a75d336815281ac07e2777c5a3011313a55 |
File details
Details for the file huffmanfile-1.0.3-cp311-cp311-macosx_10_9_universal2.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp311-cp311-macosx_10_9_universal2.whl
- Upload date:
- Size: 48.4 kB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c8df1da761d80c30ca8e2362bf92579a6f961f13bf12334720284f6fbcbc897 |
|
MD5 | 372e0b5377d7075dd14f0bb3724b7bdb |
|
BLAKE2b-256 | 173a7307af37916ee68d587390f82f55e329ecf0395c9a76de7a4316b94094e0 |
File details
Details for the file huffmanfile-1.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 95.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 724b1a5c683dfd8ee5a63a94a50a1113c0c7b3dd458b399621a1c201ee4f6693 |
|
MD5 | 6c92397f7164bde2d181e4b21bcd6d6d |
|
BLAKE2b-256 | 69cbfb6c1c5b8bcd22b9aa7a534aa028bb92d33bd7c23dd9045ea7239335969e |
File details
Details for the file huffmanfile-1.0.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 87.5 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d18942ce238cbc65105732558157dfca4c841002898276f3b7f138bf1c6c8384 |
|
MD5 | 622128f6d15fcd78b1a1a6a48f8ee8a7 |
|
BLAKE2b-256 | e5427d23184e4fd46b5c76caba559cb5b54bcab8cf692f466524ef2d56b9486d |
File details
Details for the file huffmanfile-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e21c51da6886c0da05752c7b740e2df5710656b1becbf2e62f18464cb745211b |
|
MD5 | 17a07d7a37041453c0085c9bc453e685 |
|
BLAKE2b-256 | 5ec45624837e064d62a9a157d2d2251050dab391d20103341a13668c93076495 |
File details
Details for the file huffmanfile-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 29.0 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 379a887adcb29e4ceaaaed45ab12bfaf52817342621630d6296565145132ef60 |
|
MD5 | da690d629af9aac119854ff1a1fd9bf2 |
|
BLAKE2b-256 | 0f901d8cf6ed8a450896b76b5e37731a0749932deac4a1b0757472dfd1060ddc |
File details
Details for the file huffmanfile-1.0.3-cp310-cp310-macosx_10_9_universal2.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp310-cp310-macosx_10_9_universal2.whl
- Upload date:
- Size: 48.4 kB
- Tags: CPython 3.10, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8790086ac85c350ea9d4fb4e2cf744b3f1e40448f98749977454471c68a4f49 |
|
MD5 | 2929c524c46c3518dcf80298bb542d0a |
|
BLAKE2b-256 | 5da0d1fcca16364ab29573da5364a0ee30fe44add887fdfea42351b0aedffa37 |
File details
Details for the file huffmanfile-1.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 95.7 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a752896124d86628e0e00e6d1db6e9bc0b339b63d7380b5108c07635278580b |
|
MD5 | db70c50666a51cd4b11821964d43f80e |
|
BLAKE2b-256 | 41050ef65a2f01246d61ed05dee01866b2adfc84e6ed3bd7d90253cca001558b |
File details
Details for the file huffmanfile-1.0.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 87.5 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b6e425c71b28e1ed12fcde1bde59a264c1a423553365d50b09ac95f3c701dc3 |
|
MD5 | 5943c5304e75a1191febe5c10e2cc7ce |
|
BLAKE2b-256 | 833707d03222d6e233f3fa76bce14450a95399554ac176c88c71d37b372d0289 |
File details
Details for the file huffmanfile-1.0.3-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 502bbba1755055c61da4d5c08cf12a02bee08f039841ec4d81f831706ca6d185 |
|
MD5 | 7136c1ec9a4b2cd8dea9a0fd3839cd69 |
|
BLAKE2b-256 | 9ca2b40e10f0be06c34fd49eda0bc6a7a818b8d8bf6657bf111870afc776d265 |
File details
Details for the file huffmanfile-1.0.3-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 29.0 kB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 593e1c7919daf24cee5a2eb3e92bfe40a19aa254620b7ba9218ecd26eaaaf7bc |
|
MD5 | 0625b7d42b809c9af38b33fad6757155 |
|
BLAKE2b-256 | 6dcafada30bb38c13502eb3cb253c0f3806ef8c68190016b12f519d33491b0b0 |
File details
Details for the file huffmanfile-1.0.3-cp39-cp39-macosx_10_9_universal2.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp39-cp39-macosx_10_9_universal2.whl
- Upload date:
- Size: 48.4 kB
- Tags: CPython 3.9, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d168d25ff0462bedb387e590591fc7b72691f0b810f299d49b7cc33565d0a3f |
|
MD5 | acc62f94b505ef438ab92aea5694aeb7 |
|
BLAKE2b-256 | d50643ffe664e1f5d3315de8521ba1054649183b8f3f254bb9dc9141724a3834 |
File details
Details for the file huffmanfile-1.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 95.8 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ab4cad1c80bdc6d5b05da6f19abec05d42db753ff43acfef09ebeb5588a1fda |
|
MD5 | eeb9d59e2295695a77234345d0b3cfa9 |
|
BLAKE2b-256 | efc9186eda5307a910a76354c52b54200051871ab653c8537a9153c511fe6e8a |
File details
Details for the file huffmanfile-1.0.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 87.6 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a40436771956a17fecba339b2c27ebf0a4eae1a65bb72941131d236aa4fb4ef5 |
|
MD5 | e23c64e7298e982f4eafb3b86c523dcb |
|
BLAKE2b-256 | 01afb1dfdc3cedb42f172ce1feae3bf78f455bd220f2005980d37f416fea8327 |
File details
Details for the file huffmanfile-1.0.3-cp38-cp38-macosx_11_0_arm64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 170776db16572e1099bde96e15063db7c35e94cfee4189561da358c99d4b0d32 |
|
MD5 | 28355e4b086b352838dde3d1cad2d169 |
|
BLAKE2b-256 | 70c8c3e00d9b5d04348906e4a6e45eca1a7f0469541f215f151f4de1163d6d08 |
File details
Details for the file huffmanfile-1.0.3-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp38-cp38-macosx_10_9_x86_64.whl
- Upload date:
- Size: 29.0 kB
- Tags: CPython 3.8, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb43a2b2f8f427227815cd835b7a67b78f0de538f41edb7bae7488611795be37 |
|
MD5 | 9632d0471abe74057153afb770212796 |
|
BLAKE2b-256 | fea1bb1f9b513093090e183968dd81c2da690937be4cbde3ab581127517d4565 |
File details
Details for the file huffmanfile-1.0.3-cp38-cp38-macosx_10_9_universal2.whl
.
File metadata
- Download URL: huffmanfile-1.0.3-cp38-cp38-macosx_10_9_universal2.whl
- Upload date:
- Size: 48.4 kB
- Tags: CPython 3.8, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d51e3171747870cf5144adfcd9f6147c93824dac83e35d2aedc8f523e8c25bf7 |
|
MD5 | 7e0ceaa88d73ce48935ce33b45857b94 |
|
BLAKE2b-256 | fbdedbad40f8b1adb74aef641817af54a416b7a1c8c381e3140b708057ebe6d4 |