首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在FAT16中计算文件的字节偏移量需要什么?

在FAT16中计算文件的字节偏移量需要什么?
EN

Stack Overflow用户
提问于 2016-04-08 22:20:51
回答 1查看 878关注 0票数 2

我有一个程序和一个FAT16图像。方便地,图像从引导扇区开始。在这里,我提取了根目录、每个扇区的字节和每个集群的字节。

从根目录获取子目录的字节偏移量,将r设置为从image[ 0 ]到给定的名为path的子文件夹的子目录项的字节偏移量的算法是:

代码语言:javascript
复制
    // non-C formatted externally defined values
    image = open_some_fat16_image()
    path = the_name_of_a_directory_whose_parent_is_root()
    LEN_DIR_NAME = 2
    LEN_DIRECTORY_ENTRY = 32
    FREE_ENTRY_MARKER = 0xE5
    END_OF_DIR_MARKER = 0
    OFFSET_DIR_ATTR = 11
    FLAG_DIRECTORY = 0x10
    OFFSET_FIRST_CLUSTER = 26
    current_dir = byte_loc_root;

    unsigned long r = 0;
    for ( int i = 0; i < ( *fat_fs ).root_entry_count && r == 0; i++ )
    {
        // get the name
        char dir_name[ LEN_DIR_NAME +1 ];
        unsigned long byte_loc_dir_name = current_dir + ( i * LEN_DIRECTORY_ENTRY );
        lseek( image, byte_loc_dir_name, SEEK_SET );
        read( image, dir_name, LEN_DIR_NAME );
        dir_name[ LEN_DIR_NAME ] = '\0';

        // is valid entry
        if ( FREE_ENTRY_MARKER == ( unsigned char ) dir_name[ 0 ] ) { continue; }
        if ( END_OF_DIR_MARKER == ( unsigned char ) dir_name[ 0 ] ) { break; }

        // no right whitespace
        for ( int i = 0; i < LEN_DIR_NAME; i ++ )
        {
            if ( ! isspace( dir_name[ i ] ) ) { continue; }
            dir_name[ i ] = '\0';
        }

        // is a match
        if ( 0 != strcmp( dir_name, path ) ) { continue; }

        // is a directory
        unsigned long byte_loc_dir_attr = byte_loc_dir_name + OFFSET_DIR_ATTR;
        lseek( image, byte_loc_dir_attr, SEEK_SET );
        uint8_t attr;
        read( image, &attr, 1 );
        if ( FLAG_DIRECTORY != attr ) { continue; }

        // get cluster
        unsigned long byte_loc_dir_first_cluster = byte_loc_dir_name + OFFSET_FIRST_CLUSTER;
        lseek( image, byte_loc_dir_first_cluster, SEEK_SET );
        uint16_t cluster_idx;
        read( image, & cluster_idx, LEN_FIRST_CLUSTER );

        r = cluster_idx * ( *fat_fs ).sectors_per_cluster * ( *fat_fs ).bytes_per_sector;
    }

我运行了这个程序并验证了

  • sectors_per_cluster ( == 4 ),以及
  • bytes_per_sector ( == 512 ) 匹配图像中的值(通过hex_editor )。我还验证了cluster_idx与图像中的内容匹配(通过hex_editor + FAT16浏览器)。

r = cluster_idx * sectors_per_cluster * bytes_per_sector = 960 * 4 * 512设置为: 0x1E0000。使用FAT16浏览器,我能够在我给出的子目录参数中找到文件。现在获得了其中一个文件名,我使用hex_editor在图像中查找它。子目录列表的位置是: 0x1ED200。

我非常肯定,除了r之外,我已经正确地获得了所有的值。我不确定要取哪些值才能获得r丢失的53760字节。r是如何设置的,是不是缺少了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-08 04:15:12

您应该在引导扇区内发送BPB的转储。见blocksystem#BPB

对于FAT 16,磁盘通常被布置为:

代码语言:javascript
复制
boot sector
fat 1
fat 2
root dir
cluster 2
cluster 3
...

所以你需要调整:

代码语言:javascript
复制
RootDirSectors = <round up (32 bytes * 
                           BPB.RootDirectoryEntries) to be a multiple of BPB.BytesPerSector> / BPB.BytesPerSector

Cluster2SectorNumber = 
    BPB.ReservedSectors 
     + BPB.NumberOfFats * BPB.SectorsPerFat
     + RootDirSectors

ClusterSectorNumber = 
    Cluster2SectorNumber
     + (cluster_idx - 2) * BPB.SectorsPerCluster

ClusterByteNumber =
    ClusterSectorNumber * BPB.BytesPerSector
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36510234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档