ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf.c
Revision: 1.6
Committed: Fri Jul 7 15:34:11 2006 UTC (17 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_6
Changes since 1.5: +4 -0 lines
Log Message:
*** empty log message ***

File Contents

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