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
This commit is contained in:
Maarten ter Huurne 2008-09-18 08:29:10 +00:00
parent 1a7f5bb564
commit 8ce6b5aceb
2 changed files with 43 additions and 42 deletions

View File

@ -22,10 +22,13 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif
namespace DiscIO namespace DiscIO
{ {
#ifdef _WIN32
PlainFileReader::PlainFileReader(HANDLE hFile_) PlainFileReader::PlainFileReader(HANDLE hFile_)
{ {
hFile = hFile_; hFile = hFile_;
@ -50,27 +53,22 @@ PlainFileReader::~PlainFileReader()
CloseHandle(hFile); 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); LONG offset_high = (LONG)(offset >> 32);
SetFilePointer(hFile, (DWORD)(offset & 0xFFFFFFFF), &offset_high, FILE_BEGIN); SetFilePointer(hFile, (DWORD)(offset & 0xFFFFFFFF), &offset_high, FILE_BEGIN);
if (size >= 0x100000000ULL) if (nbytes >= 0x100000000ULL)
return false; // WTF, does windows really have this limitation? return false; // WTF, does windows really have this limitation?
DWORD unused; DWORD unused;
if (!ReadFile(hFile, out_ptr, DWORD(size & 0xFFFFFFFF), &unused, NULL)) if (!ReadFile(hFile, out_ptr, DWORD(nbytes & 0xFFFFFFFF), &unused, NULL))
return false; return false;
else else
return true; return true;
} }
} // namespace #else // POSIX
#else // linux, 64-bit. We do not yet care about linux32
namespace DiscIO
{
PlainFileReader::PlainFileReader(FILE* file__) PlainFileReader::PlainFileReader(FILE* file__)
{ {
@ -88,9 +86,8 @@ namespace DiscIO
{ {
FILE* file_ = fopen(filename, "rb"); FILE* file_ = fopen(filename, "rb");
if (file_) if (file_)
{
return new PlainFileReader(file_); return new PlainFileReader(file_);
} else
return 0; return 0;
} }
@ -101,11 +98,13 @@ namespace DiscIO
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{ {
fseek(file_, offset, SEEK_SET); int seekStatus = fseek(file_, offset, SEEK_SET);
fread(out_ptr, nbytes, 1, file_); if (seekStatus != 0)
return true; return false;
size_t bytesRead = fread(out_ptr, 1, nbytes, file_);
return bytesRead == nbytes;
} }
} // namespace
#endif #endif
} // namespace

View File

@ -22,6 +22,8 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#else
#include <cstdio>
#endif #endif
namespace DiscIO namespace DiscIO
@ -43,7 +45,7 @@ public:
~PlainFileReader(); ~PlainFileReader();
u64 GetDataSize() const { return size; } u64 GetDataSize() const { return size; }
u64 GetRawSize() 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 } // namespace