1 /* 2 * Copyright 2016, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.dexlib2.dexbacked; 33 34 import com.google.common.collect.Lists; 35 import com.google.common.io.ByteStreams; 36 import org.jf.dexlib2.Opcodes; 37 import org.jf.dexlib2.dexbacked.DexBackedDexFile.NotADexFile; 38 import org.jf.dexlib2.dexbacked.ZipDexContainer.ZipDexFile; 39 import org.jf.dexlib2.iface.MultiDexContainer; 40 import org.jf.dexlib2.util.DexUtil; 41 import org.jf.dexlib2.util.DexUtil.InvalidFile; 42 import org.jf.dexlib2.util.DexUtil.UnsupportedFile; 43 44 import javax.annotation.Nonnull; 45 import javax.annotation.Nullable; 46 import java.io.BufferedInputStream; 47 import java.io.File; 48 import java.io.IOException; 49 import java.io.InputStream; 50 import java.util.Enumeration; 51 import java.util.List; 52 import java.util.zip.ZipEntry; 53 import java.util.zip.ZipFile; 54 55 /** 56 * Represents a zip file that contains dex files (i.e. an apk or jar file) 57 */ 58 public class ZipDexContainer implements MultiDexContainer<ZipDexFile> { 59 60 private final File zipFilePath; 61 @Nullable private final Opcodes opcodes; 62 63 /** 64 * Constructs a new ZipDexContainer for the given zip file 65 * 66 * @param zipFilePath The path to the zip file 67 */ ZipDexContainer(@onnull File zipFilePath, @Nullable Opcodes opcodes)68 public ZipDexContainer(@Nonnull File zipFilePath, @Nullable Opcodes opcodes) { 69 this.zipFilePath = zipFilePath; 70 this.opcodes = opcodes; 71 } 72 73 /** 74 * Gets a list of the names of dex files in this zip file. 75 * 76 * @return A list of the names of dex files in this zip file 77 */ getDexEntryNames()78 @Nonnull @Override public List<String> getDexEntryNames() throws IOException { 79 List<String> entryNames = Lists.newArrayList(); 80 ZipFile zipFile = getZipFile(); 81 try { 82 Enumeration<? extends ZipEntry> entriesEnumeration = zipFile.entries(); 83 84 while (entriesEnumeration.hasMoreElements()) { 85 ZipEntry entry = entriesEnumeration.nextElement(); 86 87 if (!isDex(zipFile, entry)) { 88 continue; 89 } 90 91 entryNames.add(entry.getName()); 92 } 93 94 return entryNames; 95 } finally { 96 zipFile.close(); 97 } 98 } 99 100 /** 101 * Loads a dex file from a specific named entry. 102 * 103 * @param entryName The name of the entry 104 * @return A ZipDexFile, or null if there is no entry with the given name 105 * @throws NotADexFile If the entry isn't a dex file 106 */ getEntry(@onnull String entryName)107 @Nullable @Override public ZipDexFile getEntry(@Nonnull String entryName) throws IOException { 108 ZipFile zipFile = getZipFile(); 109 try { 110 ZipEntry entry = zipFile.getEntry(entryName); 111 if (entry == null) { 112 return null; 113 } 114 115 return loadEntry(zipFile, entry); 116 } finally { 117 zipFile.close(); 118 } 119 } 120 isZipFile()121 public boolean isZipFile() { 122 ZipFile zipFile = null; 123 try { 124 zipFile = getZipFile(); 125 return true; 126 } catch (IOException ex) { 127 return false; 128 } catch (NotAZipFileException ex) { 129 return false; 130 } finally { 131 if(zipFile != null) { 132 try { 133 zipFile.close(); 134 } catch (IOException ex) { 135 // just eat it 136 } 137 } 138 } 139 } 140 141 public class ZipDexFile extends DexBackedDexFile implements MultiDexContainer.MultiDexFile { 142 143 private final String entryName; 144 ZipDexFile(@ullable Opcodes opcodes, @Nonnull byte[] buf, @Nonnull String entryName)145 protected ZipDexFile(@Nullable Opcodes opcodes, @Nonnull byte[] buf, @Nonnull String entryName) { 146 super(opcodes, buf, 0); 147 this.entryName = entryName; 148 } 149 getEntryName()150 @Nonnull @Override public String getEntryName() { 151 return entryName; 152 } 153 getContainer()154 @Nonnull @Override public MultiDexContainer getContainer() { 155 return ZipDexContainer.this; 156 } 157 } 158 isDex(@onnull ZipFile zipFile, @Nonnull ZipEntry zipEntry)159 protected boolean isDex(@Nonnull ZipFile zipFile, @Nonnull ZipEntry zipEntry) throws IOException { 160 InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); 161 try { 162 DexUtil.verifyDexHeader(inputStream); 163 } catch (NotADexFile ex) { 164 return false; 165 } catch (InvalidFile ex) { 166 return false; 167 } catch (UnsupportedFile ex) { 168 return false; 169 } finally { 170 inputStream.close(); 171 } 172 return true; 173 } 174 getZipFile()175 protected ZipFile getZipFile() throws IOException { 176 try { 177 return new ZipFile(zipFilePath); 178 } catch (IOException ex) { 179 throw new NotAZipFileException(); 180 } 181 } 182 183 @Nonnull loadEntry(@onnull ZipFile zipFile, @Nonnull ZipEntry zipEntry)184 protected ZipDexFile loadEntry(@Nonnull ZipFile zipFile, @Nonnull ZipEntry zipEntry) throws IOException { 185 InputStream inputStream = zipFile.getInputStream(zipEntry); 186 try { 187 byte[] buf = ByteStreams.toByteArray(inputStream); 188 return new ZipDexFile(opcodes, buf, zipEntry.getName()); 189 } finally { 190 inputStream.close(); 191 } 192 } 193 194 public static class NotAZipFileException extends RuntimeException { 195 } 196 } 197