Magisk/jni/magiskboot/hexpatch.c

51 lines
1.3 KiB
C
Raw Normal View History

2017-02-27 22:37:47 +01:00
#include "magiskboot.h"
2017-02-24 07:58:44 +01:00
static int hex2int(char c) {
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if(result > 9) result--;
return result;
}
2017-02-24 20:50:26 +01:00
static unsigned hex2ascii(char c, char d) {
int high = hex2int(c) * 16;
int low = hex2int(d);
2017-02-24 20:50:26 +01:00
return high + low;
}
2017-02-24 20:50:26 +01:00
static void hexstr2str(char *hex, unsigned char *str) {
char buf = 0;
for(int i = 0, length = strlen(hex); i < length; ++i){
if(i % 2){
str[i / 2] = hex2ascii(buf, hex[i]);
} else{
buf = hex[i];
}
}
}
2017-02-27 22:37:47 +01:00
void hexpatch(char * image, char *from, char *to) {
int fd = open(image, O_RDWR), patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
2017-02-24 20:50:26 +01:00
size_t filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
2017-02-24 20:50:26 +01:00
unsigned char *file, *pattern, *patch;
file = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
pattern = malloc(patternsize);
patch = malloc(patchsize);
hexstr2str(from, pattern);
hexstr2str(to, patch);
2017-02-24 20:50:26 +01: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;
}
}
2017-02-24 20:50:26 +01:00
munmap(file, filesize);
free(pattern);
free(patch);
close(fd);
2017-02-27 22:37:47 +01:00
}