|
| 1 | +/* blast.c |
| 2 | + * Copyright (C) 2003, 2012, 2013 Mark Adler |
| 3 | + * For conditions of distribution and use, see copyright notice in blast.h |
| 4 | + * version 1.3, 24 Aug 2013 |
| 5 | + * |
| 6 | + * blast.c decompresses data compressed by the PKWare Compression Library. |
| 7 | + * This function provides functionality similar to the explode() function of |
| 8 | + * the PKWare library, hence the name "blast". |
| 9 | + * |
| 10 | + * This decompressor is based on the excellent format description provided by |
| 11 | + * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the |
| 12 | + * example Ben provided in the post is incorrect. The distance 110001 should |
| 13 | + * instead be 111000. When corrected, the example byte stream becomes: |
| 14 | + * |
| 15 | + * 00 04 82 24 25 8f 80 7f |
| 16 | + * |
| 17 | + * which decompresses to "AIAIAIAIAIAIA" (without the quotes). |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Change history: |
| 22 | + * |
| 23 | + * 1.0 12 Feb 2003 - First version |
| 24 | + * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data |
| 25 | + * 1.2 24 Oct 2012 - Add note about using binary mode in stdio |
| 26 | + * - Fix comparisons of differently signed integers |
| 27 | + * 1.3 24 Aug 2013 - Return unused input from blast() |
| 28 | + * - Fix test code to correctly report unused input |
| 29 | + * - Enable the provision of initial input to blast() |
| 30 | + */ |
| 31 | + |
| 32 | +/* Example of how to use blast() */ |
| 33 | +#include <stdio.h> |
| 34 | +#include <stdlib.h> |
| 35 | + |
| 36 | +#include <blast.h> |
| 37 | + |
| 38 | +#define CHUNK 16384 |
| 39 | + |
| 40 | +static unsigned inf(void *how, unsigned char **buf) |
| 41 | +{ |
| 42 | + static unsigned char hold[CHUNK]; |
| 43 | + |
| 44 | + *buf = hold; |
| 45 | + return fread(hold, 1, CHUNK, (FILE *)how); |
| 46 | +} |
| 47 | + |
| 48 | +static int outf(void *how, unsigned char *buf, unsigned len) |
| 49 | +{ |
| 50 | + return fwrite(buf, 1, len, (FILE *)how) != len; |
| 51 | +} |
| 52 | + |
| 53 | +/* Decompress a PKWare Compression Library stream from stdin to stdout */ |
| 54 | +int main(void) |
| 55 | +{ |
| 56 | + int ret; |
| 57 | + unsigned left; |
| 58 | + |
| 59 | + /* decompress to stdout */ |
| 60 | + left = 0; |
| 61 | + ret = blast(inf, stdin, outf, stdout, &left, NULL); |
| 62 | + if (ret != 0) |
| 63 | + fprintf(stderr, "blast error: %d\n", ret); |
| 64 | + |
| 65 | + /* count any leftover bytes */ |
| 66 | + while (getchar() != EOF) |
| 67 | + left++; |
| 68 | + if (left) |
| 69 | + fprintf(stderr, "blast warning: %u unused bytes of input\n", left); |
| 70 | + |
| 71 | + /* return blast() error code */ |
| 72 | + return ret; |
| 73 | +} |
0 commit comments