ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf.c
Revision: 1.4
Committed: Tue Apr 20 18:11:57 2004 UTC (20 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +10 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com>
3 *
4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
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.
13 *
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.
16 *
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.
27 *
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.
37 */
38
39 #include "config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <assert.h>
44
45 #include <unistd.h>
46 #include <getopt.h>
47
48 #include "lzf.h"
49
50 typedef unsigned char u8;
51
52 static void
53 usage (int ec)
54 {
55 fprintf (stderr, "\n"
56 "lzf, a very lightweight compression/decompression filter\n"
57 "written by Marc Lehmann <pcg@goof.com> You can find more info at\n"
58 "http://liblzf.plan9.de/\n"
59 "\n"
60 "USAGE: lzf -c [-b blocksize] | -d\n"
61 " -c compress\n"
62 " -d decompress\n"
63 " -b specify the blocksize (default 64k-1)\n"
64 "\n"
65 );
66
67 exit (ec);
68 }
69
70 /*
71 * Anatomy: an lzf file consists of any number of blocks in the following format:
72 *
73 * "ZV\0" 2-byte-usize <uncompressed data>
74 * "ZV\1" 2-byte-csize 2-byte-usize <compressed data>
75 * "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI)
76 *
77 */
78
79 static void compress (unsigned int blocksize)
80 {
81 ssize_t us;
82 unsigned int cs;
83 u8 buff1[64*1024];
84 u8 buff2[64*1024];
85 u8 header[3+2+2];
86
87 header[0] = 'Z';
88 header[1] = 'V';
89
90 for(;;) {
91 us = fread (buff1, 1, blocksize, stdin);
92
93 if (us < blocksize)
94 {
95 if (us == 0)
96 break;
97 else if (!feof (stdin))
98 {
99 perror ("compress");
100 exit (1);
101 }
102 }
103
104 cs = lzf_compress (buff1, us, buff2, us - 4);
105
106 if (cs)
107 {
108 header[2] = 1;
109 header[3] = cs >> 8;
110 header[4] = cs & 0xff;
111 header[5] = us >> 8;
112 header[6] = us & 0xff;
113
114 fwrite (header, 3+2+2, 1, stdout);
115 fwrite (buff2, cs, 1, stdout);
116 }
117 else
118 {
119 header[2] = 0;
120 header[3] = us >> 8;
121 header[4] = us & 0xff;
122
123 fwrite (header, 3+2, 1, stdout);
124 fwrite (buff1, us, 1, stdout);
125 }
126 } while (!feof (stdin));
127 }
128
129 static void decompress (void)
130 {
131 ssize_t us;
132 unsigned int cs;
133 u8 buff1[64*1024];
134 u8 buff2[64*1024];
135 u8 header[3+2+2];
136
137 for(;;) {
138 if (fread (header, 3+2, 1, stdin) != 1)
139 {
140 if (feof (stdin))
141 break;
142 else
143 {
144 perror ("decompress");
145 exit (1);
146 }
147 }
148
149 if (header[0] != 'Z' || header[1] != 'V')
150 {
151 fprintf (stderr, "decompress: invalid stream - no magic number found\n");
152 exit (1);
153 }
154
155 cs = (header[3] << 8) | header[4];
156
157 if (header[2] == 1)
158 {
159 if (fread (header+3+2, 2, 1, stdin) != 1)
160 {
161 perror ("decompress");
162 exit (1);
163 }
164
165 us = (header[5] << 8) | header[6];
166
167 if (fread (buff1, cs, 1, stdin) != 1)
168 {
169 perror ("decompress");
170 exit (1);
171 }
172
173 if (lzf_decompress (buff1, cs, buff2, us) != us)
174 {
175 fprintf (stderr, "decompress: invalid stream - data corrupted\n");
176 exit (1);
177 }
178
179 fwrite (buff2, us, 1, stdout);
180 }
181 else if (header[2] == 0)
182 {
183 if (fread (buff2, cs, 1, stdin) != 1)
184 {
185 perror ("decompress");
186 exit (1);
187 }
188
189 fwrite (buff2, cs, 1, stdout);
190 }
191 else
192 {
193 fprintf (stderr, "decompress: invalid stream - unknown block type\n");
194 exit (1);
195 }
196 }
197 }
198
199 int
200 main (int argc, char *argv[])
201 {
202 int c;
203 unsigned int blocksize = 64*1024-1;
204 enum { m_compress, m_decompress } mode = m_compress;
205
206 while ((c = getopt (argc, argv, "cdb:h")) != -1)
207 switch (c)
208 {
209 case 'c':
210 mode = m_compress;
211 break;
212
213 case 'd':
214 mode = m_decompress;
215 break;
216
217 case 'b':
218 blocksize = atol (optarg);
219 break;
220
221 case 'h':
222 usage (0);
223
224 case ':':
225 fprintf (stderr, "required argument missing, use -h\n");
226 exit (1);
227
228 case '?':
229 fprintf (stderr, "unknown option, use -h\n");
230 exit (1);
231
232 default:
233 usage (1);
234 }
235
236 if (mode == m_compress)
237 compress (blocksize);
238 else if (mode == m_decompress)
239 decompress ();
240 else
241 abort ();
242
243 return 0;
244 }