// Open bzip2 file bzfilebuf* bzfilebuf::open(const char *name, std::ios_base::openmode mode) { // Fail if file already open if (this->is_open()) return NULL; // Don't support simultaneous read/write access (yet) if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) return NULL; // Build mode string for bzopen and check it [27.8.1.3.2] char char_mode[6] = "\0\0\0\0\0"; if (!this->open_mode(mode, char_mode)) return NULL; // Attempt to open file if ((file = BZ2_bzopen(name, char_mode)) == NULL) return NULL; // On success, allocate internal buffer and set flags this->enable_buffer(); io_mode = mode; own_fd = true; return this; }
FILE * SessionImpl::OpenBZip2File (/*[in]*/ const PathName & path) { AutoBZ2 autoBz2 (BZ2_bzopen(path.Get(), "rb")); if (autoBz2.Get() == 0) { FATAL_MIKTEX_ERROR ("OpenBZip2File", T_("BZ2_bzopen() failed for some."), path.Get()); } FILE * pFiles[2]; Pipe (pFiles, PIPE_SIZE); try { auto_ptr<BZip2ReaderThreadArg> pArg (new BZip2ReaderThreadArg); pArg->bzin = autoBz2.Get(); pArg->fileout = pFiles[1]; auto_ptr<Thread> pThread (Thread::Start(BZip2ReaderThread, pArg.get())); pArg.release (); autoBz2.Detach (); return (pFiles[0]); } catch (const exception &) { if (pFiles[0] != 0) { fclose (pFiles[0]); } if (pFiles[1] != 0) { fclose (pFiles[1]); } throw; } }
bzstreambuf *bzstreambuf::open( const char *name, int open_mode ) { if ( is_open( ) ) return ( bzstreambuf * ) 0; mode = open_mode; // no append nor read/write mode if ( ( mode & std::ios::ate ) || ( mode & std::ios::app ) || ( ( mode & std::ios::in ) && ( mode & std::ios::out ) ) ) return ( bzstreambuf * ) 0; char fmode[10]; for( unsigned int i = 0; i < sizeof(fmode); i++ ) fmode[i] = '\0'; char *fmodeptr = fmode; if ( mode & std::ios::in ) *fmodeptr++ = 'r'; else if ( mode & std::ios::out ) { *fmodeptr++ = 'w'; if( this->zlevel() >= 0 && this->zlevel() <= 9 ) { *fmodeptr++ = (char)48 + this->zlevel(); // '0' - '9' } } *fmodeptr++ = 'b'; *fmodeptr = '\0'; file = BZ2_bzopen( name, fmode ); if ( file == 0 ) return ( bzstreambuf * ) 0; opened = 1; return this; }
/** * 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; }
long LogOpenQuiet( char *filename, __int64 *filelen ) { char rawfileName[256]; long hnd = 0; DateFixFilename( filename, rawfileName ); // Check if we trying to open a shortcut if ( IsShortCut( rawfileName ) ){ char linkpath[256]; mystrcpy( linkpath, rawfileName ); #if DEF_WINDOWS GetShortCut( linkpath, rawfileName ); #endif } // Now see if the we want to open a short cut to a URL if ( IsURLShortCut( rawfileName ) ){ char linkpath[256]; mystrcpy( linkpath, rawfileName ); #if DEF_WINDOWS GetURLShortCut( linkpath, rawfileName ); #endif } // Check for a just a plain URL if ( IsURL( rawfileName ) ){ StatusSetID( IDS_REMOTELOG , strrchr( rawfileName,'/' ) ); hnd = (long)INetOpen( rawfileName, filelen ); return hnd; } // Check other types char format[100]; // determine if its PKZIP file and if so... dont open it, YET if ( IsFileInvalid( rawfileName, format ) ) return 0; #ifndef DEF_MAC if ( IsPKZIP( rawfileName ) ) hnd = (long)UnzipOpen( rawfileName, NULL ); else #ifdef _BZLIB_H if ( aIsBzFile( rawfileName ) ) // PLEASE DO THIS SOON FOR MAC.... at least for manual completeness, hey OSX people will love it. hnd = (long)BZ2_bzopen( rawfileName, "rb" ); else #endif #endif { hnd = (long)gzopen( rawfileName, "rb" ); } if ( filelen && hnd ) *filelen = GetFileLength( rawfileName ); return hnd; }
BZFILE* gt_xbzopen(const char *path, const char *mode) { BZFILE* file; if (!(file = BZ2_bzopen(path, mode))) { fprintf(stderr, "BZ2_bzopen(): cannot open file '%s': %s\n", path, strerror(errno)); exit(EXIT_FAILURE); } return file; }
static FD_t bzdOpen(const char * path, const char * mode) { FD_t fd; BZFILE *bzfile;; if ((bzfile = BZ2_bzopen(path, mode)) == NULL) return NULL; fd = fdNew(path); fdPop(fd); fdPush(fd, bzdio, bzfile, -1); return fdLink(fd); }
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; }
PandaWriter panda_writer_open_file( const char *filename, bool bzip) { if (bzip) { BZFILE *file; file = BZ2_bzopen(filename, "w"); return (file == NULL) ? NULL : panda_writer_new((PandaBufferWrite) bzip_write, file, (PandaDestroy) BZ2_bzclose); } else { FILE *file; file = fopen(filename, "w"); return (file == NULL) ? NULL : panda_writer_new_file(file); } }
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 inline void * fontFileOpen(fontFile *ff, const char *filename) { int n = strlen(filename); if (strcmp(filename + n - 4, ".bz2") == 0) { ff->type = bz2FontFile; ff->f.bz2 = BZ2_bzopen(filename, "rb"); ff->pos = 0; return ff->f.bz2; } else { ff->type = gzFontFile; ff->f.gz = gzopen(filename, "rb"); return ff->f.gz; } }
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; }
bzstreambuf* bzstreambuf::open( const char* name, int open_mode) { if ( is_open()) return (bzstreambuf*)0; mode = open_mode; // no append nor read/write mode if ((mode & std::ios::ate) || (mode & std::ios::app) || ((mode & std::ios::in) && (mode & std::ios::out))) return (bzstreambuf*)0; char fmode[10]; char* fmodeptr = fmode; if ( mode & std::ios::in) *fmodeptr++ = 'r'; else if ( mode & std::ios::out) *fmodeptr++ = 'w'; *fmodeptr++ = 'b'; *fmodeptr = '\0'; file = 0; file = BZ2_bzopen( name, fmode); if (file == 0) return (bzstreambuf*)0; opened = 1; return this; }
BZFILE* bzopen_report(char *file, char *mode) { if(strcmp(mode,"r") == 0){ if(file_exist(file) != 0){ error_msg("file %s is not exist\n", file); return (BZFILE*) NULL; } } if(strcmp(mode,"w") == 0){ if(file_exist(file) == 0){ error_msg("file %s is already exist\n", file); return (BZFILE*) NULL; } } BZFILE *file_handle=BZ2_bzopen(file,mode); if(!file_handle){ error_msg("Can not open the file %s\n", file); } return file_handle; }
Segment *SegmentNoaa::ReadSegmentInMemory() { 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()); segmentok = false; return this; } qDebug() << "Bz2 file " + this->fileInfo.absoluteFilePath() + " is open"; if((b = BZ2_bzopen(this->fileInfo.absoluteFilePath().toLatin1(),"rb"))==NULL) { segmentok = false; 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) { for (int i=0, j = 0; i < 20471; i+=10, j++) { for(int k = 0, l = 0; k < 5; k++) { val1_ch[k] = 0xFF & picture_line.at(i+l); l++; val2_ch[k] = 0xFF & picture_line.at(i+l); l++; tot_ch[k] = (val1_ch[k] <<= 8) | val2_ch[k]; *(this->ptrbaChannel[k].data() + heightinsegment * 2048 + j) = tot_ch[k]; } for(int k = 0 ; k < 5; k++) { if (tot_ch[k] < stat_min_ch[k] ) stat_min_ch[k] = tot_ch[k]; if (tot_ch[k] > stat_max_ch[k] ) stat_max_ch[k] = tot_ch[k]; } } heightinsegment++; } } } //NbrOfLines = heightinsegment; BZ2_bzclose ( b ); fclose(f); return this; }
int CFile::PImpl::open(const char *name, long openflags) { char buf[512]; const char *openstring; if ((openflags & CL_OPEN_READ) && (openflags & CL_OPEN_WRITE)) { openstring = "rwb"; } else if (openflags & CL_OPEN_READ) { openstring = "rb"; } else if (openflags & CL_OPEN_WRITE) { openstring = "wb"; } else { DebugPrint("Bad CLopen flags"); Assert(0); return -1; } cl_type = CLF_TYPE_INVALID; if (openflags & CL_OPEN_WRITE) { #ifdef USE_BZ2LIB if ((openflags & CL_WRITE_BZ2) && (cl_bz = BZ2_bzopen(strcat(strcpy(buf, name), ".bz2"), openstring))) { cl_type = CLF_TYPE_BZIP2; } else #endif #ifdef USE_ZLIB if ((openflags & CL_WRITE_GZ) && (cl_gz = gzopen(strcat(strcpy(buf, name), ".gz"), openstring))) { cl_type = CLF_TYPE_GZIP; } else #endif if ((cl_plain = fopen(name, openstring))) { cl_type = CLF_TYPE_PLAIN; } } else { if (!(cl_plain = fopen(name, openstring))) { // try plain first #ifdef USE_ZLIB if ((cl_gz = gzopen(strcat(strcpy(buf, name), ".gz"), "rb"))) { cl_type = CLF_TYPE_GZIP; } else #endif #ifdef USE_BZ2LIB if ((cl_bz = BZ2_bzopen(strcat(strcpy(buf, name), ".bz2"), "rb"))) { cl_type = CLF_TYPE_BZIP2; } else #endif { } } else { cl_type = CLF_TYPE_PLAIN; // Hmm, plain worked, but nevertheless the file may be compressed! if (fread(buf, 2, 1, cl_plain) == 1) { #ifdef USE_BZ2LIB if (buf[0] == 'B' && buf[1] == 'Z') { fclose(cl_plain); if ((cl_bz = BZ2_bzopen(name, "rb"))) { cl_type = CLF_TYPE_BZIP2; } else { if (!(cl_plain = fopen(name, "rb"))) { cl_type = CLF_TYPE_INVALID; } } } #endif // USE_BZ2LIB #ifdef USE_ZLIB if (buf[0] == 0x1f) { // don't check for buf[1] == 0x8b, so that old compress also works! fclose(cl_plain); if ((cl_gz = gzopen(name, "rb"))) { cl_type = CLF_TYPE_GZIP; } else { if (!(cl_plain = fopen(name, "rb"))) { cl_type = CLF_TYPE_INVALID; } } } #endif // USE_ZLIB } if (cl_type == CLF_TYPE_PLAIN) { // ok, it is not compressed rewind(cl_plain); } } } if (cl_type == CLF_TYPE_INVALID) { //fprintf(stderr, "%s in ", buf); return -1; } return 0; }
#ifdef SUPPORT_LIBBZ2 if (pathlen > 4 && strcmp(pathname + pathlen - 4, ".bz2") == 0) { inbz2 = BZ2_bzopen(pathname, "rb"); handle = (void *)inbz2; frtype = FR_LIBBZ2; } else #endif /* Otherwise use plain fopen(). The label is so that we can come back here if an attempt to read a .bz2 file indicates that it really is a plain file. */ #ifdef SUPPORT_LIBBZ2 PLAIN_FILE: #endif { in = fopen(pathname, "rb"); handle = (void *)in; frtype = FR_PLAIN; }
static inline FILE *mybzfopen(const char *fn, const char *mode) { BZFILE *bzf = BZ2_bzopen(fn, mode); return cookieopen(bzf, mode, cookie_bzread, cookie_bzwrite, cookie_bzclose); }
/** * 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; }
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; }
ZOLTAN_FILE* ZOLTAN_FILE_open(const char *path, const char *mode, const ZOLTAN_FILETYPE type) { ZOLTAN_FILE* file; char truemode[10]; char filename[FILE_NAME_SIZE+EXTENSION_SIZE]; int error = 1; int i; file = (ZOLTAN_FILE*) malloc(sizeof(ZOLTAN_FILE)); if (file == NULL) return (NULL); file->type = type; file->pos = -1; file->size = 0; if (type != STANDARD) { if (!strstr(mode, "b")) sprintf(truemode, "%sb", mode); else strcpy(truemode, mode); } strncpy (filename, path, FILE_NAME_SIZE); for (i=0; (error != 0) && (i <2) ; ++i) { if (i == 0) { /* Try the classical compressed version */ char append[4]; switch (type) { #ifdef ZOLTAN_GZIP case GZIP: strncpy (append, ".gz", EXTENSION_SIZE); break; #endif #ifdef ZOLTAN_BZ2 case BZIP2: strncpy (append, ".bz2", EXTENSION_SIZE); break; #endif #ifdef ZOLTAN_LZMA case LZMA: strncpy (append, ".lz", EXTENSION_SIZE); break; #endif default: append[0] = '\0'; break; } strncat(filename, append, FILE_NAME_SIZE + EXTENSION_SIZE); } else strncpy(filename, path, FILE_NAME_SIZE); switch (type) { case STANDARD: file->strm.fileunc = fopen(filename, mode); error = (file->strm.fileunc == NULL); break; #ifdef ZOLTAN_GZIP case GZIP: file->strm.filegz = gzopen(filename, truemode); error = (file->strm.filegz == NULL); break; #endif #ifdef ZOLTAN_BZ2 case BZIP2: file->strm.filebz2 = BZ2_bzopen(filename, truemode); error = (file->strm.filebz2 == NULL); break; #endif #ifdef ZOLTAN_LZMA case LZMA: break; #endif default: break; } } if (error) { safe_free((void **)(void *) &file); return (NULL); } if (type != STANDARD) { file->buffer = (char*) malloc(BUFF_SIZE); if (file->buffer == NULL) { safe_free((void **)(void *) &file); return (NULL); } } return (file); }
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 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 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; }
// This now supports gzip or bzip2 to compress with. long CompressLogFiles( char **logfiles, long n, int type, int deletelog ) { long count, dataread, dataout, perc; char newlogname[512], *logFN; long failed = 1; for( count=0; count<n ; count++){ logFN = logfiles[count]; if ( strstr( logFN, ".gz" ) && type==0 ) continue; if ( !IsURL(logFN) ) { void *fp; void *outfp; char *ram, *p; long blocksize = 1024*32; StopAll( 0 ); if ( ram = (char*)malloc( blocksize ) ){ __int64 dataleft, length; if ( fp=(void*)LogOpen( logFN, &length ) ){ int ret; sprintf( newlogname, "%s.gz", logFN ); switch( type ){ default: case COMPRESS_GZIP : sprintf( newlogname, "%s.gz", logFN ); if ( p = strstr( newlogname, ".bz2" ) ) mystrcpy( p, ".gz" ); outfp = gzopen( newlogname, "wb6" ); break; #ifdef _BZLIB_H // bzip2 is about 15X slower for 1/2 the size files, level6 is the best time-vs-size level roughly case COMPRESS_BZIP2 : sprintf( newlogname, "%s.bz2", logFN ); if ( p = strstr( newlogname, ".gz" ) ) mystrcpy( p, ".bz2" ); outfp = BZ2_bzopen( newlogname, "wb6" ); break; #endif } dataout = 0; if ( outfp ){ dataleft = length; dataread = 1; while( dataread>0 && !IsStopped() ){ //OutDebugs( "dataleft = %d", dataleft ); perc = (long)(100*((length-dataleft)/(float)length)); //sprintf( msgtext, "Compressing %s ...", 100*((length-dataleft)/length) ); ShowProgress( perc, FALSE, NULL ); StatusSetID( IDS_COMPRESSING, perc, dataout/1024 ); dataread = LogRead( fp, logFN, ram, blocksize ); if ( dataread>0 ) { dataleft -= dataread; if ( type == COMPRESS_GZIP ) dataout+=gzwrite( outfp, ram , dataread ); #ifdef _BZLIB_H if ( type == COMPRESS_BZIP2 ) dataout+=BZ2_bzwrite( outfp, ram , dataread ); #endif } } if ( type == COMPRESS_GZIP ) gzclose( outfp ); #ifdef _BZLIB_H if ( type == COMPRESS_BZIP2 ) BZ2_bzclose( outfp ); #endif if ( !IsStopped() ){ __int64 newsize; FILE *newfp; failed = 0; if ( (newfp = fopen( newlogname, "ab+" )) ) { newsize = GetFPLength( newfp ); if ( type == COMPRESS_BZIP2 ){ long value; value = 0; fwrite( &value, 1, 4, newfp ); value = (long)length; fwrite( &value, 1, 4, newfp ); } fclose(newfp); } StatusSetID( IDS_COMPRESSDONE, dataout/1024, newsize/1024, 100*newsize/dataout ); } else StatusSet( "Stopped" ); } ret = LogClose( (long)fp, logFN ); if ( deletelog && !failed){ remove( logFN ); } } free( ram ); } } } return failed; }