2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2014-08-31 07:34:58 -06:00
|
|
|
#include <memory>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common/Common.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2014-03-14 21:38:14 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/Blob.h"
|
|
|
|
#include "DiscIO/FileHandlerARC.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
#define ARC_ID 0x55aa382d
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2009-02-27 18:49:30 -07:00
|
|
|
CARCFile::CARCFile(const std::string& _rFilename)
|
2014-03-09 14:14:26 -06:00
|
|
|
: m_pBuffer(nullptr)
|
2013-03-19 07:59:41 -06:00
|
|
|
, m_Initialized(false)
|
2009-02-27 18:49:30 -07:00
|
|
|
{
|
2014-08-31 07:34:58 -06:00
|
|
|
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(_rFilename));
|
|
|
|
if (reader != nullptr)
|
2013-03-19 07:59:41 -06:00
|
|
|
{
|
2014-08-31 07:34:58 -06:00
|
|
|
u64 FileSize = reader->GetDataSize();
|
2013-03-19 07:59:41 -06:00
|
|
|
m_pBuffer = new u8[(u32)FileSize];
|
2014-08-31 07:34:58 -06:00
|
|
|
reader->Read(0, FileSize, m_pBuffer);
|
2013-03-19 07:59:41 -06:00
|
|
|
|
|
|
|
m_Initialized = ParseBuffer();
|
|
|
|
}
|
2009-02-27 18:49:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 08:33:52 -06:00
|
|
|
CARCFile::CARCFile(const std::string& _rFilename, u32 offset)
|
2014-03-09 14:14:26 -06:00
|
|
|
: m_pBuffer(nullptr)
|
2009-06-06 08:33:52 -06:00
|
|
|
, m_Initialized(false)
|
|
|
|
{
|
2014-08-31 07:34:58 -06:00
|
|
|
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(_rFilename));
|
|
|
|
if (reader != nullptr)
|
2009-06-06 08:33:52 -06:00
|
|
|
{
|
2014-08-31 07:34:58 -06:00
|
|
|
u64 FileSize = reader->GetDataSize() - offset;
|
2009-06-06 08:33:52 -06:00
|
|
|
m_pBuffer = new u8[(u32)FileSize];
|
2014-08-31 07:34:58 -06:00
|
|
|
reader->Read(offset, FileSize, m_pBuffer);
|
2009-06-06 08:33:52 -06:00
|
|
|
|
|
|
|
m_Initialized = ParseBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
CARCFile::CARCFile(const u8* _pBuffer, size_t _BufferSize)
|
2014-03-09 14:14:26 -06:00
|
|
|
: m_pBuffer(nullptr)
|
2009-02-27 18:49:30 -07:00
|
|
|
, m_Initialized(false)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
m_pBuffer = new u8[_BufferSize];
|
|
|
|
|
|
|
|
if (m_pBuffer)
|
|
|
|
{
|
|
|
|
memcpy(m_pBuffer, _pBuffer, _BufferSize);
|
|
|
|
m_Initialized = ParseBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CARCFile::~CARCFile()
|
|
|
|
{
|
|
|
|
delete [] m_pBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
bool CARCFile::IsInitialized()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return m_Initialized;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
size_t CARCFile::GetFileSize(const std::string& _rFullPath)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
if (pFileInfo != nullptr)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 21:38:14 -06:00
|
|
|
return (size_t)pFileInfo->m_FileSize;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
size_t CARCFile::ReadFile(const std::string& _rFullPath, u8* _pBuffer, size_t _MaxBufferSize)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
if (pFileInfo == nullptr)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pFileInfo->m_FileSize > _MaxBufferSize)
|
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:50:23 -07:00
|
|
|
memcpy(_pBuffer, &m_pBuffer[pFileInfo->m_Offset], (size_t)pFileInfo->m_FileSize);
|
2014-03-14 19:27:28 -06:00
|
|
|
return (size_t) pFileInfo->m_FileSize;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
bool CARCFile::ExportFile(const std::string& _rFullPath, const std::string& _rExportFilename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!m_Initialized)
|
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);
|
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
if (pFileInfo == nullptr)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(_rExportFilename, "wb");
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
return pFile.WriteBytes(&m_pBuffer[pFileInfo->m_Offset], (size_t) pFileInfo->m_FileSize);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
bool CARCFile::ExportAllFiles(const std::string& _rFullPath)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
bool CARCFile::ParseBuffer()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
// check ID
|
|
|
|
u32 ID = Common::swap32(*(u32*)(m_pBuffer));
|
|
|
|
|
|
|
|
if (ID != ARC_ID)
|
2009-06-06 08:33:52 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// read header
|
|
|
|
u32 FSTOffset = Common::swap32(*(u32*)(m_pBuffer + 0x4));
|
|
|
|
//u32 FSTSize = Common::swap32(*(u32*)(m_pBuffer + 0x8));
|
|
|
|
//u32 FileOffset = Common::swap32(*(u32*)(m_pBuffer + 0xC));
|
|
|
|
|
|
|
|
// read all file infos
|
|
|
|
SFileInfo Root;
|
|
|
|
Root.m_NameOffset = Common::swap32(*(u32*)(m_pBuffer + FSTOffset + 0x0));
|
|
|
|
Root.m_Offset = Common::swap32(*(u32*)(m_pBuffer + FSTOffset + 0x4));
|
|
|
|
Root.m_FileSize = Common::swap32(*(u32*)(m_pBuffer + FSTOffset + 0x8));
|
|
|
|
|
|
|
|
if (Root.IsDirectory())
|
|
|
|
{
|
2009-01-23 18:50:23 -07:00
|
|
|
m_FileInfoVector.resize((unsigned int)Root.m_FileSize);
|
2008-12-07 22:30:24 -07:00
|
|
|
const char* szNameTable = (char*)(m_pBuffer + FSTOffset);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_FileInfoVector.size(); i++)
|
|
|
|
{
|
|
|
|
u8* Offset = m_pBuffer + FSTOffset + (i * 0xC);
|
|
|
|
m_FileInfoVector[i].m_NameOffset = Common::swap32(*(u32*)(Offset + 0x0));
|
|
|
|
m_FileInfoVector[i].m_Offset = Common::swap32(*(u32*)(Offset + 0x4));
|
|
|
|
m_FileInfoVector[i].m_FileSize = Common::swap32(*(u32*)(Offset + 0x8));
|
|
|
|
|
|
|
|
szNameTable += 0xC;
|
|
|
|
}
|
|
|
|
|
2014-03-14 21:38:14 -06:00
|
|
|
BuildFilenames(1, m_FileInfoVector.size(), "", szNameTable);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 21:38:14 -06:00
|
|
|
size_t CARCFile::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const std::string& _szDirectory, const char* _szNameTable)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
size_t CurrentIndex = _FirstIndex;
|
|
|
|
|
|
|
|
while (CurrentIndex < _LastIndex)
|
|
|
|
{
|
|
|
|
SFileInfo& rFileInfo = m_FileInfoVector[CurrentIndex];
|
2014-06-29 13:22:06 -06:00
|
|
|
int const uOffset = rFileInfo.m_NameOffset & 0xFFFFFF;
|
|
|
|
|
|
|
|
rFileInfo.m_FullPath = _szDirectory + &_szNameTable[uOffset];
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// check next index
|
|
|
|
if (rFileInfo.IsDirectory())
|
|
|
|
{
|
2014-06-29 13:22:06 -06:00
|
|
|
rFileInfo.m_FullPath += '/';
|
|
|
|
CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t)rFileInfo.m_FileSize, rFileInfo.m_FullPath, _szNameTable);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2014-06-29 13:22:06 -06:00
|
|
|
else
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-06-29 13:22:06 -06:00
|
|
|
++CurrentIndex;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
return CurrentIndex;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
const SFileInfo* CARCFile::FindFileInfo(const std::string& _rFullPath) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& fileInfo : m_FileInfoVector)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 21:38:14 -06:00
|
|
|
if (!strcasecmp(fileInfo.m_FullPath.c_str(), _rFullPath.c_str()))
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-14 19:27:28 -06:00
|
|
|
return &fileInfo;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:27:28 -06:00
|
|
|
return nullptr;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
} // namespace
|