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 https://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 #define _POSIX_SOURCE /* fileno */
23 #include "zstd_zlibwrapper.h"
32 # include <sys/types.h>
33 # include <sys/mman.h>
34 # include <sys/stat.h>
37 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
43 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
45 # define SET_BINARY_MODE(file)
49 # define snprintf _snprintf
53 # define unlink delete
54 # define GZ_SUFFIX "-gz"
57 # define unlink remove
58 # define GZ_SUFFIX "-gz"
59 # define fileno(file) file->__file
61 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
62 # include <unix.h> /* for fileno */
65 #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
66 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
67 extern int unlink _Z_OF((const char *));
73 # define perror(s) pwinerror(s)
75 /* Map the Windows error number in ERROR to a locale-dependent error
76 message string and return a pointer to it. Typically, the values
77 for ERROR come from GetLastError.
79 The string pointed to shall not be modified by the application,
80 but may be overwritten by a subsequent call to strwinerror
82 The strwinerror function does not change the current setting
85 static char *strwinerror (error)
88 static char buf[1024];
91 DWORD lasterr = GetLastError();
92 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
93 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
96 0, /* Default language */
101 /* If there is an \r\n appended, zap it. */
103 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
108 if (chars > sizeof (buf) - 1) {
109 chars = sizeof (buf) - 1;
113 wcstombs(buf, msgbuf, chars + 1);
117 sprintf(buf, "unknown win32 error (%ld)", error);
120 SetLastError(lasterr);
124 static void pwinerror (s)
128 fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
130 fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
133 #endif /* UNDER_CE */
136 # define GZ_SUFFIX ".gz"
138 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
141 #define MAX_NAME_LEN 1024
144 # define local static
145 /* Needed for systems with limitation on stack size. */
151 /* for Z_SOLO, create simplified gz* functions using deflate and inflate */
153 #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
154 # include <unistd.h> /* for unlink() */
157 void *myalloc _Z_OF((void *, unsigned, unsigned));
158 void myfree _Z_OF((void *, void *));
160 void *myalloc(q, n, m)
175 typedef struct gzFile_s {
183 gzFile gzopen _Z_OF((const char *, const char *));
184 gzFile gzdopen _Z_OF((int, const char *));
185 gzFile gz_open _Z_OF((const char *, int, const char *));
187 gzFile gzopen(path, mode)
191 return gz_open(path, -1, mode);
194 gzFile gzdopen(fd, mode)
198 return gz_open(NULL, fd, mode);
201 gzFile gz_open(path, fd, mode)
209 gz = malloc(sizeof(struct gzFile_s));
212 gz->write = strchr(mode, 'w') != NULL;
213 gz->strm.zalloc = myalloc;
214 gz->strm.zfree = myfree;
215 gz->strm.opaque = Z_NULL;
217 ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
219 gz->strm.next_in = 0;
220 gz->strm.avail_in = Z_NULL;
221 ret = inflateInit2(&(gz->strm), 15 + 16);
227 gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
228 fopen(path, gz->write ? "wb" : "rb");
229 if (gz->file == NULL) {
230 gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
239 int gzwrite _Z_OF((gzFile, const void *, unsigned));
241 int gzwrite(gz, buf, len)
247 unsigned char out[BUFLEN];
249 if (gz == NULL || !gz->write)
252 strm->next_in = (void *)buf;
253 strm->avail_in = len;
255 strm->next_out = out;
256 strm->avail_out = BUFLEN;
257 (void)deflate(strm, Z_NO_FLUSH);
258 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
259 } while (strm->avail_out == 0);
263 int gzread _Z_OF((gzFile, void *, unsigned));
265 int gzread(gz, buf, len)
275 if (gz == NULL || gz->write)
280 strm->next_out = (void *)buf;
281 strm->avail_out = len;
283 got = fread(in, 1, 1, gz->file);
288 ret = inflate(strm, Z_NO_FLUSH);
289 if (ret == Z_DATA_ERROR) {
290 gz->err = Z_DATA_ERROR;
294 if (ret == Z_STREAM_END)
296 } while (strm->avail_out);
297 return len - strm->avail_out;
300 int gzclose _Z_OF((gzFile));
306 unsigned char out[BUFLEN];
309 return Z_STREAM_ERROR;
312 strm->next_in = Z_NULL;
315 strm->next_out = out;
316 strm->avail_out = BUFLEN;
317 (void)deflate(strm, Z_FINISH);
318 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
319 } while (strm->avail_out == 0);
329 const char *gzerror _Z_OF((gzFile, int *));
331 const char *gzerror(gz, err)
343 void error _Z_OF((const char *msg));
344 void gz_compress _Z_OF((FILE *in, gzFile out));
346 int gz_compress_mmap _Z_OF((FILE *in, gzFile out));
348 void gz_uncompress _Z_OF((gzFile in, FILE *out));
349 void file_compress _Z_OF((char *file, char *mode));
350 void file_uncompress _Z_OF((char *file));
351 int main _Z_OF((int argc, char *argv[]));
353 /* ===========================================================================
354 * Display error message and exit
359 fprintf(stderr, "%s: %s\n", prog, msg);
363 /* ===========================================================================
364 * Compress input to output then close both files.
367 void gz_compress(in, out)
371 local char buf[BUFLEN];
376 /* Try first compressing with mmap. If mmap fails (minigzip used in a
377 * pipe), use the normal fread loop.
379 if (gz_compress_mmap(in, out) == Z_OK) return;
382 len = (int)fread(buf, 1, sizeof(buf), in);
389 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
392 if (gzclose(out) != Z_OK) error("failed gzclose");
395 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
397 /* Try compressing the input file at once using mmap. Return Z_OK if
398 * if success, Z_ERRNO otherwise.
400 int gz_compress_mmap(in, out)
406 int ifd = fileno(in);
407 caddr_t buf; /* mmap'ed buffer for the entire input file */
408 off_t buf_len; /* length of the input file */
411 /* Determine the size of the file, needed for mmap: */
412 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
413 buf_len = sb.st_size;
414 if (buf_len <= 0) return Z_ERRNO;
416 /* Now do the actual mmap: */
417 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
418 if (buf == (caddr_t)(-1)) return Z_ERRNO;
420 /* Compress the whole file at once: */
421 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
423 if (len != (int)buf_len) error(gzerror(out, &err));
425 munmap(buf, buf_len);
427 if (gzclose(out) != Z_OK) error("failed gzclose");
430 #endif /* USE_MMAP */
432 /* ===========================================================================
433 * Uncompress input to output then close both files.
435 void gz_uncompress(in, out)
439 local char buf[BUFLEN];
444 len = gzread(in, buf, sizeof(buf));
445 if (len < 0) error (gzerror(in, &err));
448 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
449 error("failed fwrite");
452 if (fclose(out)) error("failed fclose");
454 if (gzclose(in) != Z_OK) error("failed gzclose");
458 /* ===========================================================================
459 * Compress the given file: create a corresponding .gz file and remove the
462 void file_compress(file, mode)
466 local char outfile[MAX_NAME_LEN];
470 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
471 fprintf(stderr, "%s: filename too long\n", prog);
475 strcpy(outfile, file);
476 strcat(outfile, GZ_SUFFIX);
478 in = fopen(file, "rb");
483 out = gzopen(outfile, mode);
485 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
488 gz_compress(in, out);
494 /* ===========================================================================
495 * Uncompress the given file and remove the original.
497 void file_uncompress(file)
500 local char buf[MAX_NAME_LEN];
501 char *infile, *outfile;
504 size_t len = strlen(file);
506 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
507 fprintf(stderr, "%s: filename too long\n", prog);
513 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
516 outfile[len-3] = '\0';
520 strcat(infile, GZ_SUFFIX);
522 in = gzopen(infile, "rb");
524 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
527 out = fopen(outfile, "wb");
533 gz_uncompress(in, out);
539 /* ===========================================================================
540 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
541 * -c : write to standard output
543 * -f : compress with Z_FILTERED
544 * -h : compress with Z_HUFFMAN_ONLY
545 * -r : compress with Z_RLE
546 * -1 to -9 : compression level
556 char *bname, outmode[20];
558 strcpy(outmode, "wb6 ");
561 bname = strrchr(argv[0], '/');
568 if (!strcmp(bname, "gunzip"))
570 else if (!strcmp(bname, "zcat"))
571 copyout = uncompr = 1;
574 if (strcmp(*argv, "-c") == 0)
576 else if (strcmp(*argv, "-d") == 0)
578 else if (strcmp(*argv, "-f") == 0)
580 else if (strcmp(*argv, "-h") == 0)
582 else if (strcmp(*argv, "-r") == 0)
584 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
586 outmode[2] = (*argv)[1];
591 if (outmode[3] == ' ')
594 SET_BINARY_MODE(stdin);
595 SET_BINARY_MODE(stdout);
597 file = gzdopen(fileno(stdin), "rb");
598 if (file == NULL) error("can't gzdopen stdin");
599 gz_uncompress(file, stdout);
601 file = gzdopen(fileno(stdout), outmode);
602 if (file == NULL) error("can't gzdopen stdout");
603 gz_compress(stdin, file);
607 SET_BINARY_MODE(stdout);
612 file = gzopen(*argv, "rb");
614 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
616 gz_uncompress(file, stdout);
618 file_uncompress(*argv);
622 FILE * in = fopen(*argv, "rb");
627 file = gzdopen(fileno(stdout), outmode);
628 if (file == NULL) error("can't gzdopen stdout");
630 gz_compress(in, file);
634 file_compress(*argv, outmode);
637 } while (argv++, --argc);