RSCG – Darp.BinaryObjects

RSCG – Darp.BinaryObjects
 
 

name Darp.BinaryObjects
nuget https://www.nuget.org/packages/Darp.BinaryObjects/
link https://github.com/rosslight/Darp.BinaryObjects
author Ross Light GmbH

Serialize bits of data

 

This is how you can use Darp.BinaryObjects .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

  
	<ItemGroup>
	  <PackageReference Include="Darp.BinaryObjects" Version="0.4.0" />
	</ItemGroup>

</Project>


The code that you will use is


using BitsDemo;
using Darp.BinaryObjects;

var z = new zlib_header(0x78, 0x9C);

var size = z.GetByteCount();

// Write the values back to a buffer
var writeBuffer = new byte[size];
if(z.TryWriteLittleEndian(writeBuffer))
{
    Console.WriteLine("writing to buffer" );
	foreach (var item in writeBuffer)
	{
		Console.Write(item+" ");
	}
}



using Darp.BinaryObjects;
using System.IO.Compression;

namespace BitsDemo;

[BinaryObject]
partial record zlib_header(byte cmf, byte flg)
{
    
}

 

The code that is generated is

// <auto-generated/>
#nullable enable

namespace BitsDemo
{
    /// <remarks> <list type="table">
    /// <item> <term><b>Field</b></term> <description><b>Byte Length</b></description> </item>
    /// <item> <term><see cref="cmf"/></term> <description>1</description> </item>
    /// <item> <term><see cref="flg"/></term> <description>1</description> </item>
    /// <item> <term> --- </term> <description>2</description> </item>
    /// </list> </remarks>
    partial record zlib_header : global::Darp.BinaryObjects.IWritable, global::Darp.BinaryObjects.ISpanReadable<zlib_header>
    {
        /// <inheritdoc />
        [global::System.Diagnostics.Contracts.Pure]
        [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public int GetByteCount() => 2;

        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public bool TryWriteLittleEndian(global::System.Span<byte> destination) => TryWriteLittleEndian(destination, out _);
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int bytesWritten)
        {
            bytesWritten = 0;

            if (destination.Length < 2)
                return false;
            global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.cmf);
            global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[1..], this.flg);
            bytesWritten += 2;

            return true;
        }
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public bool TryWriteBigEndian(global::System.Span<byte> destination) => TryWriteBigEndian(destination, out _);
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public bool TryWriteBigEndian(global::System.Span<byte> destination, out int bytesWritten)
        {
            bytesWritten = 0;

            if (destination.Length < 2)
                return false;
            global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.cmf);
            global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[1..], this.flg);
            bytesWritten += 2;

            return true;
        }

        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public static bool TryReadLittleEndian(global::System.ReadOnlySpan<byte> source, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out zlib_header? value) => TryReadLittleEndian(source, out value, out _);
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public static bool TryReadLittleEndian(global::System.ReadOnlySpan<byte> source, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out zlib_header? value, out int bytesRead)
        {
            bytesRead = 0;
            value = default;

            if (source.Length < 2)
                return false;
            var ___readcmf = global::Darp.BinaryObjects.Generated.Utilities.ReadUInt8(source[0..1]);
            var ___readflg = global::Darp.BinaryObjects.Generated.Utilities.ReadUInt8(source[1..2]);
            bytesRead += 2;

            value = new zlib_header(___readcmf, ___readflg);
            return true;
        }
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public static bool TryReadBigEndian(global::System.ReadOnlySpan<byte> source, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out zlib_header? value) => TryReadBigEndian(source, out value, out _);
        /// <inheritdoc />
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
        public static bool TryReadBigEndian(global::System.ReadOnlySpan<byte> source, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out zlib_header? value, out int bytesRead)
        {
            bytesRead = 0;
            value = default;

            if (source.Length < 2)
                return false;
            var ___readcmf = global::Darp.BinaryObjects.Generated.Utilities.ReadUInt8(source[0..1]);
            var ___readflg = global::Darp.BinaryObjects.Generated.Utilities.ReadUInt8(source[1..2]);
            bytesRead += 2;

            value = new zlib_header(___readcmf, ___readflg);
            return true;
        }
    }
}

namespace Darp.BinaryObjects.Generated
{
    using Darp.BinaryObjects;
    using System;
    using System.Buffers.Binary;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    /// <summary>Helper methods used by generated BinaryObjects.</summary>
    [GeneratedCodeAttribute("Darp.BinaryObjects.Generator", "0.4.0.0")]
    file static class Utilities
    {
        /// <summary> Writes a <c>byte</c> to the destination </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void WriteUInt8(Span<byte> destination, byte value)
        {
            destination[0] = value;
        }
        /// <summary> Reads a <c>byte</c> from the given source </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static byte ReadUInt8(ReadOnlySpan<byte> source)
        {
            return source[0];
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Darp.BinaryObjects