From 7217536fddde62f8d5cd15e798dc28398d5fee0c Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 18 Jul 2024 17:33:15 +1000 Subject: [PATCH] MD5Digest: Span-ify --- src/common/md5_digest.cpp | 20 ++++++++++++++++++-- src/common/md5_digest.h | 12 ++++++++++-- src/core/achievements.cpp | 2 +- src/core/bios.cpp | 2 +- src/util/cd_image_hasher.cpp | 6 +++--- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/common/md5_digest.cpp b/src/common/md5_digest.cpp index b0c877a97..f45343c27 100644 --- a/src/common/md5_digest.cpp +++ b/src/common/md5_digest.cpp @@ -137,6 +137,16 @@ void MD5Digest::Reset() this->bits[1] = 0; } +std::array MD5Digest::HashData(std::span data) +{ + std::array ret; + + MD5Digest digest; + digest.Update(data); + digest.Final(ret); + return ret; +} + void MD5Digest::Update(const void* pData, u32 cbData) { u32 t; @@ -185,7 +195,13 @@ void MD5Digest::Update(const void* pData, u32 cbData) std::memcpy(this->in, pByteData, cbData); } -void MD5Digest::Final(u8 Digest[16]) +void MD5Digest::Update(std::span data) +{ + if (!data.empty()) + Update(data.data(), static_cast(data.size_bytes())); +} + +void MD5Digest::Final(std::span digest) { u32 count; u8* p; @@ -225,5 +241,5 @@ void MD5Digest::Final(u8 Digest[16]) MD5Transform(this->buf, (u32*)this->in); byteReverse((unsigned char*)this->buf, 4); - std::memcpy(Digest, this->buf, 16); + std::memcpy(digest.data(), this->buf, 16); } diff --git a/src/common/md5_digest.h b/src/common/md5_digest.h index d401a97a4..bf03a6e10 100644 --- a/src/common/md5_digest.h +++ b/src/common/md5_digest.h @@ -1,18 +1,26 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #pragma once #include "types.h" +#include +#include + class MD5Digest { public: + static constexpr u32 DIGEST_SIZE = 16; + MD5Digest(); void Update(const void* pData, u32 cbData); - void Final(u8 Digest[16]); + void Update(std::span data); + void Final(std::span digest); void Reset(); + static std::array HashData(std::span data); + private: u32 buf[4]; u32 bits[2]; diff --git a/src/core/achievements.cpp b/src/core/achievements.cpp index 43b8ffc39..3f8559a54 100644 --- a/src/core/achievements.cpp +++ b/src/core/achievements.cpp @@ -292,7 +292,7 @@ std::string Achievements::GetGameHash(CDImage* image) MD5Digest digest; digest.Update(executable_name.c_str(), static_cast(executable_name.size())); if (hash_size > 0) - digest.Update(executable_data.data(), hash_size); + digest.Update(executable_data); u8 hash[16]; digest.Final(hash); diff --git a/src/core/bios.cpp b/src/core/bios.cpp index 0f97be364..ec3806d3f 100644 --- a/src/core/bios.cpp +++ b/src/core/bios.cpp @@ -163,7 +163,7 @@ BIOS::Hash BIOS::GetImageHash(const BIOS::Image& image) { BIOS::Hash hash; MD5Digest digest; - digest.Update(image.data(), static_cast(image.size())); + digest.Update(image); digest.Final(hash.bytes); return hash; } diff --git a/src/util/cd_image_hasher.cpp b/src/util/cd_image_hasher.cpp index f2a14398f..6a9862cf5 100644 --- a/src/util/cd_image_hasher.cpp +++ b/src/util/cd_image_hasher.cpp @@ -45,7 +45,7 @@ bool CDImageHasher::ReadIndex(CDImage* image, u8 track, u8 index, MD5Digest* dig return false; } - digest->Update(sector.data(), static_cast(sector.size())); + digest->Update(sector); } progress_callback->SetProgressValue(index_length); @@ -126,7 +126,7 @@ bool CDImageHasher::GetImageHash(CDImage* image, Hash* out_hash, } progress_callback->SetProgressValue(image->GetTrackCount()); - digest.Final(out_hash->data()); + digest.Final(*out_hash); return true; } @@ -137,6 +137,6 @@ bool CDImageHasher::GetTrackHash(CDImage* image, u8 track, Hash* out_hash, if (!ReadTrack(image, track, &digest, progress_callback)) return false; - digest.Final(out_hash->data()); + digest.Final(*out_hash); return true; }