|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _POSIX_C_SOURCE |
|
|
# define _POSIX_C_SOURCE 200112L |
|
|
#endif |
|
|
|
|
|
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS) |
|
|
# define _CRT_SECURE_NO_WARNINGS |
|
|
#endif |
|
|
#if defined(_WIN32) && !defined(_CRT_NONSTDC_NO_DEPRECATE) |
|
|
# define _CRT_NONSTDC_NO_DEPRECATE |
|
|
#endif |
|
|
|
|
|
#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(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) |
|
|
# include <fcntl.h> |
|
|
# include <io.h> |
|
|
# ifdef UNDER_CE |
|
|
# include <stdlib.h> |
|
|
# endif |
|
|
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
|
|
#else |
|
|
# define SET_BINARY_MODE(file) |
|
|
#endif |
|
|
|
|
|
#ifdef VMS |
|
|
# define unlink delete |
|
|
# define GZ_SUFFIX "-gz" |
|
|
#endif |
|
|
#if defined(__riscos) && !defined(__TARGET_UNIXLIB__) |
|
|
# define GZ_SUFFIX "/gz" |
|
|
# ifndef __GNUC__ |
|
|
# define unlink remove |
|
|
# define fileno(file) file->__file |
|
|
# endif |
|
|
#endif |
|
|
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os |
|
|
# include <unix.h> |
|
|
#endif |
|
|
|
|
|
#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) |
|
|
#ifndef WIN32 |
|
|
extern int unlink(const char *); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
#if defined(UNDER_CE) |
|
|
# include <windows.h> |
|
|
# define perror(s) pwinerror(s) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *strwinerror (error) |
|
|
DWORD error; |
|
|
{ |
|
|
static char buf[1024]; |
|
|
|
|
|
wchar_t *msgbuf; |
|
|
DWORD lasterr = GetLastError(); |
|
|
DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
| FORMAT_MESSAGE_ALLOCATE_BUFFER, |
|
|
NULL, |
|
|
error, |
|
|
0, |
|
|
(LPVOID)&msgbuf, |
|
|
0, |
|
|
NULL); |
|
|
if (chars != 0) { |
|
|
|
|
|
if (chars >= 2 |
|
|
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { |
|
|
chars -= 2; |
|
|
msgbuf[chars] = 0; |
|
|
} |
|
|
|
|
|
if (chars > sizeof (buf) - 1) { |
|
|
chars = sizeof (buf) - 1; |
|
|
msgbuf[chars] = 0; |
|
|
} |
|
|
|
|
|
wcstombs(buf, msgbuf, chars + 1); |
|
|
LocalFree(msgbuf); |
|
|
} |
|
|
else { |
|
|
sprintf(buf, "unknown win32 error (%ld)", error); |
|
|
} |
|
|
|
|
|
SetLastError(lasterr); |
|
|
return buf; |
|
|
} |
|
|
|
|
|
static void pwinerror (s) |
|
|
const char *s; |
|
|
{ |
|
|
if (s && *s) |
|
|
fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ())); |
|
|
else |
|
|
fprintf(stderr, "%s\n", strwinerror(GetLastError ())); |
|
|
} |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifndef GZ_SUFFIX |
|
|
# define GZ_SUFFIX ".gz" |
|
|
#endif |
|
|
#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) |
|
|
|
|
|
#define BUFLEN 16384 |
|
|
#define MAX_NAME_LEN 1024 |
|
|
|
|
|
#ifdef MAXSEG_64K |
|
|
# define local static |
|
|
|
|
|
#else |
|
|
# define local |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *string_copy(char *dst, char const *src, z_size_t len) { |
|
|
if (len == 0) |
|
|
return NULL; |
|
|
while (--len) { |
|
|
*dst = *src++; |
|
|
if (*dst == 0) |
|
|
return dst; |
|
|
dst++; |
|
|
} |
|
|
*dst = 0; |
|
|
return dst; |
|
|
} |
|
|
|
|
|
#ifdef Z_SOLO |
|
|
|
|
|
|
|
|
#if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) |
|
|
# include <unistd.h> |
|
|
#endif |
|
|
|
|
|
static void *myalloc(void *q, unsigned n, unsigned m) { |
|
|
(void)q; |
|
|
return calloc(n, m); |
|
|
} |
|
|
|
|
|
static void myfree(void *q, void *p) { |
|
|
(void)q; |
|
|
free(p); |
|
|
} |
|
|
|
|
|
typedef struct gzFile_s { |
|
|
FILE *file; |
|
|
int write; |
|
|
int err; |
|
|
char *msg; |
|
|
z_stream strm; |
|
|
} *gzFile; |
|
|
|
|
|
static gzFile gz_open(const char *path, int fd, const char *mode) { |
|
|
gzFile gz; |
|
|
int ret; |
|
|
|
|
|
gz = malloc(sizeof(struct gzFile_s)); |
|
|
if (gz == NULL) |
|
|
return NULL; |
|
|
gz->write = strchr(mode, 'w') != NULL; |
|
|
gz->strm.zalloc = myalloc; |
|
|
gz->strm.zfree = myfree; |
|
|
gz->strm.opaque = Z_NULL; |
|
|
if (gz->write) |
|
|
ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0); |
|
|
else { |
|
|
gz->strm.next_in = 0; |
|
|
gz->strm.avail_in = Z_NULL; |
|
|
ret = inflateInit2(&(gz->strm), 15 + 16); |
|
|
} |
|
|
if (ret != Z_OK) { |
|
|
free(gz); |
|
|
return NULL; |
|
|
} |
|
|
gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") : |
|
|
fopen(path, gz->write ? "wb" : "rb"); |
|
|
if (gz->file == NULL) { |
|
|
gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm)); |
|
|
free(gz); |
|
|
return NULL; |
|
|
} |
|
|
gz->err = 0; |
|
|
gz->msg = ""; |
|
|
return gz; |
|
|
} |
|
|
|
|
|
static gzFile gzopen(const char *path, const char *mode) { |
|
|
return gz_open(path, -1, mode); |
|
|
} |
|
|
|
|
|
static gzFile gzdopen(int fd, const char *mode) { |
|
|
return gz_open(NULL, fd, mode); |
|
|
} |
|
|
|
|
|
static int gzwrite(gzFile gz, const void *buf, unsigned len) { |
|
|
z_stream *strm; |
|
|
unsigned char out[BUFLEN]; |
|
|
|
|
|
if (gz == NULL || !gz->write) |
|
|
return 0; |
|
|
strm = &(gz->strm); |
|
|
strm->next_in = (void *)buf; |
|
|
strm->avail_in = len; |
|
|
do { |
|
|
strm->next_out = out; |
|
|
strm->avail_out = BUFLEN; |
|
|
(void)deflate(strm, Z_NO_FLUSH); |
|
|
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); |
|
|
} while (strm->avail_out == 0); |
|
|
return len; |
|
|
} |
|
|
|
|
|
static int gzread(gzFile gz, void *buf, unsigned len) { |
|
|
int ret; |
|
|
unsigned got; |
|
|
unsigned char in[1]; |
|
|
z_stream *strm; |
|
|
|
|
|
if (gz == NULL || gz->write) |
|
|
return 0; |
|
|
if (gz->err) |
|
|
return 0; |
|
|
strm = &(gz->strm); |
|
|
strm->next_out = (void *)buf; |
|
|
strm->avail_out = len; |
|
|
do { |
|
|
got = fread(in, 1, 1, gz->file); |
|
|
if (got == 0) |
|
|
break; |
|
|
strm->next_in = in; |
|
|
strm->avail_in = 1; |
|
|
ret = inflate(strm, Z_NO_FLUSH); |
|
|
if (ret == Z_DATA_ERROR) { |
|
|
gz->err = Z_DATA_ERROR; |
|
|
gz->msg = strm->msg; |
|
|
return 0; |
|
|
} |
|
|
if (ret == Z_STREAM_END) |
|
|
inflateReset(strm); |
|
|
} while (strm->avail_out); |
|
|
return len - strm->avail_out; |
|
|
} |
|
|
|
|
|
static int gzclose(gzFile gz) { |
|
|
z_stream *strm; |
|
|
unsigned char out[BUFLEN]; |
|
|
|
|
|
if (gz == NULL) |
|
|
return Z_STREAM_ERROR; |
|
|
strm = &(gz->strm); |
|
|
if (gz->write) { |
|
|
strm->next_in = Z_NULL; |
|
|
strm->avail_in = 0; |
|
|
do { |
|
|
strm->next_out = out; |
|
|
strm->avail_out = BUFLEN; |
|
|
(void)deflate(strm, Z_FINISH); |
|
|
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); |
|
|
} while (strm->avail_out == 0); |
|
|
deflateEnd(strm); |
|
|
} |
|
|
else |
|
|
inflateEnd(strm); |
|
|
fclose(gz->file); |
|
|
free(gz); |
|
|
return Z_OK; |
|
|
} |
|
|
|
|
|
static const char *gzerror(gzFile gz, int *err) { |
|
|
*err = gz->err; |
|
|
return gz->msg; |
|
|
} |
|
|
|
|
|
#endif |
|
|
|
|
|
static char *prog; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void error(const char *msg) { |
|
|
fprintf(stderr, "%s: %s\n", prog, msg); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
#ifdef USE_MMAP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int gz_compress_mmap(FILE *in, gzFile out) { |
|
|
int len; |
|
|
int err; |
|
|
int ifd = fileno(in); |
|
|
caddr_t buf; |
|
|
off_t buf_len; |
|
|
struct stat sb; |
|
|
|
|
|
|
|
|
if (fstat(ifd, &sb) < 0) return Z_ERRNO; |
|
|
buf_len = sb.st_size; |
|
|
if (buf_len <= 0) return Z_ERRNO; |
|
|
|
|
|
|
|
|
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); |
|
|
if (buf == (caddr_t)(-1)) return Z_ERRNO; |
|
|
|
|
|
|
|
|
len = gzwrite(out, (char *)buf, (unsigned)buf_len); |
|
|
|
|
|
if (len != (int)buf_len) error(gzerror(out, &err)); |
|
|
|
|
|
munmap(buf, buf_len); |
|
|
fclose(in); |
|
|
if (gzclose(out) != Z_OK) error("failed gzclose"); |
|
|
return Z_OK; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void gz_compress(FILE *in, gzFile out) { |
|
|
local char buf[BUFLEN]; |
|
|
int len; |
|
|
int err; |
|
|
|
|
|
#ifdef USE_MMAP |
|
|
|
|
|
|
|
|
|
|
|
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"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static 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"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void file_compress(char *file, char *mode) { |
|
|
local char outfile[MAX_NAME_LEN+1], *end; |
|
|
FILE *in; |
|
|
gzFile out; |
|
|
|
|
|
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { |
|
|
fprintf(stderr, "%s: filename too long\n", prog); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
end = string_copy(outfile, file, sizeof(outfile)); |
|
|
string_copy(end, GZ_SUFFIX, sizeof(outfile) - (z_size_t)(end - outfile)); |
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void file_uncompress(char *file) { |
|
|
local char buf[MAX_NAME_LEN+1]; |
|
|
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); |
|
|
} |
|
|
|
|
|
string_copy(buf, file, sizeof(buf)); |
|
|
|
|
|
if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { |
|
|
infile = file; |
|
|
outfile = buf; |
|
|
outfile[len-3] = '\0'; |
|
|
} else { |
|
|
outfile = file; |
|
|
infile = buf; |
|
|
string_copy(buf + len, GZ_SUFFIX, sizeof(buf) - len); |
|
|
} |
|
|
in = gzopen(infile, "rb"); |
|
|
if (in == NULL) { |
|
|
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); |
|
|
exit(1); |
|
|
} |
|
|
out = fopen(outfile, "wb"); |
|
|
if (out == NULL) { |
|
|
perror(file); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
gz_uncompress(in, out); |
|
|
|
|
|
unlink(infile); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
int copyout = 0; |
|
|
int uncompr = 0; |
|
|
gzFile file; |
|
|
char *bname, outmode[5]; |
|
|
|
|
|
string_copy(outmode, "wb6 ", sizeof(outmode)); |
|
|
prog = argv[0]; |
|
|
bname = strrchr(argv[0], '/'); |
|
|
if (bname) |
|
|
bname++; |
|
|
else |
|
|
bname = argv[0]; |
|
|
argc--, argv++; |
|
|
|
|
|
if (!strcmp(bname, "gunzip")) |
|
|
uncompr = 1; |
|
|
else if (!strcmp(bname, "zcat")) |
|
|
copyout = uncompr = 1; |
|
|
|
|
|
while (argc > 0) { |
|
|
if (strcmp(*argv, "-c") == 0) |
|
|
copyout = 1; |
|
|
else if (strcmp(*argv, "-d") == 0) |
|
|
uncompr = 1; |
|
|
else if (strcmp(*argv, "-f") == 0) |
|
|
outmode[3] = 'f'; |
|
|
else if (strcmp(*argv, "-h") == 0) |
|
|
outmode[3] = 'h'; |
|
|
else if (strcmp(*argv, "-r") == 0) |
|
|
outmode[3] = 'R'; |
|
|
else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && |
|
|
(*argv)[2] == 0) |
|
|
outmode[2] = (*argv)[1]; |
|
|
else |
|
|
break; |
|
|
argc--, argv++; |
|
|
} |
|
|
if (outmode[3] == ' ') |
|
|
outmode[3] = 0; |
|
|
if (argc == 0) { |
|
|
SET_BINARY_MODE(stdin); |
|
|
SET_BINARY_MODE(stdout); |
|
|
if (uncompr) { |
|
|
file = gzdopen(fileno(stdin), "rb"); |
|
|
if (file == NULL) error("can't gzdopen stdin"); |
|
|
gz_uncompress(file, stdout); |
|
|
} else { |
|
|
file = gzdopen(fileno(stdout), outmode); |
|
|
if (file == NULL) error("can't gzdopen stdout"); |
|
|
gz_compress(stdin, file); |
|
|
} |
|
|
} else { |
|
|
if (copyout) { |
|
|
SET_BINARY_MODE(stdout); |
|
|
} |
|
|
do { |
|
|
if (uncompr) { |
|
|
if (copyout) { |
|
|
file = gzopen(*argv, "rb"); |
|
|
if (file == NULL) |
|
|
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); |
|
|
else |
|
|
gz_uncompress(file, stdout); |
|
|
} else { |
|
|
file_uncompress(*argv); |
|
|
} |
|
|
} else { |
|
|
if (copyout) { |
|
|
FILE * in = fopen(*argv, "rb"); |
|
|
|
|
|
if (in == NULL) { |
|
|
perror(*argv); |
|
|
} else { |
|
|
file = gzdopen(fileno(stdout), outmode); |
|
|
if (file == NULL) error("can't gzdopen stdout"); |
|
|
|
|
|
gz_compress(in, file); |
|
|
} |
|
|
|
|
|
} else { |
|
|
file_compress(*argv, outmode); |
|
|
} |
|
|
} |
|
|
} while (argv++, --argc); |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|