Compare commits

..

No commits in common. "a3a7b8847c8f42d25526319e9540a535ce2fb348" and "38a860e4cebc65922f095153d4eae81fc6431711" have entirely different histories.

View File

@ -34,16 +34,15 @@ namespace fileio
bool validate_scr_path(const std::string& fpath)
{
auto toks = utils::string::split(fpath, '/');
for (const auto& tok : toks)
if (fpath.empty())
{
if (tok == "." || tok == "..")
{
return false;
}
return true;
}
if (tok.find(":") != std::string::npos)
constexpr static std::array bad_strings { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < bad_strings.size(); i++)
{
if (fpath.find(bad_strings[i]) != std::string::npos)
{
return false;
}
@ -52,17 +51,13 @@ namespace fileio
return true;
}
std::string build_base_path(const std::string& path_)
std::string build_base_path(const std::string& path)
{
auto path = path_;
std::replace(path.begin(), path.end(), '\\', '/');
if (!validate_scr_path(path))
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, utils::string::va("Invalid path: %s", path_.c_str()));
game::Scr_Error(utils::string::va("Invalid path: %s", path.c_str()), game::SCRIPTINSTANCE_SERVER, false);
}
// its sandboxed, but what about symlinks?
return path.starts_with("scriptdata/") ? path : "scriptdata/" + path;
}
@ -107,25 +102,18 @@ namespace fileio
}
}
int scr_get_fh()
void fwrite_to_file(bool append_newline)
{
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "fs_fwrite: invalid filehandle");
game::Scr_Error("fs_fwrite: invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
return fh;
}
void fwrite_to_file(bool append_newline)
{
auto fh = scr_get_fh();
if (scr_fhs[fh].type != scr_fh_type_e::WRITE && scr_fhs[fh].type != scr_fh_type_e::APPEND)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for writing");
game::Scr_Error("File not opened for writing", game::SCRIPTINSTANCE_SERVER, false);
}
std::string to_write = game::Scr_GetString(1, game::SCRIPTINSTANCE_SERVER);
@ -182,7 +170,7 @@ namespace fileio
{
if (scr_fd.type != scr_fh_type_e::UNUSED && scr_fd.base_path == fpath)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File already opened");
game::Scr_Error("File already opened", game::SCRIPTINSTANCE_SERVER, false);
}
}
@ -198,7 +186,7 @@ namespace fileio
if (i >= max_fhs)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "Too many files opened");
game::Scr_Error("Too many files opened", game::SCRIPTINSTANCE_SERVER, false);
}
// check mode
@ -252,7 +240,7 @@ namespace fileio
}
else
{
game::Scr_ParamError(1, game::SCRIPTINSTANCE_SERVER, utils::string::va("Invalid mode: %s", mode));
game::Scr_Error(utils::string::va("Invalid mode: %s", mode), game::SCRIPTINSTANCE_SERVER, false);
}
#ifdef DEBUG
@ -273,11 +261,16 @@ namespace fileio
gsc::function::add("fs_readline", []()
{
auto fh = scr_get_fh();
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
if (scr_fhs[fh].type != scr_fh_type_e::READ)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for reading");
game::Scr_Error("File not opened for reading", game::SCRIPTINSTANCE_SERVER, false);
}
// file is completed being read
@ -326,11 +319,16 @@ namespace fileio
gsc::function::add("fs_read", []()
{
auto fh = scr_get_fh();
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
if (scr_fhs[fh].type != scr_fh_type_e::READ)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened for reading");
game::Scr_Error("File not opened for reading", game::SCRIPTINSTANCE_SERVER, false);
}
// file is completed being read
@ -347,7 +345,7 @@ namespace fileio
if (bytes_to_read <= 0)
{
game::Scr_ParamError(1, game::SCRIPTINSTANCE_SERVER, "Trying to read <1 bytes");
game::Scr_Error("Trying to read <1 bytes", game::SCRIPTINSTANCE_SERVER, false);
}
}
@ -372,11 +370,16 @@ namespace fileio
gsc::function::add("fs_fclose", []()
{
auto fh = scr_get_fh();
auto fh = game::Scr_GetInt(game::SCRIPTINSTANCE_SERVER, 0) - 1;
if (fh < 0 || fh >= max_fhs)
{
game::Scr_Error("Invalid filehandle", game::SCRIPTINSTANCE_SERVER, false);
}
if (scr_fhs[fh].type == scr_fh_type_e::UNUSED)
{
game::Scr_ParamError(0, game::SCRIPTINSTANCE_SERVER, "File not opened");
game::Scr_Error("File not opened", game::SCRIPTINSTANCE_SERVER, false);
}
free_scr_fh(scr_fhs[fh]);