Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchData.java
More file actions
Latest commit
228 lines (204 loc) · 12.3 KB
/
Copy pathPatchData.java
File metadata and controls
228 lines (204 loc) · 12.3 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
packagecom.kdeveloper.utils;
importcom.kdeveloper.translation.KDevAPI;
importcom.kdgdev.nxt.utils.FSUtils;
importcom.kdgdev.nxt.utils.FileUtils;
importcom.kdgdev.nxt.utils.IOUtils;
importorg.apache.commons.lang3.StringEscapeUtils;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.nio.charset.Charset;
importjava.nio.file.Files;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importjava.nio.file.StandardCopyOption;
importjava.util.LinkedHashMap;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.logging.Logger;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
/**
* Created by kirill on 29.08.14.
*/
publicclassPatchData {
privateLinkedHashMap<String, String> replaceAllFiles = newLinkedHashMap<>();
privateLinkedHashMap<String, String> copyFile = newLinkedHashMap<>();
privateLinkedHashMap<String, LinkedHashMap<String, String>> replaceinfile = newLinkedHashMap<>();
privateLinkedHashMap<String, LinkedHashMap<String, MethodData>> replacemethod = newLinkedHashMap<>();
privateLinkedHashMap<String, LinkedList<ReplaceData>> replacestrings = newLinkedHashMap<>();
publicStringappfile = null;
publicStringapppackage = null;
publicStringmapping = null;
publicPatchData(Filein) throwsIOException {
List<String> fileLines = IOUtils.readLines(newFileInputStream(in), Charset.forName("UTF-8"));
Patternappfile_p = Pattern.compile("^\\s{0,}appfile\\s+\\<(.*)\\>\\s{0,}\\;");
Patternapppackage_p = Pattern.compile("^\\s{0,}apppackage\\s+\\<(.*)\\>\\s{0,}\\;");
Patternreplaceall_p = Pattern.compile("^\\s{0,}replaceinall\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s{0,}\\;");
Patternreplacefile_p = Pattern.compile("^\\s{0,}replaceinfile\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s{0,}\\;");
Patterncopyfile_p = Pattern.compile("^\\s{0,}copyfile\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s{0,}\\;");
Patternmapping_p = Pattern.compile("^\\s{0,}mapping\\s+\\\"(.*)\\\"\\s{0,}\\;");
Patternreplacemethod_p = Pattern.compile("^\\s{0,}methodreplace\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s{0,}\\;");
Patternreplacestrings_p = Pattern.compile("^\\s{0,}methodstrings\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*)\\\"\\s+\\\"(.*?)\\\"\\s+\\\"(.*?)\\\"\\s{0,}\\;");
for (Stringline : fileLines) {
Matcherappfile_m = appfile_p.matcher(line);
if (appfile_m.matches()) {
appfile = appfile_m.group(1);
continue;
}
Matcherapppackage_m = apppackage_p.matcher(line);
if (apppackage_m.matches()) {
apppackage = apppackage_m.group(1);
continue;
}
Matcherreplaceall_m = replaceall_p.matcher(line);
if (replaceall_m.matches()) {
replaceAllFiles.put(replaceall_m.group(1), replaceall_m.group(2));
continue;
}
Matchercopyfile_m = copyfile_p.matcher(line);
if (copyfile_m.matches()) {
copyFile.put(FileUtils.fixSeparator(copyfile_m.group(1)), FileUtils.fixSeparator(copyfile_m.group(2)));
continue;
}
Matcherreplacefile_m = replacefile_p.matcher(line);
if (replacefile_m.matches()) {
if (replaceinfile.containsKey(replacefile_m.group(1))) {
replaceinfile.get(replacefile_m.group(1)).put(replacefile_m.group(2), replacefile_m.group(3));
} else {
LinkedHashMap<String, String> replacer = newLinkedHashMap<>();
replacer.put(replacefile_m.group(2), replacefile_m.group(3));
replaceinfile.put(replacefile_m.group(1), replacer);
}
}
Matchermapping_m = mapping_p.matcher(line);
if (mapping_m.matches()) {
mapping = mapping_m.group(1);
}
Matcherreplacemethod_m = replacemethod_p.matcher(line);
if (replacemethod_m.matches()) {
MethodDatamethodData = newMethodData(replacemethod_m.group(3), replacemethod_m.group(4), replacemethod_m.group(5), replacemethod_m.group(6));
if (replacemethod.containsKey(replacemethod_m.group(1))) {
replacemethod.get(replacemethod_m.group(1)).put(replacemethod_m.group(2), methodData);
} else {
LinkedHashMap<String, MethodData> replacer = newLinkedHashMap<>();
replacer.put(replacemethod_m.group(2), methodData);
replacemethod.put(replacemethod_m.group(1), replacer);
}
}
Matcherreplacestrings_m = replacestrings_p.matcher(line);
if (replacestrings_m.matches()) {
ReplaceDatareplacerData = newReplaceData(replacestrings_m.group(3), replacestrings_m.group(4), replacestrings_m.group(2), replacestrings_m.group(5), replacestrings_m.group(6));
if(replacestrings.containsKey(replacestrings_m.group(1))) {
replacestrings.get(replacestrings_m.group(1)).add(replacerData);
} else {
LinkedList<ReplaceData> list = newLinkedList<>();
list.add(replacerData);
replacestrings.put(replacestrings_m.group(1), list);
}
}
}
}
@Override
publicStringtoString() {
System.out.println(appfile);
System.out.println(apppackage);
System.out.println(mapping);
System.out.println(copyFile);
System.out.println(replaceAllFiles);
System.out.println(replaceinfile);
returnsuper.toString();
}
privatestaticfinalchar[] hexChar = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
privatestaticStringunicodeEscape(Strings) {
StringBuildersb = newStringBuilder();
for (inti = 0; i < s.length(); i++) {
charc = s.charAt(i);
if ((c >> 7) > 0) {
sb.append("\\u");
sb.append(hexChar[(c >> 12) & 0xF]); // append the hex character for the left-most 4-bits
sb.append(hexChar[(c >> 8) & 0xF]); // hex for the second group of 4-bits from the left
sb.append(hexChar[(c >> 4) & 0xF]); // hex for the third group
sb.append(hexChar[c & 0xF]); // hex for the last group, e.g., the right most 4-bits
} else {
sb.append(c);
}
}
returnsb.toString();
}
publicvoidpatch(Filehere, Filesmali, Fileapk) throwsIOException {
if (appfile == null && apppackage == null) thrownewRuntimeException("Where I can patch it?!");
for (StringfileFrom : copyFile.keySet()) {
StringfileTo = copyFile.get(fileFrom);
PathpFileFrom = Paths.get(fileFrom.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath()));
PathpFileTo = Paths.get(fileTo.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath()));
LOGGER.info("Copying file: " + pFileFrom + " to " + pFileTo);
Files.copy(pFileFrom, pFileTo, StandardCopyOption.REPLACE_EXISTING);
}
List<File> files = FSUtils.walk(smali.getCanonicalPath(), ".smali");
for (Filef : files) {
for (Stringkey : replaceAllFiles.keySet()) {
Stringreplaceable_origin = newString(IOUtils.readFile(f), "UTF-8");
Stringreplaceable = newString(IOUtils.readFile(f), "UTF-8");
replaceable = replaceable.replace(StringEscapeUtils.unescapeJson(key), unicodeEscape(StringEscapeUtils.unescapeJson(replaceAllFiles.get(key))));
if (!replaceable_origin.equals(replaceable)) {
LOGGER.info("Replacing " + StringEscapeUtils.unescapeJson(key) + " to " + unicodeEscape(StringEscapeUtils.unescapeJson(replaceAllFiles.get(key))) + " in " + f.getName());
IOUtils.writeFile(replaceable.getBytes("UTF-8"), f);
}
}
}
for (Stringkey : replaceinfile.keySet()) {
Filepatchable = newFile(FileUtils.fixSeparator(key.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath())));
if (!patchable.exists()) continue;
LinkedHashMap<String, String> stringStringHashMap = replaceinfile.get(key);
for (Stringkey_r : stringStringHashMap.keySet()) {
Stringreplaceable_origin = newString(IOUtils.readFile(patchable), "UTF-8");
Stringreplaceable = newString(IOUtils.readFile(patchable), "UTF-8");
replaceable = replaceable.replace(StringEscapeUtils.unescapeJson(key_r), unicodeEscape(StringEscapeUtils.unescapeJson(stringStringHashMap.get(key_r))));
if (!replaceable_origin.equals(replaceable)) {
LOGGER.info("Replacing " + StringEscapeUtils.unescapeJson(key_r) + " to " + unicodeEscape(StringEscapeUtils.unescapeJson(stringStringHashMap.get(key_r))) + " in " + patchable.getName());
IOUtils.writeFile(replaceable.getBytes("UTF-8"), patchable);
}
}
}
for (Stringkey : replacemethod.keySet()) {
Filepatchable = newFile(FileUtils.fixSeparator(key.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath())));
if (!patchable.exists()) continue;
LinkedHashMap<String, MethodData> stringStringHashMap = replacemethod.get(key);
for (Stringkey_r : stringStringHashMap.keySet()) {
FilemethodFile = newFile(FileUtils.fixSeparator(key_r.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath())));
Stringreplaceable_origin = newString(IOUtils.readFile(patchable), "UTF-8");
Stringreplaceable = newString(IOUtils.readFile(patchable), "UTF-8");
Stringmethod = newString(IOUtils.readFile(methodFile), "UTF-8");
MethodDatamd = stringStringHashMap.get(key_r);
Stringpat = Pattern.quote(".method " + md.getAccess() + " " + md.getName() + "(" + (md.getParameters().equals("<none>") ? "" : md.getParameters()) + ")" + md.getReturn());
Patternptn = Pattern.compile(pat + "([\\s\\S]*?)\\.end method");
Matcherm = ptn.matcher(replaceable);
replaceable = m.replaceAll(unicodeEscape(Matcher.quoteReplacement(method)));
if (!replaceable_origin.equals(replaceable)) {
LOGGER.info("Replacing " + md.getName() + "(" + md.getParameters() + ") in " + patchable.getName());
IOUtils.writeFile(replaceable.getBytes("UTF-8"), patchable);
}
}
}
for (Stringkey : replacestrings.keySet()) {
Filepatchable = newFile(FileUtils.fixSeparator(key.replace("%here%", here.getCanonicalPath()).replace("%smali%", smali.getCanonicalPath())));
if (!patchable.exists()) continue;
Stringreplaceable_origin = newString(IOUtils.readFile(patchable), "UTF-8");
Stringreplaceable = newString(IOUtils.readFile(patchable), "UTF-8");
KDevAPIkapi = newKDevAPI();
for(ReplaceDatareplacer : replacestrings.get(key)) {
StringoriginalResourceId = kapi.getResouceId(apk, replacer.getType(), replacer.getName());
StringnewResourceId = kapi.getResouceId(apk, replacer.getType(), replacer.getReplacename());
replaceable = replaceable.replace(originalResourceId + replacer.getAdditional1(), newResourceId + replacer.getAdditional2());
if (!replaceable_origin.equals(replaceable)) {
LOGGER.info("Replacing " + originalResourceId + replacer.getAdditional1() + " to " + newResourceId + replacer.getAdditional2() + " in " + patchable.getName());
IOUtils.writeFile(replaceable.getBytes("UTF-8"), patchable);
}
}
}
}
privatefinalstaticLoggerLOGGER = Logger.getLogger(PatchData.class.getName());
}