From 1e68bb0ede84fbe4263c5c719fda5ca34fd1eb79 Mon Sep 17 00:00:00 2001 From: IgorEisberg Date: Sun, 10 Feb 2019 14:01:26 +0200 Subject: [PATCH 1/2] feat: Fill missing 9patch lines to fix recompile Somehow Google manages to compile MiuiSystemUI.apk (and a few other APKs I encountered) with a misformed pip_dismiss_scrim.9.png. Either way, every time I encountered such 9.patch images, the missing lines always indicated a single full div or padding. Apktool already fills missing padding lines, now it can also fill missing divs. --- .../res/decoder/Res9patchStreamDecoder.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/Res9patchStreamDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/Res9patchStreamDecoder.java index a4d4285e..a8ed5f16 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/Res9patchStreamDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/Res9patchStreamDecoder.java @@ -65,13 +65,21 @@ public class Res9patchStreamDecoder implements ResStreamDecoder { drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom); int[] xDivs = np.xDivs; - for (int i = 0; i < xDivs.length; i += 2) { - drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]); + if (xDivs.length == 0) { + drawHLine(im2, 0, 1, w); + } else { + for (int i = 0; i < xDivs.length; i += 2) { + drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]); + } } int[] yDivs = np.yDivs; - for (int i = 0; i < yDivs.length; i += 2) { - drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]); + if (yDivs.length == 0) { + drawVLine(im2, 0, 1, h); + } else { + for (int i = 0; i < yDivs.length; i += 2) { + drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]); + } } // Some images additionally use Optical Bounds From bc7302a52baa861fc5dc95fbd119c45e16ecf983 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Sun, 17 Feb 2019 13:07:32 -0500 Subject: [PATCH 2/2] test: assert that vertical/horizontal divs are added if missing --- .../androlib/decode/MissingDiv9PatchTest.java | 73 ++++++++++++++++++ .../decode/issue1522/pip_dismiss_scrim.9.png | Bin 0 -> 230 bytes 2 files changed, 73 insertions(+) create mode 100644 brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/MissingDiv9PatchTest.java create mode 100644 brut.apktool/apktool-lib/src/test/resources/decode/issue1522/pip_dismiss_scrim.9.png diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/MissingDiv9PatchTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/MissingDiv9PatchTest.java new file mode 100644 index 00000000..da913026 --- /dev/null +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/MissingDiv9PatchTest.java @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2018 Ryszard Wiśniewski + * Copyright (C) 2018 Connor Tumbleson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package brut.androlib.decode; + +import brut.androlib.BaseTest; +import brut.androlib.TestUtils; +import brut.androlib.res.decoder.Res9patchStreamDecoder; +import brut.common.BrutException; +import brut.directory.ExtFile; +import brut.util.OS; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; + +import static org.junit.Assert.*; + +public class MissingDiv9PatchTest extends BaseTest { + + @BeforeClass + public static void beforeClass() throws Exception { + TestUtils.cleanFrameworkFile(); + sTmpDir = new ExtFile(OS.createTempDirectory()); + TestUtils.copyResourceDir(MissingDiv9PatchTest.class, "decode/issue1522/", sTmpDir); + } + + @AfterClass + public static void afterClass() throws BrutException { + OS.rmdir(sTmpDir); + } + + @Test + public void assertMissingDivAdded() throws Exception { + InputStream inputStream = getFileInputStream(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + Res9patchStreamDecoder decoder = new Res9patchStreamDecoder(); + decoder.decode(inputStream, outputStream); + + BufferedImage image = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray())); + int height = image.getHeight() - 1; + + // First and last pixel will be invisible, so lets check the first column and ensure its all black + for (int y = 1; y < height; y++) { + assertEquals("y coordinate failed at: " + y, NP_COLOR, image.getRGB(0, y)); + } + + } + + private FileInputStream getFileInputStream() throws IOException { + File file = new File(sTmpDir, "pip_dismiss_scrim.9.png"); + return new FileInputStream(file.toPath().toString()); + } + + private static final int NP_COLOR = 0xff000000; +} diff --git a/brut.apktool/apktool-lib/src/test/resources/decode/issue1522/pip_dismiss_scrim.9.png b/brut.apktool/apktool-lib/src/test/resources/decode/issue1522/pip_dismiss_scrim.9.png new file mode 100644 index 0000000000000000000000000000000000000000..8bd2220ab5e3369f1b30aa70d846555df85d201b GIT binary patch literal 230 zcmeAS@N?(olHy`uVBq!ia0vp^j6jsY!2)DmIDFd^NJ-=s_~!s=hV>u<348^zcNxuK z15!G91tG}{Obm<)K#m3wqXQ%{pal$!Hl9ftKswOV#WBR<^xjF1d<=>l%yB$5qjyZuH($yiDLC!y@ypTi jGyaR^t5$He)}3UYJd1h58_8pvKo0P9^>bP0l+XkK@^3lE literal 0 HcmV?d00001