Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup ifdefs and revise hashing algorithm #503

Merged
merged 5 commits into from
Apr 26, 2024
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
5 changes: 1 addition & 4 deletions QRCoder.Xaml/XamlQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
using System;
codebude marked this conversation as resolved.
Show resolved Hide resolved
codebude marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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 @@ -80,7 +80,7 @@
using (var darkBrush = new SolidBrush(darkColor))
{
// make background transparent
using (var brush = new SolidBrush(backgroundColor))

Check warning on line 83 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'SolidBrush' is only supported on: 'windows'.
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
//Render background if set
if (backgroundImage != null)
Expand Down Expand Up @@ -143,7 +143,7 @@
{
// draw a dot
var bitmap = new Bitmap(pixelSize, pixelSize);
using (var graphics = Graphics.FromImage(bitmap))

Check warning on line 146 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Graphics.FromImage(Image)' is only supported on: 'windows'.
{
graphics.FillEllipse(brush, new Rectangle(0, 0, pixelSize, pixelSize));
graphics.Save();
Expand Down Expand Up @@ -214,14 +214,14 @@
if (image == null) return null;

float scale = Math.Min((float)newSize / image.Width, (float)newSize / image.Height);
var scaledWidth = (int)(image.Width * scale);

Check warning on line 217 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Image.Width' is only supported on: 'windows'.
var scaledHeight = (int)(image.Height * scale);

Check warning on line 218 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Image.Height' is only supported on: 'windows'.
var offsetX = (newSize - scaledWidth) / 2;
var offsetY = (newSize - scaledHeight) / 2;

var scaledImage = new Bitmap(image, new Size(scaledWidth, scaledHeight));

var bm = new Bitmap(newSize, newSize);

Check warning on line 224 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Bitmap' is only supported on: 'windows'.

using (Graphics graphics = Graphics.FromImage(bm))
{
Expand Down Expand Up @@ -258,7 +258,7 @@
}
}

#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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
codebude marked this conversation as resolved.
Show resolved Hide resolved
#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
Original file line number Diff line number Diff line change
@@ -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 @@ -80,9 +80,9 @@
using (var msJpeg = new MemoryStream())
{
// Create JPEG with specified quality
var jpgImageCodecInfo = ImageCodecInfo.GetImageEncoders().First(x => x.MimeType == "image/jpeg");

Check warning on line 83 in QRCoder/PdfByteQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'ImageCodecInfo' is only supported on: 'windows'.
var jpgEncoderParameters = new EncoderParameters(1) {
Param = new EncoderParameter[]{ new EncoderParameter(Encoder.Quality, jpgQuality) }

Check warning on line 85 in QRCoder/PdfByteQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'EncoderParameters.Param' is only supported on: 'windows'.

Check warning on line 85 in QRCoder/PdfByteQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Encoder.Quality' is only supported on: 'windows'.
};
img.Save(msJpeg, jpgImageCodecInfo, jpgEncoderParameters);
jpgArray = msJpeg.ToArray();
Expand Down Expand Up @@ -213,7 +213,7 @@
}
}

#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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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)
codebude marked this conversation as resolved.
Show resolved Hide resolved
{
}
#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)
codebude marked this conversation as resolved.
Show resolved Hide resolved
{
File.WriteAllBytes(filePath, GetRawData(compressMode));
}
#endif

public int Version { get; private set; }

Expand Down
7 changes: 1 addition & 6 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
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);
codebude marked this conversation as resolved.
Show resolved Hide resolved
#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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP1_1 && !NET6_0
#if SYSTEM_DRAWING

codebude marked this conversation as resolved.
Show resolved Hide resolved
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");
}
codebude marked this conversation as resolved.
Show resolved Hide resolved

[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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
codebude marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down
Loading
Loading