Skip to content

Commit

Permalink
Added .NETFramework 4.0 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Dec 22, 2017
1 parent 527230e commit df0d2ec
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public sealed class EncodingsRegistry

private EncodingsRegistry()
{
#if !NET40
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/iTextSharp.LGPLv2.Core/System/NetUtils/WebUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public static Stream GetResponseStream(this Uri url)
return new FileStream(url.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
var w = WebRequest.Create(url);
return w.GetResponseAsync().Result.GetResponseStream();
#if NET40
return w.GetResponse().GetResponseStream();
#else
return w.GetResponseAsync().GetAwaiter().GetResult().GetResponseStream();
#endif
}

public static Stream GetResponseStream(this string url)
Expand Down
2 changes: 1 addition & 1 deletion src/iTextSharp.LGPLv2.Core/_1-dotnet_pack.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dotnet pack -c release
paue
pause
2 changes: 2 additions & 0 deletions src/iTextSharp.LGPLv2.Core/_2-dotnet_build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotnet build
pause
10 changes: 8 additions & 2 deletions src/iTextSharp.LGPLv2.Core/iTextSharp.LGPLv2.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description> iTextSharp.LGPLv2.Core is an unofficial port of the last LGPL version of the iTextSharp (V4.1.6) to .NET Core.</Description>
<VersionPrefix>1.3.4</VersionPrefix>
<VersionPrefix>1.4.0</VersionPrefix>
<LangVersion>latest</LangVersion>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net40;netstandard1.3;netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>iTextSharp.LGPLv2.Core</AssemblyName>
Expand Down Expand Up @@ -56,4 +56,10 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>NETSTANDARD2_0</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net40'">
<DefineConstants>NET40</DefineConstants>
</PropertyGroup>
</Project>
8 changes: 7 additions & 1 deletion src/iTextSharp.LGPLv2.Core/iTextSharp/text/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,17 @@ public Document(Rectangle pageSize, float marginLeft, float marginRight, float m
/// </summary>
public static string Product { get; } = "iTextSharp.LGPLv2.Core";

#if NET40
/// <summary>
/// Gets the release number.
/// </summary>
public static string Release { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
#else
/// <summary>
/// Gets the release number.
/// </summary>
public static string Release { get; } = typeof(Document).GetTypeInfo().Assembly.GetName().Version.ToString();

#endif
/// <summary>
/// Returns the lower left y-coordinate.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/BaseFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Collections;
using System.util;
using iTextSharp.text.xml.simpleparser;
#if !NET40
using System.Runtime.Loader;
#endif

namespace iTextSharp.text.pdf
{
Expand Down Expand Up @@ -1114,7 +1116,11 @@ public static Stream GetResourceStream(string key)
// Try to use resource loader to load the properties file.
try
{
#if NET40
var assm = Assembly.GetExecutingAssembly();
#else
var assm = typeof(BaseFont).GetTypeInfo().Assembly;
#endif
istr = assm.GetManifestResourceStream(key);
}
catch
Expand All @@ -1138,7 +1144,11 @@ public static Stream GetResourceStream(string key)
string dir = (string)obj;
try
{
#if NET40
var asm = Assembly.LoadFrom(dir);
#else
var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(dir);
#endif
istr = asm.GetManifestResourceStream(key);
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,30 @@ public byte[] GetEncoded()
con.ContentType = "application/ocsp-request";
con.Accept = "application/ocsp-response";
con.Method = "POST";
#if NET40
Stream outp = con.GetRequestStream();
#else
Stream outp = con.GetRequestStreamAsync().Result;
#endif
outp.Write(array, 0, array.Length);
outp.Dispose();
HttpWebResponse response = (HttpWebResponse)con.GetResponseAsync().Result;

#if NET40
HttpWebResponse response = (HttpWebResponse)con.GetResponse();
#else
HttpWebResponse response = (HttpWebResponse)con.GetResponseAsync().GetAwaiter().GetResult();
#endif

if (response.StatusCode != HttpStatusCode.OK)
throw new IOException($"Invalid HTTP response: {(int)response.StatusCode}");
Stream inp = response.GetResponseStream();
OcspResp ocspResponse = new OcspResp(inp);
inp.Dispose();
#if NET40
response.Close();
#else
response.Dispose();
#endif

if (ocspResponse.Status != 0)
throw new IOException("Invalid status: " + ocspResponse.Status);
Expand Down
74 changes: 71 additions & 3 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ public PdfDictionary GetEncryptionDictionary()
}
}

#if NET40
SHA1 sh = new SHA1CryptoServiceProvider();
byte[] encodedRecipient = null;
byte[] seed = PublicKeyHandler.GetSeed();
sh.TransformBlock(seed, 0, seed.Length, seed, 0);
for (int i = 0; i < PublicKeyHandler.GetRecipientsSize(); i++)
{
encodedRecipient = PublicKeyHandler.GetEncodedRecipient(i);
sh.TransformBlock(encodedRecipient, 0, encodedRecipient.Length, encodedRecipient, 0);
}
if (!_encryptMetadata)
sh.TransformBlock(MetadataPad, 0, MetadataPad.Length, MetadataPad, 0);
sh.TransformFinalBlock(seed, 0, 0);
byte[] mdResult = sh.Hash;
#else
byte[] mdResult;
using (var sh = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
{
Expand All @@ -273,6 +288,7 @@ public PdfDictionary GetEncryptionDictionary()

mdResult = sh.GetHashAndReset();
}
#endif

SetupByEncryptionKey(mdResult, _keyLength);
}
Expand Down Expand Up @@ -377,8 +393,27 @@ public void SetCryptoMode(int mode, int kl)
throw new ArgumentException("No valid encryption mode");
}
}

public void SetHashKey(int number, int generation)
{
#if NET40
using (var md5 = new MD5CryptoServiceProvider())
{
md5.Initialize();
Extra[0] = (byte)number;
Extra[1] = (byte)(number >> 8);
Extra[2] = (byte)(number >> 16);
Extra[3] = (byte)generation;
Extra[4] = (byte)(generation >> 8);
md5.TransformBlock(Mkey, 0, Mkey.Length, Mkey, 0);
md5.TransformBlock(Extra, 0, Extra.Length, Extra, 0);
if (_revision == AES_128)
md5.TransformBlock(_salt, 0, _salt.Length, _salt, 0);

md5.TransformFinalBlock(Extra, 0, 0);
Key = md5.Hash;
}
#else
using (var md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5))
{
Extra[0] = (byte)number;
Expand All @@ -393,6 +428,7 @@ public void SetHashKey(int number, int generation)

Key = md5.GetHashAndReset();
}
#endif

KeySize = Mkey.Length + 5;
if (KeySize > 16)
Expand Down Expand Up @@ -509,7 +545,31 @@ private void setupGlobalEncryptionKey(byte[] documentId, byte[] userPad, byte[]
Permissions = permissions;
// use variable keylength
Mkey = new byte[_keyLength / 8];
byte[] digest = new byte[Mkey.Length];

#if NET40
//fixed by ujihara in order to follow PDF refrence
using (var md5 = new MD5CryptoServiceProvider())
{
md5.Initialize();
md5.TransformBlock(userPad, 0, userPad.Length, userPad, 0);
md5.TransformBlock(ownerKey, 0, ownerKey.Length, ownerKey, 0);

byte[] ext = new byte[4];
ext[0] = (byte)permissions;
ext[1] = (byte)(permissions >> 8);
ext[2] = (byte)(permissions >> 16);
ext[3] = (byte)(permissions >> 24);
md5.TransformBlock(ext, 0, 4, ext, 0);
if (documentId != null)
md5.TransformBlock(documentId, 0, documentId.Length, documentId, 0);
if (!_encryptMetadata)
md5.TransformBlock(MetadataPad, 0, MetadataPad.Length, MetadataPad, 0);
md5.TransformFinalBlock(ext, 0, 0);

Array.Copy(md5.Hash, 0, digest, 0, Mkey.Length);
}
#else
//fixed by ujihara in order to follow PDF refrence
var md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5);
md5.AppendData(userPad, 0, userPad.Length);
Expand All @@ -526,9 +586,8 @@ private void setupGlobalEncryptionKey(byte[] documentId, byte[] userPad, byte[]
if (!_encryptMetadata)
md5.AppendData(MetadataPad, 0, MetadataPad.Length);

byte[] digest = new byte[Mkey.Length];
Array.Copy(md5.GetHashAndReset(), 0, digest, 0, Mkey.Length);

#endif


// only use the really needed bits as input for the hash
Expand Down Expand Up @@ -556,13 +615,22 @@ private void setupUserKey()
if (_revision == STANDARD_ENCRYPTION_128 || _revision == AES_128)
{
byte[] digest;
#if NET40
using (var md5 = new MD5CryptoServiceProvider())
{
md5.Initialize();
md5.TransformBlock(_pad, 0, _pad.Length, _pad, 0);
md5.TransformFinalBlock(DocumentId, 0, DocumentId.Length);
digest = md5.Hash;
}
#else
using (var md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5))
{
md5.AppendData(_pad, 0, _pad.Length);
md5.AppendData(DocumentId, 0, DocumentId.Length);
digest = md5.GetHashAndReset();
}

#endif
Array.Copy(digest, 0, UserKey, 0, 16);
for (int k = 16; k < 32; ++k)
UserKey[k] = 0;
Expand Down
18 changes: 17 additions & 1 deletion src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,7 @@ protected internal void RebuildXref()
int top = 0;
trailer = null;
byte[] line = new byte[64];
for (;;)
for (; ; )
{
int pos = Tokens.FilePointer;
if (!Tokens.ReadLineSegment(line))
Expand Down Expand Up @@ -3502,7 +3502,22 @@ private void readDecryptedDocObj()
throw new UnsupportedPdfException("Bad certificate and key.");
}

#if NET40
using (var sh = new SHA1CryptoServiceProvider())
{
sh.TransformBlock(envelopedData, 0, 20, envelopedData, 0);
for (int i = 0; i < recipients.Size; i++)
{
byte[] encodedRecipient = recipients[i].GetBytes();
sh.TransformBlock(encodedRecipient, 0, encodedRecipient.Length, encodedRecipient, 0);
}
if ((cryptoMode & PdfWriter.DO_NOT_ENCRYPT_METADATA) != 0)
sh.TransformBlock(PdfEncryption.MetadataPad, 0, PdfEncryption.MetadataPad.Length, PdfEncryption.MetadataPad, 0);

sh.TransformFinalBlock(envelopedData, 0, 0);
encryptionKey = sh.Hash;
}
#else
using (var sh = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
{
sh.AppendData(envelopedData, 0, 20);
Expand All @@ -3516,6 +3531,7 @@ private void readDecryptedDocObj()

encryptionKey = sh.GetHashAndReset();
}
#endif
}
decrypt = new PdfEncryption();
decrypt.SetCryptoMode(cryptoMode, lengthValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,20 @@ protected internal virtual byte[] GetTsaResponse(byte[] requestBytes)
authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
con.Headers["Authorization"] = "Basic " + authInfo;
}

#if NET40
Stream outp = con.GetRequestStream();
#else
Stream outp = con.GetRequestStreamAsync().Result;
#endif
outp.Write(requestBytes, 0, requestBytes.Length);
outp.Dispose();
HttpWebResponse response = (HttpWebResponse)con.GetResponseAsync().Result;

#if NET40
HttpWebResponse response = (HttpWebResponse)con.GetResponse();
#else
HttpWebResponse response = (HttpWebResponse)con.GetResponseAsync().GetAwaiter().GetResult();
#endif
if (response.StatusCode != HttpStatusCode.OK)
throw new IOException("Invalid HTTP response: " + (int)response.StatusCode);
Stream inp = response.GetResponseStream();
Expand All @@ -177,7 +187,12 @@ protected internal virtual byte[] GetTsaResponse(byte[] requestBytes)
baos.Write(buffer, 0, bytesRead);
}
inp.Dispose();
#if NET40
response.Close();
#else
response.Dispose();
#endif

byte[] respBytes = baos.ToArray();

if (encoding != null && Util.EqualsIgnoreCase(encoding, "base64"))
Expand Down
4 changes: 4 additions & 0 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/XfaForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ public static byte[] SerializeDoc(XmlNode n)
};
var xw = XmlWriter.Create(fout, xwSettings);
n.WriteContentTo(xw);
#if NET40
xw.Close();
#else
xw.Dispose();
#endif
return fout.ToArray();
}

Expand Down
4 changes: 4 additions & 0 deletions src/iTextSharp.LGPLv2.Core/iTextSharp/text/xml/ParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public void Parse(XmlReader reader)
{
if (reader != null)
{
#if NET40
reader.Close();
#else
reader.Dispose();
#endif
}
}
}
Expand Down

0 comments on commit df0d2ec

Please sign in to comment.