1
0
Fork 0

RomFS: add RomFSFile and GetRomFSFile

This commit is contained in:
B3n30 2018-07-26 12:04:17 +02:00
parent 14878a17d9
commit b62978b5a1
2 changed files with 42 additions and 3 deletions

View File

@ -53,7 +53,24 @@ static bool MatchName(const u8* buffer, u32 name_length, const std::u16string& n
return name == std::u16string(name_buffer.begin(), name_buffer.end());
}
RomFSFile::RomFSFile() : data(nullptr), length(0) {}
RomFSFile::RomFSFile(const u8* data, u64 length) : data(data), length(length) {}
const u8* RomFSFile::Data() const {
return data;
}
u64 RomFSFile::Length() const {
return length;
}
const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path) {
RomFSFile file = GetFile(romfs, path);
return file.Data();
}
const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path) {
constexpr u32 INVALID_FIELD = 0xFFFFFFFF;
// Split path into directory names and file name
@ -73,7 +90,7 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
child_dir_offset = dir.first_child_dir_offset;
while (true) {
if (child_dir_offset == INVALID_FIELD) {
return nullptr;
return RomFSFile();
}
const u8* current_child_dir = romfs + header.dir_table_offset + child_dir_offset;
std::memcpy(&dir, current_child_dir, sizeof(dir));
@ -92,11 +109,12 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
const u8* current_file = romfs + header.file_table_offset + file_offset;
std::memcpy(&file, current_file, sizeof(file));
if (MatchName(current_file + sizeof(file), file.name_length, file_name)) {
return romfs + header.data_offset + file.data_offset;
RomFSFile res(romfs + header.data_offset + file.data_offset, file.data_length);
return res;
}
file_offset = file.next_file_offset;
}
return nullptr;
return RomFSFile();
}
} // namespace RomFS

View File

@ -10,6 +10,18 @@
namespace RomFS {
class RomFSFile {
public:
RomFSFile();
RomFSFile(const u8* data, u64 length);
const u8* Data() const;
u64 Length() const;
private:
const u8* data;
u64 length;
};
/**
* Gets the pointer to a file in a RomFS image.
* @param romfs The pointer to the RomFS image
@ -19,4 +31,13 @@ namespace RomFS {
*/
const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path);
/**
* Gets a RomFSFile class to a file in a RomFS image.
* @param romfs The pointer to the RomFS image
* @param path A vector containing the directory names and file name of the path to the file
* @return the RomFSFile to the file
* @todo reimplement this with a full RomFS manager
*/
const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path);
} // namespace RomFS