使用下面的程序,我正在尝试计算一个64bit CRC as per the ECMA-128 standard。
测试数据是"123456789“,我正在尝试匹配here提供的相同数据,这表明CRC-64/ECMA-182的结果应该是62ec59e3f1a4f00a。不幸的是,我得到了9d13a61c0e5b0ff5,这是CRC-64/WE的结果。
我从here提供的示例代码开始。我使用ECMA-12864位循环冗余码的0x42F0E1EBA9EA3693的正常多项式表示创建了64位散列。
我得到以下VS警告: C4293:'<<':移位计数为负或太大,未定义的行为。它适用于此宏:
BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );据我所知,0被移位了64位的全范围,这是未定义的行为。我很惊讶我没有看到32位crc的这个警告。
如何更正此程序以正确计算ECMA-128 64位crc,而不会有未定义的行为?
// from https://www.boost.org/doc/libs/1_67_0/libs/crc/crc.html#usage
#include <boost/crc.hpp> // for boost::crc_basic, boost::crc_optimal
#include <boost/cstdint.hpp> // for boost::uint16_t
#include <algorithm> // for std::for_each
#include <cassert> // for assert
#include <cstddef> // for std::size_t
#include <iostream> // for std::cout
#include <ostream> // for std::endl
//#define SHOW_ERROR
#if defined( SHOW_ERROR )
#define CRC ecma_crc // expected
#else
#define CRC other_crc // actually received
#endif
int main()
{
// This is "123456789" in ASCII
unsigned char const data[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
std::size_t const data_len = sizeof(data) / sizeof(data[0]);
// The expected CRC for the given data
boost::uint16_t const expected = 0x29B1;
// Expected CRCs for "123456789" as per https://www.nitrxgen.net/hashgen/
long long const other_crc = 0x9D13A61C0E5B0FF5; // Wolfgang Ehrhardt http://www.wolfgang-ehrhardt.de/crchash_en.html
long long const ecma_crc = 0x62EC59E3F1A4F00A; // CRC-64-ECMA-128 https://en.wikipedia.org/wiki/Cyclic_redundancy_check
// Simulate CRC-CCITT
boost::crc_basic<16> crc_ccitt1(0x1021, 0xFFFF, 0, false, false);
crc_ccitt1.process_bytes(data, data_len);
assert(crc_ccitt1.checksum() == expected);
// Repeat with the optimal version (assuming a 16-bit type exists)
boost::crc_optimal<16, 0x1021, 0xFFFF, 0, false, false> crc_ccitt2;
crc_ccitt2 = std::for_each(data, data + data_len, crc_ccitt2);
assert(crc_ccitt2() == expected);
// Attempt 64 bit CRC
boost::crc_basic<64> crc_64_ecma1(0x42F0E1EBA9EA3693, 0xFFFFFFFFFFFFFFFF, 0, false, false);
crc_64_ecma1.process_bytes(data, data_len);
assert(crc_64_ecma1.checksum() == CRC);
boost::crc_optimal<64, 0x42F0E1EBA9EA3693, 0xFFFFFFFFFFFFFFFF, 0, false, false> crc_64_ecma2;
crc_64_ecma2 = std::for_each(data, data + data_len, crc_64_ecma2);
assert(crc_64_ecma2() == CRC);
std::cout << "All tests passed." << std::endl;
return 0;
}https://stackoverflow.com/questions/50765230
复制相似问题