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