Improved file extraction error handling
This commit is contained in:
parent
24a47445f6
commit
9fa7edffbf
@ -1057,7 +1057,7 @@ export function extractJPEG(bytes, offset) {
|
|||||||
|
|
||||||
while (stream.hasMore()) {
|
while (stream.hasMore()) {
|
||||||
const marker = stream.getBytes(2);
|
const marker = stream.getBytes(2);
|
||||||
if (marker[0] !== 0xff) throw new Error("Invalid JPEG marker: " + marker);
|
if (marker[0] !== 0xff) throw new Error(`Invalid marker while parsing JPEG at pos ${stream.position}: ${marker}`);
|
||||||
|
|
||||||
let segmentSize = 0;
|
let segmentSize = 0;
|
||||||
switch (marker[1]) {
|
switch (marker[1]) {
|
||||||
@ -1609,7 +1609,7 @@ function parseDEFLATE(stream) {
|
|||||||
|
|
||||||
parseHuffmanBlock(stream, dynamicLiteralTable, dynamicDistanceTable);
|
parseHuffmanBlock(stream, dynamicLiteralTable, dynamicDistanceTable);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid block type");
|
throw new Error(`Invalid block type while parsing DEFLATE stream at pos ${stream.position}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1712,7 +1712,7 @@ function readHuffmanCode(stream, table) {
|
|||||||
const codeLength = codeWithLength >>> 16;
|
const codeLength = codeWithLength >>> 16;
|
||||||
|
|
||||||
if (codeLength > maxCodeLength) {
|
if (codeLength > maxCodeLength) {
|
||||||
throw new Error("Invalid code length: " + codeLength);
|
throw new Error(`Invalid Huffman Code length while parsing DEFLATE block at pos ${stream.position}: ${codeLength}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.moveBackwardsByBits(maxCodeLength - codeLength);
|
stream.moveBackwardsByBits(maxCodeLength - codeLength);
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
// import OperationError from "../errors/OperationError";
|
import OperationError from "../errors/OperationError";
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
import {scanForFileTypes, extractFile} from "../lib/FileType";
|
import {scanForFileTypes, extractFile} from "../lib/FileType";
|
||||||
import {FILE_SIGNATURES} from "../lib/FileSignatures";
|
import {FILE_SIGNATURES} from "../lib/FileSignatures";
|
||||||
@ -34,7 +34,13 @@ class ExtractFiles extends Operation {
|
|||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: cat === "Miscellaneous" ? false : true
|
value: cat === "Miscellaneous" ? false : true
|
||||||
};
|
};
|
||||||
});
|
}).concat([
|
||||||
|
{
|
||||||
|
name: "Ignore failed extractions",
|
||||||
|
type: "boolean",
|
||||||
|
value: "true"
|
||||||
|
}
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,7 +50,8 @@ class ExtractFiles extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const bytes = new Uint8Array(input),
|
const bytes = new Uint8Array(input),
|
||||||
categories = [];
|
categories = [],
|
||||||
|
ignoreFailedExtractions = args.pop(1);
|
||||||
|
|
||||||
args.forEach((cat, i) => {
|
args.forEach((cat, i) => {
|
||||||
if (cat) categories.push(Object.keys(FILE_SIGNATURES)[i]);
|
if (cat) categories.push(Object.keys(FILE_SIGNATURES)[i]);
|
||||||
@ -59,8 +66,13 @@ class ExtractFiles extends Operation {
|
|||||||
try {
|
try {
|
||||||
files.push(extractFile(bytes, detectedFile.fileDetails, detectedFile.offset));
|
files.push(extractFile(bytes, detectedFile.fileDetails, detectedFile.offset));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message.indexOf("No extraction algorithm available") < 0)
|
if (!ignoreFailedExtractions && err.message.indexOf("No extraction algorithm available") < 0) {
|
||||||
throw err;
|
throw new OperationError(
|
||||||
|
`Error while attempting to extract ${detectedFile.fileDetails.name} ` +
|
||||||
|
`at offset ${detectedFile.offset}:\n` +
|
||||||
|
`${err.message}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user