win-update-helpers.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <Wincrypt.h>
  5. #include <jansson.h>
  6. #include <cstdint>
  7. #include <string>
  8. /* ------------------------------------------------------------------------ */
  9. template<typename T, void freefunc(T)> class CustomHandle {
  10. T handle;
  11. public:
  12. inline CustomHandle() : handle(0) {}
  13. inline CustomHandle(T in) : handle(in) {}
  14. inline ~CustomHandle()
  15. {
  16. if (handle)
  17. freefunc(handle);
  18. }
  19. inline T *operator&() { return &handle; }
  20. inline operator T() const { return handle; }
  21. inline T get() const { return handle; }
  22. inline CustomHandle<T, freefunc> &operator=(T in)
  23. {
  24. if (handle)
  25. freefunc(handle);
  26. handle = in;
  27. return *this;
  28. }
  29. inline bool operator!() const { return !handle; }
  30. };
  31. void FreeProvider(HCRYPTPROV prov);
  32. void FreeHash(HCRYPTHASH hash);
  33. void FreeKey(HCRYPTKEY key);
  34. using CryptProvider = CustomHandle<HCRYPTPROV, FreeProvider>;
  35. using CryptHash = CustomHandle<HCRYPTHASH, FreeHash>;
  36. using CryptKey = CustomHandle<HCRYPTKEY, FreeKey>;
  37. /* ------------------------------------------------------------------------ */
  38. template<typename T> class LocalPtr {
  39. T *ptr = nullptr;
  40. public:
  41. inline ~LocalPtr()
  42. {
  43. if (ptr)
  44. LocalFree(ptr);
  45. }
  46. inline T **operator&() { return &ptr; }
  47. inline operator T() const { return ptr; }
  48. inline T *get() const { return ptr; }
  49. inline bool operator!() const { return !ptr; }
  50. inline T *operator->() { return ptr; }
  51. };
  52. /* ------------------------------------------------------------------------ */
  53. class Json {
  54. json_t *json;
  55. public:
  56. inline Json() : json(nullptr) {}
  57. explicit inline Json(json_t *json_) : json(json_) {}
  58. inline Json(const Json &from) : json(json_incref(from.json)) {}
  59. inline Json(Json &&from) : json(from.json) { from.json = nullptr; }
  60. inline ~Json()
  61. {
  62. if (json)
  63. json_decref(json);
  64. }
  65. inline Json &operator=(json_t *json_)
  66. {
  67. if (json)
  68. json_decref(json);
  69. json = json_;
  70. return *this;
  71. }
  72. inline Json &operator=(const Json &from)
  73. {
  74. if (json)
  75. json_decref(json);
  76. json = json_incref(from.json);
  77. return *this;
  78. }
  79. inline Json &operator=(Json &&from)
  80. {
  81. if (json)
  82. json_decref(json);
  83. json = from.json;
  84. from.json = nullptr;
  85. return *this;
  86. }
  87. inline operator json_t *() const { return json; }
  88. inline bool operator!() const { return !json; }
  89. inline const char *GetString(const char *name,
  90. const char *def = nullptr) const
  91. {
  92. json_t *obj(json_object_get(json, name));
  93. if (!obj)
  94. return def;
  95. return json_string_value(obj);
  96. }
  97. inline int64_t GetInt(const char *name, int def = 0) const
  98. {
  99. json_t *obj(json_object_get(json, name));
  100. if (!obj)
  101. return def;
  102. return json_integer_value(obj);
  103. }
  104. inline json_t *GetObject(const char *name) const
  105. {
  106. return json_object_get(json, name);
  107. }
  108. inline json_t *get() const { return json; }
  109. };
  110. /* ------------------------------------------------------------------------ */
  111. std::string vstrprintf(const char *format, va_list args);
  112. std::string strprintf(const char *format, ...);