@@ -33,12 +33,13 @@ uint32_t Crc32_Mpeg2(const uint8_t* DataIn,
3333 /* Set the initial value. */
3434 uint32_t CrcResult = UINT32_C (0xFFFFFFFF );
3535
36+ /* Loop counter for iterating through input data */
3637 size_t LoopCnt ;
3738
3839 /* Loop through the input data stream */
3940 for (LoopCnt = 0U ; LoopCnt < DataLength ; ++ LoopCnt )
4041 {
41- /* CRC32/MPEG2 Table based on nibbles. */
42+ /* Lookup table for CRC32/MPEG-2 based on 4-bit nibbles */
4243 static const uint32_t Crc32_Mpeg2_Table [16U ] =
4344 {
4445 UINT32_C (0x00000000 ), UINT32_C (0x04C11DB7 ),
@@ -50,44 +51,55 @@ uint32_t Crc32_Mpeg2(const uint8_t* DataIn,
5051 UINT32_C (0x350C9B64 ), UINT32_C (0x31CD86D3 ),
5152 UINT32_C (0x3C8EA00A ), UINT32_C (0x384FBDBD )
5253 };
53-
54+
55+ /*current byte from the input */
5456 const uint8_t DataByte = DataIn [LoopCnt ];
5557
5658 /* Perform the CRC32/MPEG2 algorithm. */
59+
60+ /* Index for first nibble lookup */
5761 uint_fast8_t DataIndex =
58- ( ((uint_fast8_t ) (CrcResult >> 28U ))
59- ^ ((uint_fast8_t ) (DataByte >> 4U )))
60- & UINT8_C (0x0F );
62+ ( ((uint_fast8_t ) (CrcResult >> 28U ))
63+ ^ ((uint_fast8_t ) (DataByte >> 4U )))
64+ & UINT8_C (0x0F );
65+
6166
62- CrcResult =
67+
68+ CrcResult =
6369 (uint32_t ) ( (uint32_t ) (CrcResult << 4U )
6470 & UINT32_C (0xFFFFFFF0 ))
6571 ^ Crc32_Mpeg2_Table [DataIndex ];
6672
67- DataIndex =
73+
74+ DataIndex =
6875 (((uint_fast8_t ) (CrcResult >> 28U ))
6976 ^ ((uint_fast8_t ) (DataByte ))) & UINT8_C (0x0F );
7077
71- CrcResult =
78+ CrcResult =
7279 (uint32_t ) ( (uint32_t ) (CrcResult << 4U )
7380 & UINT32_C (0xFFFFFFF0 ))
7481 ^ Crc32_Mpeg2_Table [DataIndex ];
82+
7583 }
7684
7785 return CrcResult ;
7886}
7987
8088int main (void )
8189{
90+ /* Test data: ASCII values for characters '1' to '9' */
8291 static const uint8_t TestData [9U ] =
8392 {
8493 0x31U , 0x32U , 0x33U , 0x34U , 0x35U , 0x36U , 0x37U , 0x38U , 0x39U
8594 };
8695
96+ /* Expected CRC32 result for the test data */
8797 static const uint32_t ControlValue = UINT32_C (0x0376E6E7 );
8898
99+ /* Compute CRC32 result for the test data */
89100 const uint32_t TestResult = Crc32_Mpeg2 (TestData , 9U );
90101
102+ /* Check if computed result matches expected value */
91103 const bool TestResultIsOk =
92104 (bool ) ((TestResult == ControlValue ) ? true : false);
93105
@@ -100,5 +112,6 @@ int main(void)
100112 printf ("CRC32 test result is: Not OK\n" );
101113 }
102114
115+ /* Return 0 if test passed, -1 otherwise */
103116 return ((TestResultIsOk == true) ? 0 : -1 );
104117}
0 commit comments