From 421dd7484060049fe31504a03e135759a7bc4348 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sun, 9 May 2021 12:31:51 -0400 Subject: [PATCH] fix bug in encryption handler. couple of code style tweaks --- .../Encryption/EncryptionHandler.cs | 21 +- .../Filters/CcittFaxDecoderStream.cs | 18 +- .../Filters/DefaultFilterProvider.cs | 240 +++++++++--------- src/UglyToad.PdfPig/IO/StreamWrapper.cs | 29 +-- 4 files changed, 148 insertions(+), 160 deletions(-) diff --git a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs index 93d420df..130ef6c9 100644 --- a/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs +++ b/src/UglyToad.PdfPig/Encryption/EncryptionHandler.cs @@ -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); diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs b/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs index 85272670..71aff5d8 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs @@ -2,8 +2,8 @@ { using System; using System.IO; - using UglyToad.PdfPig.IO; - using UglyToad.PdfPig.Util; + using IO; + using Util; /// /// 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; diff --git a/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs b/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs index 67b1f421..050f285a 100644 --- a/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs +++ b/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs @@ -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; - /// - /// - /// The default implementation of the . - /// - public class DefaultFilterProvider : IFilterProvider - { - private readonly IReadOnlyDictionary filterInstances; - - /// - /// The single instance of this provider. - /// - 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 - { - {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} - }; - } - - /// - public IReadOnlyList GetFilters(DictionaryToken dictionary) - { - if (dictionary == null) - { - throw new ArgumentNullException(nameof(dictionary)); - } - - var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F); - if (token == null) - { - return EmptyArray.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}."); - } - } - - /// - public IReadOnlyList GetNamedFilters(IReadOnlyList names) - { - if (names == null) - { - throw new ArgumentNullException(nameof(names)); - } - - var result = new List(); - - 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; - } - - /// - public IReadOnlyList GetAllFilters() - { - return filterInstances.Values.Distinct().ToList(); - } - - } + /// + /// + /// The default implementation of the . + /// + public class DefaultFilterProvider : IFilterProvider + { + private readonly IReadOnlyDictionary filterInstances; + + /// + /// The single instance of this provider. + /// + 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 + { + {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} + }; + } + + /// + public IReadOnlyList GetFilters(DictionaryToken dictionary) + { + if (dictionary == null) + { + throw new ArgumentNullException(nameof(dictionary)); + } + + var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F); + if (token == null) + { + return EmptyArray.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}."); + } + } + + /// + public IReadOnlyList GetNamedFilters(IReadOnlyList names) + { + if (names == null) + { + throw new ArgumentNullException(nameof(names)); + } + + var result = new List(); + + 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; + } + + /// + public IReadOnlyList GetAllFilters() + { + return filterInstances.Values.Distinct().ToList(); + } + + } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig/IO/StreamWrapper.cs b/src/UglyToad.PdfPig/IO/StreamWrapper.cs index 80a4668c..b1b07ca4 100644 --- a/src/UglyToad.PdfPig/IO/StreamWrapper.cs +++ b/src/UglyToad.PdfPig/IO/StreamWrapper.cs @@ -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(); } } }