win-update-helpers.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (json)
  62. json_decref(json);
  63. }
  64. inline Json &operator=(json_t *json_)
  65. {
  66. if (json)
  67. json_decref(json);
  68. json = json_;
  69. return *this;
  70. }
  71. inline Json &operator=(const Json &from)
  72. {
  73. if (json)
  74. json_decref(json);
  75. json = json_incref(from.json);
  76. return *this;
  77. }
  78. inline Json &operator=(Json &&from)
  79. {
  80. if (json)
  81. json_decref(json);
  82. json = from.json;
  83. from.json = nullptr;
  84. return *this;
  85. }
  86. inline operator json_t *() const {return json;}
  87. inline bool operator!() const {return !json;}
  88. inline const char *GetString(const char *name,
  89. const char *def = nullptr) const
  90. {
  91. json_t *obj(json_object_get(json, name));
  92. if (!obj)
  93. return def;
  94. return json_string_value(obj);
  95. }
  96. inline int64_t GetInt(const char *name, int def = 0) const
  97. {
  98. json_t *obj(json_object_get(json, name));
  99. if (!obj)
  100. return def;
  101. return json_integer_value(obj);
  102. }
  103. inline json_t *GetObject(const char *name) const
  104. {
  105. return json_object_get(json, name);
  106. }
  107. inline json_t *get() const {return json;}
  108. };
  109. /* ------------------------------------------------------------------------ */
  110. std::string vstrprintf(const char *format, va_list args);
  111. std::string strprintf(const char *format, ...);