formats.exposed/public/png/crc32.js

29 lines
577 B
JavaScript

// @ts-check
/** * @type {number[]} */
const crcTable = [];
for (let i = 0; i < 256; i++) {
let b = i;
for (let j = 0; j < 8; j++) {
b = b & 1 ? 0xedb88320 ^ (b >>> 1) : b >>> 1;
}
crcTable[i] = b >>> 0;
}
/**
* Compute the CRC32 checksum of some `Uint8Array`s.
* @param {Uint8Array[]} uint8arrays
* @returns {number}
*/
export default (...uint8arrays) => {
let crc = -1;
for (const bytes of uint8arrays) {
for (let i = 0; i < bytes.length; i++) {
crc = crcTable[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
}
}
return (crc ^ -1) >>> 0;
};