mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
fix bug in encryption handler. couple of code style tweaks
This commit is contained in:
@@ -467,18 +467,21 @@
|
|||||||
|
|
||||||
private static StringToken GetStringTokenFromDecryptedData(byte[] data)
|
private static StringToken GetStringTokenFromDecryptedData(byte[] data)
|
||||||
{
|
{
|
||||||
if (data[0] == 0xFE && data[1] == 0xFF)
|
if (data.Length >= 2)
|
||||||
{
|
{
|
||||||
var str = Encoding.BigEndianUnicode.GetString(data).Substring(1);
|
if (data[0] == 0xFE && data[1] == 0xFF)
|
||||||
|
{
|
||||||
|
var str = Encoding.BigEndianUnicode.GetString(data).Substring(1);
|
||||||
|
|
||||||
return new StringToken(str, StringToken.Encoding.Utf16BE);
|
return new StringToken(str, StringToken.Encoding.Utf16BE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data[0] == 0xFF && data[1] == 0xFE)
|
|
||||||
{
|
|
||||||
var str = Encoding.Unicode.GetString(data).Substring(1);
|
|
||||||
|
|
||||||
return new StringToken(str, StringToken.Encoding.Utf16);
|
if (data[0] == 0xFF && data[1] == 0xFE)
|
||||||
|
{
|
||||||
|
var str = Encoding.Unicode.GetString(data).Substring(1);
|
||||||
|
|
||||||
|
return new StringToken(str, StringToken.Encoding.Utf16);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StringToken(OtherEncodings.BytesAsLatin1String(data), StringToken.Encoding.Iso88591);
|
return new StringToken(OtherEncodings.BytesAsLatin1String(data), StringToken.Encoding.Iso88591);
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using UglyToad.PdfPig.IO;
|
using IO;
|
||||||
using UglyToad.PdfPig.Util;
|
using Util;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CCITT Modified Huffman RLE, Group 3 (T4) and Group 4 (T6) fax compression.
|
/// CCITT Modified Huffman RLE, Group 3 (T4) and Group 4 (T6) fax compression.
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
private int changesReferenceRowCount;
|
private int changesReferenceRowCount;
|
||||||
private int changesCurrentRowCount;
|
private int changesCurrentRowCount;
|
||||||
|
|
||||||
private int lastChangingElement = 0;
|
private int lastChangingElement;
|
||||||
|
|
||||||
private int buffer = -1;
|
private int buffer = -1;
|
||||||
private int bufferPos = -1;
|
private int bufferPos = -1;
|
||||||
@@ -78,7 +78,7 @@
|
|||||||
{
|
{
|
||||||
DecodeRow();
|
DecodeRow();
|
||||||
}
|
}
|
||||||
catch (IOException)
|
catch (InvalidOperationException)
|
||||||
{
|
{
|
||||||
if (decodedLength != 0)
|
if (decodedLength != 0)
|
||||||
{
|
{
|
||||||
@@ -344,7 +344,7 @@
|
|||||||
|
|
||||||
if (index != columns)
|
if (index != columns)
|
||||||
{
|
{
|
||||||
throw new IOException("Sum of run-lengths does not equal scan line width: " + index + " > " + columns);
|
throw new InvalidOperationException("Sum of run-lengths does not equal scan line width: " + index + " > " + columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
decodedLength = (index + 7) / 8;
|
decodedLength = (index + 7) / 8;
|
||||||
@@ -363,7 +363,7 @@
|
|||||||
|
|
||||||
if (node == null)
|
if (node == null)
|
||||||
{
|
{
|
||||||
throw new IOException("Unknown code in Huffman RLE stream");
|
throw new InvalidOperationException("Unknown code in Huffman RLE stream");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.IsLeaf)
|
if (node.IsLeaf)
|
||||||
@@ -398,7 +398,7 @@
|
|||||||
|
|
||||||
if (buffer == -1)
|
if (buffer == -1)
|
||||||
{
|
{
|
||||||
throw new IOException("Unexpected end of Huffman RLE stream");
|
throw new InvalidOperationException("Unexpected end of Huffman RLE stream");
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferPos = 0;
|
bufferPos = 0;
|
||||||
@@ -528,7 +528,7 @@
|
|||||||
}
|
}
|
||||||
else if (next.IsLeaf)
|
else if (next.IsLeaf)
|
||||||
{
|
{
|
||||||
throw new IOException("node is leaf, no other following");
|
throw new InvalidOperationException("node is leaf, no other following");
|
||||||
}
|
}
|
||||||
|
|
||||||
current = next;
|
current = next;
|
||||||
@@ -565,7 +565,7 @@
|
|||||||
}
|
}
|
||||||
else if (next.IsLeaf)
|
else if (next.IsLeaf)
|
||||||
{
|
{
|
||||||
throw new IOException("node is leaf, no other following");
|
throw new InvalidOperationException("node is leaf, no other following");
|
||||||
}
|
}
|
||||||
|
|
||||||
current = next;
|
current = next;
|
||||||
|
|||||||
@@ -1,124 +1,124 @@
|
|||||||
namespace UglyToad.PdfPig.Filters
|
namespace UglyToad.PdfPig.Filters
|
||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Core;
|
using Core;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
using UglyToad.PdfPig.Util;
|
using UglyToad.PdfPig.Util;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default implementation of the <see cref="T:UglyToad.PdfPig.Filters.IFilterProvider" />.
|
/// The default implementation of the <see cref="T:UglyToad.PdfPig.Filters.IFilterProvider" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DefaultFilterProvider : IFilterProvider
|
public class DefaultFilterProvider : IFilterProvider
|
||||||
{
|
{
|
||||||
private readonly IReadOnlyDictionary<string, IFilter> filterInstances;
|
private readonly IReadOnlyDictionary<string, IFilter> filterInstances;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The single instance of this provider.
|
/// The single instance of this provider.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly IFilterProvider Instance = new DefaultFilterProvider();
|
public static readonly IFilterProvider Instance = new DefaultFilterProvider();
|
||||||
|
|
||||||
private DefaultFilterProvider()
|
private DefaultFilterProvider()
|
||||||
{
|
{
|
||||||
var ascii85 = new Ascii85Filter();
|
var ascii85 = new Ascii85Filter();
|
||||||
var asciiHex = new AsciiHexDecodeFilter();
|
var asciiHex = new AsciiHexDecodeFilter();
|
||||||
var ccitt = new CcittFaxDecodeFilter();
|
var ccitt = new CcittFaxDecodeFilter();
|
||||||
var dct = new DctDecodeFilter();
|
var dct = new DctDecodeFilter();
|
||||||
var flate = new FlateFilter();
|
var flate = new FlateFilter();
|
||||||
var jbig2 = new Jbig2DecodeFilter();
|
var jbig2 = new Jbig2DecodeFilter();
|
||||||
var jpx = new JpxDecodeFilter();
|
var jpx = new JpxDecodeFilter();
|
||||||
var runLength = new RunLengthFilter();
|
var runLength = new RunLengthFilter();
|
||||||
var lzw = new LzwFilter();
|
var lzw = new LzwFilter();
|
||||||
|
|
||||||
filterInstances = new Dictionary<string, IFilter>
|
filterInstances = new Dictionary<string, IFilter>
|
||||||
{
|
{
|
||||||
{NameToken.Ascii85Decode.Data, ascii85},
|
{NameToken.Ascii85Decode.Data, ascii85},
|
||||||
{NameToken.Ascii85DecodeAbbreviation.Data, ascii85},
|
{NameToken.Ascii85DecodeAbbreviation.Data, ascii85},
|
||||||
{NameToken.AsciiHexDecode.Data, asciiHex},
|
{NameToken.AsciiHexDecode.Data, asciiHex},
|
||||||
{NameToken.AsciiHexDecodeAbbreviation.Data, asciiHex},
|
{NameToken.AsciiHexDecodeAbbreviation.Data, asciiHex},
|
||||||
{NameToken.CcittfaxDecode.Data, ccitt},
|
{NameToken.CcittfaxDecode.Data, ccitt},
|
||||||
{NameToken.CcittfaxDecodeAbbreviation.Data, ccitt},
|
{NameToken.CcittfaxDecodeAbbreviation.Data, ccitt},
|
||||||
{NameToken.DctDecode.Data, dct},
|
{NameToken.DctDecode.Data, dct},
|
||||||
{NameToken.DctDecodeAbbreviation.Data, dct},
|
{NameToken.DctDecodeAbbreviation.Data, dct},
|
||||||
{NameToken.FlateDecode.Data, flate},
|
{NameToken.FlateDecode.Data, flate},
|
||||||
{NameToken.FlateDecodeAbbreviation.Data, flate},
|
{NameToken.FlateDecodeAbbreviation.Data, flate},
|
||||||
{NameToken.Jbig2Decode.Data, jbig2},
|
{NameToken.Jbig2Decode.Data, jbig2},
|
||||||
{NameToken.JpxDecode.Data, jpx},
|
{NameToken.JpxDecode.Data, jpx},
|
||||||
{NameToken.RunLengthDecode.Data, runLength},
|
{NameToken.RunLengthDecode.Data, runLength},
|
||||||
{NameToken.RunLengthDecodeAbbreviation.Data, runLength},
|
{NameToken.RunLengthDecodeAbbreviation.Data, runLength},
|
||||||
{NameToken.LzwDecode, lzw},
|
{NameToken.LzwDecode, lzw},
|
||||||
{NameToken.LzwDecodeAbbreviation, lzw}
|
{NameToken.LzwDecodeAbbreviation, lzw}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyList<IFilter> GetFilters(DictionaryToken dictionary)
|
public IReadOnlyList<IFilter> GetFilters(DictionaryToken dictionary)
|
||||||
{
|
{
|
||||||
if (dictionary == null)
|
if (dictionary == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(dictionary));
|
throw new ArgumentNullException(nameof(dictionary));
|
||||||
}
|
}
|
||||||
|
|
||||||
var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F);
|
var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F);
|
||||||
if (token == null)
|
if (token == null)
|
||||||
{
|
{
|
||||||
return EmptyArray<IFilter>.Instance;
|
return EmptyArray<IFilter>.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (token)
|
switch (token)
|
||||||
{
|
{
|
||||||
case ArrayToken filters:
|
case ArrayToken filters:
|
||||||
var result = new IFilter[filters.Data.Count];
|
var result = new IFilter[filters.Data.Count];
|
||||||
for (var i = 0; i < filters.Data.Count; i++)
|
for (var i = 0; i < filters.Data.Count; i++)
|
||||||
{
|
{
|
||||||
var filterToken = filters.Data[i];
|
var filterToken = filters.Data[i];
|
||||||
var filterName = ((NameToken) filterToken).Data;
|
var filterName = ((NameToken) filterToken).Data;
|
||||||
result[i] = GetFilterStrict(filterName);
|
result[i] = GetFilterStrict(filterName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
case NameToken name:
|
case NameToken name:
|
||||||
return new[] { GetFilterStrict(name.Data) };
|
return new[] { GetFilterStrict(name.Data) };
|
||||||
default:
|
default:
|
||||||
throw new PdfDocumentFormatException($"The filter for the stream was not a valid object. Expected name or array, instead got: {token}.");
|
throw new PdfDocumentFormatException($"The filter for the stream was not a valid object. Expected name or array, instead got: {token}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyList<IFilter> GetNamedFilters(IReadOnlyList<NameToken> names)
|
public IReadOnlyList<IFilter> GetNamedFilters(IReadOnlyList<NameToken> names)
|
||||||
{
|
{
|
||||||
if (names == null)
|
if (names == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(names));
|
throw new ArgumentNullException(nameof(names));
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new List<IFilter>();
|
var result = new List<IFilter>();
|
||||||
|
|
||||||
foreach (var name in names)
|
foreach (var name in names)
|
||||||
{
|
{
|
||||||
result.Add(GetFilterStrict(name));
|
result.Add(GetFilterStrict(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IFilter GetFilterStrict(string name)
|
private IFilter GetFilterStrict(string name)
|
||||||
{
|
{
|
||||||
if (!filterInstances.TryGetValue(name, out var factory))
|
if (!filterInstances.TryGetValue(name, out var factory))
|
||||||
{
|
{
|
||||||
throw new NotSupportedException($"The filter with the name {name} is not supported yet. Please raise an issue.");
|
throw new NotSupportedException($"The filter with the name {name} is not supported yet. Please raise an issue.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyList<IFilter> GetAllFilters()
|
public IReadOnlyList<IFilter> GetAllFilters()
|
||||||
{
|
{
|
||||||
return filterInstances.Values.Distinct().ToList();
|
return filterInstances.Values.Distinct().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,39 +36,24 @@
|
|||||||
Stream.Write(buffer, offset, count);
|
Stream.Write(buffer, offset, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool CanRead
|
public override bool CanRead => Stream.CanRead;
|
||||||
{
|
|
||||||
get { return Stream.CanRead; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool CanSeek
|
public override bool CanSeek => Stream.CanSeek;
|
||||||
{
|
|
||||||
get { return Stream.CanSeek; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool CanWrite
|
public override bool CanWrite => Stream.CanWrite;
|
||||||
{
|
|
||||||
get { return Stream.CanWrite; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Length
|
public override long Length => Stream.Length;
|
||||||
{
|
|
||||||
get { return Stream.Length; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Position
|
public override long Position
|
||||||
{
|
{
|
||||||
get { return Stream.Position; }
|
get => Stream.Position;
|
||||||
set { Stream.Position = value; }
|
set => Stream.Position = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
base.Dispose(disposing);
|
base.Dispose(disposing);
|
||||||
// dispose stream
|
Stream?.Dispose();
|
||||||
using (Stream)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user