aboutsummaryrefslogtreecommitdiff
path: root/src/CRC32.cpp
blob: 0756c034fa0600384b802e52d6d474b28a2bf0cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    Copyright 2016-2023 melonDS team

    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"

// http://www.codeproject.com/KB/recipes/crc32_large.aspx

namespace melonDS
{
constexpr u32 _reflect(u32 refl, char ch)
{
    u32 value = 0;

    for(int i = 1; i < (ch + 1); i++)
    {
        if (refl & 1)
            value |= 1 << (ch - i);
        refl >>= 1;
    }

	return value;
}

constexpr auto GetCRC32Table()
{
    std::array<u32, 256> Crc32Table { 0 };
	u32 polynomial = 0x04C11DB7;

	for (int i = 0; i < 0x100; i++)
    {
        Crc32Table[i] = _reflect(i, 8) << 24;

        for (int j = 0; j < 8; j++)
            Crc32Table[i] = (Crc32Table[i] << 1) ^ (Crc32Table[i] & (1 << 31) ? polynomial : 0);

        Crc32Table[i] = _reflect(Crc32Table[i],  32);
    }
    return Crc32Table;
}

u32 CRC32(const u8 *data, int len, u32 start)
{
    auto Crc32Table = GetCRC32Table();

	u32 crc = start ^ 0xFFFFFFFF;

	while (len--)
        crc = (crc >> 8) ^ Crc32Table[(crc & 0xFF) ^ *data++];

	return (crc ^ 0xFFFFFFFF);
}

}