summaryrefslogtreecommitdiff
path: root/gzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'gzio.c')
-rw-r--r--gzio.c132
1 files changed, 117 insertions, 15 deletions
diff --git a/gzio.c b/gzio.c
index cf55486..f9afc10 100644
--- a/gzio.c
+++ b/gzio.c
@@ -1,5 +1,5 @@
1/* gzio.c -- IO on .gz files 1/* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-2009 Jean-loup Gailly. 2 * Copyright (C) 1995-2010 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * 4 *
5 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code. 5 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
@@ -7,6 +7,8 @@
7 7
8/* @(#) $Id$ */ 8/* @(#) $Id$ */
9 9
10#ifdef OLD_GZIO
11
10#ifdef _LARGEFILE64_SOURCE 12#ifdef _LARGEFILE64_SOURCE
11# ifndef _LARGEFILE_SOURCE 13# ifndef _LARGEFILE_SOURCE
12# define _LARGEFILE_SOURCE 14# define _LARGEFILE_SOURCE
@@ -38,6 +40,60 @@ struct internal_state {int dummy;}; /* for buggy compilers */
38# define Z_PRINTF_BUFSIZE 4096 40# define Z_PRINTF_BUFSIZE 4096
39#endif 41#endif
40 42
43#if defined UNDER_CE && defined NO_ERRNO_H
44# include <windows.h>
45
46/* Map the Windows error number in ERROR to a locale-dependent error
47 message string and return a pointer to it. Typically, the values
48 for ERROR come from GetLastError.
49
50 The string pointed to shall not be modified by the application,
51 but may be overwritten by a subsequent call to strwinerror
52
53 The strwinerror function does not change the current setting
54 of GetLastError. */
55
56local char *strwinerror (error)
57 DWORD error;
58{
59 static char buf[1024];
60
61 wchar_t *msgbuf;
62 DWORD lasterr = GetLastError();
63 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
64 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
65 NULL,
66 error,
67 0, /* Default language */
68 (LPVOID)&msgbuf,
69 0,
70 NULL);
71 if (chars != 0) {
72 /* If there is an \r\n appended, zap it. */
73 if (chars >= 2
74 && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
75 chars -= 2;
76 msgbuf[chars] = 0;
77 }
78
79 if (chars > sizeof (buf) - 1) {
80 chars = sizeof (buf) - 1;
81 msgbuf[chars] = 0;
82 }
83
84 wcstombs(buf, msgbuf, chars + 1);
85 LocalFree(msgbuf);
86 }
87 else {
88 sprintf(buf, "unknown win32 error (%ld)", error);
89 }
90
91 SetLastError(lasterr);
92 return buf;
93}
94
95#endif /* UNDER_CE && NO_ERRNO_H */
96
41#ifdef __MVS__ 97#ifdef __MVS__
42# pragma map (fdopen , "\174\174FDOPEN") 98# pragma map (fdopen , "\174\174FDOPEN")
43 FILE *fdopen(int, const char *); 99 FILE *fdopen(int, const char *);
@@ -191,9 +247,10 @@ local gzFile gz_open (path, mode, fd, use64)
191 } 247 }
192 s->stream.avail_out = Z_BUFSIZE; 248 s->stream.avail_out = Z_BUFSIZE;
193 249
194 errno = 0; 250 zseterrno(0);
195 s->file = fd < 0 ? (use64 ? F_OPEN64(path, fmode) : F_OPEN(path, fmode)) : 251 s->file = fd == -1 ?
196 (FILE*)fdopen(fd, fmode); 252 (use64 ? F_OPEN64(path, fmode) : F_OPEN(path, fmode)) :
253 (FILE*)fdopen(fd, fmode);
197 254
198 if (s->file == NULL) { 255 if (s->file == NULL) {
199 return destroy(s), (gzFile)Z_NULL; 256 return destroy(s), (gzFile)Z_NULL;
@@ -250,7 +307,7 @@ gzFile ZEXPORT gzdopen (fd, mode)
250{ 307{
251 char name[46]; /* allow for up to 128-bit integers */ 308 char name[46]; /* allow for up to 128-bit integers */
252 309
253 if (fd < 0) return (gzFile)Z_NULL; 310 if (fd == -1) return (gzFile)Z_NULL;
254 sprintf(name, "<fd:%d>", fd); /* for debugging */ 311 sprintf(name, "<fd:%d>", fd); /* for debugging */
255 312
256 return gz_open (name, mode, fd, 0); 313 return gz_open (name, mode, fd, 0);
@@ -291,7 +348,7 @@ local int get_byte(s)
291{ 348{
292 if (s->z_eof) return EOF; 349 if (s->z_eof) return EOF;
293 if (s->stream.avail_in == 0) { 350 if (s->stream.avail_in == 0) {
294 errno = 0; 351 zseterrno(0);
295 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); 352 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
296 if (s->stream.avail_in == 0) { 353 if (s->stream.avail_in == 0) {
297 s->z_eof = 1; 354 s->z_eof = 1;
@@ -327,7 +384,7 @@ local void check_header(s)
327 len = s->stream.avail_in; 384 len = s->stream.avail_in;
328 if (len < 2) { 385 if (len < 2) {
329 if (len) s->inbuf[0] = s->stream.next_in[0]; 386 if (len) s->inbuf[0] = s->stream.next_in[0];
330 errno = 0; 387 zseterrno(0);
331 len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file); 388 len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
332 if (len == 0) s->z_eof = 1; 389 if (len == 0) s->z_eof = 1;
333 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO; 390 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
@@ -403,7 +460,7 @@ local int destroy (s)
403 } 460 }
404 if (s->file != NULL && fclose(s->file)) { 461 if (s->file != NULL && fclose(s->file)) {
405#ifdef ESPIPE 462#ifdef ESPIPE
406 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ 463 if (zerrno() != ESPIPE) /* fclose is broken for pipes in HP/UX */
407#endif 464#endif
408 err = Z_ERRNO; 465 err = Z_ERRNO;
409 } 466 }
@@ -432,7 +489,7 @@ int ZEXPORT gzread (file, buf, len)
432 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR; 489 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
433 490
434 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1; 491 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
435 if (s->z_err == Z_STREAM_END || s->z_eof) return 0; /* EOF */ 492 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
436 493
437 next_out = (Byte*)buf; 494 next_out = (Byte*)buf;
438 s->stream.next_out = (Bytef*)buf; 495 s->stream.next_out = (Bytef*)buf;
@@ -472,12 +529,12 @@ int ZEXPORT gzread (file, buf, len)
472 len -= s->stream.avail_out; 529 len -= s->stream.avail_out;
473 s->in += len; 530 s->in += len;
474 s->out += len; 531 s->out += len;
475 if (feof(s->file)) s->z_eof = 1; 532 if (len == 0 && feof(s->file)) s->z_eof = 1;
476 return (int)len; 533 return (int)len;
477 } 534 }
478 if (s->stream.avail_in == 0 && !s->z_eof) { 535 if (s->stream.avail_in == 0 && !s->z_eof) {
479 536
480 errno = 0; 537 zseterrno(0);
481 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file); 538 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
482 if (s->stream.avail_in == 0) { 539 if (s->stream.avail_in == 0) {
483 s->z_eof = 1; 540 s->z_eof = 1;
@@ -1039,10 +1096,14 @@ int ZEXPORT gzclose (file)
1039 return destroy((gz_stream*)file); 1096 return destroy((gz_stream*)file);
1040} 1097}
1041 1098
1042#if defined(STDC) && !defined(_WIN32_WCE) 1099#if defined UNDER_CE && defined NO_ERRNO_H
1043# define zstrerror(errnum) strerror(errnum) 1100# define zstrerror(errnum) strwinerror((DWORD)errnum)
1044#else 1101#else
1045# define zstrerror(errnum) "" 1102# if defined (STDC)
1103# define zstrerror(errnum) strerror(errnum)
1104# else
1105# define zstrerror(errnum) ""
1106# endif
1046#endif 1107#endif
1047 1108
1048/* =========================================================================== 1109/* ===========================================================================
@@ -1066,7 +1127,7 @@ const char * ZEXPORT gzerror (file, errnum)
1066 *errnum = s->z_err; 1127 *errnum = s->z_err;
1067 if (*errnum == Z_OK) return (const char*)""; 1128 if (*errnum == Z_OK) return (const char*)"";
1068 1129
1069 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg); 1130 m = (char*)(*errnum == Z_ERRNO ? zstrerror(zerrno()) : s->stream.msg);
1070 1131
1071 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err); 1132 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1072 1133
@@ -1092,3 +1153,44 @@ void ZEXPORT gzclearerr (file)
1092 s->z_eof = 0; 1153 s->z_eof = 0;
1093 clearerr(s->file); 1154 clearerr(s->file);
1094} 1155}
1156
1157/* new functions in gzlib, but only partially implemented here */
1158
1159int ZEXPORT gzbuffer (file, size)
1160 gzFile file;
1161 unsigned size;
1162{
1163 return -1;
1164}
1165
1166z_off_t ZEXPORT gzoffset (file)
1167 gzFile file;
1168{
1169 return -1;
1170}
1171
1172z_off64_t ZEXPORT gzoffset64 (file)
1173 gzFile file;
1174{
1175 return -1;
1176}
1177
1178int ZEXPORT gzclose_r (file)
1179 gzFile file;
1180{
1181 return gzclose(file);
1182}
1183
1184int ZEXPORT gzclose_w (file)
1185 gzFile file;
1186{
1187 return gzclose(file);
1188}
1189
1190int gzio_old = 1;
1191
1192#else /* !OLD_GZIO */
1193
1194int gzio_old = 0;
1195
1196#endif /* OLD_GZIO */