-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
462 lines (356 loc) · 12.7 KB
/
Copy pathmain.py
File metadata and controls
462 lines (356 loc) · 12.7 KB
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
# Firmware Diff Tool - main.py
import sys
import difflib
from pathlib import Path
import filecmp
import argparse
def read_text_file(path):
with open(path, 'r', errors='ignore') as f:
return f.readlines()
def diff_text_files(file1, file2):
f1 = read_text_file(file1)
f2 = read_text_file(file2)
return difflib.unified_diff(
f1, f2,
fromfile=file1,
tofile=file2,
lineterm=''
)
def read_binary_file(path):
with open(path, 'rb') as f:
return f.read()
def diff_binary_files(file1, file2, ignore_ff=False, ignore_00=False):
b1 = read_binary_file(file1)
b2 = read_binary_file(file2)
len1 = len(b1)
len2 = len(b2)
min_len = min(len1, len2)
all_diffs = []
# Compare common length
for i in range(min_len):
if b1[i] != b2[i]:
if ignore_ff and (b1[i] == 0xFF or b2[i] == 0xFF):
continue
if ignore_00 and (b1[i] == 0x00 or b2[i] == 0x00):
continue
all_diffs.append((i, b1[i], b2[i]))
# Handle trailing differences
if len1 > len2:
for i in range(min_len, len1):
if ignore_ff and b1[i] == 0xFF:
continue
if ignore_00 and b1[i] == 0x00:
continue
all_diffs.append((i, b1[i], None))
elif len2 > len1:
for i in range(min_len, len2):
if ignore_ff and b2[i] == 0xFF:
continue
if ignore_00 and b2[i] == 0x00:
continue
all_diffs.append((i, None, b2[i]))
extra_info = None
if len1 != len2:
extra_info = f"File sizes differ: {file1}={len1} bytes, {file2}={len2} bytes"
return all_diffs, extra_info, len1, len2
def build_display_diffs(all_diffs, max_diffs):
display_diffs = all_diffs[:max_diffs]
truncated_display = len(all_diffs) > max_diffs
return display_diffs, truncated_display
def build_changed_regions(all_diffs):
if not all_diffs:
return []
offsets = sorted(offset for offset, _, _ in all_diffs)
regions = []
start = offsets[0]
end = offsets[0]
for offset in offsets[1:]:
if offset == end + 1:
end = offset
else:
regions.append((start, end))
start = offset
end = offset
regions.append((start, end))
return regions
def analyze_binary_diff(
file1,
file2,
max_diffs=100
):
all_diffs, extra_info, len1, len2 = diff_binary_files(
file1,
file2
)
display_diffs, truncated_display = build_display_diffs(all_diffs, max_diffs)
regions = build_changed_regions(all_diffs)
result = {
"mode": "bin",
"file1": file1,
"file2": file2,
"len1": len1,
"len2": len2,
"files_identical": (len(all_diffs) == 0 and len1 == len2),
"size_mismatch": (len1 != len2),
"all_diffs": all_diffs,
"display_diffs": display_diffs,
"diff_count_total": len(all_diffs),
"display_diff_count": len(display_diffs),
"truncated_display": truncated_display,
"regions": regions,
"extra_info": extra_info,
}
return result
def format_changed_regions(regions):
lines = []
lines.append("Changed regions:")
if not regions:
lines.append(" None")
return lines
for start, end in regions:
size = end - start + 1
lines.append(f" 0x{start:08X} - 0x{end:08X} ({size} bytes)")
return lines
def format_binary_diff(result, max_diffs=100, regions_only=False):
lines = []
file1 = result["file1"]
file2 = result["file2"]
len1 = result["len1"]
len2 = result["len2"]
extra_info = result["extra_info"]
display_diffs = result["display_diffs"]
regions = result["regions"]
lines.append("Binary diff result")
lines.append(f"File 1: {file1} ({len1} bytes)")
lines.append(f"File 2: {file2} ({len2} bytes)")
lines.append("")
if result["files_identical"]:
lines.append("Files are identical.")
return lines
def format_byte(b):
if b is None:
return "<none>"
return f"0x{b:02X}"
if display_diffs and not regions_only:
if result["truncated_display"]:
lines.append(
f"Showing first {result['display_diff_count']} of {result['diff_count_total']} byte differences:"
)
else:
lines.append(f"Total differences: {result['diff_count_total']}")
for offset, v1, v2 in display_diffs:
lines.append(
f"offset 0x{offset:08X} : {format_byte(v1)} -> {format_byte(v2)}"
)
lines.append("")
if extra_info:
lines.append(extra_info)
if regions or extra_info:
lines.append("")
lines.extend(format_changed_regions(regions))
return lines
def compare_directories(dir1, dir2):
dcmp = filecmp.dircmp(dir1, dir2)
lines = []
lines.append("Directory diff result")
lines.append(f"Directory 1: {dir1}")
lines.append(f"Directory 2: {dir2}")
lines.append("")
summary = {
"left_only": 0,
"right_only": 0,
"diff_files": 0,
"same_files": 0,
}
def walk_diff(dcmp_obj, rel_path=""):
current = rel_path if rel_path else "."
lines.append(f"[{current}]")
if dcmp_obj.left_only:
lines.append(" Only in directory 1:")
for name in dcmp_obj.left_only:
lines.append(f" {name}")
summary["left_only"] += len(dcmp_obj.left_only)
if dcmp_obj.right_only:
lines.append(" Only in directory 2:")
for name in dcmp_obj.right_only:
lines.append(f" {name}")
summary["right_only"] += len(dcmp_obj.right_only)
if dcmp_obj.diff_files:
lines.append(" Changed files:")
for name in dcmp_obj.diff_files:
lines.append(f" {name}")
summary["diff_files"] += len(dcmp_obj.diff_files)
if dcmp_obj.same_files:
lines.append(" Identical files:")
for name in dcmp_obj.same_files:
lines.append(f" {name}")
summary["same_files"] += len(dcmp_obj.same_files)
if (not dcmp_obj.left_only and not dcmp_obj.right_only and
not dcmp_obj.diff_files and not dcmp_obj.same_files):
lines.append(" No file differences found.")
lines.append("")
for subdir_name, sub_dcmp in dcmp_obj.subdirs.items():
sub_rel_path = f"{rel_path}/{subdir_name}" if rel_path else subdir_name
walk_diff(sub_dcmp, sub_rel_path)
walk_diff(dcmp)
lines.append("Summary:")
lines.append(f" only in directory 1 : {summary['left_only']}")
lines.append(f" only in directory 2 : {summary['right_only']}")
lines.append(f" changed files : {summary['diff_files']}")
lines.append(f" identical files : {summary['same_files']}")
return lines
def save_or_print(lines, output_file=None, saved_message="Result saved to"):
if output_file:
with open(output_file, 'w', encoding='utf-8') as f:
for line in lines:
f.write(line + '\n')
print(f"{saved_message}: {output_file}")
else:
for line in lines:
print(line)
def parse_args():
description = """Firmware Diff Tool
Primary use:
Compare firmware / binary files with region-based change analysis.
Additional modes:
text Compare two text files and output unified diff
dir Compare two directories and list changed / added / removed files
Use 'python main.py <mode> -h' for detailed help of each mode.
"""
epilog = """Examples:
python main.py --version
python main.py -h
python main.py bin old.bin new.bin
python main.py bin old.bin new.bin --regions-only
python main.py bin old.bin new.bin --max-diffs 10
python main.py text file1.txt file2.txt
python main.py text file1.txt file2.txt result.diff
python main.py dir dirA dirB
python main.py dir dirA dirB dir_result.txt
Tip:
Use 'python main.py <mode> -h' for mode-specific help.
"""
parser = argparse.ArgumentParser(
prog="main.py",
description=description,
epilog=epilog,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--version",
action="version",
version="fw_diff_tool free v0.4"
)
subparsers = parser.add_subparsers(
dest="mode",
required=True,
title="modes",
metavar="{text,bin,dir}",
help="Available comparison modes. Use '<mode> -h' for more details.",
)
parser_text = subparsers.add_parser(
"text",
help="Compare two text files",
description="Text mode\n\nCompare two text files and output unified diff.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser_text.add_argument("file1", help="Path to first text file")
parser_text.add_argument("file2", help="Path to second text file")
parser_text.add_argument("output_file", nargs="?", help="Optional output file")
parser_bin = subparsers.add_parser(
"bin",
help="Compare two binary / firmware files",
description="Binary mode\n\nCompare two binary / firmware files and show byte differences and changed regions.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser_bin.add_argument("file1", help="Path to first binary file")
parser_bin.add_argument("file2", help="Path to second binary file")
parser_bin.add_argument("output_file", nargs="?", help="Optional output file")
parser_bin.add_argument(
"--max-diffs",
type=int,
default=100,
help="Maximum number of byte differences to display (default: 100)"
)
parser_bin.add_argument(
"--regions-only",
action="store_true",
help="Show changed regions only"
)
parser_dir = subparsers.add_parser(
"dir",
help="Compare two directories",
description="Directory mode\n\nCompare two directories and list differences.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser_dir.add_argument("dir1", help="Path to first directory")
parser_dir.add_argument("dir2", help="Path to second directory")
parser_dir.add_argument("output_file", nargs="?", help="Optional output file")
return parser.parse_args()
def check_file_exists(path):
p = Path(path)
if not p.exists():
print(f"Error: file not found: {path}")
sys.exit(1)
if not p.is_file():
print(f"Error: not a file: {path}")
sys.exit(1)
def check_dir_exists(path):
p = Path(path)
if not p.exists():
print(f"Error: directory not found: {path}")
sys.exit(1)
if not p.is_dir():
print(f"Error: not a directory: {path}")
sys.exit(1)
def check_parent_dir_for_output(path):
p = Path(path)
parent = p.parent
if str(parent) and str(parent) != "." and not parent.exists():
print(f"Error: output directory not found: {parent}")
sys.exit(1)
if __name__ == "__main__":
args = parse_args()
if args.mode == "text":
check_file_exists(args.file1)
check_file_exists(args.file2)
if args.output_file:
check_parent_dir_for_output(args.output_file)
diff = list(diff_text_files(args.file1, args.file2))
if args.output_file:
with open(args.output_file, 'w', encoding='utf-8') as f:
for line in diff:
f.write(line + '\n')
print(f"Text diff result saved to: {args.output_file}")
else:
print("Text diff result")
print("")
for line in diff:
print(line)
elif args.mode == "bin":
check_file_exists(args.file1)
check_file_exists(args.file2)
if args.output_file:
check_parent_dir_for_output(args.output_file)
max_diffs = args.max_diffs
if max_diffs <= 0:
print("Error: --max-diffs must be greater than 0")
sys.exit(1)
result = analyze_binary_diff(
args.file1,
args.file2,
max_diffs=args.max_diffs
)
lines = format_binary_diff(
result,
max_diffs=args.max_diffs,
regions_only=args.regions_only
)
save_or_print(lines, args.output_file, "Binary diff result saved to")
elif args.mode == "dir":
check_dir_exists(args.dir1)
check_dir_exists(args.dir2)
if args.output_file:
check_parent_dir_for_output(args.output_file)
lines = compare_directories(args.dir1, args.dir2)
save_or_print(lines, args.output_file, "Directory diff result saved to")