Zlib crc32 结合字节序格式

Zlib crc32 combine endian format

我刚刚浏览了 Zlib CRC32 组合函数的代码,但我对 CRC32 输入的字节序感到困惑。它只适用于大端吗?如果我有小端格式,我应该在使用该函数之前先进行字节交换吗?提前致谢。

/* ========================================================================= */
local uLong crc32_combine_(crc1, crc2, len2)
uLong crc1;
uLong crc2;
z_off64_t len2;
{
int n;
unsigned long row;
unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */

/* degenerate case (also disallow negative lengths) */
if (len2 <= 0)
    return crc1;

/* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
    odd[n] = row;
    row <<= 1;
}

/* put operator for two zero bits in even */
gf2_matrix_square(even, odd);

/* put operator for four zero bits in odd */
gf2_matrix_square(odd, even);

/* apply len2 zeros to crc1 (first square will put the operator for one
   zero byte, eight zero bits, in even) */
do {
    /* apply zeros operator for this bit of len2 */
    gf2_matrix_square(even, odd);
    if (len2 & 1)
        crc1 = gf2_matrix_times(even, crc1);
    len2 >>= 1;

    /* if no more bits set, then done */
    if (len2 == 0)
        break;

    /* another iteration of the loop with odd and even swapped */
    gf2_matrix_square(odd, even);
    if (len2 & 1)
        crc1 = gf2_matrix_times(odd, crc1);
    len2 >>= 1;

    /* if no more bits set, then done */
} while (len2 != 0);

/* return combined crc */
crc1 ^= crc2;
return crc1;

}

所有 zlib 都适用于小端或大端架构。

crc32_combine() 的参数没有“字节顺序”。 crc1crc2 参数作为 32 位整数传递,而不是字节序列,因此没有字节序。

顺便说一下,there is more recent code for crc32_combine() 这样效率更高一些。