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