FLAC - CRC8 and CRC16 Calculation

CRC8 and CRC16 Calculation

The CRC calculation used by flac are not fully documented.

The parameters for the CRC8 are:

data bitorder: msbit-first p-bitorder: msbit-first polynomial: x8 + x2 + x + 1 p-factor 0x07 pre-invert: NO post-invert: NO

The lookup table is generated using:

// for each of the 256 entries for (lv_N = 0; lv_N < 256; ++lv_N) { // for each bit of a byte // - data-bitorder = msb therefore start with high-byte (shift left) for (lv_C = (UINT8)lv_N, lv_K = 0; lv_K < 8; ++lv_K) { // if bit is set then shift and xor with polynomial, otherwise only shift // - p-bitorder = msb therefore 0x07 // - data-bitorder = msb therefore test high-bit and shift left if (lv_C & 0x80) lv_C = (UINT8)(0x07 ^ (lv_C << 1)); else lv_C <<= 1; } // store entry gv_FlacCrc8Table = lv_C; }


The parameters for the CRC16 are:

data bitorder: msbit-first p-bitorder: msbit-first polynomial: x16 + x15 + x13 + 1 p-factor 0x8005 pre-invert: NO post-invert: NO

The lookup table is generated using:

// for each of the 256 entries for (lv_N = 0; lv_N < 256; ++lv_N) { // for each bit of a byte // - data-bitorder = msb therefore start with high-byte (shift left) for (lv_C = (UINT16)(lv_N<<8), lv_K = 0; lv_K < 8; ++lv_K) { // if bit is set then shift and xor with polynomial, otherwise only shift // - p-bitorder = msb therefore 0x8005 // - data-bitorder = msb therefore test high-bit and shift left if (lv_C & 0x8000) lv_C = (UINT16)(0x8005 ^ (lv_C << 1)); else lv_C <<= 1; } // store entry gv_FlacCrc16Table = lv_C; }


Read more about this topic:  FLAC

Famous quotes containing the word calculation:

    “To my thinking” boomed the Professor, begging the question as usual, “the greatest triumph of the human mind was the calculation of Neptune from the observed vagaries of the orbit of Uranus.”
    “And yours,” said the P.B.
    Samuel Beckett (1906–1989)