fix bug where hex tokens for document identifier lost bytes due to encoding

This commit is contained in:
Eliot Jones
2019-12-18 14:54:56 +00:00
parent dab64ec406
commit a167d4c1dd
3 changed files with 27 additions and 10 deletions

View File

@@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using CrossReference;
using Util.JetBrains.Annotations;
/// <summary>

View File

@@ -39,9 +39,9 @@
public IndirectReference? Info { get; }
/// <summary>
/// A list containing two-byte strings which act as file identifiers.
/// A list containing two-byte string tokens which act as file identifiers.
/// </summary>
public IReadOnlyList<string> Identifier { get; }
public IReadOnlyList<IDataToken<string>> Identifier { get; }
/// <summary>
/// The document's encryption dictionary.
@@ -77,17 +77,17 @@
if (dictionary.TryGet(NameToken.Id, out ArrayToken arr))
{
var ids = new List<string>(arr.Data.Count);
var ids = new List<IDataToken<string>>(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<string>.Instance;
Identifier = EmptyArray<IDataToken<string>>.Instance;
}
if (dictionary.TryGet(NameToken.Encrypt, out var encryptionToken))

View File

@@ -42,9 +42,27 @@
{
this.encryptionDictionary = encryptionDictionary;
var documentIdBytes = trailerDictionary.Identifier != null && trailerDictionary.Identifier.Count == 2 ?
OtherEncodings.StringAsLatin1Bytes(trailerDictionary.Identifier[0])
: EmptyArray<byte>.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<byte>.Instance;
}
password = password ?? string.Empty;