Skip to content

Commit

Permalink
Fixed linux build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
StiviiK committed Jul 27, 2019
1 parent 46b3054 commit 0d48fdc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 96 deletions.
2 changes: 1 addition & 1 deletion module/CFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int CFunctions::sign_jwt_token(lua_State* lua_vm)

// Read other arguments
const auto claims = Utils::parse_named_table(lua_vm, 2);
const auto algorithm = jwt_algorithm(reinterpret_cast<uint32_t>(lua_touserdata(lua_vm, 3)));
const auto algorithm = jwt_algorithm(reinterpret_cast<uint64_t>(lua_touserdata(lua_vm, 3)));
const auto private_key_path = lua_tostring(lua_vm, 4);
std::string private_key = private_key_path;

Expand Down
190 changes: 95 additions & 95 deletions module/Utils.h
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
#pragma once
#include <unordered_map>
#include <ostream>
#include <sstream>

#include "ILuaModuleManager.h"

static const int index_value = -1;
static const int index_key = -2;

#ifdef _DEBUG
#define DEBUG_LOG( msg ) std::cout << "[ml_jwt] " << __FILE__ << ":" << __LINE__ << ": " << msg << "\n"
#else
#define DEBUG_LOG( msg )
#endif

enum jwt_algorithm : uint32_t
{
jwt_algorithm_none = 0x123456789ABCDEFui32,
jwt_algorithm_hs256 = jwt_algorithm_none << 1,
jwt_algorithm_hs384 = jwt_algorithm_none << 2,
jwt_algorithm_hs512 = jwt_algorithm_none << 3,
jwt_algorithm_rs256 = jwt_algorithm_none << 4,
jwt_algorithm_rs384 = jwt_algorithm_none << 5,
jwt_algorithm_rs512 = jwt_algorithm_none << 6
};

class Utils
{
public:
static inline std::unordered_map<std::string, std::string> parse_named_table(lua_State* lua_vm, const int index)
{
unordered_map<std::string, std::string> result;

for(lua_pushnil(lua_vm); lua_next(lua_vm, index) != 0; lua_pop(lua_vm, 1))
{
const auto type_key = lua_type(lua_vm, index_key);
const auto type_value = lua_type(lua_vm, index_value);
if (type_key != LUA_TSTRING && type_key != LUA_TNUMBER)
{
stringstream ss;
ss << "got invalid table key, expected string or number got " << lua_typename(lua_vm, type_key);
throw runtime_error(ss.str());
}

if (type_value != LUA_TSTRING && type_value != LUA_TNUMBER)
{
stringstream ss;
ss << "got invalid table value, expected string or number got " << lua_typename(lua_vm, type_value);
throw runtime_error(ss.str());
}

auto to_string = [lua_vm](const int index) -> const std::string
{
const auto type = lua_type(lua_vm, index);
if (type == LUA_TSTRING)
{
return lua_tostring(lua_vm, index);
}

if (type == LUA_TNUMBER)
{
return std::to_string(lua_tointeger(lua_vm, index));
}
return {};
};

result[to_string(index_key)] = to_string(index_value);
}

return result;
}

static inline void get_real_path(lua_State* lua_vm, const std::string& relative_path, std::string& real_path)
{
char buf[300];
if (!pModuleManager->GetResourceFilePath(lua_vm, relative_path.c_str(), buf, sizeof(buf)))
{
stringstream ss;
ss << "invalid path: " << relative_path;
throw runtime_error(ss.str());
}

// Check if path is valid
const std::string path{ buf };
if (path.find("..") != std::string::npos)
{
stringstream ss;
ss << "path is illegal: " << relative_path;
throw runtime_error(ss.str());
}

real_path.assign(path);
}
};
#pragma once
#include <unordered_map>
#include <ostream>
#include <sstream>

#include "ILuaModuleManager.h"

static const int index_value = -1;
static const int index_key = -2;

#ifdef _DEBUG
#define DEBUG_LOG( msg ) std::cout << "[ml_jwt] " << __FILE__ << ":" << __LINE__ << ": " << msg << "\n"
#else
#define DEBUG_LOG( msg )
#endif

enum jwt_algorithm : uint64_t
{
jwt_algorithm_none = 0x123456789ABCDEF,
jwt_algorithm_hs256 = jwt_algorithm_none << 1,
jwt_algorithm_hs384 = jwt_algorithm_none << 2,
jwt_algorithm_hs512 = jwt_algorithm_none << 3,
jwt_algorithm_rs256 = jwt_algorithm_none << 4,
jwt_algorithm_rs384 = jwt_algorithm_none << 5,
jwt_algorithm_rs512 = jwt_algorithm_none << 6
};

class Utils
{
public:
static inline std::unordered_map<std::string, std::string> parse_named_table(lua_State* lua_vm, const int index)
{
unordered_map<std::string, std::string> result;

for(lua_pushnil(lua_vm); lua_next(lua_vm, index) != 0; lua_pop(lua_vm, 1))
{
const auto type_key = lua_type(lua_vm, index_key);
const auto type_value = lua_type(lua_vm, index_value);
if (type_key != LUA_TSTRING && type_key != LUA_TNUMBER)
{
stringstream ss;
ss << "got invalid table key, expected string or number got " << lua_typename(lua_vm, type_key);
throw runtime_error(ss.str());
}

if (type_value != LUA_TSTRING && type_value != LUA_TNUMBER)
{
stringstream ss;
ss << "got invalid table value, expected string or number got " << lua_typename(lua_vm, type_value);
throw runtime_error(ss.str());
}

auto to_string = [lua_vm](const int index) -> const std::string
{
const auto type = lua_type(lua_vm, index);
if (type == LUA_TSTRING)
{
return lua_tostring(lua_vm, index);
}

if (type == LUA_TNUMBER)
{
return std::to_string(lua_tointeger(lua_vm, index));
}
return {};
};

result[to_string(index_key)] = to_string(index_value);
}

return result;
}

static inline void get_real_path(lua_State* lua_vm, const std::string& relative_path, std::string& real_path)
{
char buf[300];
if (!pModuleManager->GetResourceFilePath(lua_vm, relative_path.c_str(), buf, sizeof(buf)))
{
stringstream ss;
ss << "invalid path: " << relative_path;
throw runtime_error(ss.str());
}

// Check if path is valid
const std::string path{ buf };
if (path.find("..") != std::string::npos)
{
stringstream ss;
ss << "path is illegal: " << relative_path;
throw runtime_error(ss.str());
}

real_path.assign(path);
}
};

0 comments on commit 0d48fdc

Please sign in to comment.