CChunkFileReader::Error CChunkFileReader::LoadFileHeader(File::IOFile &pFile, SChunkHeader &header, std::string *title) { if (!pFile) { ERROR_LOG(COMMON, "ChunkReader: Can't open file for reading"); return ERROR_BAD_FILE; } const u64 fileSize = pFile.GetSize(); u64 headerSize = sizeof(SChunkHeader); if (fileSize < headerSize) { ERROR_LOG(COMMON, "ChunkReader: File too small"); return ERROR_BAD_FILE; } if (!pFile.ReadArray(&header, 1)) { ERROR_LOG(COMMON, "ChunkReader: Bad header size"); return ERROR_BAD_FILE; } if (header.Revision < REVISION_MIN) { ERROR_LOG(COMMON, "ChunkReader: Wrong file revision, got %d expected >= %d", header.Revision, REVISION_MIN); return ERROR_BAD_FILE; } if (header.Revision >= REVISION_TITLE) { char titleFixed[128]; if (!pFile.ReadArray(titleFixed, sizeof(titleFixed))) { ERROR_LOG(COMMON, "ChunkReader: Unable to read title"); return ERROR_BAD_FILE; } if (title) { *title = titleFixed; } headerSize += 128; } else if (title) { title->clear(); } u32 sz = (u32)(fileSize - headerSize); if (header.ExpectedSize != sz) { ERROR_LOG(COMMON, "ChunkReader: Bad file size, got %u expected %u", sz, header.ExpectedSize); return ERROR_BAD_FILE; } return ERROR_NONE; }
std::unique_ptr<CISOFileReader> CISOFileReader::Create(File::IOFile file) { CISOHeader header; if (file.Seek(0, SEEK_SET) && file.ReadArray(&header, 1) && header.magic == CISO_MAGIC) return std::unique_ptr<CISOFileReader>(new CISOFileReader(std::move(file))); return nullptr; }
bool IsGCZBlob(File::IOFile& file) { const u64 position = file.Tell(); if (!file.Seek(0, SEEK_SET)) return false; CompressedBlobHeader header; bool is_gcz = file.ReadArray(&header, 1) && header.magic_cookie == GCZ_MAGIC; file.Seek(position, SEEK_SET); return is_gcz; }
size_t GetNextBlock(File::IOFile& in, u8* buffer) { u64 CurrentOffset = m_BlockCount * m_BlockSize; u64 i = CurrentOffset / CLUSTER_SIZE; size_t ReadBytes = 0; if (m_isScrubbing && m_FreeTable[i]) { DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, CurrentOffset); std::fill(buffer, buffer + m_BlockSize, 0x00); in.Seek(m_BlockSize, SEEK_CUR); ReadBytes = m_BlockSize; } else { DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, CurrentOffset); in.ReadArray(buffer, m_BlockSize, &ReadBytes); } m_BlockCount++; return ReadBytes; }
std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bool flagsOnly) { File::IOFile file; file.Open(filename, "rb"); if (!file) return nullptr; FileHeader header; file.ReadBytes(&header, sizeof(header)); if (header.fileId != FILE_ID || header.min_loader_version > VERSION_NUMBER) { file.Close(); return nullptr; } auto dataFile = std::make_unique<FifoDataFile>(); dataFile->m_Flags = header.flags; dataFile->m_Version = header.file_version; if (flagsOnly) { file.Close(); return dataFile; } u32 size = std::min<u32>(BP_MEM_SIZE, header.bpMemSize); file.Seek(header.bpMemOffset, SEEK_SET); file.ReadArray(dataFile->m_BPMem, size); size = std::min<u32>(CP_MEM_SIZE, header.cpMemSize); file.Seek(header.cpMemOffset, SEEK_SET); file.ReadArray(dataFile->m_CPMem, size); size = std::min<u32>(XF_MEM_SIZE, header.xfMemSize); file.Seek(header.xfMemOffset, SEEK_SET); file.ReadArray(dataFile->m_XFMem, size); size = std::min<u32>(XF_REGS_SIZE, header.xfRegsSize); file.Seek(header.xfRegsOffset, SEEK_SET); file.ReadArray(dataFile->m_XFRegs, size); // Texture memory saving was added in version 4. std::memset(dataFile->m_TexMem, 0, TEX_MEM_SIZE); if (dataFile->m_Version >= 4) { size = std::min<u32>(TEX_MEM_SIZE, header.texMemSize); file.Seek(header.texMemOffset, SEEK_SET); file.ReadArray(dataFile->m_TexMem, size); } // Read frames for (u32 i = 0; i < header.frameCount; ++i) { u64 frameOffset = header.frameListOffset + (i * sizeof(FileFrameInfo)); file.Seek(frameOffset, SEEK_SET); FileFrameInfo srcFrame; file.ReadBytes(&srcFrame, sizeof(FileFrameInfo)); FifoFrameInfo dstFrame; dstFrame.fifoData.resize(srcFrame.fifoDataSize); dstFrame.fifoStart = srcFrame.fifoStart; dstFrame.fifoEnd = srcFrame.fifoEnd; file.Seek(srcFrame.fifoDataOffset, SEEK_SET); file.ReadBytes(dstFrame.fifoData.data(), srcFrame.fifoDataSize); ReadMemoryUpdates(srcFrame.memoryUpdatesOffset, srcFrame.numMemoryUpdates, dstFrame.memoryUpdates, file); dataFile->AddFrame(dstFrame); } file.Close(); return dataFile; }
FifoDataFile *FifoDataFile::Load(const std::string &filename, bool flagsOnly) { File::IOFile file; file.Open(filename, "rb"); if (!file) return nullptr; FileHeader header; file.ReadBytes(&header, sizeof(header)); if (header.fileId != FILE_ID || header.min_loader_version > VERSION_NUMBER) { file.Close(); return nullptr; } FifoDataFile* dataFile = new FifoDataFile; dataFile->m_Flags = header.flags; if (flagsOnly) { file.Close(); return dataFile; } u32 size = std::min((u32)BP_MEM_SIZE, header.bpMemSize); file.Seek(header.bpMemOffset, SEEK_SET); file.ReadArray(dataFile->m_BPMem, size); size = std::min((u32)CP_MEM_SIZE, header.cpMemSize); file.Seek(header.cpMemOffset, SEEK_SET); file.ReadArray(dataFile->m_CPMem, size); size = std::min((u32)XF_MEM_SIZE, header.xfMemSize); file.Seek(header.xfMemOffset, SEEK_SET); file.ReadArray(dataFile->m_XFMem, size); size = std::min((u32)XF_REGS_SIZE, header.xfRegsSize); file.Seek(header.xfRegsOffset, SEEK_SET); file.ReadArray(dataFile->m_XFRegs, size); // Read frames for (u32 i = 0; i < header.frameCount; ++i) { u64 frameOffset = header.frameListOffset + (i * sizeof(FileFrameInfo)); file.Seek(frameOffset, SEEK_SET); FileFrameInfo srcFrame; file.ReadBytes(&srcFrame, sizeof(FileFrameInfo)); FifoFrameInfo dstFrame; dstFrame.fifoData = new u8[srcFrame.fifoDataSize]; dstFrame.fifoDataSize = srcFrame.fifoDataSize; dstFrame.fifoStart = srcFrame.fifoStart; dstFrame.fifoEnd = srcFrame.fifoEnd; file.Seek(srcFrame.fifoDataOffset, SEEK_SET); file.ReadBytes(dstFrame.fifoData, srcFrame.fifoDataSize); ReadMemoryUpdates(srcFrame.memoryUpdatesOffset, srcFrame.numMemoryUpdates, dstFrame.memoryUpdates, file); dataFile->AddFrame(dstFrame); } file.Close(); return dataFile; }