Beispiel #1
0
/**
 * uncompress_bzip2:
 * @name: The name of the file to attempt to decompress
 *
 * Returns: The name of the uncompressed file (in a temporary location) or NULL
 *   free the returned name after use.
 *
 * Also see: http://www.bzip.org/1.0.5/bzip2-manual-1.0.5.html
 */
gchar* uncompress_bzip2 ( gchar *name )
{
#ifdef HAVE_BZLIB_H
	FILE *ff = g_fopen ( name, "r" );
	if ( !ff )
		return NULL;

	int     bzerror;
	BZFILE* bf = BZ2_bzReadOpen ( &bzerror, ff, 0, 0, NULL, 0 ); // This should take care of the bz2 file header
	if ( bzerror != BZ_OK ) {
		BZ2_bzReadClose ( &bzerror, bf );
		// handle error
		return NULL;
	}

	GFileIOStream *gios;
	GError *error = NULL;
	GFile *gf = g_file_new_tmp ( "vik-bz2-tmp.XXXXXX", &gios, &error );
	gchar *tmpname = g_file_get_path (gf);

	GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );

	// Process in arbitary sized chunks
	char buf[4096];
	bzerror = BZ_OK;
	int nBuf;
	// Now process the actual compression data
	while ( bzerror == BZ_OK ) {
		nBuf = BZ2_bzRead ( &bzerror, bf, buf, 4096 );
		if ( bzerror == BZ_OK || bzerror == BZ_STREAM_END) {
			// do something with buf[0 .. nBuf-1]
			if ( g_output_stream_write ( gos, buf, nBuf, NULL, &error ) < 0 ) {
				g_critical ( "Couldn't write bz2 tmp %s file due to %s", tmpname, error->message );
				g_error_free (error);
				BZ2_bzReadClose ( &bzerror, bf );
				goto end;
			}
		}
	}
	if ( bzerror != BZ_STREAM_END ) {
		BZ2_bzReadClose ( &bzerror, bf );
		// handle error...
		goto end;
	} else {
		BZ2_bzReadClose ( &bzerror, bf );
		g_output_stream_close ( gos, NULL, &error );
	}

 end:
	return tmpname;
#else
	return NULL;
#endif
}
Beispiel #2
0
//----------------------------------------------------
int zu_seek(ZUFILE *f, long offset, int whence)
{
    int res = 0;
    int bzerror=BZ_OK;
    if (whence == SEEK_END) {
        return -1;              // TODO
    }
    
    switch(f->type) {         //SEEK_SET, SEEK_CUR
        case ZU_COMPRESS_NONE :
            res = fseek((FILE*)(f->zfile), offset, whence);
            f->pos = ftell((FILE*)(f->zfile));
            break;
        case ZU_COMPRESS_GZIP :
            if (whence == SEEK_SET) {
                res = gzseek((gzFile)(f->zfile), offset, whence);
            }
            else {     // !!! BUG with SEEK_CUR in ZLIB !!!
                int p1 = gztell((gzFile)(f->zfile));
                res = gzseek((gzFile)(f->zfile), p1+offset, SEEK_SET);
            }
            f->pos = gztell((gzFile)(f->zfile));
            if (res >= 0)
                res = 0;
            break;
#ifndef __ANDROID__
        case ZU_COMPRESS_BZIP :
            if (whence==SEEK_SET  &&  offset >= f->pos) {
                res = zu_bzSeekForward(f, offset-f->pos);
            }
            else if (whence==SEEK_CUR) {
                res = zu_bzSeekForward(f, offset);
            }
            else {    // BAD : reopen file
                BZ2_bzReadClose (&bzerror,(BZFILE*)(f->zfile));
                bzerror=BZ_OK;
                rewind(f->faux);
                f->pos = 0;
                f->zfile = (void *) BZ2_bzReadOpen(&bzerror,f->faux,0,0,NULL,0);
                if (bzerror != BZ_OK) {
                    BZ2_bzReadClose (&bzerror,(BZFILE*)(f->zfile));
                    fclose(f->faux);
                    f->zfile = NULL;
                    f->ok = 0;
                }
                res = zu_bzSeekForward(f, offset);
            }
            break;
#endif
    }
    return res;
}
Beispiel #3
0
VISIBILITY_ENABLE
#include "ttyrec.h"
VISIBILITY_DISABLE
#include "stream.h"
#include "error.h"
#include "gettext.h"

#define ERRORMSG(x) do if (write(fd,(x),strlen(x))) {} while(0)

#define BUFFER_SIZE 32768

#if (defined HAVE_LIBBZ2) || (defined SHIPPED_LIBBZ2)
static void read_bz2(int f, int fd, const char *arg)
{
    BZFILE* b;
    int     nBuf;
    char    buf[BUFFER_SIZE];
    int     bzerror;

    b = BZ2_bzReadOpen(&bzerror, fdopen(f,"rb"), 0, 0, NULL, 0);
    if (bzerror != BZ_OK)
    {
        BZ2_bzReadClose(&bzerror, b);
        // error
        ERRORMSG(_("Invalid/corrupt .bz2 file.\n"));
        close(fd);
        return;
    }

    bzerror = BZ_OK;
    while (bzerror == BZ_OK)
    {
        nBuf = BZ2_bzRead(&bzerror, b, buf, BUFFER_SIZE);
        if (write(fd, buf, nBuf)!=nBuf)
        {
            BZ2_bzReadClose(&bzerror, b);
            close(fd);
            return;
        }
    }
    if (bzerror != BZ_STREAM_END)
    {
        BZ2_bzReadClose(&bzerror, b);
        // error
        ERRORMSG("\033[0m");
        ERRORMSG(_("bzip2: Error during decompression.\n"));
    }
    else
        BZ2_bzReadClose(&bzerror, b);
    close(fd);
}
bool QTarBZip2Decompressor::decompressToFiles()
{
	QString root = mPathSrc + QDir::separator()  + mArchiveFileName;
  const std::string tbz2Filename = root.toStdString();
  const std::string extractTo = mPathDest.toStdString();

  FILE *tbz2File = fopen(tbz2Filename.c_str(), "rb");
  int bzError;

 const int BUF_SIZE = 10000;
// char* buf = new char[BUF_SIZE];
 boost::scoped_array< char > buf ( new char[BUF_SIZE] );
  BZFILE *pBz = BZ2_bzReadOpen(&bzError, tbz2File, 0, 0, 0, 0);

  if ( bzError != BZ_OK ) {
    qDebug() << "bzError != BZ_OK";
    BZ2_bzReadClose( &bzError, pBz );
    //delete[] buf;
    return false;
  }

  const QString tarFilename = root.section( ".bz2", 0, 0 );
  QFile tbz2FileTmp( tarFilename );
  if( !tbz2FileTmp.open( QIODevice::WriteOnly ) ) {
    BZ2_bzReadClose( &bzError, pBz );
    //delete[] buf;
    return false;
  }
  
  bzError = BZ_OK;
  while ( bzError == BZ_OK ) {
    ssize_t bytesRead = BZ2_bzRead ( &bzError, pBz, buf.get(), BUF_SIZE );
    if ( bzError == BZ_OK )
//      fwrite (buf , 1 , bytesRead , tbz2FileTmp );
    tbz2FileTmp.write( buf.get(), bytesRead );
  }
  BZ2_bzReadClose ( &bzError, pBz );

  const std::string tarFilename2 = tarFilename.toStdString();
 TAR *pTar;
 tar_open(&pTar, const_cast< char* > ( tarFilename2.c_str() ), NULL, O_RDONLY, 0644, TAR_GNU);
 tar_extract_all( pTar, const_cast< char* > ( extractTo.c_str()  ) );
 close(tar_fd(pTar));
 QFile::remove( tarFilename );
 //delete[] buf;

return true;
}
Beispiel #5
0
bool MANFrame::Decompress(const wxString& filename, const wxString& tmpfile)
{
    // open file
    FILE* f = fopen(filename.mb_str(), "rb");
    if (!f)
    {
        return false;
    }

    // open BZIP2 stream
    int bzerror;
    BZFILE* bz = BZ2_bzReadOpen(&bzerror, f, 0, 0, 0L, 0);
    if (!bz || bzerror != BZ_OK)
    {
        fclose(f);
        return false;
    }

    // open output file
    FILE* fo = fopen(tmpfile.mb_str(), "wb");
    if (!fo)
    {
        fclose(f);
        return false;
    }

    // read stream writing to uncompressed file
    char buffer[2048];
    while (bzerror != BZ_STREAM_END)
    {
        int read_bytes = BZ2_bzRead(&bzerror, bz, buffer, 2048);
        if (bzerror != BZ_OK && bzerror != BZ_STREAM_END)
        {
            BZ2_bzReadClose(&bzerror, bz);
            fclose(fo);
            fclose(f);
            return false;
        }
        fwrite(buffer, read_bytes, 1, fo);
    }

    BZ2_bzReadClose(&bzerror, bz);


    fclose(fo);
    fclose(f);
    return true;
}
Beispiel #6
0
/* tries to re-open the bz stream at the next stream start.
   returns 0 on success, -1 on failure. */
int bzReOpen(struct Input *ctx, int *error) {
    /* for copying out the last unused part of the block which
       has an EOS token in it. needed for re-initialising the
       next stream. */
  unsigned char unused[BZ_MAX_UNUSED];
  void *unused_tmp_ptr = NULL;
  int nUnused, i;

  BZ2_bzReadGetUnused(error, (BZFILE *)(ctx->fileHandle), &unused_tmp_ptr, &nUnused);
  if (*error != BZ_OK) return -1;

  /* when bzReadClose is called the unused buffer is deallocated,
     so it needs to be copied somewhere safe first. */
  for (i = 0; i < nUnused; ++i)
    unused[i] = ((unsigned char *)unused_tmp_ptr)[i];

  BZ2_bzReadClose(error, (BZFILE *)(ctx->fileHandle));
  if (*error != BZ_OK) return -1;

  /* reassign the file handle */
  ctx->fileHandle = BZ2_bzReadOpen(error, ctx->systemHandle, 0, 0, unused, nUnused);
  if (ctx->fileHandle == NULL || *error != BZ_OK) return -1;

  return 0;
}
Beispiel #7
0
int osm_planet_close(struct osm_planet *osf)
{
    int bzerror;

    /* Signal to file read thread and wait for it to exit */
    osf->exit_now = 1;
    pthread_join(osf->file_read_thread, NULL);
    pthread_detach(osf->file_read_thread);

    pthread_mutex_destroy(&osf->drained_mutex);
    pthread_mutex_destroy(&osf->filled_mutex);
    pthread_cond_destroy(&osf->drained_signal);
    pthread_cond_destroy(&osf->filled_signal);

    BZ2_bzReadClose(&bzerror, osf->bzfp);
    if(bzerror != BZ_OK)
    {
        fprintf(stderr, "osm_planet_close(): Error closing compressed file with bzip2: %d\n", bzerror);
        return 1;
    }

    if(fclose(osf->fp) != 0)
    {
        fprintf(stderr, "osm_planet_close(): Error closing file\n");
        return 1;
    }

    free(osf->recvbuff);
    free(osf);
    return 0;
}
Beispiel #8
0
/* properly cleanup any resources decompression library allocated 
 */
void cm_cleanup(int cm)
{
  switch (cm)
  {
#ifdef ENABLE_BZ2
    case CM_BZ2:
      BZ2_bzReadClose(&bzerror, bzfile);
      break;
#endif
#ifdef ENABLE_LZMA
    case CM_LZMA:
      lzma_cleanup(lzmaFile);
      break;
#endif
    default: /* CM_NONE, CM_GZ */
      break;
  }

  /* close the input stream */
  if (gzclose(infile) != Z_OK)
  {
    PrintMessage(_T("failed gzclose"));
    /* return -1; */
  }
}
Beispiel #9
0
bool NBBZip2::extract() {

	int error;

	// Reading from the bz2 file opened
	std::ofstream ofile( qPrintable( fileName ), std::ofstream::binary );

	while ( true ) {
		char buffer[ MAX_READ_SIZE ] = { "\x00" };
		int charsRead = BZ2_bzRead( &error, bz2, buffer, MAX_READ_SIZE );
		ofile.write( buffer, charsRead );

		if ( error == BZ_OK )
			continue;

		else
			break;
	}

	if ( error != BZ_STREAM_END )
		return false;

	// Close the file
	BZ2_bzReadClose( &error, bz2 );

	if ( error != BZ_OK )
		return false;

	fclose( bzFile );

	return true;
};
Beispiel #10
0
static void close_compressed_read(pcu_file* pf)
{
  int bzerror;
  BZ2_bzReadClose(&bzerror, pf->bzf);
  if (bzerror != BZ_OK)
    reel_fail("BZ2_readClose failed with code %d", bzerror);
}
Beispiel #11
0
//----------------------------------------------------
int zu_close(ZUFILE *f)
{
    int bzerror=BZ_OK;
    if (f) {
        f->ok = 0;
        f->pos = 0;
        free(f->fname);
        if (f->zfile!=NULL) {
            switch(f->type) {
                case ZU_COMPRESS_NONE :
                    fclose((FILE*)(f->zfile));
                    break;
                case ZU_COMPRESS_GZIP :
                    gzclose((gzFile)(f->zfile));
                    break;
                case ZU_COMPRESS_BZIP :
                    BZ2_bzReadClose (&bzerror,(BZFILE*)(f->zfile));
                    if (f->faux) {
                        fclose(f->faux);
                    }
                    break;
            }
        }
    }
    return 0;
}
void BZ2Stream::stopRead() {
    BZ2_bzReadClose(&bzerror_, bzfile_);

    switch (bzerror_) {
        case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR");
    }
}
Beispiel #13
0
/*
 * POST: Closes the gt_input_file
 * RETURN VALUE: Returns zero on success and error code
 */
gt_status gt_input_file_close(gt_input_file* const input_file) {
    GT_INPUT_FILE_CHECK(input_file);
    gt_status status = GT_INPUT_FILE_OK;
#ifdef HAVE_BZLIB
    int bzerr;
#endif
    switch (input_file->file_type) {
    case REGULAR_FILE:
        gt_free(input_file->file_buffer);
        if (fclose(input_file->file)) status = GT_INPUT_FILE_CLOSE_ERR;
        break;
    case GZIPPED_FILE:
        gt_free(input_file->file_buffer);
#ifdef HAVE_ZLIB
        if (gzclose((gzFile)input_file->file)) status = GT_INPUT_FILE_CLOSE_ERR;
#endif
        break;
    case BZIPPED_FILE:
        gt_free(input_file->file_buffer);
#ifdef HAVE_BZLIB
        BZ2_bzReadClose(&bzerr,input_file->file);
        if (bzerr!=BZ_OK) status = GT_INPUT_FILE_CLOSE_ERR;
#endif
        break;
    case MAPPED_FILE:
        gt_cond_error(munmap(input_file->file,input_file->file_size)==-1,SYS_UNMAP);
        if (close(input_file->fildes)) status = GT_INPUT_FILE_CLOSE_ERR;
        break;
    case STREAM:
        gt_free(input_file->file_buffer);
        break;
    }
    gt_free(input_file);
    return status;
}
Beispiel #14
0
// xmlInputCloseCallback
static int bz2_file_close(void *bzfile)
{
	int bzerror;
	BZ2_bzReadClose(&bzerror, ((struct bz2_file *)bzfile)->file);
	fclose(((struct bz2_file *)bzfile)->f);
	free(bzfile);
	return bzerror == BZ_OK ? 0 : -1;
}
Beispiel #15
0
int cfr_close(CFRFILE *stream) {
  /**************************/
  // Analog to 'fclose'.
  
  int retval = -1;
  if (stream == NULL) return(-1);

  if (stream->closed) { 
      errno = EBADF;
        return(EOF);
  }      
  switch (stream->format) {
  case 1:  // uncompressed
    { 
      retval = fclose((FILE *)(stream->data1));
      stream->error1 = retval;
      return(retval);
    }
    break;
#ifndef DONT_HAVE_BZ2
  case 2: // bzip2
    { 
      BZFILE * bzin; 
      int bzerror;
      
      bzerror = BZ_OK;
      bzin = (BZFILE *) (stream->data2);
          
      BZ2_bzReadClose( &bzerror, bzin);
      if (bzerror != BZ_OK) {
        stream->error2 = bzerror;
        stream->error1 = fclose((FILE *)(stream->data1));
        return(-1);
      }
      retval = fclose((FILE *)(stream->data1));
      stream->error1 = retval;
      return(retval);
    }
    break;
#endif
#ifndef DONT_HAVE_GZ
  case 3:  // gzip
    { 
	if(stream->data2!=NULL) {
		retval=gzclose(stream->data2);
	}
      stream->error2 = retval;
      return(retval);
    }
    break;
#endif
    default:  // this is an internal error, no diag yet.
      fprintf(stderr,"illegal format '%d' in cfr_close!\n",stream->format);
      exit(1);
  }
  free(stream);
  return(retval);
}
Beispiel #16
0
size_t AFILE_afread(void *ptr, size_t size, size_t count, AFILE *afp) 
{
#ifndef DISABLE_BZLIB 
	int32_t nbuf=0, i;
	void *unused_tmp_void=NULL;
	char *unused_tmp=NULL;
#endif

	switch(afp->c) {
		case AFILE_NO_COMPRESSION:
			return fread(ptr, size, count, afp->fp);
#ifndef DISABLE_BZLIB 
		case AFILE_BZ2_COMPRESSION:
			while(0 == nbuf && 
					!(BZ_STREAM_END == afp->bzerror && 0 == afp->n_unused && feof(afp->fp))) {
				nbuf = BZ2_bzRead(&afp->bzerror, afp->bz2, ptr, size*count);
				if(BZ_OK == afp->bzerror) {
					// return # of bytes
					return nbuf;
				}
				else if(BZ_STREAM_END == afp->bzerror) {
					// Get unused
					BZ2_bzReadGetUnused(&afp->bzerror, afp->bz2, &unused_tmp_void, &afp->n_unused);
					if(BZ_OK != afp->bzerror) AFILE_print_error("Could not BZ2_bzReadGetUnused"); 
					unused_tmp = (char*)unused_tmp_void;
					for(i=0;i<afp->n_unused;i++) {
						afp->unused[i] = unused_tmp[i];
					}
					// Close
					BZ2_bzReadClose(&afp->bzerror, afp->bz2);
					if(BZ_OK != afp->bzerror) AFILE_print_error("Could not BZ2_bzReadClose"); 
					afp->bzerror = BZ_STREAM_END; // set to the stream end for next call to this function
					// Open again if necessary
					if(0 == afp->n_unused && feof(afp->fp)) {
						return nbuf;
					}
					else {
						afp->bz2 = BZ2_bzReadOpen(&afp->bzerror, afp->fp, 0, 0, afp->unused, afp->n_unused);
						if(NULL == afp->bz2) AFILE_print_error("Could not open file");
					}
				}
				else {
					fprintf(stderr, "nbuf = %d\n", nbuf);
					fprintf(stderr, "afp->bzerror = %d\n", afp->bzerror);
					AFILE_print_error("Could not read");
				}
			}
			return nbuf;
#endif
		case AFILE_GZ_COMPRESSION:
			return gzread(afp->gz, ptr, size*count);
			break;
		default:
			AFILE_print_error("Could not recognize compresssion\n");
			break;
	}
	return 0;
}
Beispiel #17
0
  virtual ~CoinBzip2FileInput ()
  {
    int bzError = BZ_OK;
    if (bzf_ != 0)
      BZ2_bzReadClose (&bzError, bzf_);

    if (f_ != 0)
      fclose (f_);
  }
Beispiel #18
0
int
bclose (void *file)
{
  struct fhandle *f = (struct fhandle *) file;
  BZ2_bzReadClose (&bzerror, (BZFILE *) f->file2);
  fclose (f->file1);
  f->file2 = NULL;
  f->file1 = NULL;
  return 0;
}
Beispiel #19
0
void lbz_perform_close(lbz_state *state, int keep_extra_buf) {
	int bzerror;
	if(!keep_extra_buf)
		lbz_buffer_free(state);
	if(!state->bz_stream)
		return;
	BZ2_bzReadClose(&bzerror, state->bz_stream);
	fclose(state->f);
	state->bz_stream = NULL;
	state->f = NULL;
}
Beispiel #20
0
off64_t _GD_Bzip2Seek(struct _gd_raw_file* file, off64_t count,
    gd_type_t data_type, unsigned int mode __gd_unused)
{
  struct gd_bzdata *ptr = (struct gd_bzdata *)file->edata;

  dtrace("%p, %lli, 0x%X, <unused>", file, (long long)count, data_type);

  count *= GD_SIZE(data_type);

  if (ptr->base > count) {
    /* a backwards seek -- reopen the file */
    ptr->bzerror = 0;
    BZ2_bzReadClose(&ptr->bzerror, ptr->bzfile);
    ptr->bzfile = BZ2_bzReadOpen(&ptr->bzerror, ptr->stream, 0, 0, NULL, 0);

    if (ptr->bzfile == NULL || ptr->bzerror != BZ_OK) {
      fclose(ptr->stream);
      dreturn("%i", -1);
      return -1;
    }
    ptr->pos = ptr->end = 0;
    ptr->base = ptr->stream_end = 0;
  }

  /* seek forward the slow way */
  while (ptr->base + ptr->end < count) {
    int n;

    ptr->bzerror = 0;
    n = BZ2_bzRead(&ptr->bzerror, ptr->bzfile, ptr->data,
        GD_BZIP_BUFFER_SIZE);

    if (ptr->bzerror == BZ_OK || ptr->bzerror == BZ_STREAM_END) {
      ptr->base += ptr->end;
      ptr->end = n;
    } else {
      dreturn("%i", -1);
      return -1;
    }

    /* eof */
    if (ptr->bzerror != BZ_OK) {
      ptr->stream_end = 1;
      break;
    }
  }

  ptr->pos = (ptr->bzerror == BZ_STREAM_END && count >= ptr->base + ptr->end) ?
    ptr->end : count - ptr->base;

  dreturn("%lli", (long long)((ptr->base + ptr->pos) / GD_SIZE(data_type)));
  return (ptr->base + ptr->pos) / GD_SIZE(data_type);
}
void BZ2Stream::startRead() {
    bzfile_ = BZ2_bzReadOpen(&bzerror_, getFilePointer(), verbosity_, 0, getUnused(), getUnusedLength());

    switch (bzerror_) {
        case BZ_OK: break;
        default: {
            BZ2_bzReadClose(&bzerror_, bzfile_);
            throw BagException("Error opening file for reading compressed stream");
        }
    }

    clearUnused();
}
Beispiel #22
0
int fsReadCompressed(FILESYSTEM_FILE *f, void *data, int len) {
	BZFILE *bzf;
	int err, back;
	void *buf;

	if (!f)
		return -1;
	if (!(bzf = BZ2_bzReadOpen(&err, f->fp, 0, 0, NULL, 0)))
		return -1;
	BZ2_bzRead(&err, bzf, data, len);
	if (err != BZ_STREAM_END) {
		BZ2_bzReadClose(&err, bzf);
		return -1;
	}

	BZ2_bzReadGetUnused(&err, bzf, &buf, &back);
	fseek(f->fp, back * -1, SEEK_CUR);
	f->pos = ftell(f->fp) - f->offset;
	BZ2_bzReadClose(&err, bzf);
	
	return 0;
}
void DumpReader::close() {
  if (m_bz) {
    MonitorBlock sync(this);

    int ignore;
      
    if (m_bz) BZ2_bzReadClose(&ignore, m_bz);
      
    m_bz = 0;
      
    if (m_dump) fclose(m_dump);
    m_dump = 0;
    sync.notifyAll();
  }
}
Beispiel #24
0
static void read_bz2(int f, int fd, char *arg)
{
    BZFILE* b;
    int     nBuf;
    char    buf[BUFFER_SIZE];
    int     bzerror;

    b = BZ2_bzReadOpen ( &bzerror, fdopen(f,"rb"), 0, 0, NULL, 0 );
    if (bzerror != BZ_OK) {
       BZ2_bzReadClose ( &bzerror, b );
       // error
       ERRORMSG(_("Invalid/corrupt .bz2 file.\n"));
       close(fd);
       return;
    }

    bzerror = BZ_OK;
    while (bzerror == BZ_OK) {
       nBuf = BZ2_bzRead ( &bzerror, b, buf, BUFFER_SIZE);
       if (write(fd, buf, nBuf)!=nBuf)
       {
           BZ2_bzReadClose ( &bzerror, b );
           close(fd);
           return;
       }
    }
    if (bzerror != BZ_STREAM_END) {
       BZ2_bzReadClose ( &bzerror, b );
       // error
       ERRORMSG("\033[0m");
       ERRORMSG(_("bzip2: Error during decompression.\n"));
    } else {
       BZ2_bzReadClose ( &bzerror, b );
    }
    close(fd);
}
Beispiel #25
0
extern char get_header_tar_bz2(archive_handle_t *archive_handle)
{
	BZ2_bzReadOpen(archive_handle->src_fd, NULL, 0);

	archive_handle->read = read_bz2;
	archive_handle->seek = seek_by_char;

	archive_handle->offset = 0;
	while (get_header_tar(archive_handle) == EXIT_SUCCESS);

	/* Cleanup */
	BZ2_bzReadClose();
	
	/* Can only do one tar.bz2 per archive */
	return(EXIT_FAILURE);
}
Beispiel #26
0
void BzipInputStream::close()
{
	assert(m_fp);
	assert(m_cf);
	int err;
	BZ2_bzReadClose(&err, m_fp);
	::fclose(m_cf);
	m_fp = 0;
	m_cf = 0;
	if (BZ_OK != err)
	{
		std::ostringstream oss;
		oss << "BZ2_bzReadClose err=" << strbzerr(err)
			<< ", in " << BOOST_CURRENT_FUNCTION;
		throw OpenFileException("<fd>", oss.str().c_str());
	}
}
Beispiel #27
0
  void bz2streambuf::Close()
  {
    if (bzfile)
    {
      if (sync() != 0)
        error = true;

      if (!write)
        BZ2_bzReadClose(&bzerror, bzfile);
      else
      {
        unsigned int nbytes_in, nbytes_out;
        BZ2_bzWriteClose(&bzerror, bzfile, error ? 1 : 0,
                         &nbytes_in, &nbytes_out);
      }
      bzfile = NULL;
    }
  }
Beispiel #28
0
void ExtractFile(int id, char *fs)
{
	FILE *out; uint i, s; char fn[256] = "bcpxtract\\\0";
	char *min, *mout, *ws; uint os, sws; int be; BZFILE *bz;
	if(!file) return;
	strcat_s(fn, 255, fs);
	out = fopen(fn, "wb");
	if(!out) return;
	fseek(file, fent[id].offset, SEEK_SET);
	s = fent[id].endos - fent[id].offset;
	switch(fent[id].form)
	{
		case 1: // Uncompressed
			for(i = 0; i < s; i++)
				fputc(fgetc(file), out);
			break;
		case 2: // Zero-sized file
			break;
		case 3: // bzip2-compressed file
			mout = malloc(fent[id].size);
			bz = BZ2_bzReadOpen(&be, file, 0, 0, NULL, 0);
			if(be != BZ_OK) fErr(-822, "Failed to initialize bzip2 decompression.");
			BZ2_bzRead(&be, bz, mout, fent[id].size);
			if((be != BZ_OK) && (be != BZ_STREAM_END)) fErr(-872, "Failed to decompress bzip2 file.");
			BZ2_bzReadClose(&be, bz);
			fwrite(mout, fent[id].size, 1, out);
			free(mout);
			break;
		case 4: // LZRW3-compressed file
			ws = (char*)malloc(MEM_REQ);
			min = (char*)malloc(s);
			mout = (char*)malloc(os = fent[id].size);
			fread(min, s, 1, file);
			compress(COMPRESS_ACTION_DECOMPRESS, ws, min, s, mout, &os);
			if(fent[id].size != os)
				MessageBox(hwnd, "The decompressed sizes in the file table and from the decompression algorithm are different.", title, 48);
			fwrite(mout, fent[id].size, 1, out);
			free(ws); free(min); free(mout);
			break;
	}
	fclose(out);
}
Beispiel #29
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;
}
Beispiel #30
0
bool Bz2::Close()
{
    m_errcode = BZ_OK;
    if (m_bzFile) {
        if (m_mode == Bz2::OPEN_WRITE) {
            BZ2_bzWriteClose(&m_errcode, m_bzFile, 0, nullptr, nullptr);
        }
        else {
            BZ2_bzReadClose(&m_errcode, m_bzFile);
        }
        m_bzFile = nullptr;
    }
    if (m_fileStream) {
        fclose(m_fileStream);
        m_fileStream = nullptr;
    }
    
    if (m_errcode != BZ_OK) {
        return false;
    }
    return true;
}