deflateinit, deflate, deflatezlib, deflateblock, deflatezlibblock, inflateinit, inflate, inflatezlib, inflateblock, inflatezlibblock, flateerr, mkcrctab, blockcrc, adler32 – deflate compression


#include <u.h>
#include <libc.h>
#include <flate.h>

int deflateinit(void)

int deflate(void *wr, int (*w)(void*,void*,int),
void *rr, int (*r)(void*,void*,int),
int level, int debug)

int deflatezlib(void *wr, int (*w)(void*,void*,int),
void *rr, int (*r)(void*,void*,int),
int level, int debug)

int deflateblock(uchar *dst, int dsize,
uchar *src, int ssize,
int level, int debug)

int deflatezlibblock(uchar *dst, int dsize,
uchar *src, int ssize,
int level, int debug)

int inflateinit(void)

int inflate(void *wr, int (*w)(void*, void*, int),
void *getr, int (*get)(void*))

int inflatezlib(void *wr, int (*w)(void*, void*, int),
void *getr, int (*get)(void*))

int inflateblock(uchar *dst, int dsize,
uchar *src, int ssize)

int inflatezlibblock(uchar *dst, int dsize,
uchar *src, int ssize)

char *flateerr(int error)

ulong *mkcrctab(ulong poly)

ulong blockcrc(ulong *tab, ulong crc, void *buf, int n)

ulong adler32(ulong adler, void *buf, int n)


These routines compress and decompress data using the deflate compression algorithm, which is used for most gzip, zip, and zlib files.

Deflate compresses input data retrieved by calls to r with arguments rr, an input buffer, and a count of bytes to read. R should return the number of bytes read; end of input is signaled by returning zero, an input error by returning a negative number. The compressed output is written to w with arguments wr, the output data, and the number of bytes to write. W should return the number of bytes written; writing fewer than the requested number of bytes is an error. Level indicates the amount of computation deflate should do while compressing the data. Higher levels usually take more time and produce smaller outputs. Valid values are 1 to 9, inclusive; 6 is a good compromise. If debug is non-zero, cryptic debugging information is produced on standard error.

Inflate reverses the process, converting compressed data into uncompressed output. Input is retrieved one byte at a time by calling get with the argument getr. End of input is signaled by returning a negative value. The uncompressed output is written to w, which has the same interface as for deflate.

Deflateblock and inflateblock operate on blocks of memory but are otherwise similar to deflate and inflate.

The zlib functions are similar, but operate on files with a zlib header and trailer.

Deflateinit or inflateinit must be called once before any call to the corresponding routines.

If the above routines fail, they return a negative number indicating the problem. The possible values are FlateNoMem, FlateInputFail, FlateOutputFail, FlateCorrupted, and FlateInternal. Flateerr converts the number into a printable message. FlateOk is defined to be zero, the successful return value for deflateinit, deflate, deflatezlib, inflateinit, inflate, and inflatezlib. The block functions return the number of bytes produced when they succeed.

Mkcrctab allocates (using malloc(2)), initializes, and returns a table for rapid computation of 32 bit CRC values using the polynomial poly. Blockcrc uses tab, a table returned by mkcrctab, to update crc for the n bytes of data in buf, and returns the new value. Crc should initially be zero. Blockcrc pre-conditions and post-conditions crc by ones complementation.

Adler32 updates the Adler 32-bit checksum of the n bytes of data in buf. The initial value of adler (that is, its value after seeing zero bytes) should be 1.