简体   繁体   English

C#不安全的代码翻译?

[英]C# Unsafe code translation?

I have some code that I'm using in a standard C# application. 我有一些我在标准C#应用程序中使用的代码。

I'm sharing the library in a Silverlight project that doesn't allow unsafe code. 我在Silverlight项目中共享库,不允许不安全的代码。 I don't know much at all about unsafe/pointer logic/arithmetic and was wondering if someone could translate the following code snippet so that it will run without /unsafe. 我不太了解不安全/指针逻辑/算术,并且想知道是否有人可以翻译以下代码片段以便它将在没有/不安全的情况下运行。 I don't care about the performance drop since the code won't be called often on the client. 我不关心性能下降,因为代码不会经常在客户端上调用。 Thanks in advance for any assistance. 在此先感谢您的任何帮助。

    public static unsafe int GeStableHash(string name)
    {
        fixed (char* str = name)
        {
            char* chPtr = str;
            int num = 352654597;
            int num2 = num;
            int* numPtr = (int*)chPtr;
            for (int i = name.Length; i > 0; i -= 4)
            {
                num = (((num << 5) + num) + (num >> 27)) ^ numPtr[0];
                if (i <= 2)
                {
                    break;
                }
                num2 = (((num2 << 5) + num2) + (num2 >> 27)) ^ numPtr[1];
                numPtr += 2;
            }
            return (num + (num2 * 1566083941));
        }
    }

As per reply below, I looked into the Silverlight implementation of this function. 根据下面的回复,我查看了此函数的Silverlight实现。

Now I'm even more confused. 现在我更加困惑。 The code I see in the SL mscorlib.dll string.GetHashCode function looks like this (it uses the unsafe keyword and gives syntax errors in Visual Studio!): 我在SL mscorlib.dll string.GetHashCode函数中看到的代码如下所示(它使用unsafe关键字并在Visual Studio中提供语法错误!):

[SecuritySafeCritical, ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public unsafe override int GetHashCode()
{
    IntPtr arg_0F_0;
    IntPtr expr_06 = arg_0F_0 = this;
    if (expr_06 != 0)
    {
        arg_0F_0 = (IntPtr)((int)expr_06 + RuntimeHelpers.OffsetToStringData);
    }
    char* ptr = arg_0F_0;
    int num = 352654597;
    int num2 = num;
    int* ptr2 = (int*)ptr;
    int i;
    for (i = this.Length; i > 2; i -= 4)
    {
        num = ((num << 5) + num + (num >> 27) ^ *ptr2);
        num2 = ((num2 << 5) + num2 + (num2 >> 27) ^ ptr2[(IntPtr)4 / 4]);
        ptr2 += (IntPtr)8 / 4;
    }
    if (i > 0)
    {
        num = ((num << 5) + num + (num >> 27) ^ *ptr2);
    }
    return num + num2 * 1566083941;
}

The solution can be found here: https://codereview.stackexchange.com/questions/7661/c-unsafe-code-translation 解决方案可以在这里找到: https//codereview.stackexchange.com/questions/7661/c-unsafe-code-translation

It looks like this is the implementation of GetHashCode 看起来这是GetHashCode的实现

So perhaps you don't need to do anything "datatohash".GetHashCode(); 所以也许你不需要做任何“datatohash”.GetHashCode(); will suffice 就足够了

The hash code implementations you posted use pointer arithmetic for better performance. 您发布的哈希代码实现使用指针算法以获得更好的性能。 It compiles in Visual Studio if you put it in a .NET Framework project with the "allow unsafe code" option. 如果您将它放在带有“允许不安全代码”选项的.NET Framework项目中,它将在Visual Studio中进行编译。

It would be possible to compute the same result by using the same logic, but indexing the string directly. 可以通过使用相同的逻辑计算相同的结果,但直接索引字符串。 For example, the expression "Hello, World"[7] evaluates to 'W' . 例如,表达式"Hello, World"[7]计算结果为'W'

I'll leave the translation itself as the proverbial exercise for the reader. 我将翻译本身作为读者的谚语练习。

Why don't you do something like an MD5 hash? 你为什么不做像MD5哈希这样的事情? It's stable, and is implemented in various places. 它很稳定,并且在各个地方实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM