correct CRC-32 implementation; remove old code;

pull/12/head
Bryan Biedenkapp 4 years ago
parent 28c0d383d5
commit bfe8d65630

@ -368,19 +368,23 @@ bool CRC::checkCRC32(const uint8_t *in, uint32_t length)
uint8_t crc8[4U];
};
crc32 = 0xFFFFFFFFU;
uint32_t i = 0;
crc32 = 0x00000000U;
for (uint32_t i = 0U; i < (length - 4U); i++)
crc32 = (crc32 << 8) ^ CRC32_TABLE[((crc32 >> 24) ^ in[i]) & 0xFF];
for (uint32_t j = (length - 4U); j-- > 0; i++) {
uint32_t idx = ((crc32 >> 24) ^ in[i]) & 0xFFU;
crc32 = (CRC32_TABLE[idx] ^ (crc32 << 8)) & 0xFFFFFFFFU;
}
crc32 = ~crc32;
crc32 &= 0xFFFFFFFFU;
#if DEBUG_CRC
uint32_t inCrc = (in[length - 4U] << 24) | (in[length - 3U] << 16) | (in[length - 2U] << 8) | (in[length - 1U] << 0);
LogDebug(LOG_HOST, "CRC:checkCRC32(), crc = $%08X, in = $%08X, len = %u", crc32, inCrc, length);
#endif
return crc8[0U] == in[length - 4U] && crc8[1U] == in[length - 3U] && crc8[2U] == in[length - 2U] && crc8[3U] == in[length - 1U];
return crc8[0U] == in[length - 1U] && crc8[1U] == in[length - 2U] && crc8[2U] == in[length - 3U] && crc8[3U] == in[length - 4U];
}
/// <summary>
@ -398,21 +402,25 @@ void CRC::addCRC32(uint8_t* in, uint32_t length)
uint8_t crc8[4U];
};
crc32 = 0xFFFFFFFFU;
uint32_t i = 0;
crc32 = 0x00000000U;
for (uint32_t i = 0U; i < (length - 4U); i++)
crc32 = (crc32 << 8) ^ CRC32_TABLE[((crc32 >> 24) ^ in[i]) & 0xFF];
for (uint32_t j = (length - 4U); j-- > 0; i++) {
uint32_t idx = ((crc32 >> 24) ^ in[i]) & 0xFFU;
crc32 = (CRC32_TABLE[idx] ^ (crc32 << 8)) & 0xFFFFFFFFU;
}
crc32 = ~crc32;
crc32 &= 0xFFFFFFFFU;
#if DEBUG_CRC
LogDebug(LOG_HOST, "CRC:addCRC32(), crc = $%08X, len = %u", crc32, length);
#endif
in[length - 4U] = crc8[0U];
in[length - 3U] = crc8[1U];
in[length - 2U] = crc8[2U];
in[length - 1U] = crc8[3U];
in[length - 1U] = crc8[0U];
in[length - 2U] = crc8[1U];
in[length - 3U] = crc8[2U];
in[length - 4U] = crc8[3U];
}
/// <summary>

@ -62,6 +62,9 @@ namespace edac
/// <summary>Encode 32-bit CRC.</summary>
static void addCRC32(uint8_t* in, uint32_t length);
/// <summary>Generate 32-bit CRC.</summary>
static uint32_t crc32(const uint8_t *in, uint32_t length);
/// <summary>Generate 8-bit CRC.</summary>
static uint8_t crc8(const uint8_t* in, uint32_t length);

@ -96,7 +96,6 @@ bool DataBlock::decode(const uint8_t* data, const DataHeader header)
uint32_t count = P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
if ((m_serialNo == (header.getBlocksToFollow() - 1) && header.getBlocksToFollow() > 1) ||
(m_headerSap == PDU_SAP_EXT_ADDR && m_serialNo == 0U)) {
count = P25_PDU_CONFIRMED_DATA_LENGTH_BYTES - 4U;
m_lastBlock = true;
} else {
if (header.getBlocksToFollow() <= 1) {
@ -115,6 +114,8 @@ bool DataBlock::decode(const uint8_t* data, const DataHeader header)
// if this is extended addressing and the first block decode the SAP and LLId
if (m_headerSap == PDU_SAP_EXT_ADDR && m_serialNo == 0U) {
count = P25_PDU_CONFIRMED_DATA_LENGTH_BYTES - 4U;
m_sap = buffer[5U] & 0x3FU; // Service Access Point
m_llId = (buffer[2U] << 16) + (buffer[3U] << 8) + buffer[4U]; // Logical Link ID
@ -295,34 +296,3 @@ uint32_t DataBlock::getData(uint8_t* buffer) const
return 0U;
}
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
uint16_t DataBlock::crc9()
{
assert(m_data != NULL);
uint16_t crc = 0x00U;
/*
edac::CRC::tmp_bit_cnt = 135U;
edac::CRC::crc9(&crc, m_serialNo, 7U);
for (uint8_t i = 0; i < P25_PDU_CONFIRMED_DATA_LENGTH_BYTES; i++) {
edac::CRC::crc9(&crc, m_data[i], 8U);
}
edac::CRC::crc9Finish(&crc, 8U);
crc = ~crc;
crc &= 0x01FFU;
crc ^= 0x01FFU;
#if DEBUG_P25_PDU_DATA
LogDebug(LOG_P25, "DataBlock::crc9(), crc = $%03X", crc);
#endif
*/
return crc;
}

@ -86,9 +86,6 @@ namespace p25
uint8_t m_headerSap;
uint8_t* m_data;
/// <summary></summary>
uint16_t crc9();
};
} // namespace data
} // namespace p25

Loading…
Cancel
Save

Powered by TurnKey Linux.