From 0bf0441f49ed5aab4d19db3dc6ef3ae5497ebc2e Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:51:35 +0100 Subject: [PATCH] Unify I/O library modifications --- src/blua/liolib.c | 75 ++----------------------- src/lua_baselib.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++ src/lua_script.c | 127 +----------------------------------------- 3 files changed, 143 insertions(+), 196 deletions(-) diff --git a/src/blua/liolib.c b/src/blua/liolib.c index 2d69f45cd..9742a0706 100644 --- a/src/blua/liolib.c +++ b/src/blua/liolib.c @@ -17,27 +17,14 @@ #include "lauxlib.h" #include "lualib.h" -#include "../i_system.h" -#include "../doomdef.h" -#include "../d_main.h" -#include "../m_misc.h" #define IO_INPUT 1 #define IO_OUTPUT 2 -#define FILELIMIT 1024*1024 // Size limit for reading/writing files - static const char *const fnames[] = {"input", "output"}; -static const char *whitelist[] = { // Allow scripters to write files of these types to SRB2's folder - ".txt", - ".sav2", - ".cfg", - ".png", - ".bmp" -}; static int pushresult (lua_State *L, int i, const char *filename) { @@ -159,59 +146,12 @@ static int io_tostring (lua_State *L) { return 1; } -static int StartsWith(const char *a, const char *b) // this is wolfs being lazy yet again -{ - if(strncmp(a, b, strlen(b)) == 0) return 1; - return 0; -} - - static int io_open (lua_State *L) { - FILE **pf; - const char *filename = luaL_checkstring(L, 1); - int pass = 0; - size_t i; - int length = strlen(filename); - char *splitter, *forward, *backward; - char *destFilename; - const char *mode = luaL_optstring(L, 2, "r"); - - for (i = 0; i < (sizeof (whitelist) / sizeof(const char *)); i++) - { - if (!stricmp(&filename[length - strlen(whitelist[i])], whitelist[i])) - { - pass = 1; - break; - } - } - if (strstr(filename, "..") || strchr(filename, ':') || StartsWith(filename, "\\") - || StartsWith(filename, "/") || !pass) - { - luaL_error(L,"access denied to %s", filename); - return pushresult(L,0,filename); - } - - destFilename = va("%s"PATHSEP"luafiles"PATHSEP"%s", srb2home, filename); - - // Make directories as needed - splitter = destFilename; - - forward = strchr(splitter, '/'); - backward = strchr(splitter, '\\'); - while ((splitter = (forward && backward) ? min(forward, backward) : (forward ?: backward))) - { - *splitter = 0; - I_mkdir(destFilename, 0755); - *splitter = '/'; - splitter++; - - forward = strchr(splitter, '/'); - backward = strchr(splitter, '\\'); - } - - pf = newfile(L); - *pf = fopen(destFilename, mode); - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; + const char *filename = luaL_checkstring(L, 1); + const char *mode = luaL_optstring(L, 2, "r"); + FILE **pf = newfile(L); + *pf = fopen(filename, mode); + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; } @@ -454,11 +394,6 @@ static int g_write (lua_State *L, FILE *f, int arg) { else { size_t l; const char *s = luaL_checklstring(L, arg, &l); - if (ftell(f) + l > FILELIMIT) - { - luaL_error(L,"write limit bypassed in file. Changes have been discarded."); - break; - } status = status && (fwrite(s, sizeof(char), l, f) == l); } } diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 1e60863ef..d28b2d10f 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -25,6 +25,7 @@ #include "console.h" #include "k_kart.h" // SRB2Kart #include "d_netcmd.h" // IsPlayerAdmin +#include "d_main.h" #include "lua_script.h" #include "lua_libs.h" @@ -39,6 +40,119 @@ boolean luaL_checkboolean(lua_State *L, int narg) { return lua_toboolean(L, narg); } +#define FILELIMIT 1024*1024 // Size limit for reading/writing files + +static const char *whitelist[] = { // Allow scripters to write files of these types to SRB2's folder + ".txt", + ".sav2", + ".cfg", + ".png", + ".bmp" +}; + +static int StartsWith(const char *a, const char *b) // this is wolfs being lazy yet again +{ + if(strncmp(a, b, strlen(b)) == 0) return 1; + return 0; +}; + +// Wrapper for opening files +static int lib_open(lua_State *L) +{ + const char *filename = luaL_checkstring(L, 1); + int pass = 0; + size_t i; + int length = strlen(filename); + char *splitter, *forward, *backward; + char *destFilename; + + for (i = 0; i < (sizeof (whitelist) / sizeof(const char *)); i++) + { + if (!stricmp(&filename[length - strlen(whitelist[i])], whitelist[i])) + { + pass = 1; + break; + } + } + if (strstr(filename, "..") || strchr(filename, ':') || StartsWith(filename, "\\") + || StartsWith(filename, "/") || !pass) + { + return luaL_error(L,"access denied to %s", filename); + } + + destFilename = va("%s"PATHSEP"luafiles"PATHSEP"%s", srb2home, filename); + + // Make directories as needed + splitter = destFilename; + + forward = strchr(splitter, '/'); + backward = strchr(splitter, '\\'); + while ((splitter = (forward && backward) ? min(forward, backward) : (forward ?: backward))) + { + *splitter = 0; + I_mkdir(destFilename, 0755); + *splitter = '/'; + splitter++; + + forward = strchr(splitter, '/'); + backward = strchr(splitter, '\\'); + } + + // replace the filename string + lua_pushstring(L, destFilename); + lua_replace(L, 1); + // now call the real io.open + lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, 1); + lua_call(L, lua_gettop(L)-1, LUA_MULTRET); + return lua_gettop(L); +} + +// Wrapper for writing to files +static int lib_write(lua_State *L, int arg) +{ + int top = lua_gettop(L); + boolean error = true; + size_t len, size = 0; + + if (arg != 1) { + FILE *fp = *(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); + if (fp == NULL) goto call; // real file:write will throw + size = ftell(fp); + } + + // check the size of each argument. if there's too much data, + // cut off the stack and print an error + for (; arg <= top; arg++) { + if (lua_tolstring(L, arg, &len) == NULL) goto call; + if ((size += len) > FILELIMIT) { + // oops! too many bytes! + lua_settop(L, arg-1); + goto call; + } + } + error = false; + +call: + lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, 1); + lua_call(L, lua_gettop(L)-1, LUA_MULTRET); + + if (error) + return luaL_error(L,"write limit bypassed in file. Changes have been discarded."); + return lua_gettop(L); +} + +static int lib_write_f(lua_State *L) +{ + return lib_write(L, 2); +} + +static int lib_write_io(lua_State *L) +{ + return lib_write(L, 1); +} + // String concatination static int lib_concat(lua_State *L) { @@ -3334,6 +3448,29 @@ int LUA_BaseLib(lua_State *L) lua_setfield(L,-2,"__add"); // ... store it as mathematical addition lua_pop(L, 2); // pop metatable and dummy string + // replaces an existing function with another, + // saving the original as an upvalue +#define REPLACE(name, func) \ + lua_pushliteral(L, name); \ + lua_rawget(L, -2); \ + lua_pushcclosure(L, &func, 1); \ + lua_pushliteral(L, name); \ + lua_pushvalue(L, -2); \ + lua_rawset(L, -4); \ + lua_pop(L, 1); + + // replace io.open + lua_getglobal(L, "io"); + REPLACE("open", lib_open) + REPLACE("write", lib_write_io) + lua_pop(L, 1); + + // replace file:write + luaL_getmetatable(L, LUA_FILEHANDLE); + REPLACE("write", lib_write_f) + lua_pop(L, 1); +#undef REPLACE + lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); diff --git a/src/lua_script.c b/src/lua_script.c index 62511ebff..0eeea2e4b 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -229,131 +229,7 @@ static void print_jit_status(boolean verbose) CONS_Printf("\n"); lua_settop(gL, 0); /* clear stack */ } - -// IO library modifications below. this is all hardcoded in blua/liolib.c -// hope you're ready for some coping... er... copy and pasting - -#define FILELIMIT 1024*1024 // Size limit for reading/writing files - -static const char *whitelist[] = { // Allow scripters to write files of these types to SRB2's folder - ".txt", - ".sav2", - ".cfg", - ".png", - ".bmp" -}; - -static int StartsWith(const char *a, const char *b) // this is wolfs being lazy yet again -{ - if(strncmp(a, b, strlen(b)) == 0) return 1; - return 0; -}; - -static int namechecker (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - int pass = 0; - size_t i; - int length = strlen(filename); - char *splitter, *forward, *backward; - char *destFilename; - - for (i = 0; i < (sizeof (whitelist) / sizeof(const char *)); i++) - { - if (!stricmp(&filename[length - strlen(whitelist[i])], whitelist[i])) - { - pass = 1; - break; - } - } - if (strstr(filename, "..") || strchr(filename, ':') || StartsWith(filename, "\\") - || StartsWith(filename, "/") || !pass) - { - return luaL_error(L,"access denied to %s", filename); - } - - destFilename = va("%s"PATHSEP"luafiles"PATHSEP"%s", srb2home, filename); - - // Make directories as needed - splitter = destFilename; - - forward = strchr(splitter, '/'); - backward = strchr(splitter, '\\'); - while ((splitter = (forward && backward) ? min(forward, backward) : (forward ?: backward))) - { - *splitter = 0; - I_mkdir(destFilename, 0755); - *splitter = '/'; - splitter++; - - forward = strchr(splitter, '/'); - backward = strchr(splitter, '\\'); - } - - // replace the filename string - lua_pushstring(L, destFilename); - lua_replace(L, 1); - // now call the real io.open - lua_pushvalue(L, lua_upvalueindex(1)); - lua_insert(L, 1); - lua_call(L, lua_gettop(L)-1, LUA_MULTRET); - return lua_gettop(L); -} - -static int sizechecker (lua_State *L) { - boolean error = true; - - FILE *fp = *(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); - if (fp == NULL) goto call; - - int top = lua_gettop(L); - size_t size = ftell(fp); - for (int i = 2; i <= top; i++) { - size_t len; - const char *p = lua_tolstring(L, i, &len); - if (!p) goto call; - if ((size += len) > FILELIMIT) { - // oops! too many bytes! - lua_settop(L, i-1); - goto call; - } - } - - error = false; // you're good - -call: - lua_pushvalue(L, lua_upvalueindex(1)); - lua_insert(L, 1); - lua_call(L, lua_gettop(L)-1, LUA_MULTRET); - - if (error) - return luaL_error(L,"write limit bypassed in file. Changes have been discarded."); - return lua_gettop(L); -} - -static void jit_library(lua_State *L) { -#define REPLACE(name, func) \ - lua_pushliteral(L, name); \ - lua_rawget(L, -2); \ - lua_pushcclosure(L, &func, 1); \ - lua_pushliteral(L, name); \ - lua_pushvalue(L, -2); \ - lua_rawset(L, -4); \ - lua_pop(L, 1); - - // replace io.open - lua_getglobal(L, "io"); - REPLACE("open", namechecker) - lua_pop(L, 1); - - // replace file:write - luaL_getmetatable(L, LUA_FILEHANDLE); - REPLACE("write", sizechecker) - lua_pop(L, 1); - -#undef REPLACE -} - -#endif // ifndef NOBLUAJIT +#endif // Clear and create a new Lua state, laddo! // There's SCRIPTIN to be had! @@ -400,7 +276,6 @@ void LUA_ClearState(void) #ifndef NOBLUAJIT print_jit_status(true); - jit_library(L); #endif }