2
* Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de>
4
* Redistribution and use in source and binary forms, with or without modifica-
5
* tion, are permitted provided that the following conditions are met:
7
* 1. Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* 3. The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26
* OF THE POSSIBILITY OF SUCH DAMAGE.
28
* Alternatively, the contents of this file may be used under the terms of
29
* the GNU General Public License version 2 (the "GPL"), in which case the
30
* provisions of the GPL are applicable instead of the above. If you wish to
31
* allow the use of your version of this file only under the terms of the
32
* GPL and not to allow others to use your version of this file under the
33
* BSD license, indicate your decision by deleting the provisions above and
34
* replace them with the notice and other provisions required by the GPL. If
35
* you do not delete the provisions above, a recipient may use your version
36
* of this file under either the BSD or the GPL.
49
typedef unsigned char u8;
55
"lzf, a very lightweight compression/decompression filter\n"
56
"written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n"
57
"http://liblzf.plan9.de/\n"
59
"USAGE: lzf -c [-b blocksize] | -d\n"
62
" -b specify the blocksize (default 64k-1)\n"
70
* Anatomy: an lzf file consists of any number of blocks in the following format:
72
* "ZV\0" 2-byte-usize <uncompressed data>
73
* "ZV\1" 2-byte-csize 2-byte-usize <compressed data>
74
* "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI)
78
static void compress (unsigned int blocksize)
90
us = fread (buff1, 1, blocksize, stdin);
96
else if (!feof (stdin))
103
cs = lzf_compress (buff1, us, buff2, us - 4);
109
header[4] = cs & 0xff;
111
header[6] = us & 0xff;
113
fwrite (header, 3+2+2, 1, stdout);
114
fwrite (buff2, cs, 1, stdout);
120
header[4] = us & 0xff;
122
fwrite (header, 3+2, 1, stdout);
123
fwrite (buff1, us, 1, stdout);
125
} while (!feof (stdin));
128
static void decompress (void)
137
if (fread (header, 3+2, 1, stdin) != 1)
143
perror ("decompress");
148
if (header[0] != 'Z' || header[1] != 'V')
150
fprintf (stderr, "decompress: invalid stream - no magic number found\n");
154
cs = (header[3] << 8) | header[4];
158
if (fread (header+3+2, 2, 1, stdin) != 1)
160
perror ("decompress");
164
us = (header[5] << 8) | header[6];
166
if (fread (buff1, cs, 1, stdin) != 1)
168
perror ("decompress");
172
if (lzf_decompress (buff1, cs, buff2, us) != us)
174
fprintf (stderr, "decompress: invalid stream - data corrupted\n");
178
fwrite (buff2, us, 1, stdout);
180
else if (header[2] == 0)
182
if (fread (buff2, cs, 1, stdin) != 1)
184
perror ("decompress");
188
fwrite (buff2, cs, 1, stdout);
192
fprintf (stderr, "decompress: invalid stream - unknown block type\n");
199
main (int argc, char *argv[])
202
unsigned int blocksize = 64*1024-1;
203
enum { m_compress, m_decompress } mode = m_compress;
205
if (!strcmp (argv[0] + strlen (argv[0] - 5), "unlzf"))
208
while ((c = getopt (argc, argv, "cdb:h")) != -1)
220
blocksize = atol (optarg);
227
fprintf (stderr, "required argument missing, use -h\n");
231
fprintf (stderr, "unknown option, use -h\n");
238
if (mode == m_compress)
239
compress (blocksize);
240
else if (mode == m_decompress)