-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
110 lines (93 loc) · 2.54 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mjp.h"
FILE *ofile; // original file
FILE *pfile; // patch file
FILE *dfile; // destination file
FILE *cfile; // comparison file
int dfile_wr(int addr, uint8_t *buf, int len)
{
/* fwrite increase the file pointer after each write, so addr is useless here */
fwrite(buf, 1, len, dfile);
return 0;
}
int ofile_rd(int addr)
{
uint8_t buf[1];
fseek(ofile, addr, SEEK_SET);
fread(buf, 1, 1, ofile);
return buf[0];
}
int o2d_copy(int srcaddr, int desaddr, int len)
{
uint8_t *buf;
buf = malloc(len + 1);
//memset(buf, 0, len + 1);
fseek(ofile, srcaddr, SEEK_SET);
fread(buf, 1, len, ofile);
fwrite(buf, 1, len, dfile);
return 0;
}
typedef int (*mjp_copy_t)(int des, int src, int len);
int main(int argc, char **argv)
{
int dt;
ofile = fopen(argv[1], "rb");
if (ofile == NULL) {
printf("can't open original file\n");
return -1;
}
pfile = fopen(argv[2], "rb");
if (pfile == NULL) {
printf("can't open patch file\n");
return -1;
}
dfile = fopen(argv[3], "wb");
if (dfile == NULL) {
printf("can't open destination file\n");
return -1;
}
cfile = fopen(argv[4], "rb");
if (dfile == NULL) {
printf("can't open destination file\n");
return -1;
}
printf("mjp_t size %d, (mjp_dt_t %d)\n", sizeof(mjp_t), sizeof(mjp_dt_t));
/* parse to log patch file */
mjp_start(dfile_wr, ofile_rd, o2d_copy);
while ((dt = getc(pfile)) != EOF) {
mjp_parse(dt);
}
mjp_parse_done();
/* apply patch and restore */
fseek(pfile, 0, SEEK_SET);
mjp_start(dfile_wr, ofile_rd, o2d_copy); // set call back
while ((dt = getc(pfile)) != EOF) {
mjp_apply(dt);
}
mjp_apply_done();
fclose(ofile);
fclose(pfile);
fclose(dfile);
/* compare dfile and cfile */
dfile = fopen(argv[3], "rb");
if (dfile == NULL) {
printf("can't open destination file\n");
return -1;
}
while (1) {
int c, d;
c = getc(cfile);
d = getc(dfile);
if (c != d) {
printf("NG. Destination file and comparison file are not identical\n");
return -1;
}
if (c == EOF) {
break;
}
}
printf("OK. Apply patch successfully. Destination file and comparison file are identical\n");
return 0;
}