8 #if defined(_WIN32) && !defined(__BORLANDC__)
9 # define LSEEK _lseeki64
11 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
12 # define LSEEK lseek64
35 static char buf[1024];
38 DWORD lasterr = GetLastError();
39 DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
40 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
50 && msgbuf[chars - 2] ==
'\r' && msgbuf[chars - 1] ==
'\n') {
55 if (chars >
sizeof (buf) - 1) {
56 chars =
sizeof (buf) - 1;
60 wcstombs(buf, msgbuf, chars + 1);
64 sprintf(buf,
"unknown win32 error (%ld)", error);
67 SetLastError(lasterr);
77 if (state->mode == GZ_READ) {
85 state->strm.avail_in = 0;
106 state = (gz_statep)malloc(
sizeof(gz_state));
110 state->want = GZBUFSIZE;
114 state->mode = GZ_NONE;
115 state->level = Z_DEFAULT_COMPRESSION;
116 state->strategy = Z_DEFAULT_STRATEGY;
119 if (*mode >=
'0' && *mode <=
'9')
120 state->level = *mode -
'0';
124 state->mode = GZ_READ;
126 #ifndef NO_GZCOMPRESS
128 state->mode = GZ_WRITE;
131 state->mode = GZ_APPEND;
150 state->strategy = Z_FILTERED;
153 state->strategy = Z_HUFFMAN_ONLY;
156 state->strategy = Z_RLE;
159 state->strategy = Z_FIXED;
171 if (state->mode == GZ_NONE) {
177 if (state->mode == GZ_READ) {
188 len = wcstombs(NULL, (
const wchar_t *)path, 0);
189 if (len == (
size_t)-1)
194 len = strlen((
const char *)path);
195 state->path = (
char*)malloc(len + 1);
196 if (state->path == NULL) {
203 wcstombs(state->path, (
const wchar_t *)path, len + 1);
208 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
209 snprintf(state->path, len + 1,
"%s", (
const char *)path);
211 strcpy(state->path, path);
223 (cloexec ? O_CLOEXEC : 0) |
225 (state->mode == GZ_READ ?
227 (O_WRONLY | O_CREAT |
229 (exclusive ? O_EXCL : 0) |
231 (state->mode == GZ_WRITE ?
236 state->fd = fd > -1 ? fd : (
238 fd == -2 ? _wopen((
const wchar_t *)path, oflag, 0666) :
240 open((
const char *)path, oflag, 0666));
241 if (state->fd == -1) {
246 if (state->mode == GZ_APPEND)
247 state->mode = GZ_WRITE;
250 if (state->mode == GZ_READ) {
251 state->start =
LSEEK(state->fd, 0, SEEK_CUR);
252 if (state->start == -1) state->start = 0;
259 return (gzFile)state;
263 gzFile ZEXPORT
gzopen(
const char *path,
const char * mode)
265 return gz_open(path, -1, mode);
269 gzFile ZEXPORT
gzopen64(
const char *path,
const char * mode)
271 return gz_open(path, -1, mode);
275 gzFile ZEXPORT
gzdopen(
int fd,
const char *mode)
280 if (fd == -1 || (path = (
char *)malloc(7 + 3 *
sizeof(
int))) == NULL)
282 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
283 snprintf(path, 7 + 3 *
sizeof(
int),
"<fd:%d>", fd);
285 sprintf(path,
"<fd:%d>", fd);
294 gzFile ZEXPORT gzopen_w(
const wchar_t *path,
const char *mode)
296 return gz_open(path, -2, mode);
308 state = (gz_statep)file;
309 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
313 if (state->size != 0)
331 state = (gz_statep)file;
334 if (state->mode != GZ_READ ||
335 (state->err != Z_OK && state->err != Z_BUF_ERROR))
339 if (
LSEEK(state->fd, state->start, SEEK_SET) == -1)
346 z_off64_t ZEXPORT
gzseek64(gzFile file, z_off64_t offset,
int whence)
355 state = (gz_statep)file;
356 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
360 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
364 if (whence != SEEK_SET && whence != SEEK_CUR)
368 if (whence == SEEK_SET)
369 offset -= state->x.pos;
370 else if (state->seek)
371 offset += state->skip;
375 if (state->mode == GZ_READ && state->how == COPY &&
376 state->x.pos + offset >= 0) {
377 ret =
LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
385 state->strm.avail_in = 0;
386 state->x.pos += offset;
392 if (state->mode != GZ_READ)
394 offset += state->x.pos;
402 if (state->mode == GZ_READ) {
403 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
404 (
unsigned)offset : state->x.have;
414 state->skip = offset;
416 return state->x.pos + offset;
420 z_off_t ZEXPORT
gzseek(gzFile file, z_off_t offset,
int whence)
424 ret =
gzseek64(file, (z_off64_t)offset, whence);
425 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
436 state = (gz_statep)file;
437 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
441 return state->x.pos + (state->seek ? state->skip : 0);
450 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
462 state = (gz_statep)file;
463 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
467 offset =
LSEEK(state->fd, 0, SEEK_CUR);
470 if (state->mode == GZ_READ)
471 offset -= state->strm.avail_in;
481 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
492 state = (gz_statep)file;
493 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
497 return state->mode == GZ_READ ? state->past : 0;
501 const char * ZEXPORT
gzerror(gzFile file,
int *errnum)
508 state = (gz_statep)file;
509 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
514 *errnum = state->err;
515 return state->err == Z_MEM_ERROR ?
"out of memory" :
516 (state->msg == NULL ?
"" : state->msg);
527 state = (gz_statep)file;
528 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
532 if (state->mode == GZ_READ) {
548 if (state->msg != NULL) {
549 if (state->err != Z_MEM_ERROR)
555 if (err != Z_OK && err != Z_BUF_ERROR)
564 if (err == Z_MEM_ERROR)
568 if ((state->msg = (
char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
570 state->err = Z_MEM_ERROR;
573 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
574 snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
575 "%s%s%s", state->path,
": ", msg);
577 strcpy(state->msg, state->path);
578 strcat(state->msg,
": ");
579 strcat(state->msg, msg);
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
z_off_t ZEXPORT gzoffset(gzFile file)
z_off_t ZEXPORT gzseek(gzFile file, z_off_t offset, int whence)
void ZEXPORT gzclearerr(gzFile file)
z_off64_t ZEXPORT gzoffset64(gzFile file)
const char *ZEXPORT gzerror(gzFile file, int *errnum)
z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence)
local gzFile gz_open(const void *path, int fd, const char *mode)
int ZEXPORT gzeof(gzFile file)
gzFile ZEXPORT gzopen64(const char *path, const char *mode)
z_off_t ZEXPORT gztell(gzFile file)
unsigned ZLIB_INTERNAL gz_intmax()
local void gz_reset(gz_statep state)
z_off64_t ZEXPORT gztell64(gzFile file)
gzFile ZEXPORT gzopen(const char *path, const char *mode)
int ZEXPORT gzrewind(gzFile file)
int ZEXPORT gzbuffer(gzFile file, unsigned size)
gzFile ZEXPORT gzdopen(int fd, const char *mode)
static PROLOG_HANDLER error
local void gz_reset OF((gz_statep))