forked from Blackfrosch/VAGEDCSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmBinCompare.cs
447 lines (389 loc) · 15.3 KB
/
frmBinCompare.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using Be.Windows.Forms;
namespace VAGSuite
{
public partial class frmBinCompare : DevExpress.XtraEditors.XtraForm
{
private SymbolCollection m_symbols = new SymbolCollection();
public SymbolCollection Symbols
{
get { return m_symbols; }
set { m_symbols = value; }
}
private string _currentfilename = string.Empty;
private string _comparefilename = string.Empty;
private bool m_OutsideSymbolRangeCheck = false;
public bool OutsideSymbolRangeCheck
{
get { return m_OutsideSymbolRangeCheck; }
set { m_OutsideSymbolRangeCheck = value; }
}
public frmBinCompare()
{
InitializeComponent();
}
/// <summary>
/// checks if address is outside all symbol range addresses
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
private bool CheckOutsideSymbolRange(int address)
{
if (m_symbols == null) return true;
foreach (SymbolHelper sh in m_symbols)
{
if (address >= sh.Flash_start_address && address < (sh.Flash_start_address + sh.Length))
{
return false;
}
}
return true;
}
private bool ByteCompare(Byte[] ib1, Byte[] ib2, int address)
{
for (int t = 0; t < 16; t++)
{
if (m_OutsideSymbolRangeCheck)
{
if (CheckOutsideSymbolRange(address + t))
{
if ((Byte)ib1.GetValue(address + t) != (Byte)ib2.GetValue(address+t))
{
return false;
}
}
}
else
{
if ((Byte)ib1.GetValue(address + t) != (Byte)ib2.GetValue(address+t))
{
return false;
}
}
}
return true;
}
public void SetCurrentFilename(string filename)
{
label1.Text = Path.GetFileName(filename);
_currentfilename = filename;
}
public void SetCompareFilename(string filename)
{
label2.Text = Path.GetFileName(filename);
_comparefilename = filename;
}
bool[] linediffs;
List<string> currstrList = new List<string>();
List<string> compstrList = new List<string>();
public void CompareFiles()
{
//listBox1.BeginUpdate();
//listBox2.BeginUpdate();
//listBox1.Items.Clear();
//listBox2.Items.Clear();
currstrList.Clear();
compstrList.Clear();
try
{
if (File.Exists(_currentfilename))
{
if (File.Exists(_comparefilename))
{
FileInfo fi = new FileInfo(_currentfilename);
FileInfo fi2 = new FileInfo(_comparefilename);
linediffs = new bool[fi.Length / 16];
if (true)// (fi.Length == fi2.Length)
{
byte[] currfile = File.ReadAllBytes(_currentfilename);
byte[] compfile = File.ReadAllBytes(_comparefilename);
// mark the differences in the hexboxes
//FileStream fsi1 = File.OpenRead(_currentfilename);
//BinaryReader br1 = new BinaryReader(fsi1);
//FileStream fsi2 = File.OpenRead(_comparefilename);
//BinaryReader br2 = new BinaryReader(fsi2);
for (int tel = 0; tel < (fi.Length / 16); tel++)
{
try
{
//Byte[] ib1 = br1.ReadBytes(16);
//Byte[] ib2 = br2.ReadBytes(16);
if (!ByteCompare(currfile, compfile, (tel * 16)))
{
linediffs[tel] = true;
Int32 addr = tel * 16;
string s1 = addr.ToString("X6") + ": ";
string s2 = s1;
for (int t = 0; t < 16; t++)
{
Byte b1 = (Byte)currfile.GetValue((tel*16) + t);
Byte b2 = (Byte)compfile.GetValue((tel * 16) + t);
s1 += b1.ToString("X2") + " ";
s2 += b2.ToString("X2") + " ";
}
// add to string array
//listBox1.Items.Add(s1);
currstrList.Add(s1);
compstrList.Add(s2);
//listBox2.Items.Add(s2);
}
else
{
linediffs[tel] = false;
if (!checkButton1.Checked)
{
Int32 addr = tel * 16;
string s1 = addr.ToString("X6") + ": ";
string s2 = s1;
for (int t = 0; t < 16; t++)
{
Byte b1 = (Byte)currfile.GetValue((tel * 16) + t);
Byte b2 = (Byte)compfile.GetValue((tel * 16) + t);
s1 += b1.ToString("X2") + " ";
s2 += b2.ToString("X2") + " ";
}
// listBox1.Items.Add(s1);
// listBox2.Items.Add(s2);
currstrList.Add(s1);
compstrList.Add(s2);
}
}
}
catch (Exception cE)
{
Console.WriteLine(cE.Message);
}
}
//fsi1.Close();
//br1.Close();
//fsi2.Close();
//br2.Close();
}
}
}
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
//listBox1.EndUpdate();
//listBox2.EndUpdate();
listBox1.Count = currstrList.Count;
listBox2.Count = compstrList.Count;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
this.Close();
}
private void checkButton1_CheckedChanged(object sender, EventArgs e)
{
// recompare
CompareFiles();
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
/* e.DrawBackground();
e.DrawFocusRectangle();
if(e.Index < 0) return;
if (e.Index < listBox1.Items.Count)
{
if (!checkButton1.Checked)
{
if (linediffs[e.Index]) e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Red, e.Bounds);
else e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Black, e.Bounds);
}
else
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Black, e.Bounds);
}
}*/
try
{
if (e.Index >= 0)
{
string symbolName = string.Empty;
try
{
int idx = currstrList[e.Index].IndexOf(':');
if (idx > 0)
{
int address = Convert.ToInt32(currstrList[e.Index].Substring(0, idx), 16);
if (address > 0)
{
symbolName = Tools.Instance.GetSymbolNameByAddressInRange(address, address + 0x0F);
}
}
}
catch (Exception)
{
}
string drawItem = currstrList[e.Index];
if(symbolName != string.Empty) drawItem += " << " + symbolName;
if (!checkButton1.Checked)
{
if (linediffs[e.Index]) listBox1.DefaultDrawItem(e, drawItem, Brushes.Red);
else listBox1.DefaultDrawItem(e, drawItem, Brushes.Black);
}
else
{
listBox1.DefaultDrawItem(e, drawItem, Brushes.Black);
}
}
}
catch (Exception)
{
}
}
private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
{
/*e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index < 0) return;
if (e.Index < listBox1.Items.Count)
{
if (!checkButton1.Checked)
{
if (linediffs[e.Index]) e.Graphics.DrawString(listBox2.Items[e.Index].ToString(), listBox2.Font, Brushes.Red, e.Bounds);
else e.Graphics.DrawString(listBox2.Items[e.Index].ToString(), listBox2.Font, Brushes.Black, e.Bounds);
}
else
{
e.Graphics.DrawString(listBox2.Items[e.Index].ToString(), listBox2.Font, Brushes.Black, e.Bounds);
}
}*/
try
{
if (!checkButton1.Checked)
{
if (linediffs[e.Index]) listBox2.DefaultDrawItem(e, compstrList[e.Index], Brushes.Red);
else listBox2.DefaultDrawItem(e, compstrList[e.Index], Brushes.Black);
}
else
{
listBox2.DefaultDrawItem(e, compstrList[e.Index], Brushes.Black);
}
}
catch (Exception)
{
}
}
void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
Application.DoEvents();
}
catch (Exception)
{
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
Application.DoEvents();
}
catch (Exception)
{
}
}
void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
listBox1.SelectedIndex = listBox2.SelectedIndex;
Application.DoEvents();
}
catch (Exception)
{
}
}
private void simpleButton2_Click(object sender, EventArgs e)
{
if (!checkButton1.Checked)
{
try
{
int currindex = listBox1.SelectedIndex;
while (!linediffs[++currindex] && currindex < linediffs.Length) ;
listBox1.SelectedIndex = currindex;
listBox2.SelectedIndex = currindex;
Application.DoEvents();
}
catch (Exception)
{
}
}
}
private void simpleButton3_Click(object sender, EventArgs e)
{
if (!checkButton1.Checked)
{
try
{
int currindex = listBox1.SelectedIndex;
while (!linediffs[--currindex] && currindex >= 0) ;
listBox1.SelectedIndex = currindex;
listBox2.SelectedIndex = currindex;
Application.DoEvents();
}
catch (Exception)
{
}
}
}
void listBox2_OnVerticalScroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
listBox1.TopIndex = listBox2.TopIndex;
}
void listBox1_OnVerticalScroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
listBox2.TopIndex = listBox1.TopIndex;
}
private void listBox1_MouseHover(object sender, EventArgs e)
{
}
private void simpleButton4_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName, false))
{
for(int i = 0; i < currstrList.Count; i ++)
{
string symbolName = string.Empty;
try
{
int idx = currstrList[i].IndexOf(':');
if (idx > 0)
{
int address = Convert.ToInt32(currstrList[i].Substring(0, idx), 16);
if (address > 0)
{
symbolName = Tools.Instance.GetSymbolNameByAddressInRange(address, address + 0x0F);
}
}
}
catch (Exception)
{
}
string exportLine = currstrList[i] + " ---- " + compstrList[i];
if (symbolName != string.Empty) exportLine += " << " + symbolName;
sw.WriteLine(exportLine);
}
}
}
}
}
}