-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfat12++.cpp
347 lines (294 loc) · 8.96 KB
/
fat12++.cpp
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
#include <cassert>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;
constexpr int SECTOR_SIZE = 512;
const char* INDENT = "\t";
static_assert(sizeof(char) == 1, "ERROR: Program was designed for system with 8 bit chars only");
// Create string from char* padded with ' ' and maximum possible size
string get_padded_str(const char* s, int max_size) {
string result;
for (int i=0; i<max_size and s[i]!=' '; i++) {
result += s[i];
}
return result;
}
/* PACKED STRUCTS */
struct BootSector {
// BPB
char ignore1[3];
char oem[8];
uint16_t bytes_per_sector;
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t fats;
uint16_t max_root_dirs;
uint16_t sector_count;
char ignore2[1];
uint16_t sectors_per_fat;
uint16_t sectors_per_track;
uint16_t number_of_heads;
uint32_t hidden_sectors;
uint32_t total_sector_count_for_fat32;
// FAT 12 Extended
uint8_t drive_number;
char ignore4[1];
uint8_t boot_signature;
uint32_t volume_id;
char volume_label[11];
char file_system_type[8];
char boot_code[448];
uint16_t bootable_partition_signature;
}__attribute__((packed));
struct DirectoryEntry {
char file_name[8];
char file_ext[3];
// File attributes
uint8_t read_only : 1;
uint8_t hidden : 1;
uint8_t system : 1;
uint8_t volume_id : 1;
uint8_t subdirectory : 1;
uint8_t archive : 1;
uint8_t device : 1;
uint8_t unused : 1;
uint8_t reserved;
uint8_t create_time_high_res; // 10ms units, from 1-199
uint16_t create_time;
uint16_t create_date;
uint16_t last_access_date;
uint16_t ignore1;
uint16_t last_write_time;
uint16_t last_write_date;
uint16_t first_cluster;
uint32_t file_size; // in bytes
bool has_long_name() {
return (read_only | hidden | system | volume_id);
}
}__attribute__((packed));
/* CONVENIENCE FUNCTIONS */
struct FileSystemItem {
DirectoryEntry dirent;
// int cluster = -1; // -1 for root directories
FileSystemItem(DirectoryEntry _dirent)
: dirent(_dirent)
{}
virtual string name() = 0;
virtual void print_contents(int level, bool file_contents) = 0;
};
struct File : FileSystemItem {
vector<uint8_t> contents;
File(DirectoryEntry dirent, const vector<uint8_t>& cluster_data)
: FileSystemItem(dirent)
{
// cout << "FILENAME: " << name() << endl;
auto cluster = dirent.first_cluster;
cluster -= 2; // first 2 clusters are ignored
auto file_size = dirent.file_size;
auto it_begin = cluster_data.begin()+SECTOR_SIZE*cluster;
auto it_end = it_begin + file_size;
contents = vector<uint8_t>(it_begin, it_end);
}
/* Print contents of file */
void print_contents(int level, bool file_contents) override {
for (int i=0; i<level; i++) cout << INDENT;
cout << "- File: " << name() << endl;
if (not file_contents)
return;
for (int i=0; i<level; i++) cout << INDENT;
cout << " Contents: " << (char*)contents.data() << endl;
}
string name() override {
return get_padded_str(dirent.file_name, sizeof(dirent.file_name)) + "."
+ get_padded_str(dirent.file_ext, sizeof(dirent.file_ext));
;
}
};
struct Directory : FileSystemItem {
vector<FileSystemItem*> child_dirs;
Directory(DirectoryEntry dirent, const vector<uint8_t>& cluster_data)
: FileSystemItem(dirent)
{
auto cluster = dirent.first_cluster;
cluster -= 2; // first 2 clusters are ignored
auto child_dirs_data = (DirectoryEntry*)&cluster_data[cluster*SECTOR_SIZE];
const auto MAX_CHILD_DIRS = SECTOR_SIZE / 32;
for (size_t i=0; i<MAX_CHILD_DIRS; i++) {
auto *d = &child_dirs_data[i];
if (d->file_name[0] == 0x0) {
break;
} else if ((uint8_t)d->file_name[0] == 0XE5) {
// entry deleted but still available, we can skip this
continue;
}
FileSystemItem *dir;
if (d->subdirectory) {
const auto *name = d->file_name;
// Either "." or ".."
if (name[0] == '.')
continue;
dir = new Directory(*d, cluster_data);
} else {
dir = new File(*d, cluster_data);
}
child_dirs.push_back(dir);
}
}
/* Recursively print contents
*/
void print_contents(int level, bool file_contents) override
{
for (int i=0; i<level; i++)
cout << INDENT;
cout << "- Dir: " << name() << endl;
for (auto *child : child_dirs) {
auto name = child->name();
if (name == "." or name == "..")
continue;
child->print_contents(level+1, file_contents);
}
}
string name() override {
return get_padded_str(dirent.file_name, sizeof(dirent.file_name));
}
};
struct VolumeID : FileSystemItem {
VolumeID(DirectoryEntry d)
: FileSystemItem(d)
{}
void print_contents(int level, bool /*file_contents*/) override {
for(int i=0; i<level; i++) cout << INDENT;
cout << "- VolumeID: " << name() << endl;
}
string name() override {
return get_padded_str(dirent.file_name, sizeof(dirent.file_name));
}
};
/* DISK CLASS */
class Floppy {
public:
bool read(ifstream& img) {
/* READ BOOT SECTOR */
img.read(reinterpret_cast<char*>(&boot), sizeof(boot));
if (unsigned sig = boot.boot_signature; sig != 0x28 and sig != 0x29) {
cerr << "ERROR: Signature is not valid: " << sig << endl;
return false;
}
// skip reserved sectors
img.seekg((boot.reserved_sectors-1)*SECTOR_SIZE, std::ios_base::cur);
/* READ FAT */
auto fat_table_size = boot.sectors_per_fat*SECTOR_SIZE;
fat_data.resize(fat_table_size);
img.read(reinterpret_cast<char*>(fat_data.data()), fat_data.size());
// check if redundant FAT copies match the first copy
{
vector<uint8_t> fat_data_copy(fat_data.size());
for (int i=0; i<boot.fats-1; i++) {
img.read(reinterpret_cast<char*>(fat_data_copy.data()), fat_data.size());
if (fat_data != fat_data_copy) {
cerr << "FAT copy " << i+1 << " does not match the original." << endl;
return false;
}
}
}
/* READ DIRECTORIES AND CLUSTERS */
vector<DirectoryEntry> root_dirs_data(boot.max_root_dirs);
auto root_dirs_size = sizeof(DirectoryEntry)*root_dirs_data.size();
img.read(reinterpret_cast<char*>(root_dirs_data.data()), root_dirs_size);
auto sectors_read = boot.reserved_sectors + boot.sectors_per_fat*boot.fats + (boot.max_root_dirs*sizeof(DirectoryEntry)/SECTOR_SIZE);
// clusters yet to read
auto cluster_count = (boot.sector_count - sectors_read) / boot.sectors_per_cluster;
auto bytes_per_cluster = boot.sectors_per_cluster*SECTOR_SIZE;
cluster_data.resize(cluster_count*bytes_per_cluster);
img.read(reinterpret_cast<char*>(cluster_data.data()), cluster_data.size());
/* Recursively read directories */
read_dirs(root_dirs_data, cluster_data);
return true;
}
void describe_disk(bool directories=true, bool file_contents=true) {
cout << "OEM: " << get_padded_str(boot.oem, sizeof(boot.oem)) << endl;
cout << "Bytes per sector: " << boot.bytes_per_sector << endl;
cout << "Sectors per cluster: " << (int)boot.sectors_per_cluster << endl;
cout << "Reserved Sectors: " << boot.reserved_sectors << endl;
cout << "Number of FATs: " << (int)boot.fats << endl;
cout << "Max Root Directories: " << boot.max_root_dirs << endl;
cout << "Sector count: " << boot.sector_count << endl;
cout << "Sectors per FAT: " << boot.sectors_per_fat << endl;
cout << "Hidden Sectors: " << boot.hidden_sectors << endl;
cout << "Volume label: " << get_padded_str(boot.volume_label, sizeof(boot.volume_label)) << endl;
cout << "File system type: " << get_padded_str(boot.file_system_type, sizeof(boot.file_system_type)) << endl;
if (directories) {
cout << "\n";
for (auto *dir : root_dirs) {
dir->print_contents(0, file_contents);
}
}
}
private:
/* Read all the directories in the volume
*/
void read_dirs(vector<DirectoryEntry>& root_dirs_data,
const vector<uint8_t>& cluster_data) {
for (size_t i=0; i<root_dirs_data.size(); i++) {
auto *d = &root_dirs_data[i];
if (d->file_name[0] == 0x0) {
break;
} else if ((uint8_t)d->file_name[0] == 0XE5) {
// entry deleted but still available, we can skip this
continue;
}
FileSystemItem *dir;
if (d->volume_id) {
dir = new VolumeID(*d);
} else if (d->subdirectory) {
dir = new Directory(*d, cluster_data);
} else {
dir = new File(*d, cluster_data);
}
root_dirs.push_back(dir);
}
}
private:
BootSector boot;
vector<uint8_t> fat_data;
vector<FileSystemItem*> root_dirs;
vector<uint8_t> cluster_data;
};
void print_usage(const char* executable) {
cout << "USAGE:\n";
cout << " " << executable << " [-c] FILE\n";
cout << endl;
cout << " -c Prints file contents.\n";
}
int main(int argc, char** argv) {
const char *file_name;
bool file_contents = false;
if (argc == 2) {
file_name = argv[1];
} else if (argc == 3) {
if (string{"-c"} != argv[1]) {
print_usage(argv[0]);
return 1;
}
file_contents = true;
file_name = argv[2];
} else {
print_usage(argv[0]);
return 1;
}
ifstream img(file_name);
if (not img.is_open()) {
cerr << "ERROR: Could not open file: " << file_name << endl;
return 1;
}
Floppy floppy;
floppy.read(img);
floppy.describe_disk(true, file_contents);
return 0;
}