| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "zbuild.h" |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <assert.h> |
| 10 |
|
| 11 |
#include "zutil.h" |
| 12 |
|
| 13 |
#if defined(_WIN32) || defined(__CYGWIN__) |
| 14 |
# include <fcntl.h> |
| 15 |
# include <io.h> |
| 16 |
# include <string.h> |
| 17 |
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
| 18 |
# ifdef _MSC_VER |
| 19 |
# define strcasecmp _stricmp |
| 20 |
# endif |
| 21 |
#else |
| 22 |
# include <strings.h> |
| 23 |
# define SET_BINARY_MODE(file) |
| 24 |
#endif |
| 25 |
|
| 26 |
#define CHECK_ERR(err, msg) { \ |
| 27 |
if (err != Z_OK) { \ |
| 28 |
fprintf(stderr, "%s error: %d\n", msg, err); \ |
| 29 |
exit(1); \ |
| 30 |
} \ |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
#define BUFSIZE 131072 |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
void deflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t level, |
| 40 |
int32_t window_bits, int32_t mem_level, int32_t strategy, int32_t flush) { |
| 41 |
PREFIX3(stream) c_stream; |
| 42 |
uint8_t *read_buf; |
| 43 |
uint8_t *write_buf; |
| 44 |
int32_t read; |
| 45 |
int err; |
| 46 |
|
| 47 |
read_buf = (uint8_t *)malloc(read_buf_size); |
| 48 |
if (read_buf == NULL) { |
| 49 |
fprintf(stderr, "failed to create read buffer (%d)\n", read_buf_size); |
| 50 |
return; |
| 51 |
} |
| 52 |
write_buf = (uint8_t *)malloc(write_buf_size); |
| 53 |
if (write_buf == NULL) { |
| 54 |
fprintf(stderr, "failed to create write buffer (%d)\n", write_buf_size); |
| 55 |
free(read_buf); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
c_stream.zalloc = NULL; |
| 60 |
c_stream.zfree = NULL; |
| 61 |
c_stream.opaque = (void *)0; |
| 62 |
c_stream.total_in = 0; |
| 63 |
c_stream.total_out = 0; |
| 64 |
c_stream.next_out = write_buf; |
| 65 |
c_stream.avail_out = write_buf_size; |
| 66 |
|
| 67 |
err = PREFIX(deflateInit2)(&c_stream, level, Z_DEFLATED, window_bits, mem_level, strategy); |
| 68 |
CHECK_ERR(err, "deflateInit2"); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
do { |
| 73 |
read = (int32_t)fread(read_buf, 1, read_buf_size, fin); |
| 74 |
if (read <= 0) |
| 75 |
break; |
| 76 |
|
| 77 |
c_stream.next_in = (z_const uint8_t *)read_buf; |
| 78 |
c_stream.avail_in = read; |
| 79 |
|
| 80 |
do { |
| 81 |
err = PREFIX(deflate)(&c_stream, flush); |
| 82 |
if (err == Z_STREAM_END) break; |
| 83 |
CHECK_ERR(err, "deflate"); |
| 84 |
|
| 85 |
if (c_stream.next_out == write_buf + write_buf_size) { |
| 86 |
fwrite(write_buf, 1, write_buf_size, fout); |
| 87 |
c_stream.next_out = write_buf; |
| 88 |
c_stream.avail_out = write_buf_size; |
| 89 |
} |
| 90 |
} while (c_stream.next_in < read_buf + read); |
| 91 |
} while (err == Z_OK); |
| 92 |
|
| 93 |
|
| 94 |
if (flush != Z_FINISH) { |
| 95 |
c_stream.avail_in = 0; |
| 96 |
do { |
| 97 |
if (c_stream.next_out == write_buf + write_buf_size) { |
| 98 |
fwrite(write_buf, 1, write_buf_size, fout); |
| 99 |
c_stream.next_out = write_buf; |
| 100 |
c_stream.avail_out = write_buf_size; |
| 101 |
} |
| 102 |
|
| 103 |
err = PREFIX(deflate)(&c_stream, Z_FINISH); |
| 104 |
if (err == Z_STREAM_END) break; |
| 105 |
CHECK_ERR(err, "deflate"); |
| 106 |
} while (1); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if (c_stream.next_out != write_buf) { |
| 111 |
fwrite(write_buf, 1, c_stream.next_out - write_buf, fout); |
| 112 |
} |
| 113 |
|
| 114 |
err = PREFIX(deflateEnd)(&c_stream); |
| 115 |
CHECK_ERR(err, "deflateEnd"); |
| 116 |
|
| 117 |
free(read_buf); |
| 118 |
free(write_buf); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void inflate_params(FILE *fin, FILE *fout, int32_t read_buf_size, int32_t write_buf_size, int32_t window_bits, |
| 125 |
int32_t flush) { |
| 126 |
PREFIX3(stream) d_stream; |
| 127 |
uint8_t *read_buf; |
| 128 |
uint8_t *write_buf; |
| 129 |
int32_t read; |
| 130 |
int err; |
| 131 |
|
| 132 |
|
| 133 |
read_buf = (uint8_t *)malloc(read_buf_size); |
| 134 |
if (read_buf == NULL) { |
| 135 |
fprintf(stderr, "failed to create read buffer (%d)\n", read_buf_size); |
| 136 |
return; |
| 137 |
} |
| 138 |
write_buf = (uint8_t *)malloc(write_buf_size); |
| 139 |
if (write_buf == NULL) { |
| 140 |
fprintf(stderr, "failed to create write buffer (%d)\n", write_buf_size); |
| 141 |
free(read_buf); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
d_stream.zalloc = NULL; |
| 146 |
d_stream.zfree = NULL; |
| 147 |
d_stream.opaque = (void *)0; |
| 148 |
d_stream.total_in = 0; |
| 149 |
d_stream.total_out = 0; |
| 150 |
d_stream.next_out = write_buf; |
| 151 |
d_stream.avail_out = write_buf_size; |
| 152 |
|
| 153 |
err = PREFIX(inflateInit2)(&d_stream, window_bits); |
| 154 |
CHECK_ERR(err, "inflateInit2"); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
do { |
| 159 |
read = (int32_t)fread(read_buf, 1, read_buf_size, fin); |
| 160 |
if (read <= 0) |
| 161 |
break; |
| 162 |
|
| 163 |
d_stream.next_in = (z_const uint8_t *)read_buf; |
| 164 |
d_stream.avail_in = read; |
| 165 |
|
| 166 |
do { |
| 167 |
err = PREFIX(inflate)(&d_stream, flush); |
| 168 |
if (err == Z_STREAM_END) break; |
| 169 |
CHECK_ERR(err, "inflate"); |
| 170 |
|
| 171 |
if (d_stream.next_out == write_buf + write_buf_size) { |
| 172 |
fwrite(write_buf, 1, write_buf_size, fout); |
| 173 |
d_stream.next_out = write_buf; |
| 174 |
d_stream.avail_out = write_buf_size; |
| 175 |
} |
| 176 |
} while (d_stream.next_in < read_buf + read); |
| 177 |
} while (err == Z_OK); |
| 178 |
|
| 179 |
|
| 180 |
if (flush != Z_FINISH) { |
| 181 |
d_stream.avail_in = 0; |
| 182 |
do { |
| 183 |
if (d_stream.next_out == write_buf + write_buf_size) { |
| 184 |
fwrite(write_buf, 1, write_buf_size, fout); |
| 185 |
d_stream.next_out = write_buf; |
| 186 |
d_stream.avail_out = write_buf_size; |
| 187 |
} |
| 188 |
|
| 189 |
err = PREFIX(inflate)(&d_stream, Z_FINISH); |
| 190 |
if (err == Z_STREAM_END) break; |
| 191 |
CHECK_ERR(err, "inflate"); |
| 192 |
} while (1); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
if (d_stream.next_out != write_buf) { |
| 197 |
fwrite(write_buf, 1, d_stream.next_out - write_buf, fout); |
| 198 |
} |
| 199 |
|
| 200 |
err = PREFIX(inflateEnd)(&d_stream); |
| 201 |
CHECK_ERR(err, "inflateEnd"); |
| 202 |
|
| 203 |
free(read_buf); |
| 204 |
free(write_buf); |
| 205 |
} |
| 206 |
|
| 207 |
void show_help(void) { |
| 208 |
printf("Usage: minideflate [-c][-d][-k] [-f|-h|-R|-F] [-m level] [-r/-t size] [-s flush] [-w bits] [-0 to -9] [input file]\n\n" \ |
| 209 |
" -c : write to standard output\n" \ |
| 210 |
" -d : decompress\n" \ |
| 211 |
" -k : keep input file\n" \ |
| 212 |
" -f : compress with Z_FILTERED\n" \ |
| 213 |
" -h : compress with Z_HUFFMAN_ONLY\n" \ |
| 214 |
" -R : compress with Z_RLE\n" \ |
| 215 |
" -F : compress with Z_FIXED\n" \ |
| 216 |
" -m : memory level (1 to 8)\n" \ |
| 217 |
" -w : window bits..\n" \ |
| 218 |
" : -1 to -15 for raw deflate\n" |
| 219 |
" : 0 to 15 for deflate (adler32)\n" |
| 220 |
" : 16 to 31 for gzip (crc32)\n" |
| 221 |
" -s : flush type (0 to 5)\n" \ |
| 222 |
" -r : read buffer size\n" \ |
| 223 |
" -t : write buffer size\n" \ |
| 224 |
" -0 to -9 : compression level\n\n"); |
| 225 |
} |
| 226 |
|
| 227 |
int main(int argc, char **argv) { |
| 228 |
int32_t i; |
| 229 |
int32_t mem_level = DEF_MEM_LEVEL; |
| 230 |
int32_t window_bits = INT32_MAX; |
| 231 |
int32_t strategy = Z_DEFAULT_STRATEGY; |
| 232 |
int32_t level = Z_DEFAULT_COMPRESSION; |
| 233 |
int32_t read_buf_size = BUFSIZE; |
| 234 |
int32_t write_buf_size = BUFSIZE; |
| 235 |
int32_t flush = Z_NO_FLUSH; |
| 236 |
uint8_t copyout = 0; |
| 237 |
uint8_t uncompr = 0; |
| 238 |
uint8_t keep = 0; |
| 239 |
FILE *fin = stdin; |
| 240 |
FILE *fout = stdout; |
| 241 |
|
| 242 |
|
| 243 |
if (argc == 1) { |
| 244 |
show_help(); |
| 245 |
return 64; |
| 246 |
} |
| 247 |
|
| 248 |
for (i = 1; i < argc; i++) { |
| 249 |
if ((strcmp(argv[i], "-m") == 0) && (i + 1 < argc)) |
| 250 |
mem_level = atoi(argv[++i]); |
| 251 |
else if ((strcmp(argv[i], "-w") == 0) && (i + 1 < argc)) |
| 252 |
window_bits = atoi(argv[++i]); |
| 253 |
else if ((strcmp(argv[i], "-r") == 0) && (i + 1 < argc)) |
| 254 |
read_buf_size = atoi(argv[++i]); |
| 255 |
else if ((strcmp(argv[i], "-t") == 0) && (i + 1 < argc)) |
| 256 |
write_buf_size = atoi(argv[++i]); |
| 257 |
else if ((strcmp(argv[i], "-s") == 0) && (i + 1 < argc)) |
| 258 |
flush = atoi(argv[++i]); |
| 259 |
else if (strcmp(argv[i], "-c") == 0) |
| 260 |
copyout = 1; |
| 261 |
else if (strcmp(argv[i], "-d") == 0) |
| 262 |
uncompr = 1; |
| 263 |
else if (strcmp(argv[i], "-k") == 0) |
| 264 |
keep = 1; |
| 265 |
else if (strcmp(argv[i], "-f") == 0) |
| 266 |
strategy = Z_FILTERED; |
| 267 |
else if (strcmp(argv[i], "-F") == 0) |
| 268 |
strategy = Z_FIXED; |
| 269 |
else if (strcmp(argv[i], "-h") == 0) |
| 270 |
strategy = Z_HUFFMAN_ONLY; |
| 271 |
else if (strcmp(argv[i], "-R") == 0) |
| 272 |
strategy = Z_RLE; |
| 273 |
else if (argv[i][0] == '-' && argv[i][1] >= '0' && argv[i][1] <= '9' && argv[i][2] == 0) |
| 274 |
level = argv[i][1] - '0'; |
| 275 |
else if (strcmp(argv[i], "--help") == 0) { |
| 276 |
show_help(); |
| 277 |
return 0; |
| 278 |
} else if (argv[i][0] == '-') { |
| 279 |
show_help(); |
| 280 |
return 64; |
| 281 |
} else |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
SET_BINARY_MODE(stdin); |
| 286 |
SET_BINARY_MODE(stdout); |
| 287 |
|
| 288 |
if (i != argc) { |
| 289 |
fin = fopen(argv[i], "rb+"); |
| 290 |
if (fin == NULL) { |
| 291 |
fprintf(stderr, "Failed to open file: %s\n", argv[i]); |
| 292 |
exit(1); |
| 293 |
} |
| 294 |
if (!copyout) { |
| 295 |
char *out_file = (char *)calloc(1, strlen(argv[i]) + 6); |
| 296 |
if (out_file == NULL) { |
| 297 |
fprintf(stderr, "Not enough memory\n"); |
| 298 |
exit(1); |
| 299 |
} |
| 300 |
strcat(out_file, argv[i]); |
| 301 |
if (!uncompr) { |
| 302 |
if (window_bits < 0) { |
| 303 |
strcat(out_file, ".zraw"); |
| 304 |
} else if (window_bits > MAX_WBITS) { |
| 305 |
strcat(out_file, ".gz"); |
| 306 |
} else { |
| 307 |
strcat(out_file, ".z"); |
| 308 |
} |
| 309 |
} else { |
| 310 |
char *out_ext = strrchr(out_file, '.'); |
| 311 |
if (out_ext != NULL) { |
| 312 |
if (strcasecmp(out_ext, ".zraw") == 0 && window_bits == INT32_MAX) { |
| 313 |
fprintf(stderr, "Must specify window bits for raw deflate stream\n"); |
| 314 |
exit(1); |
| 315 |
} |
| 316 |
*out_ext = 0; |
| 317 |
} |
| 318 |
} |
| 319 |
fout = fopen(out_file, "wb"); |
| 320 |
if (fout == NULL) { |
| 321 |
fprintf(stderr, "Failed to open file: %s\n", out_file); |
| 322 |
exit(1); |
| 323 |
} |
| 324 |
free(out_file); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (window_bits == INT32_MAX) { |
| 329 |
window_bits = MAX_WBITS; |
| 330 |
|
| 331 |
if (uncompr) |
| 332 |
window_bits += 32; |
| 333 |
} |
| 334 |
|
| 335 |
if (window_bits == INT32_MAX) { |
| 336 |
window_bits = MAX_WBITS; |
| 337 |
|
| 338 |
if (uncompr) |
| 339 |
window_bits += 32; |
| 340 |
} |
| 341 |
|
| 342 |
if (uncompr) { |
| 343 |
inflate_params(fin, fout, read_buf_size, write_buf_size, window_bits, flush); |
| 344 |
} else { |
| 345 |
deflate_params(fin, fout, read_buf_size, write_buf_size, level, window_bits, mem_level, strategy, flush); |
| 346 |
} |
| 347 |
|
| 348 |
if (fin != stdin) { |
| 349 |
fclose(fin); |
| 350 |
if (!copyout && !keep) { |
| 351 |
unlink(argv[i]); |
| 352 |
} |
| 353 |
} |
| 354 |
if (fout != stdout) { |
| 355 |
fclose(fout); |
| 356 |
} |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |