1*eba19a3bSAndrew Jeffery /* SPDX-License-Identifier: Apache-2.0 */ 2*eba19a3bSAndrew Jeffery /* Copyright 2021 IBM Corp. */ 3*eba19a3bSAndrew Jeffery 4*eba19a3bSAndrew Jeffery #include "crc32.h" 5*eba19a3bSAndrew Jeffery 6*eba19a3bSAndrew Jeffery #include <limits.h> 7*eba19a3bSAndrew Jeffery 8*eba19a3bSAndrew Jeffery /* Very dumb CRC-32 implementation */ crc32(const void * buf,size_t len)9*eba19a3bSAndrew Jefferyuint32_t crc32(const void *buf, size_t len) 10*eba19a3bSAndrew Jeffery { 11*eba19a3bSAndrew Jeffery const uint8_t *buf8 = buf; 12*eba19a3bSAndrew Jeffery uint32_t rem = 0xffffffff; 13*eba19a3bSAndrew Jeffery 14*eba19a3bSAndrew Jeffery for (; len; len--) { 15*eba19a3bSAndrew Jeffery int i; 16*eba19a3bSAndrew Jeffery 17*eba19a3bSAndrew Jeffery rem = rem ^ *buf8; 18*eba19a3bSAndrew Jeffery for (i = 0; i < CHAR_BIT; i++) 19*eba19a3bSAndrew Jeffery rem = (rem >> 1) ^ ((rem & 1) * 0xEDB88320); 20*eba19a3bSAndrew Jeffery 21*eba19a3bSAndrew Jeffery buf8++; 22*eba19a3bSAndrew Jeffery } 23*eba19a3bSAndrew Jeffery 24*eba19a3bSAndrew Jeffery return rem ^ 0xffffffff; 25*eba19a3bSAndrew Jeffery } 26