Skip to content

Commit

Permalink
refactor:c++ implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo committed Aug 7, 2017
1 parent 7f3e383 commit c65f543
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
64 changes: 38 additions & 26 deletions lib/crc16.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,37 @@ static uint8_t const CRCLowTbl[256] = {
0x43, 0x83, 0x41, 0x81, 0x80, 0x40};

/**
* check crc16 sum by table look-up
* @param [point] *pDataIn data to be checked
* @param [int] len data length
* @return [char*] result sum, hex code string
**/
char *CRC16CheckSum(uint8_t *pDataIn, int len)
* place where magic truely works
**/
void checkSum(uint8_t *pDataIn, int len, uint8_t *sumHigh, uint8_t *sumLow)
{
uint8_t tableIndex = 0;
uint8_t sumHigh = 0xFF;
uint8_t sumLow = 0xFF;

//采用len控制for循环结束,更灵活
//这样可以对input的一部分做计算
int i = 0;
if ((0 != len) && (NULL != pDataIn))
{
for (i = len; i > 0; i--)
{
tableIndex = sumHigh ^ (*pDataIn++);
sumHigh = sumLow ^ CRCHighTbl[tableIndex];
sumLow = CRCLowTbl[tableIndex];
tableIndex = *sumHigh ^ (*pDataIn++);
*sumHigh = *sumLow ^ CRCHighTbl[tableIndex];
*sumLow = CRCLowTbl[tableIndex];
}
}
}

/**
* check crc16 sum by table look-up
* @param [point] *pDataIn data to be checked
* @param [int] len data length
* @return [char*] result sum, hex code string
**/
char *CRC16CheckSum(uint8_t *pDataIn, int len)
{
uint8_t sumHigh = 0xFF;
uint8_t sumLow = 0xFF;
checkSum(pDataIn, len, &sumHigh, &sumLow);
char *sum = new char[5];
sprintf(sum, "%02x%02x", sumHigh, sumLow);
return sum;
Expand All @@ -96,29 +105,33 @@ char *CRC16CheckSum(uint8_t *pDataIn, int len)
* check crc16 sum by table look-up
* @param [unsigned char*] *pDataIn data to be checked
* @param [int] len data length
* @param [unsigned char*] sum result variable, blow edged
* @param [unsigned char* array] sum result, BigEndian
* @return void
**/
void CRC16CheckSum(uint8_t *pDataIn, int len, uint8_t *sum)
{
uint8_t tableIndex = 0;
uint8_t sumHigh = 0xFF;
uint8_t sumLow = 0xFF;

int i = 0;
if ((0 != len) && (NULL != pDataIn))
{
for (i = len; i > 0; i--)
{
tableIndex = sumHigh ^ (*pDataIn++);
sumHigh = sumLow ^ CRCHighTbl[tableIndex];
sumLow = CRCLowTbl[tableIndex];
}
}
checkSum(pDataIn, len, &sumHigh, &sumLow);
*sum++ = sumHigh;
*sum = sumLow;
}

/**
* check crc16 sum by table look-up
* @param [unsigned char*] *pDataIn data to be checked
* @param [int] len data length
* @param [unsigned short* number] sum result
* @return void
**/
void CRC16CheckSum(uint8_t *pDataIn, int len, uint16_t *sum)
{
uint8_t sumHigh = 0xFF;
uint8_t sumLow = 0xFF;
checkSum(pDataIn, len, &sumHigh, &sumLow);
*sum = (sumHigh << 8 | sumLow);
}

/**
* verify crc16 sum by table look-up
* @param [unsigned char*] *pDataIn data to be checked
Expand All @@ -131,16 +144,15 @@ bool CRC16VerifySum(uint8_t *pDataIn, int len)
uint8_t sumHigh = 0xFF;
uint8_t sumLow = 0xFF;

int i = 0;
if ((0 == len) || (NULL == pDataIn))
{
return false;
}

int i = 0;
for (i = len; i > 0; i--)
{
tableIndex = sumHigh ^ (*pDataIn++);

sumHigh = sumLow ^ CRCHighTbl[tableIndex];
sumLow = CRCLowTbl[tableIndex];
}
Expand Down
7 changes: 7 additions & 0 deletions test_cpp/crc16_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ TEST_CASE("CRC16 sum are computed")
REQUIRE(sum == "96ce");
}

SECTION("Checksum can return an unsigned short number")
{
uint16_t sum;
CRC16CheckSum(stream, 24, &sum);
REQUIRE(sum == 38606);
}

SECTION("Verifysum should be passed")
{
bool isValid = CRC16VerifySum(stream, 26);
Expand Down

0 comments on commit c65f543

Please sign in to comment.