-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixdelta.c
284 lines (267 loc) · 8.12 KB
/
fixdelta.c
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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <strings.h>
#include <stdint.h>
#ifdef TINYFILE
#include "tinyfile.h"
#endif
/*
xdelta3 AppHeader fixer v1.0
(c) SysTools 2016
http://systools.losthost.org/?misc#fixdelta
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES!
USE AT YOUR OWN RISK!
With this tool you can use handy and short way to apply patches:
> xdelta3 -d patch.delta
instead of ugly and large:
> xdelta3 -d -s source_file patch.delta output_file
So you can apply a whole bunch of xdelta3 patches in one go,
without manual input for source or output files.
Note that xdelta3, source and delta patch files must be in the same folder.
See the batch/script examples below.
Save the text between ---/--- and ===/=== as a text file and
rename it to the appropriate name as "applyall.bat" or "applyall.sh".
Windows batch file:
applyall.bat
---/---
@echo off
for %%a in (*.delta) do xdelta3 -d "%%~a"
===/===
Linux shell script:
applyall.sh
---/---
#!/bin/bash
for a in *.delta
do
xdelta3 -d "$a"
done
===/===
No WinAPI code here because xdelta3 cross-platform tool.
*/
#define MAX_ERR_LIST 9
static char *ErrMsgList[MAX_ERR_LIST] = {
"unknown error",
"can\'t open input file",
"not a xdelta3 file",
"unrecognized header indicator bits set",
"no AppHeader in file",
"memory allocation failed for AppHeader",
"invalid filename(s) in AppHeader",
"can\'t create output file",
"output file write error"
};
/* header indicator bits */
#define VCD_SECONDARY (1U << 0) /* uses secondary compressor */
#define VCD_CODETABLE (1U << 1) /* supplies code table data */
#define VCD_APPHEADER (1U << 2) /* supplies application data */
#define VCD_HDR_MAX (VCD_SECONDARY | VCD_CODETABLE | VCD_APPHEADER)
#define XDELTA3_HEAD (0x00C4C3D6)
/* no error handling for this code, let us hope no one in their
sane mind will produce patches bigger than available free memory */
void CopyFilePart(FILE *f, FILE *fl, uint32_t offs, uint32_t size) {
void *p;
p = malloc(size);
if (p) {
fseek(fl, offs, SEEK_SET);
fread(p, 1, size, fl);
fwrite(p, 1, size, f);
free(p);
}
}
/* this function assumes that the name buffer is
large enough to hold a new file extension */
void ChangeFileExt(char *name, char *ext) {
char *p;
/* some sanity checks */
if (name && ext) {
/* find last extension */
for (p = NULL; *name; name++) {
if (*name == '.') {
p = name;
}
}
/* replace or add to the end (if no extension at all) */
strcpy(p ? p : name, ext);
}
}
uint32_t ReadVarSize(FILE *fl) {
uint32_t d;
uint8_t b;
d = 0;
do {
b = 0;
fread(&b, 1, 1, fl);
d = (d << 7) | (b & 0x7F);
} while (b & 0x80);
return(d);
}
uint32_t WriteVarSize(FILE *fl, uint32_t d) {
uint8_t x[5], i;
i = 0;
do {
x[i] = (d & 0x7F) | 0x80;
i++;
d >>= 7;
} while(d);
d = i;
x[0] ^= 0x80;
for (; i > 0; i--) {
fwrite(&x[i - 1], 1, 1, fl);
}
return(d);
}
uint32_t XDeltaFix(char *strFileInput, char *strFileOutput) {
FILE *fl, *f;
uint32_t result, d, b, sz1, sz2;
char *s, *c, *p, *o;
/* try to open the file */
result = 1;
fl = fopen(strFileInput, "rb");
if (fl) {
/* init second part size */
fseek(fl, 0, SEEK_END);
sz2 = ftell(fl);
fseek(fl, 0, SEEK_SET);
/* read and check signature */
result = 2;
d = 0;
fread(&d, 4, 1, fl);
if (d == XDELTA3_HEAD) {
/* read and check flags */
result = 3;
b = 0;
fread(&b, 1, 1, fl);
if (b <= VCD_HDR_MAX) {
/* init first part size */
sz1 = 5;
/* check for unnecessary blocks */
if (b & VCD_SECONDARY) {
d = ReadVarSize(fl);
fseek(fl, d, SEEK_CUR);
sz1 += d;
}
if (b & VCD_CODETABLE) {
d = ReadVarSize(fl);
fseek(fl, d, SEEK_CUR);
sz1 += d;
}
/* check that AppHeader block exists */
result = 4;
if (b & VCD_APPHEADER) {
/* read whole block */
d = ReadVarSize(fl);
s = (char *) malloc(d + 1);
/* memory allocation check */
result = 5;
if (s) {
/* read block */
fread(s, d, 1, fl);
s[d] = 0;
/* save current position */
d = ftell(fl);
/* find (p)atched and (o)riginal names (strip path) */
p = s;
o = NULL;
for (c = s; *c; c++) {
/* middle separator */
if ((!o) && (*c == '/') && (c[1] == '/')) {
*c = 0;
o = &c[2];
}
/* root path */
if ((*c == '/') || (*c == '\\')) {
*c = 0;
/* check last slash */
if (c[1]) {
if (!o) {
p = &c[1];
} else {
o = &c[1];
}
}
}
}
/* names parsed ok */
result = 6;
if (o && *o && *p) {
printf("%s\n%s\n", p, o);
/* now it's time to decide on output filename */
f = NULL;
if (strFileOutput) {
/* from commandline (if any) */
f = fopen(strFileOutput, "wb");
} else {
/* or as source file with ".delta" extension */
c = (char *) malloc(strlen(o) + 6 + 1);
if (c) {
strcpy(c, o);
ChangeFileExt(c, ".delta");
f = fopen(c, "wb");
free(c);
}
}
/* check output file */
result = 7;
if (f) {
/* copy first part */
CopyFilePart(f, fl, 0, sz1);
/* write new AppHeader block */
b = strlen(p) + 2 + strlen(o) + 1;
/* add var block size - need it in a final check later */
b += WriteVarSize(f, b);
/* save block string in format "patched//original/" */
fwrite(p, 1, strlen(p), f);
/* s can be used here since p already saved (may overlap) */
*s = '/';
fwrite(s, 1, 1, f);
fwrite(s, 1, 1, f);
fwrite(o, 1, strlen(o), f);
fwrite(s, 1, 1, f);
/* copy second part */
CopyFilePart(f, fl, d, sz2 - d);
/* filesize check that everything saved successfully
this should cover any possible disk write errors */
result = (ftell(f) == (sz1 + b + sz2 - d)) ? 0 : 8;
fclose(f);
}
}
free(s);
}
}
}
}
fclose(fl);
}
return(result);
}
int main(int argc, char *argv[]) {
int rt;
printf(
"xdelta3 AppHeader fixer v1.0\n"\
"(c) SysTools 2016\n"\
"http://systools.losthost.org/?misc\n\n"
);
if ((argc < 2) || (argc > 3)) {
printf(
"This program strips path from the filenames of source and output files\n"\
"inside AppHeader of xdelta3 patches, so you can call \"xdelta3 -d patch.delta\"\n"\
"without specify source and output filenames.\n\n"\
"Usage: fixdelta <input.delta> [output.delta]\n\n"\
"If optional output filename is ommited then an output file will have xdelta3\n"
"source filename (from AppHeader) with \".delta\" as extension.\n\n"\
"THIS SOFTWARE IS PROVIDED \"AS IS\" WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES!\n"\
"USE AT YOUR OWN RISK!\n\n"\
);
return(1);
}
printf("%s\n\n", argv[1]);
rt = XDeltaFix(argv[1], (argc == 3) ? argv[2] : NULL);
if (rt) {
printf("ERROR-%d: %s", rt, ErrMsgList[(rt < MAX_ERR_LIST) ? rt : 0]);
} else {
printf("\ndone");
}
printf("\n\n");
return(rt);
}