{"id":28033031,"url":"https://github.com/localspook/crc","last_synced_at":"2025-05-11T09:10:27.421Z","repository":{"id":288997638,"uuid":"969404324","full_name":"LocalSpook/crc","owner":"LocalSpook","description":"A high performance C++ CRC library with all the bells and whistles.","archived":false,"fork":false,"pushed_at":"2025-05-07T05:18:53.000Z","size":26,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-07T06:20:52.216Z","etag":null,"topics":["cpp","cpp20","crc","header-only","no-dependencies","single-file"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LocalSpook.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2025-04-20T04:17:45.000Z","updated_at":"2025-05-07T05:18:56.000Z","dependencies_parsed_at":"2025-04-21T00:37:10.994Z","dependency_job_id":null,"html_url":"https://github.com/LocalSpook/crc","commit_stats":null,"previous_names":["localspook/crc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LocalSpook%2Fcrc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LocalSpook%2Fcrc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LocalSpook%2Fcrc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LocalSpook%2Fcrc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LocalSpook","download_url":"https://codeload.github.com/LocalSpook/crc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253540834,"owners_count":21924537,"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":["cpp","cpp20","crc","header-only","no-dependencies","single-file"],"created_at":"2025-05-11T09:10:26.746Z","updated_at":"2025-05-11T09:10:27.410Z","avatar_url":"https://github.com/LocalSpook.png","language":"C++","readme":"# CRC\n\n**Warning: this library is under construction.\nIt's functional, but its main selling point—SIMD acceleration—is not yet implemented.**\n\n[![CodeQL](https://github.com/LocalSpook/crc/actions/workflows/codeql.yml/badge.svg)](https://github.com/LocalSpook/crc/actions/workflows/codeql.yml)\n\nA C++20 SIMD-accelerated high-performance constexpr-capable single-header-only CRC library with over 100 predefined CRCs and the ability to create custom ones.\n\n## API\n\nMost likely, all you're looking for is a simple function that calculates a common CRC variant.\nThere are two fundamental CRC operations: *building* a CRC to append to your message,\nand *verifying* whether an existing message is valid:\n\n```cpp\n#include \u003ccrc/crc.hpp\u003e // Or: import crc;\n\n// Build a CRC:\nstd::string_view data {\"Hello world!\"};\nstd::uint32_t crc {crc::crc32c::calculate(data)};\n\n// Verify a CRC:\nif (!crc::crc8_bluetooth::is_valid(some_message)) {\n    throw std::runtime_error {\"Message is corrupted!\"};\n}\n```\n\nFor more complex cases, the CRC can be built up incrementally:\n\n```cpp\n// Step 1: initialize CRC state.\ncrc::crc64_xz crc {};\n\n// Step 2: feed it data.\n// Notice how you can pass in any byte-like type, without any casts.\ncrc = crc::process(crc, \"Some da\"sv);\ncrc = crc::process(crc, u8\"ta processed in \"sv);\ncrc = crc::process(crc, std::vector\u003cstd::uint8_t\u003e {'p', 'a', 'r', 't', 's'});\n\n// Step 3: extract the final CRC.\nstd::uint64_t result {crc::finalize(crc};\nassert(result == crc::crc64_xz::calculate(\"Some data processed in parts\"sv));\n```\n\nAll the functions above also have overloads taking iterator pairs instead of ranges.\n\n### Choosing an algorithm\n\nThere are many algorithms for calculating CRCs.\nThe library will pick a good default (currently, that's `crc::algorithms::slice_by\u003c8\u003e`),\nbut it isn't omniscient,\nso you have the ability to specify which algorithm you want.\nThe following algorithms are available:\n\n- **`crc::algorithms::slice_by\u003cN\u003e`:** process `N` bytes at a time.\n  Requires an `N * 256 * sizeof(CRCType)` byte lookup table.\n  For example, CRC32C implemented with slice-by-4 requires a 4 KiB lookup table.\n\nTo specify an algorithm, pass it as the first parameter to `crc::\u003c...\u003e::calculate` or `crc::process`:\n\n```cpp\ncrc::crc32_mpeg2::calculate(crc::algorithms::slice_by\u003c8\u003e, ...);\n\ncrc::crc32_mpeg2 crc {};\ncrc = crc::process(crc::algorithms::slice_by\u003c8\u003e, crc, ...);\n```\n\nIf you want to write your own functions that take CRC algorithms as arguments,\nconstrain them with the `crc::algorithm` concept:\n\n```cpp\nvoid my_function(crc::algorithm auto algo, ...) {\n    crc::crc32c::calculate(algo, ...); // Pass along the algorithm.\n}\n```\n\n### Defining your own CRCs\n\nThe CRC you're looking for almost certainly comes predefined\n(if it's missing, consider [filing an issue](https://github.com/LocalSpook/crc/issues)),\nbut you can define your own too:\n\n```cpp\nusing crc32c = crc::crc\u003c\n    std::uint32_t, // The CRC's type; an unsigned integer at least as wide as the polynomial.\n    32,            // The polynomial's width.\n    0x1EDC6F41,    // The polynomial, with an implicit leading term.\n    0xFFFFFFFF,    // The initial value of the CRC register.\n    true,          // True if the bits in a byte should be ordered from LSb to MSb, false if vice-versa.\n    true,          // True if the result should be reflected during finalization.\n    0xFFFFFFFF     // The value XORed into the result at the very end, after any reflection.\n\u003e;\n```\n\nOr you can adapt existing CRCs:\n\n```cpp\n// Identical to crc::crc32, but with the opposite bit ordering.\nusing crc32_reflected = crc::crc\u003c\n    crc::crc32::crc_type,\n    crc::crc32::width,\n    crc::crc32::poly,\n    crc::crc32::initial,\n    !crc::crc32::refin, // ⭐\n    !crc::crc32::refout, // ⭐\n    crc::crc32::xorout\n\u003e;\n```\n\nNote that CRCs of width greater than 64 are currently unsupported.\n\n### Composability\n\nAll provided functions are function objects and can be passed to other algorithms:\n\n```cpp\nstd::vector\u003cstd::string\u003e strings {\"Apple\", \"Banana\", \"Cherry\", \"Dragonfruit\"};\nstd::vector\u003cstd::uint32_t\u003e crcs {std::from_range, strings | std::views::transform(crc::crc32c::calculate)};\n\n// Calculate a CRC over several noncontiguous chunks.\nstd::vector\u003cstd::vector\u003cunsigned char\u003e\u003e data {...};\nstd::uint32_t crc {crc::finalize(std::ranges::fold_left(data, crc::crc32c {}, crc::process))};\n```\n\n## Installing\n\n### With FetchContent (recommended)\n\n```cmake\nFetchContent_Declare(crc\n    GIT_REPOSITORY https://github.com/LocalSpook/crc\n    GIT_TAG ... # You should use the latest commit on the main branch.\n    SYSTEM\n)\nFetchContent_MakeAvailable(crc)\n\ntarget_link_libraries(... crc::crc)\n```\n\n### With find_package\n\n```cmake\nfind_package(crc REQUIRED)\ntarget_link_libraries(... crc::crc)\n```\n\n### With vendoring (discouraged)\n\nJust copy [`include/crc/crc.hpp`](include/crc/crc.hpp) into your directory structure.\nFor any serious project, you're highly recommended to use a proper dependency management\nsystem instead, but this method *is* officially supported.\n\n## Building\n\nSimply run:\n\n```sh\ncmake -B build [-G Ninja]\ncmake --build build\n```\n\nTo also build the module, add `-DCRC_MODULE=ON`, then pass `crc::crc-module` to `target_link_libraries`.\nThe module cannot currently be installed, so this is only available when using FetchContent.\n\nTo build tests, add `-DBUILD_TESTING=ON`.\nThe resulting test binary will be `build/bin/tests`.\nOur testing framework is Catch2;\nit will be downloaded automatically using FetchContent.\nIf you also configured with `-DCRC_MODULE=ON`,\nthe module tests will be added to the binary.\nWe have a 2 by 2 testing matrix:\ncompile versus run time, and header versus module.\nTo test just the header, run `./build/bin/tests --section header`.\nTo test just the module, run `./build/bin/tests --section module`.\n\n## Miscellaneous\n\n- This library provides zero ABI stability guarantees.\n- It should compile cleanly even at very high warning levels.\n  If you see a warning, [file an issue](https://github.com/LocalSpook/crc/issues).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalspook%2Fcrc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flocalspook%2Fcrc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalspook%2Fcrc/lists"}