Skip to content

Commit 121230d

Browse files
adamsitnikjkotasericstj
authored
Make System.Formats.Nrbf public (#103232)
Co-authored-by: Jan Kotas <[email protected]> Co-authored-by: Eric StJohn <[email protected]>
1 parent f1f0750 commit 121230d

File tree

93 files changed

+1548
-1516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1548
-1516
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ VisualStudioVersion = 17.11.34906.271
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8721A077-F840-4316-9D8A-6E48B3D5EA03}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime.Serialization.BinaryFormat", "src\System.Runtime.Serialization.BinaryFormat.csproj", "{E23A8E5A-9975-4ABE-BE54-B02328207F37}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Formats.Nrbf", "src\System.Formats.Nrbf.csproj", "{E23A8E5A-9975-4ABE-BE54-B02328207F37}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{32F1B10C-7A0C-440E-8F42-D5309D3D7DAA}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.Serialization.BinaryFormat.Tests", "tests\System.Runtime.Serialization.BinaryFormat.Tests.csproj", "{8B45136F-C266-4FE7-B5A3-CFD6F3648289}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Formats.Nrbf.Tests", "tests\System.Formats.Nrbf.Tests.csproj", "{8B45136F-C266-4FE7-B5A3-CFD6F3648289}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{D50A3723-4816-47D5-B6A2-E126D2F02722}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Formats.Nrbf", "ref\System.Formats.Nrbf.csproj", "{3415B821-490E-4B12-9A5F-135CAC6271B8}"
1317
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -25,13 +29,18 @@ Global
2529
{8B45136F-C266-4FE7-B5A3-CFD6F3648289}.Debug|Any CPU.Build.0 = Debug|Any CPU
2630
{8B45136F-C266-4FE7-B5A3-CFD6F3648289}.Release|Any CPU.ActiveCfg = Release|Any CPU
2731
{8B45136F-C266-4FE7-B5A3-CFD6F3648289}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{3415B821-490E-4B12-9A5F-135CAC6271B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{3415B821-490E-4B12-9A5F-135CAC6271B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{3415B821-490E-4B12-9A5F-135CAC6271B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{3415B821-490E-4B12-9A5F-135CAC6271B8}.Release|Any CPU.Build.0 = Release|Any CPU
2836
EndGlobalSection
2937
GlobalSection(SolutionProperties) = preSolution
3038
HideSolutionNode = FALSE
3139
EndGlobalSection
3240
GlobalSection(NestedProjects) = preSolution
3341
{E23A8E5A-9975-4ABE-BE54-B02328207F37} = {8721A077-F840-4316-9D8A-6E48B3D5EA03}
3442
{8B45136F-C266-4FE7-B5A3-CFD6F3648289} = {32F1B10C-7A0C-440E-8F42-D5309D3D7DAA}
43+
{3415B821-490E-4B12-9A5F-135CAC6271B8} = {D50A3723-4816-47D5-B6A2-E126D2F02722}
3544
EndGlobalSection
3645
GlobalSection(ExtensibilityGlobals) = postSolution
3746
SolutionGuid = {CC885ACA-F5FA-4CAA-BA5F-2517D39C3066}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// ------------------------------------------------------------------------------
4+
// Changes to this file must follow the https://aka.ms/api-review process.
5+
// ------------------------------------------------------------------------------
6+
7+
namespace System.Formats.Nrbf
8+
{
9+
public abstract partial class ArrayRecord : System.Formats.Nrbf.SerializationRecord
10+
{
11+
internal ArrayRecord() { }
12+
public override System.Formats.Nrbf.SerializationRecordId Id { get { throw null; } }
13+
public abstract System.ReadOnlySpan<int> Lengths { get; }
14+
public int Rank { get { throw null; } }
15+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The code for an array of the specified type might not be available.")]
16+
public System.Array GetArray(System.Type expectedArrayType, bool allowNulls = true) { throw null; }
17+
}
18+
public abstract partial class ClassRecord : System.Formats.Nrbf.SerializationRecord
19+
{
20+
internal ClassRecord() { }
21+
public override System.Formats.Nrbf.SerializationRecordId Id { get { throw null; } }
22+
public System.Collections.Generic.IEnumerable<string> MemberNames { get { throw null; } }
23+
public override System.Reflection.Metadata.TypeName TypeName { get { throw null; } }
24+
public System.Formats.Nrbf.ArrayRecord? GetArrayRecord(string memberName) { throw null; }
25+
public bool GetBoolean(string memberName) { throw null; }
26+
public byte GetByte(string memberName) { throw null; }
27+
public char GetChar(string memberName) { throw null; }
28+
public System.Formats.Nrbf.ClassRecord? GetClassRecord(string memberName) { throw null; }
29+
public System.DateTime GetDateTime(string memberName) { throw null; }
30+
public decimal GetDecimal(string memberName) { throw null; }
31+
public double GetDouble(string memberName) { throw null; }
32+
public short GetInt16(string memberName) { throw null; }
33+
public int GetInt32(string memberName) { throw null; }
34+
public long GetInt64(string memberName) { throw null; }
35+
public object? GetRawValue(string memberName) { throw null; }
36+
public sbyte GetSByte(string memberName) { throw null; }
37+
public System.Formats.Nrbf.SerializationRecord? GetSerializationRecord(string memberName) { throw null; }
38+
public float GetSingle(string memberName) { throw null; }
39+
public string? GetString(string memberName) { throw null; }
40+
public System.TimeSpan GetTimeSpan(string memberName) { throw null; }
41+
public ushort GetUInt16(string memberName) { throw null; }
42+
public uint GetUInt32(string memberName) { throw null; }
43+
public ulong GetUInt64(string memberName) { throw null; }
44+
public bool HasMember(string memberName) { throw null; }
45+
}
46+
public static partial class NrbfDecoder
47+
{
48+
public static System.Formats.Nrbf.SerializationRecord Decode(System.IO.Stream payload, out System.Collections.Generic.IReadOnlyDictionary<System.Formats.Nrbf.SerializationRecordId, System.Formats.Nrbf.SerializationRecord> recordMap, System.Formats.Nrbf.PayloadOptions options=null, bool leaveOpen=false) { throw null; }
49+
public static System.Formats.Nrbf.SerializationRecord Decode(System.IO.Stream payload, System.Formats.Nrbf.PayloadOptions? options=null, bool leaveOpen=false) { throw null; }
50+
public static System.Formats.Nrbf.ClassRecord DecodeClassRecord(System.IO.Stream payload, System.Formats.Nrbf.PayloadOptions? options=null, bool leaveOpen=false) { throw null; }
51+
public static bool StartsWithPayloadHeader(byte[] bytes) { throw null; }
52+
public static bool StartsWithPayloadHeader(System.IO.Stream stream) { throw null; }
53+
}
54+
public sealed partial class PayloadOptions
55+
{
56+
public PayloadOptions() { }
57+
public System.Reflection.Metadata.TypeNameParseOptions? TypeNameParseOptions { get { throw null; } set { } }
58+
public bool UndoTruncatedTypeNames { get { throw null; } set { } }
59+
}
60+
public abstract partial class PrimitiveTypeRecord : System.Formats.Nrbf.SerializationRecord
61+
{
62+
internal PrimitiveTypeRecord() { }
63+
public object Value { get { throw null; } }
64+
}
65+
public abstract partial class PrimitiveTypeRecord<T> : System.Formats.Nrbf.PrimitiveTypeRecord
66+
{
67+
internal PrimitiveTypeRecord() { }
68+
public override System.Reflection.Metadata.TypeName TypeName { get { throw null; } }
69+
public new T Value { get { throw null; } }
70+
}
71+
public abstract partial class SerializationRecord
72+
{
73+
internal SerializationRecord() { }
74+
public abstract System.Formats.Nrbf.SerializationRecordId Id { get; }
75+
public abstract System.Formats.Nrbf.SerializationRecordType RecordType { get; }
76+
public abstract System.Reflection.Metadata.TypeName TypeName { get; }
77+
public bool TypeNameMatches(System.Type type) { throw null; }
78+
}
79+
public partial struct SerializationRecordId : System.IEquatable<System.Formats.Nrbf.SerializationRecordId>
80+
{
81+
public bool Equals(System.Formats.Nrbf.SerializationRecordId other) { throw null; }
82+
public override bool Equals(object? obj) { throw null; }
83+
public override int GetHashCode() { throw null; }
84+
}
85+
public enum SerializationRecordType
86+
{
87+
SerializedStreamHeader = 0,
88+
ClassWithId = 1,
89+
SystemClassWithMembers = 2,
90+
ClassWithMembers = 3,
91+
SystemClassWithMembersAndTypes = 4,
92+
ClassWithMembersAndTypes = 5,
93+
BinaryObjectString = 6,
94+
BinaryArray = 7,
95+
MemberPrimitiveTyped = 8,
96+
MemberReference = 9,
97+
ObjectNull = 10,
98+
MessageEnd = 11,
99+
BinaryLibrary = 12,
100+
ObjectNullMultiple256 = 13,
101+
ObjectNullMultiple = 14,
102+
ArraySinglePrimitive = 15,
103+
ArraySingleObject = 16,
104+
ArraySingleString = 17,
105+
MethodCall = 21,
106+
MethodReturn = 22,
107+
}
108+
public abstract partial class SZArrayRecord<T> : System.Formats.Nrbf.ArrayRecord
109+
{
110+
internal SZArrayRecord() { }
111+
public int Length { get { throw null; } }
112+
public override System.ReadOnlySpan<int> Lengths { get { throw null; } }
113+
public abstract T?[] GetArray(bool allowNulls = true);
114+
}
115+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<CLSCompliant>false</CLSCompliant>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="System.Formats.Nrbf.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Hashing\ref\System.IO.Hashing.csproj" />
13+
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Metadata\ref\System.Reflection.Metadata.csproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
17+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
18+
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,7 @@
156156
<data name="Serialization_MemberTypeMismatchException" xml:space="preserve">
157157
<value>Specified member '{0}' was not of the expected type.</value>
158158
</data>
159+
<data name="NotSupported_NonZeroOffsets" xml:space="preserve">
160+
<value>Only arrays with zero offsets are supported.</value>
161+
</data>
159162
</root>
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<CLSCompliant>false</CLSCompliant>
7-
<!-- once the APIs get approved this is going to change -->
8-
<IsPackable>false</IsPackable>
9-
<DefineConstants>$(DefineConstants);SYSTEM_RUNTIME_SERIALIZATION_BINARYFORMAT</DefineConstants>
7+
<UseCompilerGeneratedDocXmlFile>true</UseCompilerGeneratedDocXmlFile>
8+
<IsPackable>true</IsPackable>
9+
<PackageDescription>Provides a safe reader for .NET Remoting Binary Format (NRBF) payloads.
10+
11+
Commonly Used Types:
12+
System.Formats.Nrbf.NrbfDecoder</PackageDescription>
13+
14+
<!-- Disabling baseline validation since this is a brand new package.
15+
Once this package has shipped a stable version, the following line
16+
should be removed in order to re-enable validation. -->
17+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
1018
</PropertyGroup>
1119
<ItemGroup>
1220
<ProjectReference Include="$(LibrariesProjectRoot)\System.IO.Hashing\src\System.IO.Hashing.csproj" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Formats.Nrbf;
5+
6+
[Flags]
7+
internal enum AllowedRecordTypes : uint
8+
{
9+
None = 0,
10+
SerializedStreamHeader = 1 << SerializationRecordType.SerializedStreamHeader,
11+
ClassWithId = 1 << SerializationRecordType.ClassWithId,
12+
SystemClassWithMembersAndTypes = 1 << SerializationRecordType.SystemClassWithMembersAndTypes,
13+
ClassWithMembersAndTypes = 1 << SerializationRecordType.ClassWithMembersAndTypes,
14+
BinaryObjectString = 1 << SerializationRecordType.BinaryObjectString,
15+
BinaryArray = 1 << SerializationRecordType.BinaryArray,
16+
MemberPrimitiveTyped = 1 << SerializationRecordType.MemberPrimitiveTyped,
17+
MemberReference = 1 << SerializationRecordType.MemberReference,
18+
ObjectNull = 1 << SerializationRecordType.ObjectNull,
19+
MessageEnd = 1 << SerializationRecordType.MessageEnd,
20+
BinaryLibrary = 1 << SerializationRecordType.BinaryLibrary,
21+
ObjectNullMultiple256 = 1 << SerializationRecordType.ObjectNullMultiple256,
22+
ObjectNullMultiple = 1 << SerializationRecordType.ObjectNullMultiple,
23+
ArraySinglePrimitive = 1 << SerializationRecordType.ArraySinglePrimitive,
24+
ArraySingleObject = 1 << SerializationRecordType.ArraySingleObject,
25+
ArraySingleString = 1 << SerializationRecordType.ArraySingleString,
26+
27+
Nulls = ObjectNull | ObjectNullMultiple256 | ObjectNullMultiple,
28+
29+
/// <summary>
30+
/// Any .NET object (a primitive, a reference type, a reference or single null).
31+
/// </summary>
32+
AnyObject = MemberPrimitiveTyped
33+
| ArraySingleObject | ArraySinglePrimitive | ArraySingleString | BinaryArray
34+
| ClassWithId | ClassWithMembersAndTypes | SystemClassWithMembersAndTypes
35+
| BinaryObjectString
36+
| MemberReference
37+
| ObjectNull,
38+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
using System.Diagnostics;
55
using System.IO;
6-
using System.Runtime.Serialization.BinaryFormat.Utils;
6+
using System.Formats.Nrbf.Utils;
77

8-
namespace System.Runtime.Serialization.BinaryFormat;
8+
namespace System.Formats.Nrbf;
99

1010
/// <summary>
1111
/// Array information structure.
@@ -18,15 +18,15 @@ internal readonly struct ArrayInfo
1818
{
1919
internal const int MaxArrayLength = 2147483591; // Array.MaxLength
2020

21-
internal ArrayInfo(int objectId, long totalElementsCount, BinaryArrayType arrayType = BinaryArrayType.Single, int rank = 1)
21+
internal ArrayInfo(SerializationRecordId id, long totalElementsCount, BinaryArrayType arrayType = BinaryArrayType.Single, int rank = 1)
2222
{
23-
ObjectId = objectId;
23+
Id = id;
2424
TotalElementsCount = totalElementsCount;
2525
ArrayType = arrayType;
2626
Rank = rank;
2727
}
2828

29-
internal int ObjectId { get; }
29+
internal SerializationRecordId Id { get; }
3030

3131
internal long TotalElementsCount { get; }
3232

@@ -41,7 +41,7 @@ internal int GetSZArrayLength()
4141
}
4242

4343
internal static ArrayInfo Decode(BinaryReader reader)
44-
=> new(reader.ReadInt32(), ParseValidArrayLength(reader));
44+
=> new(SerializationRecordId.Decode(reader), ParseValidArrayLength(reader));
4545

4646
internal static int ParseValidArrayLength(BinaryReader reader)
4747
{
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
using System.Collections.Generic;
55
using System.Reflection.Metadata;
6-
using System.Runtime.Serialization.BinaryFormat.Utils;
6+
using System.Formats.Nrbf.Utils;
77

8-
namespace System.Runtime.Serialization.BinaryFormat;
8+
namespace System.Formats.Nrbf;
99

10-
internal sealed class ArrayOfClassesRecord : ArrayRecord<ClassRecord>
10+
internal sealed class ArrayOfClassesRecord : SZArrayRecord<ClassRecord>
1111
{
12-
private TypeName? _elementTypeName;
12+
private TypeName? _typeName;
1313

1414
internal ArrayOfClassesRecord(ArrayInfo arrayInfo, MemberTypeInfo memberTypeInfo)
1515
: base(arrayInfo)
@@ -18,13 +18,14 @@ internal ArrayOfClassesRecord(ArrayInfo arrayInfo, MemberTypeInfo memberTypeInfo
1818
Records = [];
1919
}
2020

21-
public override RecordType RecordType => RecordType.BinaryArray;
21+
public override SerializationRecordType RecordType => SerializationRecordType.BinaryArray;
2222

2323
internal List<SerializationRecord> Records { get; }
2424

2525
private MemberTypeInfo MemberTypeInfo { get; }
2626

27-
public override TypeName ElementTypeName => _elementTypeName ??= MemberTypeInfo.GetElementTypeName();
27+
public override TypeName TypeName
28+
=> _typeName ??= MemberTypeInfo.GetArrayTypeName(ArrayInfo);
2829

2930
/// <inheritdoc/>
3031
public override ClassRecord?[] GetArray(bool allowNulls = true)
@@ -79,7 +80,4 @@ internal override (AllowedRecordTypes allowed, PrimitiveType primitiveType) GetA
7980

8081
return (allowed, primitiveType);
8182
}
82-
83-
internal override bool IsElementType(Type typeElement)
84-
=> MemberTypeInfo.IsElementType(typeElement);
8583
}

0 commit comments

Comments
 (0)