Skip to content

Instantly share code, notes, and snippets.

@coderforlife
Created July 7, 2018 15:00
Show Gist options
  • Save coderforlife/72aa30bc3f7f57d37b7808fe9dac7709 to your computer and use it in GitHub Desktop.
Save coderforlife/72aa30bc3f7f57d37b7808fe9dac7709 to your computer and use it in GitHub Desktop.
#include <stdio.h> // for FILE, fread, fwrite, feof
#include <assert.h> // for assertion checks
#include <lznt1.h> // mscomp LZNT1 header
#define BUF_SIZE 65536
/* TODO: probably use a return value for error handling or something */
void inflate(FILE *source, FILE *dest)
{
MSCompStatus status;
bool finish;
size_t have;
mscomp_stream strm;
byte in[BUF_SIZE];
byte out[BUF_SIZE];
status = lznt1_inflate_init(&strm);
if (status != MSCOMP_OK) { /* TODO: deal with error in status */ }
// decompress until end of file
do {
strm.in_avail = fread(in, 1, BUF_SIZE, source);
if (ferror(source))
{
lznt1_inflate_end(&strm);
/* TODO: deal with error reading file */
}
finish = feof(source) != 0;
strm.in = in;
// run inflate() on input until output buffer not full, finish compression if all of source has been read in
do
{
strm.out_avail = BUF_SIZE;
strm.out = out;
status = lznt1_inflate(&strm);
if (status < MSCOMP_OK)
{
lznt1_inflate_end(&strm);
/* TODO: deal with error in status */
}
have = BUF_SIZE - strm.out_avail;
if (fwrite(out, 1, have, dest) != have || ferror(dest))
{
lznt1_inflate_end(&strm);
/* TODO: deal with error writing file */
}
} while (strm.out_avail == 0);
assert(strm.in_avail == 0); // all input will be used
// done when last data in file processed
} while (!finish);
assert(status == MSCOMP_STREAM_END || status == MSCOMP_POSSIBLE_STREAM_END); // stream will be complete
// clean up and return
status = lznt1_inflate_end(&strm);
if (status != MSCOMP_OK) { /* TODO: deal with error in status */ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment