Fixed issue 5270. Don't ask me how, I just clean up code and then it works! I think it was int overflow.

This commit is contained in:
Jordan Woyak 2013-03-05 00:28:41 -06:00
parent 3ac7ee4623
commit 33a13b1a37
2 changed files with 65 additions and 45 deletions

View File

@ -15,24 +15,30 @@
// Official SVN repository and contact information can be found at // Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/ // http://code.google.com/p/dolphin-emu/
#include <algorithm>
#include <cmath>
#include "Blob.h" #include "Blob.h"
#include "CISOBlob.h" #include "CISOBlob.h"
namespace DiscIO namespace DiscIO
{ {
static const char CISO_MAGIC[] = "CISO";
CISOFileReader::CISOFileReader(std::FILE* file) CISOFileReader::CISOFileReader(std::FILE* file)
: m_file(file) : m_file(file)
{ {
m_size = m_file.GetSize(); m_size = m_file.GetSize();
memset(&header, 0, sizeof(header)); CISOHeader header;
m_file.ReadArray(&header, 1); m_file.ReadArray(&header, 1);
m_block_size = header.block_size;
CISO_Map_t count = 0; MapType count = 0;
int idx; for (u32 idx = 0; idx < CISO_MAP_SIZE; idx++)
for (idx = 0; idx < CISO_MAP_SIZE; idx++) m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID;
ciso_map[idx] = (header.map[idx] == 1) ? count++ : CISO_UNUSED_BLOCK;
} }
CISOFileReader* CISOFileReader::Create(const char* filename) CISOFileReader* CISOFileReader::Create(const char* filename)
@ -46,34 +52,45 @@ CISOFileReader* CISOFileReader::Create(const char* filename)
return NULL; return NULL;
} }
u64 CISOFileReader::GetDataSize() const
{
return GetRawSize();
}
u64 CISOFileReader::GetRawSize() const
{
return m_size;
}
bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
{ {
u64 bytesRead = 0; while (nbytes != 0)
while (bytesRead < nbytes)
{ {
u32 block = (u32)(offset / header.block_size); auto const block = offset / m_block_size;
u32 data_offset = offset % header.block_size; auto const data_offset = offset % m_block_size;
u32 bytes_to_read = (u32)min((u64)(header.block_size - data_offset), nbytes);
if ((block >= CISO_MAP_SIZE) || (ciso_map[block] == CISO_UNUSED_BLOCK)) auto const bytes_to_read = std::min(m_block_size - data_offset, nbytes);
{ if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block])
memset(out_ptr, 0, bytes_to_read);
out_ptr += bytes_to_read;
offset += bytes_to_read;
bytesRead += bytes_to_read;
}
else
{ {
// calcualte the base address // calcualte the base address
u64 file_off = CISO_HEAD_SIZE + ciso_map[block] * (u64)header.block_size + data_offset; auto const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * m_block_size + data_offset;
if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadBytes(out_ptr, bytes_to_read))) if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadArray(out_ptr, bytes_to_read)))
return false; return false;
out_ptr += bytes_to_read; out_ptr += bytes_to_read;
offset += bytes_to_read; offset += bytes_to_read;
bytesRead += bytes_to_read; nbytes -= bytes_to_read;
}
else
{
std::fill_n(out_ptr, bytes_to_read, 0);
out_ptr += bytes_to_read;
offset += bytes_to_read;
nbytes -= bytes_to_read;
} }
} }
return true; return true;
} }
@ -81,8 +98,9 @@ bool IsCISOBlob(const char* filename)
{ {
File::IOFile f(filename, "rb"); File::IOFile f(filename, "rb");
CISO_Head_t header; CISOHeader header;
return (f.ReadArray(&header, 1) && (memcmp(header.magic, CISO_MAGIC, sizeof(header.magic)) == 0)); return (f.ReadArray(&header, 1) &&
std::equal(header.magic, header.magic + sizeof(header.magic), CISO_MAGIC));
} }
} // namespace } // namespace

View File

@ -21,43 +21,45 @@
#include "Blob.h" #include "Blob.h"
#include "FileUtil.h" #include "FileUtil.h"
#define CISO_MAGIC "CISO"
#define CISO_HEAD_SIZE (0x8000)
#define CISO_MAP_SIZE (CISO_HEAD_SIZE - 8)
namespace DiscIO namespace DiscIO
{ {
bool IsCISOBlob(const char* filename); bool IsCISOBlob(const char* filename);
// Blocks that won't compress to less than 97% of the original size are stored as-is. static const u32 CISO_HEADER_SIZE = 0x8000;
struct CISO_Head_t static const u32 CISO_MAP_SIZE = CISO_HEADER_SIZE - sizeof(u32) - sizeof(char) * 4;
struct CISOHeader
{ {
u8 magic[4]; // "CISO" // "CISO"
u32 block_size; // stored as litte endian (not network byte order) char magic[4];
u8 map[CISO_MAP_SIZE]; // 0=unused, 1=used, others=invalid
// little endian
u32 block_size;
// 0=unused, 1=used, others=invalid
u8 map[CISO_MAP_SIZE];
}; };
typedef u16 CISO_Map_t;
const CISO_Map_t CISO_UNUSED_BLOCK = (CISO_Map_t)~0;
class CISOFileReader : public IBlobReader class CISOFileReader : public IBlobReader
{ {
File::IOFile m_file;
CISOFileReader(std::FILE* file);
s64 m_size;
public: public:
static CISOFileReader* Create(const char* filename); static CISOFileReader* Create(const char* filename);
u64 GetDataSize() const { return m_size; } u64 GetDataSize() const;
u64 GetRawSize() const { return m_size; } u64 GetRawSize() const;
bool Read(u64 offset, u64 nbytes, u8* out_ptr); bool Read(u64 offset, u64 nbytes, u8* out_ptr);
private: private:
CISO_Head_t header; CISOFileReader(std::FILE* file);
CISO_Map_t ciso_map[CISO_MAP_SIZE];
typedef u16 MapType;
static const MapType UNUSED_BLOCK_ID = -1;
File::IOFile m_file;
u64 m_size;
u32 m_block_size;
MapType m_ciso_map[CISO_MAP_SIZE];
}; };
} // namespace } // namespace