minigzip.c (e37bb444aa945ed0725766e986698a09bd61b1b2) | minigzip.c (4717628ed859513a3262ea68259d0605f39de0b3) |
---|---|
1/* minigzip.c -- simulate gzip using the zlib compression library 2 * Copyright (C) 1995-2006, 2010, 2011, 2016 Jean-loup Gailly 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* 7 * minigzip is a minimal implementation of the gzip utility. This is 8 * only an example of using zlib and isn't meant to replace the --- 45 unchanged lines hidden (view full) --- 54# define fileno(file) file->__file 55#endif 56#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 57# include <unix.h> /* for fileno */ 58#endif 59 60#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) 61#ifndef WIN32 /* unlink already in stdio.h for WIN32 */ | 1/* minigzip.c -- simulate gzip using the zlib compression library 2 * Copyright (C) 1995-2006, 2010, 2011, 2016 Jean-loup Gailly 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* 7 * minigzip is a minimal implementation of the gzip utility. This is 8 * only an example of using zlib and isn't meant to replace the --- 45 unchanged lines hidden (view full) --- 54# define fileno(file) file->__file 55#endif 56#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 57# include <unix.h> /* for fileno */ 58#endif 59 60#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) 61#ifndef WIN32 /* unlink already in stdio.h for WIN32 */ |
62 extern int unlink OF((const char *)); | 62 extern int unlink(const char *); |
63#endif 64#endif 65 66#if defined(UNDER_CE) 67# include <windows.h> 68# define perror(s) pwinerror(s) 69 70/* Map the Windows error number in ERROR to a locale-dependent error --- 73 unchanged lines hidden (view full) --- 144 145#ifdef Z_SOLO 146/* for Z_SOLO, create simplified gz* functions using deflate and inflate */ 147 148#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) 149# include <unistd.h> /* for unlink() */ 150#endif 151 | 63#endif 64#endif 65 66#if defined(UNDER_CE) 67# include <windows.h> 68# define perror(s) pwinerror(s) 69 70/* Map the Windows error number in ERROR to a locale-dependent error --- 73 unchanged lines hidden (view full) --- 144 145#ifdef Z_SOLO 146/* for Z_SOLO, create simplified gz* functions using deflate and inflate */ 147 148#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) 149# include <unistd.h> /* for unlink() */ 150#endif 151 |
152void *myalloc OF((void *, unsigned, unsigned)); 153void myfree OF((void *, void *)); 154 155void *myalloc(q, n, m) 156 void *q; 157 unsigned n, m; 158{ | 152void *myalloc(void *q, unsigned n, unsigned m) { |
159 (void)q; 160 return calloc(n, m); 161} 162 | 153 (void)q; 154 return calloc(n, m); 155} 156 |
163void myfree(q, p) 164 void *q, *p; 165{ | 157void myfree(void *q, void *p) { |
166 (void)q; 167 free(p); 168} 169 170typedef struct gzFile_s { 171 FILE *file; 172 int write; 173 int err; 174 char *msg; 175 z_stream strm; 176} *gzFile; 177 | 158 (void)q; 159 free(p); 160} 161 162typedef struct gzFile_s { 163 FILE *file; 164 int write; 165 int err; 166 char *msg; 167 z_stream strm; 168} *gzFile; 169 |
178gzFile gzopen OF((const char *, const char *)); 179gzFile gzdopen OF((int, const char *)); 180gzFile gz_open OF((const char *, int, const char *)); 181 182gzFile gzopen(path, mode) 183const char *path; 184const char *mode; 185{ 186 return gz_open(path, -1, mode); 187} 188 189gzFile gzdopen(fd, mode) 190int fd; 191const char *mode; 192{ 193 return gz_open(NULL, fd, mode); 194} 195 196gzFile gz_open(path, fd, mode) 197 const char *path; 198 int fd; 199 const char *mode; 200{ | 170gzFile gz_open(const char *path, int fd, const char *mode) { |
201 gzFile gz; 202 int ret; 203 204 gz = malloc(sizeof(struct gzFile_s)); 205 if (gz == NULL) 206 return NULL; 207 gz->write = strchr(mode, 'w') != NULL; 208 gz->strm.zalloc = myalloc; --- 17 unchanged lines hidden (view full) --- 226 free(gz); 227 return NULL; 228 } 229 gz->err = 0; 230 gz->msg = ""; 231 return gz; 232} 233 | 171 gzFile gz; 172 int ret; 173 174 gz = malloc(sizeof(struct gzFile_s)); 175 if (gz == NULL) 176 return NULL; 177 gz->write = strchr(mode, 'w') != NULL; 178 gz->strm.zalloc = myalloc; --- 17 unchanged lines hidden (view full) --- 196 free(gz); 197 return NULL; 198 } 199 gz->err = 0; 200 gz->msg = ""; 201 return gz; 202} 203 |
234int gzwrite OF((gzFile, const void *, unsigned)); | 204gzFile gzopen(const char *path, const char *mode) { 205 return gz_open(path, -1, mode); 206} |
235 | 207 |
236int gzwrite(gz, buf, len) 237 gzFile gz; 238 const void *buf; 239 unsigned len; 240{ | 208gzFile gzdopen(int fd, const char *mode) { 209 return gz_open(NULL, fd, mode); 210} 211 212int gzwrite(gzFile gz, const void *buf, unsigned len) { |
241 z_stream *strm; 242 unsigned char out[BUFLEN]; 243 244 if (gz == NULL || !gz->write) 245 return 0; 246 strm = &(gz->strm); 247 strm->next_in = (void *)buf; 248 strm->avail_in = len; 249 do { 250 strm->next_out = out; 251 strm->avail_out = BUFLEN; 252 (void)deflate(strm, Z_NO_FLUSH); 253 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); 254 } while (strm->avail_out == 0); 255 return len; 256} 257 | 213 z_stream *strm; 214 unsigned char out[BUFLEN]; 215 216 if (gz == NULL || !gz->write) 217 return 0; 218 strm = &(gz->strm); 219 strm->next_in = (void *)buf; 220 strm->avail_in = len; 221 do { 222 strm->next_out = out; 223 strm->avail_out = BUFLEN; 224 (void)deflate(strm, Z_NO_FLUSH); 225 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); 226 } while (strm->avail_out == 0); 227 return len; 228} 229 |
258int gzread OF((gzFile, void *, unsigned)); 259 260int gzread(gz, buf, len) 261 gzFile gz; 262 void *buf; 263 unsigned len; 264{ | 230int gzread(gzFile gz, void *buf, unsigned len) { |
265 int ret; 266 unsigned got; 267 unsigned char in[1]; 268 z_stream *strm; 269 270 if (gz == NULL || gz->write) 271 return 0; 272 if (gz->err) --- 14 unchanged lines hidden (view full) --- 287 return 0; 288 } 289 if (ret == Z_STREAM_END) 290 inflateReset(strm); 291 } while (strm->avail_out); 292 return len - strm->avail_out; 293} 294 | 231 int ret; 232 unsigned got; 233 unsigned char in[1]; 234 z_stream *strm; 235 236 if (gz == NULL || gz->write) 237 return 0; 238 if (gz->err) --- 14 unchanged lines hidden (view full) --- 253 return 0; 254 } 255 if (ret == Z_STREAM_END) 256 inflateReset(strm); 257 } while (strm->avail_out); 258 return len - strm->avail_out; 259} 260 |
295int gzclose OF((gzFile)); 296 297int gzclose(gz) 298 gzFile gz; 299{ | 261int gzclose(gzFile gz) { |
300 z_stream *strm; 301 unsigned char out[BUFLEN]; 302 303 if (gz == NULL) 304 return Z_STREAM_ERROR; 305 strm = &(gz->strm); 306 if (gz->write) { 307 strm->next_in = Z_NULL; --- 8 unchanged lines hidden (view full) --- 316 } 317 else 318 inflateEnd(strm); 319 fclose(gz->file); 320 free(gz); 321 return Z_OK; 322} 323 | 262 z_stream *strm; 263 unsigned char out[BUFLEN]; 264 265 if (gz == NULL) 266 return Z_STREAM_ERROR; 267 strm = &(gz->strm); 268 if (gz->write) { 269 strm->next_in = Z_NULL; --- 8 unchanged lines hidden (view full) --- 278 } 279 else 280 inflateEnd(strm); 281 fclose(gz->file); 282 free(gz); 283 return Z_OK; 284} 285 |
324const char *gzerror OF((gzFile, int *)); 325 326const char *gzerror(gz, err) 327 gzFile gz; 328 int *err; 329{ | 286const char *gzerror(gzFile gz, int *err) { |
330 *err = gz->err; 331 return gz->msg; 332} 333 334#endif 335 336static char *prog; 337 | 287 *err = gz->err; 288 return gz->msg; 289} 290 291#endif 292 293static char *prog; 294 |
338void error OF((const char *msg)); 339void gz_compress OF((FILE *in, gzFile out)); 340#ifdef USE_MMAP 341int gz_compress_mmap OF((FILE *in, gzFile out)); 342#endif 343void gz_uncompress OF((gzFile in, FILE *out)); 344void file_compress OF((char *file, char *mode)); 345void file_uncompress OF((char *file)); 346int main OF((int argc, char *argv[])); 347 | |
348/* =========================================================================== 349 * Display error message and exit 350 */ | 295/* =========================================================================== 296 * Display error message and exit 297 */ |
351void error(msg) 352 const char *msg; 353{ | 298void error(const char *msg) { |
354 fprintf(stderr, "%s: %s\n", prog, msg); 355 exit(1); 356} 357 | 299 fprintf(stderr, "%s: %s\n", prog, msg); 300 exit(1); 301} 302 |
358/* =========================================================================== 359 * Compress input to output then close both files. 360 */ 361 362void gz_compress(in, out) 363 FILE *in; 364 gzFile out; 365{ 366 local char buf[BUFLEN]; 367 int len; 368 int err; 369 370#ifdef USE_MMAP 371 /* Try first compressing with mmap. If mmap fails (minigzip used in a 372 * pipe), use the normal fread loop. 373 */ 374 if (gz_compress_mmap(in, out) == Z_OK) return; 375#endif 376 for (;;) { 377 len = (int)fread(buf, 1, sizeof(buf), in); 378 if (ferror(in)) { 379 perror("fread"); 380 exit(1); 381 } 382 if (len == 0) break; 383 384 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); 385 } 386 fclose(in); 387 if (gzclose(out) != Z_OK) error("failed gzclose"); 388} 389 | |
390#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */ 391 392/* Try compressing the input file at once using mmap. Return Z_OK if 393 * if success, Z_ERRNO otherwise. 394 */ | 303#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */ 304 305/* Try compressing the input file at once using mmap. Return Z_OK if 306 * if success, Z_ERRNO otherwise. 307 */ |
395int gz_compress_mmap(in, out) 396 FILE *in; 397 gzFile out; 398{ | 308int gz_compress_mmap(FILE *in, gzFile out) { |
399 int len; 400 int err; 401 int ifd = fileno(in); 402 caddr_t buf; /* mmap'ed buffer for the entire input file */ 403 off_t buf_len; /* length of the input file */ 404 struct stat sb; 405 406 /* Determine the size of the file, needed for mmap: */ --- 13 unchanged lines hidden (view full) --- 420 munmap(buf, buf_len); 421 fclose(in); 422 if (gzclose(out) != Z_OK) error("failed gzclose"); 423 return Z_OK; 424} 425#endif /* USE_MMAP */ 426 427/* =========================================================================== | 309 int len; 310 int err; 311 int ifd = fileno(in); 312 caddr_t buf; /* mmap'ed buffer for the entire input file */ 313 off_t buf_len; /* length of the input file */ 314 struct stat sb; 315 316 /* Determine the size of the file, needed for mmap: */ --- 13 unchanged lines hidden (view full) --- 330 munmap(buf, buf_len); 331 fclose(in); 332 if (gzclose(out) != Z_OK) error("failed gzclose"); 333 return Z_OK; 334} 335#endif /* USE_MMAP */ 336 337/* =========================================================================== |
338 * Compress input to output then close both files. 339 */ 340 341void gz_compress(FILE *in, gzFile out) { 342 local char buf[BUFLEN]; 343 int len; 344 int err; 345 346#ifdef USE_MMAP 347 /* Try first compressing with mmap. If mmap fails (minigzip used in a 348 * pipe), use the normal fread loop. 349 */ 350 if (gz_compress_mmap(in, out) == Z_OK) return; 351#endif 352 for (;;) { 353 len = (int)fread(buf, 1, sizeof(buf), in); 354 if (ferror(in)) { 355 perror("fread"); 356 exit(1); 357 } 358 if (len == 0) break; 359 360 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); 361 } 362 fclose(in); 363 if (gzclose(out) != Z_OK) error("failed gzclose"); 364} 365 366/* =========================================================================== |
|
428 * Uncompress input to output then close both files. 429 */ | 367 * Uncompress input to output then close both files. 368 */ |
430void gz_uncompress(in, out) 431 gzFile in; 432 FILE *out; 433{ | 369void gz_uncompress(gzFile in, FILE *out) { |
434 local char buf[BUFLEN]; 435 int len; 436 int err; 437 438 for (;;) { 439 len = gzread(in, buf, sizeof(buf)); 440 if (len < 0) error (gzerror(in, &err)); 441 if (len == 0) break; --- 7 unchanged lines hidden (view full) --- 449 if (gzclose(in) != Z_OK) error("failed gzclose"); 450} 451 452 453/* =========================================================================== 454 * Compress the given file: create a corresponding .gz file and remove the 455 * original. 456 */ | 370 local char buf[BUFLEN]; 371 int len; 372 int err; 373 374 for (;;) { 375 len = gzread(in, buf, sizeof(buf)); 376 if (len < 0) error (gzerror(in, &err)); 377 if (len == 0) break; --- 7 unchanged lines hidden (view full) --- 385 if (gzclose(in) != Z_OK) error("failed gzclose"); 386} 387 388 389/* =========================================================================== 390 * Compress the given file: create a corresponding .gz file and remove the 391 * original. 392 */ |
457void file_compress(file, mode) 458 char *file; 459 char *mode; 460{ | 393void file_compress(char *file, char *mode) { |
461 local char outfile[MAX_NAME_LEN]; 462 FILE *in; 463 gzFile out; 464 465 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { 466 fprintf(stderr, "%s: filename too long\n", prog); 467 exit(1); 468 } --- 19 unchanged lines hidden (view full) --- 488 489 unlink(file); 490} 491 492 493/* =========================================================================== 494 * Uncompress the given file and remove the original. 495 */ | 394 local char outfile[MAX_NAME_LEN]; 395 FILE *in; 396 gzFile out; 397 398 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { 399 fprintf(stderr, "%s: filename too long\n", prog); 400 exit(1); 401 } --- 19 unchanged lines hidden (view full) --- 421 422 unlink(file); 423} 424 425 426/* =========================================================================== 427 * Uncompress the given file and remove the original. 428 */ |
496void file_uncompress(file) 497 char *file; 498{ | 429void file_uncompress(char *file) { |
499 local char buf[MAX_NAME_LEN]; 500 char *infile, *outfile; 501 FILE *out; 502 gzFile in; 503 z_size_t len = strlen(file); 504 505 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) { 506 fprintf(stderr, "%s: filename too long\n", prog); --- 41 unchanged lines hidden (view full) --- 548 * -c : write to standard output 549 * -d : decompress 550 * -f : compress with Z_FILTERED 551 * -h : compress with Z_HUFFMAN_ONLY 552 * -r : compress with Z_RLE 553 * -1 to -9 : compression level 554 */ 555 | 430 local char buf[MAX_NAME_LEN]; 431 char *infile, *outfile; 432 FILE *out; 433 gzFile in; 434 z_size_t len = strlen(file); 435 436 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) { 437 fprintf(stderr, "%s: filename too long\n", prog); --- 41 unchanged lines hidden (view full) --- 479 * -c : write to standard output 480 * -d : decompress 481 * -f : compress with Z_FILTERED 482 * -h : compress with Z_HUFFMAN_ONLY 483 * -r : compress with Z_RLE 484 * -1 to -9 : compression level 485 */ 486 |
556int main(argc, argv) 557 int argc; 558 char *argv[]; 559{ | 487int main(int argc, char *argv[]) { |
560 int copyout = 0; 561 int uncompr = 0; 562 gzFile file; 563 char *bname, outmode[20]; 564 565#if !defined(NO_snprintf) && !defined(NO_vsnprintf) 566 snprintf(outmode, sizeof(outmode), "%s", "wb6 "); 567#else --- 84 unchanged lines hidden --- | 488 int copyout = 0; 489 int uncompr = 0; 490 gzFile file; 491 char *bname, outmode[20]; 492 493#if !defined(NO_snprintf) && !defined(NO_vsnprintf) 494 snprintf(outmode, sizeof(outmode), "%s", "wb6 "); 495#else --- 84 unchanged lines hidden --- |