├── .gitignore ├── CMakeLists.txt ├── Crc8.h ├── Crc16.h ├── Crc32.h ├── Crc64.h ├── CrcParameters.h ├── LICENSE ├── README.md ├── crc-table.sln ├── Crc8.cpp ├── Crc16.cpp ├── Crc32.cpp ├── crc-table.cbp ├── Crc64.cpp ├── crc-table.vcxproj.filters ├── crc-table.vcxproj └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/crc-table/FileContentIndex 2 | .vs/crc-table/v17 3 | crc-table/x64 4 | x64/Debug 5 | .vs/crc-table/copilot-chat 6 | .vs/crc-table/CopilotIndices 7 | crc-table.vcxproj.user 8 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.0) 2 | 3 | project(crc-table) 4 | 5 | set(CMAKE_CXX_STANDARD 17) 6 | include_directories(${PROJECT_SOURCE_DIR}) 7 | add_executable(crc-table main.cpp Crc8.cpp Crc16.cpp Crc32.cpp Crc64.cpp) 8 | -------------------------------------------------------------------------------- /Crc8.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC8_INCLUDED 3 | #define CRC8_INCLUDED 4 | 5 | #include 6 | #include 7 | 8 | #include "CrcParameters.h" 9 | 10 | class Crc8 11 | { 12 | public: 13 | 14 | void DisplayTable() const; 15 | void GenerateTable(uint8_t polynomial, bool reflectIn, bool reflectOut); 16 | 17 | private: 18 | 19 | uint8_t Reverse(uint8_t value) const; 20 | 21 | std::array crcTable; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Crc16.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC16_INCLUDED 3 | #define CRC16_INCLUDED 4 | 5 | #include 6 | #include 7 | 8 | #include "CrcParameters.h" 9 | 10 | class Crc16 11 | { 12 | public: 13 | 14 | void DisplayTable() const; 15 | void GenerateTable(uint16_t polynomial, bool reflectIn, bool reflectOut); 16 | 17 | private: 18 | 19 | uint16_t Reverse(uint16_t value) const; 20 | 21 | std::array crcTable; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Crc32.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC32_INCLUDED 3 | #define CRC32_INCLUDED 4 | 5 | #include 6 | #include 7 | 8 | #include "CrcParameters.h" 9 | 10 | class Crc32 11 | { 12 | public: 13 | 14 | void DisplayTable() const; 15 | void GenerateTable(uint32_t polynomial, bool reflectIn, bool reflectOut); 16 | 17 | private: 18 | 19 | uint32_t Reverse(uint32_t value) const; 20 | 21 | std::array crcTable; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Crc64.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC64_INCLUDED 3 | #define CRC64_INCLUDED 4 | 5 | #include 6 | #include 7 | 8 | #include "CrcParameters.h" 9 | 10 | class Crc64 11 | { 12 | public: 13 | 14 | void DisplayTable() const; 15 | void GenerateTable(uint64_t polynomial, bool reflectIn, bool reflectOut); 16 | 17 | private: 18 | 19 | uint64_t Reverse(uint64_t value) const; 20 | 21 | std::array crcTable; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /CrcParameters.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC_PARAMETERS_INCLUDED 3 | #define CRC_PARAMETERS_INCLUDED 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //***************************************************************************** 11 | struct CrcParameters 12 | { 13 | std::variant polynomial; 14 | bool reflectIn; 15 | bool reflectOut; 16 | 17 | enum class Type : int 18 | { 19 | CRC8, 20 | CRC16, 21 | CRC32, 22 | CRC64 23 | }; 24 | }; 25 | 26 | using CrcTypes = std::map ; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 jwellbelove, https://github.com/ETLCPP/crc-table-generator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CRC Table Generator 2 | ------------------------- 3 | 4 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) 5 | 6 | **About** 7 | 8 | Generates CRC lookup tables for common CRC algorithms for 8, 16, 32 and 64 bit CRCs. 9 | 10 | **Example** 11 | 12 | Generate the lookup table for CRC8 CCITT
13 | `crc-table crc8-ccitt` 14 | 15 | **Currently generates tables for:-** 16 | 17 | crc8-ccitt
18 | crc8-rohc
19 | crc8-cdma2000
20 | crc8-wcdma
21 | crc8-ebu
22 | crc8-i-code
23 | crc8-darc
24 | crc8-dvb-s2
25 | crc8-itu
26 | crc8-maxim
27 |
28 | crc16-ccitt
29 | crc16-aug-ccitt
30 | crc16-genibus
31 | crc16-xmodem
32 | crc16-mcrf4xx
33 | crc16-riello
34 | crc16-tms37157
35 | crc16-a
36 | crc16-kermit
37 | crc16-x25
38 | crc16-buypass
39 | crc16-dds-110
40 | crc16-arc
41 | crc16-maxim
42 | crc16-usb
43 | crc16-modbus
44 | crc16-dect-r
45 | crc16-dect-x
46 | crc16-en-13757
47 | crc16-dnp
48 | crc16-cdma2000
49 | crc16-teledisk
50 |
51 | crc32-bzip2
52 | crc32-mpeg2
53 | crc32-posix
54 | crc32
55 | crc32-jamcrc
56 | crc32-q
57 | crc32-xfr
58 | crc32-c
59 | crc32-d
60 |
61 | crc64-ecma
62 | -------------------------------------------------------------------------------- /crc-table.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30523.141 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crc-table", "crc-table.vcxproj", "{37DC6151-EE21-4FFB-B59A-47343133B218}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Debug|x64.ActiveCfg = Debug|x64 17 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Debug|x64.Build.0 = Debug|x64 18 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Debug|x86.ActiveCfg = Debug|Win32 19 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Debug|x86.Build.0 = Debug|Win32 20 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Release|x64.ActiveCfg = Release|x64 21 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Release|x64.Build.0 = Release|x64 22 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Release|x86.ActiveCfg = Release|Win32 23 | {37DC6151-EE21-4FFB-B59A-47343133B218}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {4E7F4EE7-1084-4D6D-925F-2E5FF5673F25} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Crc8.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Crc8.h" 3 | #include "CrcParameters.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //***************************************************************************** 11 | void Crc8::DisplayTable() const 12 | { 13 | int index = 0; 14 | 15 | for (int i = 0; i < 16; ++i) 16 | { 17 | for (int j = 0; j < 16; ++j) 18 | { 19 | std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << int(crcTable[index++]) << ", "; 20 | } 21 | 22 | std::cout << "\n"; 23 | } 24 | } 25 | 26 | //***************************************************************************** 27 | void Crc8::GenerateTable(uint8_t polynomial, bool reflectIn, bool reflectOut) 28 | { 29 | for (int byte = 0; byte < 256; ++byte) 30 | { 31 | uint8_t crc = (reflectIn ? Reverse(uint8_t(byte)) : byte); 32 | 33 | for (int bit = 8; bit > 0; --bit) 34 | { 35 | if (crc & 0x80) 36 | { 37 | crc = (crc << 1) ^ polynomial; 38 | } 39 | else 40 | { 41 | crc <<= 1; 42 | } 43 | } 44 | 45 | crcTable[byte] = (reflectOut ? Reverse(crc) : crc); 46 | } 47 | } 48 | 49 | //***************************************************************************** 50 | uint8_t Crc8::Reverse(uint8_t value) const 51 | { 52 | value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1); 53 | value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2); 54 | value = (value >> 4) | (value << 4); 55 | 56 | return value; 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /Crc16.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Crc16.h" 3 | #include "CrcParameters.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //***************************************************************************** 11 | void Crc16::DisplayTable() const 12 | { 13 | int index = 0; 14 | 15 | for (int i = 0; i < 16; ++i) 16 | { 17 | for (int j = 0; j < 16; ++j) 18 | { 19 | std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(4) << crcTable[index++] << ", "; 20 | } 21 | 22 | std::cout << "\n"; 23 | } 24 | } 25 | 26 | //***************************************************************************** 27 | void Crc16::GenerateTable(uint16_t polynomial, bool reflectIn, bool reflectOut) 28 | { 29 | for (int byte = 0; byte < 256; ++byte) 30 | { 31 | uint16_t crc = (reflectIn ? (Reverse(uint16_t(byte)) << 8) : byte); 32 | 33 | for (int bit = 16; bit > 0; --bit) 34 | { 35 | if (crc & 0x8000) 36 | { 37 | crc = (crc << 1) ^ polynomial; 38 | } 39 | else 40 | { 41 | crc <<= 1; 42 | } 43 | } 44 | 45 | crcTable[byte] = (reflectOut ? Reverse(crc) : crc); 46 | } 47 | } 48 | 49 | //***************************************************************************** 50 | uint16_t Crc16::Reverse(uint16_t value) const 51 | { 52 | value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1); 53 | value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2); 54 | value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4); 55 | value = (value >> 8) | (value << 8); 56 | 57 | return value; 58 | } 59 | 60 | 61 | -------------------------------------------------------------------------------- /Crc32.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Crc32.h" 3 | #include "CrcParameters.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //***************************************************************************** 11 | void Crc32::DisplayTable() const 12 | { 13 | int index = 0; 14 | 15 | for (int i = 0; i < 16; ++i) 16 | { 17 | for (int j = 0; j < 16; ++j) 18 | { 19 | std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) << crcTable[index++] << ", "; 20 | } 21 | 22 | std::cout << "\n"; 23 | } 24 | } 25 | 26 | //***************************************************************************** 27 | void Crc32::GenerateTable(uint32_t polynomial, bool reflectIn, bool reflectOut) 28 | { 29 | for (int byte = 0; byte < 256; ++byte) 30 | { 31 | uint32_t crc = (reflectIn ? (Reverse(uint32_t(byte)) << 24) : byte); 32 | 33 | for (int bit = 32; bit > 0; --bit) 34 | { 35 | if (crc & 0x80000000) 36 | { 37 | crc = (crc << 1) ^ polynomial; 38 | } 39 | else 40 | { 41 | crc <<= 1; 42 | } 43 | } 44 | 45 | crcTable[byte] = (reflectOut ? Reverse(crc) : crc); 46 | } 47 | } 48 | 49 | //***************************************************************************** 50 | uint32_t Crc32::Reverse(uint32_t value) const 51 | { 52 | value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); 53 | value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); 54 | value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); 55 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); 56 | value = (value >> 16) | (value << 16); 57 | 58 | return value; 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /crc-table.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 57 | 58 | -------------------------------------------------------------------------------- /Crc64.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Crc64.h" 3 | #include "CrcParameters.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //***************************************************************************** 11 | void Crc64::DisplayTable() const 12 | { 13 | int index = 0; 14 | 15 | for (int i = 0; i < 16; ++i) 16 | { 17 | for (int j = 0; j < 16; ++j) 18 | { 19 | std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) << crcTable[index++] << ", "; 20 | } 21 | 22 | std::cout << "\n"; 23 | } 24 | } 25 | 26 | //***************************************************************************** 27 | void Crc64::GenerateTable(uint64_t polynomial, bool reflectIn, bool reflectOut) 28 | { 29 | for (int byte = 0; byte < 256; ++byte) 30 | { 31 | uint64_t crc = (reflectIn ? (Reverse(uint64_t(byte)) << 56) : byte); 32 | 33 | for (int bit = 0; bit < 64; ++bit) 34 | { 35 | if (crc & 0x8000000000000000) 36 | { 37 | crc = (crc << 1) ^ polynomial; 38 | } 39 | else 40 | { 41 | crc <<= 1; 42 | } 43 | } 44 | 45 | crcTable[byte] = (reflectOut ? Reverse(crc) : crc); 46 | } 47 | } 48 | 49 | //***************************************************************************** 50 | uint64_t Crc64::Reverse(uint64_t value) const 51 | { 52 | value = ((value & 0xAAAAAAAAAAAAAAAA) >> 1) | ((value & 0x5555555555555555) << 1); 53 | value = ((value & 0xCCCCCCCCCCCCCCCC) >> 2) | ((value & 0x3333333333333333) << 2); 54 | value = ((value & 0xF0F0F0F0F0F0F0F0) >> 4) | ((value & 0x0F0F0F0F0F0F0F0F) << 4); 55 | value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); 56 | value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); 57 | value = (value >> 32) | (value << 32); 58 | 59 | return value; 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /crc-table.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | 52 | 53 | Resource Files 54 | 55 | 56 | 57 | 58 | Resource Files 59 | 60 | 61 | -------------------------------------------------------------------------------- /crc-table.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {37dc6151-ee21-4ffb-b59a-47343133b218} 25 | generatecrc8table 26 | 10.0 27 | crc-table 28 | 29 | 30 | 31 | Application 32 | true 33 | v143 34 | Unicode 35 | 36 | 37 | Application 38 | false 39 | v143 40 | true 41 | Unicode 42 | 43 | 44 | Application 45 | true 46 | v143 47 | Unicode 48 | 49 | 50 | Application 51 | false 52 | v143 53 | true 54 | Unicode 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | true 76 | 77 | 78 | false 79 | 80 | 81 | true 82 | 83 | 84 | false 85 | 86 | 87 | 88 | Level3 89 | true 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | true 92 | stdcpp20 93 | 94 | 95 | Console 96 | true 97 | 98 | 99 | 100 | 101 | Level3 102 | true 103 | true 104 | true 105 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | true 112 | true 113 | 114 | 115 | 116 | 117 | Level3 118 | true 119 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 120 | true 121 | stdcpp20 122 | 123 | 124 | Console 125 | true 126 | 127 | 128 | 129 | 130 | Level3 131 | true 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "CrcParameters.h" 7 | #include "Crc8.h" 8 | #include "Crc16.h" 9 | #include "Crc32.h" 10 | #include "Crc64.h" 11 | 12 | CrcTypes crcTypes; 13 | Crc8 crc8; 14 | Crc16 crc16; 15 | Crc32 crc32; 16 | Crc64 crc64; 17 | 18 | //***************************************************************************** 19 | void ListTypes() 20 | { 21 | std::cout << "Valid CRC types:\n"; 22 | 23 | // List CRC-8 types 24 | for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr) 25 | { 26 | if (itr->first.substr(0, 4) == "crc8") 27 | { 28 | const CrcParameters& parameters = itr->second; 29 | std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first 30 | << " : " 31 | << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << int(std::get(parameters.polynomial)) << " : " 32 | << std::boolalpha << std::left << std::setw(5) << std::setfill(' ') 33 | << parameters.reflectIn << " : " 34 | << parameters.reflectOut 35 | << "\n"; 36 | } 37 | } 38 | 39 | std::cout << "\n"; 40 | 41 | // List CRC-16 types 42 | for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr) 43 | { 44 | 45 | if (itr->first.substr(0, 5) == "crc16") 46 | { 47 | const CrcParameters& parameters = itr->second; 48 | std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first 49 | << " : " 50 | << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(4) << std::get(parameters.polynomial) << " : " 51 | << std::boolalpha << std::left << std::setw(5) << std::setfill(' ') 52 | << parameters.reflectIn << " : " 53 | << parameters.reflectOut 54 | << "\n"; 55 | } 56 | } 57 | 58 | std::cout << "\n"; 59 | 60 | // List CRC-32 types 61 | for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr) 62 | { 63 | if (itr->first.substr(0, 5) == "crc32") 64 | { 65 | const CrcParameters& parameters = itr->second; 66 | std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first 67 | << " : " 68 | << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) << std::get(parameters.polynomial) << " : " 69 | << std::boolalpha << std::left << std::setw(5) << std::setfill(' ') 70 | << parameters.reflectIn << " : " 71 | << parameters.reflectOut 72 | << "\n"; 73 | } 74 | } 75 | 76 | std::cout << "\n"; 77 | 78 | // List CRC-64 types 79 | for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr) 80 | { 81 | if (itr->first.substr(0, 5) == "crc64") 82 | { 83 | const CrcParameters& parameters = itr->second; 84 | std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first 85 | << " : " 86 | << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) << std::get(parameters.polynomial) << " : " 87 | << std::boolalpha << std::left << std::setw(5) << std::setfill(' ') 88 | << parameters.reflectIn << " : " 89 | << parameters.reflectOut 90 | << "\n"; 91 | } 92 | } 93 | 94 | std::cout << "\n"; 95 | } 96 | 97 | //***************************************************************************** 98 | void Initialise() 99 | { 100 | crcTypes["crc8-ccitt"] = { uint8_t(0x07), false, false }; 101 | crcTypes["crc8-rohc"] = { uint8_t(0x07), true, true }; 102 | crcTypes["crc8-cdma2000"] = { uint8_t(0x9B), false, false }; 103 | crcTypes["crc8-wcdma"] = { uint8_t(0x9B), true, true }; 104 | crcTypes["crc8-ebu"] = { uint8_t(0x1D), true, true }; 105 | crcTypes["crc8-i-code"] = { uint8_t(0x1D), false, false }; 106 | crcTypes["crc8-darc"] = { uint8_t(0x39), true, true }; 107 | crcTypes["crc8-dvb-s2"] = { uint8_t(0xD5), false, false }; 108 | crcTypes["crc8-itu"] = { uint8_t(0x07), false, false }; 109 | crcTypes["crc8-maxim"] = { uint8_t(0x31), true, true }; 110 | 111 | crcTypes["crc16-ccitt"] = { uint16_t(0x1021), false, false }; 112 | crcTypes["crc16-aug-ccitt"] = { uint16_t(0x1021), false, false }; 113 | crcTypes["crc16-genibus"] = { uint16_t(0x1021), false, false }; 114 | crcTypes["crc16-xmodem"] = { uint16_t(0x1021), false, false }; 115 | crcTypes["crc16-mcrf4xx"] = { uint16_t(0x1021), true, true }; 116 | crcTypes["crc16-riello"] = { uint16_t(0x1021), true, true }; 117 | crcTypes["crc16-tms37157"] = { uint16_t(0x1021), true, true }; 118 | crcTypes["crc16-a"] = { uint16_t(0x1021), true, true }; 119 | crcTypes["crc16-kermit"] = { uint16_t(0x1021), true, true }; 120 | crcTypes["crc16-x25"] = { uint16_t(0x1021), true, true }; 121 | crcTypes["crc16-buypass"] = { uint16_t(0x8005), false, false }; 122 | crcTypes["crc16-dds-110"] = { uint16_t(0x8005), false, false }; 123 | crcTypes["crc16-arc"] = { uint16_t(0x8005), true, true }; 124 | crcTypes["crc16-maxim"] = { uint16_t(0x8005), true, true }; 125 | crcTypes["crc16-usb"] = { uint16_t(0x8005), true, true }; 126 | crcTypes["crc16-modbus"] = { uint16_t(0x8005), true, true }; 127 | crcTypes["crc16-dect-r"] = { uint16_t(0x0589), false, false }; 128 | crcTypes["crc16-dect-x"] = { uint16_t(0x0589), false, false }; 129 | crcTypes["crc16-en-13757"] = { uint16_t(0x3D65), false, false }; 130 | crcTypes["crc16-dnp"] = { uint16_t(0x3D65), true, true }; 131 | crcTypes["crc16-cdma2000"] = { uint16_t(0xC867), false, false }; 132 | crcTypes["crc16-teledisk"] = { uint16_t(0xA097), false, false }; 133 | 134 | crcTypes["crc32-bzip2"] = { uint32_t(0x04C11DB7), false, false }; 135 | crcTypes["crc32-mpeg2"] = { uint32_t(0x04C11DB7), false, false }; 136 | crcTypes["crc32-posix"] = { uint32_t(0x04C11DB7), false, false }; 137 | crcTypes["crc32"] = { uint32_t(0x04C11DB7), true, true }; 138 | crcTypes["crc32-jamcrc"] = { uint32_t(0x04C11DB7), true, true }; 139 | crcTypes["crc32-q"] = { uint32_t(0x814141AB), false, false }; 140 | crcTypes["crc32-xfr"] = { uint32_t(0x000000AF), false, false }; 141 | crcTypes["crc32-c"] = { uint32_t(0x1EDC6F41), false, false }; 142 | crcTypes["crc32-d"] = { uint32_t(0xA833982B), false, false }; 143 | 144 | crcTypes["crc64-ecma"] = { uint64_t(0x42F0E1EBA9EA3693), false, false }; 145 | } 146 | 147 | //***************************************************************************** 148 | int main(int argc, char* argv[]) 149 | { 150 | Initialise(); 151 | 152 | if (argc > 1) 153 | { 154 | std::string crcTypeName(argv[1]); 155 | 156 | CrcTypes::const_iterator itr = crcTypes.find(crcTypeName); 157 | 158 | if (itr != crcTypes.end()) 159 | { 160 | std::cout << "Generating CRC table for " << crcTypeName << "\n\n"; 161 | 162 | const CrcParameters& parameters = itr->second; 163 | 164 | // What size of CRC is it? 165 | CrcParameters::Type crcType = CrcParameters::Type(parameters.polynomial.index()); 166 | 167 | switch (crcType) 168 | { 169 | case CrcParameters::Type::CRC8: 170 | { 171 | crc8.GenerateTable(std::get(parameters.polynomial), parameters.reflectIn, parameters.reflectOut); 172 | crc8.DisplayTable(); 173 | break; 174 | } 175 | 176 | case CrcParameters::Type::CRC16: 177 | { 178 | crc16.GenerateTable(std::get(parameters.polynomial), parameters.reflectIn, parameters.reflectOut); 179 | crc16.DisplayTable(); 180 | break; 181 | } 182 | 183 | case CrcParameters::Type::CRC32: 184 | { 185 | crc32.GenerateTable(std::get(parameters.polynomial), parameters.reflectIn, parameters.reflectOut); 186 | crc32.DisplayTable(); 187 | break; 188 | } 189 | 190 | case CrcParameters::Type::CRC64: 191 | { 192 | crc64.GenerateTable(std::get(parameters.polynomial), parameters.reflectIn, parameters.reflectOut); 193 | crc64.DisplayTable(); 194 | break; 195 | } 196 | 197 | default: 198 | { 199 | std::cout << "Unsupported CRC size\n\n"; 200 | break; 201 | } 202 | } 203 | } 204 | else 205 | { 206 | std::cout << "Unknown CRC type : " << crcTypeName << "\n"; 207 | ListTypes(); 208 | } 209 | } 210 | else 211 | { 212 | std::cout << "\nSyntax: crc-table \n"; 213 | std::cout << "Example: crc-table crc8-ccitt\n\n"; 214 | ListTypes(); 215 | } 216 | } --------------------------------------------------------------------------------