void gt_xbzwrite(BZFILE *file, void *buf, unsigned len) { gt_assert(buf && len); if (BZ2_bzwrite(file, buf, len) != len) { fprintf(stderr, "cannot write it compressed file\n"); exit(EXIT_FAILURE); } }
int bzstreambuf::flush_buffer() { // Separate the writing of the buffer from overflow() and // sync() operation. int w = pptr() - pbase(); if ( BZ2_bzwrite( file, pbase(), w) != w) return EOF; pbump( -w); return w; }
static ssize_t bzdWrite(FDSTACK_t fps, const void * buf, size_t count) { BZFILE *bzfile = fps->fp; ssize_t rc; rc = BZ2_bzwrite(bzfile, (void *)buf, count); if (rc == -1) { int zerror = 0; fps->errcookie = BZ2_bzerror(bzfile, &zerror); } return rc; }
static ssize_t bzdWrite(FD_t fd, const void * buf, size_t count) { BZFILE *bzfile; ssize_t rc; bzfile = bzdFileno(fd); rc = BZ2_bzwrite(bzfile, (void *)buf, count); if (rc == -1) { int zerror = 0; fd->errcookie = BZ2_bzerror(bzfile, &zerror); } return rc; }
static void compress_bzip2(int fd_in, int fd_out, struct compress_params *params, const char *desc) { char buffer[DPKG_BUFFER_SIZE]; char combuf[6]; int bz_errnum; BZFILE *bzfile; snprintf(combuf, sizeof(combuf), "w%d", params->level); bzfile = BZ2_bzdopen(fd_out, combuf); if (bzfile == NULL) ohshit(_("%s: error binding output to bzip2 stream"), desc); for (;;) { int actualread, actualwrite; actualread = fd_read(fd_in, buffer, sizeof(buffer)); if (actualread < 0) ohshite(_("%s: internal bzip2 read error"), desc); if (actualread == 0) /* EOF. */ break; actualwrite = BZ2_bzwrite(bzfile, buffer, actualread); if (actualwrite != actualread) { const char *errmsg = BZ2_bzerror(bzfile, &bz_errnum); if (bz_errnum == BZ_IO_ERROR) errmsg = strerror(errno); ohshit(_("%s: internal bzip2 write error: '%s'"), desc, errmsg); } } BZ2_bzWriteClose(&bz_errnum, bzfile, 0, NULL, NULL); if (bz_errnum != BZ_OK) { const char *errmsg = _("unexpected bzip2 error"); if (bz_errnum == BZ_IO_ERROR) errmsg = strerror(errno); ohshit(_("%s: internal bzip2 write error: '%s'"), desc, errmsg); } /* Because BZ2_bzWriteClose has done a fflush on the file handle, * doing a close on the file descriptor associated with it should * be safe™. */ if (close(fd_out)) ohshite(_("%s: internal bzip2 write error"), desc); }
size_t AFILE_afwrite(void *ptr, size_t size, size_t count, AFILE *afp) { switch(afp->c) { case AFILE_NO_COMPRESSION: return fwrite(ptr, size, count, afp->fp); #ifndef DISABLE_BZLIB case AFILE_BZ2_COMPRESSION: return BZ2_bzwrite(afp->bz2, ptr, size*count); #endif case AFILE_GZ_COMPRESSION: return gzwrite(afp->gz, ptr, size*count); default: AFILE_print_error("Could not recognize compresssion\n"); break; } return 0; }
static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count) { size_t wrote = 0; struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; do { int just_wrote; size_t remain = count - wrote; int to_write = (int)(remain <= INT_MAX ? remain : INT_MAX); just_wrote = BZ2_bzwrite(self->bz_file, (char*)buf, to_write); if (just_wrote < 1) { break; } wrote += just_wrote; } while (wrote < count); return wrote; }
int CFile::PImpl::write(const void *buf, size_t size) { int tp = cl_type; int ret = -1; if (tp != CLF_TYPE_INVALID) { if (tp == CLF_TYPE_PLAIN) { ret = fwrite(buf, size, 1, cl_plain); } #ifdef USE_ZLIB if (tp == CLF_TYPE_GZIP) { ret = gzwrite(cl_gz, buf, size); } #endif // USE_ZLIB #ifdef USE_BZ2LIB if (tp == CLF_TYPE_BZIP2) { ret = BZ2_bzwrite(cl_bz, const_cast<void *>(buf), size); } #endif // USE_BZ2LIB } else { errno = EBADF; } return ret; }
static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) { struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract; return BZ2_bzwrite(self->bz_file, (char*)buf, count); }
static ssize_t cookie_bzwrite(void *cookie, const char *buf, size_t nbytes) { return BZ2_bzwrite((BZFILE *)cookie, (char *)buf, nbytes); }
int64_t BZ2File::writeImpl(const char * buf, int64_t length) { assert(m_bzFile); return BZ2_bzwrite(m_bzFile, (char *)buf, length); }
static int bzputc(int c, BZFILE *bzfile) { char cc = (char) c; /* required for big endian systems */ return BZ2_bzwrite(bzfile, &cc, 1) == 1 ? cc : -1; }
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; }
/** * I have rewritten this function so it can be called from other places; * I have also rewritten it to not use a patch file but instead a passed * buffer; the bsdiff patch is already in memory. * * ryan, 7.2.09 */ int bsdiff_create_patch(char *patchFile, char *oldFile, char *newFile) { FILE* fd = NULL; BZFILE * bz = NULL; u_char *old,*pnew; off_t oldsize,newsize; off_t *I,*V; off_t scan,pos,len; off_t lastscan,lastpos,lastoffset; off_t oldscore,scsc; off_t s,Sf,lenf,Sb,lenb; off_t overlap,Ss,lens; off_t i; off_t dblen,eblen; u_char *db,*eb; u_char buf[8]; u_char header[32]; int bzerror; /*if(argc!=4) err(1,"usage: %s oldfile newfile patchfile\n",argv[0]);*/ /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ if(((fd = fopen(oldFile, "rb")) <= 0) || (fseek(fd, 0, SEEK_END) != 0) || ((oldsize = ftell(fd)) == 0) || ((old = (u_char *) malloc(oldsize + 1)) == NULL) || (fseek(fd, 0, SEEK_SET) != 0) || (fread(old,1, oldsize, fd) != oldsize) || (fclose(fd) == -1)) err(1, "%s", oldFile); if(((I = (off_t*) malloc((oldsize + 1) * sizeof(off_t))) == NULL) || ((V = (off_t*) malloc((oldsize + 1) * sizeof(off_t))) == NULL)) err(1, NULL); qsufsort(I, V, old, oldsize); free(V); /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ if(((fd = fopen(newFile, "rb")) < 0) || (fseek(fd, 0, SEEK_END) != 0) || ((newsize = ftell(fd)) == 0) || ((pnew = (u_char *) malloc(newsize + 1)) == NULL) || (fseek(fd, 0, SEEK_SET) != 0) || (fread(pnew, 1, newsize, fd) != newsize) || (fclose(fd) == -1)) err(1, "%s", newFile); if(((db=(u_char*)malloc(newsize+1))==NULL) || ((eb=(u_char*)malloc(newsize+1))==NULL)) err(1,NULL); dblen=0; eblen=0; fd = fopen(patchFile, "wb"); if(fd == NULL) err(1,"%s",patchFile); /* Header is 0 8 "BSDIFF40" 8 8 length of bzip2ed ctrl block 16 8 length of bzip2ed diff block 24 8 length of new file */ /* File is 0 32 Header 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ memcpy(header,"BSDIFF40",8); memset(header+8,0,24); if(fwrite(header,1,32,fd)!=32) err(1,"%s",patchFile); scan=0;len=0; lastscan=0;lastpos=0;lastoffset=0; bz = BZ2_bzWriteOpen ( &bzerror, fd, blockSize100k, 0, 30); if (bzerror != BZ_OK) err(1, "Problem bzWriteOpen"); while(scan<newsize) { oldscore=0; for(scsc=scan+=len;scan<newsize;scan++) { len=search(I,old,oldsize,pnew+scan,newsize-scan, 0,oldsize,&pos); for(;scsc<scan+len;scsc++) if((scsc+lastoffset<oldsize) && (old[scsc+lastoffset] == pnew[scsc])) oldscore++; if(((len==oldscore) && (len!=0)) || (len>oldscore+8)) break; if((scan+lastoffset<oldsize) && (old[scan+lastoffset] == pnew[scan])) oldscore--; }; if((len!=oldscore) || (scan==newsize)) { s=0;Sf=0;lenf=0; for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { if(old[lastpos+i]==pnew[lastscan+i]) s++; i++; if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; }; lenb=0; if(scan<newsize) { s=0;Sb=0; for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) { if(old[pos-i]==pnew[scan-i]) s++; if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; }; }; if(lastscan+lenf>scan-lenb) { overlap=(lastscan+lenf)-(scan-lenb); s=0;Ss=0;lens=0; for(i=0;i<overlap;i++) { if(pnew[lastscan+lenf-overlap+i]== old[lastpos+lenf-overlap+i]) s++; if(pnew[scan-lenb+i]== old[pos-lenb+i]) s--; if(s>Ss) { Ss=s; lens=i+1; }; }; lenf+=lens-overlap; lenb-=lens; }; for(i=0;i<lenf;i++) db[dblen+i]=pnew[lastscan+i]-old[lastpos+i]; for(i=0;i<(scan-lenb)-(lastscan+lenf);i++) eb[eblen+i]=pnew[lastscan+lenf+i]; dblen+=lenf; eblen+=(scan-lenb)-(lastscan+lenf); offtout(lenf,buf); if((8 != BZ2_bzwrite(bz,buf,8)) || (bzerror != BZ_OK)) err(1,"bzwrite"); offtout((scan-lenb)-(lastscan+lenf),buf); if((8 != BZ2_bzwrite(bz,buf,8)) || (bzerror != BZ_OK)) err(1,"bzwrite"); offtout((pos-lenb)-(lastpos+lenf),buf); if((8 != BZ2_bzwrite(bz,buf,8)) || (bzerror != BZ_OK)) err(1,"bzwrite"); lastscan=scan-lenb; lastpos=pos-lenb; lastoffset=pos-scan; }; }; BZ2_bzWriteClose ( &bzerror, bz, 0, NULL, NULL); if((fseek(fd,0,SEEK_END))!=0 || ((len=ftell(fd))==-1)) err(1,"problem 0: %s",patchFile); offtout(len-32,buf); if((fseek(fd,8,SEEK_SET)!=0) || (ftell(fd) != 8) || (fwrite(buf,1,8,fd)!=8)) err(1,"problem 1: %s",patchFile); offtout(newsize,buf); if((fseek(fd,24,SEEK_SET)!=0) || (ftell(fd) != 24)|| (fwrite(buf,1,8,fd)!=8)) err(1,"problem 2:%s",patchFile); if(fseek(fd,0,SEEK_END)!=0) err(1,"problem 3: %s",patchFile); bz = BZ2_bzWriteOpen ( &bzerror, fd, blockSize100k, 0, 30); if (bzerror != BZ_OK) err(1, "Problem bzWriteOpen"); if((dblen != BZ2_bzwrite(bz,db,dblen)) || (bzerror != BZ_OK)) { // printf("%s", BZ2_bzerror ( bz, &bzerror )); err(1,"bzwrite"); } BZ2_bzWriteClose ( &bzerror, bz, 0, NULL, NULL); if((fseek(fd,0,SEEK_END)!=0) || ((newsize=ftell(fd))==-1)) err(1,"problem 4: %s",patchFile); offtout(newsize-len,buf); if((fseek(fd,16,SEEK_SET)!=0) || (ftell(fd)!=16) || (fwrite(buf,1,8,fd)!=8)) err(1,"problem 5:%s",patchFile); if(fseek(fd,0,SEEK_END)!=0 || (ftell(fd) == -1)) err(1,"problem 6: %s",patchFile); bz = BZ2_bzWriteOpen ( &bzerror, fd, blockSize100k, 0, 30); if (bzerror != BZ_OK) err(1, "Problem bzWriteOpen"); if((eblen != BZ2_bzwrite(bz,eb,eblen)) || (bzerror != BZ_OK)) err(1,"bzwrite"); BZ2_bzWriteClose ( &bzerror, bz, 0, NULL, NULL); fclose(fd); free(db); free(eb); free(I); free(old); free(pnew); return 0; }
static int bzputs(const char *str, BZFILE *bzfile) { int len = strlen(str); return BZ2_bzwrite(bzfile, (char*) str, len) == len ? len : -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; }
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; }