-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMultiImport.cs
637 lines (535 loc) · 22.9 KB
/
MultiImport.cs
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddressLibraryManager
{
public partial class MultiImport : Form
{
public MultiImport()
{
InitializeComponent();
}
private readonly List<ComboBox>[] boxes = new List<ComboBox>[7];
private void MultiImport_Load(object sender, EventArgs e)
{
boxes[0] = new List<ComboBox> { this.comboBox1, this.comboBox2, this.comboBox3 };
boxes[1] = new List<ComboBox> { this.comboBox6, this.comboBox5, this.comboBox4 };
boxes[2] = new List<ComboBox> { this.comboBox9, this.comboBox8, this.comboBox7 };
boxes[3] = new List<ComboBox> { this.comboBox12, this.comboBox11, this.comboBox10 };
boxes[4] = new List<ComboBox> { this.comboBox15, this.comboBox14, this.comboBox13 };
boxes[5] = new List<ComboBox> { this.comboBox18, this.comboBox17, this.comboBox16 };
boxes[6] = new List<ComboBox> { this.comboBox21, this.comboBox20, this.comboBox19 };
var db = Manager.CurrentDatabase;
if (db == null)
return;
for(int i = 0; i < boxes.Length; i++)
{
var ls = boxes[i];
foreach(var c in ls)
{
c.Items.Add("");
foreach (var v in db.Versions)
c.Items.Add(v.Key.ToString());
c.SelectedIndex = 0;
}
}
}
private static string _GetSelected(ComboBox c)
{
if(c.SelectedIndex >= 0 && c.SelectedIndex < c.Items.Count)
{
var o = c.Items[c.SelectedIndex];
return o.ToString();
}
return "";
}
private void button1_Click(object sender, EventArgs e)
{
var db = Manager.CurrentDatabase;
if(db == null)
{
MessageBox.Show("Didn't load any database!");
this.DialogResult = DialogResult.Cancel;
this.Close();
return;
}
List<PDiff> platforms = new List<PDiff>();
for(int i = 0; i < boxes.Length; i++)
{
var ls = boxes[i];
if (ls[0].SelectedIndex != 0)
{
if (ls[1].SelectedIndex == 0)
{
MessageBox.Show("You didn't select previous version on row " + (i + 1).ToString() + "!");
return;
}
Version prev;
if (!Version.TryParse(_GetSelected(ls[1]), out prev))
{
MessageBox.Show("Something went wrong! This is not a valid version: " + _GetSelected(ls[1]));
return;
}
Version cur;
if (!Version.TryParse(_GetSelected(ls[0]), out cur))
{
MessageBox.Show("Something went wrong! This is not a valid version: " + _GetSelected(ls[0]));
return;
}
if(prev.Equals(cur))
{
MessageBox.Show("Can't select previous and new as same version!");
return;
}
Version cross = new Version();
if (ls[2].SelectedIndex != 0)
{
if (!Version.TryParse(_GetSelected(ls[2]), out cross))
{
MessageBox.Show("Something went wrong! This is not a valid version: " + _GetSelected(ls[2]));
return;
}
}
var p = new PDiff();
p.Previous = prev;
p.Current = cur;
p.Cross = cross;
platforms.Add(p);
}
else if (ls[1].SelectedIndex != 0 || ls[2].SelectedIndex != 0)
{
MessageBox.Show("You selected something on row " + (i + 1).ToString() + " but the current version was not marked!");
return;
}
}
if(platforms.Count == 0)
{
MessageBox.Show("Didn't select any platforms!");
this.DialogResult = DialogResult.Cancel;
this.Close();
return;
}
foreach(var p in platforms)
{
Library l = null;
if(!db.Versions.TryGetValue(p.Previous, out l))
{
MessageBox.Show("Something went wrong! Missing version in database.");
return;
}
var v = new VLib();
v.Version = p.Previous;
v.Library = l;
v.IdToOffset = new Dictionary<ulong, uint>(l.Values);
foreach (var pair in l.Values)
v.OffsetToId[pair.Value] = pair.Key;
p.prev = v;
l = null;
if (!db.Versions.TryGetValue(p.Current, out l))
{
MessageBox.Show("Something went wrong! Missing version in database.");
return;
}
// Technically it could work to just replace? But it's troublesome. Don't comment this out, because we didn't support it below with generating new ID without checking old first!
if(l.Values != null && l.Values.Count != 0)
{
MessageBox.Show("The current version " + l.Version.ToString() + " is not empty! This could cause issues, aborting.");
return;
}
v = new VLib();
v.Version = p.Current;
v.Library = l;
if (l.Values != null)
{
v.IdToOffset = new Dictionary<ulong, uint>(l.Values);
foreach (var pair in l.Values)
v.OffsetToId[pair.Value] = pair.Key;
}
p.cur = v;
if(!p.Cross.Equals(new Version()))
{
l = null;
if (!db.Versions.TryGetValue(p.Cross, out l))
{
MessageBox.Show("Something went wrong! Missing version in database.");
return;
}
if (l.Values == null || l.Values.Count == 0)
{
MessageBox.Show("The cross version " + l.Version.ToString() + " is empty! This could cause issues, aborting.");
return;
}
v = new VLib();
v.Version = p.Cross;
v.Library = l;
v.IdToOffset = new Dictionary<ulong, uint>(l.Values);
foreach (var pair in l.Values)
v.OffsetToId[pair.Value] = pair.Key;
p.cross = v;
}
}
foreach(var p in platforms)
{
p.diff_data = this.LoadFile(p.Previous.ToString(), p.Current.ToString(), p.prev.Library, p.cur.Library);
if (p.diff_data == null)
return;
if(p.cross != null)
{
p.cross_data = this.LoadFile(p.Cross.ToString(), p.Current.ToString(), p.cross.Library, p.cur.Library);
if (p.cross_data == null)
return;
}
}
foreach(var p in platforms)
{
if (!this.DoDiff(p, db, true))
return;
}
foreach (var p in platforms)
{
if (!this.DoDiff(p, db, false))
throw new InvalidOperationException();
}
var bld = new StringBuilder();
bld.AppendLine("Results:");
foreach(var p in platforms)
{
bld.AppendLine();
bld.AppendLine(p.Current.ToString() + ":");
bld.AppendLine("Matched: " + p.stats_assigned_simple);
bld.AppendLine("Removed: " + p.stats_removed);
bld.AppendLine("Added (total): " + p.stats_added_total);
bld.AppendLine("Added (new): " + p.stats_added_unique);
bld.AppendLine("Added (shared): " + p.stats_added_shared);
bld.AppendLine("Cross-check failed: " + p.stats_crossfailed);
bld.AppendLine("Cross-check wrong: " + p.stats_wrong);
bld.AppendLine("Cross-check missing: " + p.stats_missing);
}
bld.AppendLine();
bld.AppendLine("Shared means we didn't actually assign a new ID but shared it with the cross-check version.");
bld.AppendLine("Failed cross-check is normal and can mean that this portion is not present in cross-check version.");
bld.AppendLine("Missing cross-check is normal, it means the diff decided to omit this address, usually they are locs that weren't supposed to be there anyway.");
bld.AppendLine("Wrong cross-check is not normal, and means that the cross-check diff would have assigned a different ID than the one we did now. This may be bad, but unknown for sure.");
MessageBox.Show(bld.ToString(), "Info");
this.DialogResult = DialogResult.OK;
this.Close();
}
private bool DoDiff(PDiff p, Database db, bool checkPhase)
{
if (checkPhase)
{
foreach (var pair in p.diff_data.offsets)
{
if (!p.prev.OffsetToId.ContainsKey(pair.Key))
{
p.stats_missing++;
//MessageBox.Show("Previous didn't have an offset marked as an ID in version " + p.Previous.ToString() + "! Do a normal diff first.");
//return false;
}
}
foreach (var k in p.diff_data.prevbad)
{
if (!p.prev.OffsetToId.ContainsKey(k))
{
p.stats_missing++;
//MessageBox.Show("Previous didn't have an offset marked as an ID in version " + p.Previous.ToString() + "! Do a normal diff first.");
//return false;
}
}
// Do we care about missing stuff in cross?
/*if (p.cross_data != null)
{
foreach (var pair in p.cross_data.offsets)
{
if (!p.cross.OffsetToId.ContainsKey(pair.Key))
{
p.stats_missing++;
//MessageBox.Show("Cross didn't have an offset marked as an ID in version " + p.Cross.ToString() + "! Do a normal diff first.");
//return false;
}
}
foreach (var k in p.cross_data.prevbad)
{
if (!p.cross.OffsetToId.ContainsKey(k))
{
p.stats_missing++;
//MessageBox.Show("Cross didn't have an offset marked as an ID in version " + p.Cross.ToString() + "! Do a normal diff first.");
//return false;
}
}
}*/
return true;
}
if (p.cur.Library.Values == null)
p.cur.Library.Values = new SortedDictionary<ulong, uint>();
var revp = new Dictionary<uint, ulong>(p.prev.OffsetToId);
var revc = new Dictionary<uint, ulong>(p.cur.OffsetToId);
foreach(var pair in p.diff_data.offsets)
{
ulong id = 0;
if (!p.prev.OffsetToId.TryGetValue(pair.Key, out id)) // This is the missing part from before
continue;
p.stats_assigned_simple++;
p.cur.Library.Values[id] = pair.Value;
revc[pair.Value] = id;
if(p.cross_data != null)
{
uint cprev = 0;
if (p.cross.IdToOffset.TryGetValue(id, out cprev))
{
uint cnext = 0;
if (p.cross_data.offsets.TryGetValue(cprev, out cnext))
{
if (cnext != pair.Value)
p.stats_wrong++;
}
else
p.stats_crossfailed++;
}
else
p.stats_crossfailed++;
}
}
p.stats_removed += p.diff_data.prevbad.Count;
foreach(var bad in p.diff_data.curbad)
{
ulong id = 0;
p.stats_added_total++;
if(p.cross_data != null)
{
uint orig = 0;
if(p.cross_data.offsets2.TryGetValue(bad, out orig))
{
// This may fail due to missing from before.
p.cross.OffsetToId.TryGetValue(orig, out id);
}
}
if(id != 0)
{
// Success! didn't create new ID.
p.stats_added_shared++;
}
else
{
id = ++db.HighVID;
p.stats_added_unique++;
}
p.cur.Library.Values[id] = bad;
revc[bad] = id;
}
if(p.diff_data != null)
{
foreach (var pair in p.diff_data.hash)
{
ulong id;
if (revp.TryGetValue(pair.Key, out id))
{
if (p.prev.Library.Hashes == null)
p.prev.Library.Hashes = new SortedDictionary<ulong, ulong>();
p.prev.Library.Hashes[id] = pair.Value;
}
}
foreach (var pair in p.diff_data.hash2)
{
ulong id;
if (revc.TryGetValue(pair.Key, out id))
{
if (p.cur.Library.Hashes == null)
p.cur.Library.Hashes = new SortedDictionary<ulong, ulong>();
p.cur.Library.Hashes[id] = pair.Value;
}
}
}
return true;
}
private uint ParseOffset(string text, Library l)
{
if (!text.StartsWith("0x"))
return uint.MaxValue;
text = text.Substring(2);
long v;
if (!long.TryParse(text, System.Globalization.NumberStyles.AllowHexSpecifier, null, out v) || v < 0)
return uint.MaxValue;
if (v < l.BaseAddress)
return uint.MaxValue;
v -= l.BaseAddress;
if (v >= uint.MaxValue)
return uint.MaxValue;
return (uint)v;
}
private FData LoadFile(string ver1, string ver2, Library lib1, Library lib2)
{
var of = new OpenFileDialog();
of.AddExtension = true;
of.DefaultExt = "txt";
of.CheckFileExists = true;
of.Title = "Select the output.txt file from IDADiffCalculator " + ver1 + " to " + ver2;
if (of.ShowDialog() != DialogResult.OK)
return null;
var fi = new System.IO.FileInfo(of.FileName);
if (!fi.Name.Equals("output.txt", StringComparison.OrdinalIgnoreCase))
{
if (MessageBox.Show("The selected file is not output.txt! Continue anyway?", "Warning", MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
return null;
}
var fi_prev = new System.IO.FileInfo(System.IO.Path.Combine(fi.DirectoryName, "output_unmatched_prev.txt"));
var fi_next = new System.IO.FileInfo(System.IO.Path.Combine(fi.DirectoryName, "output_unmatched_next.txt"));
var fihash_prev = new System.IO.FileInfo(System.IO.Path.Combine(fi.DirectoryName, "output_hash_prev.txt"));
var fihash_next = new System.IO.FileInfo(System.IO.Path.Combine(fi.DirectoryName, "output_hash_next.txt"));
if (!fi_prev.Exists || !fi_next.Exists)
{
MessageBox.Show("Failed to find output_unmatched_prev.txt or output_unmatched_next.txt! These two files are also needed.", "Error", MessageBoxButtons.OK);
return null;
}
if (!fihash_prev.Exists || !fihash_next.Exists)
{
MessageBox.Show("Failed to find output_hash_prev.txt or output_hash_next.txt! These two files are also needed.", "Error", MessageBoxButtons.OK);
return null;
}
var f = new FData();
using(var reader = fi.OpenText())
{
string l;
while((l = reader.ReadLine()) != null)
{
if (l.Length == 0)
break;
}
while((l = reader.ReadLine()) != null)
{
var spl = l.Split(new[] { '\t' }, StringSplitOptions.None);
if(spl.Length != 2)
{
MessageBox.Show("output.txt file had invalid format!");
return null;
}
uint k = this.ParseOffset(spl[0], lib1);
if(k == uint.MaxValue)
{
MessageBox.Show("Failed to parse offset from output.txt file!");
return null;
}
uint v = this.ParseOffset(spl[1], lib2);
if(v == uint.MaxValue)
{
MessageBox.Show("Failed to parse offset from output.txt file!");
return null;
}
f.offsets.Add(k, v);
f.offsets2.Add(v, k);
}
}
using(var reader = fi_prev.OpenText())
{
string l;
while((l = reader.ReadLine()) != null)
{
if (l.Length == 0)
continue;
uint k = this.ParseOffset(l, lib1);
if(k == uint.MaxValue)
{
MessageBox.Show("Failed to parse offset from output_unmatched_prev.txt!");
return null;
}
f.prevbad.Add(k);
}
}
using (var reader = fi_next.OpenText())
{
string l;
while ((l = reader.ReadLine()) != null)
{
if (l.Length == 0)
continue;
uint k = this.ParseOffset(l, lib2);
if (k == uint.MaxValue)
{
MessageBox.Show("Failed to parse offset from output_unmatched_next.txt!");
return null;
}
f.curbad.Add(k);
}
}
using (var sw = new System.IO.StreamReader(fihash_prev.FullName, new UTF8Encoding(false)))
{
string l;
while ((l = sw.ReadLine()) != null)
{
if (l.Length == 0)
continue;
var spl = l.Split(new[] { '\t' }, StringSplitOptions.None);
if (spl.Length != 2)
throw new FormatException("Bad format: " + l);
uint a = this.ParseOffset(spl[0], lib1);
string second = spl[1];
ulong b;
if (!ulong.TryParse(second, out b))
throw new FormatException("Bad format: " + l);
f.hash[a] = b;
}
}
using (var sw = new System.IO.StreamReader(fihash_next.FullName, new UTF8Encoding(false)))
{
string l;
while ((l = sw.ReadLine()) != null)
{
if (l.Length == 0)
continue;
var spl = l.Split(new[] { '\t' }, StringSplitOptions.None);
if (spl.Length != 2)
throw new FormatException("Bad format: " + l);
uint a = this.ParseOffset(spl[0], lib2);
string second = spl[1];
ulong b;
if (!ulong.TryParse(second, out b))
throw new FormatException("Bad format: " + l);
f.hash2[a] = b;
}
}
return f;
}
private sealed class FData
{
internal Dictionary<uint, uint> offsets = new Dictionary<uint, uint>();
internal Dictionary<uint, uint> offsets2 = new Dictionary<uint, uint>();
internal List<uint> prevbad = new List<uint>();
internal List<uint> curbad = new List<uint>();
internal Dictionary<uint, ulong> hash = new Dictionary<uint, ulong>();
internal Dictionary<uint, ulong> hash2 = new Dictionary<uint, ulong>();
}
private sealed class VLib
{
internal Version Version;
internal Library Library;
internal Dictionary<uint, ulong> OffsetToId = new Dictionary<uint, ulong>();
internal Dictionary<ulong, uint> IdToOffset = new Dictionary<ulong, uint>();
}
private sealed class PDiff
{
internal Version Previous;
internal Version Current;
internal Version Cross;
internal VLib prev;
internal VLib cur;
internal VLib cross;
internal FData diff_data;
internal FData cross_data;
internal int stats_assigned_simple;
internal int stats_removed;
internal int stats_added_total;
internal int stats_added_shared;
internal int stats_added_unique;
internal int stats_wrong;
internal int stats_crossfailed;
internal int stats_missing;
}
}
}