1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com> |
2 | * Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com> |
|
|
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, |
… | |
… | |
21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
22 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
22 | * 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 |
23 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | * |
25 | * |
25 | * 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 |
26 | * 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, |
27 | * 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 |
28 | * 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 |
29 | * 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 |
30 | * BSD license, indicate your decision by deleting the provisions above and |
31 | * version of this file under the BSD license, indicate your decision |
31 | * 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 |
32 | * 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 |
33 | * of this file under either the BSD or the GPL License. |
34 | * provisions above, a recipient may use your version of this file under |
|
|
35 | * either the BSD or the GPL. |
34 | */ |
36 | */ |
35 | |
37 | |
36 | #include "config.h" |
38 | #include "config.h" |
37 | #include <stdio.h> |
39 | #include <stdio.h> |
38 | #include <string.h> |
40 | #include <string.h> |
… | |
… | |
42 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
43 | #include <fcntl.h> |
45 | #include <fcntl.h> |
44 | #include <errno.h> |
46 | #include <errno.h> |
45 | #include <limits.h> |
47 | #include <limits.h> |
46 | #include "lzf.h" |
48 | #include "lzf.h" |
|
|
49 | #include "lzf_c_best.c" |
47 | |
50 | |
48 | #ifdef HAVE_GETOPT_H |
51 | #ifdef HAVE_GETOPT_H |
49 | # include <getopt.h> |
52 | # include <getopt.h> |
50 | #endif |
53 | #endif |
51 | |
54 | |
… | |
… | |
57 | static off_t nr_read, nr_written; |
60 | static off_t nr_read, nr_written; |
58 | |
61 | |
59 | static const char *imagename; |
62 | static const char *imagename; |
60 | static enum { compress, uncompress, lzcat } mode = compress; |
63 | static enum { compress, uncompress, lzcat } mode = compress; |
61 | static int verbose = 0; |
64 | static int verbose = 0; |
|
|
65 | static int best = 0; |
62 | static int force = 0; |
66 | static int force = 0; |
63 | static long blocksize = BLOCKSIZE; |
67 | static long blocksize = BLOCKSIZE; |
64 | |
68 | |
65 | #ifdef HAVE_GETOPT_LONG |
69 | #ifdef HAVE_GETOPT_LONG |
66 | |
70 | |
67 | struct option longopts[] = { |
71 | struct option longopts[] = { |
68 | {"compress", 0, 0, 'c'}, |
72 | {"compress" , 0, 0, 'c'}, |
69 | {"decompress", 0, 0, 'd'}, |
73 | {"decompress", 0, 0, 'd'}, |
70 | {"uncompress", 0, 0, 'd'}, |
74 | {"uncompress", 0, 0, 'd'}, |
|
|
75 | {"best" , 0, 0, '9'}, |
71 | {"force", 0, 0, 'f'}, |
76 | {"force" , 0, 0, 'f'}, |
72 | {"help", 0, 0, 'h'}, |
77 | {"help" , 0, 0, 'h'}, |
73 | {"verbose", 0, 0, 'v'}, |
78 | {"verbose" , 0, 0, 'v'}, |
74 | {"blocksize", 1, 0, 'b'}, |
79 | {"blocksize" , 1, 0, 'b'}, |
75 | {0, 0, 0, 0} |
80 | {0 , 0, 0, 0} |
76 | }; |
81 | }; |
77 | |
82 | |
78 | static const char *opt = |
83 | static const char *opt = |
79 | "-c --compress compress\n" |
84 | "-c --compress compress\n" |
80 | "-d --decompress decompress\n" |
85 | "-d --decompress decompress\n" |
|
|
86 | "-9 --best best compression\n" |
81 | "-f --force force overwrite of output file\n" |
87 | "-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"; |
88 | "-h --help give this help\n" |
|
|
89 | "-v --verbose verbose mode\n" |
|
|
90 | "-b # --blocksize # set blocksize\n" |
|
|
91 | "\n"; |
83 | |
92 | |
84 | #else |
93 | #else |
85 | |
94 | |
86 | static const char *opt = |
95 | static const char *opt = |
87 | "-c compress\n" |
96 | "-c compress\n" |
88 | "-d decompress\n" |
97 | "-d decompress\n" |
|
|
98 | "-9 best compression\n" |
89 | "-f force overwrite of output file\n" |
99 | "-f force overwrite of output file\n" |
90 | "-h give this help\n" |
100 | "-h give this help\n" |
91 | "-v verbose mode\n" |
101 | "-v verbose mode\n" |
92 | "-b # set blocksize\n" |
102 | "-b # set blocksize\n" |
93 | "\n"; |
103 | "\n"; |
… | |
… | |
100 | fprintf (stderr, "\n" |
110 | fprintf (stderr, "\n" |
101 | "lzf, a very lightweight compression/decompression utility written by Stefan Traby.\n" |
111 | "lzf, a very lightweight compression/decompression utility written by Stefan Traby.\n" |
102 | "uses liblzf 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" |
103 | "http://liblzf.plan9.de/\n" |
113 | "http://liblzf.plan9.de/\n" |
104 | "\n" |
114 | "\n" |
105 | "usage: lzf [-dufhvb] [file ...]\n" |
115 | "usage: lzf [-dufhvb9] [file ...]\n" |
106 | " unlzf [file ...]\n" |
116 | " unlzf [file ...]\n" |
107 | " lzcat [file ...]\n" |
117 | " lzcat [file ...]\n" |
108 | "\n%s", |
118 | "\n%s", |
109 | opt); |
119 | opt); |
110 | |
120 | |
… | |
… | |
181 | u8 *header; |
191 | u8 *header; |
182 | |
192 | |
183 | nr_read = nr_written = 0; |
193 | nr_read = nr_written = 0; |
184 | while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) |
194 | while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) |
185 | { |
195 | { |
186 | cs = lzf_compress (&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], us > 4 ? us - 4 : us); |
196 | cs = (best ? lzf_compress_best : lzf_compress) (&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], us > 4 ? us - 4 : us); |
187 | if (cs) |
197 | if (cs) |
188 | { |
198 | { |
189 | header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; |
199 | header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; |
190 | header[0] = 'Z'; |
200 | header[0] = 'Z'; |
191 | header[1] = 'V'; |
201 | header[1] = 'V'; |
… | |
… | |
468 | |
478 | |
469 | if (strstr (imagename, "cat")) |
479 | if (strstr (imagename, "cat")) |
470 | mode = lzcat; |
480 | mode = lzcat; |
471 | |
481 | |
472 | #ifdef HAVE_GETOPT_LONG |
482 | #ifdef HAVE_GETOPT_LONG |
473 | while ((optc = getopt_long (argc, argv, "cdfhvb:", longopts, 0)) != -1) |
483 | while ((optc = getopt_long (argc, argv, "cd9fhvb:", longopts, 0)) != -1) |
474 | #else |
484 | #else |
475 | while ((optc = getopt (argc, argv, "cdfhvb:")) != -1) |
485 | while ((optc = getopt (argc, argv, "cd9fhvb:")) != -1) |
476 | #endif |
486 | #endif |
477 | { |
487 | { |
478 | switch (optc) |
488 | switch (optc) |
479 | { |
489 | { |
480 | case 'c': |
490 | case 'c': |
481 | mode = compress; |
491 | mode = compress; |
482 | break; |
492 | break; |
483 | case 'd': |
493 | case 'd': |
484 | mode = uncompress; |
494 | mode = uncompress; |
|
|
495 | break; |
|
|
496 | case '9': |
|
|
497 | best = 1; |
485 | break; |
498 | break; |
486 | case 'f': |
499 | case 'f': |
487 | force = 1; |
500 | force = 1; |
488 | break; |
501 | break; |
489 | case 'h': |
502 | case 'h': |