summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/FileHandlerARC.cpp
blob: 96bfaf9c579c353955f11f0675bd6eb9027435ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <cstddef>
#include <cstdio>
#include <cstring>
#include <string>

#include "Common/Common.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"
#include "DiscIO/Blob.h"
#include "DiscIO/FileHandlerARC.h"
#include "DiscIO/Filesystem.h"

#define ARC_ID 0x55aa382d

namespace DiscIO
{
CARCFile::CARCFile(const std::string& _rFilename)
	: m_pBuffer(nullptr)
	, m_Initialized(false)
{
	DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rFilename.c_str());
	if (pReader != nullptr)
	{
		u64 FileSize = pReader->GetDataSize();
		m_pBuffer = new u8[(u32)FileSize];
		pReader->Read(0, FileSize, m_pBuffer);
		delete pReader;

		m_Initialized = ParseBuffer();
	}
}

CARCFile::CARCFile(const std::string& _rFilename, u32 offset)
	: m_pBuffer(nullptr)
	, m_Initialized(false)
{
	DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rFilename.c_str());
	if (pReader != nullptr)
	{
		u64 FileSize = pReader->GetDataSize() - offset;
		m_pBuffer = new u8[(u32)FileSize];
		pReader->Read(offset, FileSize, m_pBuffer);
		delete pReader;

		m_Initialized = ParseBuffer();
	}
}

CARCFile::CARCFile(const u8* _pBuffer, size_t _BufferSize)
	: m_pBuffer(nullptr)
	, m_Initialized(false)
{
	m_pBuffer = new u8[_BufferSize];

	if (m_pBuffer)
	{
		memcpy(m_pBuffer, _pBuffer, _BufferSize);
		m_Initialized = ParseBuffer();
	}
}


CARCFile::~CARCFile()
{
	delete [] m_pBuffer;
}


bool CARCFile::IsInitialized()
{
	return m_Initialized;
}


size_t CARCFile::GetFileSize(const std::string& _rFullPath)
{
	if (!m_Initialized)
	{
		return 0;
	}

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo != nullptr)
	{
		return (size_t)pFileInfo->m_FileSize;
	}

	return 0;
}


size_t CARCFile::ReadFile(const std::string& _rFullPath, u8* _pBuffer, size_t _MaxBufferSize)
{
	if (!m_Initialized)
	{
		return 0;
	}

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo == nullptr)
	{
		return 0;
	}

	if (pFileInfo->m_FileSize > _MaxBufferSize)
	{
		return 0;
	}

	memcpy(_pBuffer, &m_pBuffer[pFileInfo->m_Offset], (size_t)pFileInfo->m_FileSize);
	return (size_t) pFileInfo->m_FileSize;
}


bool CARCFile::ExportFile(const std::string& _rFullPath, const std::string& _rExportFilename)
{
	if (!m_Initialized)
	{
		return false;
	}

	const SFileInfo* pFileInfo = FindFileInfo(_rFullPath);

	if (pFileInfo == nullptr)
	{
		return false;
	}

	File::IOFile pFile(_rExportFilename, "wb");

	return pFile.WriteBytes(&m_pBuffer[pFileInfo->m_Offset], (size_t) pFileInfo->m_FileSize);
}


bool CARCFile::ExportAllFiles(const std::string& _rFullPath)
{
	return false;
}


bool CARCFile::ParseBuffer()
{
	// check ID
	u32 ID = Common::swap32(*(u32*)(m_pBuffer));

	if (ID != ARC_ID)
		return false;

	// 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())
	{
		m_FileInfoVector.resize((unsigned int)Root.m_FileSize);
		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;
		}

		BuildFilenames(1, m_FileInfoVector.size(), "", szNameTable);
	}

	return true;
}


size_t CARCFile::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, const std::string& _szDirectory, const char* _szNameTable)
{
	size_t CurrentIndex = _FirstIndex;

	while (CurrentIndex < _LastIndex)
	{
		SFileInfo& rFileInfo = m_FileInfoVector[CurrentIndex];
		int const uOffset = rFileInfo.m_NameOffset & 0xFFFFFF;

		rFileInfo.m_FullPath = _szDirectory + &_szNameTable[uOffset];

		// check next index
		if (rFileInfo.IsDirectory())
		{
			rFileInfo.m_FullPath += '/';
			CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t)rFileInfo.m_FileSize, rFileInfo.m_FullPath, _szNameTable);
		}
		else
		{
			++CurrentIndex;
		}
	}

	return CurrentIndex;
}


const SFileInfo* CARCFile::FindFileInfo(const std::string& _rFullPath) const
{
	for (auto& fileInfo : m_FileInfoVector)
	{
		if (!strcasecmp(fileInfo.m_FullPath.c_str(), _rFullPath.c_str()))
		{
			return &fileInfo;
		}
	}

	return nullptr;
}
} // namespace