os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/minigzip.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/minigzip.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,320 @@
     1.4 +/* minigzip.c -- simulate gzip using the zlib compression library
     1.5 + * Copyright (C) 1995-1998 Jean-loup Gailly.
     1.6 + * For conditions of distribution and use, see copyright notice in zlib.h 
     1.7 + */
     1.8 +
     1.9 +/*
    1.10 + * minigzip is a minimal implementation of the gzip utility. This is
    1.11 + * only an example of using zlib and isn't meant to replace the
    1.12 + * full-featured gzip. No attempt is made to deal with file systems
    1.13 + * limiting names to 14 or 8+3 characters, etc... Error checking is
    1.14 + * very limited. So use minigzip only for testing; use gzip for the
    1.15 + * real thing. On MSDOS, use only on file names without extension
    1.16 + * or in pipe mode.
    1.17 + */
    1.18 +
    1.19 +/* @(#) $Id$ */
    1.20 +
    1.21 +#include <stdio.h>
    1.22 +#include "zlib.h"
    1.23 +
    1.24 +#ifdef STDC
    1.25 +#  include <string.h>
    1.26 +#  include <stdlib.h>
    1.27 +#else
    1.28 +   extern void exit  OF((int));
    1.29 +#endif
    1.30 +
    1.31 +#ifdef USE_MMAP
    1.32 +#  include <sys/types.h>
    1.33 +#  include <sys/mman.h>
    1.34 +#  include <sys/stat.h>
    1.35 +#endif
    1.36 +
    1.37 +#if defined(MSDOS) || defined(OS2) || defined(WIN32)
    1.38 +#  include <fcntl.h>
    1.39 +#  include <io.h>
    1.40 +#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
    1.41 +#else
    1.42 +#  define SET_BINARY_MODE(file)
    1.43 +#endif
    1.44 +
    1.45 +#ifdef VMS
    1.46 +#  define unlink delete
    1.47 +#  define GZ_SUFFIX "-gz"
    1.48 +#endif
    1.49 +#ifdef RISCOS
    1.50 +#  define unlink remove
    1.51 +#  define GZ_SUFFIX "-gz"
    1.52 +#  define fileno(file) file->__file
    1.53 +#endif
    1.54 +#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
    1.55 +#  include <unix.h> /* for fileno */
    1.56 +#endif
    1.57 +
    1.58 +#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
    1.59 +  extern int unlink OF((const char *));
    1.60 +#endif
    1.61 +
    1.62 +#ifndef GZ_SUFFIX
    1.63 +#  define GZ_SUFFIX ".gz"
    1.64 +#endif
    1.65 +#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
    1.66 +
    1.67 +#define BUFLEN      16384
    1.68 +#define MAX_NAME_LEN 1024
    1.69 +
    1.70 +#ifdef MAXSEG_64K
    1.71 +#  define local static
    1.72 +   /* Needed for systems with limitation on stack size. */
    1.73 +#else
    1.74 +#  define local
    1.75 +#endif
    1.76 +
    1.77 +char *prog;
    1.78 +
    1.79 +void error            OF((const char *msg));
    1.80 +void gz_compress      OF((FILE   *in, gzFile out));
    1.81 +#ifdef USE_MMAP
    1.82 +int  gz_compress_mmap OF((FILE   *in, gzFile out));
    1.83 +#endif
    1.84 +void gz_uncompress    OF((gzFile in, FILE   *out));
    1.85 +void file_compress    OF((char  *file, char *mode));
    1.86 +void file_uncompress  OF((char  *file));
    1.87 +int  main             OF((int argc, char *argv[]));
    1.88 +
    1.89 +/* ===========================================================================
    1.90 + * Display error message and exit
    1.91 + */
    1.92 +void error(msg)
    1.93 +    const char *msg;
    1.94 +{
    1.95 +    fprintf(stderr, "%s: %s\n", prog, msg);
    1.96 +    exit(1);
    1.97 +}
    1.98 +
    1.99 +/* ===========================================================================
   1.100 + * Compress input to output then close both files.
   1.101 + */
   1.102 +
   1.103 +void gz_compress(in, out)
   1.104 +    FILE   *in;
   1.105 +    gzFile out;
   1.106 +{
   1.107 +    local char buf[BUFLEN];
   1.108 +    int len;
   1.109 +    int err;
   1.110 +
   1.111 +#ifdef USE_MMAP
   1.112 +    /* Try first compressing with mmap. If mmap fails (minigzip used in a
   1.113 +     * pipe), use the normal fread loop.
   1.114 +     */
   1.115 +    if (gz_compress_mmap(in, out) == Z_OK) return;
   1.116 +#endif
   1.117 +    for (;;) {
   1.118 +        len = fread(buf, 1, sizeof(buf), in);
   1.119 +        if (ferror(in)) {
   1.120 +            perror("fread");
   1.121 +            exit(1);
   1.122 +        }
   1.123 +        if (len == 0) break;
   1.124 +
   1.125 +        if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
   1.126 +    }
   1.127 +    fclose(in);
   1.128 +    if (gzclose(out) != Z_OK) error("failed gzclose");
   1.129 +}
   1.130 +
   1.131 +#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
   1.132 +
   1.133 +/* Try compressing the input file at once using mmap. Return Z_OK if
   1.134 + * if success, Z_ERRNO otherwise.
   1.135 + */
   1.136 +int gz_compress_mmap(in, out)
   1.137 +    FILE   *in;
   1.138 +    gzFile out;
   1.139 +{
   1.140 +    int len;
   1.141 +    int err;
   1.142 +    int ifd = fileno(in);
   1.143 +    caddr_t buf;    /* mmap'ed buffer for the entire input file */
   1.144 +    off_t buf_len;  /* length of the input file */
   1.145 +    struct stat sb;
   1.146 +
   1.147 +    /* Determine the size of the file, needed for mmap: */
   1.148 +    if (fstat(ifd, &sb) < 0) return Z_ERRNO;
   1.149 +    buf_len = sb.st_size;
   1.150 +    if (buf_len <= 0) return Z_ERRNO;
   1.151 +
   1.152 +    /* Now do the actual mmap: */
   1.153 +    buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); 
   1.154 +    if (buf == (caddr_t)(-1)) return Z_ERRNO;
   1.155 +
   1.156 +    /* Compress the whole file at once: */
   1.157 +    len = gzwrite(out, (char *)buf, (unsigned)buf_len);
   1.158 +
   1.159 +    if (len != (int)buf_len) error(gzerror(out, &err));
   1.160 +
   1.161 +    munmap(buf, buf_len);
   1.162 +    fclose(in);
   1.163 +    if (gzclose(out) != Z_OK) error("failed gzclose");
   1.164 +    return Z_OK;
   1.165 +}
   1.166 +#endif /* USE_MMAP */
   1.167 +
   1.168 +/* ===========================================================================
   1.169 + * Uncompress input to output then close both files.
   1.170 + */
   1.171 +void gz_uncompress(in, out)
   1.172 +    gzFile in;
   1.173 +    FILE   *out;
   1.174 +{
   1.175 +    local char buf[BUFLEN];
   1.176 +    int len;
   1.177 +    int err;
   1.178 +
   1.179 +    for (;;) {
   1.180 +        len = gzread(in, buf, sizeof(buf));
   1.181 +        if (len < 0) error (gzerror(in, &err));
   1.182 +        if (len == 0) break;
   1.183 +
   1.184 +        if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
   1.185 +	    error("failed fwrite");
   1.186 +	}
   1.187 +    }
   1.188 +    if (fclose(out)) error("failed fclose");
   1.189 +
   1.190 +    if (gzclose(in) != Z_OK) error("failed gzclose");
   1.191 +}
   1.192 +
   1.193 +
   1.194 +/* ===========================================================================
   1.195 + * Compress the given file: create a corresponding .gz file and remove the
   1.196 + * original.
   1.197 + */
   1.198 +void file_compress(file, mode)
   1.199 +    char  *file;
   1.200 +    char  *mode;
   1.201 +{
   1.202 +    local char outfile[MAX_NAME_LEN];
   1.203 +    FILE  *in;
   1.204 +    gzFile out;
   1.205 +
   1.206 +    strcpy(outfile, file);
   1.207 +    strcat(outfile, GZ_SUFFIX);
   1.208 +
   1.209 +    in = fopen(file, "rb");
   1.210 +    if (in == NULL) {
   1.211 +        perror(file);
   1.212 +        exit(1);
   1.213 +    }
   1.214 +    out = gzopen(outfile, mode);
   1.215 +    if (out == NULL) {
   1.216 +        fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
   1.217 +        exit(1);
   1.218 +    }
   1.219 +    gz_compress(in, out);
   1.220 +
   1.221 +    unlink(file);
   1.222 +}
   1.223 +
   1.224 +
   1.225 +/* ===========================================================================
   1.226 + * Uncompress the given file and remove the original.
   1.227 + */
   1.228 +void file_uncompress(file)
   1.229 +    char  *file;
   1.230 +{
   1.231 +    local char buf[MAX_NAME_LEN];
   1.232 +    char *infile, *outfile;
   1.233 +    FILE  *out;
   1.234 +    gzFile in;
   1.235 +    int len = strlen(file);
   1.236 +
   1.237 +    strcpy(buf, file);
   1.238 +
   1.239 +    if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
   1.240 +        infile = file;
   1.241 +        outfile = buf;
   1.242 +        outfile[len-3] = '\0';
   1.243 +    } else {
   1.244 +        outfile = file;
   1.245 +        infile = buf;
   1.246 +        strcat(infile, GZ_SUFFIX);
   1.247 +    }
   1.248 +    in = gzopen(infile, "rb");
   1.249 +    if (in == NULL) {
   1.250 +        fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
   1.251 +        exit(1);
   1.252 +    }
   1.253 +    out = fopen(outfile, "wb");
   1.254 +    if (out == NULL) {
   1.255 +        perror(file);
   1.256 +        exit(1);
   1.257 +    }
   1.258 +
   1.259 +    gz_uncompress(in, out);
   1.260 +
   1.261 +    unlink(infile);
   1.262 +}
   1.263 +
   1.264 +
   1.265 +/* ===========================================================================
   1.266 + * Usage:  minigzip [-d] [-f] [-h] [-1 to -9] [files...]
   1.267 + *   -d : decompress
   1.268 + *   -f : compress with Z_FILTERED
   1.269 + *   -h : compress with Z_HUFFMAN_ONLY
   1.270 + *   -1 to -9 : compression level
   1.271 + */
   1.272 +
   1.273 +int main(argc, argv)
   1.274 +    int argc;
   1.275 +    char *argv[];
   1.276 +{
   1.277 +    int uncompr = 0;
   1.278 +    gzFile file;
   1.279 +    char outmode[20];
   1.280 +
   1.281 +    strcpy(outmode, "wb6 ");
   1.282 +
   1.283 +    prog = argv[0];
   1.284 +    argc--, argv++;
   1.285 +
   1.286 +    while (argc > 0) {
   1.287 +      if (strcmp(*argv, "-d") == 0)
   1.288 +	uncompr = 1;
   1.289 +      else if (strcmp(*argv, "-f") == 0)
   1.290 +	outmode[3] = 'f';
   1.291 +      else if (strcmp(*argv, "-h") == 0)
   1.292 +	outmode[3] = 'h';
   1.293 +      else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
   1.294 +	       (*argv)[2] == 0)
   1.295 +	outmode[2] = (*argv)[1];
   1.296 +      else
   1.297 +	break;
   1.298 +      argc--, argv++;
   1.299 +    }
   1.300 +    if (argc == 0) {
   1.301 +        SET_BINARY_MODE(stdin);
   1.302 +        SET_BINARY_MODE(stdout);
   1.303 +        if (uncompr) {
   1.304 +            file = gzdopen(fileno(stdin), "rb");
   1.305 +            if (file == NULL) error("can't gzdopen stdin");
   1.306 +            gz_uncompress(file, stdout);
   1.307 +        } else {
   1.308 +            file = gzdopen(fileno(stdout), outmode);
   1.309 +            if (file == NULL) error("can't gzdopen stdout");
   1.310 +            gz_compress(stdin, file);
   1.311 +        }
   1.312 +    } else {
   1.313 +        do {
   1.314 +            if (uncompr) {
   1.315 +                file_uncompress(*argv);
   1.316 +            } else {
   1.317 +                file_compress(*argv, outmode);
   1.318 +            }
   1.319 +        } while (argv++, --argc);
   1.320 +    }
   1.321 +    exit(0);
   1.322 +    return 0; /* to avoid warning */
   1.323 +}