cleanup formatting in smali-res-tagger

- experiment rewriting resIds in smali to remove need of public.xml
This commit is contained in:
Connor Tumbleson 2018-02-16 08:25:34 -05:00
parent dc2db53dec
commit 1d485d21e9
No known key found for this signature in database
GPG Key ID: C3CC0A201EC7DA75

View File

@ -47,15 +47,8 @@ public class ResSmaliUpdater {
for (String fileName : dir.getFiles(true)) {
try {
tagResIdsForFile(resTable, dir, fileName);
} catch (IOException ex) {
throw new AndrolibException("Could not tag resIDs for file: "
+ fileName, ex);
} catch (DirectoryException ex) {
throw new AndrolibException("Could not tag resIDs for file: "
+ fileName, ex);
} catch (AndrolibException ex) {
throw new AndrolibException("Could not tag resIDs for file: "
+ fileName, ex);
} catch (IOException | DirectoryException | AndrolibException ex) {
throw new AndrolibException("Could not tag resIDs for file: " + fileName, ex);
}
}
}
@ -65,47 +58,36 @@ public class ResSmaliUpdater {
try {
Directory dir = new FileDirectory(smaliDir);
for (String fileName : dir.getFiles(true)) {
Iterator<String> it = IOUtils.readLines(
dir.getFileInput(fileName)).iterator();
Iterator<String> it = IOUtils.readLines(dir.getFileInput(fileName)).iterator();
PrintWriter out = new PrintWriter(dir.getFileOutput(fileName));
while (it.hasNext()) {
String line = it.next();
out.println(line);
Matcher m1 = RES_NAME_PATTERN.matcher(line);
if (!m1.matches()) {
if (! m1.matches()) {
continue;
}
Matcher m2 = RES_ID_PATTERN.matcher(it.next());
if (!m2.matches()) {
if (! m2.matches()) {
throw new AndrolibException();
}
int resID = resTable.getPackage(m1.group(1))
.getType(m1.group(2)).getResSpec(m1.group(3))
.getId().id;
int resID = resTable.getPackage(m1.group(1)).getType(m1.group(2)).getResSpec(m1.group(3)).getId().id;
if (m2.group(1) != null) {
out.println(String.format(RES_ID_FORMAT_FIELD,
m2.group(1), resID));
out.println(String.format(RES_ID_FORMAT_FIELD, m2.group(1), resID));
} else {
out.println(String.format(RES_ID_FORMAT_CONST,
m2.group(2), resID));
out.println(String.format(RES_ID_FORMAT_CONST, m2.group(2), resID));
}
}
out.close();
}
} catch (IOException ex) {
throw new AndrolibException("Could not tag res IDs for: "
+ smaliDir.getAbsolutePath(), ex);
} catch (DirectoryException ex) {
throw new AndrolibException("Could not tag res IDs for: "
+ smaliDir.getAbsolutePath(), ex);
} catch (IOException | DirectoryException ex) {
throw new AndrolibException("Could not tag res IDs for: " + smaliDir.getAbsolutePath(), ex);
}
}
private void tagResIdsForFile(ResTable resTable, Directory dir,
String fileName) throws IOException, DirectoryException,
AndrolibException {
Iterator<String> it = IOUtils.readLines(dir.getFileInput(fileName))
.iterator();
private void tagResIdsForFile(ResTable resTable, Directory dir, String fileName)
throws IOException, DirectoryException, AndrolibException {
Iterator<String> it = IOUtils.readLines(dir.getFileInput(fileName)).iterator();
PrintWriter out = new PrintWriter(dir.getFileOutput(fileName));
while (it.hasNext()) {
String line = it.next();
@ -120,13 +102,10 @@ public class ResSmaliUpdater {
if (resID != -1) {
try {
ResResSpec spec = resTable.getResSpec(resID);
out.println(String.format(RES_NAME_FORMAT,
spec.getFullName()));
out.println(String.format(RES_NAME_FORMAT, spec.getFullName()));
} catch (UndefinedResObject ex) {
if (!R_FILE_PATTERN.matcher(fileName).matches()) {
LOGGER.warning(String.format(
"Undefined resource spec in %s: 0x%08x",
fileName, resID));
LOGGER.warning(String.format("Undefined resource spec in %s: 0x%08x", fileName, resID));
}
}
}
@ -149,8 +128,10 @@ public class ResSmaliUpdater {
private final static String RES_ID_FORMAT_FIELD = ".field %s:I = 0x%08x";
private final static String RES_ID_FORMAT_CONST = " const %s, 0x%08x";
private final static Pattern RES_ID_PATTERN = Pattern
.compile("^(?:\\.field (.+?):I =| const(?:|/(?:|high)16) ([pv]\\d+?),) 0x(7[a-f]0[1-9a-f](?:|[0-9a-f]{4}))$");
private final static String RES_NAME_FORMAT = "# APKTOOL/RES_NAME: %s";
private final static Pattern RES_NAME_PATTERN = Pattern
.compile("^# APKTOOL/RES_NAME: ([a-zA-Z0-9.]+):([a-z]+)/([a-zA-Z0-9._]+)$");
@ -158,6 +139,5 @@ public class ResSmaliUpdater {
private final static Pattern R_FILE_PATTERN = Pattern
.compile(".*R\\$[a-z]+\\.smali$");
private final static Logger LOGGER = Logger.getLogger(ResSmaliUpdater.class
.getName());
private final static Logger LOGGER = Logger.getLogger(ResSmaliUpdater.class.getName());
}