Exemplo n.º 1
0
/* This is an auxilliary function that lbz_getline calls when it needs to
 * actually use the BZ2_bzRead method to read more data from the bzipped file.
 **/
static int lbz_getline_read(lua_State *L, luaL_Buffer *b, lbz_state *state, int keep_eol) {
	int bzerror;

	/* The entire 'extra_buf' buffer is needed */
	luaL_addlstring(b, state->buf, state->buf_size);
	lbz_buffer_drain_all(state);

	if (!state->bz_stream) { /* No more data left at all - return data is 'success' */
		lbz_perform_close(state, 0); // Completely close it out now
		luaL_pushresult(b);
		return 1;
	}
	while(1) {
		char *buf = luaL_prepbuffer(b);
		int len = BZ2_bzRead(&bzerror, state->bz_stream, buf, LUAL_BUFFERSIZE);

		if ((bzerror != BZ_OK) && (bzerror != BZ_STREAM_END)) {
			/* Error happened, data thrown */
			lua_pushnil(L);
			lua_pushstring(L, BZ2_bzerror(state->bz_stream, &bzerror));
			return 2;
		}
		if (!lbz_handle_eol(b, buf, len, state, 1, keep_eol))
			continue;

		/* Kill the stream, keep the remaining buffer */
		if (bzerror == BZ_STREAM_END)
			lbz_perform_close(state, state->buf_size ? 1 : 0);
		return 1;
	}
	return 0;
}
Exemplo n.º 2
0
static const char * bz2_error(int errnum)
{
	bz2_file B;
	int n;
	B.lastErr = errnum;
	return BZ2_bzerror((BZFILE*)&B, &n);
}
Exemplo n.º 3
0
MIKTEXCALLBACK
BZip2ReaderThread (/*[in]*/ void * pv)
{
    try
    {
        auto_ptr<BZip2ReaderThreadArg>
        pArg (reinterpret_cast<BZip2ReaderThreadArg*>(pv));
        MIKTEX_ASSERT (pArg->bzin != 0);
        MIKTEX_ASSERT (pArg->fileout != 0);
        AutoBZ2 autoCloseBZip2Stream (pArg->bzin);
        FileStream autoCloseOutput (pArg->fileout);
        char buf[PIPE_SIZE];
        int len;
        while (! ferror(pArg->fileout)
                && (len = BZ2_bzread(pArg->bzin, buf, ARRAY_SIZE(buf))) > 0)
        {
            fwrite (buf, 1, len, pArg->fileout);
        }
        int bzerr;
        BZ2_bzerror (pArg->bzin, &bzerr);
        if (bzerr != BZ_OK)
        {
            FATAL_MIKTEX_ERROR ("BZip2ReaderThread",
                                T_("BZ2_bzread() failed for some reason."),
                                0);
        }
        if (ferror(pArg->fileout))
        {
            FATAL_CRT_ERROR ("fwrite", 0);
        }
    }
    catch (const exception &)
    {
    }
}
Exemplo n.º 4
0
Variant BZ2File::error() {
  assert(m_bzFile);
  int errnum;
  const char * errstr;
  errstr = BZ2_bzerror(m_bzFile, &errnum);
  return make_map_array(s_errno, errnum, s_errstr, String(errstr));
}
Exemplo n.º 5
0
static void
decompress_bzip2(int fd_in, int fd_out, const char *desc)
{
	char buffer[DPKG_BUFFER_SIZE];
	BZFILE *bzfile = BZ2_bzdopen(fd_in, "r");

	if (bzfile == NULL)
		ohshit(_("%s: error binding input to bzip2 stream"), desc);

	for (;;) {
		int actualread, actualwrite;

		actualread = BZ2_bzread(bzfile, buffer, sizeof(buffer));
		if (actualread < 0) {
			int bz_errnum = 0;
			const char *errmsg = BZ2_bzerror(bzfile, &bz_errnum);

			if (bz_errnum == BZ_IO_ERROR)
				errmsg = strerror(errno);
			ohshit(_("%s: internal bzip2 read error: '%s'"), desc,
			       errmsg);
		}
		if (actualread == 0) /* EOF. */
			break;

		actualwrite = fd_write(fd_out, buffer, actualread);
		if (actualwrite != actualread)
			ohshite(_("%s: internal bzip2 write error"), desc);
	}

	if (close(fd_out))
		ohshite(_("%s: internal bzip2 write error"), desc);
}
Exemplo n.º 6
0
Variant BZ2File::error() {
  assert(m_bzFile);
  int errnum;
  const char * errstr;
  errstr = BZ2_bzerror(m_bzFile, &errnum);
  return CREATE_MAP2("errno", errnum, "errstr", String(errstr));
}
Exemplo n.º 7
0
Arquivo: rpmio.c Projeto: akozumpl/rpm
static int bzdClose(FD_t fd)
{
    BZFILE *bzfile;
    int rc;

    bzfile = bzdFileno(fd);

    if (bzfile == NULL) return -2;
    /* FIX: check rc */
    BZ2_bzclose(bzfile);
    rc = 0;	/* XXX FIXME */

    /* XXX TODO: preserve fd if errors */

    if (fd) {
	if (rc == -1) {
	    int zerror = 0;
	    fd->errcookie = BZ2_bzerror(bzfile, &zerror);
	}
    }

    if (_rpmio_debug || rpmIsDebug()) fdstat_print(fd, "BZDIO", stderr);
    if (rc == 0)
	fdFree(fd);
    return rc;
}
Exemplo n.º 8
0
/*	Push data from a bzip file pointer down a stream
**	-------------------------------------
**
**   This routine is responsible for creating and PRESENTING any
**   graphic (or other) objects described by the file.
**
**
**  State of file and target stream on entry:
**		      BZFILE (bzfp) assumed open (should have bzipped content),
**		      target (sink) assumed valid.
**
**  Return values:
**	HT_INTERRUPTED  Interruption after some data read.
**	HT_PARTIAL_CONTENT	Error after some data read.
**	-1		Error before any data read.
**	HT_LOADED	Normal end of file indication on reading.
**
**  State of file and target stream on return:
**	always		bzfp still open, target stream still valid.
*/
PRIVATE int HTBzFileCopy ARGS2(
	BZFILE *,		bzfp,
	HTStream*,		sink)
{
    HTStreamClass targetClass;
    int status, bytes;
    int bzerrnum;
    int rv = HT_OK;

    /*	Push the data down the stream
    */
    targetClass = *(sink->isa); /* Copy pointers to procedures */

    /*	read and inflate bzip'd file, and push binary down sink
    */
    HTReadProgress(bytes = 0, 0);
    for (;;) {
	status = BZ2_bzread(bzfp, input_buffer, INPUT_BUFFER_SIZE);
	if (status <= 0) { /* EOF or error */
	    if (status == 0) {
		rv = HT_LOADED;
		break;
	    }
	    CTRACE((tfp, "HTBzFileCopy: Read error, bzread returns %d\n",
			status));
	    CTRACE((tfp, "bzerror   : %s\n",
			BZ2_bzerror(bzfp, &bzerrnum)));
	    if (bytes) {
		rv = HT_PARTIAL_CONTENT;
	    } else {
		rv = -1;
	    }
	    break;
	}

	(*targetClass.put_block)(sink, input_buffer, status);
	bytes += status;
	HTReadProgress(bytes, -1);
	HTDisplayPartial();

	if (HTCheckForInterrupt()) {
	    _HTProgress (TRANSFER_INTERRUPTED);
	    if (bytes) {
		rv = HT_INTERRUPTED;
	    } else {
		rv = -1;
	    }
	    break;
	}
    } /* next bufferload */

    HTFinishDisplayPartial();
    return rv;
}
Exemplo n.º 9
0
static ssize_t bzdWrite(FDSTACK_t fps, const void * buf, size_t count)
{
    BZFILE *bzfile = fps->fp;
    ssize_t rc;

    rc = BZ2_bzwrite(bzfile, (void *)buf, count);
    if (rc == -1) {
	int zerror = 0;
	fps->errcookie = BZ2_bzerror(bzfile, &zerror);
    }
    return rc;
}
Exemplo n.º 10
0
	bzstreambuf *bzstreambuf::close(  )
	{
		if ( is_open(  ) )
		{
			sync(  );
			opened = 0;
			BZ2_bzclose( file );
                        int errnum;
                        BZ2_bzerror( file, &errnum );
                        if( BZ_OK == errnum ) return this;
		}
		return ( bzstreambuf * ) 0;
	}
Exemplo n.º 11
0
Arquivo: rpmio.c Projeto: akozumpl/rpm
static ssize_t bzdWrite(FD_t fd, const void * buf, size_t count)
{
    BZFILE *bzfile;
    ssize_t rc;

    bzfile = bzdFileno(fd);
    rc = BZ2_bzwrite(bzfile, (void *)buf, count);
    if (rc == -1) {
	int zerror = 0;
	fd->errcookie = BZ2_bzerror(bzfile, &zerror);
    }
    return rc;
}
Exemplo n.º 12
0
/* Binding to libbzip2's BZ2_bzReadOpen method */
static int lbz_read(lua_State *L) {
	int bzerror = BZ_OK;
	int len;
	luaL_Buffer b;
	lbz_state *state = lbz_check_state(L, 1);
	len = luaL_checkint(L, 2);

	if (!state->bz_stream && !state->buf) {
		/* The logical end of file has been reached -- there's no more data to
		 * return, and the user should call the read_close method. */
		lua_pushnil(L);
		lua_pushstring(L, "CLOSED");
		return 2;
	}
	luaL_buffinit(L, &b);

	/* In case this function is being used alongsize the getline method, we
	 * should use the buffers that getline is using */
	if (state->buf_size) {
		int used_len = (state->buf_size < len) ? state->buf_size : len;
		luaL_addlstring(&b, state->buf, used_len);
		lbz_buffer_drain(state, used_len);
		len -= used_len;
	}

	/* Pull in chunks until all data read */
	while(len > 0) {
		char *buf = luaL_prepbuffer(&b);
		int nextRead = len > LUAL_BUFFERSIZE ? LUAL_BUFFERSIZE : len;
		int read = BZ2_bzRead(&bzerror, state->bz_stream, buf, nextRead);
		if (read > 0) {
			luaL_addsize(&b, read);
			len -= read;
		}
		if (bzerror != BZ_OK)
			goto handle_error;
	}
	luaL_pushresult(&b);
	return 1;
handle_error:
	if(BZ_STREAM_END == bzerror) {
		/* Push the data read already and mark the stream done */
		luaL_pushresult(&b);
		lbz_perform_close(state, 0);
		return 1;
	} else {
		lua_pushnil(L);
		lua_pushstring(L, BZ2_bzerror(state->bz_stream, &bzerror));
		return 2;
	}
}
Exemplo n.º 13
0
static void
compress_bzip2(int fd_in, int fd_out, struct compress_params *params, const char *desc)
{
	char buffer[DPKG_BUFFER_SIZE];
	char combuf[6];
	int bz_errnum;
	BZFILE *bzfile;

	snprintf(combuf, sizeof(combuf), "w%d", params->level);
	bzfile = BZ2_bzdopen(fd_out, combuf);
	if (bzfile == NULL)
		ohshit(_("%s: error binding output to bzip2 stream"), desc);

	for (;;) {
		int actualread, actualwrite;

		actualread = fd_read(fd_in, buffer, sizeof(buffer));
		if (actualread < 0)
			ohshite(_("%s: internal bzip2 read error"), desc);
		if (actualread == 0) /* EOF. */
			break;

		actualwrite = BZ2_bzwrite(bzfile, buffer, actualread);
		if (actualwrite != actualread) {
			const char *errmsg = BZ2_bzerror(bzfile, &bz_errnum);

			if (bz_errnum == BZ_IO_ERROR)
				errmsg = strerror(errno);
			ohshit(_("%s: internal bzip2 write error: '%s'"), desc,
			       errmsg);
		}
	}

	BZ2_bzWriteClose(&bz_errnum, bzfile, 0, NULL, NULL);
	if (bz_errnum != BZ_OK) {
		const char *errmsg = _("unexpected bzip2 error");

		if (bz_errnum == BZ_IO_ERROR)
			errmsg = strerror(errno);
		ohshit(_("%s: internal bzip2 write error: '%s'"), desc,
		       errmsg);
	}

	/* Because BZ2_bzWriteClose has done a fflush on the file handle,
	 * doing a close on the file descriptor associated with it should
	 * be safe™. */
	if (close(fd_out))
		ohshite(_("%s: internal bzip2 write error"), desc);
}
Exemplo n.º 14
0
static ssize_t bzdRead(FDSTACK_t fps, void * buf, size_t count)
{
    BZFILE *bzfile = fps->fp;
    ssize_t rc = 0;

    if (bzfile)
	rc = BZ2_bzread(bzfile, buf, count);
    if (rc == -1) {
	int zerror = 0;
	if (bzfile) {
	    fps->errcookie = BZ2_bzerror(bzfile, &zerror);
	}
    }
    return rc;
}
Exemplo n.º 15
0
Arquivo: rpmio.c Projeto: akozumpl/rpm
static ssize_t bzdRead(FD_t fd, void * buf, size_t count)
{
    BZFILE *bzfile;
    ssize_t rc = 0;

    bzfile = bzdFileno(fd);
    if (bzfile)
	rc = BZ2_bzread(bzfile, buf, count);
    if (rc == -1) {
	int zerror = 0;
	if (bzfile)
	    fd->errcookie = BZ2_bzerror(bzfile, &zerror);
    }
    return rc;
}
Exemplo n.º 16
0
//xmlInputReadCallback
static int bz2_file_read(struct bz2_file *bzfile, char *buffer, int len)
{
	int bzerror;
	if (bzfile->eof) {
		// If we run bzRead on closed file we will get SEQUENCE_ERROR
		return 0;
	}
	int size = BZ2_bzRead(&bzerror, (bzfile)->file, buffer, len);
	if (bzerror == BZ_STREAM_END) {
		bzfile->eof = true;
	}
	if (bzerror == BZ_OK || bzerror == BZ_STREAM_END)
		return size;
	else {
		oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not read from bZ2FILE: %s",
				BZ2_bzerror(bzfile->file, &bzerror));
		return -1;
	}
}
Exemplo n.º 17
0
static struct bz2_file *bz2_fd_open(int fd)
{
	struct bz2_file *b = NULL;
	FILE* f;
	int bzerror;

	f = fdopen (fd, "r" );
	if (f) {
		b = malloc(sizeof(struct bz2_file));
		b->f = f;
		b->file = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0);
		b->eof = false;
		if (bzerror != BZ_OK) {
			oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not build BZ2FILE from %s: %s",
					BZ2_bzerror(b->file, &bzerror));
			BZ2_bzReadClose(&bzerror, b->file);
			free(b);
			b = NULL;
		}
	}
	return b;
}
Exemplo n.º 18
0
// Close bzip2 file
bzfilebuf*
bzfilebuf::close()
{
  // Fail immediately if no file is open
  if (!this->is_open())
    return NULL;
  // Assume success
  bzfilebuf* retval = this;
  // Attempt to sync and close bzip2 file
  if (this->sync() == -1)
    retval = NULL;
  int errnum = 0;
  BZ2_bzerror(file, &errnum);
  if (errnum > 0)
    retval = NULL;
  BZ2_bzclose(file);
  // File is now gone anyway (postcondition [27.8.1.3.8])
  file = NULL;
  own_fd = false;
  // Destroy internal buffer if it exists
  this->disable_buffer();
  return retval;
}
Exemplo n.º 19
0
int
_nrrdEncodingBzip2_write(FILE *file, const void *_data, size_t elNum,
                         const Nrrd *nrrd, NrrdIoState *nio) {
  char me[]="_nrrdEncodingBzip2_write", err[BIFF_STRLEN];
#if TEEM_BZIP2
  size_t bsize, total_written, block_size;
  int bs, bzerror=BZ_OK;
  char *data;
  BZFILE* bzfout;

  bsize = nrrdElementSize(nrrd)*elNum;

  /* Set compression block size. */
  if (1 <= nio->bzip2BlockSize && nio->bzip2BlockSize <= 9) {
    bs = nio->bzip2BlockSize;
  } else {
    bs = 9;
  }
  /* Open bzfile for writing. Verbosity and work factor are set
     to default values. */
  bzfout = BZ2_bzWriteOpen(&bzerror, file, bs, 0, 0);
  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error opening BZFILE: %s", me, 
            BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    BZ2_bzWriteClose(&bzerror, bzfout, 0, NULL, NULL);
    return 1;
  }

  /* bzip2 can handle data sizes up to INT_MAX, so we can't just 
     pass in the bsize, because it might be too large for an int.
     Therefore it must be read in chunks if the bsize is larger 
     than INT_MAX. */
  if (bsize <= INT_MAX) {
    block_size = bsize;
  } else {
    block_size = INT_MAX;
  }

  /* This counter will help us to make sure that we write as much data
     as we think we should. */
  total_written = 0;
  /* Pointer to the blocks as we write them. */
  data = (char *)_data;
  
  /* Ok, now we can begin writing. */
  bzerror = BZ_OK;
  while (bsize - total_written > block_size) {
    BZ2_bzWrite(&bzerror, bzfout, data, block_size);
    if (BZ_OK != bzerror) break;
    /* Increment the data pointer to the next available spot. */
    data += block_size; 
    total_written += block_size;
  }
  /* write the last (possibly smaller) block when its humungous data;
     write the whole data when its small */
  if (BZ_OK == bzerror) {
    block_size = bsize >= total_written ? bsize - total_written : 0;
    BZ2_bzWrite(&bzerror, bzfout, data, block_size);
    total_written += block_size;
  }

  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error writing to BZFILE: %s",
            me, BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }

  /* Close the BZFILE. */
  BZ2_bzWriteClose(&bzerror, bzfout, 0, NULL, NULL);
  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error closing BZFILE: %s", me,
            BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }
  
  /* Check to see if we got out as much as we thought we should. */
  if (total_written != bsize) {
    sprintf(err, "%s: expected to write " _AIR_SIZE_T_CNV " bytes, but only "
            "wrote " _AIR_SIZE_T_CNV,
            me, bsize, total_written);
    biffAdd(NRRD, err);
    return 1;
  }
  
  return 0;
#else
  AIR_UNUSED(file);
  AIR_UNUSED(_data);
  AIR_UNUSED(elNum);
  AIR_UNUSED(nrrd);
  AIR_UNUSED(nio);
  sprintf(err, "%s: sorry, this nrrd not compiled with bzip2 enabled", me);
  biffAdd(NRRD, err); return 1;
#endif
}
Exemplo n.º 20
0
String BZ2File::errstr() {
  assert(m_bzFile);
  int errnum;
  return BZ2_bzerror(m_bzFile, &errnum);
}
Exemplo n.º 21
0
int64_t BZ2File::errnu() {
  assert(m_bzFile);
  int errnum = 0;
  BZ2_bzerror(m_bzFile, &errnum);
  return errnum;
}
Exemplo n.º 22
0
int
_nrrdEncodingBzip2_read(FILE *file, void *_data, size_t elNum,
                        Nrrd *nrrd, NrrdIoState *nio) {
  char me[]="_nrrdEncodingBzip2_read", err[BIFF_STRLEN];
#if TEEM_BZIP2
  size_t bsize, total_read, block_size;
  int read, bzerror=BZ_OK;
  long int bi;
  char *data;
  BZFILE* bzfin;
  
  bsize = nrrdElementSize(nrrd)*elNum;

  /* Create the BZFILE* for reading in the gzipped data. */
  bzfin = BZ2_bzReadOpen(&bzerror, file, 0, 0, NULL, 0);
  if (bzerror != BZ_OK) {
    /* there was a problem */
    sprintf(err, "%s: error opening BZFILE: %s", me, 
            BZ2_bzerror(bzfin, &bzerror));
    biffAdd(NRRD, err);
    BZ2_bzReadClose(&bzerror, bzfin);
    return 1;
  }

  /* Here is where we do the byte skipping. */
  for(bi=0; bi<nio->byteSkip; bi++) {
    unsigned char b;
    /* Check to see if a single byte was able to be read. */
    read = BZ2_bzRead(&bzerror, bzfin, &b, 1);
    if (read != 1 || bzerror != BZ_OK) {
      sprintf(err, "%s: hit an error skipping byte %ld of %ld: %s",
              me, bi, nio->byteSkip, BZ2_bzerror(bzfin, &bzerror));
      biffAdd(NRRD, err);
      return 1;
    }
  }
  
  /* bzip2 can handle data sizes up to INT_MAX, so we can't just 
     pass in the bsize, because it might be too large for an int.
     Therefore it must be read in chunks if the size is larger 
     than INT_MAX. */
  if (bsize <= INT_MAX) {
    block_size = bsize;
  } else {
    block_size = INT_MAX;
  }

  /* This counter will help us to make sure that we read as much data
     as we think we should. */
  total_read = 0;
  /* Pointer to the blocks as we read them. */
  data = (char *)_data;
  
  /* Ok, now we can begin reading. */
  bzerror = BZ_OK;
  while ((read = BZ2_bzRead(&bzerror, bzfin, data, block_size))
          && (BZ_OK == bzerror || BZ_STREAM_END == bzerror) ) {
    /* Increment the data pointer to the next available spot. */
    data += read;
    total_read += read;
    /* We only want to read as much data as we need, so we need to check
       to make sure that we don't request data that might be there but that
       we don't want.  This will reduce block_size when we get to the last
       block (which may be smaller than block_size).
    */
    if (bsize >= total_read 
        && bsize - total_read < block_size)
      block_size = bsize - total_read;
  }
  
  if (!( BZ_OK == bzerror || BZ_STREAM_END == bzerror )) {
    sprintf(err, "%s: error reading from BZFILE: %s",
            me, BZ2_bzerror(bzfin, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }

  /* Close the BZFILE. */
  BZ2_bzReadClose(&bzerror, bzfin);
  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error closing BZFILE: %s", me,
            BZ2_bzerror(bzfin, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }
  
  /* Check to see if we got out as much as we thought we should. */
  if (total_read != bsize) {
    sprintf(err, "%s: expected " _AIR_SIZE_T_CNV " bytes and received "
            _AIR_SIZE_T_CNV " bytes",
            me, bsize, total_read);
    biffAdd(NRRD, err);
    return 1;
  }
  
  return 0;
#else
  AIR_UNUSED(file);
  AIR_UNUSED(_data);
  AIR_UNUSED(elNum);
  AIR_UNUSED(nrrd);
  AIR_UNUSED(nio);
  sprintf(err, "%s: sorry, this nrrd not compiled with bzip2 enabled", me);
  biffAdd(NRRD, err); return 1;
#endif
}