diff --git a/index.js b/index.js index 496a841..a49ac9f 100644 --- a/index.js +++ b/index.js @@ -323,6 +323,102 @@ function updatePacketStats() { document.getElementById('packet-error-bits-per-block').innerText = (HAMMING_ERROR_CORRECTION ? ERROR_CORRECTION_BLOCK_SIZE : 0).toLocaleString(); document.getElementById('packet-error-bit-count').innerText = getPacketErrorBitCount(); } +function calcCrc( + bytes, + size, + polynomial, + { + initialization = 0, + reflectIn = false, + reflectOut = false, + xorOut = 0 + } = {} +) { + if(bytes.length === 0) return 0; + const validBits = (1 << size) - 1; + const mostSignificantBit = 1 << size - 1; + const bitsBeforeLastByte = size - 8; + + // setup our initial value + let crc = initialization; + + function reverseBits(value, size) { + let reversed = 0; + for(let i = 0; i < size; i++) { + // if bit position is on + if(value & (1<= 32 && crc & mostSignificantBit) { + crc >>>= 0; + } + return crc; +} +function crc8(bytes) { return calcCrc(bytes, 8, 0x07); } +function crc16(bytes) { + return calcCrc( + bytes, + 16, + 0x8005, + { + initialization: 0, + reflectIn: true, + reflectOut: true, + xorOut: 0 + } + ); +} +function crc32(bytes) { + return calcCrc( + bytes, + 32, + 0x04C11DB7, + { + initialization: 0xFFFFFFFF, + reflectIn: true, + reflectOut: true, + xorOut: 0x0 + } + ); +} + function drawChannels() { const sampleRate = getAudioContext().sampleRate; const fftSize = 2 ** FFT_SIZE_POWER;