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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
#include "codec.h"
#include <contrib/libs/xdelta3/xdelta3.h>
#include <contrib/libs/xdelta3/xdelta3-internal.h>
#include <util/system/types.h>
// routines extracted from xdelta3-main.h
// from xdelta3.c
#define VCD_ADLER32 (1U << 2) /* has adler32 checksum */
#define UNUSED(x) (void)(x)
typedef enum {
CMD_MERGE_ARG,
CMD_MERGE,
} xd3_cmd;
void* xdelta3_buffer_alloc(void* context, size_t items, usize_t size)
{
if (!context) {
return malloc(items * size);
}
XDeltaContext* xd3_context = (XDeltaContext*) context;
return xd3_context->allocate(xd3_context->opaque, items * size);
}
void xdelta3_buffer_free(void* context, void* ptr)
{
if (!context) {
free(ptr);
return;
}
XDeltaContext* xd3_context = (XDeltaContext*) context;
xd3_context->free(xd3_context->opaque, ptr);
}
void init_common_config(XDeltaContext* context, xd3_config* config, usize_t iopt_size)
{
config->alloc = xdelta3_buffer_alloc;
config->freef = xdelta3_buffer_free;
config->opaque = context;
if (iopt_size) { // option should be 0 in case of patches merge, critical for patch calculation performance
config->iopt_size = iopt_size;
}
}
int init_recode_stream(XDeltaContext* context, xd3_stream* recode_stream)
{
int ret;
int stream_flags = XD3_ADLER32_NOVER | XD3_SKIP_EMIT;
int recode_flags;
xd3_config recode_config;
recode_flags = (stream_flags & XD3_SEC_TYPE);
xd3_init_config(&recode_config, recode_flags);
init_common_config(context, &recode_config, 0);
if ((ret = xd3_config_stream(recode_stream, &recode_config)) ||
(ret = xd3_encode_init_partial(recode_stream)) ||
(ret = xd3_whole_state_init(recode_stream)))
{
xd3_free_stream(recode_stream);
return ret;
}
return 0;
}
int merge_func(xd3_stream* stream, ui8* out_data, size_t* out_size)
{
UNUSED(out_data);
UNUSED(out_size);
return xd3_whole_append_window(stream);
}
int write_output(xd3_stream* stream, ui8* out_data, size_t* out_size, size_t max_osize)
{
if (stream->avail_out > 0) {
if (*out_size + stream->avail_out > max_osize) {
return ENOMEM;
}
memcpy(out_data + *out_size, stream->next_out, stream->avail_out);
*out_size += stream->avail_out;
}
return 0;
}
int merge_output(
XDeltaContext* context,
xd3_stream* stream,
xd3_stream* recode_stream,
xd3_stream* merge_stream,
ui8** buffer,
size_t buffer_size,
ui8* out_data,
size_t* out_size,
size_t max_osize)
{
int ret;
usize_t inst_pos = 0;
xoff_t output_pos = 0;
xd3_source recode_source;
usize_t window_num = 0;
int at_least_once = 0;
/* merge_stream is set if there were arguments. this stream's input
* needs to be applied to the merge_stream source. */
if ((merge_stream != NULL) &&
(ret = xd3_merge_input_output(stream, &merge_stream->whole_target)))
{
return ret;
}
/* Enter the ENC_INPUT state and bypass the next_in == NULL test
* and (leftover) input buffering logic. */
XD3_ASSERT(recode_stream->enc_state == ENC_INIT);
recode_stream->enc_state = ENC_INPUT;
recode_stream->next_in = *buffer;
recode_stream->flags |= XD3_FLUSH;
/* This encodes the entire target. */
while (inst_pos < stream->whole_target.instlen || !at_least_once) {
xoff_t window_start = output_pos;
int window_srcset = 0;
xoff_t window_srcmin = 0;
xoff_t window_srcmax = 0;
usize_t window_pos = 0;
usize_t window_size;
/* at_least_once ensures that we encode at least one window,
* which handles the 0-byte case. */
at_least_once = 1;
XD3_ASSERT(recode_stream->enc_state == ENC_INPUT);
if ((ret = xd3_encode_input(recode_stream)) != XD3_WINSTART) {
return XD3_INVALID;
}
/* Window sizes must match from the input to the output, so that
* target copies are in-range (and so that checksums carry
* over). */
XD3_ASSERT(window_num < stream->whole_target.wininfolen);
window_size = stream->whole_target.wininfo[window_num].length;
/* Output position should also match. */
if (output_pos != stream->whole_target.wininfo[window_num].offset) {
// internal merge error: offset mismatch
return XD3_INVALID;
}
// NOTE: check if delta_codecs can decode this. option_use_checksum = 1
if ((stream->dec_win_ind & VCD_ADLER32) != 0) {
recode_stream->flags |= XD3_ADLER32_RECODE;
recode_stream->recode_adler32 = stream->whole_target.wininfo[window_num].adler32;
}
window_num++;
if (buffer_size < window_size) {
xdelta3_buffer_free(context, *buffer);
*buffer = NULL;
buffer_size = 0;
if ((*buffer = (ui8*)xdelta3_buffer_alloc(context, window_size, 1)) == NULL) {
return ENOMEM;
}
recode_stream->next_in = *buffer; // re-setting stream buffer
buffer_size = window_size;
}
/* This encodes a single target window. */
while (window_pos < window_size && inst_pos < stream->whole_target.instlen) {
xd3_winst* inst = &stream->whole_target.inst[inst_pos];
usize_t take = xd3_min(inst->size, window_size - window_pos);
xoff_t addr;
switch (inst->type) {
case XD3_RUN:
if ((ret = xd3_emit_run(recode_stream, window_pos, take, &stream->whole_target.adds[inst->addr]))) {
return ret;
}
break;
case XD3_ADD:
/* Adds are implicit, put them into the input buffer. */
memcpy(*buffer + window_pos,
stream->whole_target.adds + inst->addr, take);
break;
default: /* XD3_COPY + copy mode */
if (inst->mode != 0) {
if (window_srcset) {
window_srcmin = xd3_min(window_srcmin, inst->addr);
window_srcmax = xd3_max(window_srcmax,
inst->addr + take);
} else {
window_srcset = 1;
window_srcmin = inst->addr;
window_srcmax = inst->addr + take;
}
addr = inst->addr;
} else {
XD3_ASSERT(inst->addr >= window_start);
addr = inst->addr - window_start;
}
if ((ret = xd3_found_match(recode_stream, window_pos, take, addr, inst->mode != 0))) {
return ret;
}
break;
}
window_pos += take;
output_pos += take;
if (take == inst->size) {
inst_pos += 1;
} else {
/* Modify the instruction for the next pass. */
if (inst->type != XD3_RUN) {
inst->addr += take;
}
inst->size -= take;
}
}
xd3_avail_input(recode_stream, *buffer, window_pos);
recode_stream->enc_state = ENC_INSTR;
if (window_srcset) {
recode_stream->srcwin_decided = 1;
recode_stream->src = &recode_source;
recode_source.srclen = (usize_t)(window_srcmax - window_srcmin);
recode_source.srcbase = window_srcmin;
recode_stream->taroff = recode_source.srclen;
XD3_ASSERT(recode_source.srclen != 0);
} else {
recode_stream->srcwin_decided = 0;
recode_stream->src = NULL;
recode_stream->taroff = 0;
}
for (;;) {
switch ((ret = xd3_encode_input(recode_stream))) {
case XD3_INPUT: {
goto done_window;
}
case XD3_OUTPUT: {
/* main_file_write below */
break;
}
case XD3_GOTHEADER:
case XD3_WINSTART:
case XD3_WINFINISH: {
/* ignore */
continue;
}
case XD3_GETSRCBLK:
case 0: {
return XD3_INTERNAL;
}
default:
return ret;
}
if ((ret = write_output(recode_stream, out_data, out_size, max_osize))) {
return ret;
}
xd3_consume_output(recode_stream);
}
done_window:
(void)0;
}
return 0;
}
int process_input(
XDeltaContext* context,
xd3_cmd cmd,
xd3_stream* recode_stream,
xd3_stream* merge_stream,
const ui8* patch,
size_t patch_size,
ui8* out_data,
size_t* out_size,
size_t max_out_size)
{
int ret;
xd3_stream stream;
int stream_flags = 0;
xd3_config config;
xd3_source source;
int (*input_func)(xd3_stream*);
int (*output_func)(xd3_stream*, ui8*, size_t*);
memset(&stream, 0, sizeof(stream));
memset(&source, 0, sizeof(source));
memset(&config, 0, sizeof(config));
stream_flags |= XD3_ADLER32;
/* main_input setup. */
stream_flags |= XD3_ADLER32_NOVER | XD3_SKIP_EMIT; // TODO: add nocompress
input_func = xd3_decode_input;
if ((ret = init_recode_stream(context, recode_stream))) {
return EXIT_FAILURE;
}
config.winsize = patch_size;
config.sprevsz = xd3_pow2_roundup(config.winsize);
config.flags = stream_flags;
init_common_config(context, &config, 0);
output_func = merge_func;
if ((ret = xd3_config_stream(&stream, &config)) || (ret = xd3_whole_state_init(&stream))) {
return EXIT_FAILURE;
}
/* If we've reached EOF tell the stream to flush. */
stream.flags |= XD3_FLUSH;
xd3_avail_input(&stream, patch, patch_size);
/* Main input loop. */
int again = 0;
do {
ret = input_func(&stream);
switch (ret) {
case XD3_INPUT:
again = 0;
break;
case XD3_GOTHEADER: {
XD3_ASSERT(stream.current_window == 0);
}
/* FALLTHROUGH */
case XD3_WINSTART: {
/* e.g., set or unset XD3_SKIP_WINDOW. */
again = 1;
break;
}
case XD3_OUTPUT: {
if (ret = output_func(&stream, out_data, out_size)) {
xd3_free_stream(&stream);
return EXIT_FAILURE;
}
xd3_consume_output(&stream);
again = 1;
break;
}
case XD3_WINFINISH: {
again = 1;
break;
}
default:
/* input_func() error */
xd3_free_stream(&stream);
return EXIT_FAILURE;
}
} while (again);
if (cmd == CMD_MERGE) {
ui8* buffer = NULL;
size_t buffer_size = patch_size;
if ((buffer = (ui8*)xdelta3_buffer_alloc(context, buffer_size, 1)) == NULL) {
xd3_free_stream(&stream);
return EXIT_FAILURE;
}
ret = merge_output(
context,
&stream,
recode_stream,
merge_stream,
&buffer,
buffer_size,
out_data,
out_size,
max_out_size);
xdelta3_buffer_free(context, buffer);
if (ret) {
xd3_free_stream(&stream);
return EXIT_FAILURE;
}
} else if (cmd == CMD_MERGE_ARG) {
xd3_swap_whole_state(&stream.whole_target, &recode_stream->whole_target);
}
if ((ret = xd3_close_stream(&stream))) {
return EXIT_FAILURE;
}
xd3_free_stream(&stream);
return EXIT_SUCCESS;
}
int patch_to_stream(
XDeltaContext* context,
const ui8* patch,
size_t patch_size,
xd3_stream* recode_stream,
xd3_stream* merge_stream)
{
xd3_stream merge_input;
int ret;
xd3_config config;
memset(&config, 0, sizeof(config));
init_common_config(context, &config, 0);
if ((ret = xd3_config_stream(&merge_input, &config)) || (ret = xd3_whole_state_init(&merge_input))) {
return ret;
}
ret = process_input(
context,
CMD_MERGE_ARG,
recode_stream,
merge_stream,
patch,
patch_size,
NULL,
NULL,
0);
if (ret == 0) {
xd3_swap_whole_state(&recode_stream->whole_target, &merge_input.whole_target);
}
xd3_free_stream(recode_stream);
if (ret != 0) {
xd3_free_stream(&merge_input);
return ret;
}
if ((ret = xd3_config_stream(merge_stream, &config)) || (ret = xd3_whole_state_init(merge_stream))) {
xd3_free_stream(&merge_input);
return ret;
}
xd3_swap_whole_state(&merge_stream->whole_target, &merge_input.whole_target);
ret = 0;
xd3_free_stream(&merge_input);
return ret;
}
int merge_vcdiff_patches(
XDeltaContext* context,
const ui8* patch1,
size_t patch_size1,
const ui8* patch2,
size_t patch_size2,
ui8* result,
size_t* result_size,
size_t max_result_size)
{
xd3_stream recode_stream;
memset(&recode_stream, 0, sizeof(xd3_stream));
xd3_stream merge_stream;
memset(&merge_stream, 0, sizeof(xd3_stream));
int ret;
ret = patch_to_stream(context, patch1, patch_size1, &recode_stream, &merge_stream);
if (!ret) {
ret = process_input(context, CMD_MERGE, &recode_stream, &merge_stream, patch2, patch_size2, result, result_size, max_result_size);
}
xd3_free_stream(&recode_stream);
xd3_free_stream(&merge_stream);
return ret;
}
|