1
0

patch.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, const size_t patch_size, const wchar_t *targetFile)
  47. try {
  48. int64_t newsize;
  49. bool success;
  50. WinHandle hTarget;
  51. /* --------------------------------- *
  52. * open patch and file to patch */
  53. hTarget = CreateFile(targetFile, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
  54. if (!hTarget.Valid())
  55. throw int(GetLastError());
  56. /* --------------------------------- *
  57. * read patch header */
  58. if (memcmp(patch_data, kDeltaMagic, kMagicSize) != 0)
  59. throw int(-4);
  60. /* --------------------------------- *
  61. * allocate new file size data */
  62. newsize = offtin((const uint8_t *)patch_data + kMagicSize);
  63. if (newsize < 0 || newsize >= 0x7ffffffff)
  64. throw int(-5);
  65. vector<std::byte> newData;
  66. try {
  67. newData.resize((size_t)newsize);
  68. } catch (...) {
  69. throw int(-1);
  70. }
  71. /* --------------------------------- *
  72. * read old file */
  73. DWORD read;
  74. DWORD oldFileSize;
  75. oldFileSize = GetFileSize(hTarget, nullptr);
  76. if (oldFileSize == INVALID_FILE_SIZE)
  77. throw int(GetLastError());
  78. vector<std::byte> oldData;
  79. try {
  80. oldData.resize(oldFileSize);
  81. } catch (...) {
  82. throw int(-1);
  83. }
  84. if (!ReadFile(hTarget, oldData.data(), oldFileSize, &read, nullptr))
  85. throw int(GetLastError());
  86. if (read != oldFileSize)
  87. throw int(-1);
  88. /* --------------------------------- *
  89. * patch to new file data */
  90. size_t result = ZSTD_decompress_usingDict(zstdCtx, newData.data(), newData.size(), patch_data + kHeaderSize,
  91. patch_size - kHeaderSize, oldData.data(), oldData.size());
  92. if (result != newsize || ZSTD_isError(result))
  93. throw int(-9);
  94. /* --------------------------------- *
  95. * write new file */
  96. hTarget = nullptr;
  97. hTarget = CreateFile(targetFile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
  98. if (!hTarget.Valid())
  99. throw int(GetLastError());
  100. DWORD written;
  101. success = !!WriteFile(hTarget, newData.data(), (DWORD)newsize, &written, nullptr);
  102. if (!success || written != newsize)
  103. throw int(GetLastError());
  104. return 0;
  105. } catch (int code) {
  106. return code;
  107. }