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