1
/* minigzip.c -- simulate gzip using the zlib compression library
2
* Copyright (C) 1995-2005 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
16
/* @(#) $Id: minigzip.c 1926 2010-01-29 23:37:33Z stephena $ */
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__)
35
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
37
# define SET_BINARY_MODE(file)
41
# define unlink delete
42
# define GZ_SUFFIX "-gz"
45
# define unlink remove
46
# define GZ_SUFFIX "-gz"
47
# define fileno(file) file->__file
49
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
50
# include <unix.h> /* for fileno */
53
#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
54
extern int unlink OF((const char *));
58
# define GZ_SUFFIX ".gz"
60
#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
63
#define MAX_NAME_LEN 1024
67
/* Needed for systems with limitation on stack size. */
74
void error OF((const char *msg));
75
void gz_compress OF((FILE *in, gzFile out));
77
int gz_compress_mmap OF((FILE *in, gzFile out));
79
void gz_uncompress OF((gzFile in, FILE *out));
80
void file_compress OF((char *file, char *mode));
81
void file_uncompress OF((char *file));
82
int main OF((int argc, char *argv[]));
84
/* ===========================================================================
85
* Display error message and exit
87
void error(const char *msg)
89
fprintf(stderr, "%s: %s\n", prog, msg);
93
/* ===========================================================================
94
* Compress input to output then close both files.
97
void gz_compress(FILE *in, gzFile out)
99
local char buf[BUFLEN];
104
/* Try first compressing with mmap. If mmap fails (minigzip used in a
105
* pipe), use the normal fread loop.
107
if (gz_compress_mmap(in, out) == Z_OK) return;
110
len = (int)fread(buf, 1, sizeof(buf), in);
117
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
120
if (gzclose(out) != Z_OK) error("failed gzclose");
123
#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
125
/* Try compressing the input file at once using mmap. Return Z_OK if
126
* if success, Z_ERRNO otherwise.
128
int gz_compress_mmap(FILE *in, gzFile out)
132
int ifd = fileno(in);
133
caddr_t buf; /* mmap'ed buffer for the entire input file */
134
off_t buf_len; /* length of the input file */
137
/* Determine the size of the file, needed for mmap: */
138
if (fstat(ifd, &sb) < 0) return Z_ERRNO;
139
buf_len = sb.st_size;
140
if (buf_len <= 0) return Z_ERRNO;
142
/* Now do the actual mmap: */
143
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
144
if (buf == (caddr_t)(-1)) return Z_ERRNO;
146
/* Compress the whole file at once: */
147
len = gzwrite(out, (char *)buf, (unsigned)buf_len);
149
if (len != (int)buf_len) error(gzerror(out, &err));
151
munmap(buf, buf_len);
153
if (gzclose(out) != Z_OK) error("failed gzclose");
156
#endif /* USE_MMAP */
158
/* ===========================================================================
159
* Uncompress input to output then close both files.
161
void gz_uncompress(gzFile in, FILE *out)
163
local char buf[BUFLEN];
168
len = gzread(in, buf, sizeof(buf));
169
if (len < 0) error (gzerror(in, &err));
172
if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
173
error("failed fwrite");
176
if (fclose(out)) error("failed fclose");
178
if (gzclose(in) != Z_OK) error("failed gzclose");
182
/* ===========================================================================
183
* Compress the given file: create a corresponding .gz file and remove the
186
void file_compress(char *file, char *mode)
188
local char outfile[MAX_NAME_LEN];
192
strcpy(outfile, file);
193
strcat(outfile, GZ_SUFFIX);
195
in = fopen(file, "rb");
200
out = gzopen(outfile, mode);
202
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
205
gz_compress(in, out);
211
/* ===========================================================================
212
* Uncompress the given file and remove the original.
214
void file_uncompress(char *file)
216
local char buf[MAX_NAME_LEN];
217
char *infile, *outfile;
220
uInt len = (uInt)strlen(file);
224
if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
227
outfile[len-3] = '\0';
231
strcat(infile, GZ_SUFFIX);
233
in = gzopen(infile, "rb");
235
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
238
out = fopen(outfile, "wb");
244
gz_uncompress(in, out);
250
/* ===========================================================================
251
* Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
253
* -f : compress with Z_FILTERED
254
* -h : compress with Z_HUFFMAN_ONLY
255
* -r : compress with Z_RLE
256
* -1 to -9 : compression level
259
int main(int argc, char *argv[])
265
strcpy(outmode, "wb6 ");
271
if (strcmp(*argv, "-d") == 0)
273
else if (strcmp(*argv, "-f") == 0)
275
else if (strcmp(*argv, "-h") == 0)
277
else if (strcmp(*argv, "-r") == 0)
279
else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
281
outmode[2] = (*argv)[1];
286
if (outmode[3] == ' ')
289
SET_BINARY_MODE(stdin);
290
SET_BINARY_MODE(stdout);
292
file = gzdopen(fileno(stdin), "rb");
293
if (file == NULL) error("can't gzdopen stdin");
294
gz_uncompress(file, stdout);
296
file = gzdopen(fileno(stdout), outmode);
297
if (file == NULL) error("can't gzdopen stdout");
298
gz_compress(stdin, file);
303
file_uncompress(*argv);
305
file_compress(*argv, outmode);
307
} while (argv++, --argc);