Fix patch verity and forceencrypt

This commit is contained in:
topjohnwu
2019-09-26 03:14:56 -04:00
parent b763b81f56
commit 3d4081d0af
3 changed files with 29 additions and 32 deletions

View File

@@ -22,20 +22,20 @@ static int check_verity_pattern(const char *s) {
}
static int check_encryption_pattern(const char *s) {
const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe", nullptr };
for (int i = 0 ; encrypt_list[i]; ++i) {
int len = strlen(encrypt_list[i]);
if (strncmp(s, encrypt_list[i], len) == 0)
static const char *encrypt_list[] = { "forceencrypt", "forcefdeorfbe" };
for (auto enc : encrypt_list) {
int len = strlen(enc);
if (strncmp(s, enc, len) == 0)
return len;
}
return -1;
}
char *patch_verity(const void *buf, uint32_t &size) {
char *patch_verity(const void *buf, uint32_t &size, bool inplace) {
auto src = static_cast<const char *>(buf);
int src_size = size;
bool found = false;
char patched[4096];
auto patched = (char *)(inplace ? buf : xmalloc(size));
int write = 0;
for (int read = 0; read < src_size; ++read, ++write) {
if (int skip; (skip = check_verity_pattern(src + read)) > 0) {
@@ -47,24 +47,25 @@ char *patch_verity(const void *buf, uint32_t &size) {
patched[write] = src[read];
}
patched[write] = '\0';
return found ? strdup(patched) : nullptr;
}
void patch_encryption(void **buf, uint32_t *size) {
int skip, src_size = *size;
char *src = (char *) *buf, *patched = (char *) xcalloc(src_size, 1);
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
if ((skip = check_encryption_pattern(src + read)) > 0) {
fprintf(stderr, "Replace pattern [%.*s] with [encryptable]\n", skip, src + read);
memcpy(patched + read, "encryptable", 11);
read += skip;
write += 11;
*size -= (skip - 11);
}
patched[write] = src[read];
if (!found) {
if (!inplace)
free(patched);
return nullptr;
}
free(*buf);
*buf = patched;
return patched;
}
void patch_encryption(void *&buf, uint32_t &size) {
auto src = static_cast<char *>(buf);
int src_size = size;
int write = 0;
for (int read = 0; read < src_size; ++read, ++write) {
if (int skip; (skip = check_encryption_pattern(src + read)) > 0) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
}
src[write] = src[read];
}
src[write] = '\0';
}