Implemented CRC32-IEEE for PNG files

Update crc32.mjs, data-utils.mjs, and png-utils.mjs
This commit is contained in:
2025-04-03 23:52:14 +02:00
parent 3e2b917d21
commit c58cb8a405
3 changed files with 100 additions and 6 deletions

View File

@@ -0,0 +1,33 @@
/**
* @param data {Uint8Array}
* @param crc32 {number}
* @return {number}
*/
export function stepCrc32IEEE(data, crc32 = 0xffffffff) {
for (let i = 0; i < data.length; i++) {
crc32 ^= data[i];
for (let j = 0; j < 8; j++) {
crc32 = (crc32 >>> 1) ^ (crc32 & 1 ? 0xedb88320 : 0);
}
}
return crc32;
}
/**
* @param crc32 {number}
* @return {number}
*/
export function finishCrc32IEEE(crc32) {
return (crc32 ^ 0xffffffff) >>> 0;
}
/**
* Calculates the CRC32-IEEE for the given data
* @param data {Uint8Array}
* @param crc32 {number}
* @return {number} The resulting CRC32
*/
export function crc32IEEE(data, crc32 = 0xffffffff) {
return finishCrc32IEEE(stepCrc32IEEE(data, crc32));
}