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, |
… | |
… | |
43 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
44 | #include <fcntl.h> |
45 | #include <fcntl.h> |
45 | #include <errno.h> |
46 | #include <errno.h> |
46 | #include <limits.h> |
47 | #include <limits.h> |
47 | #include "lzf.h" |
48 | #include "lzf.h" |
|
|
49 | #include "lzf_c_best.c" |
48 | |
50 | |
49 | #ifdef HAVE_GETOPT_H |
51 | #ifdef HAVE_GETOPT_H |
50 | # include <getopt.h> |
52 | # include <getopt.h> |
51 | #endif |
53 | #endif |
52 | |
54 | |
… | |
… | |
58 | static off_t nr_read, nr_written; |
60 | static off_t nr_read, nr_written; |
59 | |
61 | |
60 | static const char *imagename; |
62 | static const char *imagename; |
61 | static enum { compress, uncompress, lzcat } mode = compress; |
63 | static enum { compress, uncompress, lzcat } mode = compress; |
62 | static int verbose = 0; |
64 | static int verbose = 0; |
|
|
65 | static int best = 0; |
63 | static int force = 0; |
66 | static int force = 0; |
64 | static long blocksize = BLOCKSIZE; |
67 | static long blocksize = BLOCKSIZE; |
65 | |
68 | |
66 | #ifdef HAVE_GETOPT_LONG |
69 | #ifdef HAVE_GETOPT_LONG |
67 | |
70 | |
68 | struct option longopts[] = { |
71 | struct option longopts[] = { |
69 | {"compress", 0, 0, 'c'}, |
72 | {"compress" , 0, 0, 'c'}, |
70 | {"decompress", 0, 0, 'd'}, |
73 | {"decompress", 0, 0, 'd'}, |
71 | {"uncompress", 0, 0, 'd'}, |
74 | {"uncompress", 0, 0, 'd'}, |
|
|
75 | {"best" , 0, 0, '9'}, |
72 | {"force", 0, 0, 'f'}, |
76 | {"force" , 0, 0, 'f'}, |
73 | {"help", 0, 0, 'h'}, |
77 | {"help" , 0, 0, 'h'}, |
74 | {"verbose", 0, 0, 'v'}, |
78 | {"verbose" , 0, 0, 'v'}, |
75 | {"blocksize", 1, 0, 'b'}, |
79 | {"blocksize" , 1, 0, 'b'}, |
76 | {0, 0, 0, 0} |
80 | {0 , 0, 0, 0} |
77 | }; |
81 | }; |
78 | |
82 | |
79 | static const char *opt = |
83 | static const char *opt = |
80 | "-c --compress compress\n" |
84 | "-c --compress compress\n" |
81 | "-d --decompress decompress\n" |
85 | "-d --decompress decompress\n" |
|
|
86 | "-9 --best best compression\n" |
82 | "-f --force force overwrite of output file\n" |
87 | "-f --force force overwrite of output file\n" |
83 | "-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"; |
84 | |
92 | |
85 | #else |
93 | #else |
86 | |
94 | |
87 | static const char *opt = |
95 | static const char *opt = |
88 | "-c compress\n" |
96 | "-c compress\n" |
89 | "-d decompress\n" |
97 | "-d decompress\n" |
|
|
98 | "-9 best compression\n" |
90 | "-f force overwrite of output file\n" |
99 | "-f force overwrite of output file\n" |
91 | "-h give this help\n" |
100 | "-h give this help\n" |
92 | "-v verbose mode\n" |
101 | "-v verbose mode\n" |
93 | "-b # set blocksize\n" |
102 | "-b # set blocksize\n" |
94 | "\n"; |
103 | "\n"; |
… | |
… | |
101 | fprintf (stderr, "\n" |
110 | fprintf (stderr, "\n" |
102 | "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" |
103 | "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" |
104 | "http://liblzf.plan9.de/\n" |
113 | "http://liblzf.plan9.de/\n" |
105 | "\n" |
114 | "\n" |
106 | "usage: lzf [-dufhvb] [file ...]\n" |
115 | "usage: lzf [-dufhvb9] [file ...]\n" |
107 | " unlzf [file ...]\n" |
116 | " unlzf [file ...]\n" |
108 | " lzcat [file ...]\n" |
117 | " lzcat [file ...]\n" |
109 | "\n%s", |
118 | "\n%s", |
110 | opt); |
119 | opt); |
111 | |
120 | |
… | |
… | |
182 | u8 *header; |
191 | u8 *header; |
183 | |
192 | |
184 | nr_read = nr_written = 0; |
193 | nr_read = nr_written = 0; |
185 | while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) |
194 | while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) |
186 | { |
195 | { |
187 | 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); |
188 | if (cs) |
197 | if (cs) |
189 | { |
198 | { |
190 | header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; |
199 | header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; |
191 | header[0] = 'Z'; |
200 | header[0] = 'Z'; |
192 | header[1] = 'V'; |
201 | header[1] = 'V'; |
… | |
… | |
469 | |
478 | |
470 | if (strstr (imagename, "cat")) |
479 | if (strstr (imagename, "cat")) |
471 | mode = lzcat; |
480 | mode = lzcat; |
472 | |
481 | |
473 | #ifdef HAVE_GETOPT_LONG |
482 | #ifdef HAVE_GETOPT_LONG |
474 | while ((optc = getopt_long (argc, argv, "cdfhvb:", longopts, 0)) != -1) |
483 | while ((optc = getopt_long (argc, argv, "cd9fhvb:", longopts, 0)) != -1) |
475 | #else |
484 | #else |
476 | while ((optc = getopt (argc, argv, "cdfhvb:")) != -1) |
485 | while ((optc = getopt (argc, argv, "cd9fhvb:")) != -1) |
477 | #endif |
486 | #endif |
478 | { |
487 | { |
479 | switch (optc) |
488 | switch (optc) |
480 | { |
489 | { |
481 | case 'c': |
490 | case 'c': |
482 | mode = compress; |
491 | mode = compress; |
483 | break; |
492 | break; |
484 | case 'd': |
493 | case 'd': |
485 | mode = uncompress; |
494 | mode = uncompress; |
|
|
495 | break; |
|
|
496 | case '9': |
|
|
497 | best = 1; |
486 | break; |
498 | break; |
487 | case 'f': |
499 | case 'f': |
488 | force = 1; |
500 | force = 1; |
489 | break; |
501 | break; |
490 | case 'h': |
502 | case 'h': |