天下

记录修行的印记

CRC32算法(查表法)代码

#include <windows.h>
#include 
<stdio.h>

void PrintCrcTable()
{
    
//Poly = 0xedb88320 WinRAR Poly
    DWORD Val;
    
for (DWORD i = 0; i < 256; i++)
    {
        Val 
= i;
        
for (DWORD k = 0; k < 8; k++)
        {
            
if (Val & 1)
                Val 
= 0xedb88320L ^ (Val >> 1);
            
else
                Val 
= Val >> 1;
        }
        printf (
"0x%08x, ", Val);
        
if (0 == ((i+1)%6))
            printf (
"\n");
    }
}



const DWORD Crc32Table[] =
{
    
0x000000000x770730960xee0e612c0x990951ba0x076dc4190x706af48f,
    
0xe963a5350x9e6495a30x0edb88320x79dcb8a40xe0d5e91e0x97d2d988,
    
0x09b64c2b0x7eb17cbd0xe7b82d070x90bf1d910x1db710640x6ab020f2,
    
0xf3b971480x84be41de0x1adad47d0x6ddde4eb0xf4d4b5510x83d385c7,
    
0x136c98560x646ba8c00xfd62f97a0x8a65c9ec0x14015c4f0x63066cd9,
    
0xfa0f3d630x8d080df50x3b6e20c80x4c69105e0xd56041e40xa2677172,
    
0x3c03e4d10x4b04d4470xd20d85fd0xa50ab56b0x35b5a8fa0x42b2986c,
    
0xdbbbc9d60xacbcf9400x32d86ce30x45df5c750xdcd60dcf0xabd13d59,
    
0x26d930ac0x51de003a0xc8d751800xbfd061160x21b4f4b50x56b3c423,
    
0xcfba95990xb8bda50f0x2802b89e0x5f0588080xc60cd9b20xb10be924,
    
0x2f6f7c870x58684c110xc1611dab0xb6662d3d0x76dc41900x01db7106,
    
0x98d220bc0xefd5102a0x71b185890x06b6b51f0x9fbfe4a50xe8b8d433,
    
0x7807c9a20x0f00f9340x9609a88e0xe10e98180x7f6a0dbb0x086d3d2d,
    
0x91646c970xe6635c010x6b6b51f40x1c6c61620x856530d80xf262004e,
    
0x6c0695ed0x1b01a57b0x8208f4c10xf50fc4570x65b0d9c60x12b7e950,
    
0x8bbeb8ea0xfcb9887c0x62dd1ddf0x15da2d490x8cd37cf30xfbd44c65,
    
0x4db261580x3ab551ce0xa3bc00740xd4bb30e20x4adfa5410x3dd895d7,
    
0xa4d1c46d0xd3d6f4fb0x4369e96a0x346ed9fc0xad6788460xda60b8d0,
    
0x44042d730x33031de50xaa0a4c5f0xdd0d7cc90x5005713c0x270241aa,
    
0xbe0b10100xc90c20860x5768b5250x206f85b30xb966d4090xce61e49f,
    
0x5edef90e0x29d9c9980xb0d098220xc7d7a8b40x59b33d170x2eb40d81,
    
0xb7bd5c3b0xc0ba6cad0xedb883200x9abfb3b60x03b6e20c0x74b1d29a,
    
0xead547390x9dd277af0x04db26150x73dc16830xe3630b120x94643b84,
    
0x0d6d6a3e0x7a6a5aa80xe40ecf0b0x9309ff9d0x0a00ae270x7d079eb1,
    
0xf00f93440x8708a3d20x1e01f2680x6906c2fe0xf762575d0x806567cb,
    
0x196c36710x6e6b06e70xfed41b760x89d32be00x10da7a5a0x67dd4acc,
    
0xf9b9df6f0x8ebeeff90x17b7be430x60b08ed50xd6d6a3e80xa1d1937e,
    
0x38d8c2c40x4fdff2520xd1bb67f10xa6bc57670x3fb506dd0x48b2364b,
    
0xd80d2bda0xaf0a1b4c0x36034af60x41047a600xdf60efc30xa867df55,
    
0x316e8eef0x4669be790xcb61b38c0xbc66831a0x256fd2a00x5268e236,
    
0xcc0c77950xbb0b47030x220216b90x5505262f0xc5ba3bbe0xb2bd0b28,
    
0x2bb45a920x5cb36a040xc2d7ffa70xb5d0cf310x2cd99e8b0x5bdeae1d,
    
0x9b64c2b00xec63f2260x756aa39c0x026d930a0x9c0906a90xeb0e363f,
    
0x720767850x050057130x95bf4a820xe2b87a140x7bb12bae0x0cb61b38,
    
0x92d28e9b0xe5d5be0d0x7cdcefb70x0bdbdf210x86d3d2d40xf1d4e242,
    
0x68ddb3f80x1fda836e0x81be16cd0xf6b9265b0x6fb077e10x18b74777,
    
0x88085ae60xff0f6a700x66063bca0x11010b5c0x8f659eff0xf862ae69,
    
0x616bffd30x166ccf450xa00ae2780xd70dd2ee0x4e0483540x3903b3c2,
    
0xa76726610xd06016f70x4969474d0x3e6e77db0xaed16a4a0xd9d65adc,
    
0x40df0b660x37d83bf00xa9bcae530xdebb9ec50x47b2cf7f0x30b5ffe9,
    
0xbdbdf21c0xcabac28a0x53b393300x24b4a3a60xbad036050xcdd70693,
    
0x54de57290x23d967bf0xb3667a2e0xc4614ab80x5d681b020x2a6f2b94,
    
0xb40bbe370xc30c8ea10x5a05df1b0x2d02ef8d
};

DWORD GenerateCrc32(DWORD PartialCrc,PUCHAR  Buffer,DWORD   Length)
{
    DWORD crc;
    DWORD i;

    
// Generate the CRC32 checksum.
    crc = PartialCrc ^ 0xffffffffL;

    
for (i = 0; i < Length; i++)
    {
        crc 
= Crc32Table[(crc ^ Buffer[i]) & 0xff^ (crc >> 8);
    }

    
return (crc ^ 0xffffffffL);
}

//测试程序
int main(int argc,char* argv[])
{
    
char* szFileName = NULL;
    
if (argc<2)
    {
        printf(
"Usage:%s <filename> \r\n",argv[0]);
        
return -1;
    }
    szFileName 
= argv[1];
    FILE 
*fp = fopen(szFileName,"rb");
    
if(NULL == fp) 
    {
        printf(
"Open %s error:%d \r\n",szFileName);
        
return 0;
    }

    DWORD crc32 
= 0;
    BYTE c 
= 0;
    
while(1 == fread(&c,1,1,fp))
        crc32 
= GenerateCrc32(crc32,&c,1);

    fclose(fp);

    printf(
"CRC32 = 0x%08X", crc32);
    
return 0;
}

posted on 2012-04-18 18:02 天下 阅读(10461) 评论(0)  编辑 收藏 引用 所属分类: Win32算法


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论