mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-05-27 23:17:03 +08:00
Merge remote-tracking branch 'origin/GT-3063_dev747368_sevenzip_filesystem'
This commit is contained in:
+38
-29
@@ -15,10 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package ghidra.formats.gfilesystem;
|
package ghidra.formats.gfilesystem;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
@@ -451,15 +448,16 @@ public class FileSystemService {
|
|||||||
* lambda will be called and it will be responsible for returning an {@link InputStream}
|
* lambda will be called and it will be responsible for returning an {@link InputStream}
|
||||||
* which has the derived contents, which will be added to the file cache for next time.
|
* which has the derived contents, which will be added to the file cache for next time.
|
||||||
* <p>
|
* <p>
|
||||||
* @param fsrl {@link FSRL} of the source file that this derived file is based on.
|
* @param fsrl {@link FSRL} of the source (or container) file that this derived file is based on
|
||||||
* @param derivedName a unique string identifying the derived file.
|
* @param derivedName a unique string identifying the derived file inside the source (or container) file
|
||||||
* @param producer a {@link DerivedFileProducer callback or lambda} that returns an
|
* @param producer a {@link DerivedFileProducer callback or lambda} that returns an
|
||||||
* {@link InputStream} that will be streamed into a file and placed into the file cache.
|
* {@link InputStream} that will be streamed into a file and placed into the file cache.
|
||||||
|
* Example: <pre>(file) -> { return new XYZDecryptorInputStream(file); }</pre>
|
||||||
* @param monitor {@link TaskMonitor} that will be monitor for cancel requests and updated
|
* @param monitor {@link TaskMonitor} that will be monitor for cancel requests and updated
|
||||||
* with file io progress.
|
* with file io progress
|
||||||
* @return {@link FileCacheEntry} with file and md5 fields.
|
* @return {@link FileCacheEntry} with file and md5 fields
|
||||||
* @throws CancelledException if the user cancels.
|
* @throws CancelledException if the user cancels
|
||||||
* @throws IOException if there was an io error.
|
* @throws IOException if there was an io error
|
||||||
*/
|
*/
|
||||||
public FileCacheEntry getDerivedFile(FSRL fsrl, String derivedName,
|
public FileCacheEntry getDerivedFile(FSRL fsrl, String derivedName,
|
||||||
DerivedFileProducer producer, TaskMonitor monitor)
|
DerivedFileProducer producer, TaskMonitor monitor)
|
||||||
@@ -470,19 +468,16 @@ public class FileSystemService {
|
|||||||
// case should be okay as the only bad result will be extra
|
// case should be okay as the only bad result will be extra
|
||||||
// work being performed recreating the contents of the same derived file a second
|
// work being performed recreating the contents of the same derived file a second
|
||||||
// time.
|
// time.
|
||||||
FileCacheEntry srcCFI = getCacheFile(fsrl, monitor);
|
FileCacheEntry cacheEntry = getCacheFile(fsrl, monitor);
|
||||||
String derivedMD5 = fileCacheNameIndex.get(srcCFI.md5, derivedName);
|
String derivedMD5 = fileCacheNameIndex.get(cacheEntry.md5, derivedName);
|
||||||
FileCacheEntry derivedFile = (derivedMD5 != null) ? fileCache.getFile(derivedMD5) : null;
|
FileCacheEntry derivedFile = (derivedMD5 != null) ? fileCache.getFile(derivedMD5) : null;
|
||||||
if (derivedFile == null) {
|
if (derivedFile == null) {
|
||||||
monitor.setMessage(derivedName + " " + fsrl.getName());
|
monitor.setMessage(derivedName + " " + fsrl.getName());
|
||||||
try (InputStream is = producer.produceDerivedStream(srcCFI.file)) {
|
try (InputStream is = producer.produceDerivedStream(cacheEntry.file)) {
|
||||||
derivedFile = fileCache.addStream(is, monitor);
|
derivedFile = fileCache.addStream(is, monitor);
|
||||||
fileCacheNameIndex.add(srcCFI.md5, derivedName, derivedFile.md5);
|
fileCacheNameIndex.add(cacheEntry.md5, derivedName, derivedFile.md5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Msg.info(null, "Found derived file in cache: " + fsrl + ", " + derivedName);
|
|
||||||
}
|
|
||||||
return derivedFile;
|
return derivedFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,15 +490,15 @@ public class FileSystemService {
|
|||||||
* lambda will be called and it will be responsible for producing and writing the derived
|
* lambda will be called and it will be responsible for producing and writing the derived
|
||||||
* file's bytes to a {@link OutputStream}, which will be added to the file cache for next time.
|
* file's bytes to a {@link OutputStream}, which will be added to the file cache for next time.
|
||||||
* <p>
|
* <p>
|
||||||
* @param fsrl {@link FSRL} of the source file that this derived file is based on.
|
* @param fsrl {@link FSRL} of the source (or container) file that this derived file is based on
|
||||||
* @param derivedName a unique string identifying the derived file.
|
* @param derivedName a unique string identifying the derived file inside the source (or container) file
|
||||||
* @param pusher a {@link DerivedFilePushProducer callback or lambda} that recieves a {@link OutputStream}.
|
* @param pusher a {@link DerivedFilePushProducer callback or lambda} that recieves a {@link OutputStream}.
|
||||||
* Example: <pre>(os) -> { ...write to outputstream os here...; }</pre>
|
* Example: <pre>(os) -> { ...write to outputstream os here...; }</pre>
|
||||||
* @param monitor {@link TaskMonitor} that will be monitor for cancel requests and updated
|
* @param monitor {@link TaskMonitor} that will be monitor for cancel requests and updated
|
||||||
* with file io progress.
|
* with file io progress
|
||||||
* @return {@link FileCacheEntry} with file and md5 fields.
|
* @return {@link FileCacheEntry} with file and md5 fields
|
||||||
* @throws CancelledException if the user cancels.
|
* @throws CancelledException if the user cancels
|
||||||
* @throws IOException if there was an io error.
|
* @throws IOException if there was an io error
|
||||||
*/
|
*/
|
||||||
public FileCacheEntry getDerivedFilePush(FSRL fsrl, String derivedName,
|
public FileCacheEntry getDerivedFilePush(FSRL fsrl, String derivedName,
|
||||||
DerivedFilePushProducer pusher, TaskMonitor monitor)
|
DerivedFilePushProducer pusher, TaskMonitor monitor)
|
||||||
@@ -514,20 +509,34 @@ public class FileSystemService {
|
|||||||
// case should be okay as the only bad result will be extra
|
// case should be okay as the only bad result will be extra
|
||||||
// work being performed recreating the contents of the same derived file a second
|
// work being performed recreating the contents of the same derived file a second
|
||||||
// time.
|
// time.
|
||||||
FileCacheEntry srcCFI = getCacheFile(fsrl, monitor);
|
FileCacheEntry cacheEntry = getCacheFile(fsrl, monitor);
|
||||||
String derivedMD5 = fileCacheNameIndex.get(srcCFI.md5, derivedName);
|
String derivedMD5 = fileCacheNameIndex.get(cacheEntry.md5, derivedName);
|
||||||
FileCacheEntry derivedFile = (derivedMD5 != null) ? fileCache.getFile(derivedMD5) : null;
|
FileCacheEntry derivedFile = (derivedMD5 != null) ? fileCache.getFile(derivedMD5) : null;
|
||||||
if (derivedFile == null) {
|
if (derivedFile == null) {
|
||||||
monitor.setMessage("Caching " + fsrl.getName() + " " + derivedName);
|
monitor.setMessage("Caching " + fsrl.getName() + " " + derivedName);
|
||||||
derivedFile = fileCache.pushStream(pusher, monitor);
|
derivedFile = fileCache.pushStream(pusher, monitor);
|
||||||
fileCacheNameIndex.add(srcCFI.md5, derivedName, derivedFile.md5);
|
fileCacheNameIndex.add(cacheEntry.md5, derivedName, derivedFile.md5);
|
||||||
}
|
|
||||||
else {
|
|
||||||
Msg.info(null, "Found derived file in cache: " + fsrl + ", " + derivedName);
|
|
||||||
}
|
}
|
||||||
return derivedFile;
|
return derivedFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the specified derived file exists in the file cache.
|
||||||
|
*
|
||||||
|
* @param fsrl {@link FSRL} of the container
|
||||||
|
* @param derivedName name of the derived file inside of the container
|
||||||
|
* @param monitor {@link TaskMonitor}
|
||||||
|
* @return boolean true if file exists at time of query, false if file is not in cache
|
||||||
|
* @throws CancelledException if user cancels
|
||||||
|
* @throws IOException if other IO error
|
||||||
|
*/
|
||||||
|
public boolean hasDerivedFile(FSRL fsrl, String derivedName, TaskMonitor monitor)
|
||||||
|
throws CancelledException, IOException {
|
||||||
|
FileCacheEntry cacheEntry = getCacheFile(fsrl, monitor);
|
||||||
|
String derivedMD5 = fileCacheNameIndex.get(cacheEntry.md5, derivedName);
|
||||||
|
return derivedMD5 != null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the container file probably holds one of the currently supported
|
* Returns true if the container file probably holds one of the currently supported
|
||||||
* filesystem types.
|
* filesystem types.
|
||||||
|
|||||||
+300
-219
File diff suppressed because it is too large
Load Diff
+80
@@ -0,0 +1,80 @@
|
|||||||
|
/* ###
|
||||||
|
* IP: GHIDRA
|
||||||
|
*
|
||||||
|
* 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 ghidra.file.formats.sevenzip;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import ghidra.app.util.recognizer.*;
|
||||||
|
import ghidra.formats.gfilesystem.*;
|
||||||
|
import ghidra.formats.gfilesystem.factory.GFileSystemFactoryWithFile;
|
||||||
|
import ghidra.formats.gfilesystem.factory.GFileSystemProbeBytesOnly;
|
||||||
|
import ghidra.util.exception.CancelledException;
|
||||||
|
import ghidra.util.task.TaskMonitor;
|
||||||
|
|
||||||
|
public class SevenZipFileSystemFactory
|
||||||
|
implements GFileSystemFactoryWithFile<SevenZipFileSystem>, GFileSystemProbeBytesOnly {
|
||||||
|
|
||||||
|
private List<Recognizer> recognizers = List.of(new SevenZipRecognizer(), new XZRecognizer(),
|
||||||
|
new Bzip2Recognizer(), new MSWIMRecognizer(), new ArjRecognizer(), new CabarcRecognizer(),
|
||||||
|
new CHMRecognizer(), new CramFSRecognizer(), new DebRecognizer(), new LhaRecognizer(),
|
||||||
|
new RarRecognizer(), new RPMRecognizer(), new VHDRecognizer(), new XarRecognizer(),
|
||||||
|
new UnixCompressRecognizer());
|
||||||
|
|
||||||
|
private final int recognizerBytesRequired;
|
||||||
|
|
||||||
|
public SevenZipFileSystemFactory() {
|
||||||
|
int max = 0;
|
||||||
|
for (Recognizer recognizer : recognizers) {
|
||||||
|
max = Math.max(max, recognizer.numberOfBytesRequired());
|
||||||
|
}
|
||||||
|
recognizerBytesRequired = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBytesRequired() {
|
||||||
|
return recognizerBytesRequired;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean probeStartBytes(FSRL containerFSRL, byte[] startBytes) {
|
||||||
|
for (Recognizer recognizer : recognizers) {
|
||||||
|
String recognized = recognizer.recognize(startBytes);
|
||||||
|
if (recognized != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SevenZipFileSystem create(FSRL containerFSRL, FSRLRoot targetFSRL, File containerFile,
|
||||||
|
FileSystemService fsService, TaskMonitor monitor)
|
||||||
|
throws IOException, CancelledException {
|
||||||
|
|
||||||
|
SevenZipFileSystem fs = new SevenZipFileSystem(targetFSRL);
|
||||||
|
try {
|
||||||
|
fs.mount(containerFile, monitor);
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
catch (IOException ioe) {
|
||||||
|
fs.close();
|
||||||
|
throw ioe;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user