-
Notifications
You must be signed in to change notification settings - Fork 1
/
clusters.c
38 lines (34 loc) · 1 KB
/
clusters.c
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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "structs.h"
inline int get_sector(fat_desc_t* fat, int sector) {
int res = fat->disk_start;
res += fat->sector_size * sector;
return res;
}
inline int get_cluster_size(fat_desc_t* fat, int start_cluster) {
int cluster = start_cluster, size = 1;
while (cluster != 0) {
cluster = get_next_cluster(fat, cluster);
size++;
}
return size;
}
inline int get_next_cluster(fat_desc_t* fat, int cluster) {
int fat_start = get_sector(fat, fat->reserved_sectors);
uint8_t ptr[3];
fseek(fat->in, fat_start + 3 * (cluster / 2), SEEK_SET);
if (fread(&ptr, sizeof(uint8_t), 3, fat->in) != 3) {
fprintf(stderr, "Error: failed to read cluster (0x%x)\n", cluster);
exit(1);
}
int res;
if (cluster & 1)
res = (ptr[2] << 4) + ((ptr[1] & 0xf0) >> 4);
else
res = ((ptr[1] & 0x0f) << 8) + ptr[0];
if (res < 0x02 || res >= 0xff0)
res = 0;
return res;
}