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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
* Patch.cpp:
* written by palmers / teso
*/
#include <Patch.hpp>
SystemMap DummyValMap;
/*
* helper class
*/
class replaceAddr
{
private:
unsigned char *data;
unsigned short len;
public:
replaceAddr (unsigned char *a, unsigned short x)
{
data = a;
len = x;
}
void operator() (Addr2Addr *a)
{
unsigned short x = 0;
unsigned char *b = NULL,
*c = NULL;
b = (unsigned char *) &a->first;
c = (unsigned char *) &a->second;
for (x = 0; x <= (len - 3); x++)
{
if (b[0] == data[x])
{
if ((b[1] == data[x + 1]) && \
(b[2] == data[x + 2]) && \
(b[3] == data[x + 3]))
{
data[x] = c[0];
data[x + 1] = c[1];
data[x + 2] = c[2];
data[x + 3] = c[3];
}
}
}
}
};
/*
* member functions
*/
string Patch::state2string ()
{
string ret;
switch (state)
{
case CLEAN:
ret = "Clean";
break;
case LINKED:
ret = "Linked";
break;
case APPLIED:
ret = "Applied";
break;
case AFAILED:
ret = "ApplyFailed";
break;
case LFAILED:
ret = "LinkFailed";
break;
default:
ret = "Unknown";
break;
}
return ret;
}
void Patch::string2state (string a)
{
if (a == "Clean")
state = CLEAN;
else if (a == "Linked")
state = LINKED;
else if (a == "Applied")
state = APPLIED;
else if (a == "ApplyFailed")
state = AFAILED;
else if (a == "LinkFailed")
state = LFAILED;
else if (a == "Unknown")
abort ();
}
string Patch::data2string (unsigned char *a)
{
string ret;
int x;
ret = itos16 ((unsigned int) a[0]);
for (x = 1; x < len; x++)
{
ret += ' ';
ret += itos16 ((unsigned int) a[x]);
}
return ret;
}
void Patch::string2data (string s, unsigned char *d)
{
string tmp;
unsigned short x,
y;
for (x = 0; x < (len - 1); x++)
{
y = s.find_first_of (" ");
tmp.resize (y);
s.copy (tmp.begin (), y);
s.erase (0, y + 1);
d[x] = (unsigned char) stoi16 (tmp) & 0xff;
tmp.erase ();
}
tmp.resize (s.length ());
s.copy (tmp.begin (), s.length ());
d[x] = (unsigned char) stoi16 (tmp) & 0xff;
}
bool Patch::initObjects (unsigned char *d, unsigned short l, unsigned int add, rwKernel *x)
{
len = l;
address = add;
local_rw = x;
data = new unsigned char[len];
back_data = new unsigned char[len];
overwr = new unsigned char[len];
copy (d, d + len, data);
copy (d, d + len, back_data);
state = CLEAN;
return true;
}
void Patch::parse (string s)
{
unsigned char *a = NULL;
string tmp;
int x = 0;
x = s.find_first_of (",");
tmp.resize (x);
s.copy (tmp.begin (), x);
s.erase (0, x + 1);
address = stoi16 (tmp);
tmp.erase ();
x = s.find_first_of (":");
tmp.resize (x);
s.copy (tmp.begin (), x);
s.erase (0, x + 1);
string2state (tmp);
tmp.erase ();
x = s.find_first_of ("(");
s.erase (0, x + 1);
x = s.find_first_of (")");
tmp.resize (x);
s.copy (tmp.begin (), x);
len = count (tmp.begin (), tmp.end (), ' ') + 1;
data = new unsigned char[len];
back_data = new unsigned char[len];
overwr = new unsigned char[len];
string2data (tmp, data);
tmp.erase ();
if (state == CLEAN)
return;
switch (state)
{
case LINKED:
case LFAILED:
a = back_data;
break;
case APPLIED:
case AFAILED:
a = overwr;
break;
}
x = s.find_first_of ("(");
s.erase (0, x + 1);
x = s.find_first_of (")");
tmp.resize (x);
s.copy (tmp.begin (), x);
string2data (tmp, a);
}
Patch::Patch ()
{
}
Patch::Patch (unsigned char *d, unsigned short l, unsigned int add)
{
initObjects (d, l, add, NULL);
}
Patch::Patch (unsigned char *d, unsigned short l, unsigned int add, rwKernel *x)
{
initObjects (d, l, add, x);
}
Patch::Patch (string s)
{
parse (s);
}
Patch::Patch (string s, rwKernel *rw)
{
parse (s);
local_rw = rw;
}
Patch::~Patch ()
{
delete data;
delete back_data;
delete overwr;
}
void Patch::initFromString (string a)
{
parse (a);
}
string Patch::getPatchAsString ()
{
unsigned char *a = NULL;
string b;
switch (state)
{
case LINKED:
case LFAILED:
a = back_data;
break;
case APPLIED:
case AFAILED:
a = overwr;
break;
}
b = itos16 (address) + ',' + state2string () + ": (" + data2string (data) + ')';
if (a != NULL)
b += ", (" + data2string (a) + ')';
b += '\n';
return b;
}
bool Patch::isLinked ()
{
return (state & LINKED);
}
bool Patch::wasChanged ()
{
int x;
for (x = 0; x < len; x++)
if (data[x] != back_data[x])
return true;
return false;
}
bool Patch::isApplied ()
{
return (state & APPLIED);
}
bool Patch::isClean ()
{
return (state & CLEAN);
}
bool Patch::isFailed ()
{
return (state & AFAILED) || (state & LFAILED);
}
int Patch::getState ()
{
return state;
}
void Patch::restore ()
{
copy (back_data, back_data + len, data);
state = CLEAN;
}
bool Patch::remove (rwKernel *rw)
{
if (state != APPLIED)
return false;
rw->write (overwr, len, address);
return true;
}
bool Patch::remove ()
{
if (local_rw == NULL)
return false;
if (state != APPLIED)
return false;
local_rw->write (overwr, len, address);
return true;
}
unsigned char *Patch::getData ()
{
return data;
}
void Patch::apply (rwKernel *rw)
{
/* pretty simple :) */
rw->read (overwr, len, address);
rw->write (data, len, address);
state = APPLIED;
}
void Patch::apply ()
{
if (local_rw == NULL)
{
state = AFAILED;
return;
}
local_rw->read (overwr, len, address);
local_rw->write (data, len, address);
state = APPLIED;
}
void Patch::link (Addr2AddrList *a2a)
{
replaceAddr x (data, len);
int y = a2a->size ();
Addr2AddrList::iterator z = a2a->begin ();
Addr2Addr *t = NULL;
/* XXX: why doesnt for_each work with pointer to list???
* its the same problem with for "(x = a2a->begin (); x != a2a->end (); ..."
* the x != end (); just f#$§ true!
*/
while (y--)
{
t = *z;
x (t);
z++;
}
state = LINKED;
}
void Patch::dump (string file)
{
unsigned char *a = NULL;
/*
* dump file format:
* <Address>,<State>: (<data, hex, byte wise>){, (<other data, hex, byte wise>)}\n
* where the data in the curled brackets in depending on the state:
* state: data in the curled bbrackets:
* clean none
* applied overwr
* afailed overwr
* linked back_date
* lfailed back_data
*/
ofstream f;
switch (state)
{
case LINKED:
case LFAILED:
a = back_data;
break;
case APPLIED:
case AFAILED:
a = overwr;
break;
}
f.open (file.c_str (), ios::ate | ios::app);
f.setf (ios::hex, ios::basefield);
f << address << ',' << state2string () << ':' << ' ';
f << '(' << data2string (data) << ')';
if (a != NULL)
f << ',' << ' ' << '(' << data2string (a) << ')';
f << endl;
f.setf (ios::dec, ios::basefield);
}
istream& operator>> (istream& is, Patch& p)
{
string tmp;
getline (is, tmp, '\n');
p.initFromString (tmp);
return is;
}
ostream& operator<< (ostream& os, Patch& p)
{
os << p.getPatchAsString ();
return os;
}
/*
* unrelated functions ....
*/
Addr2AddrList *genReplaceValMap (SymbolTable *st)
{
zzSymList::iterator x;
Addr2Addr *y = NULL;
zzSym *z = NULL;
Addr2AddrList *a2a = NULL;
a2a = new Addr2AddrList ();
/* get all symbol addressess together with dummy values */
for (x = st->symList.begin (); x != st->symList.end (); x++)
{
z = *x;
if (DummyValMap[z->Name] != 0)
{
y = new Addr2Addr ();
y->first = DummyValMap[z->Name];
y->second = z->Address;
a2a->push_back (y);
}
}
return a2a;
}
void genDummyValMap ()
{
int x = 0;
while (__n2a[x].name != NULL)
{
DummyValMap.add (string (__n2a[x].name), __n2a[x].add);
x++;
}
}
|