Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions code_snippets/chapter17/chapter17_02-001_crc32_mpeg2.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ uint32_t Crc32_Mpeg2(const uint8_t* DataIn,
/* Set the initial value. */
uint32_t CrcResult = UINT32_C(0xFFFFFFFF);

/* Loop counter for iterating through input data */
size_t LoopCnt;

/* Loop through the input data stream */
for(LoopCnt = 0U; LoopCnt < DataLength; ++LoopCnt)
{
/* CRC32/MPEG2 Table based on nibbles. */
/* Lookup table for CRC32/MPEG-2 based on 4-bit nibbles */
static const uint32_t Crc32_Mpeg2_Table[16U] =
{
UINT32_C(0x00000000), UINT32_C(0x04C11DB7),
Expand All @@ -50,44 +51,55 @@ uint32_t Crc32_Mpeg2(const uint8_t* DataIn,
UINT32_C(0x350C9B64), UINT32_C(0x31CD86D3),
UINT32_C(0x3C8EA00A), UINT32_C(0x384FBDBD)
};


/*current byte from the input */
const uint8_t DataByte = DataIn[LoopCnt];

/* Perform the CRC32/MPEG2 algorithm. */

/* Index for first nibble lookup */
uint_fast8_t DataIndex =
( ((uint_fast8_t) (CrcResult >> 28U))
^ ((uint_fast8_t) (DataByte >> 4U)))
& UINT8_C(0x0F);
( ((uint_fast8_t) (CrcResult >> 28U))
^ ((uint_fast8_t) (DataByte >> 4U)))
& UINT8_C(0x0F);


CrcResult =

CrcResult =
(uint32_t) ( (uint32_t) (CrcResult << 4U)
& UINT32_C(0xFFFFFFF0))
^ Crc32_Mpeg2_Table[DataIndex];

DataIndex =

DataIndex =
(((uint_fast8_t) (CrcResult >> 28U))
^ ((uint_fast8_t) (DataByte))) & UINT8_C(0x0F);

CrcResult =
CrcResult =
(uint32_t) ( (uint32_t) (CrcResult << 4U)
& UINT32_C(0xFFFFFFF0))
^ Crc32_Mpeg2_Table[DataIndex];

}

return CrcResult;
}

int main(void)
{
/* Test data: ASCII values for characters '1' to '9' */
static const uint8_t TestData[9U] =
{
0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, 0x38U, 0x39U
};

/* Expected CRC32 result for the test data */
static const uint32_t ControlValue = UINT32_C(0x0376E6E7);

/* Compute CRC32 result for the test data */
const uint32_t TestResult = Crc32_Mpeg2(TestData, 9U);

/* Check if computed result matches expected value */
const bool TestResultIsOk =
(bool) ((TestResult == ControlValue) ? true : false);

Expand All @@ -100,5 +112,6 @@ int main(void)
printf("CRC32 test result is: Not OK\n");
}

/* Return 0 if test passed, -1 otherwise */
return ((TestResultIsOk == true) ? 0 : -1);
}
Loading