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;
169 if (state->mode == GZ_NONE) {
175 if (state->mode == GZ_READ) {
186 len = wcstombs(NULL, (
const wchar_t *)path, 0);
187 if (len == (
size_t)-1)
192 len = strlen((
const char *)path);
193 state->path = (
char*)
malloc(len + 1);
194 if (state->path == NULL) {
201 wcstombs(state->path, (
const wchar_t *)path, len + 1);
206 strcpy(state->path, (
const char *)path);
217 (cloexec ? O_CLOEXEC : 0) |
219 (state->mode == GZ_READ ?
221 (O_WRONLY | O_CREAT |
223 (exclusive ? O_EXCL : 0) |
225 (state->mode == GZ_WRITE ?
230 state->fd = fd > -1 ? fd : (
232 fd == -2 ? _wopen((
const wchar_t *)path, oflag, 0666) :
234 open((
const char *)path, oflag, 0666));
235 if (state->fd == -1) {
240 if (state->mode == GZ_APPEND)
241 state->mode = GZ_WRITE;
244 if (state->mode == GZ_READ) {
245 state->start =
LSEEK(state->fd, 0, SEEK_CUR);
246 if (state->start == -1) state->start = 0;
253 return (gzFile)state;
257 gzFile ZEXPORT
gzopen(
const char *path,
const char * mode)
259 return gz_open(path, -1, mode);
263 gzFile ZEXPORT
gzopen64(
const char *path,
const char * mode)
265 return gz_open(path, -1, mode);
269 gzFile ZEXPORT
gzdopen(
int fd,
const char *mode)
274 if (fd == -1 || (path = (
char *)
malloc(7 + 3 *
sizeof(
int))) == NULL)
276 sprintf(path,
"<fd:%d>", fd);
284 gzFile ZEXPORT gzopen_w(
const wchar_t *path,
const char *mode)
286 return gz_open(path, -2, mode);
298 state = (gz_statep)file;
299 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
303 if (state->size != 0)
321 state = (gz_statep)file;
324 if (state->mode != GZ_READ ||
325 (state->err != Z_OK && state->err != Z_BUF_ERROR))
329 if (
LSEEK(state->fd, state->start, SEEK_SET) == -1)
336 z_off64_t ZEXPORT
gzseek64(gzFile file, z_off64_t offset,
int whence)
345 state = (gz_statep)file;
346 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
350 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
354 if (whence != SEEK_SET && whence != SEEK_CUR)
358 if (whence == SEEK_SET)
359 offset -= state->x.pos;
360 else if (state->seek)
361 offset += state->skip;
365 if (state->mode == GZ_READ && state->how == COPY &&
366 state->x.pos + offset >= 0) {
367 ret =
LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
375 state->strm.avail_in = 0;
376 state->x.pos += offset;
382 if (state->mode != GZ_READ)
384 offset += state->x.pos;
392 if (state->mode == GZ_READ) {
393 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
394 (
unsigned)offset : state->x.have;
404 state->skip = offset;
406 return state->x.pos + offset;
410 z_off_t ZEXPORT
gzseek(gzFile file, z_off_t offset,
int whence)
414 ret =
gzseek64(file, (z_off64_t)offset, whence);
415 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
426 state = (gz_statep)file;
427 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
431 return state->x.pos + (state->seek ? state->skip : 0);
440 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
452 state = (gz_statep)file;
453 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
457 offset =
LSEEK(state->fd, 0, SEEK_CUR);
460 if (state->mode == GZ_READ)
461 offset -= state->strm.avail_in;
471 return ret == (z_off_t)ret ? (z_off_t)ret : -1;
482 state = (gz_statep)file;
483 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
487 return state->mode == GZ_READ ? state->past : 0;
491 const char * ZEXPORT
gzerror(gzFile file,
int *errnum)
498 state = (gz_statep)file;
499 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
504 *errnum = state->err;
505 return state->msg == NULL ?
"" : state->msg;
516 state = (gz_statep)file;
517 if (state->mode != GZ_READ && state->mode != GZ_WRITE)
521 if (state->mode == GZ_READ) {
537 if (state->msg != NULL) {
538 if (state->err != Z_MEM_ERROR)
544 if (err != Z_OK && err != Z_BUF_ERROR)
553 if (err == Z_MEM_ERROR) {
554 state->msg = (
char *)msg;
559 if ((state->msg = (
char *)
malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
560 state->err = Z_MEM_ERROR;
561 state->msg = (
char *)
"out of memory";
564 strcpy(state->msg, state->path);
565 strcat(state->msg,
": ");
566 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
void * malloc(size_t __size)
local void gz_reset OF((gz_statep))