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