-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_patch.c
More file actions
67 lines (57 loc) · 1.8 KB
/
apply_patch.c
File metadata and controls
67 lines (57 loc) · 1.8 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bin_patch.h"
int main(int argc, char** argv) {
if (argc < 3) {
printf("usage: %s [patch] [old_file] [new_file]\n\n", argv[0]);
printf("applies a patch to old_file and writes it to new_file.\n");
printf("use generate_patch to make a compatible patch file.\n\n");
return 1;
}
FILE *patch = fopen(argv[1], "rb");
FILE *old = fopen(argv[2], "rb");
if (!patch || !old) {
printf("opening patch or old file failed\n");
return 2;
}
fseek(patch, 0L, SEEK_END);
fseek(old, 0L, SEEK_END);
uint32_t patch_len = ftell(patch);
uint32_t old_len = ftell(old);
rewind(patch);
rewind(old);
u8_t *patch_buf = (u8_t *) malloc(patch_len);
u8_t *old_buf = (u8_t *) malloc(old_len);
if (!patch_buf || !old_buf) {
printf("patch or old buffer could not be allocated\n");
fclose(patch);
fclose(old);
return 3;
}
fread(patch_buf, sizeof(u8_t), patch_len, patch);
fread(old_buf, sizeof(u8_t), old_len, old);
fclose(patch);
fclose(old);
index_t diff_leni = *((index_t *) patch_buf);
len_t diff_lenl = index_to_len(diff_leni);
struct binary_patch bp = {
.diff_len = diff_leni,
.diff_start = ((index_t *) patch_buf + 1),
.diff_delta = ((index_t *) patch_buf + diff_lenl + 1),
.heap = (u8_t *) ((index_t *) patch_buf + diff_lenl * 2 + 1)
};
apply_patch(old_buf, &bp);
FILE *new = fopen(argv[3], "wb");
if (!new) {
printf("new file could not be opened\n");
free(patch_buf);
free(old_buf);
return 4;
}
fwrite(old_buf, sizeof(u8_t), old_len, new);
fclose(new);
free(patch_buf);
free(old_buf);
return 0;
}