-
Notifications
You must be signed in to change notification settings - Fork 2
/
mjp.c
384 lines (357 loc) · 9.23 KB
/
mjp.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mjp.h"
#define BKT MJP_BKT
#define EQL MJP_EQL
#define DEL MJP_DEL
#define INS MJP_INS
#define MOD MJP_MOD
#define ESC MJP_ESC
static mjp_t mjp;
static bool mjp_is_cmd(int dt)
{
switch (dt) {
//case ESC: // ESC is not command mode, is just for byte filling
case MOD:
case INS:
case DEL:
case EQL:
case BKT:
return true;
default:
return false;
}
}
static mjp_dt_t mjp_get_oft(uint8_t *buf, int len)
{
switch (len) {
case 1:
return buf[0] + 1;
case 2:
return buf[0] + buf[1] + 1;
case 3:
return ((uint32_t)buf[1] << 8) + ((uint32_t)buf[2] << 0);
case 5:
return ((uint32_t)buf[1] << 24) +
((uint32_t)buf[2] << 16) +
((uint32_t)buf[3] << 8) +
((uint32_t)buf[4] << 0);
default:
return -1;
}
}
static mjp_dt_t mjp_parse_oft(mjp_oft_t *oft, int ch)
{
/* already parsed */
if ((oft->cnt != 0) && (oft->max == oft->cnt)) {
return -1;
}
/* doesn't support */
if ((oft->cnt >= 1) && (oft->buf[0] == 255)) {
return -2;
}
oft->buf[oft->cnt++] = ch;
if (oft->cnt == 1) {
if (oft->buf[0] == 254) {
oft->max = 5;
} else if (oft->buf[0] == 253) {
oft->max = 3;
} else if (oft->buf[0] == 252) {
oft->max = 2;
} else {
oft->max = 1;
}
}
if (oft->max == oft->cnt) {
return mjp_get_oft(oft->buf, oft->cnt);
}
return 0;
}
static void mjp_flush(void)
{
#ifdef MJP_WR_BUF_SIZE
if (mjp.wr.cnt != 0) {
if (mjp.des_wr_cb != NULL) {
mjp.des_wr_cb(mjp.wr.addr + 1 - mjp.wr.cnt, mjp.wr.buf, mjp.wr.cnt);
}
mjp.wr.cnt = 0;
}
#endif
}
static void mjp_write(mjp_dt_t addr, int dt)
{
#ifdef MJP_WR_BUF_SIZE
/* set to buffer */
mjp.wr.buf[mjp.wr.cnt++] = dt;
mjp.wr.addr = addr;
if (mjp.wr.cnt >= MJP_WR_BUF_SIZE) {
mjp_flush();
}
#else
uint8_t buf[1];
buf[0] = dt;
mjp.des_wr_cb(addr, buf, 1);
#endif
}
static int mjp_read(mjp_dt_t addr)
{
if (mjp.org_rd_cb != NULL) {
return mjp.org_rd_cb(addr);
}
return 0;
}
int mjp_start(mjp_des_wr_t des_wr_cb, mjp_org_rd_t org_rd_cb, mjp_copy_t copy_cb)
{
memset(&mjp, 0, sizeof(mjp_t));
mjp.cmd = MJP_EOF;
mjp.last_dt = MJP_EOF;
mjp.des_wr_cb = des_wr_cb;
mjp.org_rd_cb = org_rd_cb;
mjp.copy_cb = copy_cb;
return 0;
}
int mjp_apply_feed(int dt)
{
mjp_dt_t val;
switch (mjp.cmd) {
case MOD:
mjp_write(mjp.dfile_addr, dt);
mjp.dfile_addr++;
mjp.ofile_addr++;
break;
case INS:
mjp_write(mjp.dfile_addr, dt);
mjp.dfile_addr++;
break;
case DEL:
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_DEL_OFT;
}
if (val != 0) {
mjp.ofile_addr += val;
mjp.cmd = MJP_EOF;
}
break;
case EQL:
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_EQL_OFT;
}
if (val != 0) {
if (mjp.copy_cb != NULL) {
mjp_flush();
mjp.copy_cb(mjp.ofile_addr, mjp.dfile_addr, val);
mjp.dfile_addr += val;
mjp.ofile_addr += val;
} else {
int i;
for (i = 0; i < val; i++) {
mjp_write(mjp.dfile_addr, mjp_read(mjp.ofile_addr));
mjp.dfile_addr++;
mjp.ofile_addr++;
}
}
mjp.cmd = MJP_EOF;
}
break;
case BKT:
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_BKT_OFT;
}
if (val != 0) {
mjp.ofile_addr -= val;
mjp.cmd = MJP_EOF;
}
break;
default:
return MJP_ERR_FORMAT;
}
mjp.cnt++;
return 0;
}
int mjp_apply(int dt)
{
int ret = 0;
if ((mjp.cmd == BKT) || (mjp.cmd == EQL) || (mjp.cmd == DEL)) {
ret = mjp_apply_feed(dt);
mjp.last_dt = MJP_EOF;
} else if (mjp.last_dt == ESC) {
if (mjp_is_cmd(dt)) {
mjp.cmd = dt;
mjp.cnt = 0;
mjp.oft.cnt = 0;
mjp.oft.max = 0;
} else {
if (dt == ESC) {
ret = mjp_apply_feed(dt);
} else {
ret = mjp_apply_feed(ESC);
ret = mjp_apply_feed(dt);
}
}
mjp.last_dt = MJP_EOF;
} else {
if (dt != ESC) {
ret = mjp_apply_feed(dt);
}
mjp.last_dt = dt;
}
mjp.pfile_addr++;
return ret;
}
int mjp_apply_done(void)
{
mjp_flush();
return mjp.dfile_addr;
}
/*-----------------------------------------------------------------------------*/
// mjp API with log
static const char *mjp_str_tab[6] = {
"BKT",
"EQL",
"DEL",
"INS",
"MOD",
"ESC",
};
static void mjp_log_cmd(int cmd, mjp_dt_t org, mjp_dt_t des, mjp_dt_t len)
{
printf("\n");
switch (mjp.cmd) {
case MOD:
printf("%s: Modify from %d(O) %d(D) for %d bytes",
mjp_str_tab[cmd - BKT], org, des, len);
break;
case INS:
printf("%s: Insert from %d(D) for %d bytes",
mjp_str_tab[cmd - BKT], des, len);
break;
case DEL:
printf("%s: Delete from %d(O) for %d bytes",
mjp_str_tab[cmd - BKT], org, len);
break;
case EQL:
printf("%s: Copy from %d(O) to %d(D) for %d bytes",
mjp_str_tab[cmd - BKT], org, des, len);
break;
case BKT:
printf("%s: Backtrace from %d(O) to %d(O), offset %d",
mjp_str_tab[cmd - BKT], org, org - len, len);
break;
default:
printf("ERROR");
break;
}
printf("\n");
}
int mjp_parse_feed(int dt)
{
mjp_dt_t val;
switch (mjp.cmd) {
case MOD:
printf("%02X ", dt);
mjp.dfile_addr++;
mjp.ofile_addr++;
break;
case INS:
printf("%02X ", dt);
mjp.dfile_addr++;
break;
case DEL:
printf("%02X ", dt);
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_DEL_OFT;
}
if (val != 0) {
mjp_log_cmd(mjp.cmd, mjp.ofile_addr, mjp.dfile_addr, val);
mjp.ofile_addr += val;
mjp.cmd = MJP_EOF;
}
break;
case EQL:
printf("%02X ", dt);
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_EQL_OFT;
}
if (val != 0) {
mjp_log_cmd(mjp.cmd, mjp.ofile_addr, mjp.dfile_addr, val);
mjp.dfile_addr += val;
mjp.ofile_addr += val;
mjp.cmd = MJP_EOF;
}
break;
case BKT:
printf("%02X ", dt);
val = mjp_parse_oft(&mjp.oft, dt);
if (val < 0) {
return MJP_ERR_BKT_OFT;
}
if (val != 0) {
mjp_log_cmd(mjp.cmd, mjp.ofile_addr, mjp.dfile_addr, val);
mjp.ofile_addr -= val;
mjp.cmd = MJP_EOF;
}
break;
default:
printf("ERROR\n");
return MJP_ERR_FORMAT;
}
mjp.cnt++;
return 0;
}
int mjp_parse(int dt)
{
int ret = 0;
if ((mjp.cmd == BKT) || (mjp.cmd == EQL) || (mjp.cmd == DEL)) {
ret = mjp_parse_feed(dt);
mjp.last_dt = MJP_EOF;
} else if (mjp.last_dt == ESC) {
if (mjp_is_cmd(dt)) {
/* switch to new cmd mode */
if ((mjp.cmd == MOD) || (mjp.cmd == INS)) {
mjp_log_cmd(mjp.cmd, mjp.ofile_addr - mjp.cnt, mjp.dfile_addr - mjp.cnt, mjp.cnt);
}
mjp.cmd = dt;
mjp.cnt = 0;
mjp.oft.cnt = 0;
mjp.oft.max = 0;
printf("\n %6d | %s %02X %02X ",
mjp.pfile_addr - 1,
mjp_str_tab[dt - BKT],
ESC,
dt);
} else {
if (dt == ESC) {
ret = mjp_parse_feed(dt);
printf("(ESC)(ESC) ");
} else {
ret = mjp_parse_feed(ESC);
printf("(ESC) ");
ret = mjp_parse_feed(dt);
}
}
mjp.last_dt = MJP_EOF;
} else {
if (dt != ESC) {
ret = mjp_parse_feed(dt);
}
mjp.last_dt = dt;
}
mjp.pfile_addr++;
return ret;
}
int mjp_parse_done(void)
{
if ((mjp.cmd == MOD) || (mjp.cmd == INS)) {
mjp_log_cmd(mjp.cmd, mjp.ofile_addr - mjp.cnt, mjp.dfile_addr - mjp.cnt, mjp.cnt);
}
printf("\nPatch file size: %d\n", mjp.pfile_addr);
printf("Destination file size: %d\n", mjp.dfile_addr);
printf("Original file size: %d (used)\n", mjp.ofile_addr);
return 0;
}