简体   繁体   English

在 C++ 中解析 .wav 文件的数据卡盘

[英]Parsing Data chuck of a .wav file in c++

I am using a SHA1 Hash to verify the authenticity of a .wav file.我正在使用 SHA1 哈希来验证.wav文件的真实性。 The SHA1 function I am using takes in three parameter:我使用的 SHA1 函数接受三个参数:

  1. a pointer to the authentication file with .auth extension指向带有.auth扩展名的身份验证文件的指针
  2. The data buffer read from the .wav file (which must be less than 42000 bytes in size).wav文件读取的数据缓冲区(大小必须小于 42000 字节)
  3. The length of the buffer缓冲区长度
for (int i = 0; i < size_buffer; i++) {
    DataBuffer[i] = fgetc(WavResult);
}
util_sha1_calculate(&AuthContext, DataBuffer, size_buffer);

How can I set a read function to read 42000 bytes, transfer the data to util_sha1_calculate(&AuthContext, DataBuffer, size_buffer) , and start from the position it left off when the loop is repeated, and proceed to read then next 42000 bytes?如何设置读取函数以读取 42000 字节,将数据传输到util_sha1_calculate(&AuthContext, DataBuffer, size_buffer) ,并从重复循环时停止的位置开始,然后继续读取下一个 42000 字节?

You can put your shown for loop inside of another outer loop that runs until EOF is reached, eg:您可以将显示的for循环放在另一个运行直到达到 EOF 的外部循环中,例如:

size_t size;
int ch;

while (!feof(WavResult))
{
    size = 0;
    for (int i = 0; i < size_buffer; i++) {
        ch = fgetc(WavResult);
        if (ch == EOF) break;
        DataBuffer[size++] = (char) ch;
    }
    if (size > 0)
        util_sha1_calculate(&AuthContext, DataBuffer, size);
}

However, you should consider replacing the inner for loop with a single call to fread() instead, eg:但是,您应该考虑使用对fread()的单个调用替换内部for循环,例如:

size_t nRead;
while ((nRead = fread(DataBuffer, 1, size_buffer, WavResult)) > 0)
{
    util_sha1_calculate(&AuthContext, DataBuffer, nRead);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM