57 lines
1.6 KiB
C
57 lines
1.6 KiB
C
/* *****************************************************************************
|
|
* utils/checksum.c -- checksum utilities.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m 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 Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/**
|
|
* g1m_checksum8:
|
|
* Make a simple 8-bit checksum.
|
|
*
|
|
* @arg mem the memory zone.
|
|
* @arg size the memory zone size.
|
|
* @return the checksum
|
|
*/
|
|
|
|
uint8_t g1m_checksum8(void *mem, size_t size)
|
|
{
|
|
uint8_t *m = mem, c = 0;
|
|
|
|
while (size--)
|
|
c += *m++;
|
|
return (c);
|
|
}
|
|
|
|
/**
|
|
* g1m_checksum32:
|
|
* Make checksums great again.
|
|
*
|
|
* @arg mem the memory zone
|
|
* @arg size the memory zone size
|
|
* @arg checksum current checksum
|
|
* @return the new checksum
|
|
*/
|
|
|
|
uint32_t g1m_checksum32(void *mem, size_t size, uint32_t checksum)
|
|
{
|
|
uint8_t *m = mem;
|
|
|
|
while (size--)
|
|
checksum += *m++;
|
|
return (checksum);
|
|
}
|