]> git.saurik.com Git - wxWidgets.git/blob - src/zlib/minigzip.c
Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / zlib / minigzip.c
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
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
17 #include <stdio.h>
18 #include "zlib.h"
19
20 #ifdef STDC
21 # include <string.h>
22 # include <stdlib.h>
23 #endif
24
25 #ifdef USE_MMAP
26 # include <sys/types.h>
27 # include <sys/mman.h>
28 # include <sys/stat.h>
29 #endif
30
31 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
32 # include <fcntl.h>
33 # include <io.h>
34 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
35 #else
36 # define SET_BINARY_MODE(file)
37 #endif
38
39 #ifdef VMS
40 # define unlink delete
41 # define GZ_SUFFIX "-gz"
42 #endif
43 #ifdef RISCOS
44 # define unlink remove
45 # define GZ_SUFFIX "-gz"
46 # define fileno(file) file->__file
47 #endif
48 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
49 # include <unix.h> /* for fileno */
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 = (int)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 uInt len = (uInt)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] [-r] [-1 to -9] [files...]
261 * -d : decompress
262 * -f : compress with Z_FILTERED
263 * -h : compress with Z_HUFFMAN_ONLY
264 * -r : compress with Z_RLE
265 * -1 to -9 : compression level
266 */
267
268 int main(argc, argv)
269 int argc;
270 char *argv[];
271 {
272 int uncompr = 0;
273 gzFile file;
274 char outmode[20];
275
276 strcpy(outmode, "wb6 ");
277
278 prog = argv[0];
279 argc--, argv++;
280
281 while (argc > 0) {
282 if (strcmp(*argv, "-d") == 0)
283 uncompr = 1;
284 else if (strcmp(*argv, "-f") == 0)
285 outmode[3] = 'f';
286 else if (strcmp(*argv, "-h") == 0)
287 outmode[3] = 'h';
288 else if (strcmp(*argv, "-r") == 0)
289 outmode[3] = 'R';
290 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
291 (*argv)[2] == 0)
292 outmode[2] = (*argv)[1];
293 else
294 break;
295 argc--, argv++;
296 }
297 if (outmode[3] == ' ')
298 outmode[3] = 0;
299 if (argc == 0) {
300 SET_BINARY_MODE(stdin);
301 SET_BINARY_MODE(stdout);
302 if (uncompr) {
303 file = gzdopen(fileno(stdin), "rb");
304 if (file == NULL) error("can't gzdopen stdin");
305 gz_uncompress(file, stdout);
306 } else {
307 file = gzdopen(fileno(stdout), outmode);
308 if (file == NULL) error("can't gzdopen stdout");
309 gz_compress(stdin, file);
310 }
311 } else {
312 do {
313 if (uncompr) {
314 file_uncompress(*argv);
315 } else {
316 file_compress(*argv, outmode);
317 }
318 } while (argv++, --argc);
319 }
320 return 0;
321 }