cc0b6f18cd
* Add CRC32 using polynomial 0x04C11DB7 + Add macro to caculate CRC of a buffer Change-Id: If98e4e12bb53a6e5123e94e8cdffde1eb3bc4b4b Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/37753 Reviewed-by: Philipp Deppenwiese <zaolin.daisuki@gmail.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
52 lines
1.3 KiB
C
52 lines
1.3 KiB
C
/*
|
|
* This file is part of the coreboot project.
|
|
*
|
|
* Copyright (C) 2019 HardenedLinux
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; version 2 of the License.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
#include <crc_byte.h>
|
|
|
|
uint8_t crc7_byte(uint8_t prev_crc, uint8_t data)
|
|
{
|
|
const uint8_t g = 0x89;
|
|
prev_crc ^= data;
|
|
for (int i = 0; i < 8; i++) {
|
|
if (prev_crc & 0x80)
|
|
prev_crc ^= g;
|
|
prev_crc <<= 1;
|
|
}
|
|
return prev_crc;
|
|
}
|
|
|
|
uint16_t crc16_byte(uint16_t prev_crc, uint8_t data)
|
|
{
|
|
prev_crc = (uint8_t)(prev_crc >> 8)|(prev_crc << 8);
|
|
prev_crc ^= data;
|
|
prev_crc ^= (uint8_t)(prev_crc & 0xff) >> 4;
|
|
prev_crc ^= (prev_crc << 8) << 4;
|
|
prev_crc ^= ((prev_crc & 0xff) << 4) << 1;
|
|
return prev_crc;
|
|
}
|
|
|
|
uint32_t crc32_byte(uint32_t prev_crc, uint8_t data)
|
|
{
|
|
prev_crc ^= (uint32_t)data << 24;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
if ((prev_crc & 0x80000000UL) != 0)
|
|
prev_crc = ((prev_crc << 1) ^ 0x04C11DB7UL);
|
|
else
|
|
prev_crc <<= 1;
|
|
}
|
|
|
|
return prev_crc;
|
|
}
|