Esempio n. 1
0
/* Compress whatever is at avail_in and next_in and write to the output file.
   Return -1 if there is an error writing to the output file, otherwise 0.
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
   then the deflate() state is reset to start a new gzip stream.  If gz->direct
   is true, then simply write to the output file without compressing, and
   ignore flush. */
static int gz_comp(gz_state *state,
                   int flush)
{
    int ret, got;
    unsigned int have;
    z_stream *strm = &state->strm;

    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
        return -1;

    /* write directly if requested */
    if (state->direct) {
        got = write(state->fd, strm->next_in, strm->avail_in);
        if (got < 0 || (unsigned int)got != strm->avail_in) {
            gz_error(state, Z_ERRNO, strerror(errno));
            return -1;
        }
        strm->avail_in = 0;
        return 0;
    }

    /* run deflate() on provided input until it produces no more output */
    ret = Z_OK;
    do {
        /* write out current buffer contents if full, or if flushing, but if
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
            have = (unsigned int)(strm->next_out - state->x.next);
            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
                         (unsigned int)got != have)) {
                gz_error(state, Z_ERRNO, strerror(errno));
                return -1;
            }
            if (strm->avail_out == 0) {
                strm->avail_out = state->size;
                strm->next_out = state->out;
            }
            state->x.next = strm->next_out;
        }

        /* compress */
        have = strm->avail_out;
        ret = deflate(strm, flush);
        if (ret == Z_STREAM_ERROR) {
            gz_error(state, Z_STREAM_ERROR,
                      "internal error: deflate stream corrupt");
            return -1;
        }
        have -= strm->avail_out;
    } while (have);

    /* if that completed a deflate stream, allow another to start */
    if (flush == Z_FINISH)
        deflateReset(strm);

    /* all done, no errors */
    return 0;
}
Esempio n. 2
0
/* Decompress from input to the provided next_out and avail_out in the state.
   On return, state->x.have and state->x.next point to the just decompressed
   data.  If the gzip stream completes, state->how is reset to LOOK to look for
   the next gzip stream or raw data, once state->x.have is depleted.  Returns 0
   on success, -1 on failure. */
local int
gz_decomp (gz_statep state)
{
  int ret = Z_OK;
  unsigned had;
  z_streamp strm = &(state->strm);

  /* fill output buffer up to end of deflate stream */
  had = strm->avail_out;
  do
    {
      /* get more input for inflate() */
      if (strm->avail_in == 0 && gz_avail (state) == -1)
	return -1;
      if (strm->avail_in == 0)
	{
	  gz_error (state, Z_BUF_ERROR, "unexpected end of file");
	  break;
	}

      /* decompress and handle errors */
      ret = inflate (strm, Z_NO_FLUSH);
      if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT)
	{
	  gz_error (state, Z_STREAM_ERROR,
		    "internal error: inflate stream corrupt");
	  return -1;
	}
      if (ret == Z_MEM_ERROR)
	{
	  gz_error (state, Z_MEM_ERROR, "out of memory");
	  return -1;
	}
      if (ret == Z_DATA_ERROR)
	{			/* deflate stream invalid */
	  gz_error (state, Z_DATA_ERROR,
		    strm->msg == NULL ? "compressed data error" : strm->msg);
	  return -1;
	}
    }
  while (strm->avail_out && ret != Z_STREAM_END);

  /* update available output */
  state->x.have = had - strm->avail_out;
  state->x.next = strm->next_out - state->x.have;

  /* if the gzip stream completed successfully, look for another */
  if (ret == Z_STREAM_END)
    state->how = LOOK;

  /* good decompression */
  return 0;
}
Esempio n. 3
0
/* Initialize state for writing a gzip file.  Mark initialization by setting
   state->size to non-zero.  Return -1 on failure or 0 on success. */
static int gz_init(gz_state *state)
{
    int ret;
    z_stream *strm = &state->strm;

    /* allocate input buffer */
    state->in = (unsigned char *)malloc(state->want);
    if (state->in == NULL) {
        gz_error(state, Z_MEM_ERROR, "out of memory");
        return -1;
    }

    /* only need output buffer and deflate state if compressing */
    if (!state->direct) {
        /* allocate output buffer */
        state->out = (unsigned char *)malloc(state->want);
        if (state->out == NULL) {
            free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }

        /* allocate deflate memory, set up for gzip compression */
        strm->zalloc = NULL;
        strm->zfree = NULL;
        strm->opaque = NULL;
        ret = deflateInit2(strm, state->level, Z_DEFLATED,
                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
        if (ret != Z_OK) {
            free(state->out);
            free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
        strm->next_in = NULL;
    }

    /* mark state as initialized */
    state->size = state->want;

    /* initialize write buffer if compressing */
    if (!state->direct) {
        strm->avail_out = state->size;
        strm->next_out = state->out;
        state->x.next = strm->next_out;
    }
    return 0;
}
Esempio n. 4
0
/* -- see zlib.h -- */
int ZEXPORT gzclose_w(gzFile file)
{
    int ret = 0;
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return Z_STREAM_ERROR;
    state = (gz_statep)file;

    /* check that we're writing */
    if (state->mode != GZ_WRITE)
        return Z_STREAM_ERROR;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        ret += gz_zero(state, state->skip);
    }

    /* flush, free memory, and close file */
    ret += gz_comp(state, Z_FINISH);
    (void)deflateEnd(&(state->strm));
    free(state->out);
    free(state->in);
    gz_error(state, Z_OK, NULL);
    free(state->path);
    ret += close(state->fd);
    free(state);
    return ret ? Z_ERRNO : Z_OK;
}
Esempio n. 5
0
/* -- see zlib.h -- */
int ZEXPORT
gzclose_r (gzFile file)
{
  int ret, err;
  gz_statep state;

  /* get internal structure */
  if (file == NULL)
    return Z_STREAM_ERROR;
  state = (gz_statep) file;

  /* check that we're reading */
  if (state->mode != GZ_READ)
    return Z_STREAM_ERROR;

  /* free memory and close file */
  if (state->size)
    {
      inflateEnd (&(state->strm));
      free (state->out);
      free (state->in);
    }
  err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
  gz_error (state, Z_OK, NULL);
  free (state->path);
  ret = close (state->fd);
  free (state);
  return ret ? Z_ERRNO : err;
}
Esempio n. 6
0
/* -- see zlib.h -- */
int ZEXPORT gzungetc(
    int c,
    gzFile file)
{
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return -1;
    state = (gz_statep)file;

    /* check that we're reading and that there's no (serious) error */
    if (state->mode != GZ_READ ||
        (state->err != Z_OK && state->err != Z_BUF_ERROR))
        return -1;

    /* process a skip request */
    if (state->seek) {
        state->seek = 0;
        if (gz_skip(state, state->skip) == -1)
            return -1;
    }

    /* can't push EOF */
    if (c < 0)
        return -1;

    /* if output buffer empty, put byte at end (allows more pushing) */
    if (state->x.have == 0) {
        state->x.have = 1;
        state->x.next = state->out + (state->size << 1) - 1;
        state->x.next[0] = c;
        state->x.pos--;
        state->past = 0;
        return c;
    }

    /* if no room, give up (must have already done a gzungetc()) */
    if (state->x.have == (state->size << 1)) {
        gz_error(state, Z_DATA_ERROR, "out of room to push characters");
        return -1;
    }

    /* slide output data if needed and insert byte before existing data */
    if (state->x.next == state->out) {
        unsigned char *src = state->out + state->x.have;
        unsigned char *dest = state->out + (state->size << 1);
        while (src > state->out)
            *--dest = *--src;
        state->x.next = dest;
    }
    state->x.have++;
    state->x.next--;
    state->x.next[0] = c;
    state->x.pos--;
    state->past = 0;
    return c;
}
Esempio n. 7
0
File: gzlib.c Progetto: pikhq/libz
/* Reset gzip file state */
static void gz_reset(gz_state *state)
{
    state->x.have = 0;              /* no output data available */
    if (state->mode == GZ_READ) {   /* for reading ... */
        state->eof = 0;             /* not at end of file */
        state->past = 0;            /* have not read past end yet */
        state->how = LOOK;          /* look for gzip header */
    }
    state->seek = 0;                /* no seek request pending */
    gz_error(state, Z_OK, NULL);    /* clear error */
    state->x.pos = 0;               /* no uncompressed data yet */
    state->strm.avail_in = 0;       /* no input data yet */
}
Esempio n. 8
0
/* Initialize state for writing a gzip file.  Mark initialization by setting
   state->size to non-zero.  Return -1 on failure or 0 on success. */
local int gz_init(gz_statep state)
{
    int ret;
    z_streamp strm = &(state->strm);

    /* allocate input and output buffers */
    state->in = (unsigned char *)malloc(state->want);
    state->out = (unsigned char *)malloc(state->want);
    if (state->in == NULL || state->out == NULL) {
        if (state->out != NULL)
            free(state->out);
        if (state->in != NULL)
            free(state->in);
        gz_error(state, Z_MEM_ERROR, "out of memory");
        return -1;
    }

    /* allocate deflate memory, set up for gzip compression */
    strm->zalloc = Z_NULL;
    strm->zfree = Z_NULL;
    strm->opaque = Z_NULL;
    ret = deflateInit2(strm, state->level, Z_DEFLATED,
                       15 + 16, 8, state->strategy);
    if (ret != Z_OK) {
        free(state->in);
        gz_error(state, Z_MEM_ERROR, "out of memory");
        return -1;
    }

    /* mark state as initialized */
    state->size = state->want;

    /* initialize write buffer */
    strm->avail_out = state->size;
    strm->next_out = state->out;
    state->next = strm->next_out;
    return 0;
}
Esempio n. 9
0
/* Reset gzip file state */
local void gz_reset(
    gz_statep state)
{
    if (state->mode == GZ_READ) {   /* for reading ... */
        state->have = 0;            /* no output data available */
        state->eof = 0;             /* not at end of file */
        state->how = LOOK;          /* look for gzip header */
        state->direct = 1;          /* default for empty file */
    }
    state->seek = 0;                /* no seek request pending */
    gz_error(state, Z_OK, NULL);    /* clear error */
    state->pos = 0;                 /* no uncompressed data yet */
    state->strm.avail_in = 0;       /* no input data yet */
}
Esempio n. 10
0
/* -- see zlib.h -- */
void ZEXPORT gzclearerr(
    gzFile file)
{
    gz_statep state;

    /* get internal structure and check integrity */
    if (file == NULL)
        return;
    state = (gz_statep)file;
    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
        return;

    /* clear error and end-of-file */
    if (state->mode == GZ_READ)
        state->eof = 0;
    gz_error(state, Z_OK, NULL);
}
Esempio n. 11
0
/* Use read() to load a buffer -- return -1 on error, otherwise 0.  Read from
   state->fd, and update state->eof, state->err, and state->msg as appropriate.
   This function needs to loop on read(), since read() is not guaranteed to
   read the number of bytes requested, depending on the type of descriptor. */
local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have)
{
   int ret;

   *have = 0;
   do {
      ret = read(state->fd, buf + *have, len - *have);
      if (ret <= 0)
         break;
      *have += ret;
   } while (*have < len);
   if (ret < 0) {
      gz_error(state, Z_ERRNO, zstrerror());
      return -1;
   }
   if (ret == 0)
      state->eof = 1;
   return 0;
}
Esempio n. 12
0
/* -- see zlib.h -- */
int ZEXPORT gzclose_w(gzFile file)
{
    int ret = Z_OK;
    gz_state *state;

    /* get internal structure */
    if (file == NULL)
        return Z_STREAM_ERROR;
    state = (gz_state *)file;

    /* check that we're writing */
    if (state->mode != GZ_WRITE)
        return Z_STREAM_ERROR;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            ret = state->err;
    }

    /* flush, free memory, and close file */
    if (gz_comp(state, Z_FINISH) == -1)
        ret = state->err;
    if (state->size) {
        if (!state->direct) {
            (void)deflateEnd(&(state->strm));
            free(state->out);
        }
        free(state->in);
    }
    gz_error(state, Z_OK, NULL);
    free(state->path);
    if (close(state->fd) == -1)
        ret = Z_ERRNO;
    free(state);
    return ret;
}
Esempio n. 13
0
/* -- see zlib.h -- */
int ZEXPORT
gzread (gzFile file, voidp buf, unsigned len)
{
  unsigned got, n;
  gz_statep state;
  z_streamp strm;

  /* get internal structure */
  if (file == NULL)
    return -1;
  state = (gz_statep) file;
  strm = &(state->strm);

  /* check that we're reading and that there's no (serious) error */
  if (state->mode != GZ_READ ||
      (state->err != Z_OK && state->err != Z_BUF_ERROR))
    return -1;

  /* since an int is returned, make sure len fits in one, otherwise return
     with an error (this avoids the flaw in the interface) */
  if ((int) len < 0)
    {
      gz_error (state, Z_DATA_ERROR, "requested length does not fit in int");
      return -1;
    }

  /* if len is zero, avoid unnecessary operations */
  if (len == 0)
    return 0;

  /* process a skip request */
  if (state->seek)
    {
      state->seek = 0;
      if (gz_skip (state, state->skip) == -1)
	return -1;
    }

  /* get len bytes to buf, or less than len if at the end */
  got = 0;
  do
    {
      /* first just try copying data from the output buffer */
      if (state->x.have)
	{
	  n = state->x.have > len ? len : state->x.have;
	  memcpy (buf, state->x.next, n);
	  state->x.next += n;
	  state->x.have -= n;
	}

      /* output buffer empty -- return if we're at the end of the input */
      else if (state->eof && strm->avail_in == 0)
	{
	  state->past = 1;	/* tried to read past end */
	  break;
	}

      /* need output data -- for small len or new stream load up our output
         buffer */
      else if (state->how == LOOK || len < (state->size << 1))
	{
	  /* get more output, looking for header if required */
	  if (gz_fetch (state) == -1)
	    return -1;
	  continue;		/* no progress yet -- go back to copy above */
	  /* the copy above assures that we will leave with space in the
	     output buffer, allowing at least one gzungetc() to succeed */
	}

      /* large len -- read directly into user buffer */
      else if (state->how == COPY)
	{			/* read directly */
	  if (gz_load (state, (unsigned char *) buf, len, &n) == -1)
	    return -1;
	}

      /* large len -- decompress directly into user buffer */
      else
	{			/* state->how == GZIP */
	  strm->avail_out = len;
	  strm->next_out = (unsigned char *) buf;
	  if (gz_decomp (state) == -1)
	    return -1;
	  n = state->x.have;
	  state->x.have = 0;
	}

      /* update progress */
      len -= n;
      buf = (char *) buf + n;
      got += n;
      state->x.pos += n;
    }
  while (len);

  /* return number of bytes read into user buffer (will fit in int) */
  return (int) got;
}
Esempio n. 14
0
/* -- see zlib.h -- */
int ZEXPORT gzwrite(gzFile file,
                    const void *buf,
                    unsigned int len)
{
    unsigned put = len;
    gz_state *state;
    z_stream *strm;

    /* get internal structure */
    if (file == NULL)
        return 0;
    state = (gz_state *)file;
    strm = &state->strm;

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return 0;

    /* since an int is returned, make sure len fits in one, otherwise return
       with an error (this avoids the flaw in the interface) */
    if ( (unsigned int) INT_MAX < len ) {
        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
        return 0;
    }

    /* if len is zero, avoid unnecessary operations */
    if (len == 0)
        return 0;

    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
        return 0;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            return 0;
    }

    /* for small len, copy to input buffer, otherwise compress directly */
    if (len < state->size) {
        /* copy to input buffer, compress when full */
        do {
            unsigned int have, copy;

            if (strm->avail_in == 0)
                strm->next_in = state->in;
            have = (unsigned int)((strm->next_in + strm->avail_in) - state->in);
            copy = state->size - have;
            if (copy > len)
                copy = len;
            memcpy(state->in + have, buf, copy);
            strm->avail_in += copy;
            state->x.pos += copy;
            buf = (const char *)buf + copy;
            len -= copy;
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
                return 0;
        } while (len);
    }
    else {
        /* consume whatever's left in the input buffer */
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
            return 0;

        /* directly compress user buffer to file */
        strm->avail_in = len;
        strm->next_in = (const unsigned char *)buf;
        state->x.pos += len;
        if (gz_comp(state, Z_NO_FLUSH) == -1)
            return 0;
    }

    /* input was all buffered or compressed (put will fit in int) */
    return (int)put;
}
Esempio n. 15
0
/* Decompress from input to the provided next_out and avail_out in the state.
   If the end of the compressed data is reached, then verify the gzip trailer
   check value and length (modulo 2^32).  state->have and state->next are set
   to point to the just decompressed data, and the crc is updated.  If the
   trailer is verified, state->how is reset to LOOK to look for the next gzip
   stream or raw data, once state->have is depleted.  Returns 0 on success, -1
   on failure.  Failures may include invalid compressed data or a failed gzip
   trailer verification. */
local int gz_decomp(gz_statep state)
{
    int ret;
    unsigned had;
    unsigned long crc, len;
    z_streamp strm = &(state->strm);

    /* fill output buffer up to end of deflate stream */
    had = strm->avail_out;
    do {
        /* get more input for inflate() */
        if (strm->avail_in == 0 && gz_avail(state) == -1)
            return -1;
        if (strm->avail_in == 0) {
            gz_error(state, Z_DATA_ERROR, "unexpected end of file");
            return -1;
        }

        /* decompress and handle errors */
        ret = inflate(strm, Z_NO_FLUSH);
        if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
            gz_error(state, Z_STREAM_ERROR,
                      "internal error: inflate stream corrupt");
            return -1;
        }
        if (ret == Z_MEM_ERROR) {
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
        if (ret == Z_DATA_ERROR) {              /* deflate stream invalid */
            gz_error(state, Z_DATA_ERROR,
                      strm->msg == NULL ? "compressed data error" : strm->msg);
            return -1;
        }
    } while (strm->avail_out && ret != Z_STREAM_END);

    /* update available output and crc check value */
    state->have = had - strm->avail_out;
    state->next = strm->next_out - state->have;
    strm->adler = crc32(strm->adler, state->next, state->have);

    /* check gzip trailer if at end of deflate stream */
    if (ret == Z_STREAM_END) {
        if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
            gz_error(state, Z_DATA_ERROR, "unexpected end of file");
            return -1;
        }
        if (crc != strm->adler) {
            gz_error(state, Z_DATA_ERROR, "incorrect data check");
            return -1;
        }
        if (len != (strm->total_out & 0xffffffffL)) {
            gz_error(state, Z_DATA_ERROR, "incorrect length check");
            return -1;
        }
        state->how = LOOK;      /* ready for next stream, once have is 0 (leave
                                   state->direct unchanged to remember how) */
    }

    /* good decompression */
    return 0;
}
Esempio n. 16
0
/* Look for gzip header, set up for inflate or copy.  state->have must be zero.
   If this is the first time in, allocate required memory.  state->how will be
   left unchanged if there is no more input data available, will be set to COPY
   if there is no gzip header and direct copying will be performed, or it will
   be set to GZIP for decompression, and the gzip header will be skipped so
   that the next available input data is the raw deflate stream.  If direct
   copying, then leftover input data from the input buffer will be copied to
   the output buffer.  In that case, all further file reads will be directly to
   either the output buffer or a user buffer.  If decompressing, the inflate
   state and the check value will be initialized.  gz_head() will return 0 on
   success or -1 on failure.  Failures may include read errors or gzip header
   errors.  */
local int gz_head(gz_statep state)
{
    z_streamp strm = &(state->strm);
    int flags;
    unsigned len;

    /* allocate read buffers and inflate memory */
    if (state->size == 0) {
        /* allocate buffers */
        state->in = (unsigned char *)malloc(state->want);
        state->out = (unsigned char *)malloc(state->want << 1);
        if (state->in == NULL || state->out == NULL) {
            if (state->out != NULL)
                free(state->out);
            if (state->in != NULL)
                free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
        state->size = state->want;

        /* allocate inflate memory */
        state->strm.zalloc = Z_NULL;
        state->strm.zfree = Z_NULL;
        state->strm.opaque = Z_NULL;
        state->strm.avail_in = 0;
        state->strm.next_in = Z_NULL;
        if (inflateInit2(&(state->strm), -15) != Z_OK) {    /* raw inflate */
            free(state->out);
            free(state->in);
            state->size = 0;
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
    }

    /* get some data in the input buffer */
    if (strm->avail_in == 0) {
        if (gz_avail(state) == -1)
            return -1;
        if (strm->avail_in == 0)
            return 0;
    }

    /* look for the gzip magic header bytes 31 and 139 */
    if (strm->next_in[0] == 31) {
        strm->avail_in--;
        strm->next_in++;
        if (strm->avail_in == 0 && gz_avail(state) == -1)
            return -1;
        if (strm->avail_in && strm->next_in[0] == 139) {
            /* we have a gzip header, woo hoo! */
            strm->avail_in--;
            strm->next_in++;

            /* skip rest of header */
            if (NEXT() != 8) {      /* compression method */
                gz_error(state, Z_DATA_ERROR, "unknown compression method");
                return -1;
            }
            flags = NEXT();
            if (flags & 0xe0) {     /* reserved flag bits */
                gz_error(state, Z_DATA_ERROR, "unknown header flags set");
                return -1;
            }
            NEXT();                 /* modification time */
            NEXT();
            NEXT();
            NEXT();
            NEXT();                 /* extra flags */
            NEXT();                 /* operating system */
            if (flags & 4) {        /* extra field */
                len = (unsigned)NEXT();
                len += (unsigned)NEXT() << 8;
                while (len--)
                    if (NEXT() < 0)
                        break;
            }
            if (flags & 8)          /* file name */
                while (NEXT() > 0)
                    ;
            if (flags & 16)         /* comment */
                while (NEXT() > 0)
                    ;
            if (flags & 2) {        /* header crc */
                NEXT();
                NEXT();
            }
            /* an unexpected end of file is not checked for here -- it will be
               noticed on the first request for uncompressed data */

            /* set up for decompression */
            inflateReset(strm);
            strm->adler = crc32(0L, Z_NULL, 0);
            state->how = GZIP;
            state->direct = 0;
            return 0;
        }
        else {
            /* not a gzip file -- save first byte (31) and fall to raw i/o */
            state->out[0] = 31;
            state->have = 1;
        }
    }

    /* doing raw i/o, save start of raw data for seeking, copy any leftover
       input to output -- this assumes that the output buffer is larger than
       the input buffer, which also assures space for gzungetc() */
    state->raw = state->pos;
    state->next = state->out;
    if (strm->avail_in) {
        memcpy(state->next + state->have, strm->next_in, strm->avail_in);
        state->have += strm->avail_in;
        strm->avail_in = 0;
    }
    state->how = COPY;
    state->direct = 1;
    return 0;
}
Esempio n. 17
0
/* Look for gzip header, set up for inflate or copy.  state->x.have must be 0.
   If this is the first time in, allocate required memory.  state->how will be
   left unchanged if there is no more input data available, will be set to COPY
   if there is no gzip header and direct copying will be performed, or it will
   be set to GZIP for decompression.  If direct copying, then leftover input
   data from the input buffer will be copied to the output buffer.  In that
   case, all further file reads will be directly to either the output buffer or
   a user buffer.  If decompressing, the inflate state will be initialized.
   gz_look() will return 0 on success or -1 on failure. */
local int
gz_look (gz_statep state)
{
  z_streamp strm = &(state->strm);

  /* allocate read buffers and inflate memory */
  if (state->size == 0)
    {
      /* allocate buffers */
      state->in = (unsigned char *) malloc (state->want);
      state->out = (unsigned char *) malloc (state->want << 1);
      if (state->in == NULL || state->out == NULL)
	{
	  if (state->out != NULL)
	    free (state->out);
	  if (state->in != NULL)
	    free (state->in);
	  gz_error (state, Z_MEM_ERROR, "out of memory");
	  return -1;
	}
      state->size = state->want;

      /* allocate inflate memory */
      state->strm.zalloc = Z_NULL;
      state->strm.zfree = Z_NULL;
      state->strm.opaque = Z_NULL;
      state->strm.avail_in = 0;
      state->strm.next_in = Z_NULL;
      if (inflateInit2 (&(state->strm), 15 + 16) != Z_OK)
	{			/* gunzip */
	  free (state->out);
	  free (state->in);
	  state->size = 0;
	  gz_error (state, Z_MEM_ERROR, "out of memory");
	  return -1;
	}
    }

  /* get at least the magic bytes in the input buffer */
  if (strm->avail_in < 2)
    {
      if (gz_avail (state) == -1)
	return -1;
      if (strm->avail_in == 0)
	return 0;
    }

  /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
     a logical dilemma here when considering the case of a partially written
     gzip file, to wit, if a single 31 byte is written, then we cannot tell
     whether this is a single-byte file, or just a partially written gzip
     file -- for here we assume that if a gzip file is being written, then
     the header will be written in a single operation, so that reading a
     single byte is sufficient indication that it is not a gzip file) */
  if (strm->avail_in > 1 && strm->next_in[0] == 31 && strm->next_in[1] == 139)
    {
      inflateReset (strm);
      state->how = GZIP;
      state->direct = 0;
      return 0;
    }

  /* no gzip header -- if we were decoding gzip before, then this is trailing
     garbage.  Ignore the trailing garbage and finish. */
  if (state->direct == 0)
    {
      strm->avail_in = 0;
      state->eof = 1;
      state->x.have = 0;
      return 0;
    }

  /* doing raw i/o, copy any leftover input to output -- this assumes that
     the output buffer is larger than the input buffer, which also assures
     space for gzungetc() */
  state->x.next = state->out;
  if (strm->avail_in)
    {
      memcpy (state->x.next, strm->next_in, strm->avail_in);
      state->x.have = strm->avail_in;
      strm->avail_in = 0;
    }
  state->how = COPY;
  state->direct = 1;
  return 0;
}
Esempio n. 18
0
/* -- see zlib.h -- */
z_off64_t ZEXPORT gzseek64(
    gzFile file,
    z_off64_t offset,
    int whence)
{
    unsigned n;
    z_off64_t ret;
    gz_statep state;

    /* get internal structure and check integrity */
    if (file == NULL)
        return -1;
    state = (gz_statep)file;
    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
        return -1;

    /* check that there's no error */
    if (state->err != Z_OK)
        return -1;

    /* can only seek from start or relative to current position */
    if (whence != SEEK_SET && whence != SEEK_CUR)
        return -1;

    /* normalize offset to a SEEK_CUR specification */
    if (whence == SEEK_SET)
        offset -= state->pos;
    else if (state->seek)
        offset += state->skip;
    state->seek = 0;

    /* if within raw area while reading, just go there */
    if (state->mode == GZ_READ && state->how == COPY &&
        state->pos + offset >= state->raw) {
        ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
        if (ret == -1)
            return -1;
        state->have = 0;
        state->eof = 0;
        state->seek = 0;
        gz_error(state, Z_OK, NULL);
        state->strm.avail_in = 0;
        state->pos += offset;
        return state->pos;
    }

    /* calculate skip amount, rewinding if needed for back seek when reading */
    if (offset < 0) {
        if (state->mode != GZ_READ)         /* writing -- can't go backwards */
            return -1;
        offset += state->pos;
        if (offset < 0)                     /* before start of file! */
            return -1;
        if (gzrewind(file) == -1)           /* rewind, then skip to offset */
            return -1;
    }

    /* if reading, skip what's in output buffer (one less gzgetc() check) */
    if (state->mode == GZ_READ) {
        n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
            (unsigned)offset : state->have;
        state->have -= n;
        state->next += n;
        state->pos += n;
        offset -= n;
    }

    /* request skip (if not zero) */
    if (offset) {
        state->seek = 1;
        state->skip = offset;
    }
    return state->pos + offset;
}