【发布时间】:2021-02-20 11:34:12
【问题描述】:
尝试实施 exFAT 引导校验和,如以下 3.4 节所述:
https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification
我的代码没有产生正确的校验和。 :(
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
/* test for filename in parameters */
if ( argc != 2 )
{
/* assume argv[0] has the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
/* assume argv[1] has the filename to process */
FILE *filename = fopen( argv[1], "rb" );
/* check that file exists */
if ( filename == 0 )
{
printf( "Could not open %s\n", argv[1] );
exit (1);
}
else
{
unsigned char cbytes[5632];
int ibytes = fread(cbytes, 1, sizeof(cbytes), filename);
if (ibytes != 5632)
{
printf( "Can't read 5632 bytes from %s\n", argv[1] );
exit (1);
}
fclose( filename );
uint32_t chksum=0;
for (int index = 0; index < 5632; index++)
{
if ((index == 106) || (index == 107) || (index == 112))
{ continue; }
chksum = ((chksum&1) ? 0x80000000 : 0) + (chksum>>1) + cbytes[index];
}
printf("%8x\n", chksum);
}
}
}
是的,我已经检查过这个过去的问题(作者显然也永远无法获得正确的校验和)。
谁能发现我做错了什么?
【问题讨论】:
-
尝试
unsigned char cbytes[5632];,就像 MS 页面所做的那样,链接的问题建议。 -
它确实有所作为(输出不同的结果),但它仍然不会产生正确的值。
-
介绍一下你真正使用的代码怎么样?您实际提供的代码似乎不是它,因为它在几个地方拼错了
uint32_t。 -
您似乎假设扇区为 512 字节,但 exFAT 允许其他扇区大小。引导扇区中有一个字段表示扇区大小。
-
这正是我用gcc编译并使用的代码。我会重新检查拼写。 :) 是的,我确实假设了 512 字节扇区(我在闪存介质上从未见过其他任何东西),在这种情况下,偏移量 108 确实有 0x09(512 字节)。
标签: c