minigzip.c
Go to the documentation of this file.
00001 /* minigzip.c -- simulate gzip using the zlib compression library
00002  * Copyright (C) 1995-2005 Jean-loup Gailly.
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 /*
00007  * minigzip is a minimal implementation of the gzip utility. This is
00008  * only an example of using zlib and isn't meant to replace the
00009  * full-featured gzip. No attempt is made to deal with file systems
00010  * limiting names to 14 or 8+3 characters, etc... Error checking is
00011  * very limited. So use minigzip only for testing; use gzip for the
00012  * real thing. On MSDOS, use only on file names without extension
00013  * or in pipe mode.
00014  */
00015 
00016 /* @(#) $Id$ */
00017 
00018 #include <stdio.h>
00019 #include "zlib.h"
00020 
00021 #ifdef STDC
00022 #  include <string.h>
00023 #  include <stdlib.h>
00024 #endif
00025 
00026 #ifdef USE_MMAP
00027 #  include <sys/types.h>
00028 #  include <sys/mman.h>
00029 #  include <sys/stat.h>
00030 #endif
00031 
00032 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
00033 #  include <fcntl.h>
00034 #  include <io.h>
00035 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
00036 #else
00037 #  define SET_BINARY_MODE(file)
00038 #endif
00039 
00040 #ifdef VMS
00041 #  define unlink delete
00042 #  define GZ_SUFFIX "-gz"
00043 #endif
00044 #ifdef RISCOS
00045 #  define unlink remove
00046 #  define GZ_SUFFIX "-gz"
00047 #  define fileno(file) file->__file
00048 #endif
00049 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
00050 #  include <unix.h> /* for fileno */
00051 #endif
00052 
00053 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
00054   extern int unlink OF((const char *));
00055 #endif
00056 
00057 #ifndef GZ_SUFFIX
00058 #  define GZ_SUFFIX ".gz"
00059 #endif
00060 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
00061 
00062 #define BUFLEN      16384
00063 #define MAX_NAME_LEN 1024
00064 
00065 #ifdef MAXSEG_64K
00066 #  define local static
00067    /* Needed for systems with limitation on stack size. */
00068 #else
00069 #  define local
00070 #endif
00071 
00072 char *prog;
00073 
00074 void error            OF((const char *msg));
00075 void gz_compress      OF((FILE   *in, gzFile out));
00076 #ifdef USE_MMAP
00077 int  gz_compress_mmap OF((FILE   *in, gzFile out));
00078 #endif
00079 void gz_uncompress    OF((gzFile in, FILE   *out));
00080 void file_compress    OF((char  *file, char *mode));
00081 void file_uncompress  OF((char  *file));
00082 int  main             OF((int argc, char *argv[]));
00083 
00084 /* ===========================================================================
00085  * Display error message and exit
00086  */
00087 void error(msg)
00088     const char *msg;
00089 {
00090     fprintf(stderr, "%s: %s\n", prog, msg);
00091     exit(1);
00092 }
00093 
00094 /* ===========================================================================
00095  * Compress input to output then close both files.
00096  */
00097 
00098 void gz_compress(in, out)
00099     FILE   *in;
00100     gzFile out;
00101 {
00102     local char buf[BUFLEN];
00103     int len;
00104     int err;
00105 
00106 #ifdef USE_MMAP
00107     /* Try first compressing with mmap. If mmap fails (minigzip used in a
00108      * pipe), use the normal fread loop.
00109      */
00110     if (gz_compress_mmap(in, out) == Z_OK) return;
00111 #endif
00112     for (;;) {
00113         len = (int)fread(buf, 1, sizeof(buf), in);
00114         if (ferror(in)) {
00115             perror("fread");
00116             exit(1);
00117         }
00118         if (len == 0) break;
00119 
00120         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
00121     }
00122     fclose(in);
00123     if (gzclose(out) != Z_OK) error("failed gzclose");
00124 }
00125 
00126 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
00127 
00128 /* Try compressing the input file at once using mmap. Return Z_OK if
00129  * if success, Z_ERRNO otherwise.
00130  */
00131 int gz_compress_mmap(in, out)
00132     FILE   *in;
00133     gzFile out;
00134 {
00135     int len;
00136     int err;
00137     int ifd = fileno(in);
00138     caddr_t buf;    /* mmap'ed buffer for the entire input file */
00139     off_t buf_len;  /* length of the input file */
00140     struct stat sb;
00141 
00142     /* Determine the size of the file, needed for mmap: */
00143     if (fstat(ifd, &sb) < 0) return Z_ERRNO;
00144     buf_len = sb.st_size;
00145     if (buf_len <= 0) return Z_ERRNO;
00146 
00147     /* Now do the actual mmap: */
00148     buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
00149     if (buf == (caddr_t)(-1)) return Z_ERRNO;
00150 
00151     /* Compress the whole file at once: */
00152     len = gzwrite(out, (char *)buf, (unsigned)buf_len);
00153 
00154     if (len != (int)buf_len) error(gzerror(out, &err));
00155 
00156     munmap(buf, buf_len);
00157     fclose(in);
00158     if (gzclose(out) != Z_OK) error("failed gzclose");
00159     return Z_OK;
00160 }
00161 #endif /* USE_MMAP */
00162 
00163 /* ===========================================================================
00164  * Uncompress input to output then close both files.
00165  */
00166 void gz_uncompress(in, out)
00167     gzFile in;
00168     FILE   *out;
00169 {
00170     local char buf[BUFLEN];
00171     int len;
00172     int err;
00173 
00174     for (;;) {
00175         len = gzread(in, buf, sizeof(buf));
00176         if (len < 0) error (gzerror(in, &err));
00177         if (len == 0) break;
00178 
00179         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
00180             error("failed fwrite");
00181         }
00182     }
00183     if (fclose(out)) error("failed fclose");
00184 
00185     if (gzclose(in) != Z_OK) error("failed gzclose");
00186 }
00187 
00188 
00189 /* ===========================================================================
00190  * Compress the given file: create a corresponding .gz file and remove the
00191  * original.
00192  */
00193 void file_compress(file, mode)
00194     char  *file;
00195     char  *mode;
00196 {
00197     local char outfile[MAX_NAME_LEN];
00198     FILE  *in;
00199     gzFile out;
00200 
00201     strcpy(outfile, file);
00202     strcat(outfile, GZ_SUFFIX);
00203 
00204     in = fopen(file, "rb");
00205     if (in == NULL) {
00206         perror(file);
00207         exit(1);
00208     }
00209     out = gzopen(outfile, mode);
00210     if (out == NULL) {
00211         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
00212         exit(1);
00213     }
00214     gz_compress(in, out);
00215 
00216     unlink(file);
00217 }
00218 
00219 
00220 /* ===========================================================================
00221  * Uncompress the given file and remove the original.
00222  */
00223 void file_uncompress(file)
00224     char  *file;
00225 {
00226     local char buf[MAX_NAME_LEN];
00227     char *infile, *outfile;
00228     FILE  *out;
00229     gzFile in;
00230     uInt len = (uInt)strlen(file);
00231 
00232     strcpy(buf, file);
00233 
00234     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
00235         infile = file;
00236         outfile = buf;
00237         outfile[len-3] = '\0';
00238     } else {
00239         outfile = file;
00240         infile = buf;
00241         strcat(infile, GZ_SUFFIX);
00242     }
00243     in = gzopen(infile, "rb");
00244     if (in == NULL) {
00245         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
00246         exit(1);
00247     }
00248     out = fopen(outfile, "wb");
00249     if (out == NULL) {
00250         perror(file);
00251         exit(1);
00252     }
00253 
00254     gz_uncompress(in, out);
00255 
00256     unlink(infile);
00257 }
00258 
00259 
00260 /* ===========================================================================
00261  * Usage:  minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
00262  *   -d : decompress
00263  *   -f : compress with Z_FILTERED
00264  *   -h : compress with Z_HUFFMAN_ONLY
00265  *   -r : compress with Z_RLE
00266  *   -1 to -9 : compression level
00267  */
00268 
00269 int main(argc, argv)
00270     int argc;
00271     char *argv[];
00272 {
00273     int uncompr = 0;
00274     gzFile file;
00275     char outmode[20];
00276 
00277     strcpy(outmode, "wb6 ");
00278 
00279     prog = argv[0];
00280     argc--, argv++;
00281 
00282     while (argc > 0) {
00283       if (strcmp(*argv, "-d") == 0)
00284         uncompr = 1;
00285       else if (strcmp(*argv, "-f") == 0)
00286         outmode[3] = 'f';
00287       else if (strcmp(*argv, "-h") == 0)
00288         outmode[3] = 'h';
00289       else if (strcmp(*argv, "-r") == 0)
00290         outmode[3] = 'R';
00291       else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
00292                (*argv)[2] == 0)
00293         outmode[2] = (*argv)[1];
00294       else
00295         break;
00296       argc--, argv++;
00297     }
00298     if (outmode[3] == ' ')
00299         outmode[3] = 0;
00300     if (argc == 0) {
00301         SET_BINARY_MODE(stdin);
00302         SET_BINARY_MODE(stdout);
00303         if (uncompr) {
00304             file = gzdopen(fileno(stdin), "rb");
00305             if (file == NULL) error("can't gzdopen stdin");
00306             gz_uncompress(file, stdout);
00307         } else {
00308             file = gzdopen(fileno(stdout), outmode);
00309             if (file == NULL) error("can't gzdopen stdout");
00310             gz_compress(stdin, file);
00311         }
00312     } else {
00313         do {
00314             if (uncompr) {
00315                 file_uncompress(*argv);
00316             } else {
00317                 file_compress(*argv, outmode);
00318             }
00319         } while (argv++, --argc);
00320     }
00321     return 0;
00322 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55