diff --git a/src/System.Private.Windows.Core/src/System.Private.Windows.Core.csproj b/src/System.Private.Windows.Core/src/System.Private.Windows.Core.csproj
index fca56efcdfe..aed2d867893 100644
--- a/src/System.Private.Windows.Core/src/System.Private.Windows.Core.csproj
+++ b/src/System.Private.Windows.Core/src/System.Private.Windows.Core.csproj
@@ -28,9 +28,7 @@
-
-
-
+
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/AllowedRecordType.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/AllowedRecordType.cs
deleted file mode 100644
index 92f0c4ba20a..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/AllowedRecordType.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-[Flags]
-internal enum AllowedRecordTypes : uint
-{
- None = 0,
- SerializedStreamHeader = 1 << RecordType.SerializedStreamHeader,
- ClassWithId = 1 << RecordType.ClassWithId,
- SystemClassWithMembers = 1 << RecordType.SystemClassWithMembers,
- ClassWithMembers = 1 << RecordType.ClassWithMembers,
- SystemClassWithMembersAndTypes = 1 << RecordType.SystemClassWithMembersAndTypes,
- ClassWithMembersAndTypes = 1 << RecordType.ClassWithMembersAndTypes,
- BinaryObjectString = 1 << RecordType.BinaryObjectString,
- BinaryArray = 1 << RecordType.BinaryArray,
- MemberPrimitiveTyped = 1 << RecordType.MemberPrimitiveTyped,
- MemberReference = 1 << RecordType.MemberReference,
- ObjectNull = 1 << RecordType.ObjectNull,
- MessageEnd = 1 << RecordType.MessageEnd,
- BinaryLibrary = 1 << RecordType.BinaryLibrary,
- ObjectNullMultiple256 = 1 << RecordType.ObjectNullMultiple256,
- ObjectNullMultiple = 1 << RecordType.ObjectNullMultiple,
- ArraySinglePrimitive = 1 << RecordType.ArraySinglePrimitive,
- ArraySingleObject = 1 << RecordType.ArraySingleObject,
- ArraySingleString = 1 << RecordType.ArraySingleString,
-
- Nulls = ObjectNull | ObjectNullMultiple256 | ObjectNullMultiple,
-
- ///
- /// Any .NET object (a primitive, a reference type, a reference or single null).
- ///
- AnyObject = MemberPrimitiveTyped
- | ArraySingleObject | ArraySinglePrimitive | ArraySingleString | BinaryArray
- | ClassWithId | ClassWithMembers | ClassWithMembersAndTypes | SystemClassWithMembers | SystemClassWithMembersAndTypes
- | BinaryObjectString
- | MemberReference
- | ObjectNull,
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/ArrayType.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/ArrayType.cs
deleted file mode 100644
index 16a0a9bd89f..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/ArrayType.cs
+++ /dev/null
@@ -1,47 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Binary array type.
-///
-///
-///
-///
-/// [MS-NRBF] 2.4.1.1
-///
-///
-///
-public enum ArrayType : byte
-{
- ///
- /// A single-dimensional array.
- ///
- Single = 0,
-
- ///
- /// An array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes.
- ///
- Jagged = 1,
-
- ///
- /// A multi-dimensional rectangular array.
- ///
- Rectangular = 2,
-
- ///
- /// A single-dimensional array where the lower bound index is greater than 0.
- ///
- SingleOffset = 3,
-
- ///
- /// A jagged array where the lower bound index is greater than 0.
- ///
- JaggedOffset = 4,
-
- ///
- /// Multi-dimensional arrays where the lower bound index of at least one of the dimensions is greater than 0.
- ///
- RectangularOffset = 5,
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/BinaryType.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/BinaryType.cs
deleted file mode 100644
index 2ec5da67e83..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/BinaryType.cs
+++ /dev/null
@@ -1,58 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Identifies the remoting type of a class member or array item.
-///
-///
-///
-///
-/// [MS-NRBF] 2.1.2.2
-///
-///
-///
-internal enum BinaryType : byte
-{
- ///
- /// Type is defined by and it is not a string.
- ///
- Primitive,
-
- ///
- /// Type is
- /// length prefixed string.
- ///
- String,
-
- ///
- /// Type is System.Object.
- ///
- Object,
-
- ///
- /// Type is a standard .NET object.
- ///
- SystemClass,
-
- ///
- /// Type is an object.
- ///
- Class,
-
- ///
- /// Type is a single-dimensional array of objects.
- ///
- ObjectArray,
-
- ///
- /// Type is a single-dimensional array of strings.
- ///
- StringArray,
-
- ///
- /// Types is a single-dimensional array of a primitive type.
- ///
- PrimitiveArray
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/PrimitiveType.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/PrimitiveType.cs
deleted file mode 100644
index 1859ddd0e66..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/PrimitiveType.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Primitive type.
-///
-///
-///
-///
-/// [MS-NRBF] 2.1.2.3
-///
-///
-///
-internal enum PrimitiveType : byte
-{
- ///
- /// Used internally to express no value
- ///
- None = 0,
- Boolean = 1,
- Byte = 2,
- Char = 3,
- // 4 is not used in the protocol
- Decimal = 5,
- Double = 6,
- Int16 = 7,
- Int32 = 8,
- Int64 = 9,
- SByte = 10,
- Single = 11,
- TimeSpan = 12,
- DateTime = 13,
- UInt16 = 14,
- UInt32 = 15,
- UInt64 = 16,
- Null = 17,
- String = 18
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/RecordType.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/RecordType.cs
deleted file mode 100644
index 213587af06e..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Enums/RecordType.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Record type.
-///
-///
-///
-/// The enumeration does not contain all values supported by the
-/// [MS-NRBF] 2.1.2.1, but only those supported by the .
-///
-///
-public enum RecordType : byte
-{
- SerializedStreamHeader,
- ClassWithId,
- SystemClassWithMembers,
- ClassWithMembers,
- SystemClassWithMembersAndTypes,
- ClassWithMembersAndTypes,
- BinaryObjectString,
- BinaryArray,
- MemberPrimitiveTyped,
- MemberReference,
- ObjectNull,
- MessageEnd,
- BinaryLibrary,
- ObjectNullMultiple256,
- ObjectNullMultiple,
- ArraySinglePrimitive,
- ArraySingleObject,
- ArraySingleString
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ArrayInfo.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ArrayInfo.cs
deleted file mode 100644
index 38cc6b5557f..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ArrayInfo.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Array information structure.
-///
-///
-///
-///
-/// [MS-NRBF] 2.4.2.1
-///
-///
-///
-[DebuggerDisplay("Length={Length}, {ArrayType}, rank={Rank}")]
-internal readonly struct ArrayInfo
-{
- internal ArrayInfo(int objectId, uint length, ArrayType arrayType = ArrayType.Single, int rank = 1)
- {
- ObjectId = objectId;
- Length = length;
- ArrayType = arrayType;
- Rank = rank;
- }
-
- internal int ObjectId { get; }
-
- internal uint Length { get; }
-
- internal ArrayType ArrayType { get; }
-
- internal int Rank { get; }
-
- internal static ArrayInfo Parse(BinaryReader reader)
- => new(reader.ReadInt32(), (uint)ParseValidArrayLength(reader));
-
- internal static int ParseValidArrayLength(BinaryReader reader)
- {
- int length = reader.ReadInt32();
-
- if (length is < 0 or > 2147483591) // Array.MaxLength
- {
- throw new SerializationException($"Invalid array length: {length}");
- }
-
- return length;
- }
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassInfo.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassInfo.cs
deleted file mode 100644
index ecbd70b13c6..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassInfo.cs
+++ /dev/null
@@ -1,57 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Reflection.Metadata;
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Class info.
-///
-///
-///
-///
-/// [MS-NRBF] 2.3.1.1
-///
-///
-///
-[DebuggerDisplay("{Name}")]
-internal sealed class ClassInfo
-{
- private ClassInfo(int objectId, TypeName name, Dictionary memberNames)
- {
- ObjectId = objectId;
- Name = name;
- MemberNames = memberNames;
- }
-
- internal int ObjectId { get; }
-
- internal TypeName Name { get; }
-
- internal Dictionary MemberNames { get; }
-
- internal static ClassInfo Parse(BinaryReader reader, PayloadOptions payloadOptions)
- {
- int objectId = reader.ReadInt32();
- TypeName typeName = reader.ReadTypeName(payloadOptions);
- int memberCount = reader.ReadInt32();
-
- // The attackers could create an input with MANY member names.
- // If we were storing them in a list, then searching for the index
- // of given member name (done by ClassRecord indexer) would take
- // O(m * n), where m = memberCount and n = memberNameLength.
- // To prevent this from happening, we are using a Dictionary instead,
- // which has O(1) lookup time.
- Dictionary memberNames = new(StringComparer.Ordinal);
- for (int i = 0; i < memberCount; i++)
- {
- // The NRBF specification does not prohibit multiple members with the same names,
- // however it's impossible to get such output with BinaryFormatter,
- // so we prohibit that on purpose (Add is going to throw on duplicates).
- memberNames.Add(reader.ReadString(), i);
- }
-
- return new(objectId, typeName, memberNames);
- }
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassTypeInfo.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassTypeInfo.cs
deleted file mode 100644
index c06dd3ba715..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/ClassTypeInfo.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Reflection.Metadata;
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Identifies a class by it's name and library id.
-///
-///
-///
-///
-/// [MS-NRBF] 2.1.1.8
-///
-///
-///
-[DebuggerDisplay("{TypeName}")]
-internal sealed class ClassTypeInfo
-{
- internal ClassTypeInfo(TypeName typeName, int libraryId)
- {
- TypeName = typeName;
- LibraryId = libraryId;
- }
-
- internal TypeName TypeName { get; }
-
- internal int LibraryId { get; }
-
- internal static ClassTypeInfo Parse(BinaryReader reader, PayloadOptions options)
- => new(reader.ReadTypeName(options), reader.ReadInt32());
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/MemberTypeInfo.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/MemberTypeInfo.cs
deleted file mode 100644
index f1827932e73..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/MemberTypeInfo.cs
+++ /dev/null
@@ -1,197 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Reflection.Metadata;
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Member type info.
-///
-///
-///
-///
-/// [MS-NRBF] 2.3.1.2
-///
-///
-///
-internal readonly struct MemberTypeInfo
-{
- internal MemberTypeInfo(IReadOnlyList<(BinaryType BinaryType, object? AdditionalInfo)> infos) => _infos = infos;
-
- private readonly IReadOnlyList<(BinaryType BinaryType, object? AdditionalInfo)> _infos;
-
- internal IReadOnlyList<(BinaryType BinaryType, object? AdditionalInfo)> Infos => _infos;
-
- internal static MemberTypeInfo Parse(BinaryReader reader, int count, PayloadOptions options)
- {
- List<(BinaryType BinaryType, object? AdditionalInfo)> info = [];
-
- // [MS-NRBF] 2.3.1.2
- // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nrbf/aa509b5a-620a-4592-a5d8-7e9613e0a03e
-
- // All of the BinaryTypeEnumeration values come before all of the AdditionalInfo values.
- // There's not necessarily a 1:1 mapping; some enum values don't have associated AdditionalInfo.
- for (int i = 0; i < count; i++)
- {
- info.Add(((BinaryType)reader.ReadByte(), null));
- }
-
- // Check for more clarifying information
- for (int i = 0; i < info.Count; i++)
- {
- BinaryType type = info[i].BinaryType;
- switch (type)
- {
- case BinaryType.Primitive:
- case BinaryType.PrimitiveArray:
- info[i] = (type, (PrimitiveType)reader.ReadByte());
- break;
- case BinaryType.SystemClass:
- info[i] = (type, reader.ReadTypeName(options));
- break;
- case BinaryType.Class:
- info[i] = (type, ClassTypeInfo.Parse(reader, options));
- break;
- case BinaryType.String:
- case BinaryType.ObjectArray:
- case BinaryType.StringArray:
- case BinaryType.Object:
- // Other types have no additional data.
- break;
- default:
- throw new SerializationException("Unexpected binary type.");
- }
- }
-
- return new MemberTypeInfo(info);
- }
-
- internal (AllowedRecordTypes allowed, PrimitiveType primitiveType) GetNextAllowedRecordType(int currentValuesCount)
- {
- (BinaryType binaryType, object? additionalInfo) = Infos[currentValuesCount];
-
- // Every array can be either an array itself, a null or a reference (to an array)
- const AllowedRecordTypes stringArray = AllowedRecordTypes.ArraySingleString
- | AllowedRecordTypes.ObjectNull | AllowedRecordTypes.MemberReference;
- const AllowedRecordTypes primitiveArray = AllowedRecordTypes.ArraySinglePrimitive
- | AllowedRecordTypes.ObjectNull | AllowedRecordTypes.MemberReference;
- const AllowedRecordTypes objectArray = AllowedRecordTypes.ArraySingleObject
- | AllowedRecordTypes.ObjectNull | AllowedRecordTypes.MemberReference;
-
- // Every string can be a string, a null or a reference (to a string)
- const AllowedRecordTypes strings = AllowedRecordTypes.BinaryObjectString
- | AllowedRecordTypes.ObjectNull | AllowedRecordTypes.MemberReference;
-
- // Every class can be a null or a reference and a ClassWithId (TODO: verify ClassId)
- const AllowedRecordTypes classes = AllowedRecordTypes.ClassWithId
- | AllowedRecordTypes.ObjectNull | AllowedRecordTypes.MemberReference
- | AllowedRecordTypes.MemberPrimitiveTyped
- | AllowedRecordTypes.BinaryLibrary; // classes may be preceded with a library record (System too!)
- // but System classes can be expressed only by System records
- const AllowedRecordTypes systemClass = classes
- | AllowedRecordTypes.SystemClassWithMembers | AllowedRecordTypes.SystemClassWithMembersAndTypes;
- const AllowedRecordTypes nonSystemClass = classes
- | AllowedRecordTypes.ClassWithMembers | AllowedRecordTypes.ClassWithMembersAndTypes;
-
- return binaryType switch
- {
- BinaryType.Primitive => (default, (PrimitiveType)additionalInfo!),
- BinaryType.String => (strings, default),
- BinaryType.Object => (AllowedRecordTypes.AnyObject, default),
- BinaryType.StringArray => (stringArray, default),
- BinaryType.PrimitiveArray => (primitiveArray, default),
- BinaryType.Class => (nonSystemClass, default),
- BinaryType.SystemClass => (systemClass, default),
- BinaryType.ObjectArray => (objectArray, default),
- _ => throw new SerializationException($"Invalid binary type: {binaryType}.")
- };
- }
-
- internal bool IsElementType(Type typeElement, RecordMap recordMap)
- {
- (BinaryType binaryType, object? additionalInfo) = Infos[0];
-
- switch (binaryType)
- {
- case BinaryType.String:
- return typeElement == typeof(string);
- case BinaryType.StringArray:
- return typeElement == typeof(string[]);
- case BinaryType.Object:
- return typeElement == typeof(object);
- case BinaryType.ObjectArray:
- return typeElement == typeof(object[]);
- case BinaryType.Primitive:
- case BinaryType.PrimitiveArray:
- if (binaryType is BinaryType.PrimitiveArray)
- {
- if (!typeElement.IsArray)
- {
- return false;
- }
-
- typeElement = typeElement.GetElementType()!;
- }
-
- return ((PrimitiveType)additionalInfo!) switch
- {
- PrimitiveType.Boolean => typeElement == typeof(bool),
- PrimitiveType.Byte => typeElement == typeof(byte),
- PrimitiveType.Char => typeElement == typeof(char),
- PrimitiveType.Decimal => typeElement == typeof(decimal),
- PrimitiveType.Double => typeElement == typeof(double),
- PrimitiveType.Int16 => typeElement == typeof(short),
- PrimitiveType.Int32 => typeElement == typeof(int),
- PrimitiveType.Int64 => typeElement == typeof(long),
- PrimitiveType.SByte => typeElement == typeof(sbyte),
- PrimitiveType.Single => typeElement == typeof(float),
- PrimitiveType.TimeSpan => typeElement == typeof(TimeSpan),
- PrimitiveType.DateTime => typeElement == typeof(DateTime),
- PrimitiveType.UInt16 => typeElement == typeof(ushort),
- PrimitiveType.UInt32 => typeElement == typeof(uint),
- PrimitiveType.UInt64 => typeElement == typeof(ulong),
- _ => false
- };
- case BinaryType.SystemClass:
- if (typeElement.Assembly != typeof(object).Assembly)
- {
- return false;
- }
-
- TypeName typeName = (TypeName)additionalInfo!;
- string fullSystemClassName = FormatterServices.GetTypeFullNameIncludingTypeForwards(typeElement);
- return typeName.FullName == fullSystemClassName;
- case BinaryType.Class:
- ClassTypeInfo typeInfo = (ClassTypeInfo)additionalInfo!;
- string fullClassName = FormatterServices.GetTypeFullNameIncludingTypeForwards(typeElement);
- if (typeInfo.TypeName.FullName != fullClassName)
- {
- return false;
- }
-
- BinaryLibraryRecord libraryRecord = (BinaryLibraryRecord)recordMap[typeInfo.LibraryId];
- string assemblyName = FormatterServices.GetAssemblyNameIncludingTypeForwards(typeElement);
- return assemblyName == libraryRecord.LibraryName.FullName;
- default:
- throw new NotSupportedException();
- }
- }
-
- internal bool ShouldBeRepresentedAsArrayOfClassRecords()
- {
- (BinaryType binaryType, object? additionalInfo) = Infos[0];
-
- switch (binaryType)
- {
- case BinaryType.SystemClass:
- TypeName typeName = (TypeName)additionalInfo!;
- return !typeName.IsSZArray;
- case BinaryType.Class:
- ClassTypeInfo typeInfo = (ClassTypeInfo)additionalInfo!;
- return !typeInfo.TypeName.IsSZArray;
- default:
- return false;
- }
- }
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/NextInfo.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/NextInfo.cs
deleted file mode 100644
index 31fdefc3d6c..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Infos/NextInfo.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-[DebuggerDisplay("{Parent.RecordType}, {Allowed}, {PrimitiveType}")]
-internal readonly struct NextInfo
-{
- internal NextInfo(AllowedRecordTypes allowed, SerializationRecord parent,
- Stack stack, PrimitiveType primitiveType = default)
- {
- Allowed = allowed;
- Parent = parent;
- Stack = stack;
- PrimitiveType = primitiveType;
- }
-
- internal AllowedRecordTypes Allowed { get; }
-
- internal SerializationRecord Parent { get; }
-
- internal Stack Stack { get; }
-
- internal PrimitiveType PrimitiveType { get; }
-
- internal NextInfo With(AllowedRecordTypes allowed, PrimitiveType primitiveType)
- => allowed == Allowed && primitiveType == PrimitiveType
- ? this // previous record was of the same type
- : new(allowed, Parent, Stack, primitiveType);
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadOptions.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadOptions.cs
deleted file mode 100644
index 90e736d7f48..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadOptions.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Reflection.Metadata;
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-public sealed class PayloadOptions
-{
- public PayloadOptions() { }
-
- public TypeNameParseOptions? TypeNameParseOptions { get; set; }
-
- // public int MaxMemberCount { get; set; } = 100;
-
- // public bool AreCustomOffsetArraysAllowed { get; set; } = false;
-
- // public bool AreJaggedArraysAllowed { get; set; } = false;
-
- // public int MaxArrayLength { get; set; }
-
- // public int MaxArrayRank { get; set; }
-
- // public HashSet AllowedRootRecordTypes { get; } = new();
-
- // public HashSet AllowedTypes { get; } = new();
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadReader.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadReader.cs
deleted file mode 100644
index 11ba19fe01f..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/PayloadReader.cs
+++ /dev/null
@@ -1,285 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Globalization;
-using System.Text;
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-public static class PayloadReader
-{
- private static UTF8Encoding ThrowOnInvalidUtf8Encoding { get; } = new(false, throwOnInvalidBytes: true);
-
- ///
- /// Checks if given buffer contains only Binary Formatter payload.
- ///
- /// The buffer to inspect
- /// True if the first and last bytes indicate Binary Format, otherwise false.
- public static bool ContainsBinaryFormatterPayload(ReadOnlySpan bytes)
- => bytes.Length >= SerializedStreamHeaderRecord.Size + 2
- && bytes[0] == (byte)RecordType.SerializedStreamHeader
- && bytes[bytes.Length - 1] == (byte)RecordType.MessageEnd;
-
- ///
- /// Checks if given stream contains only Binary Formatter payload.
- ///
- /// The readable and seekable stream to inspect.
- /// True if the first and last bytes indicate Binary Format, otherwise false.
- /// The stream is null.
- /// The stream does not support reading or seeking.
- /// The stream was closed.
- /// It does not modify the position of the stream.
- public static bool ContainsBinaryFormatterPayload(Stream stream)
- {
- ArgumentNullException.ThrowIfNull(stream);
-
- // TODO: discuss an alternative approach, where we would parse SerializedStreamHeaderRecord
- // here and return false on failure
-
- long beginning = stream.Position;
- if (stream.Length - beginning < SerializedStreamHeaderRecord.Size + 2)
- {
- return false;
- }
-
- try
- {
- int firstByte = stream.ReadByte();
- if (firstByte != (byte)RecordType.SerializedStreamHeader)
- {
- return false;
- }
-
- stream.Seek(-1, SeekOrigin.End);
- int lastByte = stream.ReadByte();
- return lastByte == (byte)RecordType.MessageEnd;
- }
- finally
- {
- stream.Position = beginning;
- }
- }
-
- ///
- /// Reads the provided Binary Format payload.
- ///
- /// The Binary Format payload.
- /// An object that describes optional parameters to use.
- /// True to leave the payload open
- /// after the reading is finished, otherwise, false.
- /// A that represents the root object.
- /// It can be either ,
- /// a or an .
- /// When is null.
- /// The payload does not support reading or is already closed.
- /// When reading input from encounters invalid Binary Format data.
- /// When reading input from
- /// encounters invalid sequence of UTF8 characters.
- public static SerializationRecord Read(Stream payload, PayloadOptions? options = default, bool leaveOpen = false)
- {
- ArgumentNullException.ThrowIfNull(payload);
-
- using BinaryReader reader = new(payload, ThrowOnInvalidUtf8Encoding, leaveOpen: leaveOpen);
- return Read(reader, options ?? new());
- }
-
- ///
- /// Reads the provided Binary Format payload that is expected to contain an instance of any class (or struct) that is not an or a primitive type.
- ///
- /// A that represents the root object.
- ///
- public static ClassRecord ReadClassRecord(Stream payload, PayloadOptions? options = default, bool leaveOpen = false)
- => (ClassRecord)Read(payload, options, leaveOpen);
-
- private static SerializationRecord Read(BinaryReader reader, PayloadOptions options)
- {
- Stack readStack = new();
- RecordMap recordMap = new();
-
- // Everything has to start with a header
- var header = (SerializedStreamHeaderRecord)ReadNext(reader, recordMap, AllowedRecordTypes.SerializedStreamHeader, options, out _);
- // and can be followed by any Object, BinaryLibrary and a MessageEnd.
- const AllowedRecordTypes allowed = AllowedRecordTypes.AnyObject
- | AllowedRecordTypes.BinaryLibrary | AllowedRecordTypes.MessageEnd;
-
- RecordType recordType;
- SerializationRecord nextRecord;
- do
- {
- while (readStack.Count > 0)
- {
- NextInfo nextInfo = readStack.Pop();
-
- if (nextInfo.Allowed != AllowedRecordTypes.None)
- {
- // Read the next Record
- do
- {
- nextRecord = ReadNext(reader, recordMap, nextInfo.Allowed, options, out _);
- // BinaryLibrary often precedes class records.
- // It has been already added to the RecordMap and it must not be added
- // to the array record, so simply read next record.
- // It's possible to read multiple BinaryLibraryRecord in a row, hence the loop.
- }
- while (nextRecord is BinaryLibraryRecord);
-
- // Handle it:
- // - add to the parent records list,
- // - push next info if there are remaining nested records to read.
- nextInfo.Parent.HandleNextRecord(nextRecord, nextInfo);
- // Push on the top of the stack the first nested record of last read record,
- // so it gets read as next record.
- PushFirstNestedRecordInfo(nextRecord, readStack);
- }
- else
- {
- object value = reader.ReadPrimitiveType(nextInfo.PrimitiveType);
-
- nextInfo.Parent.HandleNextValue(value, nextInfo);
- }
- }
-
- nextRecord = ReadNext(reader, recordMap, allowed, options, out recordType);
- PushFirstNestedRecordInfo(nextRecord, readStack);
- }
- while (recordType != RecordType.MessageEnd);
-
- SerializationRecord rootRecord = recordMap[header.RootId];
- return rootRecord is SystemClassWithMembersAndTypesRecord systemClass
- ? systemClass.TryToMapToUserFriendly()
- : rootRecord;
- }
-
- private static SerializationRecord ReadNext(BinaryReader reader, RecordMap recordMap,
- AllowedRecordTypes allowed, PayloadOptions options, out RecordType recordType)
- {
- recordType = (RecordType)reader.ReadByte();
-
- if (((uint)allowed & (1u << (int)recordType)) == 0)
- {
- throw new SerializationException($"Unexpected type seen: {recordType}.");
- }
-
- // Following values are not part of the public API surface, as they are not supported
- // and users don't need to handle these values.
- // 18~20 are from the reference source but aren't in the OpenSpecs doc
- const RecordType CrossAppDomainMap = (RecordType)18;
- const RecordType CrossAppDomainString = (RecordType)19;
- const RecordType CrossAppDomainAssembly = (RecordType)20;
- const RecordType MethodCall = (RecordType)21;
- const RecordType MethodReturn = (RecordType)22;
-
- SerializationRecord record = recordType switch
- {
- RecordType.ArraySingleObject => ArraySingleObjectRecord.Parse(reader),
- RecordType.ArraySinglePrimitive => ParseArraySinglePrimitiveRecord(reader),
- RecordType.ArraySingleString => ArraySingleStringRecord.Parse(reader),
- RecordType.BinaryArray => BinaryArrayRecord.Parse(reader, recordMap, options),
- RecordType.BinaryLibrary => BinaryLibraryRecord.Parse(reader),
- RecordType.BinaryObjectString => BinaryObjectStringRecord.Parse(reader),
- RecordType.ClassWithId => ClassWithIdRecord.Parse(reader, recordMap),
- RecordType.ClassWithMembers => ClassWithMembersRecord.Parse(reader, recordMap, options),
- RecordType.ClassWithMembersAndTypes => ClassWithMembersAndTypesRecord.Parse(reader, recordMap, options),
- RecordType.MemberPrimitiveTyped => ParseMemberPrimitiveTypedRecord(reader),
- RecordType.MemberReference => MemberReferenceRecord.Parse(reader, recordMap),
- RecordType.MessageEnd => MessageEndRecord.Singleton,
- RecordType.ObjectNull => ObjectNullRecord.Instance,
- RecordType.ObjectNullMultiple => ObjectNullMultipleRecord.Parse(reader),
- RecordType.ObjectNullMultiple256 => ObjectNullMultiple256Record.Parse(reader),
- RecordType.SerializedStreamHeader => SerializedStreamHeaderRecord.Parse(reader),
- RecordType.SystemClassWithMembers => SystemClassWithMembersRecord.Parse(reader, options),
- RecordType.SystemClassWithMembersAndTypes => SystemClassWithMembersAndTypesRecord.Parse(reader, options),
- CrossAppDomainAssembly or CrossAppDomainMap or CrossAppDomainString
- => throw new NotSupportedException("Cross domain is not supported by design"),
- MethodCall or MethodReturn
- => throw new NotSupportedException("Remote invocation is not supported by design"),
- _ => throw new SerializationException($"Invalid RecordType value: {recordType}")
- };
-
- recordMap.Add(record);
-
- return record;
- }
-
- private static SerializationRecord ParseMemberPrimitiveTypedRecord(BinaryReader reader)
- {
- PrimitiveType primitiveType = (PrimitiveType)reader.ReadByte();
-
- return primitiveType switch
- {
- PrimitiveType.Boolean => new MemberPrimitiveTypedRecord(reader.ReadBoolean()),
- PrimitiveType.Byte => new MemberPrimitiveTypedRecord(reader.ReadByte()),
- PrimitiveType.SByte => new MemberPrimitiveTypedRecord(reader.ReadSByte()),
- PrimitiveType.Char => new MemberPrimitiveTypedRecord(reader.ReadChar()),
- PrimitiveType.Int16 => new MemberPrimitiveTypedRecord(reader.ReadInt16()),
- PrimitiveType.UInt16 => new MemberPrimitiveTypedRecord(reader.ReadUInt16()),
- PrimitiveType.Int32 => new MemberPrimitiveTypedRecord(reader.ReadInt32()),
- PrimitiveType.UInt32 => new MemberPrimitiveTypedRecord(reader.ReadUInt32()),
- PrimitiveType.Int64 => new MemberPrimitiveTypedRecord(reader.ReadInt64()),
- PrimitiveType.UInt64 => new MemberPrimitiveTypedRecord(reader.ReadUInt64()),
- PrimitiveType.Single => new MemberPrimitiveTypedRecord(reader.ReadSingle()),
- PrimitiveType.Double => new MemberPrimitiveTypedRecord(reader.ReadDouble()),
- PrimitiveType.Decimal => new MemberPrimitiveTypedRecord(decimal.Parse(reader.ReadString(), CultureInfo.InvariantCulture)),
- PrimitiveType.DateTime => new MemberPrimitiveTypedRecord(BinaryReaderExtensions.CreateDateTimeFromData(reader.ReadInt64())),
- PrimitiveType.TimeSpan => new MemberPrimitiveTypedRecord(new TimeSpan(reader.ReadInt64())),
- // String is handled with a record, never on it's own
- _ => throw new SerializationException($"Failure trying to read primitive '{primitiveType}'"),
- };
- }
-
- private static SerializationRecord ParseArraySinglePrimitiveRecord(BinaryReader reader)
- {
- ArrayInfo info = ArrayInfo.Parse(reader);
- PrimitiveType primitiveType = (PrimitiveType)reader.ReadByte();
-
- return primitiveType switch
- {
- PrimitiveType.Boolean => Read(info, reader),
- PrimitiveType.Byte => Read(info, reader),
- PrimitiveType.SByte => Read(info, reader),
- PrimitiveType.Char => Read(info, reader),
- PrimitiveType.Int16 => Read(info, reader),
- PrimitiveType.UInt16 => Read(info, reader),
- PrimitiveType.Int32 => Read(info, reader),
- PrimitiveType.UInt32 => Read(info, reader),
- PrimitiveType.Int64 => Read(info, reader),
- PrimitiveType.UInt64 => Read(info, reader),
- PrimitiveType.Single => Read(info, reader),
- PrimitiveType.Double => Read(info, reader),
- PrimitiveType.Decimal => Read(info, reader),
- PrimitiveType.DateTime => Read(info, reader),
- PrimitiveType.TimeSpan => Read(info, reader),
- _ => throw new SerializationException($"Failure trying to read primitive '{primitiveType}'"),
- };
-
- static SerializationRecord Read(ArrayInfo info, BinaryReader reader) where T : unmanaged
- => new ArraySinglePrimitiveRecord(info, ArraySinglePrimitiveRecord.ReadPrimitiveTypes(reader, (int)info.Length));
- }
-
- ///
- /// This method is responsible for pushing only the FIRST read info
- /// of the NESTED record into the .
- /// It's not pushing all of them, because it could be used as a vector of attack.
- /// Example: BinaryArrayRecord with length,
- /// where first item turns out to be
- /// that provides nulls.
- ///
- private static void PushFirstNestedRecordInfo(SerializationRecord record, Stack readStack)
- {
- if (record is ClassRecord classRecord)
- {
- if (classRecord.ExpectedValuesCount > 0)
- {
- (AllowedRecordTypes allowed, PrimitiveType primitiveType) = classRecord.GetNextAllowedRecordType();
-
- readStack.Push(new(allowed, classRecord, readStack, primitiveType));
- }
- }
- else if (record is ArrayRecord arrayRecord && arrayRecord.ValuesToRead > 0)
- {
- (AllowedRecordTypes allowed, PrimitiveType primitiveType) = arrayRecord.GetAllowedRecordType();
-
- readStack.Push(new(allowed, arrayRecord, readStack, primitiveType));
- }
- }
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayOfClassesRecord.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayOfClassesRecord.cs
deleted file mode 100644
index a6856e2b4ee..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayOfClassesRecord.cs
+++ /dev/null
@@ -1,76 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-internal sealed class ArrayOfClassesRecord : ArrayRecord
-{
- internal ArrayOfClassesRecord(ArrayInfo arrayInfo, MemberTypeInfo memberTypeInfo, RecordMap recordMap)
- : base(arrayInfo)
- {
- MemberTypeInfo = memberTypeInfo;
- RecordMap = recordMap;
- Records = [];
- }
-
- public override RecordType RecordType => RecordType.BinaryArray;
-
- internal List Records { get; }
-
- private MemberTypeInfo MemberTypeInfo { get; }
-
- private RecordMap RecordMap { get; }
-
- protected override ClassRecord?[] ToArrayOfT(bool allowNulls)
- {
- ClassRecord?[] result = new ClassRecord?[Length];
-
- int resultIndex = 0;
- foreach (SerializationRecord record in Records)
- {
- SerializationRecord actual = record is MemberReferenceRecord referenceRecord
- ? referenceRecord.GetReferencedRecord()
- : record;
-
- if (actual is ClassRecord classRecord)
- {
- result[resultIndex++] = classRecord;
- }
- else
- {
- if (!allowNulls)
- {
- ThrowHelper.ThrowArrayContainedNull();
- }
-
- int nullCount = ((NullsRecord)actual).NullCount;
- do
- {
- result[resultIndex++] = null;
- nullCount--;
- }
- while (nullCount > 0);
- }
- }
-
- return result;
- }
-
- private protected override void AddValue(object value) => Records.Add((SerializationRecord)value);
-
- internal override (AllowedRecordTypes allowed, PrimitiveType primitiveType) GetAllowedRecordType()
- {
- (AllowedRecordTypes allowed, PrimitiveType primitiveType) = MemberTypeInfo.GetNextAllowedRecordType(0);
-
- if (allowed != AllowedRecordTypes.None)
- {
- // It's an array, it can also contain multiple nulls
- return (allowed | AllowedRecordTypes.Nulls, primitiveType);
- }
-
- return (allowed, primitiveType);
- }
-
- internal override bool IsElementType(Type typeElement)
- => MemberTypeInfo.IsElementType(typeElement, RecordMap);
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayRecord.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayRecord.cs
deleted file mode 100644
index 6a692876067..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArrayRecord.cs
+++ /dev/null
@@ -1,122 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-public abstract class ArrayRecord : SerializationRecord
-{
- internal const int DefaultMaxArrayLength = 64_000;
-
- private protected ArrayRecord(ArrayInfo arrayInfo)
- {
- ArrayInfo = arrayInfo;
- ValuesToRead = arrayInfo.Length;
- }
-
- ///
- /// Length of the array.
- ///
- public uint Length => ArrayInfo.Length;
-
- ///
- /// Rank of the array.
- ///
- public int Rank => ArrayInfo.Rank;
-
- ///
- /// Type of the array.
- ///
- public ArrayType ArrayType => ArrayInfo.ArrayType;
-
- internal override int ObjectId => ArrayInfo.ObjectId;
-
- internal long ValuesToRead { get; private protected set; }
-
- private protected ArrayInfo ArrayInfo { get; }
-
- public Array ToArray(Type expectedArrayType, bool allowNulls = true, int maxLength = DefaultMaxArrayLength)
- {
- if (!IsTypeNameMatching(expectedArrayType))
- {
- throw new InvalidOperationException();
- }
-
- return Deserialize(expectedArrayType, allowNulls, maxLength);
- }
-
- private protected abstract Array Deserialize(Type arrayType, bool allowNulls, int maxLength);
-
- public override bool IsTypeNameMatching(Type type)
- => type.IsArray
- && type.GetArrayRank() == ArrayInfo.Rank
- && IsElementType(type.GetElementType()!);
-
- internal sealed override void HandleNextValue(object value, NextInfo info)
- => HandleNext(value, info, size: 1);
-
- internal sealed override void HandleNextRecord(SerializationRecord nextRecord, NextInfo info)
- => HandleNext(nextRecord, info, size: nextRecord is NullsRecord nullsRecord ? nullsRecord.NullCount : 1);
-
- private protected abstract void AddValue(object value);
-
- internal abstract bool IsElementType(Type typeElement);
-
- private void HandleNext(object value, NextInfo info, int size)
- {
- ValuesToRead -= size;
-
- if (ValuesToRead < 0)
- {
- // The only way to get here is to read a multiple null item with Count
- // larger than the number of array items that were left to read.
- ThrowHelper.ThrowUnexpectedNullRecordCount();
- }
- else if (ValuesToRead > 0)
- {
- info.Stack.Push(info);
- }
-
- AddValue(value);
- }
-
- internal abstract (AllowedRecordTypes allowed, PrimitiveType primitiveType) GetAllowedRecordType();
-}
-
-public abstract class ArrayRecord : ArrayRecord
-{
- private protected ArrayRecord(ArrayInfo arrayInfo) : base(arrayInfo)
- {
- }
-
- ///
- /// Allocates an array of and fills it with the data provided in the serialized records (in case of primitive types like or ) or the serialized records themselves.
- ///
- /// Specifies whether null values are allowed.
- /// Specifies the max length of an array that can be allocated.
- ///
- ///
- /// The array has elements and can be used as a vector of attack.
- /// Example: an array with elements that contains only nulls
- /// takes 15 bytes to serialize and more than 2 GB to deserialize!
- ///
- ///
- /// A new array is allocated every time this method is called.
- ///
- ///
- public T?[] ToArray(bool allowNulls = true, int maxLength = DefaultMaxArrayLength)
- {
- if (Length > maxLength)
- {
- ThrowHelper.ThrowMaxArrayLength(maxLength, Length);
- }
-
- return ToArrayOfT(allowNulls);
- }
-
- // PERF: if allocating new arrays is not acceptable, then we could introduce CopyTo method
-
- private protected override Array Deserialize(Type arrayType, bool allowNulls, int maxLength)
- => ToArray(allowNulls, maxLength);
-
- protected abstract T?[] ToArrayOfT(bool allowNulls);
-}
diff --git a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArraySingleObjectRecord.cs b/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArraySingleObjectRecord.cs
deleted file mode 100644
index 98dba85559d..00000000000
--- a/src/System.Private.Windows.Core/src/System.Runtime.Serialization.BinaryFormat/Records/ArraySingleObjectRecord.cs
+++ /dev/null
@@ -1,73 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.Serialization.BinaryFormat;
-
-///
-/// Single dimensional array of objects.
-///
-///
-///
-///
-/// [MS-NRBF] 2.4.3.2
-///
-///
-///
-internal sealed class ArraySingleObjectRecord : ArrayRecord