bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java

390 lines
15 KiB
Java
Raw Normal View History

2019-04-17 06:45:15 +00:00
package the.bytecode.club.bytecodeviewer.util;
2021-04-12 20:19:12 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
2019-04-17 09:22:59 +00:00
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import me.konloch.kontainer.io.DiskWriter;
2019-04-17 09:22:59 +00:00
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
2021-07-10 16:05:08 +00:00
import org.apache.commons.io.FilenameUtils;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
2019-04-17 06:45:15 +00:00
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
2021-06-28 04:13:55 +00:00
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
2021-07-11 09:14:42 +00:00
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
import static the.bytecode.club.bytecodeviewer.Constants.fs;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* Loading and saving jars
2018-01-31 15:41:24 +00:00
*
2021-07-10 16:05:08 +00:00
* NOTE: This is in the process of being replaced with the Import & Export API
*
* @author Konloch
* @author WaterWolf
2021-07-04 07:04:08 +00:00
* @since 09/26/2011
*/
2021-07-10 16:05:08 +00:00
@Deprecated
public class JarUtils
{
2021-07-25 16:54:08 +00:00
public static final Object LOCK = new Object();
2018-01-31 15:41:24 +00:00
/**
* Loads the classes and resources from the input jar file
*
2021-04-12 20:19:12 +00:00
* @param jarFile the input jar file
2018-01-31 15:41:24 +00:00
* @throws IOException
*/
2021-06-26 15:56:39 +00:00
public static void importArchiveA(final File jarFile) throws IOException
{
ResourceContainer container = new ResourceContainer(jarFile);
Map<String, byte[]> files = new LinkedHashMap<>();
2021-07-25 16:54:08 +00:00
try (FileInputStream fis = new FileInputStream(jarFile);
ZipInputStream jis = new ZipInputStream(fis)) {
ZipEntry entry;
while ((entry = jis.getNextEntry()) != null) {
try {
final String name = entry.getName();
final byte[] bytes = MiscUtils.getBytes(jis);
if (!name.endsWith(".class")) {
2019-04-17 09:22:59 +00:00
if (!entry.isDirectory())
files.put(name, bytes);
2021-07-25 16:54:08 +00:00
} else {
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) {
try {
final ClassNode cn = getNode(bytes);
container.resourceClasses.put(FilenameUtils.removeExtension(name), cn);
} catch (Exception e) {
System.err.println("Skipping: " + name);
e.printStackTrace();
}
} else {
if (!entry.isDirectory())
files.put(name, bytes);
//System.out.println(jarFile + ">" + name + ": Header does not start with CAFEBABE, ignoring.");
}
2018-01-31 15:41:24 +00:00
}
2021-07-25 16:54:08 +00:00
} catch (java.io.EOFException | ZipException e) {
//ignore cause apache unzip
} catch (Exception e) {
BytecodeViewer.handleException(e);
} finally {
jis.closeEntry();
}
2018-01-31 15:41:24 +00:00
}
}
2021-07-10 16:05:08 +00:00
container.resourceFiles = files;
BytecodeViewer.addResourceContainer(container);
2019-04-17 09:22:59 +00:00
}
2021-06-26 15:56:39 +00:00
/**
* A fallback solution to zip/jar archive importing if the first fails
*
* @param jarFile the input jar file
* @throws IOException
*/
public static void importArchiveB(final File jarFile) throws IOException
{
2021-04-12 20:19:12 +00:00
//if this ever fails, worst case import Sun's jarsigner code from JDK 7 re-sign the jar to rebuild the CRC,
// should also rebuild the archive byte offsets
2019-04-17 09:22:59 +00:00
ResourceContainer container = new ResourceContainer(jarFile);
Map<String, byte[]> files = new LinkedHashMap<>();
2019-04-17 09:22:59 +00:00
2021-04-12 20:19:12 +00:00
try (ZipFile zipFile = new ZipFile(jarFile)) {
2019-04-17 09:22:59 +00:00
Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
2021-04-12 20:19:12 +00:00
while (entries.hasMoreElements()) {
2019-04-17 09:22:59 +00:00
ZipArchiveEntry entry = entries.nextElement();
String name = entry.getName();
2021-04-12 22:31:22 +00:00
if (!entry.isDirectory()) {
2021-04-12 20:19:12 +00:00
try (InputStream in = zipFile.getInputStream(entry)) {
2021-07-10 16:05:08 +00:00
final byte[] bytes = MiscUtils.getBytes(in);
2019-04-25 21:27:35 +00:00
2021-04-12 20:19:12 +00:00
if (!name.endsWith(".class")) {
2019-04-17 09:22:59 +00:00
files.put(name, bytes);
2021-04-12 20:19:12 +00:00
} else {
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe"))
2021-06-28 06:45:43 +00:00
{
2021-04-12 20:19:12 +00:00
try {
2019-04-17 09:22:59 +00:00
final ClassNode cn = getNode(bytes);
2021-07-10 16:05:08 +00:00
container.resourceClasses.put(FilenameUtils.removeExtension(name), cn);
2021-04-12 20:19:12 +00:00
} catch (Exception e) {
2019-04-17 09:22:59 +00:00
e.printStackTrace();
}
2021-04-12 20:19:12 +00:00
} else {
2019-04-17 09:22:59 +00:00
files.put(name, bytes);
}
}
}
}
}
}
2021-07-10 16:05:08 +00:00
container.resourceFiles = files;
BytecodeViewer.addResourceContainer(container);
2018-01-31 15:41:24 +00:00
}
2021-06-26 15:56:39 +00:00
public static ArrayList<ClassNode> loadClasses(final File jarFile) throws IOException
{
2021-04-12 22:31:22 +00:00
ArrayList<ClassNode> classes = new ArrayList<>();
2021-07-25 16:54:08 +00:00
try (FileInputStream fis = new FileInputStream(jarFile);
ZipInputStream jis = new ZipInputStream(fis)) {
ZipEntry entry;
while ((entry = jis.getNextEntry()) != null) {
try {
final String name = entry.getName();
if (name.endsWith(".class")) {
byte[] bytes = MiscUtils.getBytes(jis);
if (MiscUtils.getFileHeaderMagicNumber(bytes).equalsIgnoreCase("cafebabe")) {
try {
final ClassNode cn = getNode(bytes);
classes.add(cn);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println(jarFile + ">" + name + ": Header does not start with CAFEBABE, ignoring.");
2018-01-31 15:41:24 +00:00
}
}
2021-07-25 16:54:08 +00:00
} catch (Exception e) {
BytecodeViewer.handleException(e);
} finally {
jis.closeEntry();
}
2018-01-31 15:41:24 +00:00
}
}
2021-07-25 16:54:08 +00:00
2018-01-31 15:41:24 +00:00
return classes;
}
/**
* Loads resources only, just for .APK
*
* @param zipFile the input zip file
* @throws IOException
*/
public static Map<String, byte[]> loadResources(final File zipFile) throws IOException {
2018-01-31 15:41:24 +00:00
if (!zipFile.exists())
return null; //just ignore
2021-07-11 08:50:12 +00:00
Map<String, byte[]> files = new LinkedHashMap<>();
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
try (ZipInputStream jis = new ZipInputStream(new FileInputStream(zipFile))) {
ZipEntry entry;
while ((entry = jis.getNextEntry()) != null) {
try {
final String name = entry.getName();
if (!name.endsWith(".class") && !name.endsWith(".dex")) {
if (!entry.isDirectory())
files.put(name, MiscUtils.getBytes(jis));
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
jis.closeEntry();
}
} catch (Exception e) {
BytecodeViewer.handleException(e);
} finally {
2018-01-31 15:41:24 +00:00
jis.closeEntry();
}
}
}
return files;
}
/**
* Creates a new ClassNode instances from the provided byte[]
*
* @param bytez the class file's byte[]
* @return the ClassNode instance
*/
public static ClassNode getNode(final byte[] bytez)
{
//TODO figure out why is this synchronized and if it's actually needed (probably not)
synchronized (LOCK)
{
2021-07-10 16:05:08 +00:00
return ASMUtil.bytesToNode(bytez);
}
2018-01-31 15:41:24 +00:00
}
/**
* Saves as jar with manifest
*
* @param nodeList the loaded ClassNodes
* @param path the exact path of the output jar file
* @param manifest the manifest contents
*/
public static void saveAsJar(ArrayList<ClassNode> nodeList, String path,
String manifest) {
2021-07-25 16:54:08 +00:00
try (FileOutputStream fos = new FileOutputStream(path);
JarOutputStream out = new JarOutputStream(fos)) {
2018-01-31 15:41:24 +00:00
for (ClassNode cn : nodeList) {
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
out.putNextEntry(new ZipEntry(cn.name + ".class"));
out.write(cw.toByteArray());
out.closeEntry();
}
out.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
out.write((manifest.trim() + "\r\n\r\n").getBytes());
out.closeEntry();
for (ResourceContainer container : BytecodeViewer.resourceContainers.values()) {
2021-07-10 16:05:08 +00:00
for (Entry<String, byte[]> entry : container.resourceFiles.entrySet()) {
2018-01-31 15:41:24 +00:00
String filename = entry.getKey();
if (!filename.startsWith("META-INF")) {
out.putNextEntry(new ZipEntry(filename));
out.write(entry.getValue());
out.closeEntry();
}
}
2021-07-25 16:54:08 +00:00
}
2018-01-31 15:41:24 +00:00
} catch (IOException e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
/**
* Saves a jar without the manifest
*
* @param nodeList The loaded ClassNodes
* @param path the exact jar output path
*/
2021-07-10 16:05:08 +00:00
public static void saveAsJarClassesOnly(Collection<ClassNode> nodeList, String path)
{
//TODO figure out why is this synchronized and if it's actually needed (probably not)
synchronized (LOCK)
{
2021-07-25 16:54:08 +00:00
try (FileOutputStream fos = new FileOutputStream(path);
JarOutputStream out = new JarOutputStream(fos))
{
ArrayList<String> noDupe = new ArrayList<>();
for (ClassNode cn : nodeList)
{
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
String name = cn.name + ".class";
if (!noDupe.contains(name))
{
noDupe.add(name);
out.putNextEntry(new ZipEntry(name));
out.write(cw.toByteArray());
out.closeEntry();
}
2018-01-31 15:41:24 +00:00
}
noDupe.clear();
}
catch (IOException e)
{
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
}
/**
* Saves a jar without the manifest
*
* @param nodeList The loaded ClassNodes
2021-04-12 20:19:12 +00:00
* @param dir the exact jar output path
2018-01-31 15:41:24 +00:00
*/
public static void saveAsJarClassesOnlyToDir(ArrayList<ClassNode> nodeList, String dir) {
try {
for (ClassNode cn : nodeList) {
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
String name = dir + fs + cn.name + ".class";
2018-01-31 15:41:24 +00:00
File f = new File(name);
f.mkdirs();
2021-07-11 16:41:33 +00:00
DiskWriter.replaceFileBytes(name, cw.toByteArray(), false);
2018-01-31 15:41:24 +00:00
}
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
/**
* Saves a jar without the manifest
*
* @param nodeList The loaded ClassNodes
* @param path the exact jar output path
*/
public static void saveAsJar(ArrayList<ClassNode> nodeList, String path) {
2021-07-25 16:54:08 +00:00
try (FileOutputStream fos = new FileOutputStream(path);
JarOutputStream out = new JarOutputStream(fos)) {
2021-04-12 22:31:22 +00:00
ArrayList<String> noDupe = new ArrayList<>();
2018-01-31 15:41:24 +00:00
for (ClassNode cn : nodeList) {
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
String name = cn.name + ".class";
if (!noDupe.contains(name)) {
noDupe.add(name);
out.putNextEntry(new ZipEntry(name));
out.write(cw.toByteArray());
out.closeEntry();
}
}
for (ResourceContainer container : BytecodeViewer.resourceContainers.values()) {
2021-07-10 16:05:08 +00:00
for (Entry<String, byte[]> entry : container.resourceFiles.entrySet()) {
2018-01-31 15:41:24 +00:00
String filename = entry.getKey();
if (!filename.startsWith("META-INF")) {
if (!noDupe.contains(filename)) {
noDupe.add(filename);
out.putNextEntry(new ZipEntry(filename));
out.write(entry.getValue());
out.closeEntry();
}
}
}
2021-07-25 16:54:08 +00:00
}
2018-01-31 15:41:24 +00:00
noDupe.clear();
} catch (IOException e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
}