Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions src/iTextSharp.LGPLv2.Core/iTextSharp.LGPLv2.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
<VersionPrefix>1.7.5</VersionPrefix>
<LangVersion>latest</LangVersion>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>netstandard2.0;net40;netstandard1.3;</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net40;</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>iTextSharp.LGPLv2.Core</AssemblyName>
<PackageId>iTextSharp.LGPLv2.Core</PackageId>
<PackageTags>PdfReport;Excel;Export;iTextSharp;PDF;Report;Reporting;Persian;.NET Core;aspnetcore</PackageTags>
<PackageProjectUrl>https://github.com/VahidN/iTextSharp.LGPLv2.Core</PackageProjectUrl>
<PackageLicenseUrl>http://www.gnu.org/licenses/old-licenses/lgpl-2.0-standalone.html</PackageLicenseUrl>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.1</NetStandardImplicitPackageVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
Expand All @@ -34,22 +32,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="CoreCompat.System.Drawing" Version="1.0.0-beta006" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
<PackageReference Include="System.Xml.ReaderWriter" Version="4.3.1" />
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<DefineConstants>NETSTANDARD1_3</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Drawing.Common" Version="5.0.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
Expand Down
14 changes: 14 additions & 0 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/AcroFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,20 @@ public void MergeXfaData(XmlNode n)
}
}


/// <summary>
/// Gets the field by name, and positions.
/// </summary>
/// <param name="name">the name of the field</param>
/// <returns>arraylist with value and positions</returns>
public ArrayList GetFieldAndPositions(String name)
{
ArrayList al = new ArrayList();
al.Add(GetField(name));
al.Add(GetFieldPositions(name));
return al;
}

/// <summary>
/// Regenerates the field appearance.
/// This is usefull when you change a field property, but not its value,
Expand Down
5 changes: 4 additions & 1 deletion src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/LZWDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ public void InitializeStringTable()
/// </summary>
public void WriteString(byte[] str)
{
_uncompData.Write(str, 0, str.Length);
if (str == null)
throw new Exception("Tried to write from null location in LZWdecoder, method WriteString.");
else
_uncompData.Write(str, 0, str.Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ public void SetHashKey(int number, int generation)
Extra[2] = (byte)(number >> 16);
Extra[3] = (byte)generation;
Extra[4] = (byte)(generation >> 8);
if ((Mkey != null) || (Extra != null) || (_salt != null))
throw new Exception("Null value in PDFEncryption method SetHashKey");
md5.TransformBlock(Mkey, 0, Mkey.Length, Mkey, 0);
md5.TransformBlock(Extra, 0, Extra.Length, Extra, 0);
if (_revision == AES_128)
Expand Down
129 changes: 82 additions & 47 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.X509;
using System.Diagnostics;

namespace iTextSharp.text.pdf
{
Expand Down Expand Up @@ -106,6 +107,8 @@ public class PdfReader : IPdfViewerPreferences
private PrIndirectReference _cryptoRef;
private bool _encryptionError;
private bool _hybridXref;
private bool _bBailout = false;
private int _iPRObjectDepth = 0;
private int _lastXrefPartial = -1;
private int _objGen;
private int _objNum;
Expand Down Expand Up @@ -2356,9 +2359,15 @@ protected internal void KillXref(PdfObject obj)
protected internal PdfArray ReadArray()
{
PdfArray array = new PdfArray();
while (true)
for (int i = 0; i < 2000000; i++) //Limit for bad files that cause loop
{
_iPRObjectDepth++;
if (_iPRObjectDepth > 1000) //Check that recursion depth from ReadPrObject calls isn't too much
{
break;
}
PdfObject obj = ReadPrObject();
_iPRObjectDepth--;
int type = obj.Type;
if (-type == PrTokeniser.TK_END_ARRAY)
break;
Expand All @@ -2372,8 +2381,9 @@ protected internal PdfArray ReadArray()
protected internal PdfDictionary ReadDictionary()
{
PdfDictionary dic = new PdfDictionary();
while (true)
for (int i = 0; i < 1000000; i++) //Limit for bad files that cause loop
{
i++;
Tokens.NextValidToken();
if (Tokens.TokenType == PrTokeniser.TK_END_DIC)
break;
Expand Down Expand Up @@ -2558,6 +2568,10 @@ protected internal PdfObject ReadOneObjStm(PrStream stream, int idx)
protected internal void ReadPages()
{
catalog = trailer.GetAsDict(PdfName.Root);
if (catalog == null)
{
throw new Exception("In ItextSharp PdfReader Readpages method, catalog is null.");
}
_rootPages = catalog.GetAsDict(PdfName.Pages);
pageRefs = new PageRefs(this);
}
Expand All @@ -2570,7 +2584,8 @@ protected internal virtual void ReadPdf()
pdfVersion = Tokens.CheckPdfHeader();
try
{
ReadXref();
if (_bBailout == false)
ReadXref();
}
catch (Exception e)
{
Expand All @@ -2587,7 +2602,8 @@ protected internal virtual void ReadPdf()
}
try
{
ReadDocObj();
if (_bBailout == false)
ReadDocObj();
}
catch (Exception ne)
{
Expand Down Expand Up @@ -2628,7 +2644,8 @@ protected internal void ReadPdfPartial()
pdfVersion = Tokens.CheckPdfHeader();
try
{
ReadXref();
if (_bBailout == false)
ReadXref();
}
catch (Exception e)
{
Expand All @@ -2643,8 +2660,11 @@ protected internal void ReadPdfPartial()
throw new InvalidPdfException("Rebuild failed: " + ne.Message + "; Original message: " + e.Message);
}
}
ReadDocObjPartial();
ReadPages();
if (_bBailout == false)
{
ReadDocObjPartial();
ReadPages();
}
}
catch (IOException)
{
Expand Down Expand Up @@ -2831,18 +2851,21 @@ protected internal void ReadXref()
}
catch { }
xrefByteOffset.Add(startxref);
Xref = null;
Tokens.Seek(startxref);
trailer = ReadXrefSection();
PdfDictionary trailer2 = trailer;
while (true)
if (_bBailout == false)
{
PdfNumber prev = (PdfNumber)trailer2.Get(PdfName.Prev);
if (prev == null)
break;
xrefByteOffset.Add(prev.IntValue);
Tokens.Seek(prev.IntValue);
trailer2 = ReadXrefSection();
Xref = null;
Tokens.Seek(startxref);
trailer = ReadXrefSection();
PdfDictionary trailer2 = trailer;
while (true)
{
PdfNumber prev = (PdfNumber)trailer2.Get(PdfName.Prev);
if (prev == null)
break;
xrefByteOffset.Add(prev.IntValue);
Tokens.Seek(prev.IntValue);
trailer2 = ReadXrefSection();
}
}
}

Expand Down Expand Up @@ -3051,6 +3074,13 @@ protected internal bool ReadXRefStream(int ptr)

if (prev == -1)
return true;

//before we go on, let's make sure we haven't done this a number of times that indicates a problematic recursion loop
if (new StackTrace().FrameCount > 200)
{
_bBailout = true;
throw new StackOverflowException("Likely recursion loop issue.");
}
return ReadXRefStream(prev);
}

Expand Down Expand Up @@ -3967,7 +3997,7 @@ internal void ReadPages()
_refsp = null;
_refsn = new ArrayList();
_pageInh = new ArrayList();
iteratePages((PrIndirectReference)_reader.catalog.Get(PdfName.Pages));
iteratePages((PrIndirectReference)_reader.catalog.Get(PdfName.Pages), 0);
_pageInh = null;
_reader._rootPages.Put(PdfName.Count, new PdfNumber(_refsn.Count));
}
Expand Down Expand Up @@ -4075,43 +4105,48 @@ protected internal PrIndirectReference GetSinglePage(int n)
}
}

private void iteratePages(PrIndirectReference rpage)
private void iteratePages(PrIndirectReference rpage, int depth)
{
PdfDictionary page = (PdfDictionary)GetPdfObject(rpage);
PdfArray kidsPr = page.GetAsArray(PdfName.Kids);
if (kidsPr == null)
try
{
page.Put(PdfName.TYPE, PdfName.Page);
PdfDictionary dic = (PdfDictionary)_pageInh[_pageInh.Count - 1];
foreach (PdfName key in dic.Keys)
{
if (page.Get(key) == null)
page.Put(key, dic.Get(key));
}
if (page.Get(PdfName.Mediabox) == null)
PdfDictionary page = (PdfDictionary)GetPdfObject(rpage);
PdfArray kidsPr = page.GetAsArray(PdfName.Kids);
if (kidsPr == null)
{
PdfArray arr = new PdfArray(new[] { 0, 0, PageSize.Letter.Right, PageSize.Letter.Top });
page.Put(PdfName.Mediabox, arr);
page.Put(PdfName.TYPE, PdfName.Page);
PdfDictionary dic = (PdfDictionary)_pageInh[_pageInh.Count - 1];
foreach (PdfName key in dic.Keys)
{
if (page.Get(key) == null)
page.Put(key, dic.Get(key));
}
if (page.Get(PdfName.Mediabox) == null)
{
PdfArray arr = new PdfArray(new[] { 0, 0, PageSize.Letter.Right, PageSize.Letter.Top });
page.Put(PdfName.Mediabox, arr);
}
_refsn.Add(rpage);
}
_refsn.Add(rpage);
}
else
{
page.Put(PdfName.TYPE, PdfName.Pages);
pushPageAttributes(page);
for (int k = 0; k < kidsPr.Size; ++k)
else
{
PdfObject obj = kidsPr[k];
if (!obj.IsIndirect())
page.Put(PdfName.TYPE, PdfName.Pages);
pushPageAttributes(page);
for (int k = 0; k < kidsPr.Size; ++k)
{
while (k < kidsPr.Size)
kidsPr.Remove(k);
break;
PdfObject obj = kidsPr[k];
if (!obj.IsIndirect())
{
while (k < kidsPr.Size)
kidsPr.Remove(k);
break;
}
if (depth < 2000) //Depth limit for bad files that cause loop
iteratePages((PrIndirectReference)obj, depth + 1);
}
iteratePages((PrIndirectReference)obj);
popPageAttributes();
}
popPageAttributes();
}
catch { }
}

private void popPageAttributes()
Expand Down