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 1 commit
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 @@ -121,7 +121,7 @@
{
var finderPatternSize = 7 * pixelsPerModule;
var finderPatternOffset = drawQuietZones ? 4 * pixelsPerModule : 0;
graphics.DrawImage(finderPatternImage, new Rectangle(finderPatternOffset, finderPatternOffset, finderPatternSize, finderPatternSize));

Check warning on line 124 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Graphics.DrawImage(Image, Rectangle)' is only supported on: 'windows'.
graphics.DrawImage(finderPatternImage, new Rectangle(size - finderPatternOffset - finderPatternSize, finderPatternOffset, finderPatternSize, finderPatternSize));
graphics.DrawImage(finderPatternImage, new Rectangle(finderPatternOffset, size - finderPatternOffset - finderPatternSize, finderPatternSize, finderPatternSize));
}
Expand Down Expand Up @@ -154,12 +154,12 @@

// center the dot in the module and crop to stay the right size.
var cropped = new Bitmap(pixelsPerModule, pixelsPerModule);
using (var graphics = Graphics.FromImage(cropped))

Check warning on line 157 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.DrawImage(bitmap, new Rectangle(margin, margin, pixelWidth, pixelWidth),

Check warning on line 159 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Graphics.DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)' is only supported on: 'windows'.
new RectangleF(((float)pixelSize - pixelWidth) / 2, ((float)pixelSize - pixelWidth) / 2, pixelWidth, pixelWidth),
GraphicsUnit.Pixel);
graphics.Save();

Check warning on line 162 in QRCoder/ArtQRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Graphics.Save()' is only supported on: 'windows'.
}

return cropped;
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 @@
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 @@
#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 @@
}
#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 @@ -103,21 +103,21 @@
ImageFormat iFormat;
switch (imgType) {
case ImageType.Png:
iFormat = ImageFormat.Png;

Check warning on line 106 in QRCoder/Base64QRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'ImageFormat.Png' is only supported on: 'windows'.
break;
case ImageType.Jpeg:
iFormat = ImageFormat.Jpeg;
break;
case ImageType.Gif:
iFormat = ImageFormat.Gif;

Check warning on line 112 in QRCoder/Base64QRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'ImageFormat.Gif' is only supported on: 'windows'.
break;
default:
iFormat = ImageFormat.Png;

Check warning on line 115 in QRCoder/Base64QRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'ImageFormat.Png' is only supported on: 'windows'.
break;
}
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, iFormat);

Check warning on line 120 in QRCoder/Base64QRCode.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on all platforms. 'Image.Save(Stream, ImageFormat)' is only supported on: 'windows'.
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
return base64;
Expand Down
10 changes: 3 additions & 7 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
#if NETCOREAPP
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);
#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 // -- why different ifdef than line 2519? --
codebude marked this conversation as resolved.
Show resolved Hide resolved
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
codebude marked this conversation as resolved.
Show resolved Hide resolved
#endif
var cp = this.characterSet.ToString().Replace("_", "-");
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 @@ -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
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
8 changes: 4 additions & 4 deletions QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public QRCodeData(int version)
for (var i = 0; i < size; i++)
this.ModuleMatrix.Add(new BitArray(size));
}
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0

[Obsolete("This constructor will be removed in version 2.0.0.")]
Shane32 marked this conversation as resolved.
Show resolved Hide resolved
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 +155,11 @@ public byte[] GetRawData(Compression compressMode)
return rawData;
}

#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0
[Obsolete("This constructor will be removed in version 2.0.0.")]
Shane32 marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 0 additions & 4 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,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
54 changes: 37 additions & 17 deletions QRCoderTests/Helpers/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Reflection;
#if !NETCOREAPP1_1
codebude marked this conversation as resolved.
Show resolved Hide resolved
using System.Drawing;
#endif
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
#if TEST_XAML
using SW = System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
Expand All @@ -17,7 +18,7 @@ namespace QRCoderTests.Helpers
public static class HelperFunctions
{

#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
#if TEST_XAML
public static BitmapSource ToBitmapSource(DrawingImage source)
{
DrawingVisual drawingVisual = new DrawingVisual();
Expand All @@ -44,34 +45,53 @@ public static Bitmap BitmapSourceToBitmap(DrawingImage xamlImg)
}
}
}
#endif
#endif

#if !NETCOREAPP1_1
public static string GetAssemblyPath()
codebude marked this conversation as resolved.
Show resolved Hide resolved
{
return
#if NET5_0 || NET6_0
AppDomain.CurrentDomain.BaseDirectory;
#elif NET35 || NET452
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
#if NET5_0_OR_GREATER
AppDomain.CurrentDomain.BaseDirectory;
#elif NETFRAMEWORK
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
#elif NETCOREAPP1_1
Path.GetDirectoryName(typeof(HelperFunctions).GetTypeInfo().Assembly.Location).Replace("file:\\", "");
#else
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Replace("file:\\", "");
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Replace("file:\\", "");
#endif
}
#endif


#if !NETCOREAPP1_1
public static string BitmapToHash(Bitmap bmp)
/// <summary>
/// Converts a bitmap to a hash string based on the pixel data
/// using a deterministic algorithm that ignores compression algorithm
/// differences across platforms.
Shane32 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static string BitmapToHash(Bitmap bitmap)
codebude marked this conversation as resolved.
Show resolved Hide resolved
{
byte[] imgBytes = null;
using (var ms = new MemoryStream())
// Lock the bitmap's bits.
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var bitmapData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

byte[] rgbValues;
try
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
imgBytes = ms.ToArray();
ms.Dispose();
// Create an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bitmapData.Stride) * bitmap.Height;
rgbValues = new byte[bytes];

// Copy the RGB values into the array.
codebude marked this conversation as resolved.
Show resolved Hide resolved
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, rgbValues, 0, bytes);
}
finally
{
// Unlock the bits.
bitmap.UnlockBits(bitmapData);
}
return ByteArrayToHash(imgBytes);

// Hash the resulting byte array
return ByteArrayToHash(rgbValues);
}
#endif

Expand Down
Loading
Loading