From 8ce6b5aceb726f2e3c1b4e4da153fda4d6ee858a Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 18 Sep 2008 08:29:10 +0000 Subject: [PATCH] Reinstated the changes from rev 568. Also fixed the actual bug: fread() was told to read 1 n-byte record, while it should be told to read n 1-byte records, so we know exactly how much it actually read. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@573 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DiscIO/Src/FileBlob.cpp | 77 ++++++++++++++--------------- Source/Core/DiscIO/Src/FileBlob.h | 8 +-- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/Source/Core/DiscIO/Src/FileBlob.cpp b/Source/Core/DiscIO/Src/FileBlob.cpp index 070b0fa292..0d14d27a29 100644 --- a/Source/Core/DiscIO/Src/FileBlob.cpp +++ b/Source/Core/DiscIO/Src/FileBlob.cpp @@ -22,10 +22,13 @@ #ifdef _WIN32 #include +#endif namespace DiscIO { +#ifdef _WIN32 + PlainFileReader::PlainFileReader(HANDLE hFile_) { hFile = hFile_; @@ -50,62 +53,58 @@ PlainFileReader::~PlainFileReader() CloseHandle(hFile); } -bool PlainFileReader::Read(u64 offset, u64 size, u8* out_ptr) +bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) { LONG offset_high = (LONG)(offset >> 32); SetFilePointer(hFile, (DWORD)(offset & 0xFFFFFFFF), &offset_high, FILE_BEGIN); - if (size >= 0x100000000ULL) + if (nbytes >= 0x100000000ULL) return false; // WTF, does windows really have this limitation? DWORD unused; - if (!ReadFile(hFile, out_ptr, DWORD(size & 0xFFFFFFFF), &unused, NULL)) + if (!ReadFile(hFile, out_ptr, DWORD(nbytes & 0xFFFFFFFF), &unused, NULL)) return false; else return true; } -} // namespace +#else // POSIX -#else // linux, 64-bit. We do not yet care about linux32 - -namespace DiscIO +PlainFileReader::PlainFileReader(FILE* file__) { + file_ = file__; + #if 0 + fseek64(file_, 0, SEEK_END); + #else + fseek(file_, 0, SEEK_END); // I don't have fseek64 with gcc 4.3 + #endif + size = ftell(file_); + fseek(file_, 0, SEEK_SET); +} - PlainFileReader::PlainFileReader(FILE* file__) - { - file_ = file__; - #if 0 - fseek64(file_, 0, SEEK_END); - #else - fseek(file_, 0, SEEK_END); // I don't have fseek64 with gcc 4.3 - #endif - size = ftell(file_); - fseek(file_, 0, SEEK_SET); - } - - PlainFileReader* PlainFileReader::Create(const char* filename) - { - FILE* file_ = fopen(filename, "rb"); - if (file_) - { - return new PlainFileReader(file_); - } +PlainFileReader* PlainFileReader::Create(const char* filename) +{ + FILE* file_ = fopen(filename, "rb"); + if (file_) + return new PlainFileReader(file_); + else return 0; - } +} - PlainFileReader::~PlainFileReader() - { - fclose(file_); - } +PlainFileReader::~PlainFileReader() +{ + fclose(file_); +} - bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) - { - fseek(file_, offset, SEEK_SET); - fread(out_ptr, nbytes, 1, file_); - return true; - } - -} // namespace +bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) +{ + int seekStatus = fseek(file_, offset, SEEK_SET); + if (seekStatus != 0) + return false; + size_t bytesRead = fread(out_ptr, 1, nbytes, file_); + return bytesRead == nbytes; +} #endif + +} // namespace diff --git a/Source/Core/DiscIO/Src/FileBlob.h b/Source/Core/DiscIO/Src/FileBlob.h index 3e47ffe76c..f704608ba1 100644 --- a/Source/Core/DiscIO/Src/FileBlob.h +++ b/Source/Core/DiscIO/Src/FileBlob.h @@ -22,6 +22,8 @@ #ifdef _WIN32 #include +#else +#include #endif namespace DiscIO @@ -33,8 +35,8 @@ class PlainFileReader : public IBlobReader HANDLE hFile; PlainFileReader(HANDLE hFile_); #else - FILE *file_; - PlainFileReader(FILE *file__); + FILE* file_; + PlainFileReader(FILE* file__); #endif s64 size; @@ -43,7 +45,7 @@ public: ~PlainFileReader(); u64 GetDataSize() const { return size; } u64 GetRawSize() const { return size; } - bool Read(u64 offset, u64 size, u8* out_ptr); + bool Read(u64 offset, u64 nbytes, u8* out_ptr); }; } // namespace