mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-03 00:13:09 +00:00
21 lines
620 B
TypeScript
21 lines
620 B
TypeScript
let table: Uint32Array | null = null;
|
|
|
|
function crcTable(): Uint32Array {
|
|
if (table) return table;
|
|
table = new Uint32Array(256);
|
|
for (let n = 0; n < 256; n++) {
|
|
let c = n;
|
|
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
table[n] = c >>> 0;
|
|
}
|
|
return table;
|
|
}
|
|
|
|
/** CRC-32 (ZIP, PNG) of `data`, continuing from `crc` for streamed input. */
|
|
export function crc32(data: Uint8Array, crc = 0): number {
|
|
const t = crcTable();
|
|
let c = (crc ^ 0xffffffff) >>> 0;
|
|
for (let i = 0; i < data.length; i++) c = t[(c ^ data[i]) & 0xff] ^ (c >>> 8);
|
|
return (c ^ 0xffffffff) >>> 0;
|
|
}
|