如何为Ogg建立CRC32表? [英] How to build CRC32 table for Ogg?
问题描述
来自此答案我修改了以下代码:
From this answer I adapted the code below:
function _makeCRCTable() {
const CRCTable = new Uint32Array(256);
for (let i = 256; i--;) {
let char = i;
for (let j = 8; j--;) {
char = char & 1 ? 3988292384 ^ char >>> 1 : char >>> 1;
}
CRCTable[i] = char;
}
return CRCTable;
}
此代码生成表格如此处,但对于Ogg,我需要另一个表格-如此处。
This code generates table as here, but for Ogg I need another table - as here.
来自 Ogg文档:
32位CRC值(直接算法,初始val和最终XOR = 0,
生成多项式= 0x04c11db7)
32 bit CRC value (direct algorithm, initial val and final XOR = 0, generator polynomial=0x04c11db7)
parseInt('04c11db7', 16)
返回 79764919
-我尝试过此多项式,但结果表不正确。
return 79764919
- I tried this polynomial but resulting table is not correct.
我是CRC字段的新手,如我发现的 CRC32算法有一些变体。 / p>
I am new to the CRC field, as I found there are a few variations of CRC32 algorithm.
我不确定javascript的优先级,但是异或需要在转换后发生:
I'm not sure of javascript precedence, but the xor needs to occur after the shift:
char = char & 1 ? 3988292384 ^ (char >>> 1) : char >>> 1;
但是您显示的第一个表似乎正确,因为table [128] = table [0x80] = 3988292384 = 0xEDB88320,即0x104c11db7位反转,然后右移一位。
However the first table you show seems correct, as table[128] = table[0x80] = 3988292384 = 0xEDB88320 which is 0x104c11db7 bit reversed, then shifted right one bit.
第二个表用于左移CRC,其中table [1] = x04c11db7。在这种情况下,内部循环将包括以下内容:
The second table you have is for a left shifting CRC, where table[1] = x04c11db7. In this case the inner loop would include something like this:
let char = i << 24;
for (let j = 8; j--;) {
char = char & 0x80000000 ? 0x04c11db7 ^ char << 1 : char << 1;
}
示例C代码进行比较,为模式{0x01},{0x01,0x00},{0x01,0x00,0x00},{0x01,0x00,0x00,0x00}生成crc。
Example C code for comparison, generates crc for the patterns {0x01}, {0x01,0x00}, {0x01,0x00,0x00}, {0x01,0x00,0x00,0x00}.
#include <stdio.h>
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
uint32_t crctbl[256];
void gentbl(void)
{
uint32_t crc;
uint32_t b;
uint32_t c;
uint32_t i;
for(c = 0; c < 0x100; c++){
crc = c<<24;
for(i = 0; i < 8; i++){
b = crc>>31;
crc <<= 1;
crc ^= (0 - b) & 0x04c11db7;
}
crctbl[c] = crc;
}
}
uint32_t crc32(uint8_t * bfr, size_t size)
{
uint32_t crc = 0;
while(size--)
crc = (crc << 8) ^ crctbl[(crc >> 24)^*bfr++];
return(crc);
}
int main(int argc, char** argv)
{
uint32_t crc;
uint8_t bfr[4] = {0x01,0x00,0x00,0x00};
gentbl();
crc = crc32(bfr, 1); /* 0x04c11db7 */
printf("%08x\n", crc);
crc = crc32(bfr, 2); /* 0xd219c1dc */
printf("%08x\n", crc);
crc = crc32(bfr, 3); /* 0x01d8ac87 */
printf("%08x\n", crc);
crc = crc32(bfr, 4); /* 0xdc6d9ab7 */
printf("%08x\n", crc);
return(0);
}
对于JS:
function _makeCRC32Table() {
const polynomial = 79764919;
const mask = 2147483648;
const CRCTable = new Uint32Array(256);
for (let i = 256; i--;) {
let char = i << 24;
for (let j = 8; j--;) {
char = char & mask ? polynomial ^ char << 1 : char << 1;
}
CRCTable[i] = char;
}
return CRCTable;
}
如何使用此表:
[1, 0].reduce((crc, byte) => crc << 8 >>> 0 ^ CRCTable[crc >>> 24 ^ byte], 0) >>> 0
在这里,我们添加了>> 0
接受数字模块-因为JS中没有unsigned int- JavaScript没有整数。它只有双精度浮点数。
Here we added >>> 0
that takes the module of the number - because there is no unsigned int in JS - JavaScript doesn't have integers. It only has double precision floating-point numbers.
请注意,对于Ogg,您必须以相反的顺序设置生成的CRC。
Note that for Ogg you must set generated CRC in the reverse order.
这篇关于如何为Ogg建立CRC32表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!