Skip to content
Open
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
82 changes: 62 additions & 20 deletions bsdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ 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 lastscan,lastpos,lastoffset,lastwrittenscan,lastwrittenpos;
int64_t ctrlcur[3], ctrlnext[3];
int64_t oldscore,scsc;
int64_t s,Sf,lenf,Sb,lenb;
int64_t overlap,Ss,lens;
Expand All @@ -239,7 +240,8 @@ 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;
lastscan=0;lastpos=0;lastoffset=lastwrittenscan=lastwrittenpos=0;
ctrlcur[0]=0;ctrlcur[1]=0;ctrlcur[2]=0;
while(scan<req.newsize) {
oldscore=0;

Expand Down Expand Up @@ -292,32 +294,72 @@ 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;

/* 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))
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)))
return -1;

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

/* Write extra data */
for(i=0;i<ctrlcur[1];i++)
buffer[i]=req.new[lastwrittenscan+ctrlcur[0]+i];
if (writedata(req.stream, buffer, ctrlcur[1]))
return -1;

lastwrittenscan=lastscan;
lastwrittenpos=lastpos;
};
ctrlcur[0]=ctrlnext[0];
ctrlcur[1]=ctrlnext[1];
ctrlcur[2]=ctrlnext[2];
} else {
ctrlcur[1]+=ctrlnext[1];
ctrlcur[2]+=ctrlnext[2];
};

/* 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)))
return -1;

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

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)))
return -1;

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

/* Write extra data */
for(i=0;i<ctrlcur[1];i++)
buffer[i]=req.new[lastwrittenscan+ctrlcur[0]+i];
if (writedata(req.stream, buffer, ctrlcur[1]))
return -1;
};

return 0;
}

Expand Down