diff --git a/bsdiff.c b/bsdiff.c index 628f1c1..089910a 100644 --- a/bsdiff.c +++ b/bsdiff.c @@ -185,14 +185,14 @@ static void offtout(int64_t x,uint8_t *buf) if(x<0) buf[7]|=0x80; } -static int64_t writedata(struct bsdiff_stream* stream, const void* buffer, int64_t length) +static int64_t writedata(struct bsdiff_stream* stream, const void* buffer, int64_t length, int type) { int64_t result = 0; while (length > 0) { const int smallsize = (int)MIN(length, INT_MAX); - const int writeresult = stream->write(stream, buffer, smallsize); + const int writeresult = stream->write(stream, buffer, smallsize, type); if (writeresult == -1) { return -1; @@ -222,6 +222,7 @@ static int bsdiff_internal(const struct bsdiff_request req) int64_t *I,*V; int64_t scan,pos,len; int64_t lastscan,lastpos,lastoffset; + int64_t ctrlcur[3],ctrlnext[3]; int64_t oldscore,scsc; int64_t s,Sf,lenf,Sb,lenb; int64_t overlap,Ss,lens; @@ -240,6 +241,7 @@ static int bsdiff_internal(const struct bsdiff_request req) /* Compute the differences, writing ctrl as we go */ scan=0;len=0;pos=0; lastscan=0;lastpos=0;lastoffset=0; + ctrlcur[0]=0;ctrlcur[1]=0;ctrlcur[2]=0; while(scanmalloc((oldsize+1)*sizeof(int64_t)))==NULL) + if((req.I=stream->malloc((sourcesize+1)*sizeof(int64_t)))==NULL) return -1; - if((req.buffer=stream->malloc(newsize+1))==NULL) + if((req.buffer=stream->malloc(targetsize+1))==NULL) { stream->free(req.I); return -1; } - req.old = old; - req.oldsize = oldsize; - req.new = new; - req.newsize = newsize; + req.old = source; + req.oldsize = sourcesize; + req.new = target; + req.newsize = targetsize; req.stream = stream; result = bsdiff_internal(req); @@ -360,7 +386,7 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news #include #include -static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size) +static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size, int type __attribute__((__unused__))) { int bz2err; BZFILE* bz2; diff --git a/bsdiff.h b/bsdiff.h index b37da5f..d0d290c 100644 --- a/bsdiff.h +++ b/bsdiff.h @@ -31,15 +31,27 @@ # include # include +#ifdef __cplusplus +extern "C" { +#endif + +#define BSDIFF_WRITECONTROL 0 +#define BSDIFF_WRITEDIFF 1 +#define BSDIFF_WRITEEXTRA 2 + struct bsdiff_stream { void* opaque; void* (*malloc)(size_t size); void (*free)(void* ptr); - int (*write)(struct bsdiff_stream* stream, const void* buffer, int size); + int (*write)(struct bsdiff_stream* stream, const void* buffer, int size, int type); }; -int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t newsize, struct bsdiff_stream* stream); +int bsdiff(const uint8_t* source, int64_t sourcesize, const uint8_t* target, int64_t targetsize, struct bsdiff_stream* stream); + +#ifdef __cplusplus +} +#endif #endif diff --git a/bspatch.c b/bspatch.c index b544914..17f4ab2 100644 --- a/bspatch.c +++ b/bspatch.c @@ -45,7 +45,7 @@ static int64_t offtin(uint8_t *buf) return y; } -int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, struct bspatch_stream* stream) +int bspatch(const uint8_t* source, int64_t sourcesize, uint8_t* target, int64_t targetsize, struct bspatch_stream* stream) { uint8_t buf[8]; int64_t oldpos,newpos; @@ -53,37 +53,37 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, int64_t i; oldpos=0;newpos=0; - while(newposread(stream, buf, 8)) + if (stream->read(stream, buf, 8, BSDIFF_READCONTROL)) return -1; ctrl[i]=offtin(buf); }; /* Sanity-check */ - if(newpos+ctrl[0]>newsize) + if(newpos+ctrl[0]>targetsize) return -1; /* Read diff string */ - if (stream->read(stream, new + newpos, ctrl[0])) + if (stream->read(stream, target + newpos, ctrl[0], BSDIFF_READDIFF)) return -1; /* Add old data to diff string */ for(i=0;i=0) && (oldpos+i=0) && (oldpos+inewsize) + if(newpos+ctrl[1]>targetsize) return -1; /* Read extra string */ - if (stream->read(stream, new + newpos, ctrl[1])) + if (stream->read(stream, target + newpos, ctrl[1], BSDIFF_READEXTRA)) return -1; /* Adjust pointers */ @@ -107,7 +107,7 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, #include #include -static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length) +static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length, int type __attribute__((__unused__))) { int n; int bz2err; diff --git a/bspatch.h b/bspatch.h index 099c36e..fa87b7c 100644 --- a/bspatch.h +++ b/bspatch.h @@ -30,13 +30,25 @@ # include +#ifdef __cplusplus +extern "C" { +#endif + +#define BSDIFF_READCONTROL 0 +#define BSDIFF_READDIFF 1 +#define BSDIFF_READEXTRA 2 + struct bspatch_stream { void* opaque; - int (*read)(const struct bspatch_stream* stream, void* buffer, int length); + int (*read)(const struct bspatch_stream* stream, void* buffer, int length, int type); }; -int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, struct bspatch_stream* stream); +int bspatch(const uint8_t* source, int64_t sourcesize, uint8_t* target, int64_t targetsize, struct bspatch_stream* stream); + +#ifdef __cplusplus +} +#endif #endif