diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 1366966a..9a1de9b4 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -26,6 +26,38 @@ function __vc_Canvas_set_data(self, data) self:_set_data(tostring(_ffi.cast("uintptr_t", canvas_ffi_buffer))) end +_ffi.cdef([[ + unsigned long crc32(unsigned long crc, const char *buf, unsigned len); +]]) + +local zlib +if _ffi.os == "Windows" then + zlib = _ffi.load("zlib1") +elseif _ffi.os == "OSX" then + zlib = _ffi.load("z") +elseif _ffi.os == "Linux" then + zlib = _ffi.load("libz.so.1") +else + error("platform does not support zlib " .. _ffi.os) +end + +function crc32(bytes, chksum) + local chksum = chksum or 0 + + local length = #bytes + if type(bytes) == "string" then + return zlib.crc32(chksum, bytes, length) + elseif type(bytes) == "table" then + local buffer = _ffi.new( + string.format("char[%s]", length) + ) + for i=1, length do + buffer[i - 1] = bytes[i] + end + return zlib.crc32(chksum, buffer, length) + end +end + -- Check if given table is an array function is_array(x) if #x > 0 then diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index cfe79f3c..ba9a9f86 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -30,7 +30,7 @@ static int l_resolve(lua::State* L) { static int l_read(lua::State* L) { io::path path = lua::require_string(L, 1); if (io::is_regular_file(path)) { - return lua::pushstring(L, io::read_string(path)); + return lua::pushlstring(L, io::read_string(path)); } throw std::runtime_error( "file does not exists " + util::quote(path.string()) diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 656bd504..ab45edb1 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -184,6 +184,11 @@ namespace lua { return 1; } + inline int pushlstring(lua::State* L, std::string_view view) { + lua_pushlstring(L, reinterpret_cast(view.data()), view.size()); + return 1; + } + template inline int pushfstring(lua_State* L, const char* fmt, Args... args) { lua_pushfstring(L, fmt, args...);