Remove building with NOCRYPTO option
[minix.git] / common / dist / zlib / minigzip.c
blobead70e580802bf584a5f865b612acec9129fc248
1 /* $NetBSD: minigzip.c,v 1.1.1.1 2006/01/14 20:10:31 christos Exp $ */
3 /* minigzip.c -- simulate gzip using the zlib compression library
4 * Copyright (C) 1995-2005 Jean-loup Gailly.
5 * For conditions of distribution and use, see copyright notice in zlib.h
6 */
8 /*
9 * minigzip is a minimal implementation of the gzip utility. This is
10 * only an example of using zlib and isn't meant to replace the
11 * full-featured gzip. No attempt is made to deal with file systems
12 * limiting names to 14 or 8+3 characters, etc... Error checking is
13 * very limited. So use minigzip only for testing; use gzip for the
14 * real thing. On MSDOS, use only on file names without extension
15 * or in pipe mode.
18 /* @(#) Id */
20 #include <stdio.h>
21 #include "zlib.h"
23 #ifdef STDC
24 # include <string.h>
25 # include <stdlib.h>
26 #endif
28 #ifdef USE_MMAP
29 # include <sys/types.h>
30 # include <sys/mman.h>
31 # include <sys/stat.h>
32 #endif
34 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
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
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 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
52 # include <unix.h> /* for fileno */
53 #endif
55 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
56 extern int unlink OF((const char *));
57 #endif
59 #ifndef GZ_SUFFIX
60 # define GZ_SUFFIX ".gz"
61 #endif
62 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
64 #define BUFLEN 16384
65 #define MAX_NAME_LEN 1024
67 #ifdef MAXSEG_64K
68 # define local static
69 /* Needed for systems with limitation on stack size. */
70 #else
71 # define local
72 #endif
74 char *prog;
76 void error OF((const char *msg));
77 void gz_compress OF((FILE *in, gzFile out));
78 #ifdef USE_MMAP
79 int gz_compress_mmap OF((FILE *in, gzFile out));
80 #endif
81 void gz_uncompress OF((gzFile in, FILE *out));
82 void file_compress OF((char *file, char *mode));
83 void file_uncompress OF((char *file));
84 int main OF((int argc, char *argv[]));
86 /* ===========================================================================
87 * Display error message and exit
89 void error(msg)
90 const char *msg;
92 fprintf(stderr, "%s: %s\n", prog, msg);
93 exit(1);
96 /* ===========================================================================
97 * Compress input to output then close both files.
100 void gz_compress(in, out)
101 FILE *in;
102 gzFile out;
104 local char buf[BUFLEN];
105 int len;
106 int err;
108 #ifdef USE_MMAP
109 /* Try first compressing with mmap. If mmap fails (minigzip used in a
110 * pipe), use the normal fread loop.
112 if (gz_compress_mmap(in, out) == Z_OK) return;
113 #endif
114 for (;;) {
115 len = (int)fread(buf, 1, sizeof(buf), in);
116 if (ferror(in)) {
117 perror("fread");
118 exit(1);
120 if (len == 0) break;
122 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
124 fclose(in);
125 if (gzclose(out) != Z_OK) error("failed gzclose");
128 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
130 /* Try compressing the input file at once using mmap. Return Z_OK if
131 * if success, Z_ERRNO otherwise.
133 int gz_compress_mmap(in, out)
134 FILE *in;
135 gzFile out;
137 int len;
138 int err;
139 int ifd = fileno(in);
140 caddr_t buf; /* mmap'ed buffer for the entire input file */
141 off_t buf_len; /* length of the input file */
142 struct stat sb;
144 /* Determine the size of the file, needed for mmap: */
145 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
146 buf_len = sb.st_size;
147 if (buf_len <= 0) return Z_ERRNO;
149 /* Now do the actual mmap: */
150 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
151 if (buf == (caddr_t)(-1)) return Z_ERRNO;
153 /* Compress the whole file at once: */
154 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
156 if (len != (int)buf_len) error(gzerror(out, &err));
158 munmap(buf, buf_len);
159 fclose(in);
160 if (gzclose(out) != Z_OK) error("failed gzclose");
161 return Z_OK;
163 #endif /* USE_MMAP */
165 /* ===========================================================================
166 * Uncompress input to output then close both files.
168 void gz_uncompress(in, out)
169 gzFile in;
170 FILE *out;
172 local char buf[BUFLEN];
173 int len;
174 int err;
176 for (;;) {
177 len = gzread(in, buf, sizeof(buf));
178 if (len < 0) error (gzerror(in, &err));
179 if (len == 0) break;
181 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
182 error("failed fwrite");
185 if (fclose(out)) error("failed fclose");
187 if (gzclose(in) != Z_OK) error("failed gzclose");
191 /* ===========================================================================
192 * Compress the given file: create a corresponding .gz file and remove the
193 * original.
195 void file_compress(file, mode)
196 char *file;
197 char *mode;
199 local char outfile[MAX_NAME_LEN];
200 FILE *in;
201 gzFile out;
203 strcpy(outfile, file);
204 strcat(outfile, GZ_SUFFIX);
206 in = fopen(file, "rb");
207 if (in == NULL) {
208 perror(file);
209 exit(1);
211 out = gzopen(outfile, mode);
212 if (out == NULL) {
213 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
214 exit(1);
216 gz_compress(in, out);
218 unlink(file);
222 /* ===========================================================================
223 * Uncompress the given file and remove the original.
225 void file_uncompress(file)
226 char *file;
228 local char buf[MAX_NAME_LEN];
229 char *infile, *outfile;
230 FILE *out;
231 gzFile in;
232 uInt len = (uInt)strlen(file);
234 strcpy(buf, file);
236 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
237 infile = file;
238 outfile = buf;
239 outfile[len-3] = '\0';
240 } else {
241 outfile = file;
242 infile = buf;
243 strcat(infile, GZ_SUFFIX);
245 in = gzopen(infile, "rb");
246 if (in == NULL) {
247 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
248 exit(1);
250 out = fopen(outfile, "wb");
251 if (out == NULL) {
252 perror(file);
253 exit(1);
256 gz_uncompress(in, out);
258 unlink(infile);
262 /* ===========================================================================
263 * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
264 * -d : decompress
265 * -f : compress with Z_FILTERED
266 * -h : compress with Z_HUFFMAN_ONLY
267 * -r : compress with Z_RLE
268 * -1 to -9 : compression level
271 int main(argc, argv)
272 int argc;
273 char *argv[];
275 int uncompr = 0;
276 gzFile file;
277 char outmode[20];
279 strcpy(outmode, "wb6 ");
281 prog = argv[0];
282 argc--, argv++;
284 while (argc > 0) {
285 if (strcmp(*argv, "-d") == 0)
286 uncompr = 1;
287 else if (strcmp(*argv, "-f") == 0)
288 outmode[3] = 'f';
289 else if (strcmp(*argv, "-h") == 0)
290 outmode[3] = 'h';
291 else if (strcmp(*argv, "-r") == 0)
292 outmode[3] = 'R';
293 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
294 (*argv)[2] == 0)
295 outmode[2] = (*argv)[1];
296 else
297 break;
298 argc--, argv++;
300 if (outmode[3] == ' ')
301 outmode[3] = 0;
302 if (argc == 0) {
303 SET_BINARY_MODE(stdin);
304 SET_BINARY_MODE(stdout);
305 if (uncompr) {
306 file = gzdopen(fileno(stdin), "rb");
307 if (file == NULL) error("can't gzdopen stdin");
308 gz_uncompress(file, stdout);
309 } else {
310 file = gzdopen(fileno(stdout), outmode);
311 if (file == NULL) error("can't gzdopen stdout");
312 gz_compress(stdin, file);
314 } else {
315 do {
316 if (uncompr) {
317 file_uncompress(*argv);
318 } else {
319 file_compress(*argv, outmode);
321 } while (argv++, --argc);
323 return 0;