mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-24 10:35:26 +00:00
Cleanup hexpatch
This commit is contained in:
parent
7ef0746c52
commit
2ccd8b8838
@ -6,6 +6,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
#include "bootimg.h"
|
#include "bootimg.h"
|
||||||
|
|
||||||
@ -17,13 +18,13 @@ static int hex2int(char c) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int hex2ascii(char c, char d) {
|
static unsigned hex2ascii(char c, char d) {
|
||||||
int high = hex2int(c) * 16;
|
int high = hex2int(c) * 16;
|
||||||
int low = hex2int(d);
|
int low = hex2int(d);
|
||||||
return high+low;
|
return high + low;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hexstr2str(char *hex, char *str) {
|
static void hexstr2str(char *hex, unsigned char *str) {
|
||||||
char buf = 0;
|
char buf = 0;
|
||||||
for(int i = 0, length = strlen(hex); i < length; ++i){
|
for(int i = 0, length = strlen(hex); i < length; ++i){
|
||||||
if(i % 2){
|
if(i % 2){
|
||||||
@ -36,29 +37,23 @@ static void hexstr2str(char *hex, char *str) {
|
|||||||
|
|
||||||
int hexpatch(char * image, char *from, char *to) {
|
int hexpatch(char * image, char *from, char *to) {
|
||||||
int fd = open(image, O_RDWR), patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
|
int fd = open(image, O_RDWR), patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
|
||||||
off_t filesize = lseek(fd, 0, SEEK_END);
|
size_t filesize = lseek(fd, 0, SEEK_END);
|
||||||
char *file, *pattern, *patch, *start;
|
|
||||||
file = malloc(sizeof (char) * filesize);
|
|
||||||
pattern = malloc(sizeof (char) * patternsize);
|
|
||||||
patch = malloc(sizeof (char) * patchsize);
|
|
||||||
lseek(fd, 0, SEEK_SET);
|
lseek(fd, 0, SEEK_SET);
|
||||||
read(fd, file, filesize);
|
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(from, pattern);
|
||||||
hexstr2str(to, patch);
|
hexstr2str(to, patch);
|
||||||
for (off_t i = 0; i < filesize;) {
|
for (size_t i = 0; i < filesize - patternsize; ++i) {
|
||||||
int j;
|
if (memcmp(file + i, pattern, patternsize) == 0) {
|
||||||
for (j = 0; j < patternsize; ++j) {
|
printf("Pattern %s found!\nPatching to %s\n", from, to);
|
||||||
if(file[i + j] != pattern[j]) break;
|
memset(file + i, 0, patternsize);
|
||||||
|
memcpy(file + i, patch, patchsize);
|
||||||
|
i += patternsize - 1;
|
||||||
}
|
}
|
||||||
if (j == patternsize) {
|
|
||||||
fprintf(stderr, "Pattern %s found!\nPatching to %s\n", from, to);
|
|
||||||
lseek(fd, i, SEEK_SET);
|
|
||||||
write(fd, patch, patchsize);
|
|
||||||
}
|
|
||||||
if(j == 0) j = 1;
|
|
||||||
i += j;
|
|
||||||
}
|
}
|
||||||
free(file);
|
munmap(file, filesize);
|
||||||
free(pattern);
|
free(pattern);
|
||||||
free(patch);
|
free(patch);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user