sargon.lz77

Components that can compress and expand ranges.

Description: Compression is useful for storage and transmission data formats. This compresses using a variant of the LZ77 compression algorithm. compress() and expand() are meant to be a matched set. Users should not depend on the particular data format.

Members

Classes

CompressException
class CompressException

Exception thrown on errors in compression functions, likely caused by corrupted data.

Functions

compress
auto compress(R src)

Component that returns a Range that will compress src using LZ77 compression.

copy
ubyte[] copy(R src, ubyte[] dst)
Undocumented in source. Be warned that the author may not have intended to support it.
expand
auto expand(R src)

Component to expand compressed result of LZ77 Compress.

maxCompressedSize
size_t maxCompressedSize(size_t length)

Compute the max size of a buffer needed to hold the compressed result.

test
void test()
Undocumented in source. Be warned that the author may not have intended to support it.

Structs

Adapter
struct Adapter
Undocumented in source.

Examples

This program takes a filename as an argument, compresses it, expands it, and verifies that the result matches the original contents.

int main(string args[])
{
    import std.stdio;
    import std.algorithm;
    import std.file;
    import std.compression.lz77;

    string filename;
    if (args.length < 2)
    {
        printf("need filename argument\n");
        return 1;
    }
    else
        filename = args[1];

    ubyte[] si = cast(ubyte[])std.file.read(filename);

    // Compress
    auto di = new ubyte[maxCompressedSize(si.length)];
    auto result = si.compress().copy(di);

    di = di[0..$ - result.length];

    writefln("Compression done, srclen = %s compressed = %s",
        si.length, di.length);

    // Decompress
    ubyte[] si2 = new ubyte[si.length];
    result = di.expand().copy(si2);
    assert(result.length == 0);

    writefln("Decompression done, dilen = %s decompressed = %s",
        di.length, si2.length);

    if (si != si2)
    {
        writeln("Buffers don't match");
        assert(0);
    }

    return 0;
}

References: Wikipedia

Meta

Source

See Source File
$(SARGONSRC src/sargon/_lz77.d)