在简单文件系统中计算校验和

Calculating checksums in the Simple File System

我正在开发一个用于操作 SFS 图像的控制台程序。有很多文件系统名称 SFS 所以具体来说,this 是我正在实施的规范。但是我不明白校验和是如何计算的。

这段代码我看了一段时间,很明显 这是计算校验和的地方,但我不了解 Basic。

' compute checksum
For wl = start + &H1AC To start + &H1BC
    Get ff, wl, wb
    wi = wi + wb
Next wl
wb = (256 - (wi And &HFF)) And &HFF

可以找到完整的源代码here。该片段来自 "sfs.bas" 文件,该方法称为 "format_sfs"。

我明白了。这是我的校验和方法的 C# 实现。

/// <summary>
/// Calculates the checksum of the given <paramref name="superBlock"/>.
/// </summary>
/// <param name="superBlock">The super block to calculate the checksum from.</param>
/// <returns>The calculated checksum.</returns>
private static byte Checksum(SuperBlock superBlock)
{
    byte[] bytes = Structures.ToBytes(superBlock);

    int result = 0;
    for (int i = 0x1AC; i < 0x1BD; i++)
        result += bytes[i];

    return (byte)((256 - (result & 0xFF)) & 0xFF);
}