melonDS/src/CRC32.cpp

68 lines
1.6 KiB
C++
Raw Normal View History

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
namespace melonDS
{
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;
}
constexpr auto GetCRC32Table()
2018-12-11 11:10:57 -07: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++)
{
Crc32Table[i] = _reflect(i, 8) << 24;
2018-12-11 11:10:57 -07:00
for (int j = 0; j < 8; j++)
Crc32Table[i] = (Crc32Table[i] << 1) ^ (Crc32Table[i] & (1 << 31) ? polynomial : 0);
2018-12-11 11:10:57 -07:00
Crc32Table[i] = _reflect(Crc32Table[i], 32);
2018-12-11 11:10:57 -07:00
}
return Crc32Table;
2018-12-11 11:10:57 -07:00
}
Split the cart parsing and loading steps (#1707) * Split ROMList into a .cpp file - Its definition in ROMList.h was causing multiple-definition linker errors - Introduce ROMListSize, since you can't take a sizeof() of an extern declaration - Mark ROMList and ROMListSize as const * Update ReadROMParams to accommodate ROMList changes * Split parsing and loading of NDS ROMs - Introduce an NDSCartData class for parsing a ROM file - Introduce InsertROM for loading a NDS cart - Refactor LoadROM to use NDSCartData and InsertROM under the hood * Reset cart state and initialize save memory in the NDSCartData constructor - Otherwise there's no way to know about SRAM-specific attributes before inserting the game * Add a comment to NDSCartData * First crack at splitting parsing and loading for GBACart * Add some logging calls for encrypting the secure area * Log the XXH64 hash of the inserted NDS ROM * Log the XXH64 hash of the secure area after decryption * Add some logging to Key1_LoadKeyBuf * Re-encrypt the secure area when inserting the cart, not when parsing it - This way, constructing a NDSCart doesn't imply a read from the filesystem (as is done in Key1_KeyBuf) * Load Key1_KeyBuf from memory, not from the file system - Now that the cart's secure area isn't re-encrypted until insertion, we can expect that the BIOS will be ready at this point * Add some helper query methods to NDSHeader * Query the DSi region directly from the header instead of checking the ROM again * Introduce a CartType enum - So CartCommon::Type doesn't have to return magic numbers * Reset the cart in NDSCart::InsertROM instead of the NDSCartData constructor - That way the constructor doesn't rely on the config or on file I/O when loading homebrew - This keeps the use of global state closer to one place * Add non-const getters for the carts * Add InsertROM overloads that accept unique_ptrs * Fix a comment * Rename member functions on NDSCartData and GBACartData to adhere to the convention * Rename members on NDSCartData and GBACartData to adhere to the convention * Fix build errors on some platforms * Add NDSHeader::IsDSiWare * Add a ROMListEntry parameter to the cart constructors - To allow for looking up details of SRAM or expected ROM size * Add some new getters to CartCommon * Use the Header/Banner members instead of globals * Make CartCommon abstract - It's not supposed to be instantiated anyway * Change the signature of CartCommon::Checksum - It's neither overridden nor mutating * Add some clarifying comments to NDSHeader * Delete CartCommon::ROM in its destructor - ParseROM copies its input and gives that copy to the cart object, so it's okay * Add some getters to CartCommon * Refactor NDSCart - Get rid of NDSCartData - Get rid of cart-specific global state within NDSCart (so registers are untouched) - Refactor uses of removed global variables to use the Cart pointer instead - Refactor ROMInfoDialog's icon functions to accept const arguments * Return the cart pointer - So *that's* why it was crashing. Whoops - Why is this even allowed? * Refactor GBACart - Delete CartGame::ROM in the destructor - Get rid of GBACartData - Remove some global state * Mark NDSCart::CartCommon::Type as const * Slightly refactor GBACart::CartCommon - Mark Type as const - Use enum constants - Make CartCommon itself abstract * Mark CRC32's data parameter as const * Mark GBACart::CartCommon::Checksum as const * Use assert.h instead of cassert - As demanded by the style guide * Fix some includes to adhere to the style guide * Get the ARM9 entry address directly from the header object * Use more Header fields directly * Rename some parameters to match the style guide * Remove some unused includes * Slightly change NDS_Header::IsHomebrew for clarity
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
{
auto Crc32Table = GetCRC32Table();
2018-12-11 11:10:57 -07:00
u32 crc = start ^ 0xFFFFFFFF;
2018-12-11 11:10:57 -07:00
while (len--)
crc = (crc >> 8) ^ Crc32Table[(crc & 0xFF) ^ *data++];
2018-12-11 11:10:57 -07:00
return (crc ^ 0xFFFFFFFF);
}
}