aboutsummaryrefslogtreecommitdiffstats
path: root/libfat/fatchain.c
blob: 9853a726edd755bba3b9ed05acbd538e85577806 (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * fatchain.c
 *
 * Follow a FAT chain
 */

#include "libfatint.h"
#include "ulint.h"

/*
 * Convert a cluster number (or 0 for the root directory) to a
 * sector number.  Return -1 on failure.
 */
libfat_sector_t libfat_clustertosector(const struct libfat_filesystem *fs,
				       int32_t cluster)
{
    if (cluster == 0)
	cluster = fs->rootcluster;

    if (cluster == 0)
	return fs->rootdir;
    else if (cluster < 2 || cluster >= fs->endcluster)
	return -1;
    else
	return fs->data + ((libfat_sector_t) (cluster - 2) << fs->clustshift);
}

/*
 * Get the next sector of either the root directory or a FAT chain.
 * Returns 0 on end of file and -1 on error.
 */

libfat_sector_t libfat_nextsector(struct libfat_filesystem * fs,
				  libfat_sector_t s)
{
    int32_t cluster, nextcluster;
    uint32_t fatoffset;
    libfat_sector_t fatsect;
    uint8_t *fsdata;
    uint32_t clustmask = fs->clustsize - 1;
    libfat_sector_t rs;

    if (s < fs->data) {
	if (s < fs->rootdir)
	    return -1;

	/* Root directory */
	s++;
	return (s < fs->data) ? s : 0;
    }

    rs = s - fs->data;

    if (~rs & clustmask)
	return s + 1;		/* Next sector in cluster */

    cluster = 2 + (rs >> fs->clustshift);

    if (cluster >= fs->endcluster)
	return -1;

    switch (fs->fat_type) {
    case FAT12:
	/* Get first byte */
	fatoffset = cluster + (cluster >> 1);
	fatsect = fs->fat + (fatoffset >> LIBFAT_SECTOR_SHIFT);
	fsdata = libfat_get_sector(fs, fatsect);
	if (!fsdata)
	    return -1;
	nextcluster = fsdata[fatoffset & LIBFAT_SECTOR_MASK];

	/* Get second byte */
	fatoffset++;
	fatsect = fs->fat + (fatoffset >> LIBFAT_SECTOR_SHIFT);
	fsdata = libfat_get_sector(fs, fatsect);
	if (!fsdata)
	    return -1;
	nextcluster |= fsdata[fatoffset & LIBFAT_SECTOR_MASK] << 8;

	/* Extract the FAT entry */
	if (cluster & 1)
	    nextcluster >>= 4;
	else
	    nextcluster &= 0x0FFF;

	if (nextcluster >= 0x0FF8)
	    return 0;
	break;

    case FAT16:
	fatoffset = cluster << 1;
	fatsect = fs->fat + (fatoffset >> LIBFAT_SECTOR_SHIFT);
	fsdata = libfat_get_sector(fs, fatsect);
	if (!fsdata)
	    return -1;
	nextcluster =
	    read16((le16_t *) & fsdata[fatoffset & LIBFAT_SECTOR_MASK]);

	if (nextcluster >= 0x0FFF8)
	    return 0;
	break;

    case FAT28:
	fatoffset = cluster << 2;
	fatsect = fs->fat + (fatoffset >> LIBFAT_SECTOR_SHIFT);
	fsdata = libfat_get_sector(fs, fatsect);
	if (!fsdata)
	    return -1;
	nextcluster =
	    read32((le32_t *) & fsdata[fatoffset & LIBFAT_SECTOR_MASK]);
	nextcluster &= 0x0FFFFFFF;

	if (nextcluster >= 0x0FFFFFF8)
	    return 0;
	break;

    default:
	return -1;		/* WTF? */
    }

    return libfat_clustertosector(fs, nextcluster);
}