michael@0: /* minigzip.c -- simulate gzip using the zlib compression library michael@0: * Copyright (C) 1995-2006, 2010 Jean-loup Gailly. michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: /* michael@0: * minigzip is a minimal implementation of the gzip utility. This is michael@0: * only an example of using zlib and isn't meant to replace the michael@0: * full-featured gzip. No attempt is made to deal with file systems michael@0: * limiting names to 14 or 8+3 characters, etc... Error checking is michael@0: * very limited. So use minigzip only for testing; use gzip for the michael@0: * real thing. On MSDOS, use only on file names without extension michael@0: * or in pipe mode. michael@0: */ michael@0: michael@0: /* @(#) $Id$ */ michael@0: michael@0: #include "zlib.h" michael@0: #include michael@0: michael@0: #ifdef STDC michael@0: # include michael@0: # include michael@0: #endif michael@0: michael@0: #ifdef USE_MMAP michael@0: # include michael@0: # include michael@0: # include michael@0: #endif michael@0: michael@0: #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) michael@0: # include michael@0: # include michael@0: # ifdef UNDER_CE michael@0: # include michael@0: # endif michael@0: # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) michael@0: #else michael@0: # define SET_BINARY_MODE(file) michael@0: #endif michael@0: michael@0: #ifdef VMS michael@0: # define unlink delete michael@0: # define GZ_SUFFIX "-gz" michael@0: #endif michael@0: #ifdef RISCOS michael@0: # define unlink remove michael@0: # define GZ_SUFFIX "-gz" michael@0: # define fileno(file) file->__file michael@0: #endif michael@0: #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os michael@0: # include /* for fileno */ michael@0: #endif michael@0: michael@0: #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) michael@0: #ifndef WIN32 /* unlink already in stdio.h for WIN32 */ michael@0: extern int unlink OF((const char *)); michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(UNDER_CE) michael@0: # include michael@0: # define perror(s) pwinerror(s) michael@0: michael@0: /* Map the Windows error number in ERROR to a locale-dependent error michael@0: message string and return a pointer to it. Typically, the values michael@0: for ERROR come from GetLastError. michael@0: michael@0: The string pointed to shall not be modified by the application, michael@0: but may be overwritten by a subsequent call to strwinerror michael@0: michael@0: The strwinerror function does not change the current setting michael@0: of GetLastError. */ michael@0: michael@0: static char *strwinerror (error) michael@0: DWORD error; michael@0: { michael@0: static char buf[1024]; michael@0: michael@0: wchar_t *msgbuf; michael@0: DWORD lasterr = GetLastError(); michael@0: DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM michael@0: | FORMAT_MESSAGE_ALLOCATE_BUFFER, michael@0: NULL, michael@0: error, michael@0: 0, /* Default language */ michael@0: (LPVOID)&msgbuf, michael@0: 0, michael@0: NULL); michael@0: if (chars != 0) { michael@0: /* If there is an \r\n appended, zap it. */ michael@0: if (chars >= 2 michael@0: && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { michael@0: chars -= 2; michael@0: msgbuf[chars] = 0; michael@0: } michael@0: michael@0: if (chars > sizeof (buf) - 1) { michael@0: chars = sizeof (buf) - 1; michael@0: msgbuf[chars] = 0; michael@0: } michael@0: michael@0: wcstombs(buf, msgbuf, chars + 1); michael@0: LocalFree(msgbuf); michael@0: } michael@0: else { michael@0: sprintf(buf, "unknown win32 error (%ld)", error); michael@0: } michael@0: michael@0: SetLastError(lasterr); michael@0: return buf; michael@0: } michael@0: michael@0: static void pwinerror (s) michael@0: const char *s; michael@0: { michael@0: if (s && *s) michael@0: fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ())); michael@0: else michael@0: fprintf(stderr, "%s\n", strwinerror(GetLastError ())); michael@0: } michael@0: michael@0: #endif /* UNDER_CE */ michael@0: michael@0: #ifndef GZ_SUFFIX michael@0: # define GZ_SUFFIX ".gz" michael@0: #endif michael@0: #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) michael@0: michael@0: #define BUFLEN 16384 michael@0: #define MAX_NAME_LEN 1024 michael@0: michael@0: #ifdef MAXSEG_64K michael@0: # define local static michael@0: /* Needed for systems with limitation on stack size. */ michael@0: #else michael@0: # define local michael@0: #endif michael@0: michael@0: char *prog; michael@0: michael@0: void error OF((const char *msg)); michael@0: void gz_compress OF((FILE *in, gzFile out)); michael@0: #ifdef USE_MMAP michael@0: int gz_compress_mmap OF((FILE *in, gzFile out)); michael@0: #endif michael@0: void gz_uncompress OF((gzFile in, FILE *out)); michael@0: void file_compress OF((char *file, char *mode)); michael@0: void file_uncompress OF((char *file)); michael@0: int main OF((int argc, char *argv[])); michael@0: michael@0: /* =========================================================================== michael@0: * Display error message and exit michael@0: */ michael@0: void error(msg) michael@0: const char *msg; michael@0: { michael@0: fprintf(stderr, "%s: %s\n", prog, msg); michael@0: exit(1); michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Compress input to output then close both files. michael@0: */ michael@0: michael@0: void gz_compress(in, out) michael@0: FILE *in; michael@0: gzFile out; michael@0: { michael@0: local char buf[BUFLEN]; michael@0: int len; michael@0: int err; michael@0: michael@0: #ifdef USE_MMAP michael@0: /* Try first compressing with mmap. If mmap fails (minigzip used in a michael@0: * pipe), use the normal fread loop. michael@0: */ michael@0: if (gz_compress_mmap(in, out) == Z_OK) return; michael@0: #endif michael@0: for (;;) { michael@0: len = (int)fread(buf, 1, sizeof(buf), in); michael@0: if (ferror(in)) { michael@0: perror("fread"); michael@0: exit(1); michael@0: } michael@0: if (len == 0) break; michael@0: michael@0: if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); michael@0: } michael@0: fclose(in); michael@0: if (gzclose(out) != Z_OK) error("failed gzclose"); michael@0: } michael@0: michael@0: #ifdef USE_MMAP /* MMAP version, Miguel Albrecht */ michael@0: michael@0: /* Try compressing the input file at once using mmap. Return Z_OK if michael@0: * if success, Z_ERRNO otherwise. michael@0: */ michael@0: int gz_compress_mmap(in, out) michael@0: FILE *in; michael@0: gzFile out; michael@0: { michael@0: int len; michael@0: int err; michael@0: int ifd = fileno(in); michael@0: caddr_t buf; /* mmap'ed buffer for the entire input file */ michael@0: off_t buf_len; /* length of the input file */ michael@0: struct stat sb; michael@0: michael@0: /* Determine the size of the file, needed for mmap: */ michael@0: if (fstat(ifd, &sb) < 0) return Z_ERRNO; michael@0: buf_len = sb.st_size; michael@0: if (buf_len <= 0) return Z_ERRNO; michael@0: michael@0: /* Now do the actual mmap: */ michael@0: buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); michael@0: if (buf == (caddr_t)(-1)) return Z_ERRNO; michael@0: michael@0: /* Compress the whole file at once: */ michael@0: len = gzwrite(out, (char *)buf, (unsigned)buf_len); michael@0: michael@0: if (len != (int)buf_len) error(gzerror(out, &err)); michael@0: michael@0: munmap(buf, buf_len); michael@0: fclose(in); michael@0: if (gzclose(out) != Z_OK) error("failed gzclose"); michael@0: return Z_OK; michael@0: } michael@0: #endif /* USE_MMAP */ michael@0: michael@0: /* =========================================================================== michael@0: * Uncompress input to output then close both files. michael@0: */ michael@0: void gz_uncompress(in, out) michael@0: gzFile in; michael@0: FILE *out; michael@0: { michael@0: local char buf[BUFLEN]; michael@0: int len; michael@0: int err; michael@0: michael@0: for (;;) { michael@0: len = gzread(in, buf, sizeof(buf)); michael@0: if (len < 0) error (gzerror(in, &err)); michael@0: if (len == 0) break; michael@0: michael@0: if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { michael@0: error("failed fwrite"); michael@0: } michael@0: } michael@0: if (fclose(out)) error("failed fclose"); michael@0: michael@0: if (gzclose(in) != Z_OK) error("failed gzclose"); michael@0: } michael@0: michael@0: michael@0: /* =========================================================================== michael@0: * Compress the given file: create a corresponding .gz file and remove the michael@0: * original. michael@0: */ michael@0: void file_compress(file, mode) michael@0: char *file; michael@0: char *mode; michael@0: { michael@0: local char outfile[MAX_NAME_LEN]; michael@0: FILE *in; michael@0: gzFile out; michael@0: michael@0: if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { michael@0: fprintf(stderr, "%s: filename too long\n", prog); michael@0: exit(1); michael@0: } michael@0: michael@0: strcpy(outfile, file); michael@0: strcat(outfile, GZ_SUFFIX); michael@0: michael@0: in = fopen(file, "rb"); michael@0: if (in == NULL) { michael@0: perror(file); michael@0: exit(1); michael@0: } michael@0: out = gzopen(outfile, mode); michael@0: if (out == NULL) { michael@0: fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); michael@0: exit(1); michael@0: } michael@0: gz_compress(in, out); michael@0: michael@0: unlink(file); michael@0: } michael@0: michael@0: michael@0: /* =========================================================================== michael@0: * Uncompress the given file and remove the original. michael@0: */ michael@0: void file_uncompress(file) michael@0: char *file; michael@0: { michael@0: local char buf[MAX_NAME_LEN]; michael@0: char *infile, *outfile; michael@0: FILE *out; michael@0: gzFile in; michael@0: size_t len = strlen(file); michael@0: michael@0: if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) { michael@0: fprintf(stderr, "%s: filename too long\n", prog); michael@0: exit(1); michael@0: } michael@0: michael@0: strcpy(buf, file); michael@0: michael@0: if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { michael@0: infile = file; michael@0: outfile = buf; michael@0: outfile[len-3] = '\0'; michael@0: } else { michael@0: outfile = file; michael@0: infile = buf; michael@0: strcat(infile, GZ_SUFFIX); michael@0: } michael@0: in = gzopen(infile, "rb"); michael@0: if (in == NULL) { michael@0: fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); michael@0: exit(1); michael@0: } michael@0: out = fopen(outfile, "wb"); michael@0: if (out == NULL) { michael@0: perror(file); michael@0: exit(1); michael@0: } michael@0: michael@0: gz_uncompress(in, out); michael@0: michael@0: unlink(infile); michael@0: } michael@0: michael@0: michael@0: /* =========================================================================== michael@0: * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...] michael@0: * -c : write to standard output michael@0: * -d : decompress michael@0: * -f : compress with Z_FILTERED michael@0: * -h : compress with Z_HUFFMAN_ONLY michael@0: * -r : compress with Z_RLE michael@0: * -1 to -9 : compression level michael@0: */ michael@0: michael@0: int main(argc, argv) michael@0: int argc; michael@0: char *argv[]; michael@0: { michael@0: int copyout = 0; michael@0: int uncompr = 0; michael@0: gzFile file; michael@0: char *bname, outmode[20]; michael@0: michael@0: strcpy(outmode, "wb6 "); michael@0: michael@0: prog = argv[0]; michael@0: bname = strrchr(argv[0], '/'); michael@0: if (bname) michael@0: bname++; michael@0: else michael@0: bname = argv[0]; michael@0: argc--, argv++; michael@0: michael@0: if (!strcmp(bname, "gunzip")) michael@0: uncompr = 1; michael@0: else if (!strcmp(bname, "zcat")) michael@0: copyout = uncompr = 1; michael@0: michael@0: while (argc > 0) { michael@0: if (strcmp(*argv, "-c") == 0) michael@0: copyout = 1; michael@0: else if (strcmp(*argv, "-d") == 0) michael@0: uncompr = 1; michael@0: else if (strcmp(*argv, "-f") == 0) michael@0: outmode[3] = 'f'; michael@0: else if (strcmp(*argv, "-h") == 0) michael@0: outmode[3] = 'h'; michael@0: else if (strcmp(*argv, "-r") == 0) michael@0: outmode[3] = 'R'; michael@0: else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && michael@0: (*argv)[2] == 0) michael@0: outmode[2] = (*argv)[1]; michael@0: else michael@0: break; michael@0: argc--, argv++; michael@0: } michael@0: if (outmode[3] == ' ') michael@0: outmode[3] = 0; michael@0: if (argc == 0) { michael@0: SET_BINARY_MODE(stdin); michael@0: SET_BINARY_MODE(stdout); michael@0: if (uncompr) { michael@0: file = gzdopen(fileno(stdin), "rb"); michael@0: if (file == NULL) error("can't gzdopen stdin"); michael@0: gz_uncompress(file, stdout); michael@0: } else { michael@0: file = gzdopen(fileno(stdout), outmode); michael@0: if (file == NULL) error("can't gzdopen stdout"); michael@0: gz_compress(stdin, file); michael@0: } michael@0: } else { michael@0: if (copyout) { michael@0: SET_BINARY_MODE(stdout); michael@0: } michael@0: do { michael@0: if (uncompr) { michael@0: if (copyout) { michael@0: file = gzopen(*argv, "rb"); michael@0: if (file == NULL) michael@0: fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); michael@0: else michael@0: gz_uncompress(file, stdout); michael@0: } else { michael@0: file_uncompress(*argv); michael@0: } michael@0: } else { michael@0: if (copyout) { michael@0: FILE * in = fopen(*argv, "rb"); michael@0: michael@0: if (in == NULL) { michael@0: perror(*argv); michael@0: } else { michael@0: file = gzdopen(fileno(stdout), outmode); michael@0: if (file == NULL) error("can't gzdopen stdout"); michael@0: michael@0: gz_compress(in, file); michael@0: } michael@0: michael@0: } else { michael@0: file_compress(*argv, outmode); michael@0: } michael@0: } michael@0: } while (argv++, --argc); michael@0: } michael@0: return 0; michael@0: }