00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <time.h>
00020 #include "zlib.h"
00021
00022 #ifdef STDC
00023 # include <string.h>
00024 # include <stdlib.h>
00025 #else
00026 extern void exit OF((int));
00027 #endif
00028
00029 #ifdef USE_MMAP
00030 # include <sys/types.h>
00031 # include <sys/mman.h>
00032 # include <sys/stat.h>
00033 #endif
00034
00035 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
00036 # include <fcntl.h>
00037 # include <io.h>
00038 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
00039 #else
00040 # define SET_BINARY_MODE(file)
00041 #endif
00042
00043 #ifdef VMS
00044 # define unlink delete
00045 # define GZ_SUFFIX "-gz"
00046 #endif
00047 #ifdef RISCOS
00048 # define unlink remove
00049 # define GZ_SUFFIX "-gz"
00050 # define fileno(file) file->__file
00051 #endif
00052 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
00053 # include <unix.h>
00054 #endif
00055
00056 #ifndef WIN32
00057 extern int unlink OF((const char *));
00058 #endif
00059
00060 #ifndef GZ_SUFFIX
00061 # define GZ_SUFFIX ".gz"
00062 #endif
00063 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
00064
00065 #define BUFLEN 16384
00066 #define MAX_NAME_LEN 1024
00067
00068 #ifdef MAXSEG_64K
00069 # define local static
00070
00071 #else
00072 # define local
00073 #endif
00074
00075 char *prog;
00076
00077 void error OF((const char *msg));
00078 void gz_compress OF((FILE *in, gzFile out));
00079 #ifdef USE_MMAP
00080 int gz_compress_mmap OF((FILE *in, gzFile out));
00081 #endif
00082 void gz_uncompress OF((gzFile in, FILE *out));
00083 void file_compress OF((char *file, char *mode));
00084 void file_uncompress OF((char *file));
00085
00086 #ifdef WIN32
00087 int __cdecl main OF((int argc, char *argv[]));
00088 #else
00089 int main OF((int argc, char *argv[]));
00090 #endif
00091
00092 char dotest=0;
00093 char use_stdout=0;
00094
00095
00096
00097
00098 void error(msg)
00099 const char *msg;
00100 {
00101 fprintf(stderr, "%s: %s\n", prog, msg);
00102 exit(1);
00103 }
00104
00105
00106
00107
00108
00109 void gz_compress(in, out)
00110 FILE *in;
00111 gzFile out;
00112 {
00113 local char buf[BUFLEN];
00114 int len;
00115 int err;
00116
00117 #ifdef USE_MMAP
00118
00119
00120
00121 if (gz_compress_mmap(in, out) == Z_OK) return;
00122 #endif
00123 for (;;) {
00124 len = fread(buf, 1, sizeof(buf), in);
00125 if (ferror(in)) {
00126 perror("fread");
00127 exit(1);
00128 }
00129 if (len == 0) break;
00130
00131 if(out!=NULL)
00132 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
00133 }
00134 fclose(in);
00135
00136 if(out!=NULL)
00137 if (gzclose(out) != Z_OK) error("failed gzclose");
00138 }
00139
00140 #ifdef USE_MMAP
00141
00142
00143
00144
00145 int gz_compress_mmap(in, out)
00146 FILE *in;
00147 gzFile out;
00148 {
00149 int len;
00150 int err;
00151 int ifd = fileno(in);
00152 caddr_t buf;
00153 off_t buf_len;
00154 struct stat sb;
00155
00156
00157 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
00158 buf_len = sb.st_size;
00159 if (buf_len <= 0) return Z_ERRNO;
00160
00161
00162 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
00163 if (buf == (caddr_t)(-1)) return Z_ERRNO;
00164
00165
00166 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
00167
00168 if (len != (int)buf_len) error(gzerror(out, &err));
00169
00170 munmap(buf, buf_len);
00171 fclose(in);
00172 if (gzclose(out) != Z_OK) error("failed gzclose");
00173 return Z_OK;
00174 }
00175 #endif
00176
00177
00178
00179
00180 void gz_uncompress(in, out)
00181 gzFile in;
00182 FILE *out;
00183 {
00184 local char buf[BUFLEN];
00185 int len;
00186 int err;
00187
00188 for (;;) {
00189 len = gzread(in, buf, sizeof(buf));
00190 if (len < 0) error (gzerror(in, &err));
00191 if (len == 0) break;
00192
00193 if(out!=NULL)
00194 {
00195 if(dotest==0)
00196 {
00197 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
00198 error("failed fwrite");
00199 }
00200 }
00201 }
00202 }
00203 if((out!=NULL)&&(out!=stdout))
00204 if (fclose(out)) error("failed fclose");
00205
00206 gzclose(in);
00207
00208 }
00209
00210
00211
00212
00213
00214 void file_compress(file, mode)
00215 char *file;
00216 char *mode;
00217 {
00218 local char outfile[MAX_NAME_LEN];
00219 FILE *in;
00220 gzFile out;
00221
00222 strcpy(outfile, file);
00223 strcat(outfile, GZ_SUFFIX);
00224
00225 in = fopen(file, "rb");
00226 if (in == NULL) {
00227 perror(file);
00228 exit(1);
00229 }
00230 if(use_stdout==0)
00231 out = gzopen(outfile, mode);
00232 else
00233 out = gzdopen(1, mode);
00234
00235 if (out == NULL) {
00236 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
00237 exit(1);
00238 }
00239 gz_compress(in, out);
00240
00241
00242
00243 }
00244
00245
00246
00247
00248
00249
00250 void file_uncompress(file)
00251 char *file;
00252 {
00253 local char buf[MAX_NAME_LEN];
00254 char *infile, *outfile;
00255 FILE *out;
00256 gzFile in;
00257 int len = strlen(file);
00258
00259 strcpy(buf, file);
00260
00261 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
00262 infile = file;
00263 outfile = buf;
00264 outfile[len-3] = '\0';
00265 } else {
00266 outfile = file;
00267 infile = buf;
00268 strcat(infile, GZ_SUFFIX);
00269 }
00270 in = gzopen(infile, "rb");
00271 if (in == NULL) {
00272 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
00273 exit(1);
00274 }
00275 if(use_stdout==0)
00276 {
00277 if(dotest==0)
00278 {
00279 out = fopen(outfile, "wb");
00280 if (out == NULL) {
00281 perror(file);
00282 exit(1);
00283 }
00284 }
00285 else
00286 out=NULL;
00287
00288 gz_uncompress(in, out);
00289
00290 }
00291 else
00292 gz_uncompress(in, stdout);
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 #ifdef WIN32
00305 int __cdecl main(argc, argv)
00306 #else
00307 int main(argc, argv)
00308 #endif
00309 int argc;
00310 char *argv[];
00311 {
00312 int uncompr = 0;
00313 gzFile file;
00314 char outmode[20];
00315 clock_t c1,c2;
00316
00317 strcpy(outmode, "wb6 ");
00318
00319 prog = argv[0];
00320 argc--, argv++;
00321
00322 while (argc > 0) {
00323 if (strcmp(*argv, "-t") == 0)
00324 dotest = 1;
00325 else
00326 if (strcmp(*argv, "-c") == 0)
00327 use_stdout = 1;
00328 else if (strcmp(*argv, "-d") == 0)
00329 uncompr = 1;
00330 else if (strcmp(*argv, "-f") == 0)
00331 outmode[3] = 'f';
00332 else if (strcmp(*argv, "-h") == 0)
00333 outmode[3] = 'h';
00334 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
00335 (*argv)[2] == 0)
00336 outmode[2] = (*argv)[1];
00337 else
00338 break;
00339 argc--, argv++;
00340 }
00341
00342 c1=clock();
00343
00344 if (argc == 0) {
00345 SET_BINARY_MODE(stdin);
00346 SET_BINARY_MODE(stdout);
00347 if (uncompr) {
00348 file = gzdopen(fileno(stdin), "rb");
00349 if (file == NULL) error("can't gzdopen stdin");
00350 gz_uncompress(file, stdout);
00351 } else {
00352 file = gzdopen(fileno(stdout), outmode);
00353 if (file == NULL) error("can't gzdopen stdout");
00354 gz_compress(stdin, file);
00355 }
00356 } else {
00357 do {
00358 if (uncompr) {
00359 file_uncompress(*argv);
00360 } else {
00361 file_compress(*argv, outmode);
00362 }
00363 } while (argv++, --argc);
00364 }
00365
00366 c2=clock();
00367
00368 printf("Time: %f\n",((float)c2-(float)c1)/(float)CLOCKS_PER_SEC);
00369
00370 exit(0);
00371 return 0;
00372 }