static int fontFileSeek(fontFile *ff, z_off_t offset, int whence) { if (ff->type == gzFontFile) { return gzseek(ff->f.gz, offset, whence); } else { /* bzlib has no easy equivalent so we have to fake it, * fortunately, we only have to handle a couple of cases */ int n; char buf[BUFSIZ]; switch (whence) { case SEEK_SET: n = offset - ff->pos; break; case SEEK_CUR: n = offset; break; default: return -1; } while (n > BUFSIZ) { if (BZ2_bzread(ff->f.bz2, buf, BUFSIZ) != BUFSIZ) return -1; n -= BUFSIZ; } if (BZ2_bzread(ff->f.bz2, buf, n) != n) return -1; ff->pos = offset; return offset; } }
/** ** Seek on compressed input. (I hope newer libs support it directly) ** ** @param file File handle ** @param offset Seek position ** @param whence How to seek */ static void bzseek(BZFILE *file, unsigned offset, int) { char buf[32]; while (offset > sizeof(buf)) { BZ2_bzread(file, buf, sizeof(buf)); offset -= sizeof(buf); } BZ2_bzread(file, buf, offset); }
static int load_bz2_rom(const char *fn) { int len; BZFILE *fp; void *tmp; /* Open up the file in question. */ if(!(fp = BZ2_bzopen(fn, "rb"))) { #ifdef DEBUG fprintf(stderr, "chip8_mem_load_rom: Could not open ROM: %s!\n", fn); #endif return ROM_LOAD_E_OPEN; } /* Read one chunk of the file at a time, until we have no more left. */ if(!(cart_rom = (uint8 *)malloc(0x100))) { #ifdef DEBUG fprintf(stderr, "chip8_mem_load_rom: Couldn't allocate space!\n"); #endif BZ2_bzclose(fp); return ROM_LOAD_E_ERRNO; } cart_len = 0; len = BZ2_bzread(fp, cart_rom, 0x100); while(len != 0 && len != -1) { cart_len += len; tmp = realloc(cart_rom, cart_len + 0x100); if(!tmp) { #ifdef DEBUG fprintf(stderr, "chip8_mem_load_rom: Couldn't allocate space!\n"); #endif BZ2_bzclose(fp); free(cart_rom); cart_rom = NULL; return ROM_LOAD_E_ERRNO; } cart_rom = (uint8 *)tmp; memset(cart_rom + cart_len, 0xFF, 0x100); len = BZ2_bzread(fp, cart_rom + cart_len, 0x100); } BZ2_bzclose(fp); finalize_load(fn); return ROM_LOAD_SUCCESS; }
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); }
inline size_t read(void *buffer, size_t size, size_t count){ switch(current_file){ case FILE_MODE_RAW:{ return fread(buffer, size, count, file1); } case FILE_MODE_BZ2:{ return BZ2_bzread(file2, buffer, size * count); } default:{ return Perror("Invalid read"); } } }
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 &) { } }
// Fill get area from bzip2 file bzfilebuf::int_type bzfilebuf::underflow() { // If something is left in the get area by chance, return it // (this shouldn't normally happen, as underflow is only supposed // to be called when gptr >= egptr, but it serves as error check) if (this->gptr() && (this->gptr() < this->egptr())) return traits_type::to_int_type(*(this->gptr())); // If the file hasn't been opened for reading, produce error if (!this->is_open() || !(io_mode & std::ios_base::in)) return traits_type::eof(); // Attempt to fill internal buffer from bzip2 file // (buffer must be guaranteed to exist...) int bytes_read = (int)BZ2_bzread(file, buffer, (int)buffer_size); // Indicates error or EOF if (bytes_read <= 0) { // Reset get area this->setg(buffer, buffer, buffer); return traits_type::eof(); } // Make all bytes read from file available as get area this->setg(buffer, buffer, buffer + bytes_read); // Return next character in get area return traits_type::to_int_type(*(this->gptr())); }
static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count) { struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; size_t ret = 0; do { int just_read; size_t remain = count - ret; int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX); just_read = BZ2_bzread(self->bz_file, buf, to_read); if (just_read < 1) { /* it is not safe to keep reading after an error, see #72613 */ stream->eof = 1; if (just_read < 0) { return -1; } break; } ret += just_read; } while (ret < count); return ret; }
int64_t BZ2File::readImpl(char * buf, int64_t length) { assert(m_bzFile); int len = BZ2_bzread(m_bzFile, buf, length); if (len < length) m_eof = true; return len; }
int bzstreambuf::underflow() { // used for input buffer only if ( gptr() && ( gptr() < egptr())) return * reinterpret_cast<unsigned char *>( gptr()); if ( ! (mode & std::ios::in) || ! opened) return EOF; // Josuttis' implementation of inbuf int n_putback = gptr() - eback(); if ( n_putback > 4) n_putback = 4; memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback); int num = 0; num = BZ2_bzread( file, buffer+4, bufferSize-4); if (num <= 0) // ERROR or EOF return EOF; // reset buffer pointers setg( buffer + (4 - n_putback), // beginning of putback area buffer + 4, // read position buffer + 4 + num); // end of buffer // return next character return * reinterpret_cast<unsigned char *>( gptr()); }
/** * This function tests whether a file is of FASTQ format. * * @param fn file name * * @return ALDER_YES if so, ALDER_NO otherwise. */ int alder_file_isfastq(const char *fn) { char buf[ALDER_BUFSIZE_100]; int status = 0; size_t readLen = 0; int s = alder_file_exist(fn); if (s == 0) { return ALDER_NO; } s = alder_file_isgzip(fn); int s2 = alder_file_isbzip2(fn); if (s == 1) { // open it with zlib. gzFile gz = gzopen(fn, "rb"); if (gz == NULL) { return ALDER_NO; } int len = gzread(gz, buf, sizeof(buf)); if (len < 0) { gzclose(gz); return ALDER_NO; } readLen = len; gzclose(gz); } else if (s2 == 1) { // open it with bzlib. BZFILE *bz = BZ2_bzopen(fn, "rb"); if (bz == NULL) { return ALDER_NO; } int len = BZ2_bzread(bz, buf, sizeof(buf)); if (len < 0) { BZ2_bzclose(bz); return ALDER_NO; } readLen = len; BZ2_bzclose(bz); } else { // open it with a regular fopen or open. FILE *fp = fopen(fn, "rb"); if (fp == NULL) return ALDER_NO; readLen = fread(buf, sizeof(char), sizeof(buf), fp); fclose(fp); } /* I just check the first character. */ if (readLen > 1) { if (buf[0] == '@') { status = ALDER_YES; } else { status = ALDER_NO; } } else { status = ALDER_NO; } return status; }
int gt_xbzread(BZFILE *file, void *buf, unsigned len) { int rval; if ((rval = BZ2_bzread(file, buf, len)) == -1) { fprintf(stderr, "cannot read from compressed file\n"); exit(EXIT_FAILURE); } return rval; }
static inline int fontFileRead(fontFile *ff, void *buf, unsigned len) { if (ff->type == gzFontFile) { return gzread(ff->f.gz, buf, len); } else { int r = BZ2_bzread(ff->f.bz2, buf, len); ff->pos += r; return r; } }
/* 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; }
static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract; size_t ret; ret = BZ2_bzread(self->bz_file, buf, count); if (ret == 0) { stream->eof = 1; } return ret; }
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; }
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; }
int64_t BZ2File::readImpl(char * buf, int64_t length) { if (length == 0) { return 0; } assert(m_bzFile); int len = BZ2_bzread(m_bzFile, buf, length); /* Sometimes libbz2 will return fewer bytes than requested, and set bzerror * to BZ_STREAM_END, but it's not actually EOF, and you can keep reading from * the file - so, only set EOF after a failed read. This matches PHP5. */ if (len == 0) { setEof(true); } return len; }
static inline int fontFileGetc(fontFile *ff) { if (ff->type == gzFontFile) { return gzgetc(ff->f.gz); } else { char buf; if (BZ2_bzread(ff->f.bz2, &buf, 1) != 1) { return -1; } else { ff->pos += 1; return (int) buf; } } }
long LogRead( void *refNum, char *filename, char *databuffer, long ReadCount ) { if ( refNum ){ #ifndef DEF_MAC if ( IsURL( filename ) ) return NetRead( (void*)refNum, databuffer, ReadCount ); else if ( IsPKZIP( filename ) ) return UnzipGetData( (void*)refNum, (unsigned char *)databuffer, (size_t)ReadCount ); else #ifdef _BZLIB_H if ( IsBZIP( filename ) ) // please let the mac do this one day... for completeness sake and less #ifdefs return BZ2_bzread( (void*)refNum, databuffer, ReadCount ); else #endif #endif return gzread( (gzFile)refNum, databuffer, ReadCount ); } return 0; }
static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count) { struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; size_t ret = 0; do { int just_read; size_t remain = count - ret; int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX); just_read = BZ2_bzread(self->bz_file, buf, to_read); if (just_read < 1) { stream->eof = 0 == just_read; break; } ret += just_read; } while (ret < count); return ret; }
void process_bzip2(char *pPath) { #ifdef HAVE_LIBBZ2 BZFILE *bzfile; char buf[BUF_SIZE]; int nRead; long depth = 0; int ret = 0; bzfile = BZ2_bzopen(pPath, "rb"); if (bzfile == NULL) { // don't care why return; } while ((nRead = BZ2_bzread(bzfile, &buf, BUF_SIZE)) > 0) { depth += nRead; if (is_match(buf,nRead)) { ret = 1; if (!LogTotalMatches) { send_match(hit,pPath); (void)BZ2_bzclose(bzfile); return; } } bzero(buf, sizeof(buf)); if ((ScanDepth != 0) && (depth >= (ScanDepth * 1024))) { break; } } if ((LogTotalMatches && TotalMatches) || ret) { send_match(hit, pPath); } (void)BZ2_bzclose(bzfile); #endif return; }
int CFile::PImpl::read(void *buf, size_t len) { int ret = 0; if (cl_type != CLF_TYPE_INVALID) { if (cl_type == CLF_TYPE_PLAIN) { ret = fread(buf, 1, len, cl_plain); } #ifdef USE_ZLIB if (cl_type == CLF_TYPE_GZIP) { ret = gzread(cl_gz, buf, len); } #endif // USE_ZLIB #ifdef USE_BZ2LIB if (cl_type == CLF_TYPE_BZIP2) { ret = BZ2_bzread(cl_bz, buf, len); } #endif // USE_BZ2LIB } else { errno = EBADF; } return ret; }
int builtin_bzip2_cmd(int argc, char *argv[]) { if(argc < 3) { ds_printf("Usage: %s option infile outfile\n" "Options: \n" " -9 -Compress with mode from 0 to 9\n" " -d -Decompress\n\n" "Examples: %s -9 /cd/file.dat /ram/file.dat.bz2\n" " %s -d /ram/file.dat.bz2 /ram/file.dat\n", argv[0], argv[0], argv[0]); return CMD_NO_ARG; } char dst[MAX_FN_LEN]; char buff[512]; int len = 0; file_t fd; BZFILE *zfd; if(argc < 4) { char tmp[MAX_FN_LEN]; relativeFilePath_wb(tmp, argv[2], strrchr(argv[2], '/')); sprintf(dst, "%s.bz2", tmp); } else { strcpy(dst, argv[3]); } if(!strcmp("-d", argv[1])) { ds_printf("DS_PROCESS: Decompressing '%s' ...\n", argv[2]); zfd = BZ2_bzopen(argv[2], "rb"); if(zfd == NULL) { ds_printf("DS_ERROR: Can't create file: %s\n", argv[2]); return CMD_ERROR; } fd = fs_open(dst, O_WRONLY | O_CREAT); if(fd < 0) { ds_printf("DS_ERROR: Can't open file: %s\n", dst); BZ2_bzclose(zfd); return CMD_ERROR; } while((len = BZ2_bzread(zfd, buff, sizeof(buff))) > 0) { fs_write(fd, buff, len); } BZ2_bzclose(zfd); fs_close(fd); ds_printf("DS_OK: File decompressed."); } else if(argv[1][1] >= '0' && argv[1][1] <= '9') { char mode[3]; sprintf(mode, "wb%c", argv[1][1]); ds_printf("DS_PROCESS: Compressing '%s' with mode '%c' ...\n", argv[2], argv[1][1]); zfd = BZ2_bzopen(dst, mode); if(zfd == NULL) { ds_printf("DS_ERROR: Can't create file: %s\n", dst); return CMD_ERROR; } fd = fs_open(argv[2], O_RDONLY); if(fd < 0) { ds_printf("DS_ERROR: Can't open file: %s\n", argv[2]); BZ2_bzclose(zfd); return CMD_ERROR; } while((len = fs_read(fd, buff, sizeof(buff))) > 0) { if(!BZ2_bzwrite(zfd, buff, len)) { ds_printf("DS_ERROR: Error writing to file: %s\n", dst); BZ2_bzclose(zfd); fs_close(fd); return CMD_ERROR; } } BZ2_bzclose(zfd); fs_close(fd); ds_printf("DS_OK: File compressed."); } else { return CMD_NO_ARG; } return CMD_OK; }
int jnb_io_read_bz2(char* in_filename, char** out_data) { #ifdef BZLIB_SUPPORT char* bzfilename; BZFILE* bzf; bzfilename = malloc(strlen(in_filename) + 5); strcpy(bzfilename, in_filename); strcat(bzfilename, ".bz2"); bzf = BZ2_bzopen(bzfilename, "rb"); free(bzfilename); bzfilename = NULL; *out_data = NULL; if (bzf != NULL) { int bufsize = 0; int bufpos = 0; int br; char* ptr; do { if (bufpos >= bufsize) { bufsize += 1024 * 1024; *out_data = (char*) realloc(*out_data, bufsize); if (*out_data == NULL) { perror("realloc()"); return -2; } } br = BZ2_bzread(bzf, *out_data + bufpos, bufsize - bufpos); if (br == -1) { fprintf(stderr, "bzread failed.\n"); return -3; } bufpos += br; } while (br>0); /* try to shrink buffer... */ ptr = (char*) realloc(*out_data, bufpos); if (ptr != NULL) *out_data = ptr; BZ2_bzclose(bzf); return 0; } #endif /* BZLIB_SUPPORT */ return -1; }
static ssize_t cookie_bzread(void *cookie, char *buf, size_t nbytes) { return BZ2_bzread((BZFILE *)cookie, buf, nbytes); }
// ======================================================= int CImageDisk::getCompressionLevelForImage(char *szFilename) // [Main-Thread] { //return(COMPRESS_LZO); BEGIN; if (!strcmp(szFilename, "stdin")) return COMPRESS_NONE; gzFile gzImageFile; FILE *fImageFile; BZFILE *bzImageFile; DWORD dwRes; CVolumeHeader headVolume; const char *szLabel = MAGIC_BEGIN_VOLUME; char cBuf[64]; showDebug(3, "TRACE_001\n"); // ------ 0. Check for LZO compression fImageFile = fopen(szFilename, "rb"); if (fImageFile == NULL) goto checkBzip2; dwRes = fread(cBuf, 1, 16, fImageFile); fclose(fImageFile); if (dwRes != 16) goto checkBzip2; if (strncmp(cBuf+1, "LZO", 3) == 0) RETURN_int(COMPRESS_LZO); showDebug(3, "TRACE_002\n"); // ------ 1. Check for a bzip2 compression checkBzip2: bzImageFile = BZ2_bzopen(szFilename, "rb"); if (bzImageFile == NULL) goto checkNone; dwRes = BZ2_bzread(bzImageFile, &headVolume, sizeof(CVolumeHeader)); BZ2_bzclose(bzImageFile); if (dwRes != sizeof(CVolumeHeader)) goto checkNone; if (strncmp(headVolume.szMagicString, szLabel, strlen(szLabel)) == 0) RETURN_int(COMPRESS_BZIP2); showDebug(3, "TRACE_003\n"); // ------ 2. Check for no compression checkNone: fImageFile = fopen(szFilename, "rb"); if (fImageFile == NULL) goto checkGzip; dwRes = fread(&headVolume, 1, sizeof(CVolumeHeader), fImageFile); fclose(fImageFile); if (dwRes != sizeof(CVolumeHeader)) goto checkGzip; if (strncmp(headVolume.szMagicString, szLabel, strlen(szLabel)) == 0) RETURN_int(COMPRESS_NONE); showDebug(3, "TRACE_004\n"); // ------ 3. Check for a gzip compression checkGzip: gzImageFile = gzopen(szFilename, "rb"); if (gzImageFile == NULL) RETURN_int(-1); // error dwRes = gzread(gzImageFile, &headVolume, sizeof(CVolumeHeader)); gzclose(gzImageFile); if (dwRes != sizeof(CVolumeHeader)) RETURN_int(-1); // error if (strncmp(headVolume.szMagicString, szLabel, strlen(szLabel)) == 0) //if (strcmp(headMain.szAppName, "PartImage") == 0) RETURN_int(COMPRESS_GZIP); showDebug(3, "TRACE_005\n"); RETURN_int(-1); // error }
int main(int argc,char *argv[]) { int decompress = 0; int level = 9; char *fn_r = NULL; char *fn_w = NULL; #ifdef _WIN32 if(BZ2DLLLoadLibrary()<0){ fprintf(stderr,"Loading of %s failed. Giving up.\n", BZ2_LIBNAME); exit(1); } printf("Loading of %s succeeded. Library version is %s.\n", BZ2_LIBNAME, BZ2_bzlibVersion() ); #endif while(++argv,--argc){ if(**argv =='-' || **argv=='/'){ char *p; for(p=*argv+1;*p;p++){ if(*p=='d'){ decompress = 1; }else if('1'<=*p && *p<='9'){ level = *p - '0'; }else{ usage(); exit(1); } } }else{ break; } } if(argc>=1){ fn_r = *argv; argc--;argv++; }else{ fn_r = NULL; } if(argc>=1){ fn_w = *argv; argc--;argv++; }else{ fn_w = NULL; } { int len; char buff[0x1000]; char mode[10]; if(decompress){ BZFILE *BZ2fp_r = NULL; FILE *fp_w = NULL; if(fn_w){ if((fp_w = fopen(fn_w,"wb"))==NULL){ printf("can't open [%s]\n",fn_w); perror("reason:"); exit(1); } }else{ fp_w = stdout; } if((fn_r == NULL && (BZ2fp_r = BZ2_bzdopen(fileno(stdin),"rb"))==NULL) || (fn_r != NULL && (BZ2fp_r = BZ2_bzopen(fn_r,"rb"))==NULL)){ printf("can't bz2openstream\n"); exit(1); } while((len=BZ2_bzread(BZ2fp_r,buff,0x1000))>0){ fwrite(buff,1,len,fp_w); } BZ2_bzclose(BZ2fp_r); if(fp_w != stdout) fclose(fp_w); }else{ BZFILE *BZ2fp_w = NULL; FILE *fp_r = NULL; if(fn_r){ if((fp_r = fopen(fn_r,"rb"))==NULL){ printf("can't open [%s]\n",fn_r); perror("reason:"); exit(1); } }else{ fp_r = stdin; } mode[0]='w'; mode[1] = '0' + level; mode[2] = '\0'; if((fn_w == NULL && (BZ2fp_w = BZ2_bzdopen(fileno(stdout),mode))==NULL) || (fn_w !=NULL && (BZ2fp_w = BZ2_bzopen(fn_w,mode))==NULL)){ printf("can't bz2openstream\n"); exit(1); } while((len=fread(buff,1,0x1000,fp_r))>0){ BZ2_bzwrite(BZ2fp_w,buff,len); } BZ2_bzclose(BZ2fp_w); if(fp_r!=stdin)fclose(fp_r); } } #ifdef _WIN32 BZ2DLLFreeLibrary(); #endif return 0; }
/** * Takes a data stream compressed with bzip2, and writes it out to disk, uncompresses it, and deletes the * compressed version. */ static bool DecompressBZipToDisk( const char *outFilename, const char *srcFilename, char *data, int bytesTotal ) { if ( g_pFileSystem->FileExists( outFilename ) || !data || bytesTotal < 1 ) { return false; } // Create the subdirs char * tmpDir = CloneString( outFilename ); COM_CreatePath( tmpDir ); delete[] tmpDir; // open the file for writing char fullSrcPath[MAX_PATH]; Q_MakeAbsolutePath( fullSrcPath, sizeof( fullSrcPath ), srcFilename, com_gamedir ); if ( !g_pFileSystem->FileExists( fullSrcPath ) ) { // Write out the .bz2 file, for simplest decompression FileHandle_t ifp = g_pFileSystem->Open( fullSrcPath, "wb" ); if ( !ifp ) { return false; } int bytesWritten = g_pFileSystem->Write( data, bytesTotal, ifp ); g_pFileSystem->Close( ifp ); if ( bytesWritten != bytesTotal ) { // couldn't write out all of the .bz2 file g_pFileSystem->RemoveFile( srcFilename ); return false; } } // Prepare the uncompressed filehandle FileHandle_t ofp = g_pFileSystem->Open( outFilename, "wb" ); if ( !ofp ) { g_pFileSystem->RemoveFile( srcFilename ); return false; } // And decompress! const int OutBufSize = 65536; char buf[ OutBufSize ]; BZFILE *bzfp = BZ2_bzopen( fullSrcPath, "rb" ); while ( 1 ) { int bytesRead = BZ2_bzread( bzfp, buf, OutBufSize ); if ( bytesRead < 0 ) { break; // error out } if ( bytesRead > 0 ) { int bytesWritten = g_pFileSystem->Write( buf, bytesRead, ofp ); if ( bytesWritten != bytesRead ) { break; // error out } } else { g_pFileSystem->Close( ofp ); BZ2_bzclose( bzfp ); g_pFileSystem->RemoveFile( srcFilename ); return true; } } // We failed somewhere, so clean up and exit g_pFileSystem->Close( ofp ); BZ2_bzclose( bzfp ); g_pFileSystem->RemoveFile( srcFilename ); g_pFileSystem->RemoveFile( outFilename ); return false; }