1 /* minigzip.c contains minimal changes required to be compiled with zlibWrapper:
2 * - #include "zlib.h" was changed to #include "zstd_zlibwrapper.h" */
4 /* minigzip.c -- simulate gzip using the zlib compression library
5 * Copyright (C) 1995-2006, 2010, 2011 Jean-loup Gailly.
6 * For conditions of distribution and use, see http://www.zlib.net/zlib_license.html
10 * minigzip is a minimal implementation of the gzip utility. This is
11 * only an example of using zlib and isn't meant to replace the
12 * full-featured gzip. No attempt is made to deal with file systems
13 * limiting names to 14 or 8+3 characters, etc... Error checking is
14 * very limited. So use minigzip only for testing; use gzip for the
15 * real thing. On MSDOS, use only on file names without extension
21 #include "zstd_zlibwrapper.h"
30 # include <sys/types.h>
31 # include <sys/mman.h>
32 # include <sys/stat.h>
35 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
41 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
43 # define SET_BINARY_MODE(file)
47 # define snprintf _snprintf
51 # define unlink delete
52 # define GZ_SUFFIX "-gz"
55 # define unlink remove
56 # define GZ_SUFFIX "-gz"
57 # define fileno(file) file->__file
59 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
60 # include <unix.h> /* for fileno */
63 #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
64 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
65 extern int unlink OF((const char *));
71 # define perror(s) pwinerror(s)
73 /* Map the Windows error number in ERROR to a locale-dependent error
74 message string and return a pointer to it. Typically, the values
75 for ERROR come from GetLastError.
77 The string pointed to shall not be modified by the application,
78 but may be overwritten by a subsequent call to strwinerror
80 The strwinerror function does not change the current setting
83 static char *strwinerror (error)
86 static char buf[1024];
89 DWORD lasterr = GetLastError();
90 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
91 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
94 0, /* Default language */
99 /* If there is an \r\n appended, zap it. */
101 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
106 if (chars > sizeof (buf) - 1) {
107 chars = sizeof (buf) - 1;
111 wcstombs(buf, msgbuf, chars + 1);
115 sprintf(buf, "unknown win32 error (%ld)", error);
118 SetLastError(lasterr);
122 static void pwinerror (s)
126 fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
128 fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
131 #endif /* UNDER_CE */
134 # define GZ_SUFFIX ".gz"
136 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
139 #define MAX_NAME_LEN 1024
142 # define local static
143 /* Needed for systems with limitation on stack size. */
149 /* for Z_SOLO, create simplified gz* functions using deflate and inflate */
151 #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
152 # include <unistd.h> /* for unlink() */
155 void *myalloc OF((void *, unsigned, unsigned));
156 void myfree OF((void *, void *));
158 void *myalloc(q, n, m)
173 typedef struct gzFile_s {
181 gzFile gzopen OF((const char *, const char *));
182 gzFile gzdopen OF((int, const char *));
183 gzFile gz_open OF((const char *, int, const char *));
185 gzFile gzopen(path, mode)
189 return gz_open(path, -1, mode);
192 gzFile gzdopen(fd, mode)
196 return gz_open(NULL, fd, mode);
199 gzFile gz_open(path, fd, mode)
207 gz = malloc(sizeof(struct gzFile_s));
210 gz->write = strchr(mode, 'w') != NULL;
211 gz->strm.zalloc = myalloc;
212 gz->strm.zfree = myfree;
213 gz->strm.opaque = Z_NULL;
215 ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
217 gz->strm.next_in = 0;
218 gz->strm.avail_in = Z_NULL;
219 ret = inflateInit2(&(gz->strm), 15 + 16);
225 gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
226 fopen(path, gz->write ? "wb" : "rb");
227 if (gz->file == NULL) {
228 gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
237 int gzwrite OF((gzFile, const void *, unsigned));
239 int gzwrite(gz, buf, len)
245 unsigned char out[BUFLEN];
247 if (gz == NULL || !gz->write)
250 strm->next_in = (void *)buf;
251 strm->avail_in = len;
253 strm->next_out = out;
254 strm->avail_out = BUFLEN;
255 (void)deflate(strm, Z_NO_FLUSH);
256 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
257 } while (strm->avail_out == 0);
261 int gzread OF((gzFile, void *, unsigned));
263 int gzread(gz, buf, len)
273 if (gz == NULL || gz->write)
278 strm->next_out = (void *)buf;
279 strm->avail_out = len;
281 got = fread(in, 1, 1, gz->file);
286 ret = inflate(strm, Z_NO_FLUSH);
287 if (ret == Z_DATA_ERROR) {
288 gz->err = Z_DATA_ERROR;
292 if (ret == Z_STREAM_END)
294 } while (strm->avail_out);
295 return len - strm->avail_out;
298 int gzclose OF((gzFile));
304 unsigned char out[BUFLEN];
307 return Z_STREAM_ERROR;
310 strm->next_in = Z_NULL;
313 strm->next_out = out;
314 strm->avail_out = BUFLEN;
315 (void)deflate(strm, Z_FINISH);
316 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
317 } while (strm->avail_out == 0);
327 const char *gzerror OF((gzFile, int *));
329 const char *gzerror(gz, err)
341 void error OF((const char *msg));
342 void gz_compress OF((FILE *in, gzFile out));
344 int gz_compress_mmap OF((FILE *in, gzFile out));
346 void gz_uncompress OF((gzFile in, FILE *out));
347 void file_compress OF((char *file, char *mode));
348 void file_uncompress OF((char *file));
349 int main OF((int argc, char *argv[]));
351 /* ===========================================================================
352 * Display error message and exit
357 fprintf(stderr, "%s: %s\n", prog, msg);
361 /* ===========================================================================
362 * Compress input to output then close both files.
365 void gz_compress(in, out)
369 local char buf[BUFLEN];
374 /* Try first compressing with mmap. If mmap fails (minigzip used in a
375 * pipe), use the normal fread loop.
377 if (gz_compress_mmap(in, out) == Z_OK) return;
380 len = (int)fread(buf, 1, sizeof(buf), in);
387 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
390 if (gzclose(out) != Z_OK) error("failed gzclose");
393 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
395 /* Try compressing the input file at once using mmap. Return Z_OK if
396 * if success, Z_ERRNO otherwise.
398 int gz_compress_mmap(in, out)
404 int ifd = fileno(in);
405 caddr_t buf; /* mmap'ed buffer for the entire input file */
406 off_t buf_len; /* length of the input file */
409 /* Determine the size of the file, needed for mmap: */
410 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
411 buf_len = sb.st_size;
412 if (buf_len <= 0) return Z_ERRNO;
414 /* Now do the actual mmap: */
415 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
416 if (buf == (caddr_t)(-1)) return Z_ERRNO;
418 /* Compress the whole file at once: */
419 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
421 if (len != (int)buf_len) error(gzerror(out, &err));
423 munmap(buf, buf_len);
425 if (gzclose(out) != Z_OK) error("failed gzclose");
428 #endif /* USE_MMAP */
430 /* ===========================================================================
431 * Uncompress input to output then close both files.
433 void gz_uncompress(in, out)
437 local char buf[BUFLEN];
442 len = gzread(in, buf, sizeof(buf));
443 if (len < 0) error (gzerror(in, &err));
446 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
447 error("failed fwrite");
450 if (fclose(out)) error("failed fclose");
452 if (gzclose(in) != Z_OK) error("failed gzclose");
456 /* ===========================================================================
457 * Compress the given file: create a corresponding .gz file and remove the
460 void file_compress(file, mode)
464 local char outfile[MAX_NAME_LEN];
468 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
469 fprintf(stderr, "%s: filename too long\n", prog);
473 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
474 snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX);
476 strcpy(outfile, file);
477 strcat(outfile, GZ_SUFFIX);
480 in = fopen(file, "rb");
485 out = gzopen(outfile, mode);
487 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
490 gz_compress(in, out);
496 /* ===========================================================================
497 * Uncompress the given file and remove the original.
499 void file_uncompress(file)
502 local char buf[MAX_NAME_LEN];
503 char *infile, *outfile;
506 size_t len = strlen(file);
508 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
509 fprintf(stderr, "%s: filename too long\n", prog);
513 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
514 snprintf(buf, sizeof(buf), "%s", file);
519 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
522 outfile[len-3] = '\0';
526 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
527 snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX);
529 strcat(infile, GZ_SUFFIX);
532 in = gzopen(infile, "rb");
534 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
537 out = fopen(outfile, "wb");
543 gz_uncompress(in, out);
549 /* ===========================================================================
550 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
551 * -c : write to standard output
553 * -f : compress with Z_FILTERED
554 * -h : compress with Z_HUFFMAN_ONLY
555 * -r : compress with Z_RLE
556 * -1 to -9 : compression level
566 char *bname, outmode[20];
568 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
569 snprintf(outmode, sizeof(outmode), "%s", "wb6 ");
571 strcpy(outmode, "wb6 ");
575 bname = strrchr(argv[0], '/');
582 if (!strcmp(bname, "gunzip"))
584 else if (!strcmp(bname, "zcat"))
585 copyout = uncompr = 1;
588 if (strcmp(*argv, "-c") == 0)
590 else if (strcmp(*argv, "-d") == 0)
592 else if (strcmp(*argv, "-f") == 0)
594 else if (strcmp(*argv, "-h") == 0)
596 else if (strcmp(*argv, "-r") == 0)
598 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
600 outmode[2] = (*argv)[1];
605 if (outmode[3] == ' ')
608 SET_BINARY_MODE(stdin);
609 SET_BINARY_MODE(stdout);
611 file = gzdopen(fileno(stdin), "rb");
612 if (file == NULL) error("can't gzdopen stdin");
613 gz_uncompress(file, stdout);
615 file = gzdopen(fileno(stdout), outmode);
616 if (file == NULL) error("can't gzdopen stdout");
617 gz_compress(stdin, file);
621 SET_BINARY_MODE(stdout);
626 file = gzopen(*argv, "rb");
628 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
630 gz_uncompress(file, stdout);
632 file_uncompress(*argv);
636 FILE * in = fopen(*argv, "rb");
641 file = gzdopen(fileno(stdout), outmode);
642 if (file == NULL) error("can't gzdopen stdout");
644 gz_compress(in, file);
648 file_compress(*argv, outmode);
651 } while (argv++, --argc);