Skip to content

AndyTechnologies/tiny-crc32c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tiny-crc32c

tiny-crc32c es una librería header-only en C++23 para el cálculo de CRC32C (polinomio Castagnoli 0x1EDC6F41 reflejado: 0x82F63B78), sin dependencias externas.

Características

  • Tabla en tiempo de compilación: consteval genera la tabla de 256 entradas en compilación.
  • Interfaz Raw: función tcrc32::crc32(const uint8_t* data, size_t length, uint32_t init = 0) para uso de bajo nivel.
  • STL-friendly: overload para contenedores con data() y size(), como std::vector, std::string, std::array.
  • Modo incremental: clase tcrc32::CRC32C con métodos update() y digest().
  • constexpr: permite cálculo en tiempo de compilación.
  • C++23: concepts para validaciones internas.

Instalación

git clone https://github.com/AndyTechnologies/tiny-crc32c.git
cd tiny-crc32c
mkdir build && cd build
cmake ..
cmake --build .

Tu proyecto solo necesita incluir:

#define _IMPL_TINY_CRC32_
#include "tiny_crc32c.hpp"

Nota: Debes definir el macro _IMPL_TINY_CRC32_ en un único archivo fuente antes de incluir el header para generar las definiciones de las funciones en tiempo de compilación. En otros archivos solo incluye el header sin el define.

Uso

Cálculo en una sola pasada

#include "tiny_crc32c.hpp"
#include <iostream>

int main() {
    const uint8_t data[] = {'H','o','l','a'};
    uint32_t crc = tcrc32::crc32(data, sizeof(data));
    std::cout << std::hex << crc;
}

Sobrecarga STL

std::vector<uint8_t> v = {...};
uint32_t crc = tcrc32::crc32(v);

Cálculo incremental

#include "tiny_crc32c.hpp"

tcrc32::CRC32C ctx;
ctx.update(data, len1);
ctx.update(data2, len2);
uint32_t crc = ctx.digest();

constexpr en tiempo de compilación

constexpr char s[] = "compile-time";
constexpr auto a = []{
    std::array<uint8_t, sizeof(s)-1> arr{};
    for (size_t i = 0; i < arr.size(); ++i) arr[i] = static_cast<uint8_t>(s[i]);
    return arr;
}();
static_assert(tcrc32::crc32(a.data(), a.size()) == 0x428A4F9Du);

Tests

Se incluye suite de tests con doctest:

cd build
tests --output-on-failure

Cobertura de tests:

  • Vectores conocidos (RFC, ASCII, UTF-8, frases comunes).
  • Consistencia raw vs incremental.
  • Buffers aleatorios y grandes.
  • Casos límite (longitudes cero, valor inicial no nulo).
  • Validaciones internas de la tabla.

Benchmark

La librería incluye un benchmark opcional para evaluar su rendimiento:

cd benchmarks
cmake -B build -S .
cmake --build build --config Release
./build/tiny_crc32c_benchmark

Ejemplo de salida

Benchmark CRC32C (Castagnoli polynomial)
---------------------------------------
Data size: 1 MiB
Iterations: 1000

Raw API:         0.35 ms / iter
STL overload:    0.36 ms / iter
Incremental API: 0.42 ms / iter

Benchmark en tiempo de compilación

g++ -std=c++23 -ftime-report benchmark_compile.cpp -o bench

Esto ayuda a visualizar el impacto del consteval en tiempo de compilación.

Contribuciones

PRs bienvenidas. Mantener estilo header-only, sin deps externas.

Licencia

MIT

About

A tiny library to calculate a: cyclic redundancy check (CRC), in the fastest way.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages