updater.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #define WINVER 0x0600
  18. #define _WIN32_WINDOWS 0x0600
  19. #define _WIN32_WINNT 0x0600
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <windows.h>
  22. #include <versionhelpers.h>
  23. #include <winhttp.h>
  24. #include <commctrl.h>
  25. #include <Wincrypt.h>
  26. #include <shlobj.h>
  27. #include <shellapi.h>
  28. #include <malloc.h>
  29. #include <stdlib.h>
  30. #include <tchar.h>
  31. #include <strsafe.h>
  32. #include <ctype.h>
  33. #include <blake2.h>
  34. #include <zstd.h>
  35. #include <array>
  36. #include <string>
  37. #include <vector>
  38. #include "helpers.hpp"
  39. constexpr uint8_t kBlake2HashLength = 20;
  40. constexpr uint8_t kBlake2StrLength = kBlake2HashLength * 2;
  41. using B2Hash = std::array<std::byte, kBlake2HashLength>;
  42. #if defined _M_IX86
  43. #pragma comment(linker, "/manifestdependency:\"type='win32' " \
  44. "name='Microsoft.Windows.Common-Controls' " \
  45. "version='6.0.0.0' " \
  46. "processorArchitecture='x86' " \
  47. "publicKeyToken='6595b64144ccf1df' " \
  48. "language='*'\"")
  49. #elif defined _M_IA64
  50. #pragma comment(linker, "/manifestdependency:\"type='win32' " \
  51. "name='Microsoft.Windows.Common-Controls' " \
  52. "version='6.0.0.0' " \
  53. "processorArchitecture='ia64' " \
  54. "publicKeyToken='6595b64144ccf1df' " \
  55. "language='*'\"")
  56. #elif defined _M_X64
  57. #pragma comment(linker, "/manifestdependency:\"type='win32' " \
  58. "name='Microsoft.Windows.Common-Controls' " \
  59. "version='6.0.0.0' " \
  60. "processorArchitecture='amd64' " \
  61. "publicKeyToken='6595b64144ccf1df' " \
  62. "language='*'\"")
  63. #else
  64. #pragma comment(linker, "/manifestdependency:\"type='win32' " \
  65. "name='Microsoft.Windows.Common-Controls' " \
  66. "version='6.0.0.0' processorArchitecture='*' " \
  67. "publicKeyToken='6595b64144ccf1df' " \
  68. "language='*'\"")
  69. #endif
  70. #include <util/windows/WinHandle.hpp>
  71. #include "resource.h"
  72. bool HTTPGetFile(HINTERNET hConnect, const wchar_t *url, const wchar_t *outputPath, const wchar_t *extraHeaders,
  73. int *responseCode);
  74. bool HTTPGetBuffer(HINTERNET hConnect, const wchar_t *url, const wchar_t *extraHeaders, std::vector<std::byte> &out,
  75. int *responseCode);
  76. bool HTTPPostData(const wchar_t *url, const BYTE *data, int dataLen, const wchar_t *extraHeaders, int *responseCode,
  77. std::string &response);
  78. void HashToString(const B2Hash &in, std::string &out);
  79. void StringToHash(const std::string &in, B2Hash &out);
  80. bool CalculateFileHash(const wchar_t *path, B2Hash &hash);
  81. int ApplyPatch(ZSTD_DCtx *zstdCtx, const std::byte *patch_data, size_t patch_size, const wchar_t *targetFile);
  82. extern HWND hwndMain;
  83. extern HCRYPTPROV hProvider;
  84. extern size_t totalFileSize;
  85. extern size_t completedFileSize;
  86. extern HANDLE cancelRequested;
  87. #pragma pack(push, r1, 1)
  88. typedef struct {
  89. BLOBHEADER blobheader;
  90. RSAPUBKEY rsapubkey;
  91. } PUBLICKEYHEADER;
  92. #pragma pack(pop, r1)
  93. void FreeWinHttpHandle(HINTERNET handle);
  94. using HttpHandle = CustomHandle<HINTERNET, FreeWinHttpHandle>;
  95. /* ------------------------------------------------------------------------ */
  96. class ZSTDDCtx {
  97. ZSTD_DCtx *ctx = nullptr;
  98. public:
  99. inline ZSTDDCtx() { ctx = ZSTD_createDCtx(); }
  100. inline ~ZSTDDCtx() { ZSTD_freeDCtx(ctx); }
  101. inline operator ZSTD_DCtx *() const { return ctx; }
  102. inline ZSTD_DCtx *get() const { return ctx; }
  103. };