Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ if (WIN32)
)
target_link_libraries(SimpleGraphic
PRIVATE
"bcrypt.lib"
"winmm.lib"
)
endif ()
Expand Down
2 changes: 2 additions & 0 deletions engine/system/sys_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <filesystem>
#include <optional>
#include <span>
#include <string>

// =======
Expand Down Expand Up @@ -79,6 +80,7 @@ class sys_IMain {
virtual bool SetWorkDir(std::filesystem::path const& newCwd = {}) = 0;
virtual void SpawnProcess(std::filesystem::path cmdName, const char* argList) = 0;
virtual std::optional<std::string> OpenURL(const char* url) = 0;
virtual bool GenerateSecretBytes(std::span<std::byte> output) = 0;
virtual void Error(const char* fmt, ...) = 0;
virtual void Exit(const char* msg = NULL) = 0;
virtual void Restart() = 0;
Expand Down
1 change: 1 addition & 0 deletions engine/system/win/sys_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class sys_main_c: public sys_IMain {
bool SetWorkDir(std::filesystem::path const& newCwd = {});
void SpawnProcess(std::filesystem::path cmdName, const char* argList);
std::optional<std::string> OpenURL(const char* url); // return value has failure reason
bool GenerateSecretBytes(std::span<std::byte> output);
void Error(const char* fmt, ...);
void Exit(const char* msg = NULL);
void Restart();
Expand Down
26 changes: 25 additions & 1 deletion engine/system/win/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "core.h"

#ifdef _WIN32
#include <minwindef.h>
#include <bcrypt.h>
#include <eh.h>
#include <Shlobj.h>
#elif __linux__
Expand All @@ -30,6 +32,7 @@

#include <GLFW/glfw3.h>
#include <filesystem>
#include <fstream>
#include <map>
#include <set>
#include <thread>
Expand Down Expand Up @@ -498,7 +501,7 @@ const char* PlatformOpenURL(const char* url)
#endif
}
#else
const char* PlatformOpenURL(const char* url);
extern "C" const char* PlatformOpenURL(const char* url);
#endif

std::optional<std::string> sys_main_c::OpenURL(const char* url)
Expand All @@ -512,6 +515,27 @@ std::optional<std::string> sys_main_c::OpenURL(const char* url)
return {};
}

bool PlatformGenerateSecretBytes(char* output, size_t count)
{
#ifdef _WIN32
if (BCRYPT_SUCCESS(BCryptGenRandom(nullptr, (UCHAR*)output, (ULONG)count, BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
return true;
}
return false;
#else
std::ifstream is("/dev/urandom", std::ios::binary);
return !!is.read(output, count);
#endif
}

bool sys_main_c::GenerateSecretBytes(std::span<std::byte> output)
{
if (output.empty()) {
return true;
}
return PlatformGenerateSecretBytes((char*)output.data(), output.size());
}

// ==============================
// System Initialisation/Shutdown
// ==============================
Expand Down
19 changes: 19 additions & 0 deletions ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
** compressedGzip = Deflate(uncompressed, true)
** uncompressed = Inflate(compressed)
** uncompressedGzip = Inflate(compressedGzip)
** bytes = GenerateSecretBytes(count)
** msec = GetTime()
** path[, pathACP[, err]] = GetScriptPath()
** path[, pathACP[, err]] = GetRuntimePath()
Expand Down Expand Up @@ -1646,6 +1647,23 @@ static int l_Inflate(lua_State* L)
}
}

static int l_GenerateSecretBytes(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
int n = lua_gettop(L);
ui->LAssert(L, n >= 1, "Usage: GenerateSecretBytes(count)");
ui->LAssert(L, lua_isnumber(L, 1), "GenerateSecretBytes() argument 1: expected number, got %s", luaL_typename(L, 1));
int isInteger{};
lua_Number count = lua_tonumberx(L, 1, &isInteger);
ui->LAssert(L, isInteger == 1 && count >= 0, "GenerateSecretBytes() argument 1: expected a whole non-negative count");
std::vector<char> buf((size_t)count);
if (!ui->sys->GenerateSecretBytes(std::as_writable_bytes(std::span(buf)))) {
return 0;
}
lua_pushlstring(L, buf.data(), buf.size());
return 1;
}

static int l_GetTime(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
Expand Down Expand Up @@ -2261,6 +2279,7 @@ int ui_main_c::InitAPI(lua_State* L)
ADDFUNC(Paste);
ADDFUNC(Deflate);
ADDFUNC(Inflate);
ADDFUNC(GenerateSecretBytes);
ADDFUNC(GetTime);
ADDFUNC(GetScriptPath);
ADDFUNC(GetRuntimePath);
Expand Down
Loading