Skip to content

[API Proposal]: Add API to detect Binary Format (MS-NRBF) header #84090

@JeremyKuhne

Description

@JeremyKuhne

Background and motivation

With BinaryFormatter depreciation transitioning off of usages requires looking at incoming data to see if it is valid Binary Format data. Knowing that it is allows fallback logic to be implemented.

Knowing this also is potentially useful for other filtering of incoming binary streams.

We're seeing numerous places where we need this in the WinForms stack and expect that our customers will need the same.

API Proposal

namespace System.Runtime.Serialization.Formatters.Binary;

public sealed class BinaryFormatter : IFormatter
{
    public static bool HasValidBinaryFormatHeader(ReadOnlySpan<byte> data);
    public static bool HasValidBinaryFormatHeader(byte[] data);

    // Stream overload possible? Not all streams are seekable...
}

API Usage

void ProcessClipboardCsvData(ReadOnlySpan<byte> data)
{
    if (BinaryFormatter.HasValidBinaryFormatHeader(data))
    {
        // Handle legacy data
        return;
    }

    // Simple character stream, create string...
}

Alternative Designs

Could potentially invert the logic as it is remotely possible to match a non binary formatted stream. E.g. DoesNotHaveValid....

Risks

No specific known risks.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions