{"id":16991641,"url":"https://github.com/pstolarz/crac","last_synced_at":"2025-06-11T21:33:53.214Z","repository":{"id":62850376,"uuid":"551681389","full_name":"pstolarz/CRaC","owner":"pstolarz","description":"C++17 CRC template library","archived":false,"fork":false,"pushed_at":"2024-11-07T19:43:41.000Z","size":137,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T04:13:54.493Z","etag":null,"topics":["crc","crc16","crc32","crc8","template"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pstolarz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-14T21:58:45.000Z","updated_at":"2024-11-07T19:43:45.000Z","dependencies_parsed_at":"2024-01-05T20:40:21.779Z","dependency_job_id":"619dc424-cdfc-4f1c-aeef-2eb6bf79893a","html_url":"https://github.com/pstolarz/CRaC","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pstolarz%2FCRaC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pstolarz%2FCRaC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pstolarz%2FCRaC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pstolarz%2FCRaC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pstolarz","download_url":"https://codeload.github.com/pstolarz/CRaC/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514200,"owners_count":21116903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crc","crc16","crc32","crc8","template"],"created_at":"2024-10-14T03:26:44.989Z","updated_at":"2025-04-12T04:13:59.573Z","avatar_url":"https://github.com/pstolarz.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CRaC: C++17 CRC template library\n\nCRaC is C++ single-header template library aiming for fast CRC calculation with\nlimited runtime footprint size (especially embedded systems case). The library\nextensively exploits compile-time oriented features of the C++ language\nrequiring at least C++17 compliant compiler.\n\nFor C-language binding see the [`c_bind`](c_bind) directory content.\n\n## Features\n\nThe following features distinguish the library among dozens of other CRC libraries.\n\n**Runtime fast, compile-time optimized**\n\nLookup tables (LUT) generation and (almost) all conditional branches are defined\nand checked at the compile time by a compiler, emitting the runtime output in\nreduced form, optimized for a particular CRC algorithm. Moreover, if an input\nfor a calculated CRC checksum is known at the compile time, the library may\ncalculate it purely at the compilation stage with no code emitted to the runtime.\nAs an example - the library unit tests are performed entirely at the compile-time\nlevel by [`crac_test.h`](include/crac_test.h) header, and may be activated by\ndefining `CRAC_TEST`.\n\n**Limited runtime footprint**\n\nThe library exploits special type of reduced size lookup table to decrease\nruntime footprint size. Most CRC libraries out there, which use lookup tables to\nincrease CRC computation speed, base on a single 256-elements table to calculate\nsingle-byte CRC checksum. Such approach may by a blocker for tiny embedded\nplatforms, where 1kB CRC-32 lookup table is too large. CRaC incorporates\n16- or 32-elements lookup tables, drastically reducing the footprint, without\nsignificant performance penalty (see below for details).\n\nNOTE: Default type of LUT is chosen by defining appropriates `CRAC_LUT`\nmacro-define or passing appropriate argument to `crc_algo` template.\n\n**100+ predefined CRCs**\n\nBeside allowing to define arbitrary CRCs, the library provides large set of\npredefined CRCs ready to use out of the box. Note, the definitions are provided\ndirectly at the compile-time level, therefore don't occupy runtime outcome\nunless reference exists in the source code - if you don't use it, you don't pay\nfor it.\n\n## Performance\n\nCRaC, as most modern C++ libraries, heavily depends on compiler optimization\ntechniques to produce fast and efficient code. The following table presents\nCRC-32 computation timings for 1 million iterations over 256-bytes table with\n256-elements lookup table used (values in ms):\n\n|            | gcc -O   | gcc      |\n|------------|----------|----------|\n| CRaC       |**0.440** | 1.350    |\n| CRC++      | 0.522    | 1.08     |\n| libcrc     | 0.516    | 0.518    |\n\nAs may be seen, even basic optimization turned on (`-O`) produces code even\nmore efficient than pure C library (`libcrc`).\n\nCompiler version used for tests: `gcc (Debian 13.3.0-3) 13.3.0`.\n\n**Lookup table type vs performance**\n\nThe following table presents CRC-32 compilation timings for different lookup\ntables. The same test conditions as previously (values in ms):\n\n|         | gcc -O   | gcc      |\n|---------|----------|----------|\n| LUT-256 | 0.440    | 1.350    |\n| LUT-32  | 0.680    | 1.608    |\n| LUT-16  | 0.922    | 2.592    |\n\nAs a conclusion, unoptimized compilation introduces performance loss much more\nseverely than lookup table size reduction.\n\n## Usage\n\nThe library can calculate CRC checksum purely at the compile time (if an input\ndata is known at the source code level, aka `constexpr`) or classically during\nthe runtime phase. In the latter case the computation may be performed in two\ntypes of modes - single step mode and the block mode.\n\n```c++\n#include \"crac.h\"\n\n// ...\n\nusing namespace crac;\n\n// crc_check_str is defined by CRaC library as compile-time\n// constant \"123456789\" string w/o trailing null-terminator\n\nconstexpr size_t len = sizeof(crc_check_str);\n\n// compile-time CRC calculation\nstatic_assert(CRC32::calc(crc_check_str, len) == CRC32::check_val);\n\n// runtime calculation: single step mode\nauto crc = CRC32::calc(crc_check_str, len);\n\n// runtime calculation: block mode\nauto block_eng = CRC32::get_block_eng();\nblock_eng.update(crc_check_str, 3);\nblock_eng.update(crc_check_str + 3, 3);\nblock_eng.update(crc_check_str + 6, len - 6);\nassert(crc == block_eng.final());\n\n// Custom CRC-5 definition:\n//   polynomial: 0x15,\n//   direct-input,\n//   direct-output,\n//   initial value: 0,\n//   output XOR value: 0.\nusing CRC5_CUSTOM = crc_algo\u003c5, 0x15, false, false, 0, 0\u003e;\nstatic_assert(CRC5_CUSTOM::calc(crc_check_str, len) == CRC5_CUSTOM::check_val);\n```\n\n## Predefined CRCs\n\nThe library provides the following set of predefined CRCs:\n\n| CRC                    | Poly                    | ReflIn | ReflOut | InitVal  | XorOut   | CheckVal                |\n|------------------------|-------------------------|--------|---------|----------|----------|-------------------------|\n| CRC1                   | 0x1                     | Y      | Y       | 0        | 0        | 0x1                     |\n| CRC3_GSM               | 0x3                     | N      | N       | 0        | ~0       | 0x4                     |\n| CRC3_ROHC              | 0x3                     | Y      | Y       | ~0       | 0        | 0x6                     |\n| CRC4_ITU               | 0x3                     | Y      | Y       | 0        | 0        | 0x7                     |\n| CRC4_INTERLAKEN        | 0x3                     | N      | N       | ~0       | ~0       | 0xb                     |\n| CRC5_USB               | 0x05                    | Y      | Y       | ~0       | ~0       | 0x19                    |\n| CRC5_EPC               | 0x09                    | N      | N       | 0x09     | 0        | 0                       |\n| CRC5_ITU               | 0x15                    | Y      | Y       | 0        | 0        | 0x07                    |\n| CRC6_ITU               | 0x03                    | Y      | Y       | 0        | 0        | 0x06                    |\n| CRC6_CDMA2000_B        | 0x07                    | N      | N       | ~0       | 0        | 0x3b                    |\n| CRC6_DARC              | 0x19                    | Y      | Y       | 0        | 0        | 0x26                    |\n| CRC6_NR                | 0x21                    | N      | N       | 0        | 0        | 0x15                    |\n| CRC6_CDMA2000_A        | 0x27                    | N      | N       | ~0       | 0        | 0x0d                    |\n| CRC6_GSM               | 0x2f                    | N      | N       | 0        | ~0       | 0x13                    |\n| CRC7                   | 0x09                    | N      | N       | 0        | 0        | 0x75                    |\n| CRC7_UMTS              | 0x45                    | N      | N       | 0        | 0        | 0x61                    |\n| CRC7_ROHC              | 0x4f                    | Y      | Y       | ~0       | 0        | 0x53                    |\n| CRC7_MVB               | 0x65                    | N      | N       | 0        | 0        | 0x1f                    |\n| CRC8                   | 0x07                    | N      | N       | 0        | 0        | 0xf4                    |\n| CRC8_HDLC              | 0x07                    | Y      | Y       | ~0       | ~0       | 0x2f                    |\n| CRC8_ITU               | 0x07                    | N      | N       | 0        | 0x55     | 0xa1                    |\n| CRC8_ROHC              | 0x07                    | Y      | Y       | ~0       | 0        | 0xd0                    |\n| CRC8_EBU               | 0x1d                    | Y      | Y       | ~0       | 0        | 0x97                    |\n| CRC8_GSM_A             | 0x1d                    | N      | N       | 0        | 0        | 0x37                    |\n| CRC8_HITAG             | 0x1d                    | N      | N       | ~0       | 0        | 0xb4                    |\n| CRC8_ICODE             | 0x1d                    | N      | N       | 0xfd     | 0        | 0x7e                    |\n| CRC8_MIFRAME_MAD       | 0x1d                    | N      | N       | 0xc7     | 0        | 0x99                    |\n| CRC8_SAE_J1850         | 0x1d                    | N      | N       | ~0       | ~0       | 0x4b                    |\n| CRC8_AUTOSAR           | 0x2f                    | N      | N       | ~0       | ~0       | 0xdf                    |\n| CRC8_OPENSAFETY        | 0x2f                    | N      | N       | 0        | 0        | 0x3e                    |\n| CRC8_MAXIM             | 0x31                    | Y      | Y       | 0        | 0        | 0xa1                    |\n| CRC8_NRSC5             | 0x31                    | N      | N       | ~0       | 0        | 0xf7                    |\n| CRC8_DARC              | 0x39                    | Y      | Y       | 0        | 0        | 0x15                    |\n| CRC8_GSM_B             | 0x49                    | N      | N       | 0        | ~0       | 0x94                    |\n| CRC8_CDMA2000          | 0x9b                    | N      | N       | ~0       | 0        | 0xda                    |\n| CRC8_LTE               | 0x9b                    | N      | N       | 0        | 0        | 0xea                    |\n| CRC8_WCDMA             | 0x9b                    | Y      | Y       | 0        | 0        | 0x25                    |\n| CRC8_DVB_S2            | 0xd5                    | N      | N       | 0        | 0        | 0xbc                    |\n| CRC8_BLUETOOTH         | 0xa7                    | Y      | Y       | 0        | 0        | 0x26                    |\n| CRC10_GSM              | 0x175                   | N      | N       | 0        | ~0       | 0x12a                   |\n| CRC10                  | 0x233                   | N      | N       | 0        | 0        | 0x199                   |\n| CRC10_CDMA2000         | 0x3d9                   | N      | N       | ~0       | 0        | 0x233                   |\n| CRC11_UMTS             | 0x307                   | N      | N       | 0        | 0        | 0x061                   |\n| CRC11                  | 0x385                   | N      | N       | 0x01a    | 0        | 0x5a3                   |\n| CRC11_NR               | 0x621                   | N      | N       | 0        | 0        | 0x5ca                   |\n| CRC12_DECT             | 0x80f                   | N      | N       | 0        | 0        | 0xf5b                   |\n| CRC12_UMTS             | 0x80f                   | N      | Y       | 0        | 0        | 0xdaf                   |\n| CRC12_GSM              | 0xd31                   | N      | N       | 0        | ~0       | 0xb34                   |\n| CRC12_CDMA2000         | 0xf13                   | N      | N       | ~0       | 0        | 0xd4d                   |\n| CRC13_BBC              | 0x1cf5                  | N      | N       | 0        | 0        | 0x04fa                  |\n| CRC14_GSM              | 0x202d                  | N      | N       | 0        | ~0       | 0x30ae                  |\n| CRC14_DARC             | 0x0805                  | Y      | Y       | 0        | 0        | 0x082d                  |\n| CRC15                  | 0x4599                  | N      | N       | 0        | 0        | 0x059e                  |\n| CRC15_MPT1327          | 0x6815                  | N      | N       | 0        | 0x0001   | 0x2566                  |\n| CRC16_DECT_R           | 0x0589                  | N      | N       | 0        | 0x0001   | 0x007e                  |\n| CRC16_DECT_X           | 0x0589                  | N      | N       | 0        | 0        | 0x007f                  |\n| CRC16_NRSC5            | 0x080b                  | Y      | Y       | ~0       | 0        | 0xa066                  |\n| CRC16_AUG_CCITT        | 0x1021                  | N      | N       | 0x1d0f   | 0        | 0xe5cc                  |\n| CRC16_CCITT_FALSE      | 0x1021                  | N      | N       | ~0       | 0        | 0x29b1                  |\n| CRC16_GENIBUS          | 0x1021                  | N      | N       | ~0       | ~0       | 0xd64e                  |\n| CRC16_GSM              | 0x1021                  | N      | N       | 0        | ~0       | 0xce3c                  |\n| CRC16_ISO_IEC14443_3_A | 0x1021                  | Y      | Y       | 0xc6c6   | 0        | 0xbf05                  |\n| CRC16_KERMIT           | 0x1021                  | Y      | Y       | 0        | 0        | 0x2189                  |\n| CRC16_MCRF4XX          | 0x1021                  | Y      | Y       | ~0       | 0        | 0x6f91                  |\n| CRC16_RIELLO           | 0x1021                  | Y      | Y       | 0xb2aa   | 0        | 0x63d0                  |\n| CRC16_TMS37157         | 0x1021                  | Y      | Y       | 0x89ec   | 0        | 0x26b1                  |\n| CRC16_X25              | 0x1021                  | Y      | Y       | ~0       | ~0       | 0x906e                  |\n| CRC16_XMODEM           | 0x1021                  | N      | N       | 0        | 0        | 0x31c3                  |\n| CRC16_PROFIBUS         | 0x1dcf                  | N      | N       | ~0       | ~0       | 0xa819                  |\n| CRC16_CHAKRAVARTY      | 0x2f15                  | N      | N       | 0        | 0        | 0xa2d1                  |\n| CRC16_DNP              | 0x3d65                  | Y      | Y       | 0        | ~0       | 0xea82                  |\n| CRC16_EN13757          | 0x3d65                  | N      | N       | 0        | ~0       | 0xc2b7                  |\n| CRC16_M17              | 0x5935                  | N      | N       | ~0       | 0        | 0x772b                  |\n| CRC16_OPENSAFETY_A     | 0x5935                  | N      | N       | 0        | 0        | 0x5d38                  |\n| CRC16_LJ1200           | 0x6f63                  | N      | N       | 0        | 0        | 0xbdf4                  |\n| CRC16_OPENSAFETY_B     | 0x755b                  | N      | N       | 0        | 0        | 0x20fe                  |\n| CRC16_ARC              | 0x8005                  | Y      | Y       | 0        | 0        | 0xbb3d                  |\n| CRC16_BUYPASS          | 0x8005                  | N      | N       | 0        | 0        | 0xfee8                  |\n| CRC16_CMS              | 0x8005                  | N      | N       | ~0       | 0        | 0xaee7                  |\n| CRC16_DDS110           | 0x8005                  | N      | N       | 0x800d   | 0        | 0x9ecf                  |\n| CRC16_MAXIM            | 0x8005                  | Y      | Y       | 0        | ~0       | 0x44c2                  |\n| CRC16_MODBUS           | 0x8005                  | Y      | Y       | ~0       | 0        | 0x4b37                  |\n| CRC16_USB              | 0x8005                  | Y      | Y       | ~0       | ~0       | 0xb4c8                  |\n| CRC16_T10_DIF          | 0x8bb7                  | N      | N       | 0        | 0        | 0xd0db                  |\n| CRC16_CDMA2000         | 0xc867                  | N      | N       | ~0       | 0        | 0x4c06                  |\n| CRC16_ARINC            | 0xa02b                  | N      | N       | 0        | 0        | 0xeba4                  |\n| CRC16_TELEDISK         | 0xa097                  | N      | N       | 0        | 0        | 0x0fb3                  |\n| CRC17_CAN_FD           | 0x1685b                 | N      | N       | 0        | 0        | 0x04f03                 |\n| CRC21_CAN_FD           | 0x102899                | N      | N       | 0        | 0        | 0x0ed841                |\n| CRC24_BLE              | 0x00065b                | Y      | Y       | 0x555555 | 0        | 0xc25a56                |\n| CRC24_INTERLAKEN       | 0x328b63                | N      | N       | ~0       | ~0       | 0xb4f3e6                |\n| CRC24_FLEXRAY_A        | 0x5d6dcb                | N      | N       | 0xfedcba | 0        | 0x7979bd                |\n| CRC24_FLEXRAY_B        | 0x5d6dcb                | N      | N       | 0xabcdef | 0        | 0x1f23b8                |\n| CRC24_LTE_B            | 0x800063                | N      | N       | 0        | 0        | 0x23ef52                |\n| CRC24_OS9              | 0x800063                | N      | N       | ~0       | ~0       | 0x200fa5                |\n| CRC24                  | 0x864cfb                | N      | N       | 0xb704ce | 0        | 0x21cf02                |\n| CRC24_LTE_A            | 0x864cfb                | N      | N       | 0        | 0        | 0xcde703                |\n| CRC24_NR_C             | 0xb2b117                | N      | N       | 0        | 0        | 0xf48279                |\n| CRC30_CDMA             | 0x2030b9c7              | N      | N       | ~0       | ~0       | 0x04c34abf              |\n| CRC31_PHILIPS          | 0x04c11db7              | N      | N       | ~0       | ~0       | 0x0ce9e46c              |\n| CRC32_XFER             | 0x000000af              | N      | N       | 0        | 0        | 0xbd0be338              |\n| CRC32                  | 0x04c11db7              | Y      | Y       | ~0       | ~0       | 0xcbf43926              |\n| CRC32_BZIP2            | 0x04c11db7              | N      | N       | ~0       | ~0       | 0xfc891918              |\n| CRC32_EDC              | 0x04c11db7              | N      | N       | 0        | 0        | 0x89a1897f              |\n| CRC32_JAMCRC           | 0x04c11db7              | Y      | Y       | ~0       | 0        | 0x340bc6d9              |\n| CRC32_MPEG2            | 0x04c11db7              | N      | N       | ~0       | 0        | 0x0376e6e7              |\n| CRC32_POSIX            | 0x04c11db7              | N      | N       | 0        | ~0       | 0x765e7680              |\n| CRC32_C                | 0x1edc6f41              | Y      | Y       | ~0       | ~0       | 0xe3069283              |\n| CRC32_K2               | 0x32583499              | Y      | Y       | ~0       | 0        | 0x1148ab33              |\n| CRC32_MEF              | 0x741b8cd7              | Y      | Y       | ~0       | 0        | 0xd2c22f51              |\n| CRC32_CDROM_EDC        | 0x8001801b              | Y      | Y       | 0        | 0        | 0x6ec2edc4              |\n| CRC32_Q                | 0x814141ab              | N      | N       | 0        | 0        | 0x3010bf7f              |\n| CRC32_D                | 0xa833982b              | Y      | Y       | ~0       | ~0       | 0x87315576              |\n| CRC32_AUTOSAR          | 0xf4acfb13              | Y      | Y       | ~0       | ~0       | 0x1697d06a              |\n| CRC40_GSM              | 0x0004820009            | N      | N       | 0        | ~0       | 0xd4164fc646            |\n| CRC64_GO_ISO           | 0x000000000000001b      | Y      | Y       | ~0       | ~0       | 0xb90956c775a41001      |\n| CRC64_MS               | 0x259c84cba6426349      | Y      | Y       | ~0       | 0        | 0x75d4b74f024eceea      |\n| CRC64                  | 0x42f0e1eba9ea3693      | N      | N       | 0        | 0        | 0x6c40df5f0b497347      |\n| CRC64_WE               | 0x42f0e1eba9ea3693      | N      | N       | ~0       | ~0       | 0x62ec59e3f1a4f00a      |\n| CRC64_XZ               | 0x42f0e1eba9ea3693      | Y      | Y       | ~0       | ~0       | 0x995dc9bbdf1939fa      |\n| CRC64_REDIS            | 0xad93d23594c935a9      | Y      | Y       | 0        | 0        | 0xe9c6d914c4b8d9ca      |\n| CRC82_DARC(*)          | 0x0308c0111011401440411 | Y      | Y       | 0        | 0        | 0x09ea83f625023801fd612 |\n\nSome of the above algorithms have one or more aliases (e.g. `CRC16_KERMIT`,\n`CRC16_BLUETOOTH`, `CRC16_CCITT`, `CRC16_CCITT_TRUE`, `CRC16_V41_LSB` refer\nto the same CRC). See [`crac.h`](include/crac.h) for more details.\n\n(*) Only for `gcc`/`clang` compilers. Enabled by defining `CRAC_EXTINT`.\n\n### References\n\n1. [Catalogue of parametrised CRC algorithms](https://reveng.sourceforge.io/crc-catalogue/all.htm); CRC RevEng\n2. [Cyclic redundancy code (CRC)](https://infineon.github.io/mtb-pdl-cat2/pdl_api_reference_manual/html/group__group__crypto__lld__crc__functions.html); CAT2 Peripheral Driver Library\n3. [Cyclic redundancy code (CRC)](https://en.wikipedia.org/wiki/Cyclic_redundancy_check); Wikipedia\n## License\n\n2 clause BSD license. See [`LICENSE`](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpstolarz%2Fcrac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpstolarz%2Fcrac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpstolarz%2Fcrac/lists"}