#405 check encryption token value for null

This commit is contained in:
Eliot Jones
2022-01-11 16:13:52 +00:00
parent a342115b9c
commit 83948f42d7
2 changed files with 70 additions and 28 deletions

View File

@@ -417,6 +417,35 @@ endobj";
Assert.Equal(3, third.Number.ObjectNumber);
}
[Fact]
public void ReadsDictionaryContainingNull()
{
const string input = @"14224 0 obj
<</Type /XRef
/Root 8 0 R
/Prev 116
/Length 84
/Size 35
/W [1 3 2]
/Index [0 1 6 1 8 2 25 10]
/ID [ (ù¸7<C2B8>ãA×<41>žòÜ4<C39C><34>Š•)]
/Info 6 0 R
/Encrypt null>>
endobj";
var scanner = GetScanner(input);
var tokens = ReadToEnd(scanner);
var dictionaryToken = tokens[0].Data as DictionaryToken;
Assert.NotNull(dictionaryToken);
var encryptValue = dictionaryToken.Data["Encrypt"];
Assert.IsType<NullToken>(encryptValue);
}
private static PdfTokenScanner GetScanner(string s, TestObjectLocationProvider locationProvider = null)
{
var input = StringBytesTestConverter.Convert(s, false);

View File

@@ -162,17 +162,7 @@
private static (IndirectReference, DictionaryToken) ParseTrailer(CrossReferenceTable crossReferenceTable, bool isLenientParsing, IPdfTokenScanner pdfTokenScanner,
out EncryptionDictionary encryptionDictionary)
{
encryptionDictionary = null;
if (crossReferenceTable.Trailer.EncryptionToken != null)
{
if (!DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out DictionaryToken encryptionDictionaryToken))
{
throw new PdfDocumentFormatException($"Unrecognized encryption token in trailer: {crossReferenceTable.Trailer.EncryptionToken}.");
}
encryptionDictionary = EncryptionDictionaryFactory.Read(encryptionDictionaryToken, pdfTokenScanner);
}
encryptionDictionary = GetEncryptionDictionary(crossReferenceTable, pdfTokenScanner);
var rootDictionary = DirectObjectFinder.Get<DictionaryToken>(crossReferenceTable.Trailer.Root, pdfTokenScanner);
@@ -183,5 +173,28 @@
return (crossReferenceTable.Trailer.Root, rootDictionary);
}
private static EncryptionDictionary GetEncryptionDictionary(CrossReferenceTable crossReferenceTable, IPdfTokenScanner pdfTokenScanner)
{
if (crossReferenceTable.Trailer.EncryptionToken == null)
{
return null;
}
if (!DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out DictionaryToken encryptionDictionaryToken))
{
if (DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out NullToken _))
{
return null;
}
throw new PdfDocumentFormatException($"Unrecognized encryption token in trailer: {crossReferenceTable.Trailer.EncryptionToken}.");
}
var result = EncryptionDictionaryFactory.Read(encryptionDictionaryToken, pdfTokenScanner);
return result;
}
}
}