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