michael@0: /* gzlib.c -- zlib functions common to reading and writing gzip files michael@0: * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: #include "gzguts.h" michael@0: michael@0: #if defined(_WIN32) && !defined(__BORLANDC__) michael@0: # define LSEEK _lseeki64 michael@0: #else michael@0: #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 michael@0: # define LSEEK lseek64 michael@0: #else michael@0: # define LSEEK lseek michael@0: #endif michael@0: #endif michael@0: michael@0: /* Local functions */ michael@0: local void gz_reset OF((gz_statep)); michael@0: local gzFile gz_open OF((const void *, int, const char *)); michael@0: michael@0: #if defined UNDER_CE michael@0: michael@0: /* Map the Windows error number in ERROR to a locale-dependent error message michael@0: string and return a pointer to it. Typically, the values for ERROR come michael@0: from GetLastError. michael@0: michael@0: The string pointed to shall not be modified by the application, but may be michael@0: overwritten by a subsequent call to gz_strwinerror michael@0: michael@0: The gz_strwinerror function does not change the current setting of michael@0: GetLastError. */ michael@0: char ZLIB_INTERNAL *gz_strwinerror (error) michael@0: DWORD error; michael@0: { michael@0: static char buf[1024]; michael@0: michael@0: wchar_t *msgbuf; michael@0: DWORD lasterr = GetLastError(); michael@0: DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM michael@0: | FORMAT_MESSAGE_ALLOCATE_BUFFER, michael@0: NULL, michael@0: error, michael@0: 0, /* Default language */ michael@0: (LPVOID)&msgbuf, michael@0: 0, michael@0: NULL); michael@0: if (chars != 0) { michael@0: /* If there is an \r\n appended, zap it. */ michael@0: if (chars >= 2 michael@0: && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { michael@0: chars -= 2; michael@0: msgbuf[chars] = 0; michael@0: } michael@0: michael@0: if (chars > sizeof (buf) - 1) { michael@0: chars = sizeof (buf) - 1; michael@0: msgbuf[chars] = 0; michael@0: } michael@0: michael@0: wcstombs(buf, msgbuf, chars + 1); michael@0: LocalFree(msgbuf); michael@0: } michael@0: else { michael@0: sprintf(buf, "unknown win32 error (%ld)", error); michael@0: } michael@0: michael@0: SetLastError(lasterr); michael@0: return buf; michael@0: } michael@0: michael@0: #endif /* UNDER_CE */ michael@0: michael@0: /* Reset gzip file state */ michael@0: local void gz_reset(state) michael@0: gz_statep state; michael@0: { michael@0: state->x.have = 0; /* no output data available */ michael@0: if (state->mode == GZ_READ) { /* for reading ... */ michael@0: state->eof = 0; /* not at end of file */ michael@0: state->past = 0; /* have not read past end yet */ michael@0: state->how = LOOK; /* look for gzip header */ michael@0: } michael@0: state->seek = 0; /* no seek request pending */ michael@0: gz_error(state, Z_OK, NULL); /* clear error */ michael@0: state->x.pos = 0; /* no uncompressed data yet */ michael@0: state->strm.avail_in = 0; /* no input data yet */ michael@0: } michael@0: michael@0: /* Open a gzip file either by name or file descriptor. */ michael@0: local gzFile gz_open(path, fd, mode) michael@0: const void *path; michael@0: int fd; michael@0: const char *mode; michael@0: { michael@0: gz_statep state; michael@0: size_t len; michael@0: int oflag; michael@0: #ifdef O_CLOEXEC michael@0: int cloexec = 0; michael@0: #endif michael@0: #ifdef O_EXCL michael@0: int exclusive = 0; michael@0: #endif michael@0: michael@0: /* check input */ michael@0: if (path == NULL) michael@0: return NULL; michael@0: michael@0: /* allocate gzFile structure to return */ michael@0: state = (gz_statep)malloc(sizeof(gz_state)); michael@0: if (state == NULL) michael@0: return NULL; michael@0: state->size = 0; /* no buffers allocated yet */ michael@0: state->want = GZBUFSIZE; /* requested buffer size */ michael@0: state->msg = NULL; /* no error message yet */ michael@0: michael@0: /* interpret mode */ michael@0: state->mode = GZ_NONE; michael@0: state->level = Z_DEFAULT_COMPRESSION; michael@0: state->strategy = Z_DEFAULT_STRATEGY; michael@0: state->direct = 0; michael@0: while (*mode) { michael@0: if (*mode >= '0' && *mode <= '9') michael@0: state->level = *mode - '0'; michael@0: else michael@0: switch (*mode) { michael@0: case 'r': michael@0: state->mode = GZ_READ; michael@0: break; michael@0: #ifndef NO_GZCOMPRESS michael@0: case 'w': michael@0: state->mode = GZ_WRITE; michael@0: break; michael@0: case 'a': michael@0: state->mode = GZ_APPEND; michael@0: break; michael@0: #endif michael@0: case '+': /* can't read and write at the same time */ michael@0: free(state); michael@0: return NULL; michael@0: case 'b': /* ignore -- will request binary anyway */ michael@0: break; michael@0: #ifdef O_CLOEXEC michael@0: case 'e': michael@0: cloexec = 1; michael@0: break; michael@0: #endif michael@0: #ifdef O_EXCL michael@0: case 'x': michael@0: exclusive = 1; michael@0: break; michael@0: #endif michael@0: case 'f': michael@0: state->strategy = Z_FILTERED; michael@0: break; michael@0: case 'h': michael@0: state->strategy = Z_HUFFMAN_ONLY; michael@0: break; michael@0: case 'R': michael@0: state->strategy = Z_RLE; michael@0: break; michael@0: case 'F': michael@0: state->strategy = Z_FIXED; michael@0: break; michael@0: case 'T': michael@0: state->direct = 1; michael@0: break; michael@0: default: /* could consider as an error, but just ignore */ michael@0: ; michael@0: } michael@0: mode++; michael@0: } michael@0: michael@0: /* must provide an "r", "w", or "a" */ michael@0: if (state->mode == GZ_NONE) { michael@0: free(state); michael@0: return NULL; michael@0: } michael@0: michael@0: /* can't force transparent read */ michael@0: if (state->mode == GZ_READ) { michael@0: if (state->direct) { michael@0: free(state); michael@0: return NULL; michael@0: } michael@0: state->direct = 1; /* for empty file */ michael@0: } michael@0: michael@0: /* save the path name for error messages */ michael@0: #ifdef _WIN32 michael@0: if (fd == -2) { michael@0: len = wcstombs(NULL, path, 0); michael@0: if (len == (size_t)-1) michael@0: len = 0; michael@0: } michael@0: else michael@0: #endif michael@0: len = strlen((const char *)path); michael@0: state->path = (char *)malloc(len + 1); michael@0: if (state->path == NULL) { michael@0: free(state); michael@0: return NULL; michael@0: } michael@0: #ifdef _WIN32 michael@0: if (fd == -2) michael@0: if (len) michael@0: wcstombs(state->path, path, len + 1); michael@0: else michael@0: *(state->path) = 0; michael@0: else michael@0: #endif michael@0: #if !defined(NO_snprintf) && !defined(NO_vsnprintf) michael@0: snprintf(state->path, len + 1, "%s", (const char *)path); michael@0: #else michael@0: strcpy(state->path, path); michael@0: #endif michael@0: michael@0: /* compute the flags for open() */ michael@0: oflag = michael@0: #ifdef O_LARGEFILE michael@0: O_LARGEFILE | michael@0: #endif michael@0: #ifdef O_BINARY michael@0: O_BINARY | michael@0: #endif michael@0: #ifdef O_CLOEXEC michael@0: (cloexec ? O_CLOEXEC : 0) | michael@0: #endif michael@0: (state->mode == GZ_READ ? michael@0: O_RDONLY : michael@0: (O_WRONLY | O_CREAT | michael@0: #ifdef O_EXCL michael@0: (exclusive ? O_EXCL : 0) | michael@0: #endif michael@0: (state->mode == GZ_WRITE ? michael@0: O_TRUNC : michael@0: O_APPEND))); michael@0: michael@0: /* open the file with the appropriate flags (or just use fd) */ michael@0: state->fd = fd > -1 ? fd : ( michael@0: #ifdef _WIN32 michael@0: fd == -2 ? _wopen(path, oflag, 0666) : michael@0: #endif michael@0: open((const char *)path, oflag, 0666)); michael@0: if (state->fd == -1) { michael@0: free(state->path); michael@0: free(state); michael@0: return NULL; michael@0: } michael@0: if (state->mode == GZ_APPEND) michael@0: state->mode = GZ_WRITE; /* simplify later checks */ michael@0: michael@0: /* save the current position for rewinding (only if reading) */ michael@0: if (state->mode == GZ_READ) { michael@0: state->start = LSEEK(state->fd, 0, SEEK_CUR); michael@0: if (state->start == -1) state->start = 0; michael@0: } michael@0: michael@0: /* initialize stream */ michael@0: gz_reset(state); michael@0: michael@0: /* return stream */ michael@0: return (gzFile)state; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: gzFile ZEXPORT gzopen(path, mode) michael@0: const char *path; michael@0: const char *mode; michael@0: { michael@0: return gz_open(path, -1, mode); michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: gzFile ZEXPORT gzopen64(path, mode) michael@0: const char *path; michael@0: const char *mode; michael@0: { michael@0: return gz_open(path, -1, mode); michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: gzFile ZEXPORT gzdopen(fd, mode) michael@0: int fd; michael@0: const char *mode; michael@0: { michael@0: char *path; /* identifier for error messages */ michael@0: gzFile gz; michael@0: michael@0: if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) michael@0: return NULL; michael@0: #if !defined(NO_snprintf) && !defined(NO_vsnprintf) michael@0: snprintf(path, 7 + 3 * sizeof(int), "", fd); /* for debugging */ michael@0: #else michael@0: sprintf(path, "", fd); /* for debugging */ michael@0: #endif michael@0: gz = gz_open(path, fd, mode); michael@0: free(path); michael@0: return gz; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: #ifdef _WIN32 michael@0: gzFile ZEXPORT gzopen_w(path, mode) michael@0: const wchar_t *path; michael@0: const char *mode; michael@0: { michael@0: return gz_open(path, -2, mode); michael@0: } michael@0: #endif michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzbuffer(file, size) michael@0: gzFile file; michael@0: unsigned size; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return -1; michael@0: michael@0: /* make sure we haven't already allocated memory */ michael@0: if (state->size != 0) michael@0: return -1; michael@0: michael@0: /* check and set requested size */ michael@0: if (size < 2) michael@0: size = 2; /* need two bytes to check magic header */ michael@0: state->want = size; michael@0: return 0; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzrewind(file) michael@0: gzFile file; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: michael@0: /* check that we're reading and that there's no error */ michael@0: if (state->mode != GZ_READ || michael@0: (state->err != Z_OK && state->err != Z_BUF_ERROR)) michael@0: return -1; michael@0: michael@0: /* back up and start over */ michael@0: if (LSEEK(state->fd, state->start, SEEK_SET) == -1) michael@0: return -1; michael@0: gz_reset(state); michael@0: return 0; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off64_t ZEXPORT gzseek64(file, offset, whence) michael@0: gzFile file; michael@0: z_off64_t offset; michael@0: int whence; michael@0: { michael@0: unsigned n; michael@0: z_off64_t ret; michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return -1; michael@0: michael@0: /* check that there's no error */ michael@0: if (state->err != Z_OK && state->err != Z_BUF_ERROR) michael@0: return -1; michael@0: michael@0: /* can only seek from start or relative to current position */ michael@0: if (whence != SEEK_SET && whence != SEEK_CUR) michael@0: return -1; michael@0: michael@0: /* normalize offset to a SEEK_CUR specification */ michael@0: if (whence == SEEK_SET) michael@0: offset -= state->x.pos; michael@0: else if (state->seek) michael@0: offset += state->skip; michael@0: state->seek = 0; michael@0: michael@0: /* if within raw area while reading, just go there */ michael@0: if (state->mode == GZ_READ && state->how == COPY && michael@0: state->x.pos + offset >= 0) { michael@0: ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); michael@0: if (ret == -1) michael@0: return -1; michael@0: state->x.have = 0; michael@0: state->eof = 0; michael@0: state->past = 0; michael@0: state->seek = 0; michael@0: gz_error(state, Z_OK, NULL); michael@0: state->strm.avail_in = 0; michael@0: state->x.pos += offset; michael@0: return state->x.pos; michael@0: } michael@0: michael@0: /* calculate skip amount, rewinding if needed for back seek when reading */ michael@0: if (offset < 0) { michael@0: if (state->mode != GZ_READ) /* writing -- can't go backwards */ michael@0: return -1; michael@0: offset += state->x.pos; michael@0: if (offset < 0) /* before start of file! */ michael@0: return -1; michael@0: if (gzrewind(file) == -1) /* rewind, then skip to offset */ michael@0: return -1; michael@0: } michael@0: michael@0: /* if reading, skip what's in output buffer (one less gzgetc() check) */ michael@0: if (state->mode == GZ_READ) { michael@0: n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? michael@0: (unsigned)offset : state->x.have; michael@0: state->x.have -= n; michael@0: state->x.next += n; michael@0: state->x.pos += n; michael@0: offset -= n; michael@0: } michael@0: michael@0: /* request skip (if not zero) */ michael@0: if (offset) { michael@0: state->seek = 1; michael@0: state->skip = offset; michael@0: } michael@0: return state->x.pos + offset; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off_t ZEXPORT gzseek(file, offset, whence) michael@0: gzFile file; michael@0: z_off_t offset; michael@0: int whence; michael@0: { michael@0: z_off64_t ret; michael@0: michael@0: ret = gzseek64(file, (z_off64_t)offset, whence); michael@0: return ret == (z_off_t)ret ? (z_off_t)ret : -1; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off64_t ZEXPORT gztell64(file) michael@0: gzFile file; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return -1; michael@0: michael@0: /* return position */ michael@0: return state->x.pos + (state->seek ? state->skip : 0); michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off_t ZEXPORT gztell(file) michael@0: gzFile file; michael@0: { michael@0: z_off64_t ret; michael@0: michael@0: ret = gztell64(file); michael@0: return ret == (z_off_t)ret ? (z_off_t)ret : -1; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off64_t ZEXPORT gzoffset64(file) michael@0: gzFile file; michael@0: { michael@0: z_off64_t offset; michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return -1; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return -1; michael@0: michael@0: /* compute and return effective offset in file */ michael@0: offset = LSEEK(state->fd, 0, SEEK_CUR); michael@0: if (offset == -1) michael@0: return -1; michael@0: if (state->mode == GZ_READ) /* reading */ michael@0: offset -= state->strm.avail_in; /* don't count buffered input */ michael@0: return offset; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: z_off_t ZEXPORT gzoffset(file) michael@0: gzFile file; michael@0: { michael@0: z_off64_t ret; michael@0: michael@0: ret = gzoffset64(file); michael@0: return ret == (z_off_t)ret ? (z_off_t)ret : -1; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: int ZEXPORT gzeof(file) michael@0: gzFile file; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return 0; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return 0; michael@0: michael@0: /* return end-of-file state */ michael@0: return state->mode == GZ_READ ? state->past : 0; michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: const char * ZEXPORT gzerror(file, errnum) michael@0: gzFile file; michael@0: int *errnum; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return NULL; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return NULL; michael@0: michael@0: /* return error information */ michael@0: if (errnum != NULL) michael@0: *errnum = state->err; michael@0: return state->err == Z_MEM_ERROR ? "out of memory" : michael@0: (state->msg == NULL ? "" : state->msg); michael@0: } michael@0: michael@0: /* -- see zlib.h -- */ michael@0: void ZEXPORT gzclearerr(file) michael@0: gzFile file; michael@0: { michael@0: gz_statep state; michael@0: michael@0: /* get internal structure and check integrity */ michael@0: if (file == NULL) michael@0: return; michael@0: state = (gz_statep)file; michael@0: if (state->mode != GZ_READ && state->mode != GZ_WRITE) michael@0: return; michael@0: michael@0: /* clear error and end-of-file */ michael@0: if (state->mode == GZ_READ) { michael@0: state->eof = 0; michael@0: state->past = 0; michael@0: } michael@0: gz_error(state, Z_OK, NULL); michael@0: } michael@0: michael@0: /* Create an error message in allocated memory and set state->err and michael@0: state->msg accordingly. Free any previous error message already there. Do michael@0: not try to free or allocate space if the error is Z_MEM_ERROR (out of michael@0: memory). Simply save the error message as a static string. If there is an michael@0: allocation failure constructing the error message, then convert the error to michael@0: out of memory. */ michael@0: void ZLIB_INTERNAL gz_error(state, err, msg) michael@0: gz_statep state; michael@0: int err; michael@0: const char *msg; michael@0: { michael@0: /* free previously allocated message and clear */ michael@0: if (state->msg != NULL) { michael@0: if (state->err != Z_MEM_ERROR) michael@0: free(state->msg); michael@0: state->msg = NULL; michael@0: } michael@0: michael@0: /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ michael@0: if (err != Z_OK && err != Z_BUF_ERROR) michael@0: state->x.have = 0; michael@0: michael@0: /* set error code, and if no message, then done */ michael@0: state->err = err; michael@0: if (msg == NULL) michael@0: return; michael@0: michael@0: /* for an out of memory error, return literal string when requested */ michael@0: if (err == Z_MEM_ERROR) michael@0: return; michael@0: michael@0: /* construct error message with path */ michael@0: if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == michael@0: NULL) { michael@0: state->err = Z_MEM_ERROR; michael@0: return; michael@0: } michael@0: #if !defined(NO_snprintf) && !defined(NO_vsnprintf) michael@0: snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, michael@0: "%s%s%s", state->path, ": ", msg); michael@0: #else michael@0: strcpy(state->msg, state->path); michael@0: strcat(state->msg, ": "); michael@0: strcat(state->msg, msg); michael@0: #endif michael@0: return; michael@0: } michael@0: michael@0: #ifndef INT_MAX michael@0: /* portably return maximum value for an int (when limits.h presumed not michael@0: available) -- we need to do this to cover cases where 2's complement not michael@0: used, since C standard permits 1's complement and sign-bit representations, michael@0: otherwise we could just use ((unsigned)-1) >> 1 */ michael@0: unsigned ZLIB_INTERNAL gz_intmax() michael@0: { michael@0: unsigned p, q; michael@0: michael@0: p = 1; michael@0: do { michael@0: q = p; michael@0: p <<= 1; michael@0: p++; michael@0: } while (p > q); michael@0: return q >> 1; michael@0: } michael@0: #endif