]> git.saurik.com Git - wxWidgets.git/blob - src/zlib/minigzip.c
new libraries added
[wxWidgets.git] / 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
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
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
13 * or in pipe mode.
14 */
15
16 /* @(#) $Id$ */
17
18 #include <stdio.h>
19 #include "../zlib/zlib.h"
20
21 #ifdef STDC
22 # include <string.h>
23 # include <stdlib.h>
24 #else
25 extern void exit OF((int));
26 #endif
27
28 #ifdef USE_MMAP
29 # include <sys/types.h>
30 # include <sys/mman.h>
31 # include <sys/stat.h>
32 #endif
33
34 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
35 # include <fcntl.h>
36 # include <io.h>
37 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
38 #else
39 # define SET_BINARY_MODE(file)
40 #endif
41
42 #ifdef VMS
43 # define unlink delete
44 # define GZ_SUFFIX "-gz"
45 #endif
46 #ifdef RISCOS
47 # define unlink remove
48 # define GZ_SUFFIX "-gz"
49 # define fileno(file) file->__file
50 #endif
51
52 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
53 extern int unlink OF((const char *));
54 #endif
55
56 #ifndef GZ_SUFFIX
57 # define GZ_SUFFIX ".gz"
58 #endif
59 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
60
61 #define BUFLEN 16384
62 #define MAX_NAME_LEN 1024
63
64 #ifdef MAXSEG_64K
65 # define local static
66 /* Needed for systems with limitation on stack size. */
67 #else
68 # define local
69 #endif
70
71 char *prog;
72
73 void error OF((const char *msg));
74 void gz_compress OF((FILE *in, gzFile out));
75 #ifdef USE_MMAP
76 int gz_compress_mmap OF((FILE *in, gzFile out));
77 #endif
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[]));
82
83 /* ===========================================================================
84 * Display error message and exit
85 */
86 void error(msg)
87 const char *msg;
88 {
89 fprintf(stderr, "%s: %s\n", prog, msg);
90 exit(1);
91 }
92
93 /* ===========================================================================
94 * Compress input to output then close both files.
95 */
96
97 void gz_compress(in, out)
98 FILE *in;
99 gzFile out;
100 {
101 local char buf[BUFLEN];
102 int len;
103 int err;
104
105 #ifdef USE_MMAP
106 /* Try first compressing with mmap. If mmap fails (minigzip used in a
107 * pipe), use the normal fread loop.
108 */
109 if (gz_compress_mmap(in, out) == Z_OK) return;
110 #endif
111 for (;;) {
112 len = fread(buf, 1, sizeof(buf), in);
113 if (ferror(in)) {
114 perror("fread");
115 exit(1);
116 }
117 if (len == 0) break;
118
119 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
120 }
121 fclose(in);
122 if (gzclose(out) != Z_OK) error("failed gzclose");
123 }
124
125 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
126
127 /* Try compressing the input file at once using mmap. Return Z_OK if
128 * if success, Z_ERRNO otherwise.
129 */
130 int gz_compress_mmap(in, out)
131 FILE *in;
132 gzFile out;
133 {
134 int len;
135 int err;
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 */
139 struct stat sb;
140
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;
145
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;
149
150 /* Compress the whole file at once: */
151 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
152
153 if (len != (int)buf_len) error(gzerror(out, &err));
154
155 munmap(buf, buf_len);
156 fclose(in);
157 if (gzclose(out) != Z_OK) error("failed gzclose");
158 return Z_OK;
159 }
160 #endif /* USE_MMAP */
161
162 /* ===========================================================================
163 * Uncompress input to output then close both files.
164 */
165 void gz_uncompress(in, out)
166 gzFile in;
167 FILE *out;
168 {
169 local char buf[BUFLEN];
170 int len;
171 int err;
172
173 for (;;) {
174 len = gzread(in, buf, sizeof(buf));
175 if (len < 0) error (gzerror(in, &err));
176 if (len == 0) break;
177
178 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
179 error("failed fwrite");
180 }
181 }
182 if (fclose(out)) error("failed fclose");
183
184 if (gzclose(in) != Z_OK) error("failed gzclose");
185 }
186
187
188 /* ===========================================================================
189 * Compress the given file: create a corresponding .gz file and remove the
190 * original.
191 */
192 void file_compress(file, mode)
193 char *file;
194 char *mode;
195 {
196 local char outfile[MAX_NAME_LEN];
197 FILE *in;
198 gzFile out;
199
200 strcpy(outfile, file);
201 strcat(outfile, GZ_SUFFIX);
202
203 in = fopen(file, "rb");
204 if (in == NULL) {
205 perror(file);
206 exit(1);
207 }
208 out = gzopen(outfile, mode);
209 if (out == NULL) {
210 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
211 exit(1);
212 }
213 gz_compress(in, out);
214
215 unlink(file);
216 }
217
218
219 /* ===========================================================================
220 * Uncompress the given file and remove the original.
221 */
222 void file_uncompress(file)
223 char *file;
224 {
225 local char buf[MAX_NAME_LEN];
226 char *infile, *outfile;
227 FILE *out;
228 gzFile in;
229 int len = strlen(file);
230
231 strcpy(buf, file);
232
233 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
234 infile = file;
235 outfile = buf;
236 outfile[len-3] = '\0';
237 } else {
238 outfile = file;
239 infile = buf;
240 strcat(infile, GZ_SUFFIX);
241 }
242 in = gzopen(infile, "rb");
243 if (in == NULL) {
244 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
245 exit(1);
246 }
247 out = fopen(outfile, "wb");
248 if (out == NULL) {
249 perror(file);
250 exit(1);
251 }
252
253 gz_uncompress(in, out);
254
255 unlink(infile);
256 }
257
258
259 /* ===========================================================================
260 * Usage: minigzip [-d] [-f] [-h] [-1 to -9] [files...]
261 * -d : decompress
262 * -f : compress with Z_FILTERED
263 * -h : compress with Z_HUFFMAN_ONLY
264 * -1 to -9 : compression level
265 */
266
267 int main(argc, argv)
268 int argc;
269 char *argv[];
270 {
271 int uncompr = 0;
272 gzFile file;
273 char outmode[20];
274
275 strcpy(outmode, "wb6 ");
276
277 prog = argv[0];
278 argc--, argv++;
279
280 while (argc > 0) {
281 if (strcmp(*argv, "-d") == 0)
282 uncompr = 1;
283 else if (strcmp(*argv, "-f") == 0)
284 outmode[3] = 'f';
285 else if (strcmp(*argv, "-h") == 0)
286 outmode[3] = 'h';
287 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
288 (*argv)[2] == 0)
289 outmode[2] = (*argv)[1];
290 else
291 break;
292 argc--, argv++;
293 }
294 if (argc == 0) {
295 SET_BINARY_MODE(stdin);
296 SET_BINARY_MODE(stdout);
297 if (uncompr) {
298 file = gzdopen(fileno(stdin), "rb");
299 if (file == NULL) error("can't gzdopen stdin");
300 gz_uncompress(file, stdout);
301 } else {
302 file = gzdopen(fileno(stdout), outmode);
303 if (file == NULL) error("can't gzdopen stdout");
304 gz_compress(stdin, file);
305 }
306 } else {
307 do {
308 if (uncompr) {
309 file_uncompress(*argv);
310 } else {
311 file_compress(*argv, outmode);
312 }
313 } while (argv++, --argc);
314 }
315 exit(0);
316 return 0; /* to avoid warning */
317 }