yolobs-studio/UI/win-update/updater/patch.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

321 lines
6.8 KiB
C++
Raw Normal View History

2018-05-29 19:13:02 +00:00
/*
* Copyright (c) 2017-2018 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2017-04-19 19:54:15 +00:00
#include "updater.hpp"
#include <stdint.h>
#include <vector>
2018-05-29 19:13:02 +00:00
#ifdef _MSC_VER
2019-09-22 21:19:10 +00:00
#define restrict __restrict
#include <lzma.h>
#undef restrict
2018-05-29 19:13:02 +00:00
#else
2019-09-22 21:19:10 +00:00
#include <lzma.h>
2018-05-29 19:13:02 +00:00
#endif
2017-04-19 19:54:15 +00:00
using namespace std;
2019-09-22 21:19:10 +00:00
#define MAX_BUF_SIZE 262144
2017-04-19 19:54:15 +00:00
#define READ_BUF_SIZE 32768
/* ------------------------------------------------------------------------ */
class LZMAStream {
lzma_stream strm = {};
bool initialized = false;
public:
inline ~LZMAStream()
{
if (initialized) {
lzma_end(&strm);
}
}
inline bool init_decoder()
{
2019-09-22 21:19:10 +00:00
lzma_ret ret = lzma_stream_decoder(&strm, 200 * 1024 * 1024, 0);
2017-04-19 19:54:15 +00:00
initialized = (ret == LZMA_OK);
return initialized;
}
inline operator lzma_stream *() { return &strm; }
inline bool operator!() const { return !initialized; }
inline lzma_stream *get() { return &strm; }
};
class File {
FILE *f = nullptr;
public:
inline ~File()
{
if (f)
fclose(f);
}
inline FILE **operator&() { return &f; }
inline operator FILE *() const { return f; }
inline bool operator!() const { return !f; }
};
/* ------------------------------------------------------------------------ */
struct bspatch_stream {
void *opaque;
int (*read)(const struct bspatch_stream *stream, void *buffer,
2019-09-22 21:19:10 +00:00
int length);
2017-04-19 19:54:15 +00:00
};
/* ------------------------------------------------------------------------ */
static int64_t offtin(const uint8_t *buf)
{
int64_t y;
y = buf[7] & 0x7F;
y = y * 256;
y += buf[6];
y = y * 256;
y += buf[5];
y = y * 256;
y += buf[4];
y = y * 256;
y += buf[3];
y = y * 256;
y += buf[2];
y = y * 256;
y += buf[1];
y = y * 256;
y += buf[0];
if (buf[7] & 0x80)
y = -y;
return y;
}
/* ------------------------------------------------------------------------ */
static int bspatch(const uint8_t *old, int64_t oldsize, uint8_t *newp,
2019-09-22 21:19:10 +00:00
int64_t newsize, struct bspatch_stream *stream)
2017-04-19 19:54:15 +00:00
{
uint8_t buf[8];
int64_t oldpos, newpos;
int64_t ctrl[3];
int64_t i;
oldpos = 0;
newpos = 0;
while (newpos < newsize) {
/* Read control data */
for (i = 0; i <= 2; i++) {
if (stream->read(stream, buf, 8))
return -1;
ctrl[i] = offtin(buf);
};
/* Sanity-check */
if (newpos + ctrl[0] > newsize)
return -1;
/* Read diff string */
if (stream->read(stream, newp + newpos, (int)ctrl[0]))
return -1;
/* Add old data to diff string */
for (i = 0; i < ctrl[0]; i++)
if ((oldpos + i >= 0) && (oldpos + i < oldsize))
newp[newpos + i] += old[oldpos + i];
/* Adjust pointers */
newpos += ctrl[0];
oldpos += ctrl[0];
/* Sanity-check */
if (newpos + ctrl[1] > newsize)
return -1;
/* Read extra string */
if (stream->read(stream, newp + newpos, (int)ctrl[1]))
return -1;
/* Adjust pointers */
newpos += ctrl[1];
oldpos += ctrl[2];
};
return 0;
}
/* ------------------------------------------------------------------------ */
struct patch_data {
2019-09-22 21:19:10 +00:00
HANDLE h;
lzma_stream *strm;
uint8_t buf[READ_BUF_SIZE];
2017-04-19 19:54:15 +00:00
};
static int read_lzma(const struct bspatch_stream *stream, void *buffer, int len)
{
if (!len)
return 0;
2019-09-22 21:19:10 +00:00
patch_data *data = (patch_data *)stream->opaque;
HANDLE h = data->h;
lzma_stream *strm = data->strm;
2017-04-19 19:54:15 +00:00
strm->avail_out = (size_t)len;
2019-09-22 21:19:10 +00:00
strm->next_out = (uint8_t *)buffer;
2017-04-19 19:54:15 +00:00
for (;;) {
if (strm->avail_in == 0) {
DWORD read_size;
if (!ReadFile(h, data->buf, READ_BUF_SIZE, &read_size,
2019-09-22 21:19:10 +00:00
nullptr))
2017-04-19 19:54:15 +00:00
return -1;
if (read_size == 0)
return -1;
strm->avail_in = (size_t)read_size;
2019-09-22 21:19:10 +00:00
strm->next_in = data->buf;
2017-04-19 19:54:15 +00:00
}
lzma_ret ret = lzma_code(strm, LZMA_RUN);
if (ret == LZMA_STREAM_END)
return 0;
if (ret != LZMA_OK)
return -1;
if (strm->avail_out == 0)
break;
}
return 0;
}
int ApplyPatch(const wchar_t *patchFile, const wchar_t *targetFile)
try {
2019-09-22 21:19:10 +00:00
uint8_t header[24];
int64_t newsize;
2017-04-19 19:54:15 +00:00
struct bspatch_stream stream;
2019-09-22 21:19:10 +00:00
bool success;
2017-04-19 19:54:15 +00:00
2019-09-22 21:19:10 +00:00
WinHandle hPatch;
WinHandle hTarget;
2017-04-19 19:54:15 +00:00
LZMAStream strm;
/* --------------------------------- *
* open patch and file to patch */
2019-09-22 21:19:10 +00:00
hPatch = CreateFile(patchFile, GENERIC_READ, 0, nullptr, OPEN_EXISTING,
0, nullptr);
2017-04-19 19:54:15 +00:00
if (!hPatch.Valid())
throw int(GetLastError());
hTarget = CreateFile(targetFile, GENERIC_READ, 0, nullptr,
2019-09-22 21:19:10 +00:00
OPEN_EXISTING, 0, nullptr);
2017-04-19 19:54:15 +00:00
if (!hTarget.Valid())
throw int(GetLastError());
/* --------------------------------- *
* read patch header */
DWORD read;
success = !!ReadFile(hPatch, header, sizeof(header), &read, nullptr);
if (success && read == sizeof(header)) {
if (memcmp(header, "JIMSLEY/BSDIFF43", 16))
throw int(-4);
} else {
throw int(GetLastError());
}
/* --------------------------------- *
* allocate new file size data */
newsize = offtin(header + 16);
if (newsize < 0 || newsize >= 0x7ffffffff)
throw int(-5);
vector<uint8_t> newData;
try {
2018-05-29 19:13:02 +00:00
newData.resize((size_t)newsize);
2017-04-19 19:54:15 +00:00
} catch (...) {
throw int(-1);
}
/* --------------------------------- *
* read old file */
DWORD targetFileSize;
targetFileSize = GetFileSize(hTarget, nullptr);
if (targetFileSize == INVALID_FILE_SIZE)
throw int(GetLastError());
vector<uint8_t> oldData;
try {
oldData.resize(targetFileSize);
} catch (...) {
throw int(-1);
}
if (!ReadFile(hTarget, &oldData[0], targetFileSize, &read, nullptr))
throw int(GetLastError());
if (read != targetFileSize)
throw int(-1);
/* --------------------------------- *
* patch to new file data */
if (!strm.init_decoder())
throw int(-10);
patch_data data;
2019-09-22 21:19:10 +00:00
data.h = hPatch;
2017-04-19 19:54:15 +00:00
data.strm = strm.get();
2019-09-22 21:19:10 +00:00
stream.read = read_lzma;
2017-04-19 19:54:15 +00:00
stream.opaque = &data;
int ret = bspatch(oldData.data(), oldData.size(), newData.data(),
2019-09-22 21:19:10 +00:00
newData.size(), &stream);
2017-04-19 19:54:15 +00:00
if (ret != 0)
throw int(-9);
/* --------------------------------- *
* write new file */
hTarget = nullptr;
hTarget = CreateFile(targetFile, GENERIC_WRITE, 0, nullptr,
2019-09-22 21:19:10 +00:00
CREATE_ALWAYS, 0, nullptr);
2017-04-19 19:54:15 +00:00
if (!hTarget.Valid())
throw int(GetLastError());
DWORD written;
2019-09-22 21:19:10 +00:00
success = !!WriteFile(hTarget, newData.data(), (DWORD)newsize, &written,
nullptr);
2017-04-19 19:54:15 +00:00
if (!success || written != newsize)
throw int(GetLastError());
return 0;
} catch (int code) {
return code;
}