2017-09-14 23:11:56 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <utils.hpp>
|
2019-02-10 03:57:51 -05:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskboot.hpp"
|
2016-09-08 20:59:48 +08:00
|
|
|
|
2021-02-28 14:36:48 -08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static void hex2byte(const char *hex, uint8_t *buf) {
|
2020-12-30 22:11:24 -08:00
|
|
|
char high, low;
|
2021-02-28 14:36:48 -08:00
|
|
|
for (int i = 0, length = strlen(hex); i < length; i += 2) {
|
2020-12-30 22:11:24 -08:00
|
|
|
high = toupper(hex[i]) - '0';
|
|
|
|
low = toupper(hex[i + 1]) - '0';
|
2021-02-28 14:36:48 -08:00
|
|
|
buf[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2016-09-08 20:59:48 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 01:50:55 -08:00
|
|
|
int hexpatch(const char *file, const char *from, const char *to) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int patched = 1;
|
2021-02-28 14:36:48 -08:00
|
|
|
|
2021-11-30 01:50:55 -08:00
|
|
|
auto m = mmap_data(file, true);
|
2021-02-28 14:36:48 -08:00
|
|
|
|
|
|
|
vector<uint8_t> pattern(strlen(from) / 2);
|
|
|
|
vector<uint8_t> patch(strlen(to) / 2);
|
|
|
|
|
|
|
|
hex2byte(from, pattern.data());
|
|
|
|
hex2byte(to, patch.data());
|
|
|
|
|
2021-11-30 01:50:55 -08:00
|
|
|
uint8_t * const end = m.buf + m.sz;
|
|
|
|
for (uint8_t *curr = m.buf; curr < end; curr += pattern.size()) {
|
2021-02-28 14:36:48 -08:00
|
|
|
curr = static_cast<uint8_t*>(memmem(curr, end - curr, pattern.data(), pattern.size()));
|
|
|
|
if (curr == nullptr)
|
|
|
|
return patched;
|
2021-11-30 01:50:55 -08:00
|
|
|
fprintf(stderr, "Patch @ %08X [%s] -> [%s]\n", (unsigned)(curr - m.buf), from, to);
|
2021-02-28 14:36:48 -08:00
|
|
|
memset(curr, 0, pattern.size());
|
|
|
|
memcpy(curr, patch.data(), patch.size());
|
|
|
|
patched = 0;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2019-06-08 14:29:30 +02:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
return patched;
|
2017-02-28 05:37:47 +08:00
|
|
|
}
|