/* minigzip.c -- simulate gzip using the zlib compression library * Copyright (C) 1995-2006, 2010, 2011, 2016 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h
*/
/* * minigzip is a minimal implementation of the gzip utility. This is * only an example of using zlib and isn't meant to replace the * full-featured gzip. No attempt is made to deal with file systems * limiting names to 14 or 8+3 characters, etc... Error checking is * very limited. So use minigzip only for testing; use gzip for the * real thing. On MSDOS, use only on file names without extension * or in pipe mode.
*/
/* @(#) $Id$ */
#include"zlib.h" #include <stdio.h>
#ifdef STDC # include <string.h> # include <stdlib.h> #endif
#ifdef USE_MMAP # include <sys/types.h> # include <sys/mman.h> # include <sys/stat.h> #endif
#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) #ifndef WIN32 /* unlink already in stdio.h for WIN32 */ externint unlink(constchar *); #endif #endif
#ifdefined(UNDER_CE) # include <windows.h> # define perror(s) pwinerror(s)
/* Map the Windows error number in ERROR to a locale-dependent error message string and return a pointer to it. Typically, the values for ERROR come from GetLastError.
The string pointed to shall not be modified by the application, but may be overwritten by a subsequent call to strwinerror
The strwinerror function does not change the current setting
of GetLastError. */
#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
/* Try compressing the input file at once using mmap. Return Z_OK if * if success, Z_ERRNO otherwise.
*/ int gz_compress_mmap(FILE *in, gzFile out) { int len; int err; int ifd = fileno(in);
caddr_t buf; /* mmap'ed buffer for the entire input file */
off_t buf_len; /* length of the input file */ struct stat sb;
/* Determine the size of the file, needed for mmap: */ if (fstat(ifd, &sb) < 0) return Z_ERRNO;
buf_len = sb.st_size; if (buf_len <= 0) return Z_ERRNO;
/* Now do the actual mmap: */
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); if (buf == (caddr_t)(-1)) return Z_ERRNO;
/* Compress the whole file at once: */
len = gzwrite(out, (char *)buf, (unsigned)buf_len);
if (len != (int)buf_len) error(gzerror(out, &err));
/* =========================================================================== * Compress input to output then close both files.
*/
void gz_compress(FILE *in, gzFile out) {
local char buf[BUFLEN]; int len; int err;
#ifdef USE_MMAP /* Try first compressing with mmap. If mmap fails (minigzip used in a * pipe), use the normal fread loop.
*/ if (gz_compress_mmap(in, out) == Z_OK) return; #endif for (;;) {
len = (int)fread(buf, 1, sizeof(buf), in); if (ferror(in)) {
perror("fread"); exit(1);
} if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
}
fclose(in); if (gzclose(out) != Z_OK) error("failed gzclose");
}
/* =========================================================================== * Uncompress input to output then close both files.
*/ void gz_uncompress(gzFile in, FILE *out) {
local char buf[BUFLEN]; int len; int err;
for (;;) {
len = gzread(in, buf, sizeof(buf)); if (len < 0) error (gzerror(in, &err)); if (len == 0) break;
if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
error("failed fwrite");
}
} if (fclose(out)) error("failed fclose");
if (gzclose(in) != Z_OK) error("failed gzclose");
}
/* =========================================================================== * Compress the given file: create a corresponding .gz file and remove the * original.
*/ void file_compress(char *file, char *mode) {
local char outfile[MAX_NAME_LEN];
FILE *in;
gzFile out;
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
fprintf(stderr, "%s: filename too long\n", prog); exit(1);
}
in = fopen(file, "rb"); if (in == NULL) {
perror(file); exit(1);
}
out = gzopen(outfile, mode); if (out == NULL) {
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); exit(1);
}
gz_compress(in, out);
unlink(file);
}
/* =========================================================================== * Uncompress the given file and remove the original.
*/ void file_uncompress(char *file) {
local char buf[MAX_NAME_LEN]; char *infile, *outfile;
FILE *out;
gzFile in;
z_size_t len = strlen(file);
if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
fprintf(stderr, "%s: filename too long\n", prog); exit(1);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.