Magisk/native/jni/magiskboot/pattern.cpp

76 lines
1.7 KiB
C++
Raw Normal View History

2017-12-06 17:30:48 +00:00
#include <malloc.h>
#include <string.h>
2020-03-09 08:50:30 +00:00
#include <utils.hpp>
2019-02-10 08:57:51 +00:00
2020-03-09 08:50:30 +00:00
#include "magiskboot.hpp"
2017-12-06 17:30:48 +00:00
#define MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
2017-12-06 17:30:48 +00:00
static int check_verity_pattern(const char *s) {
int skip = s[0] == ',';
if (0) {}
MATCH("verify");
MATCH("avb");
MATCH("support_scfs");
else return -1;
2017-12-06 17:30:48 +00:00
if (s[skip] == '=') {
while (s[skip] != '\0' && s[skip] != ' ' && s[skip] != '\n' && s[skip] != ',')
++skip;
2017-12-06 17:30:48 +00:00
}
return skip;
2017-12-06 17:30:48 +00:00
}
#undef MATCH
#define MATCH(p) else if (strncmp(s, p, sizeof(p) - 1) == 0) return (sizeof(p) - 1)
2017-12-06 17:30:48 +00:00
static int check_encryption_pattern(const char *s) {
if (0) {}
MATCH("forceencrypt");
MATCH("forcefdeorfbe");
MATCH("fileencryption");
else return -1;
2017-12-06 17:30:48 +00:00
}
2019-09-26 07:14:56 +00:00
char *patch_verity(const void *buf, uint32_t &size, bool inplace) {
auto src = static_cast<const char *>(buf);
auto dest = (char *)(inplace ? buf : xmalloc(size));
int src_size = size;
2019-02-25 04:09:34 +00:00
bool found = false;
int write = 0;
for (int read = 0; read < src_size;) {
if (int skip; (skip = check_verity_pattern(src + read)) > 0) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
2019-02-25 04:09:34 +00:00
found = true;
} else {
dest[write++] = src[read++];
2017-12-06 17:30:48 +00:00
}
2017-12-31 11:30:56 +00:00
}
dest[write] = '\0';
2019-09-26 07:14:56 +00:00
if (!found) {
if (!inplace)
free(dest);
2019-09-26 07:14:56 +00:00
return nullptr;
}
return dest;
2017-12-06 17:30:48 +00:00
}
2019-09-26 07:49:05 +00:00
void patch_encryption(void *buf, uint32_t &size) {
2019-09-26 07:14:56 +00:00
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;
2017-12-06 17:30:48 +00:00
}
2019-09-26 07:14:56 +00:00
src[write] = src[read];
2017-12-06 17:30:48 +00:00
}
2019-09-26 07:14:56 +00:00
src[write] = '\0';
2017-12-06 17:30:48 +00:00
}