From d50dc288cd6cbd83528e1e9e1c108e1cdb6a388e Mon Sep 17 00:00:00 2001 From: cpq Date: Mon, 8 Mar 2021 15:40:39 +0000 Subject: [PATCH] Add mg_crc32 --- src/util.c | 10 ++++++++++ src/util.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/util.c b/src/util.c index 2df5124a..8ea74ccd 100644 --- a/src/util.c +++ b/src/util.c @@ -273,6 +273,16 @@ int64_t mg_to64(struct mg_str str) { return result * neg; } +uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) { + int i; + crc = ~crc; + while (len--) { + crc ^= *(unsigned char *) buf++; + for (i = 0; i < 8; i++) crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1; + } + return ~crc; +} + double mg_time(void) { #if MG_ARCH == MG_ARCH_WIN32 SYSTEMTIME sysnow; diff --git a/src/util.h b/src/util.h index 52f243a3..1e172cf1 100644 --- a/src/util.h +++ b/src/util.h @@ -12,6 +12,7 @@ bool mg_globmatch(const char *pattern, int plen, const char *s, int n); bool mg_next_comma_entry(struct mg_str *s, struct mg_str *k, struct mg_str *v); uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); +uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len); char *mg_hexdump(const void *buf, size_t len); char *mg_hex(const void *buf, int len, char *dst); void mg_unhex(const char *buf, int len, unsigned char *to);