diff --git a/Source/Core/DiscIO/Src/CISOBlob.cpp b/Source/Core/DiscIO/Src/CISOBlob.cpp index b3a2e31b2b..8d267c15fb 100644 --- a/Source/Core/DiscIO/Src/CISOBlob.cpp +++ b/Source/Core/DiscIO/Src/CISOBlob.cpp @@ -15,24 +15,30 @@ // Official SVN repository and contact information can be found at // http://code.google.com/p/dolphin-emu/ +#include +#include + #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) @@ -46,34 +52,45 @@ 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)) - { - memset(out_ptr, 0, bytes_to_read); - out_ptr += bytes_to_read; - offset += bytes_to_read; - bytesRead += bytes_to_read; - } - else + 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]) { // 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; out_ptr += 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; } @@ -81,8 +98,9 @@ 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 diff --git a/Source/Core/DiscIO/Src/CISOBlob.h b/Source/Core/DiscIO/Src/CISOBlob.h index e87e616a21..9b490f0638 100644 --- a/Source/Core/DiscIO/Src/CISOBlob.h +++ b/Source/Core/DiscIO/Src/CISOBlob.h @@ -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