Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/NearShare.ViewModels/NearShare.ViewModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<WarningsAsErrors>nullable</WarningsAsErrors>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Text.Json.Serialization;

namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo;

[JsonSerializable(typeof(CdpDeviceInfo))]
internal sealed partial class DeviceInfoJsonContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace ShortDev.Microsoft.ConnectedDevices.Messages.Connection.DeviceInfo;
public static DeviceInfoMessage Parse<TReader>(ref TReader reader) where TReader : struct, IEndianReader, allows ref struct
=> new()
{
DeviceInfo = JsonSerializer.Deserialize<CdpDeviceInfo>(reader.ReadStringWithLength()) ?? throw new CdpProtocolException("Invalid device info")
DeviceInfo = JsonSerializer.Deserialize(
reader.ReadStringWithLength(),
DeviceInfoJsonContext.Default.CdpDeviceInfo
) ?? throw new CdpProtocolException("Invalid device info")
};

/// <summary>
Expand All @@ -21,6 +24,11 @@ public static DeviceInfoMessage Parse<TReader>(ref TReader reader) where TReader

public void Write<TWriter>(ref TWriter writer) where TWriter : struct, IEndianWriter, allows ref struct
{
writer.WriteWithLength(JsonSerializer.Serialize(DeviceInfo));
writer.WriteWithLength(
JsonSerializer.Serialize(
DeviceInfo,
DeviceInfoJsonContext.Default.CdpDeviceInfo
)
);
}
}
1,586 changes: 1,586 additions & 0 deletions lib/ShortDev.Microsoft.ConnectedDevices/Serialization/Helper.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Bond;

/// <summary>
/// Provides utility methods for tracking recursion depth and throwing an exception when the
/// tracked depth has exceeded DeserializerControls.Active.MaxDepth.
/// </summary>
internal static class MaxDepthChecker
{
/// <summary>The depth tracked for the current thread.</summary>
/// <remarks>
/// This needn't correspond 1:1 with schema structure. It is an approximate representation of how deeply deserialization
/// has recurred, in order to provide a defense-in-depth measure against stack overflows.
/// </remarks>
[ThreadStatic]
static int t_depth;

/// <summary>Validates the current depth against the limit, assuming it's about to be incremented, and returns it.</summary>
/// <exception cref="InvalidDataException">Recursion depth exceeded DeserializerControls.MaxDepth.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ValidateDepthForIncrement()
{
int depth = t_depth;
Debug.Assert(depth >= 0);

// Check with >= rather than > as we're validating depth+1.
if (depth >= DeserializerControls.Active.MaxDepth)
{
ThrowTooDeepException();
}

return depth;
}

/// <summary>Sets the tracked depth.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetDepth(int depth)
{
Debug.Assert(depth >= 0 && depth <= DeserializerControls.Active.MaxDepth);
t_depth = depth;
}

/// <summary>Undoes an increment to the current depth and throws an exception indicating max depth exceeded.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowTooDeepException() =>
throw new InvalidDataException($"Recursion depth exceeded {nameof(DeserializerControls)}.{nameof(DeserializerControls.MaxDepth)}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Runtime.CompilerServices;

namespace Bond.Expressions;

/// <summary>
/// ThrowExpression is a utility that makes it easy to create expressions that throw exceptions.
/// </summary>
internal static class ThrowExpression
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowInvalidTypeException(BondDataType actualType)
{
throw new InvalidDataException(string.Format("Invalid type {0}", actualType));
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowInvalidTypeException(BondDataType expectedType, BondDataType actualType)
{
throw new InvalidDataException(string.Format("Invalid type {0}, expected {1}", actualType, expectedType));
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowInvalidDataException(string message)
{
throw new InvalidDataException(message);
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowRequiredFieldMissingException(string schema, string field)
{
throw new InvalidDataException(string.Format("Required field {0}.{1} missing", schema, field));
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowRequiredFieldsMissingException(List<string> names, int index)
{
throw new InvalidDataException(string.Format("Required field {0} missing", names[index]));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Bond;
using Bond.IO.Unsafe;
using Bond.IO.Unsafe;
using Bond.Protocols;

namespace ShortDev.Microsoft.ConnectedDevices.Serialization;
Expand All @@ -12,14 +11,14 @@ public static ValueSet Parse(ref HeapEndianReader reader)
byte[] data = new byte[reader.Stream.Length - reader.Stream.Position];
reader.ReadBytes(data);
CompactBinaryReader<InputBuffer> bondReader = new(new(data));
return Deserialize<ValueSet>.From(bondReader);
return (ValueSet)ValueSetHelper.Deserialize(bondReader);
}

public void Write<TWriter>(ref TWriter writer) where TWriter : struct, IEndianWriter, allows ref struct
{
OutputBuffer buffer = new();
CompactBinaryWriter<OutputBuffer> bondWriter = new(buffer);
Serialize.To(bondWriter, this);
ValueSetHelper.Serialize(this, bondWriter);
writer.Write(buffer.Data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
<WarningsAsErrors>Nullable</WarningsAsErrors>
<Version>0.1.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ShortDev.Microsoft.CorrelationVector" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.7" />
<PackageReference Include="Bond.CSharp" Version="13.0.2" />
<PackageReference Include="Bond.Core.CSharp" Version="13.0.2" />
<PackageReference Include="ShortDev.IO" Version="0.2.0-alpha.1" />
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions src/NearShare.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
<PropertyGroup>
<RootNamespace>NearShare</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<RunAOTCompilation>False</RunAOTCompilation>
<PublishTrimmed>false</PublishTrimmed>
<AndroidPackageFormat>aab</AndroidPackageFormat>
</PropertyGroup>
<ItemGroup Label="Nuget">
Expand Down
Loading