1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 * |
|
4 * This code is free software; you can redistribute it and/or modify it |
|
5 * under the terms of the GNU General Public License version 2 only, as |
|
6 * published by the Free Software Foundation. Sun designates this |
|
7 * particular file as subject to the "Classpath" exception as provided |
|
8 * by Sun in the LICENSE file that accompanied this code. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
21 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
22 * have any questions. |
|
23 */ |
|
24 |
|
25 /* |
|
26 * This file is available under and governed by the GNU General Public |
|
27 * License version 2 only, as published by the Free Software Foundation. |
|
28 * However, the following notice accompanied the original version of this |
|
29 * file and, per its terms, should not be removed: |
|
30 * |
|
31 * minigzip.c -- simulate gzip using the zlib compression library |
|
32 * Copyright (C) 1995-1998 Jean-loup Gailly. |
|
33 * For conditions of distribution and use, see copyright notice in zlib.h |
|
34 */ |
|
35 |
|
36 /* |
|
37 * minigzip is a minimal implementation of the gzip utility. This is |
|
38 * only an example of using zlib and isn't meant to replace the |
|
39 * full-featured gzip. No attempt is made to deal with file systems |
|
40 * limiting names to 14 or 8+3 characters, etc... Error checking is |
|
41 * very limited. So use minigzip only for testing; use gzip for the |
|
42 * real thing. On MSDOS, use only on file names without extension |
|
43 * or in pipe mode. |
|
44 */ |
|
45 |
|
46 #include <stdio.h> |
|
47 #include "zlib.h" |
|
48 |
|
49 #ifdef STDC |
|
50 # include <string.h> |
|
51 # include <stdlib.h> |
|
52 #else |
|
53 extern void exit OF((int)); |
|
54 #endif |
|
55 |
|
56 #ifdef USE_MMAP |
|
57 # include <sys/types.h> |
|
58 # include <sys/mman.h> |
|
59 # include <sys/stat.h> |
|
60 #endif |
|
61 |
|
62 #if defined(MSDOS) || defined(OS2) || defined(WIN32) |
|
63 # include <fcntl.h> |
|
64 # include <io.h> |
|
65 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
|
66 #else |
|
67 # define SET_BINARY_MODE(file) |
|
68 #endif |
|
69 |
|
70 #ifdef VMS |
|
71 # define unlink delete |
|
72 # define GZ_SUFFIX "-gz" |
|
73 #endif |
|
74 #ifdef RISCOS |
|
75 # define unlink remove |
|
76 # define GZ_SUFFIX "-gz" |
|
77 # define fileno(file) file->__file |
|
78 #endif |
|
79 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os |
|
80 # include <unix.h> /* for fileno */ |
|
81 #endif |
|
82 |
|
83 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */ |
|
84 extern int unlink OF((const char *)); |
|
85 #endif |
|
86 |
|
87 #ifndef GZ_SUFFIX |
|
88 # define GZ_SUFFIX ".gz" |
|
89 #endif |
|
90 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) |
|
91 |
|
92 #define BUFLEN 16384 |
|
93 #define MAX_NAME_LEN 1024 |
|
94 |
|
95 #ifdef MAXSEG_64K |
|
96 # define local static |
|
97 /* Needed for systems with limitation on stack size. */ |
|
98 #else |
|
99 # define local |
|
100 #endif |
|
101 |
|
102 char *prog; |
|
103 |
|
104 void error OF((const char *msg)); |
|
105 void gz_compress OF((FILE *in, gzFile out)); |
|
106 #ifdef USE_MMAP |
|
107 int gz_compress_mmap OF((FILE *in, gzFile out)); |
|
108 #endif |
|
109 void gz_uncompress OF((gzFile in, FILE *out)); |
|
110 void file_compress OF((char *file, char *mode)); |
|
111 void file_uncompress OF((char *file)); |
|
112 int main OF((int argc, char *argv[])); |
|
113 |
|
114 /* =========================================================================== |
|
115 * Display error message and exit |
|
116 */ |
|
117 void error(msg) |
|
118 const char *msg; |
|
119 { |
|
120 fprintf(stderr, "%s: %s\n", prog, msg); |
|
121 exit(1); |
|
122 } |
|
123 |
|
124 /* =========================================================================== |
|
125 * Compress input to output then close both files. |
|
126 */ |
|
127 |
|
128 void gz_compress(in, out) |
|
129 FILE *in; |
|
130 gzFile out; |
|
131 { |
|
132 local char buf[BUFLEN]; |
|
133 int len; |
|
134 int err; |
|
135 |
|
136 #ifdef USE_MMAP |
|
137 /* Try first compressing with mmap. If mmap fails (minigzip used in a |
|
138 * pipe), use the normal fread loop. |
|
139 */ |
|
140 if (gz_compress_mmap(in, out) == Z_OK) return; |
|
141 #endif |
|
142 for (;;) { |
|
143 len = fread(buf, 1, sizeof(buf), in); |
|
144 if (ferror(in)) { |
|
145 perror("fread"); |
|
146 exit(1); |
|
147 } |
|
148 if (len == 0) break; |
|
149 |
|
150 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); |
|
151 } |
|
152 fclose(in); |
|
153 if (gzclose(out) != Z_OK) error("failed gzclose"); |
|
154 } |
|
155 |
|
156 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */ |
|
157 |
|
158 /* Try compressing the input file at once using mmap. Return Z_OK if |
|
159 * if success, Z_ERRNO otherwise. |
|
160 */ |
|
161 int gz_compress_mmap(in, out) |
|
162 FILE *in; |
|
163 gzFile out; |
|
164 { |
|
165 int len; |
|
166 int err; |
|
167 int ifd = fileno(in); |
|
168 caddr_t buf; /* mmap'ed buffer for the entire input file */ |
|
169 off_t buf_len; /* length of the input file */ |
|
170 struct stat sb; |
|
171 |
|
172 /* Determine the size of the file, needed for mmap: */ |
|
173 if (fstat(ifd, &sb) < 0) return Z_ERRNO; |
|
174 buf_len = sb.st_size; |
|
175 if (buf_len <= 0) return Z_ERRNO; |
|
176 |
|
177 /* Now do the actual mmap: */ |
|
178 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); |
|
179 if (buf == (caddr_t)(-1)) return Z_ERRNO; |
|
180 |
|
181 /* Compress the whole file at once: */ |
|
182 len = gzwrite(out, (char *)buf, (unsigned)buf_len); |
|
183 |
|
184 if (len != (int)buf_len) error(gzerror(out, &err)); |
|
185 |
|
186 munmap(buf, buf_len); |
|
187 fclose(in); |
|
188 if (gzclose(out) != Z_OK) error("failed gzclose"); |
|
189 return Z_OK; |
|
190 } |
|
191 #endif /* USE_MMAP */ |
|
192 |
|
193 /* =========================================================================== |
|
194 * Uncompress input to output then close both files. |
|
195 */ |
|
196 void gz_uncompress(in, out) |
|
197 gzFile in; |
|
198 FILE *out; |
|
199 { |
|
200 local char buf[BUFLEN]; |
|
201 int len; |
|
202 int err; |
|
203 |
|
204 for (;;) { |
|
205 len = gzread(in, buf, sizeof(buf)); |
|
206 if (len < 0) error (gzerror(in, &err)); |
|
207 if (len == 0) break; |
|
208 |
|
209 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { |
|
210 error("failed fwrite"); |
|
211 } |
|
212 } |
|
213 if (fclose(out)) error("failed fclose"); |
|
214 |
|
215 if (gzclose(in) != Z_OK) error("failed gzclose"); |
|
216 } |
|
217 |
|
218 |
|
219 /* =========================================================================== |
|
220 * Compress the given file: create a corresponding .gz file and remove the |
|
221 * original. |
|
222 */ |
|
223 void file_compress(file, mode) |
|
224 char *file; |
|
225 char *mode; |
|
226 { |
|
227 local char outfile[MAX_NAME_LEN]; |
|
228 FILE *in; |
|
229 gzFile out; |
|
230 |
|
231 strcpy(outfile, file); |
|
232 strcat(outfile, GZ_SUFFIX); |
|
233 |
|
234 in = fopen(file, "rb"); |
|
235 if (in == NULL) { |
|
236 perror(file); |
|
237 exit(1); |
|
238 } |
|
239 out = gzopen(outfile, mode); |
|
240 if (out == NULL) { |
|
241 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); |
|
242 exit(1); |
|
243 } |
|
244 gz_compress(in, out); |
|
245 |
|
246 unlink(file); |
|
247 } |
|
248 |
|
249 |
|
250 /* =========================================================================== |
|
251 * Uncompress the given file and remove the original. |
|
252 */ |
|
253 void file_uncompress(file) |
|
254 char *file; |
|
255 { |
|
256 local char buf[MAX_NAME_LEN]; |
|
257 char *infile, *outfile; |
|
258 FILE *out; |
|
259 gzFile in; |
|
260 int len = strlen(file); |
|
261 |
|
262 strcpy(buf, file); |
|
263 |
|
264 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { |
|
265 infile = file; |
|
266 outfile = buf; |
|
267 outfile[len-3] = '\0'; |
|
268 } else { |
|
269 outfile = file; |
|
270 infile = buf; |
|
271 strcat(infile, GZ_SUFFIX); |
|
272 } |
|
273 in = gzopen(infile, "rb"); |
|
274 if (in == NULL) { |
|
275 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); |
|
276 exit(1); |
|
277 } |
|
278 out = fopen(outfile, "wb"); |
|
279 if (out == NULL) { |
|
280 perror(file); |
|
281 exit(1); |
|
282 } |
|
283 |
|
284 gz_uncompress(in, out); |
|
285 |
|
286 unlink(infile); |
|
287 } |
|
288 |
|
289 |
|
290 /* =========================================================================== |
|
291 * Usage: minigzip [-d] [-f] [-h] [-1 to -9] [files...] |
|
292 * -d : decompress |
|
293 * -f : compress with Z_FILTERED |
|
294 * -h : compress with Z_HUFFMAN_ONLY |
|
295 * -1 to -9 : compression level |
|
296 */ |
|
297 |
|
298 int main(argc, argv) |
|
299 int argc; |
|
300 char *argv[]; |
|
301 { |
|
302 int uncompr = 0; |
|
303 gzFile file; |
|
304 char outmode[20]; |
|
305 |
|
306 strcpy(outmode, "wb6 "); |
|
307 |
|
308 prog = argv[0]; |
|
309 argc--, argv++; |
|
310 |
|
311 while (argc > 0) { |
|
312 if (strcmp(*argv, "-d") == 0) |
|
313 uncompr = 1; |
|
314 else if (strcmp(*argv, "-f") == 0) |
|
315 outmode[3] = 'f'; |
|
316 else if (strcmp(*argv, "-h") == 0) |
|
317 outmode[3] = 'h'; |
|
318 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && |
|
319 (*argv)[2] == 0) |
|
320 outmode[2] = (*argv)[1]; |
|
321 else |
|
322 break; |
|
323 argc--, argv++; |
|
324 } |
|
325 if (argc == 0) { |
|
326 SET_BINARY_MODE(stdin); |
|
327 SET_BINARY_MODE(stdout); |
|
328 if (uncompr) { |
|
329 file = gzdopen(fileno(stdin), "rb"); |
|
330 if (file == NULL) error("can't gzdopen stdin"); |
|
331 gz_uncompress(file, stdout); |
|
332 } else { |
|
333 file = gzdopen(fileno(stdout), outmode); |
|
334 if (file == NULL) error("can't gzdopen stdout"); |
|
335 gz_compress(stdin, file); |
|
336 } |
|
337 } else { |
|
338 do { |
|
339 if (uncompr) { |
|
340 file_uncompress(*argv); |
|
341 } else { |
|
342 file_compress(*argv, outmode); |
|
343 } |
|
344 } while (argv++, --argc); |
|
345 } |
|
346 exit(0); |
|
347 return 0; /* to avoid warning */ |
|
348 } |
|