inflate.c is a single-file decompressor for DEFLATE data format as defined in RFC 1951 [1] and used in variety of other formats, protocols and applications.
inflate.c is optimized for code length and clarity, not for performance.
You can obtain its current sources via mercurial at [2].
The interface for the decompressor consists of one function:
int inflate(
void **dst, size_t *dstlen, size_t *dstused,
const void *src, size_t srclen, size_t *srcused
);
This function will decompress up to srclen bytes from src into the output buffer. The initial size and pointer to the output buffer are taken from *dstlen and *dst respectively.
If the output buffer is too short, it will be resized using realloc; in such cases *dstlen and *dst will be updated accordingly.
*dst can be NULL, in which case a fresh output buffer will be allocated.
inflate will return zero on success, and non-zero on failure.
If dstused is not NULL, the length of the decompressed data will be stored into the location referenced by dstused.
If srcused is not NULL, the number of input bytes consumed for decompression will be stored into the location referenced by srcused.
Here’s a short demo program:
#include "inflate.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
unsigned char code[] = {
0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xd7,
0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49,
0x51, 0xe4, 0x02, 0x00
};
size_t text_size, text_bytes;
void *text;
int errcode;
text = NULL;
errcode = inflate(
&text, &text_size, &text_bytes,
code, sizeof(code), NULL
);
if (errcode != 0) {
printf("Inflate failed. Why?\n");
return 1;
} else {
fwrite(text, 1, text_bytes, stdout);
return 0;
}
}
You can compile and run this program, for example like this:
$ cc inflate.c test.c -o test
$ ./test
Hello, world!