Skip to content

Add option to list cluster chains #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion man/fatcat.1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ be unreadable.
.RS 4
This will display information about the given \fBcluster\fP. It will display its address, which
is the offset of the cluster in the image, and the FAT entries (next cluster, unallocated
or end of cluster)
or end of cluster). A list of cluster chains is displayed if \fB-n\fB is used.
.RE

.PP
\fB\-n\fP
.RS 4
List cluster chains when displaying cluster information.
.RE

.PP
Expand Down
16 changes: 8 additions & 8 deletions src/analysis/FatChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,25 @@ void FatChains::displayOrphaned(list<FatChain> orphanedChains)
cout << endl;
}

int FatChains::chainSize(int cluster, bool *isContiguous)
int FatChains::chainSize(int cluster, list<Segment>& segments)
{
set<int> visited;
int length = 0;
bool stop;

if (isContiguous != NULL) {
*isContiguous = true;
}
int start = cluster;

do {
stop = true;
int currentCluster = cluster;
visited.insert(cluster);
length++;
cluster = system.nextCluster(cluster);
if (system.validCluster(cluster) && cluster!=FAT_LAST) {
if (currentCluster+1 != cluster && isContiguous != NULL) {
*isContiguous = false;
if (cluster==FAT_LAST) {
segments.push_back(pair(start, currentCluster));
} else if (system.validCluster(cluster)) {
if (currentCluster+1 != cluster) {
segments.push_back(pair(start, currentCluster));
start = cluster;
}
if (visited.find(cluster) != visited.end()) {
cerr << "! Loop detected, " << currentCluster << " points to " << cluster << " that I already met" << endl;
Expand Down
4 changes: 3 additions & 1 deletion src/analysis/FatChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using namespace std;

typedef pair<int, int> Segment;

class FatChains : public FatModule
{
public:
Expand Down Expand Up @@ -55,7 +57,7 @@ class FatChains : public FatModule
/**
* Size of a chain in the FAT
*/
int chainSize(int cluster, bool *isContiguous);
int chainSize(int cluster, list<Segment>& segments);

protected:
bool saveEntries;
Expand Down
26 changes: 22 additions & 4 deletions src/fatcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void usage()
cout << endl;
cout << "FAT Hacking" << endl;
cout << " -@ [cluster]: Get the cluster address and information" << endl;
cout << " -n: enable listing of cluster chains in cluster information" << endl;
cout << " -2: analysis & compare the 2 FATs" << endl;
cout << " -b [file]: backup the FATs (see -t)" << endl;
cout << "* -p [file]: restore (patch) the FATs (see -t)" << endl;
Expand Down Expand Up @@ -117,6 +118,9 @@ int main(int argc, char *argv[])
// -@: get the cluster address
bool address = false;

// -n: list cluster chains
bool showChains = false;

// -k: analysis the chains
bool chains = false;

Expand Down Expand Up @@ -158,7 +162,7 @@ int main(int argc, char *argv[])
OutputFormatType outputFormat = Default;

// Parsing command line
while ((index = getopt(argc, argv, "il:L:r:R:s:dc:hx:2@:ob:p:w:v:mt:Sze:O:fk:a:F:")) != -1) {
while ((index = getopt(argc, argv, "il:L:r:R:s:dc:hx:2@:nob:p:w:v:mt:Sze:O:fk:a:F:")) != -1) {
switch (index) {
case 'a':
attributesProvided = true;
Expand Down Expand Up @@ -202,6 +206,9 @@ int main(int argc, char *argv[])
address = true;
cluster = ATOU(optarg);
break;
case 'n':
showChains = true;
break;
case 'o':
chains = true;
break;
Expand Down Expand Up @@ -326,16 +333,27 @@ int main(int argc, char *argv[])
int next2 = fat.nextCluster(cluster, 1);
printf("FAT1: %u (%08x)\n", next1, next1);
printf("FAT2: %u (%08x)\n", next2, next2);
bool isContiguous = false;

FatChains chains(fat);
unsigned long long size = chains.chainSize(cluster, &isContiguous);
list<Segment> segments;
unsigned long long size = chains.chainSize(cluster, segments);
printf("Chain size: %llu (%llu / %s)\n", size, size*fat.bytesPerCluster, prettySize(size*fat.bytesPerCluster).c_str());
if (isContiguous) {
if (segments.size() < 2) {
printf("Chain is contiguous\n");
} else {
printf("Chain is not contiguous\n");
}

if (showChains) {
list<Segment>::iterator it;
int num=1;
for (it=segments.begin(); it != segments.end(); num++,it++) {
int start = it->first;
int end = it->second;
int size = end - start + 1;
printf("Chain %d: %d..%d (%d clusters)\n", num, start, end, size);
}
}
} else if (chains) {
FatChains chains(fat);
chains.chainsAnalysis();
Expand Down