Skip to content

Commit 7d197f1

Browse files
authored
Refactor Crc64 type (#769)
Context: dotnet/android#5451 Split the code to static helper class, which can be used from `Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.GetPackageName`. This way the linker can strip the whole `System.Security.Cryptography.Primitives` assembly out in simple XA apps. The `Crc64` is still needed in XA, so we still keep it based on `System.Security.Cryptography.HashAlgorithm`.
1 parent 876442f commit 7d197f1

File tree

4 files changed

+99
-38
lines changed

4 files changed

+99
-38
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace Java.Interop.Tools.JavaCallableWrappers
22
{
3-
partial class Crc64
3+
partial class Crc64Helper
44
{
5-
static readonly ulong[,] Table = {
5+
static readonly ulong[,] table = {
66
{
77
0x0000000000000000, 0x7ad870c830358979, 0xf5b0e190606b12f2, 0x8f689158505e9b8b,
88
0xc038e5739841b68f, 0xbae095bba8743ff6, 0x358804e3f82aa47d, 0x4f50742bc81f2d04,

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,9 @@ public override void Initialize ()
5353
length = 0;
5454
}
5555

56-
unsafe protected override void HashCore (byte [] array, int ibStart, int cbSize)
56+
protected override unsafe void HashCore (byte [] array, int ibStart, int cbSize)
5757
{
58-
int len = cbSize;
59-
int idx = ibStart;
60-
61-
fixed (ulong* tptr = Table) {
62-
fixed (byte* aptr = array) {
63-
while (len >= 8) {
64-
crc ^= *((ulong*)(aptr + idx));
65-
crc =
66-
tptr [7 * 256 + (crc & 0xff)] ^
67-
tptr [6 * 256 + ((crc >> 8) & 0xff)] ^
68-
tptr [5 * 256 + ((crc >> 16) & 0xff)] ^
69-
tptr [4 * 256 + ((crc >> 24) & 0xff)] ^
70-
tptr [3 * 256 + ((crc >> 32) & 0xff)] ^
71-
tptr [2 * 256 + ((crc >> 40) & 0xff)] ^
72-
tptr [1 * 256 + ((crc >> 48) & 0xff)] ^
73-
tptr [0 * 256 + (crc >> 56)];
74-
idx += 8;
75-
len -= 8;
76-
}
77-
78-
while (len > 0) {
79-
crc = tptr [0 * 256 + ((crc ^ aptr[idx]) & 0xff)] ^ (crc >> 8);
80-
idx++;
81-
len--;
82-
}
83-
}
84-
}
85-
86-
length += (ulong) cbSize;
58+
Crc64Helper.HashCore (array, ibStart, cbSize, ref crc, ref length);
8759
}
8860

8961
protected override byte [] HashFinal () => BitConverter.GetBytes (crc ^ length);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Originally ported from: https://github.com/gityf/crc/blob/8045f50ba6e4193d4ee5d2539025fef26e613c9f/crc/crc64.c
3+
*
4+
* Copyright (c) 2012, Salvatore Sanfilippo <antirez at gmail dot com>
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* * Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* * Neither the name of Redis nor the names of its contributors may be used
16+
* to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE. */
30+
31+
using System;
32+
using System.Security.Cryptography;
33+
34+
namespace Java.Interop.Tools.JavaCallableWrappers
35+
{
36+
/// <summary>
37+
/// CRC64 variant: crc-64-jones 64-bit
38+
/// * Poly: 0xad93d23594c935a9
39+
///
40+
/// Changes beyond initial implementation:
41+
/// * Starting Value: ulong.MaxValue
42+
/// * XOR length in HashFinal()
43+
/// * Using spliced table for faster processing
44+
/// </summary>
45+
internal static partial class Crc64Helper
46+
{
47+
48+
internal static byte [] Compute (byte [] array)
49+
{
50+
ulong crc = ulong.MaxValue;
51+
ulong length = 0;
52+
53+
HashCore (array, 0, array.Length, ref crc, ref length);
54+
55+
return BitConverter.GetBytes (crc ^ length);
56+
}
57+
58+
internal static unsafe void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length)
59+
{
60+
int len = cbSize;
61+
int idx = ibStart;
62+
63+
fixed (ulong* tptr = table) {
64+
fixed (byte* aptr = array) {
65+
while (len >= 8) {
66+
crc ^= *((ulong*) (aptr + idx));
67+
crc =
68+
tptr [7 * 256 + (crc & 0xff)] ^
69+
tptr [6 * 256 + ((crc >> 8) & 0xff)] ^
70+
tptr [5 * 256 + ((crc >> 16) & 0xff)] ^
71+
tptr [4 * 256 + ((crc >> 24) & 0xff)] ^
72+
tptr [3 * 256 + ((crc >> 32) & 0xff)] ^
73+
tptr [2 * 256 + ((crc >> 40) & 0xff)] ^
74+
tptr [1 * 256 + ((crc >> 48) & 0xff)] ^
75+
tptr [0 * 256 + (crc >> 56)];
76+
idx += 8;
77+
len -= 8;
78+
}
79+
80+
while (len > 0) {
81+
crc = tptr [0 * 256 + ((crc ^ aptr [idx]) & 0xff)] ^ (crc >> 8);
82+
idx++;
83+
len--;
84+
}
85+
}
86+
}
87+
88+
length += (ulong) cbSize;
89+
}
90+
}
91+
}

src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ public static string GetPackageName (Type type)
205205
case PackageNamingPolicy.LowercaseWithAssemblyName:
206206
return "assembly_" + (assemblyName.Replace ('.', '_') + "." + type.Namespace).ToLowerInvariant ();
207207
case PackageNamingPolicy.LowercaseCrc64:
208-
using (var crc = new Crc64 ())
209-
return CRC_PREFIX + ToHash (type.Namespace + ":" + assemblyName, crc);
208+
return CRC_PREFIX + ToCrc64 (type.Namespace + ":" + assemblyName);
210209
default:
211210
throw new NotSupportedException ($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported.");
212211
}
@@ -574,8 +573,7 @@ public static string GetPackageName (TypeDefinition type, TypeDefinitionCache? c
574573
case PackageNamingPolicy.LowercaseWithAssemblyName:
575574
return "assembly_" + (type.GetPartialAssemblyName (cache).Replace ('.', '_') + "." + type.Namespace).ToLowerInvariant ();
576575
case PackageNamingPolicy.LowercaseCrc64:
577-
using (var crc = new Crc64 ())
578-
return CRC_PREFIX + ToHash (type.Namespace + ":" + type.GetPartialAssemblyName (cache), crc);
576+
return CRC_PREFIX + ToCrc64 (type.Namespace + ":" + type.GetPartialAssemblyName (cache));
579577
default:
580578
throw new NotSupportedException ($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported.");
581579
}
@@ -644,10 +642,10 @@ static IEnumerable<MethodDefinition> GetBaseConstructors (TypeDefinition type, T
644642
}
645643
#endif // HAVE_CECIL
646644

647-
static string ToHash (string value, HashAlgorithm algorithm)
645+
static string ToCrc64 (string value)
648646
{
649647
var data = Encoding.UTF8.GetBytes (value);
650-
var hash = algorithm.ComputeHash (data);
648+
var hash = Crc64Helper.Compute (data);
651649
var buf = new StringBuilder (hash.Length * 2);
652650
foreach (var b in hash)
653651
buf.AppendFormat ("{0:x2}", b);

0 commit comments

Comments
 (0)