Bz2LineWriter::Bz2LineWriter(const string& fileName, bool plain) : bzerror(BZ_OK), uncompressed(plain || fileName == "-" || fileName.find(".bz2") == string::npos), out(NULL), open(true) {
		if (uncompressed) {
//			cerr << "UNCOMPRESSED OUTPUT!!!" << endl;
			if (fileName != "-") {
				uncompressedFile.open(fileName.c_str());
				if (uncompressedFile.fail()) {
					/* handle error */
					wcout << L"Could not open the stupid file for writing!!!" << endl;
					exit(3);
				}
				out = new ostream(uncompressedFile.rdbuf());
			} else
				out = new ostream(cout.rdbuf());
		} else {
//			cerr << "COMPRESSED OUTPUT!!!" << endl;
			plainHandle = fopen(fileName.c_str(), "w");
			if (!plainHandle) {
				/* handle error */
				wcout << L"Could not open the stupid file for writing!!!" << endl;
				exit(5);
			}
			bzip2Handle = BZ2_bzWriteOpen(&bzerror, plainHandle, 9, 0, 0);
			if (bzerror != BZ_OK) {
				BZ2_bzWriteClose(&bzerror, bzip2Handle, 0, NULL, NULL);
				/* handle error */
				wcout << L"Trouble writing to the stupid file!!!" << endl;
				exit(6);
			}
			
			bzerror = BZ_OK;
		}
	}
Beispiel #2
0
void BzipOutputStream::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_bzWriteOpen(&err, m_cf
		,  9   // blocksize100k
		,  0   // verbosity
		, 30   // workFactor, default=30
		);
	if (0 == m_fp)
	{
		std::ostringstream oss;
		oss << "fd=" << fd << ", mode=" << mode << ", err=" << strbzerr(err);
		throw OpenFileException("<fd>", oss.str().c_str());
	}
}
Beispiel #3
0
static void write_bz2(int f, int fd, const char *arg)
{
    BZFILE* b;
    int     nBuf;
    char    buf[BUFFER_SIZE];
    int     bzerror;

    b = BZ2_bzWriteOpen(&bzerror, fdopen(f,"wb"), 9, 0, 0);
    if (bzerror != BZ_OK)
    {
        BZ2_bzWriteClose(&bzerror, b, 0,0,0);
        // error
        // the writer will get smitten with sigpipe
        close(fd);
        return;
    }

    bzerror = BZ_OK;
    while ((nBuf=read(fd, buf, BUFFER_SIZE))>0)
    {
        BZ2_bzWrite(&bzerror, b, buf, nBuf);
        if (bzerror!=BZ_OK)
        {
            BZ2_bzWriteClose(&bzerror, b, 0,0,0);
            close(fd);
            return;
        }
    }
    BZ2_bzWriteClose(&bzerror, b, 0,0,0);
    close(fd);
}
Beispiel #4
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);
 }
Beispiel #5
0
static void open_compressed_write(pcu_file* pf)
{
  int bzerror;
  int verbosity = 0;
  int blockSize100k = 9;
  int workFactor = 30;
  pf->bzf = BZ2_bzWriteOpen(&bzerror, pf->f, blockSize100k, verbosity, workFactor);
  if (bzerror != BZ_OK)
    reel_fail("BZ2_bzWriteOpen failed with code %d", bzerror);
}
void BZ2Stream::startWrite() {
    bzfile_ = BZ2_bzWriteOpen(&bzerror_, getFilePointer(), block_size_100k_, verbosity_, work_factor_);

    switch (bzerror_) {
        case BZ_OK: break;
        default: {
            BZ2_bzWriteClose(&bzerror_, bzfile_, 0, NULL, NULL);
            throw BagException("Error opening file for writing compressed stream");
        }
    }

    setCompressedIn(0);
}
Beispiel #7
0
int fsWriteCompressed(FILESYSTEM_FILE *f, void *data, int len) {
	BZFILE *bzf;
	int err;

	if (!f)
		return -1;
	
	if (!(bzf = BZ2_bzWriteOpen(&err, f->fp, 9, 0, 0)))
		return -1;
	BZ2_bzWrite(&err, bzf, data, len);
	BZ2_bzWriteClose(&err, bzf, 0, NULL, NULL);

	f->pos = ftell(f->fp);

	return 0;
}
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;
		}
	}
};
Beispiel #9
0
  CoinBzip2FileOutput (const std::string &fileName):
    CoinFileOutput (fileName), f_ (0), bzf_ (0)
  {
    int bzError = BZ_OK;

    f_ = fopen (fileName.c_str (), "w");

    if (f_ != 0)
      bzf_ = BZ2_bzWriteOpen (&bzError, f_,
			      9, /* Number of 100k blocks used for compression.
				    Must be between 1 and 9 inclusive. As 9
				    gives best compression and I guess we can
				    spend some memory, we use it. */
			      0, /* verbosity */
			      30 /* suggested by bzlib manual */ );

    if (f_ == 0 || bzError != BZ_OK || bzf_ == 0)
      throw CoinError ("Could not open file for writing!",
		       "CoinBzip2FileOutput",
		       "CoinBzip2FileOutput");
  }
Beispiel #10
0
int main() {
  enum {
    nBuf = 100
  };
  char buf[nBuf];
  for (int i = 0; i < nBuf; ++i) {
    buf[i] = static_cast<char>(i);
  }

  int bzerror(0);
  int abandon(0);
  unsigned int nbytes_in(0);
  unsigned int nbytes_out(0);

  FILE* f = fopen("myfile.bz2", "w");
  if (!f) {
    std::cerr << "fopen failed" << std::endl;
    return EXIT_FAILURE;
  }

  BZFILE* b = BZ2_bzWriteOpen(&bzerror, f, 9, 0, 0);
  if (bzerror != BZ_OK) {
    BZ2_bzWriteClose(&bzerror, b, abandon, &nbytes_in, &nbytes_out);
    std::cerr << "BZ2_bzWriteOpen failed" << std::endl;
    return EXIT_FAILURE;
  }

  BZ2_bzWrite(&bzerror, b, buf, nBuf);
  if (bzerror == BZ_IO_ERROR) {
    BZ2_bzWriteClose(&bzerror, b, abandon, &nbytes_in, &nbytes_out);
    std::cerr << "BZ_IO_ERROR (write)" << std::endl;
    return EXIT_FAILURE;
  }

  BZ2_bzWriteClose(&bzerror, b, abandon, &nbytes_in, &nbytes_out);
  if (bzerror == BZ_IO_ERROR) {
    std::cerr << "BZ_IO_ERROR (close)" << std::endl;
    return EXIT_FAILURE;
  }
}
Beispiel #11
0
// only can call on unopened BzipOutputStream
void BzipOutputStream::open(const char* fpath, const char* mode)
{
	assert(0 == m_fp);

	int err;
	m_cf = fopen(fpath, mode);
	if (0 == m_cf)
	{
		std::ostringstream oss;
		oss << "mode=" << mode;
		throw OpenFileException(fpath, oss.str().c_str());
	}
	m_fp = BZ2_bzWriteOpen(&err, m_cf
		,  9   // blocksize100k
		,  0   // verbosity
		, 30   // workFactor, default=30
		);
	if (0 == m_fp)
	{
		std::ostringstream oss;
		oss << "mode=" << mode << ", err=" << strbzerr(err);
		throw OpenFileException(fpath, oss.str().c_str());
	}
}
Beispiel #12
0
	bool Open(const char* patch_file)
	{
		Close();
		_patch = fopen(patch_file, "wb");
		_stubname = patch_file;
		_stubname += ".stub";
		_stub = fopen(_stubname.c_str(), "wb");
		if(_patch==NULL || _stub==NULL) return false;
		// [HEADER]
		// 0	8 "JPATCH10"
		// 8	4 ctrl block offset
		// 12	4 data block offset
		// [FILE]
		// header
		// ctrl block
		// data block
		if(fwrite("JPATCH10", 1, 8, _patch)!=8) return false;
		if(fwrite("JPATCH10", 1, 8, _patch)!=8) return false;
		int bzerror;
		_bzpatch = BZ2_bzWriteOpen(&bzerror, _patch, 9, 0, 0);
		if(_bzpatch==NULL) return false;
		_Op = OPERATOR_NA;
		return true;
	}
Beispiel #13
0
AFILE *AFILE_afdopen(int filedes, const char *mode, int32_t compression) 
{
	AFILE *afp = NULL;
	if(NULL == strchr(mode, 'r') && NULL == strchr(mode, 'w')) {
		AFILE_print_error("Improper mode");
	}

	afp = calloc(1, sizeof(AFILE));
	if(NULL == afp) AFILE_print_error("Could not allocate memory\n");
	afp->fp=NULL;
#ifndef DISABLE_BZLIB 
	afp->bz2=NULL;
	afp->n_unused=0;
#endif
	afp->gz=NULL;
	afp->c=compression;

	switch(afp->c) {
		case AFILE_NO_COMPRESSION:
			afp->fp = fdopen(filedes, mode);
			if(NULL == afp->fp) {
				free(afp); 
				return NULL;
			}
			break;
#ifndef DISABLE_BZLIB 
		case AFILE_BZ2_COMPRESSION:
			afp->fp = fdopen(filedes, mode);
			if(NULL == afp->fp) {
				free(afp); 
				return NULL;
			}
			if(NULL != strchr(mode, 'r')) {
				afp->open_type = AFILE_BZ2_READ;
				afp->bz2 = BZ2_bzReadOpen(&afp->bzerror, afp->fp, 0, 0, afp->unused, afp->n_unused);
				if(NULL == afp->bz2) {
					free(afp); 
					return NULL;
				}
			}
			else {
				afp->open_type = AFILE_BZ2_WRITE;
				// 900K blockSize100k
				// 30 workFactor
				afp->bz2 = BZ2_bzWriteOpen(&afp->bzerror, afp->fp, 9, 0, 30); 
			}
#endif
		case AFILE_GZ_COMPRESSION:
			afp->gz = gzdopen(filedes, mode);
			if(NULL == afp->gz) {
				free(afp); 
				return NULL;
			}
			break;
		default:
			AFILE_print_error("Could not recognize compresssion\n");
			break;
	}

	return afp;
}
Beispiel #14
0
int mpk_package_packmpk(struct mpk_pkginfo *pkg, const char *srcdir,
    const char *outdir)
{
    TAR *tar;
    BZFILE *bz2;
    FILE *tbz2_file;
    int tar_fd;
    int bzerr;
    char src[PATH_MAX + 1];
    char dst[PATH_MAX + 1];
    char tar_fpath[PATH_MAX + 1];
    char tbz2_fpath[PATH_MAX + 1];
    unsigned char buffer[CHUNKSIZE];
    int size;
    struct mpk_file *file;

    /* create tar */

    sprintf(tar_fpath, "/tmp/%s_files.tar", pkg->name);
    if (access(tar_fpath, F_OK) == 0)
        if (unlink(tar_fpath) != 0)
            goto err0;

    if (tar_open(&tar, tar_fpath, NULL, O_WRONLY|O_CREAT, 0644, 0) != 0)
        goto err0;

    for (file = pkg->tool.lh_first; file; file = file->items.le_next) {
        sprintf(src, "%s/tool/%s", srcdir, file->name);
        sprintf(dst, "tool/%s", file->name);
        if (tar_append_tree(tar, src, dst) != 0)
            goto err2;
    }

    for (file = pkg->data.lh_first; file; file = file->items.le_next) {
        if (file->type == MPK_FILE_TYPE_R || file->type == MPK_FILE_TYPE_EXE
                || file->type == MPK_FILE_TYPE_W
                || file->type == MPK_FILE_TYPE_S) {
            sprintf(src, "%s/data/%s", srcdir, file->name);
            sprintf(dst, "data/%s", file->name);
            if (tar_append_tree(tar, src, dst) != 0)
                goto err2;
        }
    }

    sprintf(src, "%s/manifest.txt", srcdir);
    if (tar_append_file(tar, src, "manifest.txt") != 0)
        goto err2;

    tar_close(tar);


    /* compress using bz2 */

    int version_str_len = mpk_version_serializedsize(&pkg->version);
    char *version_str;
    if (!(version_str = malloc(version_str_len + 1)))
        goto err2;
    if (mpk_version_serialize(version_str, NULL, version_str_len, &pkg->version)
            != MPK_SUCCESS) {
        free(version_str);
        goto err2;
    }
    version_str[version_str_len] = 0;

    sprintf(tbz2_fpath, "%s/%s-%s.mpk", outdir, pkg->name, version_str);
    free(version_str);
    printf("path:%s\n", tbz2_fpath);

    if ((tar_fd = open(tar_fpath, O_RDONLY)) == -1)
        goto err1;

    if ((tbz2_file = fopen(tbz2_fpath, "wb")) == NULL)
        goto err3;
    bz2 = BZ2_bzWriteOpen(&bzerr, tbz2_file, 9, 0, 30);
    if (bzerr != BZ_OK)
        goto err4;

    while ((size = read(tar_fd, buffer, CHUNKSIZE)) > 0)
        BZ2_bzWrite(&bzerr, bz2, buffer, size);
    BZ2_bzWriteClose(&bzerr, bz2, 0, NULL, NULL);
    fclose(tbz2_file);
    close(tar_fd);
    if (bzerr != BZ_OK || size < 0)
        goto err1;

    if (unlink(tar_fpath) != 0)
        goto err0;

    return MPK_SUCCESS;

err4:
    fclose(tbz2_file);
err3:
    close(tar_fd);
    goto err1;
err2:
    tar_close(tar);
err1:
    unlink(tar_fpath);
err0:
    return MPK_FAILURE;
}
Beispiel #15
0
//Private methods
bool CtbzPlugin::Compress(const std::string& input, const std::string& output, IProgressbar* callback)
{
  bool Ret = false;
  FILE* Out = fopen(output.c_str(), "wb");

  // Open up the output file
  if(!Out)
  {
    std::cout << i8n("Error out file!") << '\n';
    Ret = false;
  }
  else
  {
    BZFILE* BZ = 0;
    int Err = 0;
    BZ = BZ2_bzWriteOpen(&Err, Out, 9, 0, 90);

    if(Err != BZ_OK)
    {
      std::cout << i8n("Error bzWriteOpen!") << '\n';
      Ret = false;
    }
    else
    {
      // Open up the input file
      std::ifstream In(input.c_str(), std::ios::in | std::ios::binary);

      if(!In.good())
      {
        std::cout << i8n("Error in file!") << '\n';
        Ret = false;
      }
      else
      {
        // Get the file size. (I hate C I/O, so don't use them :D )
        struct stat Info;
        double Total;
        //Try to stat file.
        if(stat(input.c_str(), &Info) == -1)
        {
          std::cout << i8n("Cannot stat ") << input.c_str() << '\n';
          Ret = false;
        }
        else
        {
          char Buffer[4096];
          memset(Buffer, 0, 4096);
          Total = Info.st_size;
          double Done = 0;
          do
          {
            In.read(Buffer, 4096);
            std::streamsize BytesRead = In.gcount();
            Done += BytesRead;
            int Result = static_cast<int>((Done*50)/Total)+50;
            std::string Mess(i8n("bz2 compression of\n"));
            std::stringstream DoneStr;
            DoneStr << (Result-50)*2;
            Mess += output + " :  " + DoneStr.str() + "%";
            bool Continue = callback->UpdateProgress(Mess, false, Result);
            if(!Continue)
              break;
            BZ2_bzWrite(&Err, BZ, Buffer, BytesRead);
          } while(In.good());

          if( In.bad() || !In.eof() )
            Ret = false;
          else
            Ret = true;

          In.close();
        }

        // Close up.
        BZ2_bzWriteClose(&Err, BZ, 0, 0, 0);
        fclose(Out);
        Out = 0;
      }
    }
  }

  return Ret;
}
Beispiel #16
0
static int file_load(char *fn_in, char *infmt, char *outfmt)
{
    char buf_in[BUFLEN], buf_out[BUFLEN];
    FILE *fp_in, *fp_out;
    BZFILE *bzf_in, *bzf_out;
    gzFile gzf_in, gzf_out;
    lzma_stream lzma_str = LZMA_STREAM_INIT;
	struct timeval tv_start, tv_stop;
    int ret, err, fmt_in, fmt_out, sl, buf_in_pos, buf_in_len, buf_out_len;
    uint64_t bytes, usec;

    fp_in = fp_out = bzf_in = bzf_out = gzf_in = gzf_out = NULL;

    fmt_in = str2fmt(infmt);
    fmt_out = str2fmt(outfmt);

    //printf("fmt_in %u fmt_out %u\n", fmt_in, fmt_out);
    if(fmt_in == fmt_out)
    {
    	fprintf(stderr, "fmt_in %d == fmt_out %d. not supported.\n", fmt_in, fmt_out);
    	return -1;
    }

    ret = gettimeofday(&tv_start, NULL);
    if(ret != 0)
    {
    	fprintf(stderr, "gettimeofday failed\n");
    }

    if(fmt_in == FMT_GZIP)
    {
        gzf_in = gzopen(fn_in, "rb");
        if(!gzf_in)
        {
            printf("file_load: gzopen failed\n");
            return -1;
        }
    }
    else
    {
    	fp_in = fopen(fn_in, "rb");
    	if(!fp_in)
        {
        	printf("file_load: fopen failed\n");
        	return -1;
        }

        if(fmt_in == FMT_BZIP2)
        {
            bzf_in = BZ2_bzReadOpen(&err, fp_in, 0, 0, NULL, 0);
            if(!bzf_in)
            {
                fprintf(stderr, "bzReadOpen bzerr %d\n", err);
                fclose(fp_in);
                return -1;
            }
        }
        else if(fmt_in == FMT_XZ)
        {
        	ret = xz_dec_init(&lzma_str);
        	if(ret != LZMA_OK)
            {
            	printf("xz_enc_init failed %d\n", ret);
            	fclose(fp_out);
            	return -1;
            }
        }
    }

    sl = strlen(fn_in);
    fn_in[sl-strlen(infmt)-1] = 0;
    snprintf(buf_out, FILENAME_MAX, "%s.%s", fn_in, outfmt);
    buf_out[FILENAME_MAX-1] = 0;
    fn_in[sl-strlen(infmt)-1] = '.';
    printf("%s -> %s", fn_in, buf_out);

    if(fmt_out == FMT_GZIP)
    {
        gzf_out = gzopen(buf_out, "wb9");
        if(gzf_out == NULL)
        {
            printf("file_load: gzopen failed\n");
            return -1;
        }
    }
    else
    {
        fp_out = fopen(buf_out, "wb");
        if(!fp_out)
        {
        	printf("fp_out fopen failed\n");
            return 1;
        }

        if(fmt_out == FMT_BZIP2)
        {
            bzf_out = BZ2_bzWriteOpen(&err, fp_out, 9, 0, 0);
            if(!bzf_out)
            {
                printf("bzerr %d\n", err);
                fclose(fp_out);
                return -1;
            }
        }
        else if(fmt_out == FMT_XZ)
        {
        	ret = xz_enc_init(&lzma_str);
        	if(ret != LZMA_OK)
            {
            	printf("xz_enc_init failed %d\n", ret);
            	fclose(fp_out);
            	return -1;
            }
        }
    }
    
    buf_in_pos = 0;
    buf_in_len = 0;

    bytes = 0;
    while(1)
    {
    	switch(fmt_in)
        {
        	case FMT_BZIP2:
        	{
                ret = BZ2_bzRead(&err, bzf_in, buf_out, BUFLEN);
                break;
            }

            case FMT_GZIP:
            {
                ret = gzread(gzf_in, buf_out, BUFLEN);
                err = BZ_OK;
                break;
            }

            case FMT_RAW:
            {
            	ret = fread(buf_out, BUFLEN, 1, fp_in);
                err = BZ_OK;
                break;
            }

            case FMT_XZ:
            {
            	if(buf_in_len == 0)
                {
                	ret = fread(buf_in, BUFLEN, 1, fp_in);
                }

            	ret = xz_dec_read(&lzma_str, buf_in + buf_in_pos, &buf_in_len, buf_out, &buf_out_len);
            	break;
            }

            default:
            {
            	printf("unknown fmt_in\n");
            	ret = 0;
            	break;
            }
        }

        /* process here */
        if((ret > 0) && ((err == BZ_OK) || (err == BZ_STREAM_END)))
        {
            bytes += ret;

            switch(fmt_out)
            {
                case FMT_BZIP2:
                {
                    BZ2_bzWrite(&err, bzf_out, buf_out, ret);
                    break;
                }

                case FMT_GZIP:
                {
                    ret = gzwrite(gzf_out, buf_out, ret);
                    break;
                }

                case FMT_XZ:
                {
                	ret = xz_enc_write(&lzma_str, buf_out, ret, fp_out);
                	break;
                }

                case FMT_RAW:
                {
                    ret = fwrite(buf_out, ret, 1, fp_out);
                    break;
                }

                case FMT_NULL:
                default:
                {
                    break;
                }
            }
        }
        else
        {
        	break;
        }
    }

    switch(fmt_in)
    {
    	case FMT_BZIP2:
    	{
            BZ2_bzReadClose(&err, bzf_in);
            if(err != BZ_OK)
            {
                printf("close bzerr %d\n", err);
            }
    
            fclose(fp_in);

    		break;
        }

        case FMT_GZIP:
        {
            gzclose(gzf_in);
        	break;
        }

        case FMT_RAW:
        {
            fclose(fp_in);
        	break;
        }

        default:
        {
        	break;
        }
    }

    switch(fmt_out)
    {
    	case FMT_BZIP2:
    	{
            BZ2_bzWriteClose(&err, bzf_out, 0, NULL, NULL);
            if(err != BZ_OK)
            {
                printf("close bzerr %d\n", err);
            }
    
            fclose(fp_out);
    		break;
        }

        case FMT_GZIP:
        {
            gzclose(gzf_out);
        	break;
        }

        case FMT_XZ:
        {
        	ret = xz_enc_exit(&lzma_str, fp_out);
        	fclose(fp_out);
        	break;
        }

        case FMT_RAW:
        {
            fclose(fp_out);
        	break;
        }

        default:
        {
        	break;
        }
    }

    ret = gettimeofday(&tv_stop, NULL);
    if(ret != 0)
    {
    	printf("WARN: gettimeofday failed\n");
    }

    usec = (tv_stop.tv_sec - tv_start.tv_sec) * 1000000 + (tv_stop.tv_usec - tv_start.tv_usec);

    printf(" [%" PRIu64 " bytes; %" PRIu64 " MiB/s]\n", bytes, bytes / usec);

    return 0;
}
Beispiel #17
0
int bsdiff(u_char* old, off_t oldsize, u_char* newp, off_t newsize, FILE* pf)
{
	size_t offset;
	off_t *I=NULL,*V=NULL;
	off_t scan,pos = 0,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=NULL,*eb=NULL;
	u_char buf[8];
	u_char header[32];
	BZFILE * pfbz2=NULL;
	int bz2err;
	unsigned int nbytes_in;
	unsigned int nbytes_out;


	if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
		((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) {
		goto error;
	}

	qsufsort(I,V,old,oldsize);

	free(V);
	V = NULL;

	if(((db=malloc(newsize+1))==NULL) ||
		((eb=malloc(newsize+1))==NULL)) {
		goto error;
	}
	dblen=0;
	eblen=0;

	offset = ftell(pf);

	/* Header is
		0	8	length of newp file
		8	8	length of bzip2ed ctrl block
		16	8	length of bzip2ed diff block
		24	8	length of bzip2ed extra block */
	/* File is
		0	32	Header
		32	??	Bzip2ed ctrl block
		??	??	Bzip2ed diff block
		??	??	Bzip2ed extra block */
	memcpy(header, "BSDIFFXX", 8);
	offtout(0, header + 8);
	offtout(0, header + 16);
	offtout(newsize, header + 24);
	if (fwrite(header, 32, 1, pf) != 1) {
		printf("fwrite(%s)", "patch_file");
		goto error;
	}

	/* Compute the differences, writing ctrl as we go */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) {
		printf("BZ2_bzWriteOpen, bz2err = %d", bz2err);
		goto error;
	}
	scan=0;len=0;
	lastscan=0;lastpos=0;lastoffset=0;
	while(scan<newsize) {
		oldscore=0;

		for(scsc=scan+=len;scan<newsize;scan++) {
			len=search(I,old,oldsize,newp+scan,newsize-scan,
					0,oldsize,&pos);

			for(;scsc<scan+len;scsc++)
			if((scsc+lastoffset<oldsize) &&
				(old[scsc+lastoffset] == newp[scsc]))
				oldscore++;

			if(((len==oldscore) && (len!=0)) || 
				(len>oldscore+8)) break;

			if((scan+lastoffset<oldsize) &&
				(old[scan+lastoffset] == newp[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]==newp[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]==newp[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(newp[lastscan+lenf-overlap+i]==
					   old[lastpos+lenf-overlap+i]) s++;
					if(newp[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]=newp[lastscan+i]-old[lastpos+i];
			for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
				eb[eblen+i]=newp[lastscan+lenf+i];

			dblen+=lenf;
			eblen+=(scan-lenb)-(lastscan+lenf);

			offtout(lenf,buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK) {
				printf("BZ2_bzWrite, bz2err = %d", bz2err);
				goto error;
			}

			offtout((scan-lenb)-(lastscan+lenf),buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK) {
				printf("BZ2_bzWrite, bz2err = %d", bz2err);
				goto error;
			}

			offtout((pos-lenb)-(lastpos+lenf),buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK) {
				printf("BZ2_bzWrite, bz2err = %d", bz2err);
				goto error;
			}

			lastscan=scan-lenb;
			lastpos=pos-lenb;
			lastoffset=pos-scan;
		};
	};

	BZ2_bzWriteClose(&bz2err, pfbz2, 0, &nbytes_in, &nbytes_out);
	pfbz2 = NULL;
	if (bz2err != BZ_OK) {
		printf("BZ2_bzWriteClose, bz2err = %d", bz2err);
		goto error;
	}

	/* Compute size of compressed ctrl data */
	if ((len = ftell(pf)) == -1) {
		printf("ftello");
		goto error;
	}
	offtout((off_t)(len-32-offset), header + 8);
	printf("--ctrl data %d %d %d\n", len-32-offset, nbytes_in, nbytes_out);

	/* Write compressed diff data */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) {
		printf("BZ2_bzWriteOpen, bz2err = %d", bz2err);
		goto error;
	}
	BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
	if (bz2err != BZ_OK) {
		printf("BZ2_bzWrite, bz2err = %d", bz2err);
		goto error;
	}
	BZ2_bzWriteClose(&bz2err, pfbz2, 0, &nbytes_in, &nbytes_out);
	pfbz2 = NULL;
	if (bz2err != BZ_OK) {
		printf("BZ2_bzWriteClose, bz2err = %d", bz2err);
		goto error;
	}

	/* Compute size of compressed diff data */
	if ((newsize = ftell(pf)) == -1) {
		printf("ftello");
		goto error;
	}
	offtout(newsize - len, header + 16);
	printf("--diff data %d %d %d\n", newsize - len, nbytes_in, nbytes_out);

	/* Write compressed extra data */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) {
		printf("BZ2_bzWriteOpen, bz2err = %d", bz2err);
		goto error;
	}
	BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
	if (bz2err != BZ_OK) {
		printf("BZ2_bzWrite, bz2err = %d", bz2err);
		goto error;
	}
	BZ2_bzWriteClose(&bz2err, pfbz2, 0, &nbytes_in, &nbytes_out);
	pfbz2 = NULL;
	if (bz2err != BZ_OK) {
		printf("BZ2_bzWriteClose, bz2err = %d", bz2err);
		goto error;
	}
	if ((len = ftell(pf)) == -1) {
		printf("ftello");
		goto error;
	}
	printf("--extra data %d %d %d\n", len - newsize, nbytes_in, nbytes_out);
	printf("--total %d\n", len - offset);

	/* Seek to the beginning, write the header, and close the file */
	if (fseek(pf, (long)(0+offset), SEEK_SET)) {
		printf("fseeko");
		goto error;
	}
	if (fwrite(header, 32, 1, pf) != 1) {
		printf("fwrite(%s)", "patch_file");
		goto error;
	}
	fseek(pf, len, SEEK_SET);
	/* Free the memory we used */
	free(db);
	free(eb);
	free(I);

	return 0;

error:
	if(pfbz2) BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
	if(I) free(I);
	if(V) free(V);
	if(db) free(db);
	if(eb) free(eb);
	return -1;
}
Beispiel #18
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;
}
Beispiel #19
0
int
_nrrdEncodingBzip2_write(FILE *file, const void *_data, size_t elNum,
                         const Nrrd *nrrd, NrrdIoState *nio) {
  char me[]="_nrrdEncodingBzip2_write", err[BIFF_STRLEN];
#if TEEM_BZIP2
  size_t bsize, total_written, block_size;
  int bs, bzerror=BZ_OK;
  char *data;
  BZFILE* bzfout;

  bsize = nrrdElementSize(nrrd)*elNum;

  /* Set compression block size. */
  if (1 <= nio->bzip2BlockSize && nio->bzip2BlockSize <= 9) {
    bs = nio->bzip2BlockSize;
  } else {
    bs = 9;
  }
  /* Open bzfile for writing. Verbosity and work factor are set
     to default values. */
  bzfout = BZ2_bzWriteOpen(&bzerror, file, bs, 0, 0);
  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error opening BZFILE: %s", me, 
            BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    BZ2_bzWriteClose(&bzerror, bzfout, 0, NULL, NULL);
    return 1;
  }

  /* bzip2 can handle data sizes up to INT_MAX, so we can't just 
     pass in the bsize, because it might be too large for an int.
     Therefore it must be read in chunks if the bsize is larger 
     than INT_MAX. */
  if (bsize <= INT_MAX) {
    block_size = bsize;
  } else {
    block_size = INT_MAX;
  }

  /* This counter will help us to make sure that we write as much data
     as we think we should. */
  total_written = 0;
  /* Pointer to the blocks as we write them. */
  data = (char *)_data;
  
  /* Ok, now we can begin writing. */
  bzerror = BZ_OK;
  while (bsize - total_written > block_size) {
    BZ2_bzWrite(&bzerror, bzfout, data, block_size);
    if (BZ_OK != bzerror) break;
    /* Increment the data pointer to the next available spot. */
    data += block_size; 
    total_written += block_size;
  }
  /* write the last (possibly smaller) block when its humungous data;
     write the whole data when its small */
  if (BZ_OK == bzerror) {
    block_size = bsize >= total_written ? bsize - total_written : 0;
    BZ2_bzWrite(&bzerror, bzfout, data, block_size);
    total_written += block_size;
  }

  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error writing to BZFILE: %s",
            me, BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }

  /* Close the BZFILE. */
  BZ2_bzWriteClose(&bzerror, bzfout, 0, NULL, NULL);
  if (BZ_OK != bzerror) {
    sprintf(err, "%s: error closing BZFILE: %s", me,
            BZ2_bzerror(bzfout, &bzerror));
    biffAdd(NRRD, err);
    return 1;
  }
  
  /* Check to see if we got out as much as we thought we should. */
  if (total_written != bsize) {
    sprintf(err, "%s: expected to write " _AIR_SIZE_T_CNV " bytes, but only "
            "wrote " _AIR_SIZE_T_CNV,
            me, bsize, total_written);
    biffAdd(NRRD, err);
    return 1;
  }
  
  return 0;
#else
  AIR_UNUSED(file);
  AIR_UNUSED(_data);
  AIR_UNUSED(elNum);
  AIR_UNUSED(nrrd);
  AIR_UNUSED(nio);
  sprintf(err, "%s: sorry, this nrrd not compiled with bzip2 enabled", me);
  biffAdd(NRRD, err); return 1;
#endif
}
Beispiel #20
0
bool GenPatch(const char* patch_file, CBundle& Origin, CBundle& Lastest)
{
	CBundlePatcher patcher;

	if(!patcher.Open(patch_file)) return false;

	std::map<std::string, CFile>::iterator i;
	for(i=Origin._Files.begin(); i!=Origin._Files.end(); i++)
	{
		if(Lastest._Files.find(i->first)==Lastest._Files.end())
		{
			if(!patcher.DelFile(i->first)) return false;
		}
	}

	for(i=Lastest._Files.begin(); i!=Lastest._Files.end(); i++)
	{
		if(Origin._Files.find(i->first)==Origin._Files.end())
		{
			if(!patcher.AddFile(i->first, "", i->second._MD5)) return false;
			void* mem;
			size_t size, offset;
			if(!ReadFile(i->second, mem, size)) return false;
			offset = ftell(patcher._stub);
			if(!patcher.AddOperatorAppend(size, offset)) return false;
			int bzerror;
			BZFILE* bzf = BZ2_bzWriteOpen(&bzerror, patcher._stub, 9, 0, 0);
			if(bzf==NULL)
			{
				free(mem);
				return false;
			}
			BZ2_bzWrite(&bzerror, bzf, mem, size);
			free(mem);
			if(bzerror!=BZ_OK)
			{
				BZ2_bzWriteClose(&bzerror, bzf, 0, NULL, NULL);
				return false;
			}
			unsigned int inbytes, outbytes;
			BZ2_bzWriteClose(&bzerror, bzf, 0, &inbytes, &outbytes);
			if(bzerror!=BZ_OK)
			{
				return false;
			}
			if(!patcher.AddOperatorEnd()) return false;
		}
		else
		{
			CFile& srcf = Origin._Files[i->first];
			CFile& dstf = i->second;
			if(srcf._MD5!=dstf._MD5)
			{
				if(!patcher.AddFile(i->first, srcf._MD5, dstf._MD5)) return false;
				void* src = NULL;
				void* dst = NULL;
				size_t src_size, dst_size, offset;

				for(size_t i=0; i<dstf._Sections.size(); i++)
				{
					offset = ftell(patcher._stub);

					CSection* s = srcf.GetSectionByMD5(dstf._Sections[i]._MD5);
					if(s==NULL)
					{
						s = srcf.GetSectionByName(dstf._Sections[i]._Name);
					}

					if(s)
					{
						if(s->_MD5==dstf._Sections[i]._MD5)
						{
							// copy
							patcher.AddOperatorCopy(s->_Size, s->_Offset);
						}
						else
						{
							if(src==NULL && !ReadFile(srcf, src, src_size)) return false;
							if(dst==NULL && !ReadFile(dstf, dst, dst_size)) return false;

							// patch
							patcher.AddOperatorPatch(s->_Offset, s->_Size, offset);
							bsdiff((u_char*)src+s->_Offset, s->_Size, (u_char*)dst+dstf._Sections[i]._Offset, dstf._Sections[i]._Size, patcher._stub);
						}
					}
					else
					{
						if(dst==NULL && !ReadFile(dstf, dst, dst_size)) return false;

						// append
						if(!patcher.AddOperatorAppend(dstf._Sections[i]._Size, offset)) return false;
						int bzerror;
						BZFILE* bzf = BZ2_bzWriteOpen(&bzerror, patcher._stub, 9, 0, 0);
						if(bzf==NULL) return false;
						BZ2_bzWrite(&bzerror, bzf, (char*)dst+dstf._Sections[i]._Offset, dstf._Sections[i]._Size);
						if(bzerror!=BZ_OK)
						{
							BZ2_bzWriteClose(&bzerror, bzf, 0, NULL, NULL);
							return false;
						}
						unsigned int inbytes, outbytes;
						BZ2_bzWriteClose(&bzerror, bzf, 0, &inbytes, &outbytes);
						if(bzerror!=BZ_OK)
						{
							return false;
						}
					}
				}

				if(!patcher.AddOperatorEnd()) return false;
				if(src) free(src);
				if(dst) free(dst);
			}
		}
	}

	if(!patcher.DelFile("")) return false;
	if(!patcher.Close(true)) return false;

	return true;
}
Beispiel #21
0
int DIFF_main(int argc,char *argv[])
{
	int fd;
	u_char *old,*_new;
	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];
	FILE * pf;
	BZFILE * pfbz2;
	int bz2err;

	int bytesWritten=0;

	if(argc!=4) errx(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=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
		((old=(u_char*)malloc(oldsize+1))==NULL) ||
		(lseek(fd,0,SEEK_SET)!=0) ||
		(read(fd,old,oldsize)!=oldsize) ||
		(close(fd)==-1)) err(1,"%s",argv[1]);

	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=open(argv[2],O_RDONLY|O_BINARY,0))<0) ||
		((newsize=lseek(fd,0,SEEK_END))==-1) ||
		((_new=(u_char*)malloc(newsize+1))==NULL) ||
		(lseek(fd,0,SEEK_SET)!=0) ||
		(read(fd,_new,newsize)!=newsize) ||
		(close(fd)==-1)) err(1,"%s",argv[2]);

	if(((db=(u_char*)malloc(newsize+1))==NULL) ||
		((eb=(u_char*)malloc(newsize+1))==NULL)) err(1,NULL);
	dblen=0;
	eblen=0;

	/* Create the patch file */
	if ((pf = fopen(argv[3], "wb")) == NULL)
		err(1, "%s", argv[3]);

	/* 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);
	offtout(0, header + 8);
	offtout(0, header + 16);
	offtout(newsize, header + 24);
	if (fwrite(header, 32, 1, pf) != 1)
		err(1, "fwrite(%s)", argv[3]);

	/* Compute the differences, writing ctrl as we go */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
		errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
	scan=0;len=0;
	lastscan=0;lastpos=0;lastoffset=0;
	while(scan<newsize) {
		oldscore=0;

		for(scsc=scan+=len;scan<newsize;scan++) {
			len=search(I,old,oldsize,_new+scan,newsize-scan,
				0,oldsize,&pos);

			for(;scsc<scan+len;scsc++)
				if((scsc+lastoffset<oldsize) &&
					(old[scsc+lastoffset] == _new[scsc]))
					oldscore++;

			if(((len==oldscore) && (len!=0)) || 
				(len>oldscore+8)) break;

			if((scan+lastoffset<oldsize) &&
				(old[scan+lastoffset] == _new[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]==_new[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]==_new[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(_new[lastscan+lenf-overlap+i]==
						old[lastpos+lenf-overlap+i]) s++;
					if(_new[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]=_new[lastscan+i]-old[lastpos+i];
			for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
				eb[eblen+i]=_new[lastscan+lenf+i];

			dblen+=lenf;
			eblen+=(scan-lenb)-(lastscan+lenf);

			offtout(lenf,buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK)
				errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
			bytesWritten+=8;
		//	printf("bz2err 8 %i\n", bytesWritten);

			offtout((scan-lenb)-(lastscan+lenf),buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK)
				errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
			bytesWritten+=8;
		//	printf("bz2err 8 %i\n", bytesWritten);

			offtout((pos-lenb)-(lastpos+lenf),buf);
			BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
			if (bz2err != BZ_OK)
				errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
			bytesWritten+=8;
		//	printf("bz2err 8 %i\n", bytesWritten);

			lastscan=scan-lenb;
			lastpos=pos-lenb;
			lastoffset=pos-scan;
		};
	};
	BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
	if (bz2err != BZ_OK)
		errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

	/* Compute size of compressed ctrl data */
	if ((len = ftello(pf)) == -1)
		err(1, "ftello");
	offtout(len-32, header + 8);

	/* Write compressed diff data */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
		errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
	BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
	bytesWritten+=dblen;
//	printf("bz2err dblen %i %i\n", dblen, bytesWritten);
	if (bz2err != BZ_OK)
		errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
	BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
	if (bz2err != BZ_OK)
		errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

	/* Compute size of compressed diff data */
	if ((newsize = ftello(pf)) == -1)
		err(1, "ftello");
	offtout(newsize - len, header + 16);

	/* Write compressed extra data */
	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
		errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
	BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
	if (bz2err != BZ_OK)
		errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
	bytesWritten+=eblen;
	//printf("bz2err eblen %i %i\n", eblen, bytesWritten);
	BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
	if (bz2err != BZ_OK)
		errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

	// REMOVEME
//	if ((newsize = ftello(pf)) == -1)
//		err(1, "ftello");

	/* Seek to the beginning, write the header, and close the file */
	if (fseeko(pf, 0, SEEK_SET))
		err(1, "fseeko");
	if (fwrite(header, 32, 1, pf) != 1)
		err(1, "fwrite(%s)", argv[3]);
	if (fclose(pf))
		err(1, "fclose");

	/* Free the memory we used */
	free(db);
	free(eb);
	free(I);
	free(old);
	free(_new);

	return 0;
}
Beispiel #22
0
void file_read_func(gpointer data, gpointer user_data) {
  tp_args_t* tp_arg = (tp_args_t*) data;
  gchar buf[READBUFZ];
  size_t nread=0, currPos=tp_arg->startPos, readSz=READBUFZ;
  FILE* fd = fopen(tp_arg->filen, "r");
  FILE* tmpFd = fopen(tp_arg->tmpFilen, "w");
  BZFILE* b;
  int     bzerror;
  gint    processed = 0;
  unsigned int bytesIn=0, bytesOut=0;

  if (!fd) {
    /* handle error */
    perror("couln't open input file");
    g_atomic_int_set(&(tp_arg->error), TRUE);
    return;
  }

  if (!tmpFd) {
    /* handle error */
    perror("couln't open tempfile");
    g_atomic_int_set(&(tp_arg->error), TRUE);
    return;
  }

  b = BZ2_bzWriteOpen( &bzerror, tmpFd, 9, 0, 0 );
  if (bzerror != BZ_OK) {
    /* handle error */
    fprintf(stderr, "BZError %d\n", bzerror);
    if (!errno) perror("system Error");
    g_atomic_int_set(&(tp_arg->error), TRUE);
    BZ2_bzWriteClose(&bzerror, b, FALSE, NULL, NULL);
    return;
  }
 
  fseek(fd, tp_arg->startPos, SEEK_SET);
  while (currPos < tp_arg->endPos && !feof(fd) && !_recieved_SIGINT) {
    if (currPos + readSz > tp_arg->endPos)
      readSz = tp_arg->endPos - currPos;
    nread = fread(&(buf[0]), 1, readSz, fd);
    currPos += nread;
    if (tp_arg->verbose) {
      processed = (currPos - tp_arg->startPos)/(double)(tp_arg->endPos - tp_arg->startPos) * 100;
      g_atomic_int_set(&(tp_arg->processed), processed);
    }
    /* 
    fprintf(stderr, "Read %d, %ld remain\n", nread, tp_arg->endPos - currPos);
    */
    BZ2_bzWrite(&bzerror, b, &(buf[0]), nread);
    if (bzerror == BZ_IO_ERROR) { 
      BZ2_bzWriteClose(&bzerror, b, FALSE, NULL, NULL);
      /* handle error */
      fprintf(stderr, "BZError %d\n", bzerror);
      if (!errno) perror("system Error");
      g_atomic_int_set(&(tp_arg->error), TRUE);
      break;
    }
  }

  BZ2_bzWriteClose(&bzerror, b, FALSE, &bytesIn, &bytesOut);
  if (bzerror == BZ_IO_ERROR) {
    /* handle error */
    fprintf(stderr, "BZError %d\n", bzerror);
    if (!errno) perror("system Error");
    g_atomic_int_set(&(tp_arg->error), TRUE);
  }
  fclose(fd);
  fclose(tmpFd);
  g_atomic_int_set(&(tp_arg->done), TRUE);
}
// This is main() from bsdiff.c, with the following changes:
//
//    - old, oldsize, newdata, newsize are arguments; we don't load this
//      data from files.  old and newdata are owned by the caller; we
//      don't free them at the end.
//
//    - the "I" block of memory is owned by the caller, who passes a
//      pointer to *I, which can be NULL.  This way if we call
//      bsdiff() multiple times with the same 'old' data, we only do
//      the qsufsort() step the first time.
//
int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize,
           const char* patch_filename)
{
    int fd;
    off_t *I;
    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];
    FILE * pf;
    BZFILE * pfbz2;
    int bz2err;

    if (*IP == NULL) {
        off_t* V;
        *IP = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
        V = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
        qsufsort(*IP, V, old, oldsize);
        free(V);
    }
    I = *IP;

    if(((db=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL) ||
            ((eb=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL)) err(1,NULL);
    dblen=0;
    eblen=0;

    /* Create the patch file */
    if ((pf = fopen(patch_filename, "w")) == NULL)
        err(1, "%s", patch_filename);

    /* 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);
    offtout(0, header + 8);
    offtout(0, header + 16);
    offtout(newsize, header + 24);
    if (fwrite(header, 32, 1, pf) != 1)
        err(1, "fwrite(%s)", patch_filename);

    /* Compute the differences, writing ctrl as we go */
    if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
        errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
    scan=0;
    len=0;
    lastscan=0;
    lastpos=0;
    lastoffset=0;
    while(scan<newsize) {
        oldscore=0;

        for(scsc=scan+=len; scan<newsize; scan++) {
            len=search(I,old,oldsize,newdata+scan,newsize-scan,
                       0,oldsize,&pos);

            for(; scsc<scan+len; scsc++)
                if((scsc+lastoffset<oldsize) &&
                        (old[scsc+lastoffset] == newdata[scsc]))
                    oldscore++;

            if(((len==oldscore) && (len!=0)) ||
                    (len>oldscore+8)) break;

            if((scan+lastoffset<oldsize) &&
                    (old[scan+lastoffset] == newdata[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]==newdata[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]==newdata[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(newdata[lastscan+lenf-overlap+i]==
                            old[lastpos+lenf-overlap+i]) s++;
                    if(newdata[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]=newdata[lastscan+i]-old[lastpos+i];
            for(i=0; i<(scan-lenb)-(lastscan+lenf); i++)
                eb[eblen+i]=newdata[lastscan+lenf+i];

            dblen+=lenf;
            eblen+=(scan-lenb)-(lastscan+lenf);

            offtout(lenf,buf);
            BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
            if (bz2err != BZ_OK)
                errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);

            offtout((scan-lenb)-(lastscan+lenf),buf);
            BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
            if (bz2err != BZ_OK)
                errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);

            offtout((pos-lenb)-(lastpos+lenf),buf);
            BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
            if (bz2err != BZ_OK)
                errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);

            lastscan=scan-lenb;
            lastpos=pos-lenb;
            lastoffset=pos-scan;
        };
    };
    BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
    if (bz2err != BZ_OK)
        errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

    /* Compute size of compressed ctrl data */
    if ((len = ftello(pf)) == -1)
        err(1, "ftello");
    offtout(len-32, header + 8);

    /* Write compressed diff data */
    if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
        errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
    BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
    if (bz2err != BZ_OK)
        errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
    BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
    if (bz2err != BZ_OK)
        errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

    /* Compute size of compressed diff data */
    if ((newsize = ftello(pf)) == -1)
        err(1, "ftello");
    offtout(newsize - len, header + 16);

    /* Write compressed extra data */
    if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
        errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
    BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
    if (bz2err != BZ_OK)
        errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
    BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
    if (bz2err != BZ_OK)
        errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);

    /* Seek to the beginning, write the header, and close the file */
    if (fseeko(pf, 0, SEEK_SET))
        err(1, "fseeko");
    if (fwrite(header, 32, 1, pf) != 1)
        err(1, "fwrite(%s)", patch_filename);
    if (fclose(pf))
        err(1, "fclose");

    /* Free the memory we used */
    free(db);
    free(eb);

    return 0;
}