Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

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

4 files changed

Lines changed: 99 additions & 38 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespaceJava.Interop.Tools.JavaCallableWrappers
22
{
3-
partialclassCrc64
3+
partialclassCrc64Helper
44
{
5-
staticreadonlyulong[,]Table={
5+
staticreadonlyulong[,]table={
66
{
77
0x0000000000000000,0x7ad870c830358979,0xf5b0e190606b12f2,0x8f689158505e9b8b,
88
0xc038e5739841b68f,0xbae095bba8743ff6,0x358804e3f82aa47d,0x4f50742bc81f2d04,

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

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

56-
unsafeprotectedoverridevoidHashCore(byte[]array,intibStart,intcbSize)
56+
protectedoverrideunsafevoidHashCore(byte[]array,intibStart,intcbSize)
5757
{
58-
intlen=cbSize;
59-
intidx=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,refcrc,reflength);
8759
}
8860

8961
protectedoverridebyte[]HashFinal()=>BitConverter.GetBytes(crc^length);
Lines changed: 91 additions & 0 deletions
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+
usingSystem;
32+
usingSystem.Security.Cryptography;
33+
34+
namespaceJava.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+
internalstaticpartialclassCrc64Helper
46+
{
47+
48+
internalstaticbyte[]Compute(byte[]array)
49+
{
50+
ulongcrc=ulong.MaxValue;
51+
ulonglength=0;
52+
53+
HashCore(array,0,array.Length,refcrc,reflength);
54+
55+
returnBitConverter.GetBytes(crc^length);
56+
}
57+
58+
internalstaticunsafevoidHashCore(byte[]array,intibStart,intcbSize,refulongcrc,refulonglength)
59+
{
60+
intlen=cbSize;
61+
intidx=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‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ public static string GetPackageName (Type type)
205205
casePackageNamingPolicy.LowercaseWithAssemblyName:
206206
return"assembly_"+(assemblyName.Replace('.','_')+"."+type.Namespace).ToLowerInvariant();
207207
casePackageNamingPolicy.LowercaseCrc64:
208-
using(varcrc=newCrc64())
209-
returnCRC_PREFIX+ToHash(type.Namespace+":"+assemblyName,crc);
208+
returnCRC_PREFIX+ToCrc64(type.Namespace+":"+assemblyName);
210209
default:
211210
thrownewNotSupportedException($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported.");
212211
}
@@ -574,8 +573,7 @@ public static string GetPackageName (TypeDefinition type, TypeDefinitionCache? c
574573
casePackageNamingPolicy.LowercaseWithAssemblyName:
575574
return"assembly_"+(type.GetPartialAssemblyName(cache).Replace('.','_')+"."+type.Namespace).ToLowerInvariant();
576575
casePackageNamingPolicy.LowercaseCrc64:
577-
using(varcrc=newCrc64())
578-
returnCRC_PREFIX+ToHash(type.Namespace+":"+type.GetPartialAssemblyName(cache),crc);
576+
returnCRC_PREFIX+ToCrc64(type.Namespace+":"+type.GetPartialAssemblyName(cache));
579577
default:
580578
thrownewNotSupportedException($"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-
staticstringToHash(stringvalue,HashAlgorithmalgorithm)
645+
staticstringToCrc64(stringvalue)
648646
{
649647
vardata=Encoding.UTF8.GetBytes(value);
650-
varhash=algorithm.ComputeHash(data);
648+
varhash=Crc64Helper.Compute(data);
651649
varbuf=newStringBuilder(hash.Length*2);
652650
foreach(varbinhash)
653651
buf.AppendFormat("{0:x2}",b);

0 commit comments

Comments
 (0)