@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { smaliFile(file, dfv); return super.visitFile(file, attrs); } });
public static void smaliFile(String fileName, char[] data, DexFileVisitor dcv) throws IOException { // System.err.println("parsing " + f.getAbsoluteFile()); ANTLRInputStream is = new ANTLRInputStream(data, data.length); is.name = fileName; smali0(dcv, is); }
private void test0(DexWeaver iw, String prefix) throws IOException, RecognitionException { DexClassNode before = Smali.smaliFile2Node(prefix + "-before.smali", getClass() .getResourceAsStream("/weave/smali/" + prefix + "-before.smali")); DexClassNode expectedAfter = Smali.smaliFile2Node(prefix + "-after.smali", getClass() .getResourceAsStream("/weave/smali/" + prefix + "-after.smali")); DexFileNode dfn = new DexFileNode(); before.accept(iw.wrap(dfn)); assertEqual(expectedAfter, dfn.clzs.get(0)); DexClassNode expectedGen = Smali.smaliFile2Node(prefix + "-gen.j", getClass() .getResourceAsStream("/weave/smali/" + prefix + "-gen.smali")); dfn.clzs.clear(); iw.buildInvocationClz(dfn); assertEqual(expectedGen, dfn.clzs.get(0)); }
private void dotest(Path f) throws Exception { Path smali0 = new File("target/" + f.getFileName() + "-smali0.zip").toPath(); try (FileSystem fs0 = BaseCmd.createZip(smali0)) { Baksmali.from(f).to(fs0.getPath("/")); } Path smali1 = new File("target/" + f.getFileName() + "-smali1.zip").toPath(); try (FileSystem fs0 = BaseCmd.openZip(smali0); FileSystem fs1 = BaseCmd.createZip(smali1)) { BaksmaliDumper baksmaliDumper = new BaksmaliDumper(); BaksmaliDexFileVisitor v = new BaksmaliDexFileVisitor(fs1.getPath("/"), baksmaliDumper); Smali.smali(fs0.getPath("/"), v); } } }
Smali.smaliFile2Node("fake.smali", smali); DexClassNode dexClassNode2 = Smali.smaliFile2Node("fake.smali", psmali); Assert.assertEquals("cmp smalip", psmali, pbaksmali(dexClassNode2));
public static DexClassNode smaliFile2Node(String name, String buff) throws IOException { DexFileNode dfn = new DexFileNode(); smaliFile(name, buff, dfn); return dfn.clzs.size() > 0 ? dfn.clzs.get(0) : null; }
public static void smaliFile(String name, String buff, DexFileVisitor dcv) throws IOException { ANTLRInputStream is = new ANTLRInputStream(buff); is.name = name; smali0(dcv, is); }
public static DexClassNode smaliFile2Node(String name, InputStream in) throws IOException { DexFileNode dfn = new DexFileNode(); smaliFile(name, in, dfn); return dfn.clzs.size() > 0 ? dfn.clzs.get(0) : null; }
public static void smaliFile(String name, InputStream in, DexFileVisitor dcv) throws IOException { try (InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { ANTLRInputStream is = new ANTLRInputStream(reader); is.name = name; smali0(dcv, is); } }
public static void smali(Path base, final DexFileVisitor dfv) throws IOException { if (Files.isDirectory(base)) { Files.walkFileTree(base, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path fn = dir.getFileName(); if (fn != null && fn.toString().startsWith(".")) { return FileVisitResult.SKIP_SUBTREE; } return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { smaliFile(file, dfv); return super.visitFile(file, attrs); } }); } else if (Files.isRegularFile(base)) { smaliFile(base, dfv); } } }
public static void smaliFile(Path path, DexFileVisitor dcv) throws IOException { try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { ANTLRInputStream is = new ANTLRInputStream(reader); is.name = path.toString(); smali0(dcv, is); } }
@Test public void test() throws IOException { DexFileNode dfn = new DexFileNode(); try (InputStream is = SmaliTest.class.getResourceAsStream("/a.smali")) { Smali.smaliFile("a.smali", is, dfn); } for (DexClassNode dcn : dfn.clzs) { BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); new BaksmaliDumper(true, true).baksmaliClass(dcn, new BaksmaliDumpOut(w)); w.flush(); } }