File indexing completed on 2024-05-05 17:57:23

0001 /*
0002     SPDX-FileCopyrightText: 2003 Csaba Karai <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "crc32.h"
0009 
0010 #define MASK1 0x00FFFFFF
0011 #define MASK2 0xFFFFFFFF
0012 #define POLYNOMIAL 0xEDB88320
0013 
0014 bool CRC32::crc_initialized = false;
0015 unsigned long CRC32::crc_table[256];
0016 
0017 CRC32::CRC32(unsigned long initialValue)
0018 {
0019     crc_accum = initialValue;
0020 
0021     if (!crc_initialized) {
0022         for (int byte = 0; byte != 256; byte++) {
0023             unsigned long data = byte;
0024 
0025             for (int i = 8; i > 0; --i)
0026                 data = data & 1 ? (data >> 1) ^ POLYNOMIAL : data >> 1;
0027 
0028             crc_table[byte] = data;
0029         }
0030 
0031         crc_initialized = true;
0032     }
0033 }
0034 
0035 void CRC32::update(unsigned char *buffer, int bufferLen)
0036 {
0037     while (bufferLen-- > 0)
0038         crc_accum = ((crc_accum >> 8) & MASK1) ^ crc_table[(crc_accum & 0xff) ^ *buffer++];
0039 }
0040 
0041 unsigned long CRC32::result()
0042 {
0043     return (~crc_accum) & MASK2;
0044 }