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