2017-02-27 21:37:47 +00:00
|
|
|
#include "magiskboot.h"
|
2016-09-08 12:59:48 +00:00
|
|
|
|
2017-03-07 16:54:23 +00:00
|
|
|
static void hex2byte(const char *hex, unsigned char *str) {
|
|
|
|
char high, low;
|
|
|
|
for (int i = 0, length = strlen(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 12:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 13:16:59 +00:00
|
|
|
void hexpatch(const char *image, const char *from, const char *to) {
|
|
|
|
int patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
|
|
|
|
size_t filesize;
|
2017-02-24 19:50:26 +00:00
|
|
|
unsigned char *file, *pattern, *patch;
|
2017-03-04 13:16:59 +00:00
|
|
|
mmap_rw(image, &file, &filesize);
|
2017-02-24 19:50:26 +00:00
|
|
|
pattern = malloc(patternsize);
|
|
|
|
patch = malloc(patchsize);
|
2017-03-07 16:54:23 +00:00
|
|
|
hex2byte(from, pattern);
|
|
|
|
hex2byte(to, patch);
|
2017-02-24 19:50:26 +00:00
|
|
|
for (size_t i = 0; i < filesize - patternsize; ++i) {
|
|
|
|
if (memcmp(file + i, pattern, patternsize) == 0) {
|
|
|
|
printf("Pattern %s found!\nPatching to %s\n", from, to);
|
|
|
|
memset(file + i, 0, patternsize);
|
|
|
|
memcpy(file + i, patch, patchsize);
|
|
|
|
i += patternsize - 1;
|
2016-09-08 12:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-24 19:50:26 +00:00
|
|
|
munmap(file, filesize);
|
2016-09-08 12:59:48 +00:00
|
|
|
free(pattern);
|
|
|
|
free(patch);
|
2017-02-27 21:37:47 +00:00
|
|
|
}
|