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
23 changes: 20 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Utilities/TypeMapHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static ulong HashJavaName (string name, bool is64Bit)

// Native code (EmbeddedAssemblies::typemap_java_to_managed in embedded-assemblies.cc) will operate on wchar_t cast to a byte array, we need to do
// the same
return HashBytes (Encoding.Unicode.GetBytes (name), is64Bit);
return HashString (name, Encoding.Unicode, is64Bit);
}

/// <summary>
Expand All @@ -29,10 +29,27 @@ public static ulong HashJavaNameForCLR (string name, bool is64Bit)
return UInt64.MaxValue;
}

return HashBytes (Encoding.UTF8.GetBytes (name), is64Bit);
return HashString (name, Encoding.UTF8, is64Bit);
}

static ulong HashBytes (byte[] bytes, bool is64Bit)
// Java type names are always ASCII and typically 20-100 characters,
// so the encoded byte count is well within stackalloc limits.
// The unsafe Encoding.GetBytes(char*, int, byte*, int) overload is
// used because the Span-based overload requires netstandard2.1+.
static unsafe ulong HashString (string name, Encoding encoding, bool is64Bit)
{
int byteCount = encoding.GetByteCount (name);
Span<byte> buffer = byteCount <= 256
Comment thread
jonathanpeppers marked this conversation as resolved.
? stackalloc byte [byteCount]
: new byte [byteCount];
fixed (char* pChars = name)
fixed (byte* pBuffer = buffer) {
encoding.GetBytes (pChars, name.Length, pBuffer, byteCount);
}
return HashBytes (buffer, is64Bit);
}

static ulong HashBytes (ReadOnlySpan<byte> bytes, bool is64Bit)
{
if (is64Bit) {
return XxHash3.HashToUInt64 (bytes);
Expand Down
1 change: 1 addition & 0 deletions tools/tmt/tmt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<OutputPath>../../bin/$(Configuration)/bin/typemap-tool</OutputPath>
<OutputType>Exe</OutputType>
<LibZipSharpBundleAllNativeLibraries>true</LibZipSharpBundleAllNativeLibraries>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<RollForward>Major</RollForward>
</PropertyGroup>
Expand Down