int bz2streambuf::underflow() { Assert(!write); int charsread = BZ2_bzRead(&bzerror, bzfile, (void *)buf, bufsize * sizeof(char)) / sizeof(char); if ((bzerror == BZ_OK) || (bzerror == BZ_STREAM_END)) { if (charsread == 0) { setg(0, 0, 0); return EOF; } else { setg(buf, buf, buf + charsread); return *buf & 0xFF; } } else { error = true; setg(0, 0, 0); return EOF; } }
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; };
void BZ2Stream::read(void* ptr, size_t size) { BZ2_bzRead(&bzerror_, bzfile_, ptr, size); advanceOffset(size); switch (bzerror_) { case BZ_OK: return; case BZ_STREAM_END: if (getUnused() || getUnusedLength() > 0) ROS_ERROR("unused data already available"); else { char* unused; int nUnused; BZ2_bzReadGetUnused(&bzerror_, bzfile_, (void**) &unused, &nUnused); setUnused(unused); setUnusedLength(nUnused); } return; case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR: error reading from compressed stream"); break; case BZ_UNEXPECTED_EOF: throw BagIOException("BZ_UNEXPECTED_EOF: compressed stream ended before logical end-of-stream detected"); break; case BZ_DATA_ERROR: throw BagIOException("BZ_DATA_ERROR: data integrity error detected in compressed stream"); break; case BZ_DATA_ERROR_MAGIC: throw BagIOException("BZ_DATA_ERROR_MAGIC: stream does not begin with requisite header bytes"); break; case BZ_MEM_ERROR: throw BagIOException("BZ_MEM_ERROR: insufficient memory available"); break; } }
//---------------------------------------------------- char *zu_gets(ZUFILE *f, char *buf, int len) { int nb = 0; int bzerror=BZ_OK; char *ret = NULL; switch(f->type) { case ZU_COMPRESS_NONE : if((ret = fgets(buf, len, (FILE*)(f->zfile)))) nb = strlen(buf); break; case ZU_COMPRESS_GZIP : if((ret = gzgets((gzFile)(f->zfile), buf, len))) nb = strlen(buf); break; case ZU_COMPRESS_BZIP : nb = BZ2_bzRead(&bzerror,(BZFILE*)(f->zfile), buf, len-1); for(int i=0; i<nb; i++) if(buf[i] == '\n') { int seek = f->pos; f->pos += nb; buf[i+1] = '\0'; return zu_seek(f, seek + i + 1, SEEK_SET) == -1 ? NULL : buf; } if(nb > 0) { buf[nb] = '\0'; ret = buf; } } f->pos += nb; return ret; }
/* 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; }
int readFile_bz2(void *buffer, int length, struct FileHandle_bz2 *fp) { int err; int rlen = BZ2_bzRead(&err, fp->bfp, buffer, length); return rlen; }
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; }
size_t Bz2::Read(void *buffer, size_t size, size_t count) { m_errcode = BZ_OK; BZ2_bzRead(&m_errcode, m_bzFile, (void*)buffer, (int)(size * count)); if (m_errcode != BZ_OK) { return 0; } return 0; }
/* Compatibility function to make BZ2_bzRead look like gzread. */ int bzread(BZFILE * file, void *buf, int len) { int bzerror = BZ_OK; int retval = BZ2_bzRead(&bzerror, file, buf, len); if (bzerror == BZ_OK || bzerror == BZ_STREAM_END) { return retval; } else { fprintf(stderr, "bzip error %d\n", bzerror); return -1; } }
virtual int readRaw (void *buffer, int size) { int bzError = BZ_OK; int count = BZ2_bzRead (&bzError, bzf_, buffer, size); if (bzError == BZ_OK || bzError == BZ_STREAM_END) return count; // Error? return 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 }
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); }
//---------------------------------------------------------------------------------- // Read a part of the file in multi line fasta format bool CFastqReader::GetPartFromMultilneFasta(uchar *&_part, uint64 &_size) { uint64 readed = 0; if(!containsNextChromosome) { if(IsEof()) return false; } if(mode == m_plain) readed = fread(part+part_filled, 1, part_size-part_filled, in); else if(mode == m_gzip) readed = gzread(in_gzip, part+part_filled, (int) (part_size-part_filled)); else if(mode == m_bzip2) readed = BZ2_bzRead(&bzerror, in_bzip2, part+part_filled, (int) (part_size-part_filled)); int64 total_filled = part_filled + readed; int64 last_header_pos = 0; int64 pos = 0; for(int64 i = 0 ; i < total_filled ;++i )//find last '>' and remove EOLs { if(part[i] == '>') { int64 tmp = i; SkipNextEOL(part,i,total_filled); copy(part+tmp, part+i, part+pos); last_header_pos = pos; pos += i - tmp; } if(part[i] != '\n' && part[i] != '\r') { part[pos++] = part[i]; } } _part = part; if(last_header_pos == 0)//data in block belong to one seq { part_filled = kmer_len - 1; _size = pos; pmm_fastq->reserve(part); copy(_part+_size-part_filled, _part+_size, part); containsNextChromosome = false; } else//next seq starts at last_header_pos { _size = last_header_pos; part_filled = pos - last_header_pos; pmm_fastq->reserve(part); copy(_part + last_header_pos, _part + pos, part); containsNextChromosome = true; } return true; }
static void compressed_read(pcu_file* pf, void* data, size_t size) { int bzerror; int len; int rv; assert(size < INT_MAX); len = size; rv = BZ2_bzRead(&bzerror, pf->bzf, data, len); if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) reel_fail("BZ2_bzRead failed with code %d", bzerror); assert(rv == len); }
//----------------------------------------------------------------- int zu_bzSeekForward(ZUFILE *f, unsigned long nbytes_) // for internal use { unsigned long nbytes = nbytes_; char buf[ZU_BUFREADSIZE]; unsigned long nbread = 0; int nb; int bzerror=BZ_OK; while (bzerror==BZ_OK && nbytes>=ZU_BUFREADSIZE) { nb = BZ2_bzRead(&bzerror,(BZFILE*)(f->zfile), buf, ZU_BUFREADSIZE); nbytes -= nb; nbread += nb; } if (bzerror==BZ_OK && nbytes>0) { nb = BZ2_bzRead(&bzerror,(BZFILE*)(f->zfile), buf, nbytes); nbread += nb; } f->pos += nbread; return nbread==nbytes_ ? 0 : -1; }
int SegmentNoaa::ReadNbrOfLines() { FILE* f; BZFILE* b; int nBuf; char buf[ 11090 * 2 ]; int bzerror; quint16 val1_ch[5], val2_ch[5],tot_ch[5]; QByteArray picture_line; int heightinsegment = 0; f = fopen ( this->fileInfo.absoluteFilePath().toLatin1(), "rb" ); if ( !f ) { qDebug() << QString("file %1 not found ! ").arg(this->fileInfo.absoluteFilePath()); return 0; } if((b = BZ2_bzopen(this->fileInfo.absoluteFilePath().toLatin1(),"rb"))==NULL) { qDebug() << "error in BZ2_bzopen"; } bzerror = BZ_OK; while ( bzerror == BZ_OK ) { nBuf = BZ2_bzRead ( &bzerror, b, buf, 11090 * 2 ); if ( bzerror == BZ_OK || bzerror == BZ_STREAM_END) { QByteArray data = QByteArray::fromRawData(buf, sizeof(buf)); //picture_line = data.mid( 1500, 20480 ); if ((data.at(0) & 0xFF) == 0x02 && (data.at(1) & 0xFF) == 0x84 && (data.at(2) & 0xFF) == 0x01 && (data.at(3) & 0xFF) == 0x6f && (data.at(4) & 0xFF) == 0x03 && (data.at(5) & 0xFF) == 0x5c && (data.at(6) & 0xFF) == 0x01 && (data.at(7) & 0xFF) == 0x9d && (data.at(8) & 0xFF) == 0x02 && (data.at(9) & 0xFF) == 0x0f && (data.at(10) & 0xFF) == 0x00 && (data.at(11) & 0xFF) == 0x95) { heightinsegment++; } } } BZ2_bzclose ( b ); fclose(f); return heightinsegment; }
int read_line_bz2(BZFILE* fp, char *line){ char c; int bzerror; int i=0; do{ BZ2_bzRead(&bzerror, fp, &c, sizeof(char)); if(bzerror ==BZ_OK && c != '\n') line[i++]=c; }while(bzerror == BZ_OK && c != '\n'); line[i]='\0'; return bzerror; }
static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length) { int n; int bz2err; BZFILE* bz2; bz2 = (BZFILE*)stream->opaque; n = BZ2_bzRead(&bz2err, bz2, buffer, length); if (n != length) return -1; return 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); }
/* 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; } }
int DumpReader::read(char *buff, int len) { int res = -1; if (m_bzStatus >= 0) { if (m_bz) { MonitorBlock block(this); res = m_bz ? BZ2_bzRead(&m_bzStatus, m_bz, buff, len) : -1; } } MonitorBlock::yield(); return res; }
size_t bread (void *ptr, size_t size, size_t nitems, void *file) { struct fhandle *f = (struct fhandle *) file; size_t ret; ret = BZ2_bzRead (&bzerror, (BZFILE *) f->file2, ptr, size * nitems); if (bzerror == BZ_STREAM_END) { char unused[1024]; int nunused = sizeof (unused); BZ2_bzReadGetUnused (&bzerror, (BZFILE *) f->file2, (void **)&unused, &nunused); } return ret; }
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; }
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; }
size_t BzipInputStream::read(void* buf, size_t size) { assert(m_cf); assert(m_fp); int err = BZ_OK; int nRead = BZ2_bzRead(&err, m_fp, buf, size); if (BZ_OK != err) { std::ostringstream oss; oss << "BZ2_bzRead err=" << strbzerr(err) << ", in " << BOOST_CURRENT_FUNCTION; throw OpenFileException("<fd>", oss.str().c_str()); } assert(nRead <= (int)size); return (size_t)nRead; }
//---------------------------------------------------- int zu_read(ZUFILE *f, void *buf, long len) { int nb = 0; int bzerror=BZ_OK; switch(f->type) { case ZU_COMPRESS_NONE : nb = (int)fread(buf, 1, len, (FILE*)(f->zfile)); break; case ZU_COMPRESS_GZIP : nb = gzread((gzFile)(f->zfile), buf, len); break; case ZU_COMPRESS_BZIP : nb = BZ2_bzRead(&bzerror,(BZFILE*)(f->zfile), buf, len); break; } f->pos += nb; return nb; }
//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; } }
int readFile(struct Input *ctx, char * buffer, int len) { void *f = ctx->fileHandle; int l = 0, error = 0; if (ctx->eof || (len == 0)) return 0; switch(ctx->type) { case Input::plainFile: l = read(*(int *)f, buffer, len); if (l <= 0) ctx->eof = 1; break; case Input::gzipFile: l = gzread((gzFile)f, buffer, len); if (l <= 0) ctx->eof = 1; break; case Input::bzip2File: l = BZ2_bzRead(&error, (BZFILE *)f, buffer, len); /* error codes BZ_OK and BZ_STREAM_END are both "OK", but the stream end means the reader needs to be reset from the original handle. */ if (error != BZ_OK) { /* for stream errors, try re-opening the stream before admitting defeat. */ if (error != BZ_STREAM_END || bzReOpen(ctx, &error) != 0) { l = 0; ctx->eof = 1; } } break; default: fprintf(stderr, "Bad file type\n"); break; } if (l < 0) { fprintf(stderr, "File reader received error %d (%d)\n", l, error); l = 0; } return l; }
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); }
GT_INLINE size_t gt_input_file_fill_buffer(gt_input_file* const input_file) { #ifdef HAVE_BZLIB int bzerr; #endif GT_INPUT_FILE_CHECK(input_file); input_file->global_pos += input_file->buffer_size; input_file->buffer_pos = 0; input_file->buffer_begin = 0; if (gt_expect_true( (input_file->file_type==STREAM && !feof(input_file->file)) || (input_file->file_type==REGULAR_FILE && !feof(input_file->file)))) { input_file->buffer_size = fread(input_file->file_buffer,sizeof(uint8_t),GT_INPUT_BUFFER_SIZE,input_file->file); if (input_file->buffer_size==0) { input_file->eof = true; } return input_file->buffer_size; } else if (input_file->file_type==MAPPED_FILE && input_file->global_pos < input_file->file_size) { input_file->buffer_size = input_file->file_size-input_file->global_pos; return input_file->buffer_size; #ifdef HAVE_ZLIB } else if (input_file->file_type==GZIPPED_FILE && !gzeof((gzFile)input_file->file)) { input_file->buffer_size = gzread((gzFile)input_file->file,input_file->file_buffer,GT_INPUT_BUFFER_SIZE); if (input_file->buffer_size==0) { input_file->eof = true; } return input_file->buffer_size; #endif #ifdef HAVE_BZLIB } else if (input_file->file_type==BZIPPED_FILE) { input_file->buffer_size = BZ2_bzRead(&bzerr,input_file->file,input_file->file_buffer,GT_INPUT_BUFFER_SIZE); if(input_file->buffer_size==0) { input_file->eof=true; } return input_file->buffer_size; #endif } else { input_file->eof = true; return 0; } }