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