]>
git.saurik.com Git - wxWidgets.git/blob - src/zlib/minigzip.c
1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-1998 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
25 extern void exit
OF((int));
29 # include <sys/types.h>
30 # include <sys/mman.h>
31 # include <sys/stat.h>
34 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
37 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
39 # define SET_BINARY_MODE(file)
43 # define unlink delete
44 # define GZ_SUFFIX "-gz"
47 # define unlink remove
48 # define GZ_SUFFIX "-gz"
49 # define fileno(file) file->__file
52 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
53 extern int unlink
OF((const char *));
57 # define GZ_SUFFIX ".gz"
59 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
62 #define MAX_NAME_LEN 1024
66 /* Needed for systems with limitation on stack size. */
73 void error
OF((const char *msg
));
74 void gz_compress
OF((FILE *in
, gzFile out
));
76 int gz_compress_mmap
OF((FILE *in
, gzFile out
));
78 void gz_uncompress
OF((gzFile in
, FILE *out
));
79 void file_compress
OF((char *file
, char *mode
));
80 void file_uncompress
OF((char *file
));
81 int main
OF((int argc
, char *argv
[]));
83 /* ===========================================================================
84 * Display error message and exit
89 fprintf(stderr
, "%s: %s\n", prog
, msg
);
93 /* ===========================================================================
94 * Compress input to output then close both files.
97 void gz_compress(in
, out
)
101 local
char buf
[BUFLEN
];
106 /* Try first compressing with mmap. If mmap fails (minigzip used in a
107 * pipe), use the normal fread loop.
109 if (gz_compress_mmap(in
, out
) == Z_OK
) return;
112 len
= fread(buf
, 1, sizeof(buf
), in
);
119 if (gzwrite(out
, buf
, (unsigned)len
) != len
) error(gzerror(out
, &err
));
122 if (gzclose(out
) != Z_OK
) error("failed gzclose");
125 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
127 /* Try compressing the input file at once using mmap. Return Z_OK if
128 * if success, Z_ERRNO otherwise.
130 int gz_compress_mmap(in
, out
)
136 int ifd
= fileno(in
);
137 caddr_t buf
; /* mmap'ed buffer for the entire input file */
138 off_t buf_len
; /* length of the input file */
141 /* Determine the size of the file, needed for mmap: */
142 if (fstat(ifd
, &sb
) < 0) return Z_ERRNO
;
143 buf_len
= sb
.st_size
;
144 if (buf_len
<= 0) return Z_ERRNO
;
146 /* Now do the actual mmap: */
147 buf
= mmap((caddr_t
) 0, buf_len
, PROT_READ
, MAP_SHARED
, ifd
, (off_t
)0);
148 if (buf
== (caddr_t
)(-1)) return Z_ERRNO
;
150 /* Compress the whole file at once: */
151 len
= gzwrite(out
, (char *)buf
, (unsigned)buf_len
);
153 if (len
!= (int)buf_len
) error(gzerror(out
, &err
));
155 munmap(buf
, buf_len
);
157 if (gzclose(out
) != Z_OK
) error("failed gzclose");
160 #endif /* USE_MMAP */
162 /* ===========================================================================
163 * Uncompress input to output then close both files.
165 void gz_uncompress(in
, out
)
169 local
char buf
[BUFLEN
];
174 len
= gzread(in
, buf
, sizeof(buf
));
175 if (len
< 0) error (gzerror(in
, &err
));
178 if ((int)fwrite(buf
, 1, (unsigned)len
, out
) != len
) {
179 error("failed fwrite");
182 if (fclose(out
)) error("failed fclose");
184 if (gzclose(in
) != Z_OK
) error("failed gzclose");
188 /* ===========================================================================
189 * Compress the given file: create a corresponding .gz file and remove the
192 void file_compress(file
, mode
)
196 local
char outfile
[MAX_NAME_LEN
];
200 strcpy(outfile
, file
);
201 strcat(outfile
, GZ_SUFFIX
);
203 in
= fopen(file
, "rb");
208 out
= gzopen(outfile
, mode
);
210 fprintf(stderr
, "%s: can't gzopen %s\n", prog
, outfile
);
213 gz_compress(in
, out
);
219 /* ===========================================================================
220 * Uncompress the given file and remove the original.
222 void file_uncompress(file
)
225 local
char buf
[MAX_NAME_LEN
];
226 char *infile
, *outfile
;
229 int len
= strlen(file
);
233 if (len
> SUFFIX_LEN
&& strcmp(file
+len
-SUFFIX_LEN
, GZ_SUFFIX
) == 0) {
236 outfile
[len
-3] = '\0';
240 strcat(infile
, GZ_SUFFIX
);
242 in
= gzopen(infile
, "rb");
244 fprintf(stderr
, "%s: can't gzopen %s\n", prog
, infile
);
247 out
= fopen(outfile
, "wb");
253 gz_uncompress(in
, out
);
259 /* ===========================================================================
260 * Usage: minigzip [-d] [-f] [-h] [-1 to -9] [files...]
262 * -f : compress with Z_FILTERED
263 * -h : compress with Z_HUFFMAN_ONLY
264 * -1 to -9 : compression level
275 strcpy(outmode
, "wb6 ");
281 if (strcmp(*argv
, "-d") == 0)
283 else if (strcmp(*argv
, "-f") == 0)
285 else if (strcmp(*argv
, "-h") == 0)
287 else if ((*argv
)[0] == '-' && (*argv
)[1] >= '1' && (*argv
)[1] <= '9' &&
289 outmode
[2] = (*argv
)[1];
295 SET_BINARY_MODE(stdin
);
296 SET_BINARY_MODE(stdout
);
298 file
= gzdopen(fileno(stdin
), "rb");
299 if (file
== NULL
) error("can't gzdopen stdin");
300 gz_uncompress(file
, stdout
);
302 file
= gzdopen(fileno(stdout
), outmode
);
303 if (file
== NULL
) error("can't gzdopen stdout");
304 gz_compress(stdin
, file
);
309 file_uncompress(*argv
);
311 file_compress(*argv
, outmode
);
313 } while (argv
++, --argc
);
316 return 0; /* to avoid warning */