Commit b5f51048 authored by Jack Andersen's avatar Jack Andersen

Checksum and Card copy fixes

parent 3121f67f
......@@ -30,7 +30,7 @@ class BlockAllocationTable
public:
explicit BlockAllocationTable(uint32_t blockCount = (uint32_t(ECardSize::Card2043Mb) * MbitToBlocks));
BlockAllocationTable(uint8_t data[BlockSize]);
~BlockAllocationTable();
~BlockAllocationTable() = default;
uint16_t getNextBlock(uint16_t block) const;
uint16_t nextFreeBlock(uint16_t maxBlock, uint16_t startingBlock) const;
......
......@@ -124,14 +124,10 @@ class Card
SystemString m_filename;
FILE* m_fileHandle = nullptr;
Directory m_dir;
Directory m_dirBackup;
Directory* m_currentDir;
Directory* m_previousDir;
BlockAllocationTable m_bat;
BlockAllocationTable m_batBackup;
BlockAllocationTable* m_currentBat;
BlockAllocationTable* m_previousBat;
Directory m_dirs[2];
BlockAllocationTable m_bats[2];
uint8_t m_currentDir;
uint8_t m_currentBat;
uint16_t m_maxBlock;
char m_game[5] = {'\0'};
......@@ -149,7 +145,11 @@ public:
* @brief Card
* @param other
*/
Card(const Card& other);
Card(const Card& other) = delete;
Card& operator=(const Card& other) = delete;
Card(Card&& other) = default;
Card& operator=(Card&& other) = default;
/**
* @brief Card
* @param filepath
......
......@@ -29,9 +29,7 @@ class Directory
public:
Directory();
Directory(uint8_t data[BlockSize]);
Directory(const Directory& other);
void operator=(const Directory& other);
~Directory();
~Directory() = default;
bool hasFreeFile() const;
int32_t numFreeFiles() const;
......
......@@ -40,7 +40,7 @@ public:
File();
File(char data[0x40]);
File(const char* filename);
~File() {}
~File() = default;
};
}
......
......@@ -358,6 +358,7 @@ static inline int Stat(const SystemChar* path, Sstat* statOut)
* @param checksumInv
*/
void calculateChecksumBE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv);
void calculateChecksumLE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv);
}
#endif // __KABU_UTIL_HPP__
......@@ -16,18 +16,14 @@ void BlockAllocationTable::swapEndian()
void BlockAllocationTable::updateChecksum()
{
swapEndian();
calculateChecksumBE(reinterpret_cast<uint16_t*>(__raw + 4), 0xFFE, &m_checksum, &m_checksumInv);
swapEndian();
calculateChecksumLE(reinterpret_cast<uint16_t*>(__raw + 4), 0xFFE, &m_checksum, &m_checksumInv);
}
bool BlockAllocationTable::valid() const
{
uint16_t ckSum, ckSumInv;
BlockAllocationTable tmp = *this;
tmp.swapEndian();
calculateChecksumBE(reinterpret_cast<const uint16_t*>(tmp.__raw + 4), 0xFFE, &ckSum, &ckSumInv);
return (SBig(ckSum) == m_checksum && SBig(ckSumInv) == m_checksumInv);
calculateChecksumLE(reinterpret_cast<const uint16_t*>(__raw + 4), 0xFFE, &ckSum, &ckSumInv);
return (ckSum == m_checksum && ckSumInv == m_checksumInv);
}
BlockAllocationTable::BlockAllocationTable(uint32_t blockCount)
......@@ -38,8 +34,6 @@ BlockAllocationTable::BlockAllocationTable(uint32_t blockCount)
updateChecksum();
}
BlockAllocationTable::~BlockAllocationTable() {}
uint16_t BlockAllocationTable::getNextBlock(uint16_t block) const
{
if ((block < FSTBlocks) || (block > (BATSize - FSTBlocks)))
......
This diff is collapsed.
......@@ -15,18 +15,14 @@ void Directory::swapEndian()
void Directory::updateChecksum()
{
swapEndian();
calculateChecksumBE(reinterpret_cast<uint16_t*>(__raw), 0xFFE, &m_checksum, &m_checksumInv);
swapEndian();
calculateChecksumLE(reinterpret_cast<uint16_t*>(__raw), 0xFFE, &m_checksum, &m_checksumInv);
}
bool Directory::valid() const
{
uint16_t ckSum, ckSumInv;
Directory tmp = *this;
tmp.swapEndian();
calculateChecksumBE(reinterpret_cast<const uint16_t*>(tmp.__raw), 0xFFE, &ckSum, &ckSumInv);
return (SBig(ckSum) == m_checksum && SBig(ckSumInv) == m_checksumInv);
calculateChecksumLE(reinterpret_cast<const uint16_t*>(__raw), 0xFFE, &ckSum, &ckSumInv);
return (ckSum == m_checksum && ckSumInv == m_checksumInv);
}
Directory::Directory()
......@@ -38,12 +34,6 @@ Directory::Directory()
Directory::Directory(uint8_t data[]) { memcpy(__raw, data, BlockSize); }
Directory::Directory(const Directory& other) { memcpy(__raw, other.__raw, BlockSize); }
void Directory::operator=(const Directory& other) { memcpy(__raw, other.__raw, BlockSize); }
Directory::~Directory() {}
bool Directory::hasFreeFile() const
{
for (uint16_t i = 0; i < 127; i++)
......
......@@ -41,4 +41,20 @@ void calculateChecksumBE(const uint16_t* data, size_t len, uint16_t* checksum, u
if (*checksumInv == 0xFFFF)
*checksumInv = 0;
}
void calculateChecksumLE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv)
{
*checksum = 0;
*checksumInv = 0;
for (size_t i = 0; i < len; ++i)
{
*checksum += data[i];
*checksumInv += uint16_t(data[i] ^ 0xFFFF);
}
if (*checksum == 0xFFFF)
*checksum = 0;
if (*checksumInv == 0xFFFF)
*checksumInv = 0;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment