fix(crc32): use correct init value for ESP-IDF ROM CRC32

esp_rom_crc32_le(0xFFFFFFFF, ...) inverts the input (~0xFFFFFFFF = 0),
so the effective init is 0x00000000, not 0xFFFFFFFF (standard zlib).
This caused all NVS page/entry CRC checks to fail silently.
This commit is contained in:
kerms 2026-03-11 11:03:42 +01:00
parent 0e2f8c4828
commit 1601c0ad38
Signed by: kerms
GPG Key ID: 5432C10DDCF8DAD5
1 changed files with 2 additions and 2 deletions

View File

@ -1,6 +1,6 @@
/**
* Pre-computed CRC32 lookup table using reflected polynomial 0xEDB88320.
* Compatible with zlib.crc32() used by ESP-IDF.
* Compatible with esp_rom_crc32_le(0xFFFFFFFF, ...) used by ESP-IDF NVS/OTA.
*/
const CRC32_TABLE: Uint32Array = (() => {
const table = new Uint32Array(256);
@ -16,7 +16,7 @@ const CRC32_TABLE: Uint32Array = (() => {
/** Compute CRC32 of a Uint8Array. Returns unsigned 32-bit integer. */
export function crc32(data: Uint8Array): number {
let crc = 0xFFFFFFFF;
let crc = 0x00000000;
for (let i = 0; i < data.length; i++) {
crc = (crc >>> 8) ^ CRC32_TABLE[(crc ^ data[i]) & 0xFF];
}