Browse Source

Remove BinaryFormatter (#20455)

pull/20459/head
Julien Lebosquain 3 weeks ago
committed by GitHub
parent
commit
f07e42d2bb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 72
      src/Avalonia.Base/Input/Platform/BinaryFormatterHelper.cs
  2. 4
      src/Avalonia.Base/Input/Platform/Clipboard.cs
  3. 5
      src/Avalonia.Base/Input/Platform/DataObjectToDataTransferItemWrapper.cs
  4. 4
      src/Avalonia.Base/Input/Platform/DataTransferToDataObjectWrapper.cs

72
src/Avalonia.Base/Input/Platform/BinaryFormatterHelper.cs

@ -1,72 +0,0 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Avalonia.Compatibility;
using Avalonia.Logging;
namespace Avalonia.Input.Platform;
// TODO12: remove
[Obsolete("Remove in v12")]
internal static class BinaryFormatterHelper
{
// Compatibility with WinForms + WPF...
private static ReadOnlySpan<byte> SerializedObjectGuid
=> [
// FD9EA796-3B13-4370-A679-56106BB288FB
0x96, 0xa7, 0x9e, 0xfd,
0x13, 0x3b,
0x70, 0x43,
0xa6, 0x79, 0x56, 0x10, 0x6b, 0xb2, 0x88, 0xfb
];
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We still use BinaryFormatter for WinForms drag and drop compatibility")]
[UnconditionalSuppressMessage("Trimming", "IL3050", Justification = "We still use BinaryFormatter for WinForms drag and drop compatibility")]
public static byte[]? TrySerializeUsingBinaryFormatter(object data, DataFormat dataFormat)
{
if (!OperatingSystemEx.IsWindows())
return null;
Logger.TryGet(LogEventLevel.Warning, LogArea.Win32Platform)?.Log(
null,
"Using BinaryFormatter to serialize data format {Format}. This won't be supported in Avalonia v12. Prefer passing a byte[] or Stream instead.",
dataFormat);
var stream = new MemoryStream();
var serializedGuid = SerializedObjectGuid;
#if NET6_0_OR_GREATER
stream.Write(serializedGuid);
#else
stream.Write(serializedGuid.ToArray(), 0, serializedGuid.Length);
#endif
#pragma warning disable SYSLIB0011 // Type or member is obsolete
new BinaryFormatter().Serialize(stream, data);
#pragma warning restore SYSLIB0011 // Type or member is obsolete
return stream.GetBuffer();
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We still use BinaryFormatter for WinForms drag and drop compatibility")]
[UnconditionalSuppressMessage("Trimming", "IL3050", Justification = "We still use BinaryFormatter for WinForms drag and drop compatibility")]
public static object? TryDeserializeUsingBinaryFormatter(byte[]? bytes)
{
var serializedObjectGuid = SerializedObjectGuid;
// Our Win32 backend used to automatically serialize/deserialize objects using the BinaryFormatter.
// Only keep that behavior for compatibility with IDataObject.
if (OperatingSystemEx.IsWindows() && bytes is not null && bytes.AsSpan().StartsWith(serializedObjectGuid))
{
using var stream = new MemoryStream(bytes);
stream.Position = serializedObjectGuid.Length;
#pragma warning disable SYSLIB0011 // Type or member is obsolete
return new BinaryFormatter().Deserialize(stream);
#pragma warning restore SYSLIB0011 // Type or member is obsolete
}
return null;
}
}

4
src/Avalonia.Base/Input/Platform/Clipboard.cs

@ -75,9 +75,7 @@ internal sealed class Clipboard(IClipboardImpl clipboardImpl) : IClipboard
.ToArray(); .ToArray();
} }
var typedFormat = DataFormat.CreateBytesPlatformFormat(format); return null;
var bytes = await dataTransfer.TryGetValueAsync(typedFormat).ConfigureAwait(false);
return BinaryFormatterHelper.TryDeserializeUsingBinaryFormatter(bytes) ?? bytes;
} }
public Task<IAsyncDataTransfer?> TryGetDataAsync() public Task<IAsyncDataTransfer?> TryGetDataAsync()

5
src/Avalonia.Base/Input/Platform/DataObjectToDataTransferItemWrapper.cs

@ -1,11 +1,8 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
using Avalonia.Compatibility; using Avalonia.Compatibility;
using Avalonia.Logging;
namespace Avalonia.Input.Platform; namespace Avalonia.Input.Platform;
@ -73,7 +70,7 @@ internal sealed class DataObjectToDataTransferItemWrapper(
return buffer; return buffer;
default: default:
return BinaryFormatterHelper.TrySerializeUsingBinaryFormatter(data, format); return null;
} }
} }
} }

4
src/Avalonia.Base/Input/Platform/DataTransferToDataObjectWrapper.cs

@ -36,9 +36,7 @@ internal sealed class DataTransferToDataObjectWrapper(IDataTransfer dataTransfer
.ToArray(); .ToArray();
} }
var typedFormat = DataFormat.CreateBytesPlatformFormat(dataFormat); return null;
var bytes = DataTransfer.TryGetValue(typedFormat);
return BinaryFormatterHelper.TryDeserializeUsingBinaryFormatter(bytes) ?? bytes;
} }
} }

Loading…
Cancel
Save