summaryrefslogtreecommitdiffstats
path: root/Modules/zlib/minigzip.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2010-07-30 20:03:17 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2010-07-30 20:03:17 (GMT)
commitce126edfd0df77da41b961f85d82d94c450c61f1 (patch)
tree10f6b5021ff273b85aa70f05e9cd1e450944dea5 /Modules/zlib/minigzip.c
parentd496c4c9360b481ca68b68b6bba6b6687136e442 (diff)
downloadcpython-ce126edfd0df77da41b961f85d82d94c450c61f1.zip
cpython-ce126edfd0df77da41b961f85d82d94c450c61f1.tar.gz
cpython-ce126edfd0df77da41b961f85d82d94c450c61f1.tar.bz2
Import files from zlib 1.2.5.
Diffstat (limited to 'Modules/zlib/minigzip.c')
-rw-r--r--Modules/zlib/minigzip.c139
1 files changed, 131 insertions, 8 deletions
diff --git a/Modules/zlib/minigzip.c b/Modules/zlib/minigzip.c
index 7f1de49..9825ccc 100644
--- a/Modules/zlib/minigzip.c
+++ b/Modules/zlib/minigzip.c
@@ -1,5 +1,5 @@
/* minigzip.c -- simulate gzip using the zlib compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2006, 2010 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
@@ -15,8 +15,8 @@
/* @(#) $Id$ */
-#include <stdio.h>
#include "zlib.h"
+#include <stdio.h>
#ifdef STDC
# include <string.h>
@@ -32,6 +32,9 @@
#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)
@@ -41,13 +44,84 @@
# define unlink delete
# define GZ_SUFFIX "-gz"
#endif
+#ifdef RISCOS
+# define unlink remove
+# define GZ_SUFFIX "-gz"
+# define fileno(file) file->__file
+#endif
#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
# include <unix.h> /* for fileno */
#endif
+#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
extern int unlink OF((const char *));
#endif
+#endif
+
+#if defined(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. */
+
+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, /* Default language */
+ (LPVOID)&msgbuf,
+ 0,
+ NULL);
+ if (chars != 0) {
+ /* If there is an \r\n appended, zap it. */
+ 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 /* UNDER_CE */
#ifndef GZ_SUFFIX
# define GZ_SUFFIX ".gz"
@@ -193,6 +267,11 @@ void file_compress(file, mode)
FILE *in;
gzFile out;
+ if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
+ fprintf(stderr, "%s: filename too long\n", prog);
+ exit(1);
+ }
+
strcpy(outfile, file);
strcat(outfile, GZ_SUFFIX);
@@ -222,7 +301,12 @@ void file_uncompress(file)
char *infile, *outfile;
FILE *out;
gzFile in;
- uInt len = (uInt)strlen(file);
+ size_t len = strlen(file);
+
+ if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
+ fprintf(stderr, "%s: filename too long\n", prog);
+ exit(1);
+ }
strcpy(buf, file);
@@ -253,7 +337,8 @@ void file_uncompress(file)
/* ===========================================================================
- * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
+ * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
+ * -c : write to standard output
* -d : decompress
* -f : compress with Z_FILTERED
* -h : compress with Z_HUFFMAN_ONLY
@@ -265,17 +350,30 @@ int main(argc, argv)
int argc;
char *argv[];
{
+ int copyout = 0;
int uncompr = 0;
gzFile file;
- char outmode[20];
+ char *bname, outmode[20];
strcpy(outmode, "wb6 ");
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, "-d") == 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';
@@ -305,11 +403,36 @@ int main(argc, argv)
gz_compress(stdin, file);
}
} else {
+ if (copyout) {
+ SET_BINARY_MODE(stdout);
+ }
do {
if (uncompr) {
- file_uncompress(*argv);
+ 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 {
- file_compress(*argv, outmode);
+ 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);
}