Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide more information to writedata callback. #22

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions bsdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<req.newsize) {
oldscore=0;

Expand Down Expand Up @@ -292,24 +294,38 @@ static int bsdiff_internal(const struct bsdiff_request req)
lenb-=lens;
};

offtout(lenf,buf);
offtout((scan-lenb)-(lastscan+lenf),buf+8);
offtout((pos-lenb)-(lastpos+lenf),buf+16);

/* Write control data */
if (writedata(req.stream, buf, sizeof(buf)))
return -1;
ctrlnext[0]=lenf;
ctrlnext[1]=(scan-lenb)-(lastscan+lenf);
ctrlnext[2]=(pos-lenb)-(lastpos+lenf);

if (ctrlnext[0]) {
if (ctrlcur[0]||ctrlcur[1]||ctrlcur[2]) {
offtout(ctrlcur[0],buf);
offtout(ctrlcur[1],buf+8);
offtout(ctrlcur[2],buf+16);

/* Write control data */
if (writedata(req.stream, buf, sizeof(buf), BSDIFF_WRITECONTROL))
return -1;
};
ctrlcur[0]=ctrlnext[0];
ctrlcur[1]=ctrlnext[1];
ctrlcur[2]=ctrlnext[2];
} else {
ctrlcur[1]+=ctrlnext[1];
ctrlcur[2]+=ctrlnext[2];
};

/* Write diff data */
for(i=0;i<lenf;i++)
buffer[i]=req.new[lastscan+i]-req.old[lastpos+i];
if (writedata(req.stream, buffer, lenf))
if (writedata(req.stream, buffer, lenf, BSDIFF_WRITEDIFF))
return -1;

/* Write extra data */
for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
buffer[i]=req.new[lastscan+lenf+i];
if (writedata(req.stream, buffer, (scan-lenb)-(lastscan+lenf)))
if (writedata(req.stream, buffer, (scan-lenb)-(lastscan+lenf), BSDIFF_WRITEEXTRA))
return -1;

lastscan=scan-lenb;
Expand All @@ -318,27 +334,37 @@ static int bsdiff_internal(const struct bsdiff_request req)
};
};

if (ctrlcur[0]||ctrlcur[1]) {
offtout(ctrlcur[0],buf);
offtout(ctrlcur[1],buf+8);
offtout(ctrlcur[2],buf+16);

/* Write control data */
if (writedata(req.stream, buf, sizeof(buf), BSDIFF_WRITECONTROL))
return -1;
};

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);
Expand All @@ -360,7 +386,7 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news
#include <stdlib.h>
#include <unistd.h>

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;
Expand Down
16 changes: 14 additions & 2 deletions bsdiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@
# include <stddef.h>
# include <stdint.h>

#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
20 changes: 10 additions & 10 deletions bspatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,45 @@ 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;
int64_t ctrl[3];
int64_t i;

oldpos=0;newpos=0;
while(newpos<newsize) {
while(newpos<targetsize) {
/* Read control data */
for(i=0;i<=2;i++) {
if (stream->read(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<ctrl[0];i++)
if((oldpos+i>=0) && (oldpos+i<oldsize))
new[newpos+i]+=old[oldpos+i];
if((oldpos+i>=0) && (oldpos+i<sourcesize))
target[newpos+i]+=source[oldpos+i];

/* Adjust pointers */
newpos+=ctrl[0];
oldpos+=ctrl[0];

/* Sanity-check */
if(newpos+ctrl[1]>newsize)
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 */
Expand All @@ -107,7 +107,7 @@ int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize,
#include <unistd.h>
#include <fcntl.h>

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;
Expand Down
16 changes: 14 additions & 2 deletions bspatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,25 @@

# include <stdint.h>

#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