From a27ba3ff4be70a1d46e9065151ef36850686dc39 Mon Sep 17 00:00:00 2001 From: Skylot Date: Sat, 5 Feb 2022 17:37:13 +0000 Subject: [PATCH] fix(res): skip '.9.png' decode if patch data not found (#1112) --- .../main/java/jadx/api/ResourcesLoader.java | 5 +++-- .../utils/android/Res9patchStreamDecoder.java | 21 +++++++++++++------ .../jadx/gui/utils/search/ResourceIndex.java | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/jadx-core/src/main/java/jadx/api/ResourcesLoader.java b/jadx-core/src/main/java/jadx/api/ResourcesLoader.java index be3672fd..43e01c6c 100644 --- a/jadx-core/src/main/java/jadx/api/ResourcesLoader.java +++ b/jadx-core/src/main/java/jadx/api/ResourcesLoader.java @@ -126,8 +126,9 @@ public final class ResourcesLoader { if (name.endsWith(".9.png")) { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { Res9patchStreamDecoder decoder = new Res9patchStreamDecoder(); - decoder.decode(inputStream, os); - return ResContainer.decodedData(rf.getDeobfName(), os.toByteArray()); + if (decoder.decode(inputStream, os)) { + return ResContainer.decodedData(rf.getDeobfName(), os.toByteArray()); + } } catch (Exception e) { LOG.error("Failed to decode 9-patch png image, path: {}", name, e); } diff --git a/jadx-core/src/main/java/jadx/core/utils/android/Res9patchStreamDecoder.java b/jadx-core/src/main/java/jadx/core/utils/android/Res9patchStreamDecoder.java index 1c674aac..7b407d55 100644 --- a/jadx-core/src/main/java/jadx/core/utils/android/Res9patchStreamDecoder.java +++ b/jadx-core/src/main/java/jadx/core/utils/android/Res9patchStreamDecoder.java @@ -24,6 +24,8 @@ import java.io.OutputStream; import javax.imageio.ImageIO; +import org.jetbrains.annotations.Nullable; + import jadx.core.utils.exceptions.JadxRuntimeException; /** @@ -31,16 +33,19 @@ import jadx.core.utils.exceptions.JadxRuntimeException; */ public class Res9patchStreamDecoder { - public void decode(InputStream in, OutputStream out) { + public boolean decode(InputStream in, OutputStream out) { try { BufferedImage im = ImageIO.read(in); + NinePatch np = getNinePatch(in); + if (np == null) { + return false; + } int w = im.getWidth(); int h = im.getHeight(); BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB); im2.createGraphics().drawImage(im, 1, 1, w, h, null); - NinePatch np = getNinePatch(in); drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight); drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom); @@ -55,28 +60,32 @@ public class Res9patchStreamDecoder { } ImageIO.write(im2, "png", out); + return true; } catch (Exception e) { throw new JadxRuntimeException("9patch image decode error", e); } } + @Nullable private NinePatch getNinePatch(InputStream in) throws IOException { ExtDataInput di = new ExtDataInput(in); - find9patchChunk(di); + if (!find9patchChunk(di)) { + return null; + } return NinePatch.decode(di); } - private void find9patchChunk(DataInput di) throws IOException { + private boolean find9patchChunk(DataInput di) throws IOException { di.skipBytes(8); while (true) { int size; try { size = di.readInt(); } catch (IOException ex) { - throw new JadxRuntimeException("Cant find nine patch chunk", ex); + return false; } if (di.readInt() == NP_CHUNK_TYPE) { - return; + return true; } di.skipBytes(size + 4); } diff --git a/jadx-gui/src/main/java/jadx/gui/utils/search/ResourceIndex.java b/jadx-gui/src/main/java/jadx/gui/utils/search/ResourceIndex.java index fe349385..fc6fc2bb 100644 --- a/jadx-gui/src/main/java/jadx/gui/utils/search/ResourceIndex.java +++ b/jadx-gui/src/main/java/jadx/gui/utils/search/ResourceIndex.java @@ -203,7 +203,7 @@ public class ResourceIndex { resNodes.add(resNode); } } else { - LOG.debug("Resource skipped because of size limit: {} res size {} bytes", resNode, size); + LOG.debug("Resource index skipped because of size limit: {} res size {} bytes", resNode, size); } } } -- GitLab