Commit d3f85250 authored by Libretro-Admin's avatar Libretro-Admin
Browse files

Cleanup more of VirtualFS/NativeVFS

parent bbc06771
Pipeline #44242 passed with stage
in 40 seconds
......@@ -175,114 +175,5 @@ void NativeVFS::readdirentries(const std::string& path, std::function<bool(const
#endif
}
// Really dumb, maybe we should use boost?
bool NativeVFS::is_absolute_path(const std::string& path)
{
if(!path.size())
return false;
if(is_path_separator(path[0]))
return true;
#if defined(WIN32) || defined(DOS)
if(path.size() >= 2 && path[1] == ':')
{
if((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z'))
return true;
}
#endif
return false;
}
void NativeVFS::check_firop_safe(const std::string& path)
{
//
// First, check for any 8-bit characters, and print a warning about portability.
//
for(size_t x = 0; x < path.size(); x++)
{
if(path[x] & 0x80)
{
MDFN_printf(_("WARNING: Referenced path \"%s\" contains at least one 8-bit non-ASCII character; this may cause portability issues.\n"), path.c_str());
break;
}
}
if(!MDFN_GetSettingB("filesys.untrusted_fip_check"))
return;
// We could make this more OS-specific, but it shouldn't hurt to try to weed out usage of characters that are path
// separators in one OS but not in another, and we'd also run more of a risk of missing a special path separator case
// in some OS.
std::string unsafe_reason;
#ifdef WIN32
if(!UTF8_validate(path, true))
unsafe_reason += _("Invalid UTF-8. ");
#endif
if(path.find('\0') != std::string::npos)
unsafe_reason += _("Contains null(0). ");
if(path.find(':') != std::string::npos)
unsafe_reason += _("Contains colon. ");
if(path.find('\\') != std::string::npos)
unsafe_reason += _("Contains backslash. ");
if(path.find('/') != std::string::npos)
unsafe_reason += _("Contains forward slash. ");
if(path == "..")
unsafe_reason += _("Is parent directory. ");
#if defined(DOS) || defined(WIN32)
//
// http://support.microsoft.com/kb/74496
// http://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html
//
{
static const char* dev_names[] =
{
"CON", "PRN", "AUX", "CLOCK$", "NUL", "CONIN$", "CONOUT$",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM¹", "COM²", "COM³",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "LPT¹", "LPT²", "LPT³",
NULL
};
//
const char* pcs = path.c_str();
for(const char** ls = dev_names; *ls != NULL; ls++)
{
size_t lssl = strlen(*ls);
if(!MDFN_strazicmp(*ls, pcs, lssl))
{
if(pcs[lssl] == 0 || pcs[lssl] == ':' || pcs[lssl] == '.' || pcs[lssl] == ' ')
{
unsafe_reason += _("Is (likely) a reserved device name. ");
break;
}
}
}
}
#endif
if(unsafe_reason.size() > 0)
throw MDFN_Error(0, _("Referenced path \"%s\" (escaped) is potentially unsafe. %s Refer to the documentation about the \"filesys.untrusted_fip_check\" setting.\n"), MDFN_strescape(path).c_str(), unsafe_reason.c_str());
}
void NativeVFS::get_file_path_components(const std::string &file_path, std::string* dir_path_out, std::string* file_base_out, std::string *file_ext_out)
{
#if defined(WIN32) || defined(DOS)
if(file_path.size() >= 3 && ((file_path[0] >= 'a' && file_path[0] <= 'z') || (file_path[0] >= 'A' && file_path[0] <= 'Z')) && file_path[1] == ':' && file_path.find_last_of(allowed_path_separators) == std::string::npos)
{
VirtualFS::get_file_path_components(file_path.substr(0, 2) + '.' + preferred_path_separator + file_path.substr(2), dir_path_out, file_base_out, file_ext_out);
return;
}
#endif
VirtualFS::get_file_path_components(file_path, dir_path_out, file_base_out, file_ext_out);
}
}
......@@ -35,9 +35,6 @@ class NativeVFS final : public VirtualFS
virtual Stream* open(const std::string& path, const uint32 mode, const bool throw_on_noent = true, const CanaryType canary = CanaryType::open) override;
virtual void readdirentries(const std::string& path, std::function<bool(const std::string&)> callb) override;
virtual bool is_absolute_path(const std::string& path) override;
virtual void get_file_path_components(const std::string& file_path, std::string* dir_path_out, std::string* file_base_out = nullptr, std::string *file_ext_out = nullptr) override;
virtual void check_firop_safe(const std::string& path) override;
};
}
......
......@@ -37,75 +37,5 @@ VirtualFS::~VirtualFS()
}
void VirtualFS::get_file_path_components(const std::string &file_path, std::string* dir_path_out, std::string* file_base_out, std::string *file_ext_out)
{
const size_t final_ds = file_path.find_last_of(allowed_path_separators); // in file_path
std::string file_name;
size_t fn_final_dot; // in local var file_name
// Temporary output:
std::string dir_path, file_base, file_ext;
if(final_ds == std::string::npos)
{
dir_path = ".";
file_name = file_path;
}
else
{
dir_path = file_path.substr(0, final_ds);
file_name = file_path.substr(final_ds + 1);
}
fn_final_dot = file_name.find_last_of('.');
if(fn_final_dot != std::string::npos)
{
file_base = file_name.substr(0, fn_final_dot);
file_ext = file_name.substr(fn_final_dot);
}
else
{
file_base = file_name;
file_ext = "";
}
// Write outputs at end, in case file_path references the same std::string as pointed to
// by one of the outputs.
if(dir_path_out)
*dir_path_out = dir_path;
if(file_base_out)
*file_base_out = file_base;
if(file_ext_out)
*file_ext_out = file_ext;
}
void VirtualFS::check_firop_safe(const std::string& path)
{
}
bool VirtualFS::is_path_separator(const char c)
{
return allowed_path_separators.find(c) != std::string::npos;
}
bool VirtualFS::is_absolute_path(const std::string& path)
{
return true;
}
std::string VirtualFS::eval_fip(const std::string& dir_path, const std::string& rel_path, bool skip_safety_check)
{
if(!skip_safety_check)
check_firop_safe(rel_path);
if(is_absolute_path(rel_path))
return rel_path;
else
return dir_path + preferred_path_separator + rel_path;
}
}
......@@ -85,20 +85,6 @@ class VirtualFS
//
//
//
virtual bool is_absolute_path(const std::string& path);
virtual bool is_path_separator(const char c);
//
// Note: It IS permissible for an output to point to the same string as the file_path reference.
//
virtual void get_file_path_components(const std::string& file_path, std::string* dir_path_out, std::string* file_base_out = nullptr, std::string *file_ext_out = nullptr);
// File-inclusion for-read-only path, for PSF and CUE/TOC sheet usage.
// throws exception if not safe(and/or on other error).
virtual void check_firop_safe(const std::string& path);
virtual std::string eval_fip(const std::string& dir_path, const std::string& rel_path, bool skip_safety_check = false);
INLINE char get_preferred_path_separator(void) { return preferred_path_separator; }
protected:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment