Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed issue 5270. Don't ask me how, I just clean up code and then it …
…works! I think it was int overflow.
  • Loading branch information
jordan-woyak committed Mar 5, 2013
1 parent 3ac7ee4 commit 33a13b1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
64 changes: 41 additions & 23 deletions Source/Core/DiscIO/Src/CISOBlob.cpp
Expand Up @@ -15,24 +15,30 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include <algorithm>
#include <cmath>

#include "Blob.h"
#include "CISOBlob.h"

namespace DiscIO
{

static const char CISO_MAGIC[] = "CISO";

CISOFileReader::CISOFileReader(std::FILE* file)
: m_file(file)
{
m_size = m_file.GetSize();

memset(&header, 0, sizeof(header));
CISOHeader header;
m_file.ReadArray(&header, 1);

m_block_size = header.block_size;

CISO_Map_t count = 0;
int idx;
for (idx = 0; idx < CISO_MAP_SIZE; idx++)
ciso_map[idx] = (header.map[idx] == 1) ? count++ : CISO_UNUSED_BLOCK;
MapType count = 0;
for (u32 idx = 0; idx < CISO_MAP_SIZE; idx++)
m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID;
}

CISOFileReader* CISOFileReader::Create(const char* filename)
Expand All @@ -46,43 +52,55 @@ CISOFileReader* CISOFileReader::Create(const char* filename)
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)
{
u64 bytesRead = 0;
while (bytesRead < nbytes)
while (nbytes != 0)
{
u32 block = (u32)(offset / header.block_size);
u32 data_offset = offset % header.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 block = offset / m_block_size;
auto const data_offset = offset % m_block_size;

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);
// calcualte the base address
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.ReadArray(out_ptr, bytes_to_read)))
return false;

out_ptr += bytes_to_read;
offset += bytes_to_read;
bytesRead += bytes_to_read;
nbytes -= bytes_to_read;
}
else
{
// calcualte the base address
u64 file_off = CISO_HEAD_SIZE + ciso_map[block] * (u64)header.block_size + data_offset;

if (!(m_file.Seek(file_off, SEEK_SET) && m_file.ReadBytes(out_ptr, bytes_to_read)))
return false;

std::fill_n(out_ptr, bytes_to_read, 0);
out_ptr += bytes_to_read;
offset += bytes_to_read;
bytesRead += bytes_to_read;
nbytes -= bytes_to_read;
}
}

return true;
}

bool IsCISOBlob(const char* filename)
{
File::IOFile f(filename, "rb");

CISO_Head_t header;
return (f.ReadArray(&header, 1) && (memcmp(header.magic, CISO_MAGIC, sizeof(header.magic)) == 0));
CISOHeader header;
return (f.ReadArray(&header, 1) &&
std::equal(header.magic, header.magic + sizeof(header.magic), CISO_MAGIC));
}

} // namespace
44 changes: 23 additions & 21 deletions Source/Core/DiscIO/Src/CISOBlob.h
Expand Up @@ -21,43 +21,45 @@
#include "Blob.h"
#include "FileUtil.h"

#define CISO_MAGIC "CISO"
#define CISO_HEAD_SIZE (0x8000)
#define CISO_MAP_SIZE (CISO_HEAD_SIZE - 8)

namespace DiscIO
{

bool IsCISOBlob(const char* filename);

// Blocks that won't compress to less than 97% of the original size are stored as-is.
struct CISO_Head_t
static const u32 CISO_HEADER_SIZE = 0x8000;
static const u32 CISO_MAP_SIZE = CISO_HEADER_SIZE - sizeof(u32) - sizeof(char) * 4;

struct CISOHeader
{
u8 magic[4]; // "CISO"
u32 block_size; // stored as litte endian (not network byte order)
u8 map[CISO_MAP_SIZE]; // 0=unused, 1=used, others=invalid
// "CISO"
char magic[4];

// 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
{
File::IOFile m_file;
CISOFileReader(std::FILE* file);
s64 m_size;

public:
static CISOFileReader* Create(const char* filename);

u64 GetDataSize() const { return m_size; }
u64 GetRawSize() const { return m_size; }
u64 GetDataSize() const;
u64 GetRawSize() const;
bool Read(u64 offset, u64 nbytes, u8* out_ptr);

private:
CISO_Head_t header;
CISO_Map_t ciso_map[CISO_MAP_SIZE];
CISOFileReader(std::FILE* file);

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
Expand Down

0 comments on commit 33a13b1

Please sign in to comment.