2017-04-19 19:54:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <Wincrypt.h>
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
template<typename T, void freefunc(T)> class CustomHandle {
|
|
|
|
T handle;
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline CustomHandle() : handle(0) {}
|
|
|
|
inline CustomHandle(T in) : handle(in) {}
|
|
|
|
inline ~CustomHandle()
|
|
|
|
{
|
|
|
|
if (handle)
|
|
|
|
freefunc(handle);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline T *operator&() { return &handle; }
|
|
|
|
inline operator T() const { return handle; }
|
|
|
|
inline T get() const { return handle; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
inline CustomHandle<T, freefunc> &operator=(T in)
|
|
|
|
{
|
|
|
|
if (handle)
|
|
|
|
freefunc(handle);
|
|
|
|
handle = in;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline bool operator!() const { return !handle; }
|
2017-04-19 19:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void FreeProvider(HCRYPTPROV prov);
|
|
|
|
void FreeHash(HCRYPTHASH hash);
|
|
|
|
void FreeKey(HCRYPTKEY key);
|
|
|
|
|
|
|
|
using CryptProvider = CustomHandle<HCRYPTPROV, FreeProvider>;
|
2019-09-22 21:19:10 +00:00
|
|
|
using CryptHash = CustomHandle<HCRYPTHASH, FreeHash>;
|
|
|
|
using CryptKey = CustomHandle<HCRYPTKEY, FreeKey>;
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
template<typename T> class LocalPtr {
|
|
|
|
T *ptr = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline ~LocalPtr()
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
LocalFree(ptr);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline T **operator&() { return &ptr; }
|
|
|
|
inline operator T() const { return ptr; }
|
|
|
|
inline T *get() const { return ptr; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline bool operator!() const { return !ptr; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline T *operator->() { return ptr; }
|
2017-04-19 19:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
class Json {
|
|
|
|
json_t *json;
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline Json() : json(nullptr) {}
|
|
|
|
explicit inline Json(json_t *json_) : json(json_) {}
|
|
|
|
inline Json(const Json &from) : json(json_incref(from.json)) {}
|
2019-09-22 21:19:10 +00:00
|
|
|
inline Json(Json &&from) : json(from.json) { from.json = nullptr; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline ~Json()
|
|
|
|
{
|
2017-04-19 19:54:15 +00:00
|
|
|
if (json)
|
|
|
|
json_decref(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Json &operator=(json_t *json_)
|
|
|
|
{
|
|
|
|
if (json)
|
|
|
|
json_decref(json);
|
|
|
|
json = json_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline Json &operator=(const Json &from)
|
|
|
|
{
|
|
|
|
if (json)
|
|
|
|
json_decref(json);
|
|
|
|
json = json_incref(from.json);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline Json &operator=(Json &&from)
|
|
|
|
{
|
|
|
|
if (json)
|
|
|
|
json_decref(json);
|
|
|
|
json = from.json;
|
|
|
|
from.json = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline operator json_t *() const { return json; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline bool operator!() const { return !json; }
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
inline const char *GetString(const char *name,
|
2019-09-22 21:19:10 +00:00
|
|
|
const char *def = nullptr) const
|
2017-04-19 19:54:15 +00:00
|
|
|
{
|
|
|
|
json_t *obj(json_object_get(json, name));
|
|
|
|
if (!obj)
|
|
|
|
return def;
|
|
|
|
return json_string_value(obj);
|
|
|
|
}
|
|
|
|
inline int64_t GetInt(const char *name, int def = 0) const
|
|
|
|
{
|
|
|
|
json_t *obj(json_object_get(json, name));
|
|
|
|
if (!obj)
|
|
|
|
return def;
|
|
|
|
return json_integer_value(obj);
|
|
|
|
}
|
|
|
|
inline json_t *GetObject(const char *name) const
|
|
|
|
{
|
|
|
|
return json_object_get(json, name);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline json_t *get() const { return json; }
|
2017-04-19 19:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
std::string vstrprintf(const char *format, va_list args);
|
|
|
|
std::string strprintf(const char *format, ...);
|