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