4

1つの古くて大きなアプリケーションをDelphiから.NET(C#)に部分的に変換する必要があります。これがCRC32の(少なくとも私にとっては)奇妙な実装の1つです。

function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; assembler;
asm
         AND    EDX,EDX
         JZ     @Exit
         AND    ECX,ECX
         JLE    @Exit
         PUSH   EBX
         PUSH   EDI
         XOR    EBX,EBX
         LEA    EDI,CS:[OFFSET @CRC32]
@Start:  MOV    BL,AL
         SHR    EAX,8
         XOR    BL,[EDX]
         XOR    EAX,[EDI + EBX * 4]
         INC    EDX
         DEC    ECX
         JNZ    @Start
         POP    EDI
         POP    EBX
@Exit:   RET
         DB 0, 0, 0, 0, 0 // Align Table
@CRC32:  DD 000000000h, 077073096h, 0EE0E612Ch, 0990951BAh
         DD 0076DC419h, 0706AF48Fh, 0E963A535h, 09E6495A3h
         DD 00EDB8832h, 079DCB8A4h, 0E0D5E91Eh, 097D2D988h
         DD 009B64C2Bh, 07EB17CBDh, 0E7B82D07h, 090BF1D91h
         DD 01DB71064h, 06AB020F2h, 0F3B97148h, 084BE41DEh
         DD 01ADAD47Dh, 06DDDE4EBh, 0F4D4B551h, 083D385C7h
         DD 0136C9856h, 0646BA8C0h, 0FD62F97Ah, 08A65C9ECh
         DD 014015C4Fh, 063066CD9h, 0FA0F3D63h, 08D080DF5h
         DD 03B6E20C8h, 04C69105Eh, 0D56041E4h, 0A2677172h
         DD 03C03E4D1h, 04B04D447h, 0D20D85FDh, 0A50AB56Bh
         DD 035B5A8FAh, 042B2986Ch, 0DBBBC9D6h, 0ACBCF940h
         DD 032D86CE3h, 045DF5C75h, 0DCD60DCFh, 0ABD13D59h
         DD 026D930ACh, 051DE003Ah, 0C8D75180h, 0BFD06116h
         DD 021B4F4B5h, 056B3C423h, 0CFBA9599h, 0B8BDA50Fh
         DD 02802B89Eh, 05F058808h, 0C60CD9B2h, 0B10BE924h
         DD 02F6F7C87h, 058684C11h, 0C1611DABh, 0B6662D3Dh
         DD 076DC4190h, 001DB7106h, 098D220BCh, 0EFD5102Ah
         DD 071B18589h, 006B6B51Fh, 09FBFE4A5h, 0E8B8D433h
         DD 07807C9A2h, 00F00F934h, 09609A88Eh, 0E10E9818h
         DD 07F6A0DBBh, 0086D3D2Dh, 091646C97h, 0E6635C01h
         DD 06B6B51F4h, 01C6C6162h, 0856530D8h, 0F262004Eh
         DD 06C0695EDh, 01B01A57Bh, 08208F4C1h, 0F50FC457h
         DD 065B0D9C6h, 012B7E950h, 08BBEB8EAh, 0FCB9887Ch
         DD 062DD1DDFh, 015DA2D49h, 08CD37CF3h, 0FBD44C65h
         DD 04DB26158h, 03AB551CEh, 0A3BC0074h, 0D4BB30E2h
         DD 04ADFA541h, 03DD895D7h, 0A4D1C46Dh, 0D3D6F4FBh
         DD 04369E96Ah, 0346ED9FCh, 0AD678846h, 0DA60B8D0h
         DD 044042D73h, 033031DE5h, 0AA0A4C5Fh, 0DD0D7CC9h
         DD 05005713Ch, 0270241AAh, 0BE0B1010h, 0C90C2086h
         DD 05768B525h, 0206F85B3h, 0B966D409h, 0CE61E49Fh
         DD 05EDEF90Eh, 029D9C998h, 0B0D09822h, 0C7D7A8B4h
         DD 059B33D17h, 02EB40D81h, 0B7BD5C3Bh, 0C0BA6CADh
         DD 0EDB88320h, 09ABFB3B6h, 003B6E20Ch, 074B1D29Ah
         DD 0EAD54739h, 09DD277AFh, 004DB2615h, 073DC1683h
         DD 0E3630B12h, 094643B84h, 00D6D6A3Eh, 07A6A5AA8h
         DD 0E40ECF0Bh, 09309FF9Dh, 00A00AE27h, 07D079EB1h
         DD 0F00F9344h, 08708A3D2h, 01E01F268h, 06906C2FEh
         DD 0F762575Dh, 0806567CBh, 0196C3671h, 06E6B06E7h
         DD 0FED41B76h, 089D32BE0h, 010DA7A5Ah, 067DD4ACCh
         DD 0F9B9DF6Fh, 08EBEEFF9h, 017B7BE43h, 060B08ED5h
         DD 0D6D6A3E8h, 0A1D1937Eh, 038D8C2C4h, 04FDFF252h
         DD 0D1BB67F1h, 0A6BC5767h, 03FB506DDh, 048B2364Bh
         DD 0D80D2BDAh, 0AF0A1B4Ch, 036034AF6h, 041047A60h
         DD 0DF60EFC3h, 0A867DF55h, 0316E8EEFh, 04669BE79h
         DD 0CB61B38Ch, 0BC66831Ah, 0256FD2A0h, 05268E236h
         DD 0CC0C7795h, 0BB0B4703h, 0220216B9h, 05505262Fh
         DD 0C5BA3BBEh, 0B2BD0B28h, 02BB45A92h, 05CB36A04h
         DD 0C2D7FFA7h, 0B5D0CF31h, 02CD99E8Bh, 05BDEAE1Dh
         DD 09B64C2B0h, 0EC63F226h, 0756AA39Ch, 0026D930Ah
         DD 09C0906A9h, 0EB0E363Fh, 072076785h, 005005713h
         DD 095BF4A82h, 0E2B87A14h, 07BB12BAEh, 00CB61B38h
         DD 092D28E9Bh, 0E5D5BE0Dh, 07CDCEFB7h, 00BDBDF21h
         DD 086D3D2D4h, 0F1D4E242h, 068DDB3F8h, 01FDA836Eh
         DD 081BE16CDh, 0F6B9265Bh, 06FB077E1h, 018B74777h
         DD 088085AE6h, 0FF0F6A70h, 066063BCAh, 011010B5Ch
         DD 08F659EFFh, 0F862AE69h, 0616BFFD3h, 0166CCF45h
         DD 0A00AE278h, 0D70DD2EEh, 04E048354h, 03903B3C2h
         DD 0A7672661h, 0D06016F7h, 04969474Dh, 03E6E77DBh
         DD 0AED16A4Ah, 0D9D65ADCh, 040DF0B66h, 037D83BF0h
         DD 0A9BCAE53h, 0DEBB9EC5h, 047B2CF7Fh, 030B5FFE9h
         DD 0BDBDF21Ch, 0CABAC28Ah, 053B39330h, 024B4A3A6h
         DD 0BAD03605h, 0CDD70693h, 054DE5729h, 023D967BFh
         DD 0B3667A2Eh, 0C4614AB8h, 05D681B02h, 02A6F2B94h
         DD 0B40BBE37h, 0C30C8EA1h, 05A05DF1Bh, 02D02EF8Dh
         DD 074726F50h, 0736E6F69h, 0706F4320h, 067697279h
         DD 028207468h, 031202963h, 020393939h, 048207962h
         DD 06E656761h, 064655220h, 06E616D64h, 06FBBA36Eh
end;

データはネットワーク経由で送信され、宛先デバイスは正常に処理されるため、現在使用されており、何らかの形で機能しています。私はC#関数から同じ結果を達成する必要があります。このため、[)amienによるC#のCRC32に関連するStackOverflowソリューションで最も推奨されるもので遊んでいました。ここにリンクしてください:しかし、異なる結果が得られています:

//Old-Delphi
procedure TestCrc();
var
  ret: LongWord;
const
  lArr: array[0..2] of Byte = (49, 50, 51); //"123"
begin
  ret := CRC32(0, @lArr, Length(lArr)); //1997126336
  //print IntToStr(ret); //1997126336
end

とC#で

//New-C#
static void Main(string[] args)
{
    Crc32 crc32 = new Crc32(); //Using Default ctor
    String hash = String.Empty;
    string testStr = "123"; //in bytes {49, 50, 51}
    byte[] out_bytes = crc32.ComputeHash(Encoding.ASCII.GetBytes(testStr));
    foreach (byte b in out_bytes) hash += b.ToString("x2").ToLower();
    Console.WriteLine("CRC-32 is {0} ({1})", hash, BitConverter.ToUInt32(out_bytes, 0));
    //Output is Uint32 value = 3529721992
}

違いはどこにあるのかわかりません-おそらくテーブルとシードですか?または、古い機能をDLLでラップし、COMを介して新しいC#アプリケーションからアクセスするためのより良いソリューションはありますか?

ご意見をお聞かせください...

4

2 に答える 2

7

投稿したアセンブリコードを「エミュレート」しました。それはあなたのテスト(1997126336)の同じ結果を与えます。

class Program
{
    static void Main(string[] args)
    {
        byte[] test = { 49, 50, 51 };
        Console.WriteLine(CRC32(0, test));
        Console.ReadLine();
    }

    static uint CRC32(uint CRC, byte[] data)
    {
        if (data == null || data.Length == 0) return CRC;

        uint eax = CRC, ebx = 0, ecx = (uint)data.Length, edx = 0;

    Start:
        ebx = (ebx & 0xFFFF0000) | (eax & 0x0000FFFF);
        eax >>= 8;
        ebx = (ebx & 0xFFFF0000) | (data[edx] ^ (ebx & 0x000000FF));
        eax = eax ^ Table[ebx];
        edx++;
        ecx--;
        if (ecx != 0) goto Start;
        return eax;
    }

    static readonly uint[] Table = {                              
        0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
        0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
        0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
        0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
        0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
        0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
        0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
        0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
        0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
        0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
        0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
        0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
        0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
        0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
        0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
        0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
        0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
        0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
        0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
        0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
        0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
        0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
        0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
        0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
        0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
        0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
        0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
        0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
        0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
        0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
        0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
        0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
        0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
        0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
        0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
        0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
        0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
        0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
        0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
        0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
        0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
        0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
        0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
        0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
        0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
        0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
        0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
        0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
        0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
        0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
        0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
        0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
        0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
        0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
        0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
        0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
        0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
        0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
        0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
        0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
        0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
        0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
        0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
        0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
        0x74726F50, 0x736E6F69, 0x706F4320, 0x67697279,
        0x28207468, 0x31202963, 0x20393939, 0x48207962,
        0x6E656761, 0x64655220, 0x6E616D64, 0x6FBBA36E };

}
于 2012-10-13T21:52:59.947 に答える
6

@DavidHeffernanの提案に従って、C#でも同じ結果を達成しました。

using System;
using System.Text;

namespace CRC32_Tst
{
    class Program
    {
        static void Main(string[] args)
        {
            UInt32[] MyTAB = new UInt32[] { 
                0x000000000, 0x077073096, 0x0EE0E612C, 0x0990951BA, 
                0x0076DC419, 0x0706AF48F, 0x0E963A535, 0x09E6495A3, 
                0x00EDB8832, 0x079DCB8A4, 0x0E0D5E91E, 0x097D2D988, 
                0x009B64C2B, 0x07EB17CBD, 0x0E7B82D07, 0x090BF1D91, 
                0x01DB71064, 0x06AB020F2, 0x0F3B97148, 0x084BE41DE, 
                0x01ADAD47D, 0x06DDDE4EB, 0x0F4D4B551, 0x083D385C7, 
                0x0136C9856, 0x0646BA8C0, 0x0FD62F97A, 0x08A65C9EC, 
                0x014015C4F, 0x063066CD9, 0x0FA0F3D63, 0x08D080DF5, 
                0x03B6E20C8, 0x04C69105E, 0x0D56041E4, 0x0A2677172, 
                0x03C03E4D1, 0x04B04D447, 0x0D20D85FD, 0x0A50AB56B, 
                0x035B5A8FA, 0x042B2986C, 0x0DBBBC9D6, 0x0ACBCF940, 
                0x032D86CE3, 0x045DF5C75, 0x0DCD60DCF, 0x0ABD13D59, 
                0x026D930AC, 0x051DE003A, 0x0C8D75180, 0x0BFD06116, 
                0x021B4F4B5, 0x056B3C423, 0x0CFBA9599, 0x0B8BDA50F, 
                0x02802B89E, 0x05F058808, 0x0C60CD9B2, 0x0B10BE924, 
                0x02F6F7C87, 0x058684C11, 0x0C1611DAB, 0x0B6662D3D, 
                0x076DC4190, 0x001DB7106, 0x098D220BC, 0x0EFD5102A, 
                0x071B18589, 0x006B6B51F, 0x09FBFE4A5, 0x0E8B8D433, 
                0x07807C9A2, 0x00F00F934, 0x09609A88E, 0x0E10E9818, 
                0x07F6A0DBB, 0x0086D3D2D, 0x091646C97, 0x0E6635C01, 
                0x06B6B51F4, 0x01C6C6162, 0x0856530D8, 0x0F262004E, 
                0x06C0695ED, 0x01B01A57B, 0x08208F4C1, 0x0F50FC457, 
                0x065B0D9C6, 0x012B7E950, 0x08BBEB8EA, 0x0FCB9887C, 
                0x062DD1DDF, 0x015DA2D49, 0x08CD37CF3, 0x0FBD44C65, 
                0x04DB26158, 0x03AB551CE, 0x0A3BC0074, 0x0D4BB30E2, 
                0x04ADFA541, 0x03DD895D7, 0x0A4D1C46D, 0x0D3D6F4FB, 
                0x04369E96A, 0x0346ED9FC, 0x0AD678846, 0x0DA60B8D0, 
                0x044042D73, 0x033031DE5, 0x0AA0A4C5F, 0x0DD0D7CC9, 
                0x05005713C, 0x0270241AA, 0x0BE0B1010, 0x0C90C2086, 
                0x05768B525, 0x0206F85B3, 0x0B966D409, 0x0CE61E49F, 
                0x05EDEF90E, 0x029D9C998, 0x0B0D09822, 0x0C7D7A8B4, 
                0x059B33D17, 0x02EB40D81, 0x0B7BD5C3B, 0x0C0BA6CAD, 
                0x0EDB88320, 0x09ABFB3B6, 0x003B6E20C, 0x074B1D29A, 
                0x0EAD54739, 0x09DD277AF, 0x004DB2615, 0x073DC1683, 
                0x0E3630B12, 0x094643B84, 0x00D6D6A3E, 0x07A6A5AA8, 
                0x0E40ECF0B, 0x09309FF9D, 0x00A00AE27, 0x07D079EB1, 
                0x0F00F9344, 0x08708A3D2, 0x01E01F268, 0x06906C2FE, 
                0x0F762575D, 0x0806567CB, 0x0196C3671, 0x06E6B06E7, 
                0x0FED41B76, 0x089D32BE0, 0x010DA7A5A, 0x067DD4ACC, 
                0x0F9B9DF6F, 0x08EBEEFF9, 0x017B7BE43, 0x060B08ED5, 
                0x0D6D6A3E8, 0x0A1D1937E, 0x038D8C2C4, 0x04FDFF252, 
                0x0D1BB67F1, 0x0A6BC5767, 0x03FB506DD, 0x048B2364B, 
                0x0D80D2BDA, 0x0AF0A1B4C, 0x036034AF6, 0x041047A60, 
                0x0DF60EFC3, 0x0A867DF55, 0x0316E8EEF, 0x04669BE79, 
                0x0CB61B38C, 0x0BC66831A, 0x0256FD2A0, 0x05268E236, 
                0x0CC0C7795, 0x0BB0B4703, 0x0220216B9, 0x05505262F, 
                0x0C5BA3BBE, 0x0B2BD0B28, 0x02BB45A92, 0x05CB36A04, 
                0x0C2D7FFA7, 0x0B5D0CF31, 0x02CD99E8B, 0x05BDEAE1D, 
                0x09B64C2B0, 0x0EC63F226, 0x0756AA39C, 0x0026D930A, 
                0x09C0906A9, 0x0EB0E363F, 0x072076785, 0x005005713, 
                0x095BF4A82, 0x0E2B87A14, 0x07BB12BAE, 0x00CB61B38, 
                0x092D28E9B, 0x0E5D5BE0D, 0x07CDCEFB7, 0x00BDBDF21, 
                0x086D3D2D4, 0x0F1D4E242, 0x068DDB3F8, 0x01FDA836E, 
                0x081BE16CD, 0x0F6B9265B, 0x06FB077E1, 0x018B74777, 
                0x088085AE6, 0x0FF0F6A70, 0x066063BCA, 0x011010B5C, 
                0x08F659EFF, 0x0F862AE69, 0x0616BFFD3, 0x0166CCF45, 
                0x0A00AE278, 0x0D70DD2EE, 0x04E048354, 0x03903B3C2, 
                0x0A7672661, 0x0D06016F7, 0x04969474D, 0x03E6E77DB, 
                0x0AED16A4A, 0x0D9D65ADC, 0x040DF0B66, 0x037D83BF0, 
                0x0A9BCAE53, 0x0DEBB9EC5, 0x047B2CF7F, 0x030B5FFE9, 
                0x0BDBDF21C, 0x0CABAC28A, 0x053B39330, 0x024B4A3A6, 
                0x0BAD03605, 0x0CDD70693, 0x054DE5729, 0x023D967BF, 
                0x0B3667A2E, 0x0C4614AB8, 0x05D681B02, 0x02A6F2B94, 
                0x0B40BBE37, 0x0C30C8EA1, 0x05A05DF1B, 0x02D02EF8D, 
                0x074726F50, 0x0736E6F69, 0x0706F4320, 0x067697279, 
                0x028207468, 0x031202963, 0x020393939, 0x048207962, 
                0x06E656761, 0x064655220, 0x06E616D64, 0x06FBBA36E
                            };
            //UInt32 DefaultSeed = 0xffffffff;
            UInt32 UsedSeed = 0x00000000;

            string testStr = "123";
            byte[] inBytes = Encoding.ASCII.GetBytes(testStr);

            UInt32 ret_uint = CalculateHash(MyTAB, UsedSeed, inBytes, 0, inBytes.Length);

            Console.WriteLine("CRC-32 is {0}", ret_uint); //1997126336


            Console.ReadKey();

        }

        public static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
        {
            UInt32 crc = seed;
            for (int i = start; i < size; i++)
                unchecked
                {
                    crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
                }
            return crc;
        }
    }
}
于 2012-10-13T21:57:19.110 に答える