ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf.c
(Generate patch)

Comparing liblzf/lzf.c (file contents):
Revision 1.6 by root, Fri Jul 7 15:34:11 2006 UTC vs.
Revision 1.14 by root, Fri Mar 30 20:58:09 2012 UTC

1/* 1/*
2 * Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com>
2 * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> 3 * Copyright (c) 2012 Marc Lehmann <schmorp@schmorp.de>
3 * 4 *
4 * Redistribution and use in source and binary forms, with or without modifica- 5 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met: 6 * tion, are permitted provided that the following conditions are met:
6 * 7 *
7 * 1. Redistributions of source code must retain the above copyright notice, 8 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer. 9 * this list of conditions and the following disclaimer.
9 * 10 *
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 13 * 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 * 14 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 * 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- 16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 * 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 23 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 * 25 *
28 * Alternatively, the contents of this file may be used under the terms of 26 * 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 27 * the GNU General Public License ("GPL") version 2 or any later version,
30 * provisions of the GPL are applicable instead of the above. If you wish to 28 * in which case the provisions of the GPL are applicable instead of
31 * allow the use of your version of this file only under the terms of the 29 * the above. If you wish to allow the use of your version of this file
32 * GPL and not to allow others to use your version of this file under the 30 * only under the terms of the GPL and not to allow others to use your
33 * BSD license, indicate your decision by deleting the provisions above and 31 * version of this file under the BSD license, indicate your decision
34 * replace them with the notice and other provisions required by the GPL. If 32 * by deleting the provisions above and replace them with the notice
35 * you do not delete the provisions above, a recipient may use your version 33 * and other provisions required by the GPL. If you do not delete the
34 * provisions above, a recipient may use your version of this file under
36 * of this file under either the BSD or the GPL. 35 * either the BSD or the GPL.
37 */ 36 */
38 37
39#include "config.h" 38#include "config.h"
40
41#include <stdio.h> 39#include <stdio.h>
40#include <string.h>
42#include <stdlib.h> 41#include <stdlib.h>
43#include <string.h>
44#include <assert.h>
45
46#include <unistd.h> 42#include <unistd.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <fcntl.h>
47#include <getopt.h> 46#include <errno.h>
48 47#include <limits.h>
49#include "lzf.h" 48#include "lzf.h"
49#include "lzf_c_best.c"
50
51#ifdef HAVE_GETOPT_H
52# include <getopt.h>
53#endif
54
55#define BLOCKSIZE (1024 * 64 - 1)
56#define MAX_BLOCKSIZE BLOCKSIZE
50 57
51typedef unsigned char u8; 58typedef unsigned char u8;
52 59
60static off_t nr_read, nr_written;
61
62static const char *imagename;
63static enum { compress, uncompress, lzcat } mode = compress;
64static int verbose = 0;
65static int best = 0;
66static int force = 0;
67static long blocksize = BLOCKSIZE;
68
69#ifdef HAVE_GETOPT_LONG
70
71 struct option longopts[] = {
72 {"compress" , 0, 0, 'c'},
73 {"decompress", 0, 0, 'd'},
74 {"uncompress", 0, 0, 'd'},
75 {"best" , 0, 0, '9'},
76 {"force" , 0, 0, 'f'},
77 {"help" , 0, 0, 'h'},
78 {"verbose" , 0, 0, 'v'},
79 {"blocksize" , 1, 0, 'b'},
80 {0 , 0, 0, 0}
81 };
82
83 static const char *opt =
84 "-c --compress compress\n"
85 "-d --decompress decompress\n"
86 "-9 --best best compression\n"
87 "-f --force force overwrite of output file\n"
88 "-h --help give this help\n"
89 "-v --verbose verbose mode\n"
90 "-b # --blocksize # set blocksize\n"
91 "\n";
92
93#else
94
95 static const char *opt =
96 "-c compress\n"
97 "-d decompress\n"
98 "-9 best compression\n"
99 "-f force overwrite of output file\n"
100 "-h give this help\n"
101 "-v verbose mode\n"
102 "-b # set blocksize\n"
103 "\n";
104
105#endif
106
53static void 107static void
54usage (int ec) 108usage (int rc)
55{ 109{
56 fprintf (stderr, "\n" 110 fprintf (stderr, "\n"
57 "lzf, a very lightweight compression/decompression filter\n" 111 "lzf, a very lightweight compression/decompression utility written by Stefan Traby.\n"
58 "written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n" 112 "uses liblzf written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n"
59 "http://liblzf.plan9.de/\n" 113 "http://liblzf.plan9.de/\n"
60 "\n" 114 "\n"
61 "USAGE: lzf -c [-b blocksize] | -d\n" 115 "usage: lzf [-dufhvb9] [file ...]\n"
62 " -c compress\n" 116 " unlzf [file ...]\n"
63 " -d decompress\n" 117 " lzcat [file ...]\n"
64 " -b specify the blocksize (default 64k-1)\n"
65 "\n" 118 "\n%s",
66 ); 119 opt);
67 120
68 exit (ec); 121 exit (rc);
122}
123
124static inline ssize_t
125rread (int fd, void *buf, size_t len)
126{
127 ssize_t rc = 0, offset = 0;
128 char *p = buf;
129
130 while (len && (rc = read (fd, &p[offset], len)) > 0)
131 {
132 offset += rc;
133 len -= rc;
134 }
135
136 nr_read += offset;
137
138 if (rc < 0)
139 return rc;
140
141 return offset;
142}
143
144/* returns 0 if all written else -1 */
145static inline ssize_t
146wwrite (int fd, void *buf, size_t len)
147{
148 ssize_t rc;
149 char *b = buf;
150 size_t l = len;
151
152 while (l)
153 {
154 rc = write (fd, b, l);
155 if (rc < 0)
156 {
157 fprintf (stderr, "%s: write error: ", imagename);
158 perror ("");
159 return -1;
160 }
161
162 l -= rc;
163 b += rc;
164 }
165
166 nr_written += len;
167 return 0;
69} 168}
70 169
71/* 170/*
72 * Anatomy: an lzf file consists of any number of blocks in the following format: 171 * Anatomy: an lzf file consists of any number of blocks in the following format:
73 * 172 *
173 * \x00 EOF (optional)
74 * "ZV\0" 2-byte-usize <uncompressed data> 174 * "ZV\0" 2-byte-usize <uncompressed data>
75 * "ZV\1" 2-byte-csize 2-byte-usize <compressed data> 175 * "ZV\1" 2-byte-csize 2-byte-usize <compressed data>
76 * "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI) 176 * "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI)
77 *
78 */ 177 */
79 178
80static void compress (unsigned int blocksize) 179
180#define TYPE0_HDR_SIZE 5
181#define TYPE1_HDR_SIZE 7
182#define MAX_HDR_SIZE 7
183#define MIN_HDR_SIZE 5
184
185static int
186compress_fd (int from, int to)
81{ 187{
82 ssize_t us; 188 ssize_t us, cs, len;
83 unsigned int cs; 189 u8 buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
84 u8 buff1[64*1024]; 190 u8 buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
85 u8 buff2[64*1024];
86 u8 header[3+2+2]; 191 u8 *header;
87 192
88 header[0] = 'Z'; 193 nr_read = nr_written = 0;
89 header[1] = 'V'; 194 while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0)
90
91 for(;;) {
92 us = fread (buff1, 1, blocksize, stdin);
93
94 if (us < blocksize)
95 { 195 {
96 if (us == 0) 196 cs = (best ? lzf_compress_best : lzf_compress) (&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], us > 4 ? us - 4 : us);
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) 197 if (cs)
108 { 198 {
199 header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE];
200 header[0] = 'Z';
201 header[1] = 'V';
109 header[2] = 1; 202 header[2] = 1;
110 header[3] = cs >> 8; 203 header[3] = cs >> 8;
111 header[4] = cs & 0xff; 204 header[4] = cs & 0xff;
112 header[5] = us >> 8; 205 header[5] = us >> 8;
113 header[6] = us & 0xff; 206 header[6] = us & 0xff;
114 207 len = cs + TYPE1_HDR_SIZE;
115 fwrite (header, 3+2+2, 1, stdout);
116 fwrite (buff2, cs, 1, stdout);
117 } 208 }
118 else 209 else
119 { 210 { // write uncompressed
211 header = &buf1[MAX_HDR_SIZE - TYPE0_HDR_SIZE];
212 header[0] = 'Z';
213 header[1] = 'V';
120 header[2] = 0; 214 header[2] = 0;
121 header[3] = us >> 8; 215 header[3] = us >> 8;
122 header[4] = us & 0xff; 216 header[4] = us & 0xff;
123 217 len = us + TYPE0_HDR_SIZE;
124 fwrite (header, 3+2, 1, stdout);
125 fwrite (buff1, us, 1, stdout);
126 } 218 }
127 } while (!feof (stdin));
128}
129 219
130static void decompress (void) 220 if (wwrite (to, header, len) == -1)
131{ 221 return -1;
132 ssize_t us; 222 }
133 unsigned int cs;
134 u8 buff1[64*1024];
135 u8 buff2[64*1024];
136 u8 header[3+2+2];
137 223
138 for(;;) { 224 return 0;
139 if (fread (header, 3+2, 1, stdin) != 1) 225}
226
227static int
228uncompress_fd (int from, int to)
229{
230 u8 header[MAX_HDR_SIZE];
231 u8 buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
232 u8 buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
233 u8 *p;
234 int l, rd;
235 ssize_t rc, cs, us, bytes, over = 0;
236
237 nr_read = nr_written = 0;
238 while (1)
239 {
240 rc = rread (from, header + over, MAX_HDR_SIZE - over);
241 if (rc < 0)
140 { 242 {
141 if (feof (stdin)) 243 fprintf (stderr, "%s: read error: ", imagename);
244 perror ("");
245 return -1;
246 }
247
248 rc += over;
249 over = 0;
250 if (!rc || header[0] == 0)
251 return 0;
252
253 if (rc < MIN_HDR_SIZE || header[0] != 'Z' || header[1] != 'V')
254 {
255 fprintf (stderr, "%s: invalid data stream - magic not found or short header\n", imagename);
256 return -1;
257 }
258
259 switch (header[2])
260 {
261 case 0:
262 cs = -1;
263 us = (header[3] << 8) | header[4];
264 p = &header[TYPE0_HDR_SIZE];
142 break; 265 break;
143 else 266 case 1:
267 if (rc < TYPE1_HDR_SIZE)
144 { 268 {
145 perror ("decompress"); 269 goto short_read;
146 exit (1);
147 } 270 }
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]; 271 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]; 272 us = (header[5] << 8) | header[6];
167 273 p = &header[TYPE1_HDR_SIZE];
168 if (fread (buff1, cs, 1, stdin) != 1) 274 break;
169 { 275 default:
170 perror ("decompress"); 276 fprintf (stderr, "%s: unknown blocktype\n", imagename);
171 exit (1); 277 return -1;
172 } 278 }
173 279
280 bytes = cs == -1 ? us : cs;
281 l = &header[rc] - p;
282
283 if (l > 0)
284 memcpy (buf1, p, l);
285
286 if (l > bytes)
287 {
288 over = l - bytes;
289 memmove (header, &p[bytes], over);
290 }
291
292 p = &buf1[l];
293 rd = bytes - l;
294 if (rd > 0)
295 if ((rc = rread (from, p, rd)) != rd)
296 goto short_read;
297
298 if (cs == -1)
299 {
300 if (wwrite (to, buf1, us))
301 return -1;
302 }
303 else
304 {
174 if (lzf_decompress (buff1, cs, buff2, us) != us) 305 if (lzf_decompress (buf1, cs, buf2, us) != us)
175 { 306 {
176 fprintf (stderr, "decompress: invalid stream - data corrupted\n"); 307 fprintf (stderr, "%s: decompress: invalid stream - data corrupted\n", imagename);
177 exit (1); 308 return -1;
178 } 309 }
179 310
180 fwrite (buff2, us, 1, stdout); 311 if (wwrite (to, buf2, us))
181 } 312 return -1;
182 else if (header[2] == 0)
183 {
184 if (fread (buff2, cs, 1, stdin) != 1)
185 {
186 perror ("decompress");
187 exit (1);
188 } 313 }
189
190 fwrite (buff2, cs, 1, stdout);
191 } 314 }
192 else 315
316 return 0;
317
318short_read:
319 fprintf (stderr, "%s: short data\n", imagename);
320 return -1;
321}
322
323static int
324open_out (const char *name)
325{
326 int fd;
327 int m = O_EXCL;
328
329 if (force)
330 m = 0;
331
332 fd = open (name, O_CREAT | O_WRONLY | O_TRUNC | m, 600);
333#if defined(__MINGW32__)
334 _setmode(fd, _O_BINARY);
335#endif
336 return fd;
337}
338
339static int
340compose_name (const char *fname, char *oname)
341{
342 char *p;
343
344 if (mode == compress)
345 {
346 if (strlen (fname) > PATH_MAX - 4)
193 { 347 {
194 fprintf (stderr, "decompress: invalid stream - unknown block type\n"); 348 fprintf (stderr, "%s: %s.lzf: name too long", imagename, fname);
195 exit (1); 349 return -1;
196 } 350 }
351
352 strcpy (oname, fname);
353 strcat (oname, ".lzf");
197 } 354 }
355 else
356 {
357 if (strlen (fname) > PATH_MAX)
358 {
359 fprintf (stderr, "%s: %s: name too long\n", imagename, fname);
360 return -1;
361 }
362
363 strcpy (oname, fname);
364 p = &oname[strlen (oname)] - 4;
365 if (p < oname || strcmp (p, ".lzf"))
366 {
367 fprintf (stderr, "%s: %s: unknown suffix\n", imagename, fname);
368 return -1;
369 }
370
371 *p = 0;
372 }
373
374 return 0;
375}
376
377static int
378run_file (const char *fname)
379{
380 int fd, fd2;
381 int rc;
382 struct stat mystat;
383 char oname[PATH_MAX + 1];
384
385 if (mode != lzcat)
386 if (compose_name (fname, oname))
387 return -1;
388
389#if !defined(__MINGW32__)
390 rc = lstat (fname, &mystat);
391#else
392 rc = stat (fname, &mystat);
393#endif
394 fd = open (fname, O_RDONLY);
395#if defined(__MINGW32__)
396 _setmode(fd, _O_BINARY);
397#endif
398 if (rc || fd == -1)
399 {
400 fprintf (stderr, "%s: %s: ", imagename, fname);
401 perror ("");
402 return -1;
403 }
404
405 if (!S_ISREG (mystat.st_mode))
406 {
407 fprintf (stderr, "%s: %s: not a regular file.\n", imagename, fname);
408 close (fd);
409 return -1;
410 }
411
412 if (mode == lzcat)
413 {
414 rc = uncompress_fd (fd, 1);
415 close (fd);
416 return rc;
417 }
418
419 fd2 = open_out (oname);
420 if (fd2 == -1)
421 {
422 fprintf (stderr, "%s: %s: ", imagename, oname);
423 perror ("");
424 close (fd);
425 return -1;
426 }
427
428 if (mode == compress)
429 {
430 rc = compress_fd (fd, fd2);
431 if (!rc && verbose)
432 fprintf (stderr, "%s: %5.1f%% -- replaced with %s\n",
433 fname, nr_read == 0 ? 0 : 100.0 - nr_written / ((double) nr_read / 100.0), oname);
434 }
435 else
436 {
437 rc = uncompress_fd (fd, fd2);
438 if (!rc && verbose)
439 fprintf (stderr, "%s: %5.1f%% -- replaced with %s\n",
440 fname, nr_written == 0 ? 0 : 100.0 - nr_read / ((double) nr_written / 100.0), oname);
441 }
442
443#if !defined(__MINGW32__)
444 fchmod (fd2, mystat.st_mode);
445#else
446 chmod (oname, mystat.st_mode);
447#endif
448 close (fd);
449 close (fd2);
450
451 if (!rc)
452 unlink (fname);
453
454 return rc;
198} 455}
199 456
200int 457int
201main (int argc, char *argv[]) 458main (int argc, char *argv[])
202{ 459{
460 char *p = argv[0];
203 int c; 461 int optc;
204 unsigned int blocksize = 64*1024-1; 462 int rc = 0;
205 enum { m_compress, m_decompress } mode = m_compress;
206 463
207 if (!strcmp (argv[0] + strlen (argv[0] - 5), "unlzf")) 464 errno = 0;
465 p = getenv ("LZF_BLOCKSIZE");
466 if (p)
467 {
468 blocksize = strtoul (p, 0, 0);
469 if (errno || !blocksize || blocksize > MAX_BLOCKSIZE)
470 blocksize = BLOCKSIZE;
471 }
472
473 p = strrchr (argv[0], '/');
474 imagename = p ? ++p : argv[0];
475
476 if (!strncmp (imagename, "un", 2) || !strncmp (imagename, "de", 2))
208 mode = m_decompress; 477 mode = uncompress;
209 478
479 if (strstr (imagename, "cat"))
480 mode = lzcat;
481
482#ifdef HAVE_GETOPT_LONG
483 while ((optc = getopt_long (argc, argv, "cd9fhvb:", longopts, 0)) != -1)
484#else
210 while ((c = getopt (argc, argv, "cdb:h")) != -1) 485 while ((optc = getopt (argc, argv, "cd9fhvb:")) != -1)
486#endif
487 {
211 switch (c) 488 switch (optc)
212 { 489 {
213 case 'c': 490 case 'c':
214 mode = m_compress; 491 mode = compress;
215 break; 492 break;
216
217 case 'd': 493 case 'd':
218 mode = m_decompress; 494 mode = uncompress;
219 break; 495 break;
220
221 case 'b': 496 case '9':
222 blocksize = atol (optarg); 497 best = 1;
223 break; 498 break;
224 499 case 'f':
500 force = 1;
501 break;
225 case 'h': 502 case 'h':
226 usage (0); 503 usage (0);
227 504 break;
228 case ':':
229 fprintf (stderr, "required argument missing, use -h\n");
230 exit (1);
231
232 case '?': 505 case 'v':
233 fprintf (stderr, "unknown option, use -h\n"); 506 verbose = 1;
234 exit (1); 507 break;
235 508 case 'b':
509 errno = 0;
510 blocksize = strtoul (optarg, 0, 0);
511 if (errno || !blocksize || blocksize > MAX_BLOCKSIZE)
512 blocksize = BLOCKSIZE;
513 break;
236 default: 514 default:
237 usage (1); 515 usage (1);
516 break;
238 } 517 }
518 }
239 519
520 if (optind == argc)
521 { // stdin stdout
522 if (!force)
523 {
524 if ((mode == uncompress || mode == lzcat) && isatty (0))
525 {
526 fprintf (stderr, "%s: compressed data not read from a terminal. Use -f to force decompression.\n", imagename);
527 exit (1);
528 }
529 if (mode == compress && isatty (1))
530 {
531 fprintf (stderr, "%s: compressed data not written to a terminal. Use -f to force compression.\n", imagename);
532 exit (1);
533 }
534 }
535
240 if (mode == m_compress) 536 if (mode == compress)
241 compress (blocksize); 537 rc = compress_fd (0, 1);
242 else if (mode == m_decompress)
243 decompress ();
244 else 538 else
245 abort (); 539 rc = uncompress_fd (0, 1);
246 540
247 return 0; 541 exit (rc ? 1 : 0);
542 }
543
544 while (optind < argc)
545 rc |= run_file (argv[optind++]);
546
547 exit (rc ? 1 : 0);
248} 548}
549

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines