-
Notifications
You must be signed in to change notification settings - Fork 569
[TrimmableTypeMap] Use Crc64 package naming by default with LowercaseCrc64 compatibility #11193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5e1adf6
9d16443
174f08f
1049678
ec92067
8d5709b
05cebc7
3722fd9
e9ae72c
fa8493d
ab689ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using Java.Interop.Tools.JavaCallableWrappers; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| internal static class ScannerHashingHelper | ||
| { | ||
| internal static string ToLegacyCrc64 (string ns, string assemblyName) | ||
| { | ||
| int byteCount = GetNamespaceAssemblyUtf8ByteCount (ns, assemblyName); | ||
| byte[] rented = ArrayPool<byte>.Shared.Rent (byteCount); | ||
| try { | ||
| int bytesWritten = GetNamespaceAssemblyUtf8Bytes (ns, assemblyName, rented.AsSpan (0, byteCount)); | ||
| ulong crc = ulong.MaxValue; | ||
| ulong length = 0; | ||
| Crc64Helper.HashCore (rented, 0, bytesWritten, ref crc, ref length); | ||
| Span<byte> hash = stackalloc byte [8]; | ||
| WriteUInt64LittleEndian (hash, crc ^ length); | ||
| return ToHexString (hash, lowercase: true); | ||
| } finally { | ||
| ArrayPool<byte>.Shared.Return (rented); | ||
| } | ||
| } | ||
|
|
||
| internal static string ToCrc64 (string ns, string assemblyName) | ||
| { | ||
| const int stackallocThresholdBytes = 256; | ||
| int byteCount = GetNamespaceAssemblyUtf8ByteCount (ns, assemblyName); | ||
| Span<byte> utf8Buffer = byteCount <= stackallocThresholdBytes | ||
| ? stackalloc byte [stackallocThresholdBytes] | ||
| : new byte [byteCount]; | ||
|
|
||
| int bytesWritten = GetNamespaceAssemblyUtf8Bytes (ns, assemblyName, utf8Buffer.Slice (0, byteCount)); | ||
| Span<byte> hash = stackalloc byte [8]; | ||
| System.IO.Hashing.Crc64.Hash (utf8Buffer.Slice (0, bytesWritten), hash); | ||
| return ToHexString (hash, lowercase: true); | ||
| } | ||
|
|
||
| static int GetNamespaceAssemblyUtf8ByteCount (string ns, string assemblyName) | ||
| { | ||
| return System.Text.Encoding.UTF8.GetByteCount (ns) + 1 + System.Text.Encoding.UTF8.GetByteCount (assemblyName); | ||
| } | ||
|
|
||
| static unsafe int GetNamespaceAssemblyUtf8Bytes (string ns, string assemblyName, Span<byte> destination) | ||
| { | ||
| int bytesWritten = 0; | ||
| fixed (char* nsPtr = ns) | ||
| fixed (byte* destinationPtr = destination) { | ||
| bytesWritten += System.Text.Encoding.UTF8.GetBytes (nsPtr, ns.Length, destinationPtr, destination.Length); | ||
| } | ||
|
|
||
| destination [bytesWritten++] = (byte) ':'; | ||
|
|
||
| fixed (char* assemblyNamePtr = assemblyName) | ||
| fixed (byte* destinationPtr = destination) { | ||
| bytesWritten += System.Text.Encoding.UTF8.GetBytes (assemblyNamePtr, assemblyName.Length, destinationPtr + bytesWritten, destination.Length - bytesWritten); | ||
| } | ||
|
|
||
| return bytesWritten; | ||
| } | ||
|
|
||
| static string ToHexString (ReadOnlySpan<byte> hash, bool lowercase) | ||
| { | ||
| const int maxStackCharLength = 128; | ||
| int charLength = hash.Length * 2; | ||
| Span<char> chars = charLength <= maxStackCharLength | ||
| ? stackalloc char [charLength] | ||
| : new char [charLength]; | ||
|
|
||
| for (int i = 0, j = 0; i < hash.Length; i += 1, j += 2) { | ||
| byte b = hash [i]; | ||
| chars [j] = GetHexValue (b / 16, lowercase); | ||
| chars [j + 1] = GetHexValue (b % 16, lowercase); | ||
| } | ||
|
|
||
| return ((ReadOnlySpan<char>) chars).ToString (); | ||
| } | ||
|
|
||
| static void WriteUInt64LittleEndian (Span<byte> destination, ulong value) | ||
| { | ||
| destination [0] = (byte) value; | ||
| destination [1] = (byte) (value >> 8); | ||
| destination [2] = (byte) (value >> 16); | ||
| destination [3] = (byte) (value >> 24); | ||
| destination [4] = (byte) (value >> 32); | ||
| destination [5] = (byte) (value >> 40); | ||
| destination [6] = (byte) (value >> 48); | ||
| destination [7] = (byte) (value >> 56); | ||
| } | ||
|
|
||
| static char GetHexValue (int value, bool lowercase) | ||
| { | ||
| return (char) (value < 10 | ||
| ? value + '0' | ||
| : value - 10 + (lowercase ? 'a' : 'A')); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| <AndroidVersionCodePattern Condition=" '$(AndroidUseLegacyVersionCode)' != 'True' And '$(AndroidVersionCodePattern)' == '' ">{abi}{versionCode:D5}</AndroidVersionCodePattern> | ||
| <AndroidResourceGeneratorTargetName>UpdateGeneratedFiles</AndroidResourceGeneratorTargetName> | ||
| <AndroidUseApkSigner Condition=" '$(AndroidUseApkSigner)' == '' ">True</AndroidUseApkSigner> | ||
| <_AndroidPackageNamingPolicySetByUser Condition=" '$(AndroidPackageNamingPolicy)' != '' ">true</_AndroidPackageNamingPolicySetByUser> | ||
| <AndroidPackageNamingPolicy Condition=" '$(AndroidPackageNamingPolicy)' == '' ">LowercaseCrc64</AndroidPackageNamingPolicy> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 05cebc7: reverted the global |
||
| <AndroidUseManagedDesignTimeResourceGenerator Condition=" '$(AndroidUseManagedDesignTimeResourceGenerator)' == '' And '$(OS)' != 'Windows_NT' ">False</AndroidUseManagedDesignTimeResourceGenerator> | ||
| <BundleToolVersion Condition="'$(BundleToolVersion)' == ''">@BUNDLETOOL_VERSION@</BundleToolVersion> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,12 +72,12 @@ static string[]? AllUserTypesAssemblyPaths { | |
| } | ||
| } | ||
|
|
||
| static string NormalizeCrc64 (string javaName) | ||
| static string NormalizeHashedPackageName (string javaName) | ||
| { | ||
| if (javaName.StartsWith ("crc64", StringComparison.Ordinal)) { | ||
| if (javaName.StartsWith ("crc64", StringComparison.Ordinal) || javaName.StartsWith ("xx64", StringComparison.Ordinal)) { | ||
| int slash = javaName.IndexOf ('/'); | ||
| if (slash > 0) { | ||
| return "crc64.../" + javaName.Substring (slash + 1); | ||
| return "hash.../" + javaName.Substring (slash + 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefixed |
||
| } | ||
| } | ||
| return javaName; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One problem is if you have
"\0"and"\0\0\0\0"both with all zeros, then the crc value will be the same. Is that a problem here?We XOR the length on this one: