mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-06 17:00:19 +02:00
Adds a FileFormatCheck for checking if a file format is squashfs
This will be used to ensure a squashfs file that is selected is sane
This commit is contained in:
1 parent
d77a503510
commit
86cbf06778
3 files changed
+74
-1
No files matched your search
@@ -1,8 +1,9 @@
|
||||
set(NAME Common)
|
||||
set(SRCS
|
||||
ArgumentLoader.cpp
|
||||
EnvironmentLoader.cpp
|
||||
Config.cpp
|
||||
EnvironmentLoader.cpp
|
||||
FileFormatCheck.cpp
|
||||
StringUtil.cpp)
|
||||
|
||||
add_library(${NAME} STATIC ${SRCS})
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace FEX::FormatCheck {
|
||||
bool IsSquashFS(std::string const &Filename) {
|
||||
// If it is a regular file then we need to check if it is a valid archive
|
||||
struct SquashFSHeader {
|
||||
uint32_t magic;
|
||||
uint32_t inode_count;
|
||||
uint32_t mtime;
|
||||
uint32_t block_size;
|
||||
uint32_t fragment_entry_count;
|
||||
uint16_t compression_id;
|
||||
uint16_t block_log;
|
||||
uint16_t flags;
|
||||
uint16_t id_count;
|
||||
uint16_t version_major;
|
||||
uint16_t version_minor;
|
||||
uint64_t More[8]; // More things that don't matter to us
|
||||
};
|
||||
|
||||
SquashFSHeader Header{};
|
||||
std::fstream File(Filename, std::ios::in);
|
||||
|
||||
if (!File.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!File.seekg(0, std::fstream::end)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto FileSize = File.tellg();
|
||||
if (File.fail()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FileSize <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!File.seekg(0, std::fstream::beg)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FileSize < sizeof(SquashFSHeader)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!File.read(reinterpret_cast<char*>(&Header), sizeof(SquashFSHeader))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure the cookie matches
|
||||
if (Header.magic == 0x73717368) {
|
||||
// Sanity check the version
|
||||
uint32_t version = (uint32_t)Header.version_major << 16 | Header.version_minor;
|
||||
if (version >= 0x00040000) {
|
||||
// Everything is sane, we can add it
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace FEX::FormatCheck {
|
||||
bool IsSquashFS(std::string const &Filename);
|
||||
}
|
||||
Reference in new issue
Block a user