2018-12-11 11:10:57 -07:00
|
|
|
/*
|
2024-06-15 09:01:19 -06:00
|
|
|
Copyright 2016-2024 melonDS team
|
2018-12-11 11:10:57 -07:00
|
|
|
|
|
|
|
This file is part of melonDS.
|
|
|
|
|
|
|
|
melonDS 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, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
melonDS 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with melonDS. If not, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CRC32.h"
|
|
|
|
|
2018-12-11 11:16:19 -07:00
|
|
|
// http://www.codeproject.com/KB/recipes/crc32_large.aspx
|
|
|
|
|
2023-11-25 10:32:09 -07:00
|
|
|
namespace melonDS
|
|
|
|
{
|
2023-08-28 12:01:15 -06:00
|
|
|
constexpr u32 _reflect(u32 refl, char ch)
|
2018-12-11 11:10:57 -07:00
|
|
|
{
|
|
|
|
u32 value = 0;
|
|
|
|
|
|
|
|
for(int i = 1; i < (ch + 1); i++)
|
|
|
|
{
|
|
|
|
if (refl & 1)
|
|
|
|
value |= 1 << (ch - i);
|
|
|
|
refl >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:01:15 -06:00
|
|
|
constexpr auto GetCRC32Table()
|
2018-12-11 11:10:57 -07:00
|
|
|
{
|
2023-08-28 12:01:15 -06:00
|
|
|
std::array<u32, 256> Crc32Table { 0 };
|
2018-12-11 11:10:57 -07:00
|
|
|
u32 polynomial = 0x04C11DB7;
|
|
|
|
|
|
|
|
for (int i = 0; i < 0x100; i++)
|
|
|
|
{
|
2023-08-28 12:01:15 -06:00
|
|
|
Crc32Table[i] = _reflect(i, 8) << 24;
|
2018-12-11 11:10:57 -07:00
|
|
|
|
|
|
|
for (int j = 0; j < 8; j++)
|
2023-08-28 12:01:15 -06:00
|
|
|
Crc32Table[i] = (Crc32Table[i] << 1) ^ (Crc32Table[i] & (1 << 31) ? polynomial : 0);
|
2018-12-11 11:10:57 -07:00
|
|
|
|
2023-08-28 12:01:15 -06:00
|
|
|
Crc32Table[i] = _reflect(Crc32Table[i], 32);
|
2018-12-11 11:10:57 -07:00
|
|
|
}
|
2023-08-28 12:01:15 -06:00
|
|
|
return Crc32Table;
|
2018-12-11 11:10:57 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
u32 CRC32(const u8 *data, int len, u32 start)
|
2018-12-11 11:10:57 -07:00
|
|
|
{
|
2023-08-28 12:01:15 -06:00
|
|
|
auto Crc32Table = GetCRC32Table();
|
2018-12-11 11:10:57 -07:00
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
u32 crc = start ^ 0xFFFFFFFF;
|
2018-12-11 11:10:57 -07:00
|
|
|
|
|
|
|
while (len--)
|
2023-08-28 12:01:15 -06:00
|
|
|
crc = (crc >> 8) ^ Crc32Table[(crc & 0xFF) ^ *data++];
|
2018-12-11 11:10:57 -07:00
|
|
|
|
|
|
|
return (crc ^ 0xFFFFFFFF);
|
|
|
|
}
|
2023-11-25 10:32:09 -07:00
|
|
|
|
|
|
|
}
|