aboutsummaryrefslogtreecommitdiffstats
path: root/libfat/fatchain.c
diff options
context:
space:
mode:
Diffstat (limited to 'libfat/fatchain.c')
-rw-r--r--libfat/fatchain.c190
1 files changed, 96 insertions, 94 deletions
diff --git a/libfat/fatchain.c b/libfat/fatchain.c
index e00f46ac..9853a726 100644
--- a/libfat/fatchain.c
+++ b/libfat/fatchain.c
@@ -26,15 +26,15 @@
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);
+ 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);
}
/*
@@ -42,91 +42,93 @@ libfat_sector_t libfat_clustertosector(const struct libfat_filesystem *fs,
* Returns 0 on end of file and -1 on error.
*/
-libfat_sector_t libfat_nextsector(struct libfat_filesystem *fs,
+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);
+ 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);
}