From 7cf09140925e75fd03437696eb93e252640e290d Mon Sep 17 00:00:00 2001 From: Tau Date: Thu, 8 Nov 2018 10:20:21 -0500 Subject: [PATCH] util/crc.c: Add simple CRC-32 implementation --- util/crc.c | 28 ++++++++++++++++++++++++++++ util/crc.h | 6 ++++++ util/meson.build | 2 ++ 3 files changed, 36 insertions(+) create mode 100644 util/crc.c create mode 100644 util/crc.h diff --git a/util/crc.c b/util/crc.c new file mode 100644 index 0000000..5e4defb --- /dev/null +++ b/util/crc.c @@ -0,0 +1,28 @@ +#include +#include + +#include "util/crc.h" + +uint32_t crc32(const void *src, size_t nbytes, uint32_t in) +{ + const uint8_t *bytes; + uint32_t crc; + size_t i; + + bytes = src; + crc = ~in; + + for (i = 0 ; i < nbytes * 8 ; i++) { + if (i % 8 == 0) { + crc ^= *bytes++; + } + + if (crc & 1) { + crc = (crc >> 1) ^ 0xEDB88320; + } else { + crc = (crc >> 1); + } + } + + return ~crc; +} diff --git a/util/crc.h b/util/crc.h new file mode 100644 index 0000000..16ca927 --- /dev/null +++ b/util/crc.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +uint32_t crc32(const void *src, size_t nbytes, uint32_t in); diff --git a/util/meson.build b/util/meson.build index 9eab6d3..c9f447f 100644 --- a/util/meson.build +++ b/util/meson.build @@ -7,6 +7,8 @@ util_lib = static_library( capnhook.get_variable('hook_dep'), ], sources : [ + 'crc.c', + 'crc.h', 'dprintf.c', 'dprintf.h', 'setupapi.c',