static void openFile_bz2(struct FileHandle_bz2 *fp, const char *path) { int err; fp->fp = fopen(path, "rb"); if (fp->fp == NULL) openFileError(path); fp->bfp = BZ2_bzReadOpen(&err, fp->fp, 0, 0, NULL, 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; }
NBBZip2::NBBZip2( QString archive, QString file ) { int error = 0; bz2FileName = QString( archive ); if ( not file.isEmpty() ) { if ( QFileInfo( file ).isDir() ) { fileName = QDir( file ).filePath( QString( archive ) ); fileName.chop( 4 ); } else if ( QFileInfo( file ).exists() ) { QFile::rename( file, file + ".old" ); fileName = QString( file ); } else { fileName = QString( file ); } } else { fileName = QString( archive ); fileName.chop( 4 ); } bzFile = fopen( qPrintable( bz2FileName ), "r" ); bz2 = BZ2_bzReadOpen( &error, bzFile, 0, 0, NULL, 0 ); };
void BzipInputStream::dopen(int fd, const char* mode) { assert(0 == m_fp); int err; #ifdef _MSC_VER m_cf = _fdopen(fd, mode); #else m_cf = fdopen(fd, mode); #endif if (0 == m_cf) { std::ostringstream oss; oss << "fd=" << fd << ", mode=" << mode; throw OpenFileException("<fd>", oss.str().c_str()); } m_fp = BZ2_bzReadOpen(&err, m_cf , 0 // verbosity, 0 will not print msg , 0 // small , NULL // unused , 0 // nUnused ); if (0 == m_fp) { std::ostringstream oss; oss << "fd=" << fd << ", mode=" << mode << ", err=" << strbzerr(err); throw OpenFileException("<fd>", oss.str().c_str()); } }
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; }
void bz2streambuf::Open(FILE *f_) { f = f_; if (!write) bzfile = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0); else bzfile = BZ2_bzWriteOpen(&bzerror, f, 5, 0, 0); error = (bzerror != BZ_OK); }
bool Bz2::Open(const std::string &path, Bz2::OpenMode mode) { m_errcode = BZ_OK; if (mode == Bz2::OPEN_WRITE) { m_fileStream = fopen(path.c_str(), "wb"); m_bzFile = BZ2_bzReadOpen(&m_errcode, m_fileStream, 0, 0, nullptr, 0); } else { m_fileStream = fopen(path.c_str(), "rb"); m_bzFile = BZ2_bzReadOpen(&m_errcode, m_fileStream, 0, 0, nullptr, 0); } if (m_errcode != BZ_OK) { this->Close(); return false; } return true; }
struct Input *inputOpen(const char *name) { const char *ext = strrchr(name, '.'); struct Input *ctx = (struct Input *)malloc(sizeof(*ctx)); if (!ctx) return NULL; memset(ctx, 0, sizeof(*ctx)); ctx->name = (char *)malloc(strlen(name) + 1); if (ctx->name) strcpy(ctx->name, name); if (ext && !strcmp(ext, ".gz")) { ctx->fileHandle = (void *)gzopen(name, "rb"); ctx->type = Input::gzipFile; } else if (ext && !strcmp(ext, ".bz2")) { int error = 0; ctx->systemHandle = fopen(name, "rb"); if (!ctx->systemHandle) { fprintf(stderr, "error while opening file %s\n", name); exit(10); } ctx->fileHandle = (void *)BZ2_bzReadOpen(&error, ctx->systemHandle, 0, 0, NULL, 0); ctx->type = Input::bzip2File; } else { int *pfd = (int *)malloc(sizeof(int)); if (pfd) { if (!strcmp(name, "-")) { *pfd = STDIN_FILENO; } else { int flags = O_RDONLY; #ifdef O_LARGEFILE flags |= O_LARGEFILE; #endif *pfd = open(name, flags); if (*pfd < 0) { free(pfd); pfd = NULL; } } } ctx->fileHandle = (void *)pfd; ctx->type = Input::plainFile; } if (!ctx->fileHandle) { fprintf(stderr, "error while opening file %s\n", name); exit(10); } ctx->buf_ptr = 0; ctx->buf_fill = 0; return ctx; }
/** * 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 }
static void open_compressed_read(pcu_file* pf) { int bzerror; int verbosity = 0; int small = 0; void* unused = NULL; int nUnused = 0; pf->bzf = BZ2_bzReadOpen(&bzerror, pf->f, verbosity, small, unused, nUnused); if (bzerror != BZ_OK) reel_fail("BZ2_bzReadOpen failed with code %d", bzerror); }
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); }
//---------------------------------------------------- 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; }
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(); }
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 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; }
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; }
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); }
CoinBzip2FileInput (const std::string &fileName): CoinGetslessFileInput (fileName), f_ (0), bzf_ (0) { int bzError = BZ_OK; readType_="bzlib"; f_ = fopen (fileName.c_str (), "r"); if (f_ != 0) bzf_ = BZ2_bzReadOpen (&bzError, f_, 0, 0, 0, 0); if (f_ == 0 || bzError != BZ_OK || bzf_ == 0) throw CoinError ("Could not open file for reading!", "CoinBzip2FileInput", "CoinBzip2FileInput"); }
void * bopen (const char *filename, const char *mode) { struct fhandle *f = &fhandle; f->file1 = fopen (filename, mode); if (! f->file1) return NULL; f->file2 = BZ2_bzReadOpen (&bzerror, f->file1, 0, 0, NULL, 0); if (bzerror != BZ_OK) { errno = bzerror; bclose (f); return NULL; } return f; }
NBBZip2::NBBZip2( QString archive, NBBZip2::Mode openmode, QString file ) { NBBZip2::Mode mode = openmode; int error = 0; switch( mode ) { case NBBZip2::READ : { bz2FileName = QString( archive ); if ( not file.isEmpty() ) { if ( QFileInfo( file ).isDir() ) { fileName = QDir( file ).filePath( QString( archive ) ); fileName.chop( 4 ); } else if ( QFileInfo( file ).exists() ) { QFile::rename( file, file + ".old" ); fileName = QString( file ); } else { fileName = QString( file ); } } else { fileName = QString( archive ); fileName.chop( 4 ); } bzFile = fopen( qPrintable( bz2FileName ), "r" ); bz2 = BZ2_bzReadOpen( &error, bzFile, 0, 0, NULL, 0 ); break; } case NBBZip2::WRITE : { bz2FileName = QString( archive ); fileName = QString( file ); bzFile = fopen( qPrintable( bz2FileName ), "w" ); bz2 = BZ2_bzWriteOpen( &error, bzFile, 9, 0, 30 ); break; } } };
/* Initialize decompression library (if needed) 0=success, nonzero means error during initialization */ int cm_init(gzFile in, int cm) { infile = in; /* save gzFile for reading/cleanup */ switch (cm) { #ifdef ENABLE_BZ2 case CM_BZ2: bzfile = BZ2_bzReadOpen(&bzerror, in, 0, 0, NULL, 0); return bzerror; #endif #ifdef ENABLE_LZMA case CM_LZMA: return lzma_init(in, &lzmaFile); #endif default: /* CM_NONE, CM_GZ */ return 0; /* success */ } }
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); }
static struct gd_bzdata *_GD_Bzip2DoOpen(int dirfd, struct _gd_raw_file* file) { int fd; struct gd_bzdata *ptr; dtrace("%i, %p", dirfd, file); if ((ptr = (struct gd_bzdata *)malloc(sizeof(struct gd_bzdata))) == NULL) { dreturn("%p", NULL); return NULL; } if ((fd = gd_OpenAt(file->D, dirfd, file->name, O_RDONLY, 0666)) == -1) { free(ptr); dreturn("%p", NULL); return NULL; } if ((ptr->stream = fdopen(fd, "rb")) == NULL) { close(fd); free(ptr); dreturn("%p", NULL); return NULL; } ptr->bzerror = ptr->stream_end = 0; ptr->bzfile = BZ2_bzReadOpen(&ptr->bzerror, ptr->stream, 0, 0, NULL, 0); if (ptr->bzfile == NULL || ptr->bzerror != BZ_OK) { fclose(ptr->stream); free(ptr); dreturn("%p", NULL); return NULL; } ptr->pos = ptr->end = 0; ptr->base = 0; dreturn("%p", ptr); return ptr; }
//---------------------------------------------------------------------------------- // Open the file bool CFastqReader::OpenFiles() { if(in || in_gzip || in_bzip2) return false; // Uncompressed file if(mode == m_plain) { if((in = fopen(input_file_name.c_str(), "rb")) == NULL) return false; } // Gzip-compressed file else if(mode == m_gzip) { if((in_gzip = gzopen(input_file_name.c_str(), "rb")) == NULL) return false; gzbuffer(in_gzip, gzip_buffer_size); } // Bzip2-compressed file else if(mode == m_bzip2) { in = fopen(input_file_name.c_str(), "rb"); if(!in) return false; setvbuf(in, NULL, _IOFBF, bzip2_buffer_size); if((in_bzip2 = BZ2_bzReadOpen(&bzerror, in, 0, 0, NULL, 0)) == NULL) { fclose(in); return false; } } // Reserve via PMM pmm_fastq->reserve(part); part_filled = 0; return true; }
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; }
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; }
/* Binding to libbzip2's BZ2_bzReadOpen method */ int lbz_read_open(lua_State *L) { size_t len; const char *fname = lua_tolstring(L, 1, &len); FILE *f = fopen(fname, "rb"); if (f == NULL) return luaL_error(L, "Failed to fopen %s", fname); int bzerror; lbz_state *state = (lbz_state *) lua_newuserdata(L, sizeof(lbz_state)); state->bz_stream = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0); state->f = f; lbz_buffer_init(state); luaL_getmetatable(L, LBZ_STATE_META); lua_setmetatable(L, -2); if (bzerror != BZ_OK) lua_pushnil(L); return 1; }
DumpReader::DumpReader(const std::string &dump) : m_dump(0), m_bz(0), m_dumpSize(0), m_bzStatus(BZ_OK) { m_dump = fopen(dump.c_str(), "r"); if (!m_dump) { throwErrno(); } int status; m_bz = BZ2_bzReadOpen(&status, m_dump, 0, 0, 0, 0); if (status) { throw std::logic_error("bzip error"); } if (fseek(m_dump, 0, SEEK_END) || -1 == (m_dumpSize = ftell(m_dump)) || fseek(m_dump, 0, SEEK_SET)) { throwErrno(); } }
struct osm_planet *osm_planet_open(const char *filename) { struct osm_planet *osf = calloc(1, sizeof(struct osm_planet)); int bzerror; if( !(osf->fp = fopen(filename, "rb"))) { fprintf(stderr, "osm_planet_open(): Unable to open file <%s>: %s\n", filename, strerror(errno)); goto open_failed; } osf->bzfp = BZ2_bzReadOpen(&bzerror, osf->fp, 1, 0, NULL, 0); if(bzerror != BZ_OK) { fprintf(stderr, "osm_planet_open(): Unable to open compressed file with bzip2: %d\n", bzerror); goto open_failed; } pthread_mutex_init(&osf->drained_mutex, NULL); pthread_mutex_init(&osf->filled_mutex, NULL); pthread_cond_init(&osf->drained_signal, NULL); pthread_cond_init(&osf->filled_signal, NULL); if(pthread_create(&osf->file_read_thread, NULL, start_file_read_thread, osf) != 0) { fprintf(stderr, "osm_planet_open(): Unable to start file read thread\n"); goto open_failed; } return osf; open_failed: free(osf); return NULL; }
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); }