Always prefer abbreviated keys, over full ones, when doing any dictionary lookups (issue 14256)

Note that issue 14256 was specifically about *inline* images, please refer to:
 - https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf#G7.1852045
 - https://www.pdfa.org/safedocs-unearths-pdf-inline-image-issue/
 - https://pdf-issues.pdfa.org/32000-2-2020/clause08.html#H8.9.7

However, during review of the initial PR in https://github.com/mozilla/pdf.js/pull/14257#issuecomment-964469710, it was suggested that we instead do this *unconditionally for all* dictionary lookups.
In addition to re-ordering the existing call-sites in the `src/core`-code, and adding non-PRODUCTION/TESTING asserts to catch future errors, for consistency a number of existing `if`/`switch`-blocks were re-factored to also check the abbreviated keys first.
This commit is contained in:
Jonas Jenwald 2021-11-09 22:39:21 +01:00
parent 4ee906adf4
commit ea1c348c67
12 changed files with 730 additions and 104 deletions

View file

@ -513,7 +513,7 @@ class Parser {
}
// Extract the name of the first (i.e. the current) image filter.
const filter = dict.get("Filter", "F");
const filter = dict.get("F", "Filter");
let filterName;
if (isName(filter)) {
filterName = filter.name;
@ -527,14 +527,21 @@ class Parser {
// Parse image stream.
const startPos = stream.pos;
let length;
if (filterName === "DCTDecode" || filterName === "DCT") {
length = this.findDCTDecodeInlineStreamEnd(stream);
} else if (filterName === "ASCII85Decode" || filterName === "A85") {
length = this.findASCII85DecodeInlineStreamEnd(stream);
} else if (filterName === "ASCIIHexDecode" || filterName === "AHx") {
length = this.findASCIIHexDecodeInlineStreamEnd(stream);
} else {
length = this.findDefaultInlineStreamEnd(stream);
switch (filterName) {
case "DCT":
case "DCTDecode":
length = this.findDCTDecodeInlineStreamEnd(stream);
break;
case "A85":
case "ASCII85Decode":
length = this.findASCII85DecodeInlineStreamEnd(stream);
break;
case "AHx":
case "ASCIIHexDecode":
length = this.findASCIIHexDecodeInlineStreamEnd(stream);
break;
default:
length = this.findDefaultInlineStreamEnd(stream);
}
let imageStream = stream.makeSubStream(startPos, length, dict);
@ -694,15 +701,12 @@ class Parser {
}
filter(stream, dict, length) {
let filter = dict.get("Filter", "F");
let params = dict.get("DecodeParms", "DP");
let filter = dict.get("F", "Filter");
let params = dict.get("DP", "DecodeParms");
if (isName(filter)) {
if (Array.isArray(params)) {
warn(
"/DecodeParms should not contain an Array, " +
"when /Filter contains a Name."
);
warn("/DecodeParms should not be an Array, when /Filter is a Name.");
}
return this.makeFilter(stream, filter.name, length, params);
}
@ -740,59 +744,60 @@ class Parser {
try {
const xrefStreamStats = this.xref.stats.streamTypes;
if (name === "FlateDecode" || name === "Fl") {
xrefStreamStats[StreamType.FLATE] = true;
if (params) {
return new PredictorStream(
new FlateStream(stream, maybeLength),
maybeLength,
params
);
}
return new FlateStream(stream, maybeLength);
}
if (name === "LZWDecode" || name === "LZW") {
xrefStreamStats[StreamType.LZW] = true;
let earlyChange = 1;
if (params) {
if (params.has("EarlyChange")) {
earlyChange = params.get("EarlyChange");
switch (name) {
case "Fl":
case "FlateDecode":
xrefStreamStats[StreamType.FLATE] = true;
if (params) {
return new PredictorStream(
new FlateStream(stream, maybeLength),
maybeLength,
params
);
}
return new PredictorStream(
new LZWStream(stream, maybeLength, earlyChange),
maybeLength,
params
);
}
return new LZWStream(stream, maybeLength, earlyChange);
}
if (name === "DCTDecode" || name === "DCT") {
xrefStreamStats[StreamType.DCT] = true;
return new JpegStream(stream, maybeLength, params);
}
if (name === "JPXDecode" || name === "JPX") {
xrefStreamStats[StreamType.JPX] = true;
return new JpxStream(stream, maybeLength, params);
}
if (name === "ASCII85Decode" || name === "A85") {
xrefStreamStats[StreamType.A85] = true;
return new Ascii85Stream(stream, maybeLength);
}
if (name === "ASCIIHexDecode" || name === "AHx") {
xrefStreamStats[StreamType.AHX] = true;
return new AsciiHexStream(stream, maybeLength);
}
if (name === "CCITTFaxDecode" || name === "CCF") {
xrefStreamStats[StreamType.CCF] = true;
return new CCITTFaxStream(stream, maybeLength, params);
}
if (name === "RunLengthDecode" || name === "RL") {
xrefStreamStats[StreamType.RLX] = true;
return new RunLengthStream(stream, maybeLength);
}
if (name === "JBIG2Decode") {
xrefStreamStats[StreamType.JBIG] = true;
return new Jbig2Stream(stream, maybeLength, params);
return new FlateStream(stream, maybeLength);
case "LZW":
case "LZWDecode":
xrefStreamStats[StreamType.LZW] = true;
let earlyChange = 1;
if (params) {
if (params.has("EarlyChange")) {
earlyChange = params.get("EarlyChange");
}
return new PredictorStream(
new LZWStream(stream, maybeLength, earlyChange),
maybeLength,
params
);
}
return new LZWStream(stream, maybeLength, earlyChange);
case "DCT":
case "DCTDecode":
xrefStreamStats[StreamType.DCT] = true;
return new JpegStream(stream, maybeLength, params);
case "JPX":
case "JPXDecode":
xrefStreamStats[StreamType.JPX] = true;
return new JpxStream(stream, maybeLength, params);
case "A85":
case "ASCII85Decode":
xrefStreamStats[StreamType.A85] = true;
return new Ascii85Stream(stream, maybeLength);
case "AHx":
case "ASCIIHexDecode":
xrefStreamStats[StreamType.AHX] = true;
return new AsciiHexStream(stream, maybeLength);
case "CCF":
case "CCITTFaxDecode":
xrefStreamStats[StreamType.CCF] = true;
return new CCITTFaxStream(stream, maybeLength, params);
case "RL":
case "RunLengthDecode":
xrefStreamStats[StreamType.RLX] = true;
return new RunLengthStream(stream, maybeLength);
case "JBIG2Decode":
xrefStreamStats[StreamType.JBIG] = true;
return new Jbig2Stream(stream, maybeLength, params);
}
warn(`Filter "${name}" is not supported.`);
return stream;