diff --git a/source/WUD/content/WiiUDiscContentsHeader.cpp b/source/WUD/content/WiiUDiscContentsHeader.cpp index 3faf617..40886ee 100644 --- a/source/WUD/content/WiiUDiscContentsHeader.cpp +++ b/source/WUD/content/WiiUDiscContentsHeader.cpp @@ -26,7 +26,6 @@ std::optional> WiiUDiscContentsHeader::m return {}; } if (!discReader->hasDiscKey) { - DEBUG_FUNCTION_LINE_ERR(); if (!discReader->readEncrypted(buffer.get(), offset, LENGTH)) { DEBUG_FUNCTION_LINE_ERR("Failed to read data"); return {}; diff --git a/source/fs/FSUtils.cpp b/source/fs/FSUtils.cpp index 6bbd45a..6e0b507 100644 --- a/source/fs/FSUtils.cpp +++ b/source/fs/FSUtils.cpp @@ -1,64 +1,10 @@ #include "FSUtils.h" #include "CFile.hpp" -#include "utils/logger.h" -#include "utils/utils.h" +#include +#include #include -#include -#include -#include #include -int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) { - //! always initialze input - *inbuffer = nullptr; - if (size) - *size = 0; - - int32_t iFd = open(filepath, O_RDONLY); - if (iFd < 0) - return -1; - - uint32_t filesize = lseek(iFd, 0, SEEK_END); - lseek(iFd, 0, SEEK_SET); - - auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40)); - if (buffer == nullptr) { - close(iFd); - return -2; - } - - uint32_t blocksize = 0x4000; - uint32_t done = 0; - int32_t readBytes = 0; - - while (done < filesize) { - if (done + blocksize > filesize) { - blocksize = filesize - done; - } - readBytes = read(iFd, buffer + done, blocksize); - if (readBytes <= 0) - break; - done += readBytes; - } - - close(iFd); - - if (done != filesize) { - free(buffer); - buffer = nullptr; - return -3; - } - - *inbuffer = buffer; - - //! sign is optional input - if (size) { - *size = filesize; - } - - return filesize; -} - int32_t FSUtils::CheckFile(const char *filepath) { if (!filepath) return 0; @@ -130,39 +76,6 @@ int32_t FSUtils::CreateSubfolder(const char *fullpath) { return 1; } - -bool FSUtils::copyFile(const std::string &in, const std::string &out) { - // Using C++ buffers is **really** slow. Copying in 1023 byte chunks. - // Let's do it the old way. - size_t size; - - int source = open(in.c_str(), O_RDONLY, 0); - int dest = open(out.c_str(), 0x602, 0644); - if (source < 0) { - return false; - } - if (dest < 0) { - close(source); - return false; - } - - auto bufferSize = 1024 * 1024; - char *buf = (char *) memalign(0x40, ROUNDUP(bufferSize, 0x40)); - if (buf == NULL) { - return false; - } - - while ((size = read(source, buf, bufferSize)) > 0) { - write(dest, buf, size); - } - - free(buf); - - close(source); - close(dest); - return true; -} - int32_t FSUtils::saveBufferToFile(const char *path, void *buffer, uint32_t size) { CFile file(path, CFile::WriteOnly); if (!file.isOpen()) { diff --git a/source/fs/FSUtils.h b/source/fs/FSUtils.h index 67dd76a..3b727e2 100644 --- a/source/fs/FSUtils.h +++ b/source/fs/FSUtils.h @@ -5,13 +5,9 @@ class FSUtils { public: - static int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size); - static int32_t CreateSubfolder(const char *fullpath); static int32_t CheckFile(const char *filepath); - static bool copyFile(const std::string &in, const std::string &out); - static int32_t saveBufferToFile(const char *path, void *buffer, uint32_t size); }; \ No newline at end of file diff --git a/source/utils/TinySHA1.hpp b/source/utils/TinySHA1.hpp deleted file mode 100644 index 32ad0fa..0000000 --- a/source/utils/TinySHA1.hpp +++ /dev/null @@ -1,197 +0,0 @@ -// clang-format off -/* - * - * TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based - * on the implementation in boost::uuid::details. - * - * SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1 - * - * Copyright (c) 2012-22 SAURAV MOHAPATRA - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#ifndef _TINY_SHA1_HPP_ -#define _TINY_SHA1_HPP_ -#include -#include -#include -#include -namespace sha1 -{ - class SHA1 - { - public: - typedef uint32_t digest32_t[5]; - typedef uint8_t digest8_t[20]; - inline static uint32_t LeftRotate(uint32_t value, size_t count) { - return (value << count) ^ (value >> (32-count)); - } - SHA1(){ reset(); } - virtual ~SHA1() {} - SHA1(const SHA1& s) { *this = s; } - const SHA1& operator = (const SHA1& s) { - memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t)); - memcpy(m_block, s.m_block, 64); - m_blockByteIndex = s.m_blockByteIndex; - m_byteCount = s.m_byteCount; - return *this; - } - SHA1& reset() { - m_digest[0] = 0x67452301; - m_digest[1] = 0xEFCDAB89; - m_digest[2] = 0x98BADCFE; - m_digest[3] = 0x10325476; - m_digest[4] = 0xC3D2E1F0; - m_blockByteIndex = 0; - m_byteCount = 0; - return *this; - } - SHA1& processByte(uint8_t octet) { - this->m_block[this->m_blockByteIndex++] = octet; - ++this->m_byteCount; - if(m_blockByteIndex == 64) { - this->m_blockByteIndex = 0; - processBlock(); - } - return *this; - } - SHA1& processBlock(const void* const start, const void* const end) { - const uint8_t* begin = static_cast(start); - const uint8_t* finish = static_cast(end); - while(begin != finish) { - processByte(*begin); - begin++; - } - return *this; - } - SHA1& processBytes(const void* const data, size_t len) { - const uint8_t* block = static_cast(data); - processBlock(block, block + len); - return *this; - } - const uint32_t* getDigest(digest32_t digest) { - size_t bitCount = this->m_byteCount * 8; - processByte(0x80); - if (this->m_blockByteIndex > 56) { - while (m_blockByteIndex != 0) { - processByte(0); - } - while (m_blockByteIndex < 56) { - processByte(0); - } - } else { - while (m_blockByteIndex < 56) { - processByte(0); - } - } - processByte(0); - processByte(0); - processByte(0); - processByte(0); - processByte( static_cast((bitCount>>24) & 0xFF)); - processByte( static_cast((bitCount>>16) & 0xFF)); - processByte( static_cast((bitCount>>8 ) & 0xFF)); - processByte( static_cast((bitCount) & 0xFF)); - - memcpy(digest, m_digest, 5 * sizeof(uint32_t)); - return digest; - } - const uint8_t* getDigestBytes(digest8_t digest) { - digest32_t d32; - getDigest(d32); - size_t di = 0; - digest[di++] = ((d32[0] >> 24) & 0xFF); - digest[di++] = ((d32[0] >> 16) & 0xFF); - digest[di++] = ((d32[0] >> 8) & 0xFF); - digest[di++] = ((d32[0]) & 0xFF); - - digest[di++] = ((d32[1] >> 24) & 0xFF); - digest[di++] = ((d32[1] >> 16) & 0xFF); - digest[di++] = ((d32[1] >> 8) & 0xFF); - digest[di++] = ((d32[1]) & 0xFF); - - digest[di++] = ((d32[2] >> 24) & 0xFF); - digest[di++] = ((d32[2] >> 16) & 0xFF); - digest[di++] = ((d32[2] >> 8) & 0xFF); - digest[di++] = ((d32[2]) & 0xFF); - - digest[di++] = ((d32[3] >> 24) & 0xFF); - digest[di++] = ((d32[3] >> 16) & 0xFF); - digest[di++] = ((d32[3] >> 8) & 0xFF); - digest[di++] = ((d32[3]) & 0xFF); - - digest[di++] = ((d32[4] >> 24) & 0xFF); - digest[di++] = ((d32[4] >> 16) & 0xFF); - digest[di++] = ((d32[4] >> 8) & 0xFF); - digest[di++] = ((d32[4]) & 0xFF); - return digest; - } - - protected: - void processBlock() { - uint32_t w[80]; - for (size_t i = 0; i < 16; i++) { - w[i] = (m_block[i*4 + 0] << 24); - w[i] |= (m_block[i*4 + 1] << 16); - w[i] |= (m_block[i*4 + 2] << 8); - w[i] |= (m_block[i*4 + 3]); - } - for (size_t i = 16; i < 80; i++) { - w[i] = LeftRotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1); - } - - uint32_t a = m_digest[0]; - uint32_t b = m_digest[1]; - uint32_t c = m_digest[2]; - uint32_t d = m_digest[3]; - uint32_t e = m_digest[4]; - - for (std::size_t i=0; i<80; ++i) { - uint32_t f = 0; - uint32_t k = 0; - - if (i<20) { - f = (b & c) | (~b & d); - k = 0x5A827999; - } else if (i<40) { - f = b ^ c ^ d; - k = 0x6ED9EBA1; - } else if (i<60) { - f = (b & c) | (b & d) | (c & d); - k = 0x8F1BBCDC; - } else { - f = b ^ c ^ d; - k = 0xCA62C1D6; - } - uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i]; - e = d; - d = c; - c = LeftRotate(b, 30); - b = a; - a = temp; - } - - m_digest[0] += a; - m_digest[1] += b; - m_digest[2] += c; - m_digest[3] += d; - m_digest[4] += e; - } - private: - digest32_t m_digest; - uint8_t m_block[64]; - size_t m_blockByteIndex; - size_t m_byteCount; - }; -} -#endif diff --git a/source/utils/utils.cpp b/source/utils/utils.cpp index 12c4e19..421acdd 100644 --- a/source/utils/utils.cpp +++ b/source/utils/utils.cpp @@ -1,6 +1,4 @@ #include "utils.h" -#include "../fs/FSUtils.h" -#include "TinySHA1.hpp" #include "logger.h" #include #include @@ -40,29 +38,6 @@ void Utils::dumpHex(const void *data, size_t size) { } } - -std::string Utils::calculateSHA1(const char *buffer, size_t size) { - sha1::SHA1 s; - s.processBytes(buffer, size); - uint32_t digest[5]; - s.getDigest(digest); - char tmp[48]; - snprintf(tmp, 45, "%08X%08X%08X%08X%08X", digest[0], digest[1], digest[2], digest[3], digest[4]); - return tmp; -} - -std::string Utils::hashFile(const std::string &path) { - uint8_t *data = NULL; - uint32_t size = 0; - FSUtils::LoadFileToMem(path.c_str(), &data, &size); - if (data == NULL) { - return calculateSHA1(NULL, 0); - } - std::string result = calculateSHA1(reinterpret_cast(data), size); - free(data); - return result; -} - unsigned int swap_uint32(unsigned int val) { val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); return (val << 16) | (val >> 16);