2021-07-11 09:14:42 +00:00
|
|
|
package the.bytecode.club.bytecodeviewer.resources;
|
2021-07-10 16:05:08 +00:00
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
|
|
|
import org.apache.commons.compress.archivers.zip.ZipFile;
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
|
import org.objectweb.asm.tree.ClassNode;
|
|
|
|
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
|
2021-07-11 09:14:42 +00:00
|
|
|
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
|
2021-07-10 16:05:08 +00:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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/>. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2021-07-10 16:05:08 +00:00
|
|
|
/**
|
|
|
|
* @author Konloch
|
|
|
|
* @since 7/10/2021
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public class ResourceContainerImporter
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
2021-07-11 08:44:37 +00:00
|
|
|
private final ResourceContainer container;
|
2021-07-10 16:05:08 +00:00
|
|
|
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter(ResourceContainer container)
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
this.container = container;
|
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Return the linked container
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainer getContainer()
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
2021-07-11 08:44:37 +00:00
|
|
|
return container;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Start importing the container file as a file
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter importAsFile() throws IOException
|
|
|
|
{
|
|
|
|
return addUnknownFile(container.file.getName(), new FileInputStream(container.file), false);
|
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Start importing the container file as a zip archive
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter importAsZip() throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
container.resourceClasses.clear();
|
|
|
|
container.resourceClassBytes.clear();
|
|
|
|
container.resourceFiles.clear();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//attempt to import using Java ZipInputStream
|
|
|
|
importZipInputStream(false);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
//fallback to apache commons ZipFile
|
|
|
|
importApacheZipFile(false);
|
|
|
|
}
|
2021-07-11 08:44:37 +00:00
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Adds an unknown resource to the container
|
|
|
|
* This will sort the file and start the file-specific adding process
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter addUnknownFile(String name, InputStream stream, boolean classesOnly) throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
//TODO remove this .class check and just look for cafebabe
|
|
|
|
if (name.endsWith(".class"))
|
|
|
|
addClassResource(name, stream);
|
|
|
|
else if(!classesOnly)
|
|
|
|
addResource(name, stream);
|
2021-07-11 08:44:37 +00:00
|
|
|
|
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Adds a class resource to the container
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter addClassResource(String name, InputStream stream) throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
byte[] bytes = MiscUtils.getBytes(stream);
|
|
|
|
if (MiscUtils.getFileHeader(bytes).equalsIgnoreCase("cafebabe"))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final ClassNode cn = ASMUtil.bytesToNode(bytes);
|
|
|
|
|
|
|
|
//classes are copied into memory twice
|
|
|
|
ClassNode existingNode = container.resourceClasses.put(FilenameUtils.removeExtension(name), cn);
|
|
|
|
container.resourceClassBytes.put(name, bytes);
|
|
|
|
if( existingNode != null)
|
|
|
|
{
|
|
|
|
//TODO prompt to ask the user if they would like to overwrite the resource conflict
|
2021-07-13 14:33:32 +00:00
|
|
|
// or solve it automatically by creating a new resource container for each conflict (means no editing)
|
2021-07-10 16:05:08 +00:00
|
|
|
|
|
|
|
System.err.println("WARNING: Resource Conflict: " + name);
|
|
|
|
System.err.println("Suggested Fix: Contact Konloch to add support for resource conflicts");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
System.err.println(container.file + ">" + name + ": Header does not start with CAFEBABE, ignoring.");
|
|
|
|
}
|
2021-07-11 08:44:37 +00:00
|
|
|
|
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Adds a file resource to the container
|
|
|
|
*/
|
2021-07-11 08:44:37 +00:00
|
|
|
public ResourceContainerImporter addResource(String name, InputStream stream) throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
byte[] bytes = MiscUtils.getBytes(stream);
|
|
|
|
container.resourceFiles.put(name, bytes);
|
2021-07-11 08:44:37 +00:00
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Imports resources from zip archives using ZipInputStream
|
|
|
|
*/
|
|
|
|
private ResourceContainerImporter importZipInputStream(boolean classesOnly) throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
ZipInputStream jis = new ZipInputStream(new FileInputStream(container.file));
|
|
|
|
ZipEntry entry;
|
|
|
|
while ((entry = jis.getNextEntry()) != null)
|
|
|
|
{
|
|
|
|
final String name = entry.getName();
|
|
|
|
|
|
|
|
//skip directories
|
|
|
|
if(entry.isDirectory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
addUnknownFile(name, jis, classesOnly);
|
|
|
|
jis.closeEntry();
|
|
|
|
}
|
|
|
|
jis.close();
|
2021-07-11 08:44:37 +00:00
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:22:47 +00:00
|
|
|
/**
|
|
|
|
* Imports resources from zip archives using Apache ZipFile
|
|
|
|
*
|
|
|
|
* TODO if this ever fails: import Sun's jarsigner code from JDK 7, re-sign the jar to rebuild the CRC,
|
|
|
|
* should also rebuild the archive byte offsets
|
|
|
|
*/
|
|
|
|
private ResourceContainerImporter importApacheZipFile(boolean classesOnly) throws IOException
|
2021-07-10 16:05:08 +00:00
|
|
|
{
|
|
|
|
try (ZipFile zipFile = new ZipFile(container.file))
|
|
|
|
{
|
|
|
|
Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
|
|
|
|
while (entries.hasMoreElements())
|
|
|
|
{
|
|
|
|
ZipArchiveEntry entry = entries.nextElement();
|
|
|
|
String name = entry.getName();
|
|
|
|
|
|
|
|
if(entry.isDirectory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try (InputStream in = zipFile.getInputStream(entry))
|
|
|
|
{
|
|
|
|
addUnknownFile(name, in, classesOnly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 08:44:37 +00:00
|
|
|
|
|
|
|
return this;
|
2021-07-10 16:05:08 +00:00
|
|
|
}
|
|
|
|
}
|