-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAT.cc
executable file
·66 lines (57 loc) · 1.5 KB
/
FAT.cc
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
#include "FAT.h"
#include <iostream>
#include <sstream>
#include "lib/easyloggingpp/easylogging++.h"
namespace libFAT {
namespace Human68k {
FAT::FAT(FATType type, const void* buffer, size_t fat_size) {
if (type == FAT12) {
for (int i = 0; i < fat_size; i++) {
const size_t offset = 3 * i / 2;
uint12_t value;
if (i % 2 == 0) {
value = (((uint8_t*)buffer)[offset + 1] & 0x0f) << 8;
value |= ((uint8_t*)buffer)[offset];
} else {
value = (((uint8_t*)buffer)[offset] & 0xf0) >> 4;
value |= ((uint8_t*)buffer)[offset + 1] << 4;
}
entries_.push_back(value);
}
} else if (type == FAT16) {
for (int i = 0; i < fat_size; i++) {
entries_.push_back(((uint16_t*)buffer)[i]);
}
}
}
const FAT::uint12_t FAT::GetEntry(size_t cluster) {
try {
return entries_.at(cluster);
} catch (const std::out_of_range& oob) {
LOG(INFO) << "Unexpected OOB on FAT";
return 0x000;
}
}
const FAT::uint12_t FAT::GetNextFreeEntry() {
for (const auto& entry : entries_) {
if (entry == 0x000) {
return entry;
}
}
return 0x000;
}
void FAT::DebugPrint() const {
LOG(INFO) << "FAT contents:";
std::string line;
for (int i = 0; i < entries_.size(); i++) {
if (i % 16 == 0) {
LOG(INFO) << line;
line = "";
}
char buf[512];
sprintf(buf, "%03x ", entries_[i]);
line.append(buf);
}
}
} // namespace Human68k
} // namespace libFAT