patch.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "updater.hpp"
  17. #include <vector>
  18. using namespace std;
  19. /* ------------------------------------------------------------------------ */
  20. static int64_t offtin(const uint8_t *buf)
  21. {
  22. int64_t y;
  23. y = buf[7] & 0x7F;
  24. y = y * 256;
  25. y += buf[6];
  26. y = y * 256;
  27. y += buf[5];
  28. y = y * 256;
  29. y += buf[4];
  30. y = y * 256;
  31. y += buf[3];
  32. y = y * 256;
  33. y += buf[2];
  34. y = y * 256;
  35. y += buf[1];
  36. y = y * 256;
  37. y += buf[0];
  38. if (buf[7] & 0x80)
  39. y = -y;
  40. return y;
  41. }
  42. /* ------------------------------------------------------------------------ */
  43. constexpr const char *kDeltaMagic = "BOUF//ZSTD//DICT";
  44. constexpr int kMagicSize = 16;
  45. constexpr int kHeaderSize = kMagicSize + 8; // magic + int64_t delta size
  46. int ApplyPatch(ZSTD_DCtx *zstdCtx, const std::byte *patch_data,
  47. const size_t patch_size, const wchar_t *targetFile)
  48. try {
  49. int64_t newsize;
  50. bool success;
  51. WinHandle hTarget;
  52. /* --------------------------------- *
  53. * open patch and file to patch */
  54. hTarget = CreateFile(targetFile, GENERIC_READ, 0, nullptr,
  55. OPEN_EXISTING, 0, nullptr);
  56. if (!hTarget.Valid())
  57. throw int(GetLastError());
  58. /* --------------------------------- *
  59. * read patch header */
  60. if (memcmp(patch_data, kDeltaMagic, kMagicSize) != 0)
  61. throw int(-4);
  62. /* --------------------------------- *
  63. * allocate new file size data */
  64. newsize = offtin((const uint8_t *)patch_data + kMagicSize);
  65. if (newsize < 0 || newsize >= 0x7ffffffff)
  66. throw int(-5);
  67. vector<std::byte> newData;
  68. try {
  69. newData.resize((size_t)newsize);
  70. } catch (...) {
  71. throw int(-1);
  72. }
  73. /* --------------------------------- *
  74. * read old file */
  75. DWORD read;
  76. DWORD oldFileSize;
  77. oldFileSize = GetFileSize(hTarget, nullptr);
  78. if (oldFileSize == INVALID_FILE_SIZE)
  79. throw int(GetLastError());
  80. vector<std::byte> oldData;
  81. try {
  82. oldData.resize(oldFileSize);
  83. } catch (...) {
  84. throw int(-1);
  85. }
  86. if (!ReadFile(hTarget, oldData.data(), oldFileSize, &read, nullptr))
  87. throw int(GetLastError());
  88. if (read != oldFileSize)
  89. throw int(-1);
  90. /* --------------------------------- *
  91. * patch to new file data */
  92. size_t result = ZSTD_decompress_usingDict(
  93. zstdCtx, newData.data(), newData.size(),
  94. patch_data + kHeaderSize, patch_size - kHeaderSize,
  95. oldData.data(), oldData.size());
  96. if (result != newsize || ZSTD_isError(result))
  97. throw int(-9);
  98. /* --------------------------------- *
  99. * write new file */
  100. hTarget = nullptr;
  101. hTarget = CreateFile(targetFile, GENERIC_WRITE, 0, nullptr,
  102. CREATE_ALWAYS, 0, nullptr);
  103. if (!hTarget.Valid())
  104. throw int(GetLastError());
  105. DWORD written;
  106. success = !!WriteFile(hTarget, newData.data(), (DWORD)newsize, &written,
  107. nullptr);
  108. if (!success || written != newsize)
  109. throw int(GetLastError());
  110. return 0;
  111. } catch (int code) {
  112. return code;
  113. }