1
0
Fork 0
forked from suyu/suyu

loader: Refactor to also pass filepath into IdentifyType.

This commit is contained in:
bunnei 2018-01-20 14:55:54 -05:00
parent 2dafd0d287
commit 023aef053c
8 changed files with 19 additions and 19 deletions

View file

@ -364,7 +364,7 @@ SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const
namespace Loader {
FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file, const std::string&) {
static constexpr u16 ELF_MACHINE_ARM{0x28};
u32 magic = 0;

View file

@ -22,12 +22,13 @@ public:
/**
* Returns the type of the file
* @param file FileUtil::IOFile open file
* @param filepath Path of the file that we are opening.
* @return FileType found, or FileType::Error if this loader doesn't know it
*/
static FileType IdentifyType(FileUtil::IOFile& file);
static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
FileType GetFileType() override {
return IdentifyType(file);
return IdentifyType(file, filename);
}
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;

View file

@ -21,11 +21,11 @@ const std::initializer_list<Kernel::AddressMapping> default_address_mappings = {
{0x1F000000, 0x600000, false}, // entire VRAM
};
FileType IdentifyFile(FileUtil::IOFile& file) {
FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) {
FileType type;
#define CHECK_TYPE(loader) \
type = AppLoader_##loader::IdentifyType(file); \
type = AppLoader_##loader::IdentifyType(file, filepath); \
if (FileType::Error != type) \
return type;
@ -45,13 +45,13 @@ FileType IdentifyFile(const std::string& file_name) {
return FileType::Unknown;
}
return IdentifyFile(file);
return IdentifyFile(file, file_name);
}
FileType GuessFromExtension(const std::string& extension_) {
std::string extension = Common::ToLower(extension_);
if (extension == ".elf" || extension == ".axf")
if (extension == ".elf")
return FileType::ELF;
else if (extension == ".nro")
return FileType::NRO;
@ -117,7 +117,7 @@ std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
std::string filename_filename, filename_extension;
Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension);
FileType type = IdentifyFile(file);
FileType type = IdentifyFile(file, filename);
FileType filename_type = GuessFromExtension(filename_extension);
if (type != filename_type) {

View file

@ -37,9 +37,10 @@ enum class FileType {
/**
* Identifies the type of a bootable file based on the magic value in its header.
* @param file open file
* @param filepath Path of the file that we are opening.
* @return FileType of file
*/
FileType IdentifyFile(FileUtil::IOFile& file);
FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath);
/**
* Identifies the type of a bootable file based on the magic value in its header.

View file

@ -45,7 +45,7 @@ struct ModHeader {
};
static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file) {
FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file, const std::string&) {
// Read NSO header
NroHeader nro_header{};
file.Seek(0, SEEK_SET);

View file

@ -4,7 +4,6 @@
#pragma once
#include <map>
#include <string>
#include "common/common_types.h"
#include "common/file_util.h"
@ -23,12 +22,13 @@ public:
/**
* Returns the type of the file
* @param file FileUtil::IOFile open file
* @param filepath Path of the file that we are opening.
* @return FileType found, or FileType::Error if this loader doesn't know it
*/
static FileType IdentifyType(FileUtil::IOFile& file);
static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
FileType GetFileType() override {
return IdentifyType(file);
return IdentifyType(file, filepath);
}
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;

View file

@ -4,7 +4,6 @@
#include <vector>
#include <lz4.h>
#include "common/common_funcs.h"
#include "common/logging/log.h"
#include "common/swap.h"
@ -47,7 +46,7 @@ struct ModHeader {
};
static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) {
FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file, const std::string&) {
u32 magic = 0;
file.Seek(0, SEEK_SET);
if (1 != file.ReadArray<u32>(&magic, 1)) {

View file

@ -4,7 +4,6 @@
#pragma once
#include <map>
#include <string>
#include "common/common_types.h"
#include "common/file_util.h"
@ -23,12 +22,13 @@ public:
/**
* Returns the type of the file
* @param file FileUtil::IOFile open file
* @param filepath Path of the file that we are opening.
* @return FileType found, or FileType::Error if this loader doesn't know it
*/
static FileType IdentifyType(FileUtil::IOFile& file);
static FileType IdentifyType(FileUtil::IOFile& file, const std::string& filepath);
FileType GetFileType() override {
return IdentifyType(file);
return IdentifyType(file, filepath);
}
static VAddr LoadModule(const std::string& path, VAddr load_base);
@ -36,7 +36,6 @@ public:
ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
private:
std::string filepath;
};