-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (95 loc) · 3.08 KB
/
Program.cs
File metadata and controls
126 lines (95 loc) · 3.08 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
using System;
using System.IO;
using ExFat;
using ExFat.IO;
using ExFat.Partition;
using ExFat.Partition.Entries;
class Program
{
static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("usage: FindAffectedFilesExfat <device or image> <map file>");
return 1;
}
string imageFile = args[0];
string mapFile = args[1];
using (var image = File.OpenRead(imageFile))
using (var partition = new ExFatPartition(image))
{
var map = Map.Parse(mapFile);
RecurseFindMapEntries(partition, partition.RootDirectoryDataDescriptor, "", map);
bool notFoundHeading = false;
foreach (var entry in map.Entries)
{
if (!entry.Found)
{
if (!notFoundHeading)
{
Console.WriteLine("The following regions were not found linked to any file:");
notFoundHeading = true;
}
Console.WriteLine("- 0x{0:X12}: {1} bytes", entry.Offset, entry.Length);
}
}
}
return 0;
}
static void RecurseFindMapEntries(ExFatPartition partition, DataDescriptor directoryDescriptor, string path, Map map)
{
var entries = partition.GetMetaEntries(directoryDescriptor);
foreach (var entry in entries)
{
if (entry.Primary is FileExFatDirectoryEntry fileEntry)
{
string subpath = path + "/" + entry.ExtensionsFileName;
var report = EnumerateClustersAndCompareWithMap(partition, entry, subpath, map);
if (report != null)
{
Console.WriteLine(report.Path);
foreach (var region in report.MissingRegions)
Console.WriteLine("* {0} bytes at offset {1} (0x{1:X12})", region.LocalLength, region.LocalOffset);
Console.WriteLine();
}
if (fileEntry.FileAttributes.Value.HasFlag(ExFatFileAttributes.Directory))
RecurseFindMapEntries(partition, entry.DataDescriptor, subpath, map);
}
}
}
static Report? EnumerateClustersAndCompareWithMap(ExFatPartition partition, ExFatMetaDirectoryEntry entry, string path, Map map)
{
Report? report = null;
int clusterLength = partition.BytesPerCluster;
long localOffset = 0;
long remainingBytes = (long)entry.DataDescriptor.LogicalLength;
Cluster cluster = entry.DataDescriptor.FirstCluster;
while (remainingBytes > 0)
{
long clusterOffset = partition.GetClusterOffset(cluster);
if (clusterLength > remainingBytes)
clusterLength = (int)remainingBytes;
var mapEntry = map.Find(clusterOffset, clusterLength);
if (mapEntry != null)
{
if (mapEntry.Found)
Console.Error.WriteLine("SANITY FAILURE: Multiple links found to cluster at offset {0}", clusterOffset);
map.SplitIfNecessary(mapEntry, clusterOffset, clusterLength);
mapEntry.Found = true;
mapEntry.LocalOffset = localOffset + (mapEntry.Offset - clusterOffset);
mapEntry.LocalLength = (int)Math.Min(remainingBytes, mapEntry.Length);
report ??= new Report(path);
report.MissingRegions.Add(mapEntry);
}
if (cluster.IsLast)
break;
localOffset += clusterLength;
remainingBytes -= clusterLength;
if (entry.DataDescriptor.Contiguous)
cluster = cluster + 1;
else
cluster = partition.GetNextCluster(cluster);
}
return report;
}
}