From a167d4c1dd96c1bbc0616965971195db9f565df8 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Wed, 18 Dec 2019 14:54:56 +0000 Subject: [PATCH] fix bug where hex tokens for document identifier lost bytes due to encoding --- .../CrossReference/CrossReferenceTable.cs | 1 - .../CrossReference/TrailerDictionary.cs | 12 +++++----- .../Encryption/EncryptionHandler.cs | 24 ++++++++++++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs b/src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs index b12edd45..7dcc9b94 100644 --- a/src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs +++ b/src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using CrossReference; using Util.JetBrains.Annotations; /// diff --git a/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs b/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs index 13c1b233..adc207fe 100644 --- a/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs +++ b/src/UglyToad.PdfPig/CrossReference/TrailerDictionary.cs @@ -39,9 +39,9 @@ public IndirectReference? Info { get; } /// - /// A list containing two-byte strings which act as file identifiers. + /// A list containing two-byte string tokens which act as file identifiers. /// - public IReadOnlyList Identifier { get; } + public IReadOnlyList> Identifier { get; } /// /// The document's encryption dictionary. @@ -77,17 +77,17 @@ if (dictionary.TryGet(NameToken.Id, out ArrayToken arr)) { - var ids = new List(arr.Data.Count); + var ids = new List>(arr.Data.Count); foreach (var token in arr.Data) { if (token is StringToken str) { - ids.Add(str.Data); + ids.Add(str); } else if (token is HexToken hex) { - ids.Add(hex.Data); + ids.Add(hex); } } @@ -95,7 +95,7 @@ } else { - Identifier = EmptyArray.Instance; + Identifier = EmptyArray>.Instance; } if (dictionary.TryGet(NameToken.Encrypt, out var encryptionToken)) diff --git a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs index 9c5fddd0..20db5c25 100644 --- a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs +++ b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs @@ -42,9 +42,27 @@ { this.encryptionDictionary = encryptionDictionary; - var documentIdBytes = trailerDictionary.Identifier != null && trailerDictionary.Identifier.Count == 2 ? - OtherEncodings.StringAsLatin1Bytes(trailerDictionary.Identifier[0]) - : EmptyArray.Instance; + byte[] documentIdBytes; + + if (trailerDictionary.Identifier != null && trailerDictionary.Identifier.Count == 2) + { + var token = trailerDictionary.Identifier[0]; + + switch (token) + { + case HexToken hex: + documentIdBytes = hex.Bytes.ToArray(); + break; + default: + documentIdBytes = OtherEncodings.StringAsLatin1Bytes(token.Data); + break; + } + + } + else + { + documentIdBytes = EmptyArray.Instance; + } password = password ?? string.Empty;