std.compression.lz77 - D Programming Language - Digital Mars

D Programming Language 2.0

# std.compression.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.

Example:
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:

https:
//en.wikipedia.org/wiki/LZ77_and_LZ78

License:
Boost License 1.0.

Authors:
Walter Bright

Source:
std/compression/lz77.d

size_t maxCompressedSize(size_t length);
  Compute the max size of a buffer needed to hold the compressed result.

  Parameters:
  length number of bytes of uncompressed data

  Returns:
  max size of compressed buffer

class CompressException: object.Exception;
  Exception thrown on errors in compression functions, likely caused by corrupted data.

auto compress(R)(R src);
  Component that returns a Range that will compress src using LZ77 compression.

  Description:
  Compresses input of arbitrarily large size.

  compress() does not allocate memory, although it uses a little over 8Kb of stack if src is not an array.

  Parameters:
  src An InputRange of ubytes. If it is an array, it will go faster and will use much less stack.

  Returns:
  InputRange

auto expand(R)(R src);
  Component to expand compressed result of LZ77 Compress.

  Description:
  Does not allocate memory. The expand operation is quite a bit faster than the corresponding compress.

  Parameters:
  src An InputRange over data generated by compress.

  Returns:
  An InputRange which provides the expanded data.

Copyright Digital Mars 2013. | Page generated by Ddoc.