1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-2006, 2010 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__)
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 *));
57 #if defined(UNDER_CE) && defined(NO_ERRNO_H)
59 # define perror(s) pwinerror(s)
61 /* Map the Windows error number in ERROR to a locale-dependent error
62 message string and return a pointer to it. Typically, the values
63 for ERROR come from GetLastError.
65 The string pointed to shall not be modified by the application,
66 but may be overwritten by a subsequent call to strwinerror
68 The strwinerror function does not change the current setting
71 static char *strwinerror (error)
74 static char buf[1024];
77 DWORD lasterr = GetLastError();
78 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
79 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
82 0, /* Default language */
87 /* If there is an \r\n appended, zap it. */
89 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
94 if (chars > sizeof (buf) - 1) {
95 chars = sizeof (buf) - 1;
99 wcstombs(buf, msgbuf, chars + 1);
103 sprintf(buf, "unknown win32 error (%ld)", error);
106 SetLastError(lasterr);
110 static void pwinerror (s)
114 fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
116 fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
119 #endif /* UNDER_CE && NO_ERRNO_H */
122 # define GZ_SUFFIX ".gz"
124 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
127 #define MAX_NAME_LEN 1024
130 # define local static
131 /* Needed for systems with limitation on stack size. */
138 void error OF((const char *msg));
139 void gz_compress OF((FILE *in, gzFile out));
141 int gz_compress_mmap OF((FILE *in, gzFile out));
143 void gz_uncompress OF((gzFile in, FILE *out));
144 void file_compress OF((char *file, char *mode));
145 void file_uncompress OF((char *file));
146 int main OF((int argc, char *argv[]));
148 /* ===========================================================================
149 * Display error message and exit
154 fprintf(stderr, "%s: %s\n", prog, msg);
158 /* ===========================================================================
159 * Compress input to output then close both files.
162 void gz_compress(in, out)
166 local char buf[BUFLEN];
171 /* Try first compressing with mmap. If mmap fails (minigzip used in a
172 * pipe), use the normal fread loop.
174 if (gz_compress_mmap(in, out) == Z_OK) return;
177 len = (int)fread(buf, 1, sizeof(buf), in);
184 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
187 if (gzclose(out) != Z_OK) error("failed gzclose");
190 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
192 /* Try compressing the input file at once using mmap. Return Z_OK if
193 * if success, Z_ERRNO otherwise.
195 int gz_compress_mmap(in, out)
201 int ifd = fileno(in);
202 caddr_t buf; /* mmap'ed buffer for the entire input file */
203 off_t buf_len; /* length of the input file */
206 /* Determine the size of the file, needed for mmap: */
207 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
208 buf_len = sb.st_size;
209 if (buf_len <= 0) return Z_ERRNO;
211 /* Now do the actual mmap: */
212 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
213 if (buf == (caddr_t)(-1)) return Z_ERRNO;
215 /* Compress the whole file at once: */
216 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
218 if (len != (int)buf_len) error(gzerror(out, &err));
220 munmap(buf, buf_len);
222 if (gzclose(out) != Z_OK) error("failed gzclose");
225 #endif /* USE_MMAP */
227 /* ===========================================================================
228 * Uncompress input to output then close both files.
230 void gz_uncompress(in, out)
234 local char buf[BUFLEN];
239 len = gzread(in, buf, sizeof(buf));
240 if (len < 0) error (gzerror(in, &err));
243 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
244 error("failed fwrite");
247 if (fclose(out)) error("failed fclose");
249 if (gzclose(in) != Z_OK) error("failed gzclose");
253 /* ===========================================================================
254 * Compress the given file: create a corresponding .gz file and remove the
257 void file_compress(file, mode)
261 local char outfile[MAX_NAME_LEN];
265 strcpy(outfile, file);
266 strcat(outfile, GZ_SUFFIX);
268 in = fopen(file, "rb");
273 out = gzopen(outfile, mode);
275 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
278 gz_compress(in, out);
284 /* ===========================================================================
285 * Uncompress the given file and remove the original.
287 void file_uncompress(file)
290 local char buf[MAX_NAME_LEN];
291 char *infile, *outfile;
294 uInt len = (uInt)strlen(file);
298 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
301 outfile[len-3] = '\0';
305 strcat(infile, GZ_SUFFIX);
307 in = gzopen(infile, "rb");
309 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
312 out = fopen(outfile, "wb");
318 gz_uncompress(in, out);
324 /* ===========================================================================
325 * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
327 * -f : compress with Z_FILTERED
328 * -h : compress with Z_HUFFMAN_ONLY
329 * -r : compress with Z_RLE
330 * -1 to -9 : compression level
341 strcpy(outmode, "wb6 ");
347 if (strcmp(*argv, "-d") == 0)
349 else if (strcmp(*argv, "-f") == 0)
351 else if (strcmp(*argv, "-h") == 0)
353 else if (strcmp(*argv, "-r") == 0)
355 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
357 outmode[2] = (*argv)[1];
362 if (outmode[3] == ' ')
365 SET_BINARY_MODE(stdin);
366 SET_BINARY_MODE(stdout);
368 file = gzdopen(fileno(stdin), "rb");
369 if (file == NULL) error("can't gzdopen stdin");
370 gz_uncompress(file, stdout);
372 file = gzdopen(fileno(stdout), outmode);
373 if (file == NULL) error("can't gzdopen stdout");
374 gz_compress(stdin, file);
379 file_uncompress(*argv);
381 file_compress(*argv, outmode);
383 } while (argv++, --argc);