This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipspatch.c
448 lines (420 loc) · 14.3 KB
/
ipspatch.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
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
/*
This program is hereby released to the public domain.
~aaaaaa123456789, 2016-05-01
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#define BLOCK_SIZE 8192
#define MINIMUM_RUN 8
#define MAXIMUM_REDUNDANCY 6
// you cannot write a patch block with an offset of 4,542,278 (0x454F46) because it is the end of file marker
#define EOF_MARKER 4542278
struct patch_block {
unsigned offset;
unsigned short length;
char payload[];
};
int main(int, char **);
void usage(const char *);
void patch(const char *, const char *, const char *);
void copy_contents(FILE *, FILE *, unsigned);
struct patch_block * read_patch_block(FILE *);
void apply_patch_block(FILE *, struct patch_block *);
unsigned convert_buffer_to_number(const char *, unsigned char);
void create_patch(const char *, const char *, const char *);
FILE * open_file_checked(const char *, unsigned *);
FILE * open_patch_checked(const char *);
char get_special_value(FILE *, unsigned);
void create_patch_for_data(const char *, const char *, unsigned, unsigned, FILE *, char);
unsigned get_segment_length(const char *, const char *, unsigned, int);
void write_patch_block_for_data(const char *, unsigned, unsigned, FILE *, char);
int check_runs(const char *, unsigned);
void write_value(unsigned, unsigned char, FILE *);
void dump_patch(const char *);
char * read_line(void);
char * trim_string(const char *);
void build_patch(const char *);
int convert_hex_string_to_number(const char *, unsigned char);
void parse_patch_code_line(char *, unsigned, unsigned, FILE *);
unsigned char hex_digit_value(char);
int main (int argc, char ** argv) {
if (argc < 2) usage(*argv);
if (!strcmp(argv[1], "patch")) {
if (argc != 5) usage(*argv);
patch(argv[2], argv[3], argv[4]);
} else if (!strcmp(argv[1], "create")) {
if (argc != 5) usage(*argv);
create_patch(argv[2], argv[3], argv[4]);
} else if (!strcmp(argv[1], "dump")) {
if (argc != 3) usage(*argv);
dump_patch(argv[2]);
} else if (!strcmp(argv[1], "build")) {
if (argc != 3) usage(*argv);
build_patch(argv[2]);
} else
usage(*argv);
return 0;
}
void usage (const char * program_name) {
fprintf(stderr, "usage:\n"
" creating a patch: %s create <file1> <file2> <output.ips>\n"
" applying a patch: %s patch <file> <patch.ips> <result>\n"
" dumping a patch: %s dump <patch.ips> (dumps code to stdout)\n"
" building a patch: %s build <patch.ips> (reads code from stdin)\n",
program_name, program_name, program_name, program_name);
exit(1);
}
void error_and_exit (const char * error, ...) {
va_list ap;
va_start(ap, error);
vfprintf(stderr, error, ap);
exit(1);
}
void patch (const char * source, const char * patch_file, const char * output) {
unsigned file_length;
FILE * source_file = open_file_checked(source, &file_length);
FILE * patch_fp = open_patch_checked(patch_file);
FILE * target_file = fopen(output, "wb+");
copy_contents(target_file, source_file, file_length);
fclose(source_file);
struct patch_block * block;
while (!feof(patch_fp)) {
block = read_patch_block(patch_fp);
if (!block) break;
apply_patch_block(target_file, block);
free(block);
}
fclose(patch_fp);
fclose(target_file);
}
void copy_contents (FILE * destination, FILE * source, unsigned length) {
char buf[BLOCK_SIZE];
unsigned next;
while (length) {
next = (length > BLOCK_SIZE) ? BLOCK_SIZE : length;
!fread(buf, 1, next, source);
fwrite(buf, 1, next, destination);
length -= next;
}
}
struct patch_block * read_patch_block (FILE * fp) {
char buf[5];
unsigned offset, length;
if (fread(buf, 1, 5, fp) < 5) return NULL;
offset = convert_buffer_to_number(buf, 3);
if (offset == EOF_MARKER) return NULL;
length = convert_buffer_to_number(buf + 3, 2);
struct patch_block * result = malloc(sizeof(struct patch_block) + (length ? length : 3));
result -> offset = offset;
result -> length = length;
if (!length) length = 3;
if (fread(result -> payload, 1, length, fp) < length) {
free(result);
return NULL;
}
return result;
}
void apply_patch_block (FILE * file, struct patch_block * block) {
fseek(file, block -> offset, 0);
if (block -> length) {
fwrite(block -> payload, 1, block -> length, file);
return;
}
unsigned length = convert_buffer_to_number(block -> payload, 2);
char fill = block -> payload[2];
void * buffer = malloc(length);
memset(buffer, fill, length);
fwrite(buffer, 1, length, file);
free(buffer);
}
unsigned convert_buffer_to_number (const char * buffer, unsigned char length) {
unsigned result;
unsigned char p;
for (p = result = 0; p < length; p ++) result = (result << 8) | ((unsigned char) buffer[p]);
return result;
}
void create_patch (const char * from, const char * to, const char * output) {
unsigned first_length, second_length;
FILE * first_file = open_file_checked(from, &first_length);
FILE * second_file = open_file_checked(to, &second_length);
if (first_length > second_length) error_and_exit("error: first file %s is larger than second file %s\n", from, to);
FILE * patch_file = fopen(output, "wb");
if (!patch_file) error_and_exit("error: cannot open %s for writing\n", output);
fwrite("PATCH", 1, 5, patch_file);
char buffer1[BLOCK_SIZE];
char buffer2[BLOCK_SIZE];
unsigned p, next;
char special = get_special_value(first_file, first_length);
for (p = 0; p < first_length; p += BLOCK_SIZE) {
next = first_length - p;
if (next > BLOCK_SIZE) next = BLOCK_SIZE;
!fread(buffer1, 1, next, first_file);
!fread(buffer2, 1, next, second_file);
create_patch_for_data(buffer1, buffer2, next, p, patch_file, special);
}
fclose(first_file);
if (first_length < second_length) {
next = second_length - first_length;
void * data = malloc(next);
!fread(data, 1, next, second_file);
write_patch_block_for_data(data, next, first_length, patch_file, special);
free(data);
}
fclose(second_file);
fwrite("EOF", 1, 3, patch_file);
fclose(patch_file);
}
FILE * open_file_checked (const char * file, unsigned * file_length) {
FILE * fp = fopen(file, "rb");
if (!fp) error_and_exit("error: cannot open %s for reading\n", file);
if (fseek(fp, 0, 2)) error_and_exit("error: cannot seek on %s\n", file);
long length = ftell(fp);
if (length < 0)
error_and_exit("error: cannot determine length of %s\n", file);
else if (length > 16777216)
error_and_exit("error: %s is larger than 16 MB\n", file);
if (file_length) *file_length = length;
rewind(fp);
return fp;
}
FILE * open_patch_checked (const char * patch_file) {
FILE * fp = fopen(patch_file, "rb");
if (!fp) error_and_exit("error: cannot open %s for reading\n", patch_file);
char buf[5];
if ((fread(buf, 1, 5, fp) != 5) || memcmp(buf, "PATCH", 5)) error_and_exit("error: %s is not a valid patch file\n", patch_file);
return fp;
}
char get_special_value (FILE * fp, unsigned length) {
if (length < EOF_MARKER) return 0;
long pos = ftell(fp);
fseek(fp, EOF_MARKER - 1, 0);
char result;
!fread(&result, 1, 1, fp);
fseek(fp, pos, 0);
return result;
}
void create_patch_for_data (const char * first_buffer, const char * second_buffer, unsigned length, unsigned offset, FILE * output, char special) {
unsigned pos = 0;
unsigned block, next;
while (pos < length) {
if (first_buffer[pos] == second_buffer[pos]) {
pos += get_segment_length(first_buffer + pos, second_buffer + pos, length - pos, 1);
continue;
}
block = 0;
next = 0;
do {
block += next;
block += get_segment_length(first_buffer + pos + block, second_buffer + pos + block, length - pos - block, 0);
next = get_segment_length(first_buffer + pos + block, second_buffer + pos + block, length - pos - block, 1);
} while (((pos + block) < length) && (next < MAXIMUM_REDUNDANCY));
write_patch_block_for_data(second_buffer + pos, block, offset + pos, output, special);
pos += block;
}
}
unsigned get_segment_length (const char * first_buffer, const char * second_buffer, unsigned length, int kind) {
// 0: differing, 1: equal
unsigned pos;
for (pos = 0; (pos < length) && ((first_buffer[pos] == second_buffer[pos]) == kind); pos ++);
return pos;
}
void write_patch_block_for_data (const char * data, unsigned length, unsigned offset, FILE * output, char special) {
if (!length) return;
if (offset == EOF_MARKER) {
char * buf = malloc(length + 1);
*buf = special;
memcpy(buf + 1, data, length);
write_patch_block_for_data(buf, length + 1, EOF_MARKER - 1, output, special);
free(buf);
return;
}
unsigned run;
for (run = 1; (run < length) && (data[run] == *data); run ++);
if ((offset != EOF_MARKER) && (run >= MINIMUM_RUN)) {
if (run > 32767) run = 32767;
write_value(offset, 3, output);
write_value(0, 2, output);
write_value(run, 2, output);
write_value(*data, 1, output);
write_patch_block_for_data(data + run, length - run, offset + run, output, special);
return;
}
unsigned current, remainder;
if (length < 32768) {
current = length;
remainder = 0;
} else {
current = 32767;
remainder = length - 32767;
}
int run_pos = check_runs(data, current);
if (run_pos > 0) {
remainder += current - run_pos;
current = run_pos;
if ((offset + current) == EOF_MARKER) {
remainder --;
current ++;
}
}
write_value(offset, 3, output);
write_value(current, 2, output);
fwrite(data, 1, current, output);
if (remainder) write_patch_block_for_data(data + current, remainder, offset + current, output, special);
}
int check_runs (const char * data, unsigned length) {
unsigned pos, cmp;
if (length < MINIMUM_RUN) return -1;
for (pos = 0; pos <= (length - MINIMUM_RUN); pos ++) {
for (cmp = 0; (cmp < MINIMUM_RUN) && (data[pos] == data[pos + cmp]); cmp ++);
if (cmp >= MINIMUM_RUN) return pos;
}
return -1;
}
void write_value (unsigned value, unsigned char length, FILE * file) {
char buf[16];
unsigned char p;
for (p = 0; p < length; p ++) buf[p] = (value >> (8 * (length - p - 1))) & 255;
fwrite(buf, 1, length, file);
}
void dump_patch (const char * patch_file) {
FILE * fp = open_patch_checked(patch_file);
struct patch_block * block;
while (!feof(fp)) {
block = read_patch_block(fp);
if (!block) break;
printf("%06x:", block -> offset);
if (block -> length) {
unsigned p;
for (p = 0; p < block -> length; p ++) printf(" %02hhx", block -> payload[p]);
putchar('\n');
} else
printf(" %04x * %02hhx\n", convert_buffer_to_number(block -> payload, 2), block -> payload[2]);
free(block);
}
fclose(fp);
}
char * read_line (void) {
int character;
char * result = NULL;
unsigned length = 0;
while (1) {
character = getchar();
if ((character == EOF) || (character == '\n')) break;
result = realloc(result, length + 1);
result[length ++] = character;
}
result = realloc(result, length + 1);
result[length] = 0;
return result;
}
char * trim_string (const char * string) {
if (!string) return NULL;
char * result = malloc(strlen(string) + 1);
while (isspace(*string)) string ++;
if (!*string) {
*result = 0;
return result;
}
unsigned last = strlen(string);
while (isspace(string[last - 1])) last --;
strncpy(result, string, last);
result[last] = 0;
return result;
}
void build_patch (const char * patch_file) {
FILE * fp = fopen(patch_file, "wb");
if (!fp) error_and_exit("error: could not open %s for writing\n", patch_file);
fwrite("PATCH", 1, 5, fp);
char * line;
char * split;
char * address_line;
unsigned line_number = 0;
int address;
while (!feof(stdin)) {
line = read_line();
line_number ++;
if (!(line && *line)) goto comment;
if (*line == ':') goto comment;
split = strchr(line, ':');
if (!split) goto comment;
*split = 0;
address_line = trim_string(line);
address = convert_hex_string_to_number(address_line, 6);
if (address < 0)
fprintf(stderr, "warning: line %u: address %s is not valid\n", line_number, line);
else if (address == EOF_MARKER)
fprintf(stderr, "warning: line %u: address matches end-of-file marker 0x%06x\n", line_number, EOF_MARKER);
else
parse_patch_code_line(split + 1, address, line_number, fp);
free(address_line);
comment:
free(line);
}
fwrite("EOF", 1, 3, fp);
fclose(fp);
}
int convert_hex_string_to_number (const char * string, unsigned char limit) {
if (!(string && *string)) return -1;
unsigned long min_overflow = 1UL << (4 * limit);
char * errp;
unsigned long converted = strtoul(string, &errp, 16);
if (*errp) return -1;
if (converted >= min_overflow) return -1;
return converted;
}
void parse_patch_code_line (char * contents, unsigned address, unsigned line_number, FILE * output) {
char * split = strchr(contents, '*');
char * tmp;
if (split) {
int length, data;
*split = 0;
tmp = trim_string(contents);
length = convert_hex_string_to_number(tmp, 4);
free(tmp);
tmp = trim_string(split + 1);
data = convert_hex_string_to_number(tmp, 2);
free(tmp);
if ((length < 0) || (data < 0)) {
fprintf(stderr, "warning: line %u: invalid contents for address 0x%06x\n", line_number, address);
return;
}
write_value(address, 3, output);
write_value(0, 2, output);
write_value(length, 2, output);
write_value(data, 1, output);
return;
}
unsigned char * buf = malloc(65536);
unsigned char last_digit = 255;
unsigned length = 0;
for (; *contents; contents ++) {
if (isspace(*contents)) continue;
if (!isxdigit(*contents)) {
fprintf(stderr, "warning: line %u: invalid contents for address 0x%06x\n", line_number, address);
free(buf);
return;
}
if (last_digit == 255)
last_digit = hex_digit_value(*contents);
else {
buf[length ++] = (last_digit << 4) | hex_digit_value(*contents);
if (length > 65535) {
fprintf(stderr, "warning: line %u: invalid contents for address 0x%06x\n", line_number, address);
free(buf);
return;
}
last_digit = 255;
}
}
write_value(address, 3, output);
write_value(length, 2, output);
fwrite(buf, 1, length, output);
free(buf);
}
unsigned char hex_digit_value (char digit) {
if (digit < 'A') return digit - 48;
return (digit & ~32) - 55;
}