Skip to content

Commit

Permalink
Merge pull request #503 from Shane32/ifdef
Browse files Browse the repository at this point in the history
Cleanup ifdefs and revise hashing algorithm
  • Loading branch information
codebude committed Apr 26, 2024
2 parents 6ec439e + 6550b8d commit 940e9d8
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 93 deletions.
5 changes: 1 addition & 4 deletions QRCoder.Xaml/XamlQRCode.cs
@@ -1,5 +1,4 @@
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
using System;
using System;
using System.Windows;
using System.Windows.Media;
using static QRCoder.QRCodeGenerator;
Expand Down Expand Up @@ -93,5 +92,3 @@ public static DrawingImage GetQRCode(string plainText, int pixelsPerModule, stri
}
}
}

#endif
6 changes: 3 additions & 3 deletions QRCoder/ArtQRCode.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING

using System;
using System.Drawing;
Expand All @@ -9,7 +9,7 @@
// pull request raised to extend library used.
namespace QRCoder
{
#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public class ArtQRCode : AbstractQRCode, IDisposable
Expand Down Expand Up @@ -258,7 +258,7 @@ public enum BackgroundImageStyle
}
}

#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class ArtQRCodeHelper
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/Base64QRCode.cs
Expand Up @@ -62,7 +62,7 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
return Convert.ToBase64String(pngData, Base64FormattingOptions.None);
}

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
#pragma warning disable CA1416 // Validate platform compatibility
var qr = new QRCode(QrCodeData);
var base64 = string.Empty;
Expand All @@ -77,7 +77,7 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
#endif
}

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Expand All @@ -93,7 +93,7 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
}
#endif

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Expand Down
14 changes: 5 additions & 9 deletions QRCoder/PayloadGenerator.cs
Expand Up @@ -2516,14 +2516,10 @@ public override string ToString()
var cp = characterSet.ToString().Replace("_", "-");
var bytes = ToBytes();

#if !NET35_OR_GREATER && !NETSTANDARD1_3_OR_GREATER
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
#if NETSTANDARD1_3
return Encoding.GetEncoding(cp).GetString(bytes,0,bytes.Length);
#else
return Encoding.GetEncoding(cp).GetString(bytes);
#if !NETFRAMEWORK
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
return Encoding.GetEncoding(cp).GetString(bytes, 0, bytes.Length);
}

/// <summary>
Expand All @@ -2536,7 +2532,7 @@ public byte[] ToBytes()
{
//Setup byte encoder
//Encode return string as byte[] with correct CharacterSet
#if !NET35_OR_GREATER
#if !NETFRAMEWORK
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
var cp = this.characterSet.ToString().Replace("_", "-");
Expand Down Expand Up @@ -3115,7 +3111,7 @@ private static string ConvertStringToEncoding(string message, string encoding)
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(message);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
return iso.GetString(isoBytes, 0, isoBytes.Length);
return iso.GetString(isoBytes);
}

private static string EscapeInput(string inp, bool simple = false)
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/PdfByteQRCode.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
Expand All @@ -11,7 +11,7 @@
namespace QRCoder
{

#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
// ReSharper disable once InconsistentNaming
Expand Down Expand Up @@ -213,7 +213,7 @@ public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
}
}

#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class PdfByteQRCodeHelper
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/QRCode.cs
@@ -1,12 +1,12 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using static QRCoder.QRCodeGenerator;

namespace QRCoder
{
#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public class QRCode : AbstractQRCode, IDisposable
Expand Down Expand Up @@ -128,7 +128,7 @@ internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadi
}
}

#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class QRCodeHelper
Expand Down
6 changes: 2 additions & 4 deletions QRCoder/QRCodeData.cs
Expand Up @@ -21,11 +21,11 @@ public QRCodeData(int version)
for (var i = 0; i < size; i++)
this.ModuleMatrix.Add(new BitArray(size));
}
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0

public QRCodeData(string pathToRawData, Compression compressMode) : this(File.ReadAllBytes(pathToRawData), compressMode)
{
}
#endif

public QRCodeData(byte[] rawData, Compression compressMode)
{
var bytes = new List<byte>(rawData);
Expand Down Expand Up @@ -154,12 +154,10 @@ public byte[] GetRawData(Compression compressMode)
return rawData;
}

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0
public void SaveRawData(string filePath, Compression compressMode)
{
File.WriteAllBytes(filePath, GetRawData(compressMode));
}
#endif

public int Version { get; private set; }

Expand Down
7 changes: 1 addition & 6 deletions QRCoder/QRCodeGenerator.cs
Expand Up @@ -1026,8 +1026,7 @@ private static bool IsUtf8(EncodingMode encoding, string plainText, bool forceUt
private static bool IsValidISO(string input)
{
var bytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(input);
//var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes,0,bytes.Length);
var result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
return String.Equals(input, result);
}

Expand Down Expand Up @@ -1107,11 +1106,7 @@ private static string ConvertToIso8859(string value, string Iso = "ISO-8859-2")
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(value);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0
return iso.GetString(isoBytes);
#else
return iso.GetString(isoBytes, 0, isoBytes.Length);
#endif
}

private static string PlainTextToBinaryByte(string plainText, EciMode eciMode, bool utf8BOM, bool forceUtf8)
Expand Down
8 changes: 5 additions & 3 deletions QRCoder/QRCoder.csproj
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net35;net40;netstandard1.3;netstandard2.0;net5.0;net5.0-windows;net6.0;net6.0-windows</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<DefineConstants Condition="'$(TargetFramework)' != 'net6.0' AND '$(TargetFramework)' != 'netstandard1.3'">$(DefineConstants);SYSTEM_DRAWING</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net5.0-windows'">$(DefineConstants);NET5_0_WINDOWS</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
Expand Down Expand Up @@ -48,9 +49,10 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net5.0' or '$(TargetFramework)' == 'net5.0-windows' ">
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0-windows' ">
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0-windows' ">
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>

<PropertyGroup>
<FrameworkPathOverride Condition="'$(TargetFramework)' == 'net35'">$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/SvgQRCode.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0_OR_GREATER
#if !NETSTANDARD1_3
using QRCoder.Extensions;
using System;
using System.Collections;
Expand Down Expand Up @@ -267,14 +267,14 @@ public class SvgLogo
private object _logoRaw;
private bool _isEmbedded;

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
/// <summary>
/// Create a logo object to be used in SvgQRCode renderer
/// </summary>
/// <param name="iconRasterized">Logo to be rendered as Bitmap/rasterized graphic</param>
/// <param name="iconSizePercent">Degree of percentage coverage of the QR code by the logo</param>
/// <param name="fillLogoBackground">If true, the background behind the logo will be cleaned</param>
#if NET6_0_WINDOWS
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public SvgLogo(Bitmap iconRasterized, int iconSizePercent = 15, bool fillLogoBackground = true)
Expand Down
12 changes: 6 additions & 6 deletions QRCoderTests/ArtQRCodeRendererTests.cs
@@ -1,4 +1,4 @@
#if !NETCOREAPP1_1 && !NET6_0
#if SYSTEM_DRAWING

using Xunit;
using QRCoder;
Expand All @@ -23,7 +23,7 @@ public void can_create_standard_qrcode_graphic()
var bmp = new ArtQRCode(data).GetGraphic(10);

var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("cb38c3156eaf13cdfba699bdafc3a84c");
result.ShouldBe("df510ce9feddc0dd8c23c54e700abbf0");
}

[Fact]
Expand All @@ -40,7 +40,7 @@ public void can_create_standard_qrcode_graphic_with_custom_finder()
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, finderPatternImage: finder);

var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("5df3f2892eeb01e9c282ad10f642dec2");
result.ShouldBe("e28a3779b9b975b85984e36f596c9a35");
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public void can_create_standard_qrcode_graphic_without_quietzone()
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, drawQuietZones: false);

var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("632315c8695416fc82fe06a202688433");
result.ShouldBe("54408da26852d6c67ab7cad2656da7fa");
}

[Fact]
Expand All @@ -66,7 +66,7 @@ public void can_create_standard_qrcode_graphic_with_background()

var result = HelperFunctions.BitmapToHash(bmp);

result.ShouldBe("bbea08507282773175cfe7b52f0ddae4");
result.ShouldBe("7f039ccde219ae78e4f768466376a17f");
}

[Fact]
Expand Down Expand Up @@ -99,7 +99,7 @@ public void can_render_artqrcode_from_helper()
//Create QR code
var bmp = ArtQRCodeHelper.GetQRCode("A", 10, Color.Black, Color.White, Color.Transparent, QRCodeGenerator.ECCLevel.L);
var result = HelperFunctions.BitmapToHash(bmp);
result.ShouldBe("57ecaa9bdeadcdcbeac8a19d734907ff");
result.ShouldBe("a1975852df9b537344468bd44d54abe0");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion QRCoderTests/Base64QRCodeRendererTests.cs
Expand Up @@ -55,7 +55,7 @@ public void can_render_base64_qrcode_transparent()
base64QRCode.ShouldBe(Convert.ToBase64String(pngCodeGfx));
}

#if NETFRAMEWORK || NETCOREAPP2_0 || NET5_0 || NET6_0_WINDOWS
#if SYSTEM_DRAWING
[Fact]
[Category("QRRenderer/Base64QRCode")]
public void can_render_base64_qrcode_jpeg()
Expand Down
4 changes: 2 additions & 2 deletions QRCoderTests/Helpers/CategoryDiscoverer.cs
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if !NET35 && !NET452
#if !NETFRAMEWORK
using Xunit.Abstractions;
#endif
using Xunit.Sdk;

namespace QRCoderTests.Helpers.XUnitExtenstions
{
#if NET35 || NET452
#if NETFRAMEWORK
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CategoryAttribute : Attribute
{
Expand Down

0 comments on commit 940e9d8

Please sign in to comment.