-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsdiff.c
More file actions
391 lines (334 loc) · 12.2 KB
/
Copy pathbsdiff.c
File metadata and controls
391 lines (334 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*-
* Copyright 2003-2005 Colin Percival
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "sais/sais.h"
#include <bzlib.h>
#include "bsdiff.h"
#define MIN( x, y ) (((x)<(y)) ? (x) : (y))
static void offtout( off_t x, uint8_t *buf ) {
off_t y = (x < 0) ? -x : x;
buf[0] = y % 256;
y -= buf[0];
y = y / 256;
buf[1] = y % 256;
y -= buf[1];
y = y / 256;
buf[2] = y % 256;
y -= buf[2];
y = y / 256;
buf[3] = y % 256;
y -= buf[3];
y = y / 256;
buf[4] = y % 256;
y -= buf[4];
y = y / 256;
buf[5] = y % 256;
y -= buf[5];
y = y / 256;
buf[6] = y % 256;
y -= buf[6];
y = y / 256;
buf[7] = y % 256;
if ( x < 0 ) buf[7] |= 0x80;
}
static off_t matchlen( const uint8_t *base, off_t baseSize,
const uint8_t *variant, off_t varSize ) {
off_t i;
for ( i = 0; (i < baseSize) && (i < varSize); i++ )
if ( base[i] != variant[i] ) break;
return i;
}
static off_t search( off_t *I, const uint8_t *base, off_t baseSize,
const uint8_t *variant, off_t varSize, off_t st, off_t en,
off_t *pos ) {
off_t x, y;
if ( en - st < 2 ) {
x = matchlen( base + I[st], baseSize - I[st], variant, varSize );
y = matchlen( base + I[en], baseSize - I[en], variant, varSize );
if ( x > y ) {
*pos = I[st];
return x;
} else {
*pos = I[en];
return y;
}
};
x = st + (en - st) / 2;
if ( memcmp( base + I[x], variant, MIN( baseSize - I[x], varSize )) < 0 ) {
return search( I, base, baseSize, variant, varSize, x, en, pos );
} else {
return search( I, base, baseSize, variant, varSize, st, x, pos );
};
}
static size_t writeHeader( DiffStream *pStream, uint8_t *buff ) {
return pStream->writeHeader( pStream, buff );
}
/* Returns the amount of data written */
static size_t writeDataBlock( DiffStream *pStream, uint8_t *buff,
size_t size ) {
size_t bzBuffSize = (size_t) ((1.01 * (float)size) + 600.0F);
uint8_t *bzBuff = (uint8_t *) malloc( bzBuffSize );
bz_stream bzStream;
bzStream.next_in = (char *) buff;
bzStream.avail_in = size;
bzStream.next_out = (char *) bzBuff;
bzStream.avail_out = bzBuffSize;
bzStream.bzalloc = NULL;
bzStream.bzfree = NULL;
bzStream.opaque = NULL;
int bzret = 0;
bzret = BZ2_bzCompressInit( &bzStream, 9, 0, 0 );
if ( bzret != BZ_OK ) {
fprintf( stderr, "BZ2_bzCompressInit: failed to init compression\n" );
BZ2_bzCompressEnd( &bzStream );
free( bzBuff );
return 0;
}
do {
bzret = BZ2_bzCompress( &bzStream, BZ_FINISH );
if ( bzret != BZ_RUN_OK && bzret != BZ_FINISH_OK &&
bzret != BZ_STREAM_END ) {
fprintf( stderr,
"BZ2_bzCompress: failed to compress data: errCode %d\n",
bzret );
BZ2_bzCompressEnd( &bzStream );
free( bzBuff );
return 0;
}
} while ( bzret != BZ_STREAM_END );
size_t diffCompressedSize = bzStream.total_out_hi32;
diffCompressedSize <<= 32;
diffCompressedSize += bzStream.total_out_lo32;
BZ2_bzCompressEnd( &bzStream );
size_t count = pStream->write( pStream, bzBuff, diffCompressedSize );
free( bzBuff );
return count;
}
size_t bsdiff( const uint8_t *base, size_t baseSize, const uint8_t *variant,
size_t varSize, DiffStream *stream ) {
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;
size_t ctrlDataSize, diffDataSize, extraDataSize;
size_t ctrlBuffSize, diffBuffSize, extraBuffSize;
size_t bzCtrlBuffSize, bzDiffBuffSize, bzExtraBuffSize;
uint8_t *ctrlBuff = NULL;
uint8_t *diffBuff = NULL;
uint8_t *extraBuff = NULL;
uint8_t header[32];
size_t estimatedSize;
size_t patchSize;
I = (off_t *) malloc((baseSize + 1) * sizeof( off_t ));
I[0] = baseSize;
sais( base, I + 1, baseSize );
/*
* Assuming that the diff and extra buffers will never exceed the variant size.
* For the ctrl buffer it may exceed (specially for very small files). It will be extended
* later if an overflow is detected.
*/
ctrlBuffSize = diffBuffSize = extraBuffSize = varSize;
diffBuff = (uint8_t *) malloc(diffBuffSize);
extraBuff = (uint8_t *) malloc(extraBuffSize);
ctrlBuff = (uint8_t *) malloc(ctrlBuffSize);
memset( ctrlBuff, 0, ctrlBuffSize);
memset( diffBuff, 0, diffBuffSize);
memset( extraBuff, 0, extraBuffSize);
ctrlDataSize = 0;
diffDataSize = 0;
extraDataSize = 0;
/* 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( varSize, header + 24 );
scan = 0;
len = 0;
lastscan = 0;
lastpos = 0;
lastoffset = 0;
while ( scan < varSize ) {
oldscore = 0;
for ( scsc = scan += len; scan < varSize; scan++ ) {
len = search( I, base, baseSize, variant + scan, varSize - scan,
0, baseSize, &pos );
for ( ; scsc < scan + len; scsc++ )
if ((scsc + lastoffset < baseSize) &&
(base[scsc + lastoffset] == variant[scsc]))
oldscore++;
if (((len == oldscore) && (len != 0)) ||
(len > oldscore + 8))
break;
if ((scan + lastoffset < baseSize) &&
(base[scan + lastoffset] == variant[scan]))
oldscore--;
};
if ((len != oldscore) || (scan == varSize)) {
s = 0;
Sf = 0;
lenf = 0;
for ( i = 0; (lastscan + i < scan) && (lastpos + i < baseSize); ) {
if ( base[lastpos + i] == variant[lastscan + i] ) s++;
i++;
if ( s * 2 - i > Sf * 2 - lenf ) {
Sf = s;
lenf = i;
};
};
lenb = 0;
if ( scan < varSize ) {
s = 0;
Sb = 0;
for ( i = 1; (scan >= lastscan + i) && (pos >= i); i++ ) {
if ( base[pos - i] == variant[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 ( variant[lastscan + lenf - overlap + i] ==
base[lastpos + lenf - overlap + i] )
s++;
if ( variant[scan - lenb + i] ==
base[pos - lenb + i] )
s--;
if ( s > Ss ) {
Ss = s;
lens = i + 1;
};
};
lenf += lens - overlap;
lenb -= lens;
};
for ( i = 0; i < lenf; i++ )
diffBuff[diffDataSize + i] =
variant[lastscan + i] - base[lastpos + i];
for ( i = 0; i < (scan - lenb) - (lastscan + lenf); i++ )
extraBuff[extraDataSize + i] = variant[lastscan + lenf + i];
diffDataSize += lenf;
extraDataSize += (scan - lenb) - (lastscan + lenf);
//append to ctrl buffer
uint8_t buff[8 * 3];
offtout( lenf, buff );
offtout((scan - lenb) - (lastscan + lenf), buff + 8 );
offtout((pos - lenb) - (lastpos + lenf), buff + 16 );
if (ctrlDataSize + 8 * 3 > ctrlBuffSize) {
fprintf(stdout, "stackoverflow in ctrl buffer detected.\n");
fprintf(stdout, "extending the ctrl data buffer\n");
uint8_t *oldBuff = ctrlBuff;
ctrlBuff = (uint8_t *) malloc(ctrlBuffSize * 2);
memcpy(ctrlBuff, oldBuff, ctrlBuffSize);
fprintf(stdout, "old buffer size: %zu, new size: %zu\n", ctrlBuffSize, ctrlBuffSize * 2);
ctrlBuffSize *= 2;
free(oldBuff);
}
memcpy( ctrlBuff + ctrlDataSize, buff, 8 * 3 );
ctrlDataSize += 8 * 3;
lastscan = scan - lenb;
lastpos = pos - lenb;
lastoffset = pos - scan;
};
};
// set the value for the header size
stream->headerSize = sizeof( header );
estimatedSize = stream->headerSize + ctrlDataSize + diffDataSize + extraDataSize;
stream->init( stream, estimatedSize );
// write ctrl buffer
fprintf(stdout, "writing ctrl buffer: size: %zu\n", ctrlDataSize);
bzCtrlBuffSize = writeDataBlock( stream, ctrlBuff, ctrlDataSize);
if ( bzCtrlBuffSize == 0 ) {
fprintf( stderr, "writeDataBlock: ctrl buffer failed\n" );
goto fail;
}
// write the diff buffer
fprintf(stdout, "writing diff buffer\n");
bzDiffBuffSize = writeDataBlock( stream, diffBuff, diffDataSize);
if ( bzDiffBuffSize == 0 ) {
fprintf( stderr, "writeDataBlock: diff buffer failed\n" );
goto fail;
}
// write the extra data
fprintf(stdout, "writing extra buffer");
bzExtraBuffSize = writeDataBlock( stream, extraBuff, extraDataSize);
if ( bzExtraBuffSize == 0 ) {
fprintf( stderr, "writeDataBlock: extra buffer failed\n" );
goto fail;
}
// update header with ctrl buffer length
offtout( bzCtrlBuffSize, header + 8 );
// update the header with diff buffer length
offtout( bzDiffBuffSize, header + 16 );
if ( writeHeader( stream, header ) == 0 ) {
fprintf( stderr, "writeHeader: header failed\n" );
goto fail;
}
fprintf( stdout, "bsdiff header: %zu\tctrl: %zu\tdiff: %zu\textra: %zu\n",
stream->headerSize, bzCtrlBuffSize, bzDiffBuffSize, bzExtraBuffSize );
stream->end( stream );
free( ctrlBuff );
free( diffBuff );
free( extraBuff );
free( I );
patchSize = stream->headerSize + bzCtrlBuffSize + bzDiffBuffSize + bzExtraBuffSize;
fprintf(stdout, "patch size: %zu\n", patchSize);
return patchSize;
fail:
if ( ctrlBuff != NULL ) {
free( ctrlBuff );
}
if ( diffBuff != NULL ) {
free( diffBuff );
}
if ( extraBuff != NULL ) {
free( extraBuff );
}
if ( I != NULL ) {
free( I );
}
return 0;
}