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