Replace remaining uses of zlib crc32 with Common/Hash.h

This commit is contained in:
Pokechu22 2021-12-10 13:34:40 -08:00
parent 2652aed85c
commit 0c19f895d3
6 changed files with 38 additions and 23 deletions

View File

@ -534,11 +534,25 @@ void SetHash64Function()
u32 ComputeCRC32(std::string_view data) u32 ComputeCRC32(std::string_view data)
{ {
const Bytef* buf = reinterpret_cast<const Bytef*>(data.data()); return ComputeCRC32(reinterpret_cast<const u8*>(data.data()), static_cast<u32>(data.size()));
uInt len = static_cast<uInt>(data.size()); }
u32 ComputeCRC32(const u8* ptr, u32 length)
{
return UpdateCRC32(StartCRC32(), ptr, length);
}
u32 StartCRC32()
{
return crc32(0L, Z_NULL, 0);
}
u32 UpdateCRC32(u32 crc, const u8* ptr, u32 length)
{
static_assert(std::is_same_v<const u8*, const Bytef*>);
static_assert(std::is_same_v<u32, uInt>);
// Use zlib's crc32 implementation to compute the hash // Use zlib's crc32 implementation to compute the hash
u32 hash = crc32(0L, Z_NULL, 0); // crc32_z (which takes a size_t) would be better, but it isn't available on Android
hash = crc32(hash, buf, len); return crc32(crc, ptr, length);
return hash;
} }
} // namespace Common } // namespace Common

View File

@ -17,4 +17,7 @@ u64 GetHash64(const u8* src, u32 len, u32 samples);
void SetHash64Function(); void SetHash64Function();
u32 ComputeCRC32(std::string_view data); u32 ComputeCRC32(std::string_view data);
u32 ComputeCRC32(const u8* ptr, u32 length);
u32 StartCRC32();
u32 UpdateCRC32(u32 crc, const u8* ptr, u32 length);
} // namespace Common } // namespace Common

View File

@ -8,10 +8,10 @@
#include <iterator> #include <iterator>
#include <mbedtls/bignum.h> #include <mbedtls/bignum.h>
#include <zlib.h>
#include "Common/BitUtils.h" #include "Common/BitUtils.h"
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/Hash.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
@ -146,9 +146,9 @@ void MotionPlus::Reset()
void MotionPlus::CalibrationData::UpdateChecksum() void MotionPlus::CalibrationData::UpdateChecksum()
{ {
// Checksum is crc32 of all data other than the checksum itself. // Checksum is crc32 of all data other than the checksum itself.
auto crc_result = crc32(0, Z_NULL, 0); u32 crc_result = Common::StartCRC32();
crc_result = crc32(crc_result, reinterpret_cast<const Bytef*>(this), 0xe); crc_result = Common::UpdateCRC32(crc_result, reinterpret_cast<const u8*>(this), 0xe);
crc_result = crc32(crc_result, reinterpret_cast<const Bytef*>(this) + 0x10, 0xe); crc_result = Common::UpdateCRC32(crc_result, reinterpret_cast<const u8*>(this) + 0x10, 0xe);
crc32_lsb = u16(crc_result); crc32_lsb = u16(crc_result);
crc32_msb = u16(crc_result >> 16); crc32_msb = u16(crc_result >> 16);

View File

@ -16,13 +16,13 @@
#include <mbedtls/sha1.h> #include <mbedtls/sha1.h>
#include <pugixml.hpp> #include <pugixml.hpp>
#include <unzip.h> #include <unzip.h>
#include <zlib.h>
#include "Common/Align.h" #include "Common/Align.h"
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/Hash.h"
#include "Common/HttpRequest.h" #include "Common/HttpRequest.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
@ -1041,7 +1041,7 @@ void VolumeVerifier::SetUpHashing()
[](const GroupToVerify& a, const GroupToVerify& b) { return a.offset < b.offset; }); [](const GroupToVerify& a, const GroupToVerify& b) { return a.offset < b.offset; });
if (m_hashes_to_calculate.crc32) if (m_hashes_to_calculate.crc32)
m_crc32_context = crc32(0, nullptr, 0); m_crc32_context = Common::StartCRC32();
if (m_hashes_to_calculate.md5) if (m_hashes_to_calculate.md5)
{ {
@ -1171,9 +1171,8 @@ void VolumeVerifier::Process()
if (m_hashes_to_calculate.crc32) if (m_hashes_to_calculate.crc32)
{ {
m_crc32_future = std::async(std::launch::async, [this, byte_increment] { m_crc32_future = std::async(std::launch::async, [this, byte_increment] {
// It would be nice to use crc32_z here instead of crc32, but it isn't available on Android
m_crc32_context = m_crc32_context =
crc32(m_crc32_context, m_data.data(), static_cast<unsigned int>(byte_increment)); Common::UpdateCRC32(m_crc32_context, m_data.data(), static_cast<u32>(byte_increment));
}); });
} }

View File

@ -172,7 +172,7 @@ private:
Hashes<bool> m_hashes_to_calculate{}; Hashes<bool> m_hashes_to_calculate{};
bool m_calculating_any_hash = false; bool m_calculating_any_hash = false;
unsigned long m_crc32_context = 0; u32 m_crc32_context = 0;
mbedtls_md5_context m_md5_context{}; mbedtls_md5_context m_md5_context{};
mbedtls_sha1_context m_sha1_context{}; mbedtls_sha1_context m_sha1_context{};

View File

@ -7,9 +7,8 @@
#include <cstring> #include <cstring>
#include <optional> #include <optional>
#include <zlib.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Hash.h"
namespace ciface::DualShockUDPClient::Proto namespace ciface::DualShockUDPClient::Proto
{ {
@ -217,11 +216,6 @@ struct FromClient
}; };
} // namespace MessageType } // namespace MessageType
static inline u32 CRC32(const void* buffer, unsigned length)
{
return crc32(crc32(0L, Z_NULL, 0), static_cast<const Bytef*>(buffer), length);
}
template <typename MsgType> template <typename MsgType>
struct Message struct Message
{ {
@ -236,7 +230,11 @@ struct Message
m_message.message_type = MsgType::TYPE; m_message.message_type = MsgType::TYPE;
} }
void Finish() { m_message.header.crc32 = CRC32(&m_message, sizeof(m_message)); } void Finish()
{
m_message.header.crc32 =
Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(m_message));
}
template <class ToMsgType> template <class ToMsgType>
std::optional<ToMsgType> CheckAndCastTo() std::optional<ToMsgType> CheckAndCastTo()
@ -244,7 +242,8 @@ struct Message
const u32 crc32_in_header = m_message.header.crc32; const u32 crc32_in_header = m_message.header.crc32;
// zero out the crc32 in the packet once we got it since that's whats needed for calculation // zero out the crc32 in the packet once we got it since that's whats needed for calculation
m_message.header.crc32 = 0; m_message.header.crc32 = 0;
const u32 crc32_calculated = CRC32(&m_message, sizeof(ToMsgType)); const u32 crc32_calculated =
Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(ToMsgType));
if (crc32_in_header != crc32_calculated) if (crc32_in_header != crc32_calculated)
{ {
NOTICE_LOG_FMT( NOTICE_LOG_FMT(