1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-2006, 2010, 2011 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
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
9 * full-featured gzip. No attempt is made to deal with file systems
10 * limiting names to 14 or 8+3 characters, etc... Error checking is
11 * very limited. So use minigzip only for testing; use gzip for the
12 * real thing. On MSDOS, use only on file names without extension
27 # include <sys/types.h>
28 # include <sys/mman.h>
29 # include <sys/stat.h>
32 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
38 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
40 # define SET_BINARY_MODE(file)
44 # define snprintf _snprintf
48 # define unlink delete
49 # define GZ_SUFFIX "-gz"
52 # define unlink remove
53 # define GZ_SUFFIX "-gz"
54 # define fileno(file) file->__file
56 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
57 # include <unix.h> /* for fileno */
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 *));
68 # define perror(s) pwinerror(s)
70 /* Map the Windows error number in ERROR to a locale-dependent error
71 message string and return a pointer to it. Typically, the values
72 for ERROR come from GetLastError.
74 The string pointed to shall not be modified by the application,
75 but may be overwritten by a subsequent call to strwinerror
77 The strwinerror function does not change the current setting
80 static char *strwinerror (error
)
83 static char buf
[1024];
86 DWORD lasterr
= GetLastError();
87 DWORD chars
= FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
88 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
91 0, /* Default language */
96 /* If there is an \r\n appended, zap it. */
98 && msgbuf
[chars
- 2] == '\r' && msgbuf
[chars
- 1] == '\n') {
103 if (chars
> sizeof (buf
) - 1) {
104 chars
= sizeof (buf
) - 1;
108 wcstombs(buf
, msgbuf
, chars
+ 1);
112 sprintf(buf
, "unknown win32 error (%ld)", error
);
115 SetLastError(lasterr
);
119 static void pwinerror (s
)
123 fprintf(stderr
, "%s: %s\n", s
, strwinerror(GetLastError ()));
125 fprintf(stderr
, "%s\n", strwinerror(GetLastError ()));
128 #endif /* UNDER_CE */
131 # define GZ_SUFFIX ".gz"
133 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
136 #define MAX_NAME_LEN 1024
139 # define local static
140 /* Needed for systems with limitation on stack size. */
146 /* for Z_SOLO, create simplified gz* functions using deflate and inflate */
148 #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
149 # include <unistd.h> /* for unlink() */
152 void *myalloc
OF((void *, unsigned, unsigned));
153 void myfree
OF((void *, void *));
155 void *myalloc(q
, n
, m
)
170 typedef struct gzFile_s
{
178 gzFile gzopen
OF((const char *, const char *));
179 gzFile gzdopen
OF((int, const char *));
180 gzFile gz_open
OF((const char *, int, const char *));
182 gzFile
gzopen(path
, mode
)
186 return gz_open(path
, -1, mode
);
189 gzFile
gzdopen(fd
, mode
)
193 return gz_open(NULL
, fd
, mode
);
196 gzFile
gz_open(path
, fd
, mode
)
204 gz
= malloc(sizeof(struct gzFile_s
));
207 gz
->write
= strchr(mode
, 'w') != NULL
;
208 gz
->strm
.zalloc
= myalloc
;
209 gz
->strm
.zfree
= myfree
;
210 gz
->strm
.opaque
= Z_NULL
;
212 ret
= deflateInit2(&(gz
->strm
), -1, 8, 15 + 16, 8, 0);
214 gz
->strm
.next_in
= 0;
215 gz
->strm
.avail_in
= Z_NULL
;
216 ret
= inflateInit2(&(gz
->strm
), 15 + 16);
222 gz
->file
= path
== NULL
? fdopen(fd
, gz
->write
? "wb" : "rb") :
223 fopen(path
, gz
->write
? "wb" : "rb");
224 if (gz
->file
== NULL
) {
225 gz
->write
? deflateEnd(&(gz
->strm
)) : inflateEnd(&(gz
->strm
));
234 int gzwrite
OF((gzFile
, const void *, unsigned));
236 int gzwrite(gz
, buf
, len
)
242 unsigned char out
[BUFLEN
];
244 if (gz
== NULL
|| !gz
->write
)
247 strm
->next_in
= (void *)buf
;
248 strm
->avail_in
= len
;
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);
258 int gzread
OF((gzFile
, void *, unsigned));
260 int gzread(gz
, buf
, len
)
270 if (gz
== NULL
|| gz
->write
)
275 strm
->next_out
= (void *)buf
;
276 strm
->avail_out
= len
;
278 got
= fread(in
, 1, 1, gz
->file
);
283 ret
= inflate(strm
, Z_NO_FLUSH
);
284 if (ret
== Z_DATA_ERROR
) {
285 gz
->err
= Z_DATA_ERROR
;
289 if (ret
== Z_STREAM_END
)
291 } while (strm
->avail_out
);
292 return len
- strm
->avail_out
;
295 int gzclose
OF((gzFile
));
301 unsigned char out
[BUFLEN
];
304 return Z_STREAM_ERROR
;
307 strm
->next_in
= Z_NULL
;
310 strm
->next_out
= out
;
311 strm
->avail_out
= BUFLEN
;
312 (void)deflate(strm
, Z_FINISH
);
313 fwrite(out
, 1, BUFLEN
- strm
->avail_out
, gz
->file
);
314 } while (strm
->avail_out
== 0);
324 const char *gzerror
OF((gzFile
, int *));
326 const char *gzerror(gz
, err
)
338 void error
OF((const char *msg
));
339 void gz_compress
OF((FILE *in
, gzFile out
));
341 int gz_compress_mmap
OF((FILE *in
, gzFile out
));
343 void gz_uncompress
OF((gzFile in
, FILE *out
));
344 void file_compress
OF((char *file
, char *mode
));
345 void file_uncompress
OF((char *file
));
346 int main
OF((int argc
, char *argv
[]));
348 /* ===========================================================================
349 * Display error message and exit
354 fprintf(stderr
, "%s: %s\n", prog
, msg
);
358 /* ===========================================================================
359 * Compress input to output then close both files.
362 void gz_compress(in
, out
)
366 local
char buf
[BUFLEN
];
371 /* Try first compressing with mmap. If mmap fails (minigzip used in a
372 * pipe), use the normal fread loop.
374 if (gz_compress_mmap(in
, out
) == Z_OK
) return;
377 len
= (int)fread(buf
, 1, sizeof(buf
), in
);
384 if (gzwrite(out
, buf
, (unsigned)len
) != len
) error(gzerror(out
, &err
));
387 if (gzclose(out
) != Z_OK
) error("failed gzclose");
390 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
392 /* Try compressing the input file at once using mmap. Return Z_OK if
393 * if success, Z_ERRNO otherwise.
395 int gz_compress_mmap(in
, out
)
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 */
406 /* Determine the size of the file, needed for mmap: */
407 if (fstat(ifd
, &sb
) < 0) return Z_ERRNO
;
408 buf_len
= sb
.st_size
;
409 if (buf_len
<= 0) return Z_ERRNO
;
411 /* Now do the actual mmap: */
412 buf
= mmap((caddr_t
) 0, buf_len
, PROT_READ
, MAP_SHARED
, ifd
, (off_t
)0);
413 if (buf
== (caddr_t
)(-1)) return Z_ERRNO
;
415 /* Compress the whole file at once: */
416 len
= gzwrite(out
, (char *)buf
, (unsigned)buf_len
);
418 if (len
!= (int)buf_len
) error(gzerror(out
, &err
));
420 munmap(buf
, buf_len
);
422 if (gzclose(out
) != Z_OK
) error("failed gzclose");
425 #endif /* USE_MMAP */
427 /* ===========================================================================
428 * Uncompress input to output then close both files.
430 void gz_uncompress(in
, out
)
434 local
char buf
[BUFLEN
];
439 len
= gzread(in
, buf
, sizeof(buf
));
440 if (len
< 0) error (gzerror(in
, &err
));
443 if ((int)fwrite(buf
, 1, (unsigned)len
, out
) != len
) {
444 error("failed fwrite");
447 if (fclose(out
)) error("failed fclose");
449 if (gzclose(in
) != Z_OK
) error("failed gzclose");
453 /* ===========================================================================
454 * Compress the given file: create a corresponding .gz file and remove the
457 void file_compress(file
, mode
)
461 local
char outfile
[MAX_NAME_LEN
];
465 if (strlen(file
) + strlen(GZ_SUFFIX
) >= sizeof(outfile
)) {
466 fprintf(stderr
, "%s: filename too long\n", prog
);
470 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
471 snprintf(outfile
, sizeof(outfile
), "%s%s", file
, GZ_SUFFIX
);
473 strcpy(outfile
, file
);
474 strcat(outfile
, GZ_SUFFIX
);
477 in
= fopen(file
, "rb");
482 out
= gzopen(outfile
, mode
);
484 fprintf(stderr
, "%s: can't gzopen %s\n", prog
, outfile
);
487 gz_compress(in
, out
);
493 /* ===========================================================================
494 * Uncompress the given file and remove the original.
496 void file_uncompress(file
)
499 local
char buf
[MAX_NAME_LEN
];
500 char *infile
, *outfile
;
503 size_t len
= strlen(file
);
505 if (len
+ strlen(GZ_SUFFIX
) >= sizeof(buf
)) {
506 fprintf(stderr
, "%s: filename too long\n", prog
);
510 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
511 snprintf(buf
, sizeof(buf
), "%s", file
);
516 if (len
> SUFFIX_LEN
&& strcmp(file
+len
-SUFFIX_LEN
, GZ_SUFFIX
) == 0) {
519 outfile
[len
-3] = '\0';
523 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
524 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", GZ_SUFFIX
);
526 strcat(infile
, GZ_SUFFIX
);
529 in
= gzopen(infile
, "rb");
531 fprintf(stderr
, "%s: can't gzopen %s\n", prog
, infile
);
534 out
= fopen(outfile
, "wb");
540 gz_uncompress(in
, out
);
546 /* ===========================================================================
547 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
548 * -c : write to standard output
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
563 char *bname
, outmode
[20];
565 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
566 snprintf(outmode
, sizeof(outmode
), "%s", "wb6 ");
568 strcpy(outmode
, "wb6 ");
572 bname
= strrchr(argv
[0], '/');
579 if (!strcmp(bname
, "gunzip"))
581 else if (!strcmp(bname
, "zcat"))
582 copyout
= uncompr
= 1;
585 if (strcmp(*argv
, "-c") == 0)
587 else if (strcmp(*argv
, "-d") == 0)
589 else if (strcmp(*argv
, "-f") == 0)
591 else if (strcmp(*argv
, "-h") == 0)
593 else if (strcmp(*argv
, "-r") == 0)
595 else if ((*argv
)[0] == '-' && (*argv
)[1] >= '1' && (*argv
)[1] <= '9' &&
597 outmode
[2] = (*argv
)[1];
602 if (outmode
[3] == ' ')
605 SET_BINARY_MODE(stdin
);
606 SET_BINARY_MODE(stdout
);
608 file
= gzdopen(fileno(stdin
), "rb");
609 if (file
== NULL
) error("can't gzdopen stdin");
610 gz_uncompress(file
, stdout
);
612 file
= gzdopen(fileno(stdout
), outmode
);
613 if (file
== NULL
) error("can't gzdopen stdout");
614 gz_compress(stdin
, file
);
618 SET_BINARY_MODE(stdout
);
623 file
= gzopen(*argv
, "rb");
625 fprintf(stderr
, "%s: can't gzopen %s\n", prog
, *argv
);
627 gz_uncompress(file
, stdout
);
629 file_uncompress(*argv
);
633 FILE * in
= fopen(*argv
, "rb");
638 file
= gzdopen(fileno(stdout
), outmode
);
639 if (file
== NULL
) error("can't gzdopen stdout");
641 gz_compress(in
, file
);
645 file_compress(*argv
, outmode
);
648 } while (argv
++, --argc
);