2017-09-14 23:11:56 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
static void hex2byte(uint8_t *hex, uint8_t *str) {
|
2020-12-30 22:11:24 -08:00
|
|
|
char high, low;
|
|
|
|
for (int i = 0, length = strlen((char *) hex); i < length; i += 2) {
|
|
|
|
high = toupper(hex[i]) - '0';
|
|
|
|
low = toupper(hex[i + 1]) - '0';
|
|
|
|
str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
|
|
|
|
}
|
2016-09-08 20:59:48 +08:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:29:30 +02:00
|
|
|
int hexpatch(const char *image, const char *from, const char *to) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
|
|
|
|
int patched = 1;
|
|
|
|
size_t filesize;
|
|
|
|
uint8_t *file, *pattern, *patch;
|
|
|
|
mmap_rw(image, file, filesize);
|
|
|
|
pattern = (uint8_t *) xmalloc(patternsize);
|
|
|
|
patch = (uint8_t *) xmalloc(patchsize);
|
|
|
|
hex2byte((uint8_t *) from, pattern);
|
|
|
|
hex2byte((uint8_t *) to, patch);
|
|
|
|
for (size_t i = 0; filesize > 0 && i < filesize - patternsize; ++i) {
|
|
|
|
if (memcmp(file + i, pattern, patternsize) == 0) {
|
|
|
|
fprintf(stderr, "Patch @ %08X [%s]->[%s]\n", (unsigned) i, from, to);
|
|
|
|
memset(file + i, 0, patternsize);
|
|
|
|
memcpy(file + i, patch, patchsize);
|
|
|
|
i += patternsize - 1;
|
|
|
|
patched = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(file, filesize);
|
|
|
|
free(pattern);
|
|
|
|
free(patch);
|
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
|
|
|
}
|