From 1d485d21e9e47be97284b6512439f28c61cdded8 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Fri, 16 Feb 2018 08:25:34 -0500 Subject: [PATCH] cleanup formatting in smali-res-tagger - experiment rewriting resIds in smali to remove need of public.xml --- .../brut/androlib/res/ResSmaliUpdater.java | 56 ++++++------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/ResSmaliUpdater.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/ResSmaliUpdater.java index 2325c17b..fcc35336 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/ResSmaliUpdater.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/ResSmaliUpdater.java @@ -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 it = IOUtils.readLines( - dir.getFileInput(fileName)).iterator(); + Iterator 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 it = IOUtils.readLines(dir.getFileInput(fileName)) - .iterator(); + private void tagResIdsForFile(ResTable resTable, Directory dir, String fileName) + throws IOException, DirectoryException, AndrolibException { + Iterator 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()); }