Skip to content

Commit

Permalink
#483 make skip missing fonts even more resilient to nonsense files
Browse files Browse the repository at this point in the history
  • Loading branch information
EliotJones committed Dec 11, 2022
1 parent 2aed996 commit c8874c5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
34 changes: 34 additions & 0 deletions src/UglyToad.PdfPig.Fonts/CorruptCompressedDataException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace UglyToad.PdfPig.Fonts
{
using System;
using System.Runtime.Serialization;

/// <summary>
/// Thrown when a PDF contains an invalid compressed data stream.
/// </summary>
[Serializable]
public class CorruptCompressedDataException : Exception
{
/// <inheritdoc />
public CorruptCompressedDataException()
{
}

/// <inheritdoc />
public CorruptCompressedDataException(string message) : base(message)
{
}

/// <inheritdoc />
public CorruptCompressedDataException(string message, Exception inner) : base(message, inner)
{
}

/// <inheritdoc />
protected CorruptCompressedDataException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/IResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

internal interface IResourceStore
{
void LoadResourceDictionary(DictionaryToken resourceDictionary);
void LoadResourceDictionary(DictionaryToken resourceDictionary, InternalParsingOptions parsingOptions);

/// <summary>
/// Remove any named resources and associated state for the last resource dictionary loaded.
Expand Down
19 changes: 15 additions & 4 deletions src/UglyToad.PdfPig/Content/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ResourceStore(IPdfTokenScanner scanner, IFontFactory fontFactory)
this.fontFactory = fontFactory;
}

public void LoadResourceDictionary(DictionaryToken resourceDictionary)
public void LoadResourceDictionary(DictionaryToken resourceDictionary, InternalParsingOptions parsingOptions)
{
lastLoadedFont = (null, null);

Expand All @@ -43,7 +43,7 @@ public void LoadResourceDictionary(DictionaryToken resourceDictionary)
{
var fontDictionary = DirectObjectFinder.Get<DictionaryToken>(fontBase, scanner);

LoadFontDictionary(fontDictionary);
LoadFontDictionary(fontDictionary, parsingOptions);
}

if (resourceDictionary.TryGet(NameToken.Xobject, out var xobjectBase))
Expand Down Expand Up @@ -132,7 +132,7 @@ public void UnloadResourceDictionary()
currentResourceState.Pop();
}

private void LoadFontDictionary(DictionaryToken fontDictionary)
private void LoadFontDictionary(DictionaryToken fontDictionary, InternalParsingOptions parsingOptions)
{
lastLoadedFont = (null, null);

Expand All @@ -157,7 +157,18 @@ private void LoadFontDictionary(DictionaryToken fontDictionary)
continue;
}

loadedFonts[reference] = fontFactory.Get(fontObject);
try
{
loadedFonts[reference] = fontFactory.Get(fontObject);
}
catch
{
if (!parsingOptions.SkipMissingFonts)
{
throw;
}
}

}
else if (pair.Value is DictionaryToken fd)
{
Expand Down
14 changes: 11 additions & 3 deletions src/UglyToad.PdfPig/Filters/FlateFilter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace UglyToad.PdfPig.Filters
{
using Fonts;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -79,10 +80,17 @@ private byte[] Decompress(byte[] input)
memoryStream.ReadByte();
memoryStream.ReadByte();

using (var deflate = new DeflateStream(memoryStream, CompressionMode.Decompress))
try
{
deflate.CopyTo(output);
return output.ToArray();
using (var deflate = new DeflateStream(memoryStream, CompressionMode.Decompress))
{
deflate.CopyTo(output);
return output.ToArray();
}
}
catch (InvalidDataException ex)
{
throw new CorruptCompressedDataException("Invalid Flate compressed stream encountered", ex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private void ProcessFormXObject(StreamToken formStream)
var hasResources = formStream.StreamDictionary.TryGet<DictionaryToken>(NameToken.Resources, pdfScanner, out var formResources);
if (hasResources)
{
resourceStore.LoadResourceDictionary(formResources);
resourceStore.LoadResourceDictionary(formResources, parsingOptions);
}

// 1. Save current state.
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/Parser/PageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageT
{
var resource = pageTreeMembers.ParentResources.Dequeue();

resourceStore.LoadResourceDictionary(resource);
resourceStore.LoadResourceDictionary(resource, parsingOptions);
stackDepth++;
}

if (dictionary.TryGet(NameToken.Resources, pdfScanner, out DictionaryToken resources))
{
resourceStore.LoadResourceDictionary(resources);
resourceStore.LoadResourceDictionary(resources, parsingOptions);
stackDepth++;
}

Expand Down

0 comments on commit c8874c5

Please sign in to comment.