-
Notifications
You must be signed in to change notification settings - Fork 18
/
pattern.cpp
64 lines (53 loc) · 1.56 KB
/
pattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <base.hpp>
#include "magiskboot.hpp"
#define MATCH(p) else if (strncmp(s + skip, p, sizeof(p) - 1) == 0) skip += (sizeof(p) - 1)
static int skip_verity_pattern(const char *s) {
int skip = s[0] == ',';
if (0) {}
MATCH("verifyatboot");
MATCH("verify");
MATCH("avb_keys");
MATCH("avb");
MATCH("support_scfs");
MATCH("fsverity");
else return -1;
if (s[skip] == '=') {
while (!strchr(" \n,", s[skip]))
++skip;
}
return skip;
}
static int skip_encryption_pattern(const char *s) {
int skip = s[0] == ',';
if (0) {}
MATCH("forceencrypt");
MATCH("forcefdeorfbe");
MATCH("fileencryption");
else return -1;
if (s[skip] == '=') {
while (!strchr(" \n,", s[skip]))
++skip;
}
return skip;
}
static uint32_t remove_pattern(char *src, uint32_t size, int(*pattern_skip)(const char *)) {
int orig_sz = size;
int write = 0;
for (int read = 0; read < orig_sz;) {
if (int skip = pattern_skip(src + read); skip > 0) {
fprintf(stderr, "Remove pattern [%.*s]\n", skip, src + read);
size -= skip;
read += skip;
} else {
src[write++] = src[read++];
}
}
memset(src + write, 0, orig_sz - write);
return size;
}
uint32_t patch_verity(void *buf, uint32_t size) {
return remove_pattern(static_cast<char *>(buf), size, skip_verity_pattern);
}
uint32_t patch_encryption(void *buf, uint32_t size) {
return remove_pattern(static_cast<char *>(buf), size, skip_encryption_pattern);
}