From 8bf68dc87cc50f4ee9db4bc2d1efd632b6a2e877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sat, 15 Sep 2018 19:48:28 +0200 Subject: [PATCH 1/7] C++ compatibility: Added extern "C" for C++ code. --- bsdiff.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bsdiff.h b/bsdiff.h index b37da5f..b325c7f 100644 --- a/bsdiff.h +++ b/bsdiff.h @@ -31,6 +31,10 @@ # include # include +#ifdef __cplusplus +extern "C" { +#endif + struct bsdiff_stream { void* opaque; @@ -42,4 +46,8 @@ struct bsdiff_stream int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t newsize, struct bsdiff_stream* stream); +#ifdef __cplusplus +} +#endif + #endif From 0752124ed9b6c8e7fe94265518889c1474d28558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sat, 15 Sep 2018 19:49:42 +0200 Subject: [PATCH 2/7] C++ compatibility: "new" is reserved word in C++, thus I renamed old->source and new->target. --- bsdiff.c | 14 +++++++------- bsdiff.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bsdiff.c b/bsdiff.c index 628f1c1..0165b0e 100644 --- a/bsdiff.c +++ b/bsdiff.c @@ -321,24 +321,24 @@ static int bsdiff_internal(const struct bsdiff_request req) return 0; } -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) { int result; struct bsdiff_request req; - if((req.I=stream->malloc((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); diff --git a/bsdiff.h b/bsdiff.h index b325c7f..1fcd683 100644 --- a/bsdiff.h +++ b/bsdiff.h @@ -44,7 +44,7 @@ struct bsdiff_stream int (*write)(struct bsdiff_stream* stream, const void* buffer, int size); }; -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 } From 547636c1f1e5c060cef0fb45aed42f6a0afd7d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sat, 15 Sep 2018 19:54:22 +0200 Subject: [PATCH 3/7] Merging control records having zero in their first triplet with the previous record (original bsdiff produces records having both the first and second item zeroes, i.e. representing only a seek in the old file). --- bsdiff.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/bsdiff.c b/bsdiff.c index 628f1c1..8992214 100644 --- a/bsdiff.c +++ b/bsdiff.c @@ -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(scan Date: Sat, 15 Sep 2018 20:10:15 +0200 Subject: [PATCH 4/7] Forwards information about what data block is being written to writedata() callback. --- bsdiff.c | 12 ++++++------ bsdiff.h | 6 +++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bsdiff.c b/bsdiff.c index 628f1c1..4b5de15 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; @@ -297,19 +297,19 @@ static int bsdiff_internal(const struct bsdiff_request req) offtout((pos-lenb)-(lastpos+lenf),buf+16); /* Write control data */ - if (writedata(req.stream, buf, sizeof(buf))) + if (writedata(req.stream, buf, sizeof(buf), BSDIFF_WRITECONTROL)) return -1; /* Write diff data */ for(i=0;i #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..54fe6c6 100644 --- a/bsdiff.h +++ b/bsdiff.h @@ -31,13 +31,17 @@ # include # include +#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); From e362fbc408c35202090eb2db2851d33820b0ccb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sat, 15 Sep 2018 20:36:57 +0200 Subject: [PATCH 5/7] Update bsdiff.c Fixed previous merge. --- bsdiff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bsdiff.c b/bsdiff.c index 14e859a..089910a 100644 --- a/bsdiff.c +++ b/bsdiff.c @@ -294,7 +294,7 @@ static int bsdiff_internal(const struct bsdiff_request req) lenb-=lens; }; - ctrlnext[0]=lenf; + ctrlnext[0]=lenf; ctrlnext[1]=(scan-lenb)-(lastscan+lenf); ctrlnext[2]=(pos-lenb)-(lastpos+lenf); @@ -340,7 +340,7 @@ static int bsdiff_internal(const struct bsdiff_request req) offtout(ctrlcur[2],buf+16); /* Write control data */ - if (writedata(req.stream, buf, sizeof(buf))) + if (writedata(req.stream, buf, sizeof(buf), BSDIFF_WRITECONTROL)) return -1; }; From a3d3688cb717be296aac05d3df0ea9ee0d536386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sun, 16 Sep 2018 18:49:00 +0200 Subject: [PATCH 6/7] C++ compatibility: Added extern "C" for C++ code. C++ compatibility: "new" is reserved word in C++, thus I renamed old->source and new->target. --- bspatch.c | 16 ++++++++-------- bspatch.h | 10 +++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bspatch.c b/bspatch.c index b544914..28830e1 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,7 +53,7 @@ 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)) @@ -62,28 +62,28 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, }; /* 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])) 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])) return -1; /* Adjust pointers */ diff --git a/bspatch.h b/bspatch.h index 099c36e..e713de7 100644 --- a/bspatch.h +++ b/bspatch.h @@ -30,13 +30,21 @@ # include +#ifdef __cplusplus +extern "C" { +#endif + struct bspatch_stream { void* opaque; int (*read)(const struct bspatch_stream* stream, void* buffer, int length); }; -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 From 50ebe61030026783945aa085f3fa04aaaf0094da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Kom=C3=ADnek?= Date: Sun, 16 Sep 2018 18:51:50 +0200 Subject: [PATCH 7/7] Forwards information about what data block is being written to readdata() callback. --- bspatch.c | 8 ++++---- bspatch.h | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bspatch.c b/bspatch.c index b544914..e6a17ed 100644 --- a/bspatch.c +++ b/bspatch.c @@ -56,7 +56,7 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, while(newposread(stream, buf, 8)) + if (stream->read(stream, buf, 8, BSDIFF_READCONTROL)) return -1; ctrl[i]=offtin(buf); }; @@ -66,7 +66,7 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, return -1; /* Read diff string */ - if (stream->read(stream, new + newpos, ctrl[0])) + if (stream->read(stream, new + newpos, ctrl[0], BSDIFF_READDIFF)) return -1; /* Add old data to diff string */ @@ -83,7 +83,7 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, return -1; /* Read extra string */ - if (stream->read(stream, new + newpos, ctrl[1])) + if (stream->read(stream, new + 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..7b510b6 100644 --- a/bspatch.h +++ b/bspatch.h @@ -30,10 +30,14 @@ # include +#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);