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(DWORD error)
87 static char buf[1024];
90 DWORD lasterr = GetLastError();
91 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
92 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
95 0, /* Default language */
100 /* If there is an \r\n appended, zap it. */
102 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
107 if (chars > sizeof (buf) - 1) {
108 chars = sizeof (buf) - 1;
112 wcstombs(buf, msgbuf, chars + 1);
116 sprintf(buf, "unknown win32 error (%ld)", error);
119 SetLastError(lasterr);
123 static void pwinerror (const char *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 _Z_OF((void *, unsigned, unsigned));
156 void myfree _Z_OF((void *, void *));
158 void *myalloc(q, n, m)
173 typedef struct gzFile_s {
181 gzFile gzopen _Z_OF((const char *, const char *));
182 gzFile gzdopen _Z_OF((int, const char *));
183 gzFile gz_open _Z_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(const char *path, int fd, const char *mode) {
203 gz = malloc(sizeof(struct gzFile_s));
206 gz->write = strchr(mode, 'w') != NULL;
207 gz->strm.zalloc = myalloc;
208 gz->strm.zfree = myfree;
209 gz->strm.opaque = Z_NULL;
211 ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
213 gz->strm.next_in = 0;
214 gz->strm.avail_in = Z_NULL;
215 ret = inflateInit2(&(gz->strm), 15 + 16);
221 gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
222 fopen(path, gz->write ? "wb" : "rb");
223 if (gz->file == NULL) {
224 gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
233 int gzwrite _Z_OF((gzFile, const void *, unsigned));
235 int gzwrite(gzFile gz, const void *buf, unsigned len) {
237 unsigned char out[BUFLEN];
239 if (gz == NULL || !gz->write)
242 strm->next_in = (void *)buf;
243 strm->avail_in = len;
245 strm->next_out = out;
246 strm->avail_out = BUFLEN;
247 (void)deflate(strm, Z_NO_FLUSH);
248 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
249 } while (strm->avail_out == 0);
253 int gzread _Z_OF((gzFile, void *, unsigned));
255 int gzread(gzFile gz, void *buf, unsigned len) {
261 if (gz == NULL || gz->write)
266 strm->next_out = (void *)buf;
267 strm->avail_out = len;
269 got = fread(in, 1, 1, gz->file);
274 ret = inflate(strm, Z_NO_FLUSH);
275 if (ret == Z_DATA_ERROR) {
276 gz->err = Z_DATA_ERROR;
280 if (ret == Z_STREAM_END)
282 } while (strm->avail_out);
283 return len - strm->avail_out;
286 int gzclose _Z_OF((gzFile));
288 int gzclose(gzFile gz) {
290 unsigned char out[BUFLEN];
293 return Z_STREAM_ERROR;
296 strm->next_in = Z_NULL;
299 strm->next_out = out;
300 strm->avail_out = BUFLEN;
301 (void)deflate(strm, Z_FINISH);
302 fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
303 } while (strm->avail_out == 0);
313 const char *gzerror _Z_OF((gzFile, int *));
315 const char *gzerror(gzFile gz, int *err)
325 void error _Z_OF((const char *msg));
326 void gz_compress _Z_OF((FILE *in, gzFile out));
328 int gz_compress_mmap _Z_OF((FILE *in, gzFile out));
330 void gz_uncompress _Z_OF((gzFile in, FILE *out));
331 void file_compress _Z_OF((char *file, char *mode));
332 void file_uncompress _Z_OF((char *file));
333 int main _Z_OF((int argc, char *argv[]));
335 /* ===========================================================================
336 * Display error message and exit
338 void error(const char *msg)
340 fprintf(stderr, "%s: %s\n", prog, msg);
344 /* ===========================================================================
345 * Compress input to output then close both files.
348 void gz_compress(FILE *in, gzFile out)
350 local char buf[BUFLEN];
355 /* Try first compressing with mmap. If mmap fails (minigzip used in a
356 * pipe), use the normal fread loop.
358 if (gz_compress_mmap(in, out) == Z_OK) return;
361 len = (int)fread(buf, 1, sizeof(buf), in);
368 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
371 if (gzclose(out) != Z_OK) error("failed gzclose");
374 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
376 /* Try compressing the input file at once using mmap. Return Z_OK if
377 * if success, Z_ERRNO otherwise.
379 int gz_compress_mmap(FILE *in, gzFile out) {
382 int ifd = fileno(in);
383 caddr_t buf; /* mmap'ed buffer for the entire input file */
384 off_t buf_len; /* length of the input file */
387 /* Determine the size of the file, needed for mmap: */
388 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
389 buf_len = sb.st_size;
390 if (buf_len <= 0) return Z_ERRNO;
392 /* Now do the actual mmap: */
393 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
394 if (buf == (caddr_t)(-1)) return Z_ERRNO;
396 /* Compress the whole file at once: */
397 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
399 if (len != (int)buf_len) error(gzerror(out, &err));
401 munmap(buf, buf_len);
403 if (gzclose(out) != Z_OK) error("failed gzclose");
406 #endif /* USE_MMAP */
408 /* ===========================================================================
409 * Uncompress input to output then close both files.
411 void gz_uncompress(gzFile in, FILE *out) {
412 local char buf[BUFLEN];
417 len = gzread(in, buf, sizeof(buf));
418 if (len < 0) error (gzerror(in, &err));
421 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
422 error("failed fwrite");
425 if (fclose(out)) error("failed fclose");
427 if (gzclose(in) != Z_OK) error("failed gzclose");
431 /* ===========================================================================
432 * Compress the given file: create a corresponding .gz file and remove the
435 void file_compress(char *file, char *mode) {
436 local char outfile[MAX_NAME_LEN];
440 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
441 fprintf(stderr, "%s: filename too long\n", prog);
445 strcpy(outfile, file);
446 strcat(outfile, GZ_SUFFIX);
448 in = fopen(file, "rb");
453 out = gzopen(outfile, mode);
455 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
458 gz_compress(in, out);
464 /* ===========================================================================
465 * Uncompress the given file and remove the original.
467 void file_uncompress(char *file) {
468 local char buf[MAX_NAME_LEN];
469 char *infile, *outfile;
472 size_t len = strlen(file);
474 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
475 fprintf(stderr, "%s: filename too long\n", prog);
481 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
484 outfile[len-3] = '\0';
488 strcat(infile, GZ_SUFFIX);
490 in = gzopen(infile, "rb");
492 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
495 out = fopen(outfile, "wb");
501 gz_uncompress(in, out);
507 /* ===========================================================================
508 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
509 * -c : write to standard output
511 * -f : compress with Z_FILTERED
512 * -h : compress with Z_HUFFMAN_ONLY
513 * -r : compress with Z_RLE
514 * -1 to -9 : compression level
517 int main(int argc, char *argv[]) {
521 char *bname, outmode[20];
523 strcpy(outmode, "wb6 ");
526 bname = strrchr(argv[0], '/');
533 if (!strcmp(bname, "gunzip"))
535 else if (!strcmp(bname, "zcat"))
536 copyout = uncompr = 1;
539 if (strcmp(*argv, "-c") == 0)
541 else if (strcmp(*argv, "-d") == 0)
543 else if (strcmp(*argv, "-f") == 0)
545 else if (strcmp(*argv, "-h") == 0)
547 else if (strcmp(*argv, "-r") == 0)
549 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
551 outmode[2] = (*argv)[1];
556 if (outmode[3] == ' ')
559 SET_BINARY_MODE(stdin);
560 SET_BINARY_MODE(stdout);
562 file = gzdopen(fileno(stdin), "rb");
563 if (file == NULL) error("can't gzdopen stdin");
564 gz_uncompress(file, stdout);
566 file = gzdopen(fileno(stdout), outmode);
567 if (file == NULL) error("can't gzdopen stdout");
568 gz_compress(stdin, file);
572 SET_BINARY_MODE(stdout);
577 file = gzopen(*argv, "rb");
579 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
581 gz_uncompress(file, stdout);
583 file_uncompress(*argv);
587 FILE * in = fopen(*argv, "rb");
592 file = gzdopen(fileno(stdout), outmode);
593 if (file == NULL) error("can't gzdopen stdout");
595 gz_compress(in, file);
599 file_compress(*argv, outmode);
602 } while (argv++, --argc);