19 package org.sleuthkit.autopsy.modules.photoreccarver;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.logging.Level;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.NodeList;
46 class PhotoRecCarverOutputParser {
48 private final Path basePath;
49 private static final Logger logger = Logger.getLogger(PhotoRecCarverFileIngestModule.class.getName());
51 PhotoRecCarverOutputParser(Path base) {
66 List<LayoutFile> parse(File xmlInputFile,
long id, AbstractFile af)
throws FileNotFoundException, IOException {
68 final Document doc = XMLUtil.loadDoc(PhotoRecCarverOutputParser.class, xmlInputFile.toString());
73 Element root = doc.getDocumentElement();
75 logger.log(Level.SEVERE,
"Error loading config file: invalid file format (bad root).");
79 NodeList fileObjects = root.getElementsByTagName(
"fileobject");
80 final int numberOfFiles = fileObjects.getLength();
82 if (numberOfFiles == 0) {
92 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
95 List<CarvedFileContainer> carvedFileContainer =
new ArrayList<>();
97 for (
int fileIndex = 0; fileIndex < numberOfFiles; ++fileIndex) {
98 entry = (Element) fileObjects.item(fileIndex);
99 fileNames = entry.getElementsByTagName(
"filename");
100 fileSizes = entry.getElementsByTagName(
"filesize");
101 fileRanges = entry.getElementsByTagName(
"byte_run");
103 fileSize=Long.parseLong(fileSizes.item(0).getTextContent());
104 fileName=fileNames.item(0).getTextContent();
105 filePath = Paths.get(fileName);
106 if (filePath.startsWith(basePath)) {
107 fileName = filePath.getFileName().toString();
110 List<TskFileRange> tskRanges =
new ArrayList<>();
111 for (
int rangeIndex = 0; rangeIndex < fileRanges.getLength(); ++rangeIndex) {
113 Long img_offset = Long.parseLong(((Element) fileRanges.item(rangeIndex)).getAttribute(
"img_offset"));
114 Long len = Long.parseLong(((Element) fileRanges.item(rangeIndex)).getAttribute(
"len"));
117 long fileByteStart = af.convertToImgOffset(img_offset);
118 if (fileByteStart == -1) {
120 logger.log(Level.INFO,
"Error while parsing PhotoRec output for file {0}", fileName);
125 long fileByteEnd = img_offset + len;
126 if (fileByteEnd > af.getSize()) {
127 long overshoot = fileByteEnd - af.getSize();
128 if (fileSize > overshoot) {
129 fileSize = fileSize - overshoot;
136 tskRanges.add(
new TskFileRange(fileByteStart, len, rangeIndex));
139 if (!tskRanges.isEmpty()) {
140 carvedFileContainer.add(
new CarvedFileContainer(fileName, fileSize,
id, tskRanges));
143 return fileManager.addCarvedFiles(carvedFileContainer);
145 catch (NumberFormatException | TskCoreException ex) {
146 logger.log(Level.SEVERE,
"Error parsing PhotoRec output and inserting it into the database: {0}", ex);
149 List<LayoutFile> empty = Collections.emptyList();