Programming 版 (精华区)

发信人: hitool (HIT-007), 信区: Programming
标  题: [程序大战]20040907
发信站: 哈工大紫丁香 (Mon Oct 18 20:20:35 2004), 站内

这是一个通过查表实现的快速算法。

unsigned long CalcTable32[256]=
{
        0x00000000,0xDB064171,0x6D0AC393,0xB60C82E2,
        0xDB158626,0x0013C757,0xB61F45B5,0x6D1904C4,
        0x6D2D4D3D,0xB62B0C4C,0x00278EAE,0xDB21CFDF,
        0xB638CB1B,0x6D3E8A6A,0xDB320888,0x003449F9,
        0xDA5A9A7A,0x015CDB0B,0xB75059E9,0x6C561898,
        0x014F1C5C,0xDA495D2D,0x6C45DFCF,0xB7439EBE,
        0xB777D747,0x6C719636,0xDA7D14D4,0x017B55A5,
        0x6C625161,0xB7641010,0x016892F2,0xDA6ED383,
        0x6FB37585,0xB4B534F4,0x02B9B616,0xD9BFF767,
        0xB4A6F3A3,0x6FA0B2D2,0xD9AC3030,0x02AA7141,
        0x029E38B8,0xD99879C9,0x6F94FB2B,0xB492BA5A,
        0xD98BBE9E,0x028DFFEF,0xB4817D0D,0x6F873C7C,
        0xB5E9EFFF,0x6EEFAE8E,0xD8E32C6C,0x03E56D1D,
        0x6EFC69D9,0xB5FA28A8,0x03F6AA4A,0xD8F0EB3B,
        0xD8C4A2C2,0x03C2E3B3,0xB5CE6151,0x6EC82020,
        0x03D124E4,0xD8D76595,0x6EDBE777,0xB5DDA606,
        0xDF66EA0B,0x0460AB7A,0xB26C2998,0x696A68E9,
        0x04736C2D,0xDF752D5C,0x6979AFBE,0xB27FEECF,
        0xB24BA736,0x694DE647,0xDF4164A5,0x044725D4,
        0x695E2110,0xB2586061,0x0454E283,0xDF52A3F2,
        0x053C7071,0xDE3A3100,0x6836B3E2,0xB330F293,
        0xDE29F657,0x052FB726,0xB32335C4,0x682574B5,
        0x68113D4C,0xB3177C3D,0x051BFEDF,0xDE1DBFAE,
        0xB304BB6A,0x6802FA1B,0xDE0E78F9,0x05083988,
        0xB0D59F8E,0x6BD3DEFF,0xDDDF5C1D,0x06D91D6C,
        0x6BC019A8,0xB0C658D9,0x06CADA3B,0xDDCC9B4A,
        0xDDF8D2B3,0x06FE93C2,0xB0F21120,0x6BF45051,
        0x06ED5495,0xDDEB15E4,0x6BE79706,0xB0E1D677,
        0x6A8F05F4,0xB1894485,0x0785C667,0xDC838716,
        0xB19A83D2,0x6A9CC2A3,0xDC904041,0x07960130,
        0x07A248C9,0xDCA409B8,0x6AA88B5A,0xB1AECA2B,
        0xDCB7CEEF,0x07B18F9E,0xB1BD0D7C,0x6ABB4C0D,
        0x65CB9567,0xBECDD416,0x08C156F4,0xD3C71785,
        0xBEDE1341,0x65D85230,0xD3D4D0D2,0x08D291A3,
        0x08E6D85A,0xD3E0992B,0x65EC1BC9,0xBEEA5AB8,
        0xD3F35E7C,0x08F51F0D,0xBEF99DEF,0x65FFDC9E,
        0xBF910F1D,0x64974E6C,0xD29BCC8E,0x099D8DFF,
        0x6484893B,0xBF82C84A,0x098E4AA8,0xD2880BD9,
        0xD2BC4220,0x09BA0351,0xBFB681B3,0x64B0C0C2,
        0x09A9C406,0xD2AF8577,0x64A30795,0xBFA546E4,
        0x0A78E0E2,0xD17EA193,0x67722371,0xBC746200,
        0xD16D66C4,0x0A6B27B5,0xBC67A557,0x6761E426,
        0x6755ADDF,0xBC53ECAE,0x0A5F6E4C,0xD1592F3D,
        0xBC402BF9,0x67466A88,0xD14AE86A,0x0A4CA91B,
        0xD0227A98,0x0B243BE9,0xBD28B90B,0x662EF87A,
        0x0B37FCBE,0xD031BDCF,0x663D3F2D,0xBD3B7E5C,
        0xBD0F37A5,0x660976D4,0xD005F436,0x0B03B547,
        0x661AB183,0xBD1CF0F2,0x0B107210,0xD0163361,
        0xBAAD7F6C,0x61AB3E1D,0xD7A7BCFF,0x0CA1FD8E,
        0x61B8F94A,0xBABEB83B,0x0CB23AD9,0xD7B47BA8,
        0xD7803251,0x0C867320,0xBA8AF1C2,0x618CB0B3,
        0x0C95B477,0xD793F506,0x619F77E4,0xBA993695,
        0x60F7E516,0xBBF1A467,0x0DFD2685,0xD6FB67F4,
        0xBBE26330,0x60E42241,0xD6E8A0A3,0x0DEEE1D2,
        0x0DDAA82B,0xD6DCE95A,0x60D06BB8,0xBBD62AC9,
        0xD6CF2E0D,0x0DC96F7C,0xBBC5ED9E,0x60C3ACEF,
        0xD51E0AE9,0x0E184B98,0xB814C97A,0x6312880B,
        0x0E0B8CCF,0xD50DCDBE,0x63014F5C,0xB8070E2D,
        0xB83347D4,0x633506A5,0xD5398447,0x0E3FC536,
        0x6326C1F2,0xB8208083,0x0E2C0261,0xD52A4310,
        0x0F449093,0xD442D1E2,0x624E5300,0xB9481271,
        0xD45116B5,0x0F5757C4,0xB95BD526,0x625D9457,
        0x6269DDAE,0xB96F9CDF,0x0F631E3D,0xD4655F4C,
        0xB97C5B88,0x627A1AF9,0xD476981B,0x0F70D96A
};

BYTE NFindEntry32(BYTE b3,BYTE *b2,BYTE *b1,BYTE *b0)
{
        *b2=HIBYTE(HIWORD(CalcTable32[b3]));
        *b1=LOBYTE(LOWORD(CalcTable32[b3]));
        *b0=LOBYTE(HIWORD(CalcTable32[b3]));
        return HIBYTE(LOWORD(CalcTable32[b3]));
}

void FindCRC32(void)
{
        DWORD wanted=0x12345678; //Wanted CRC32
        DWORD crc;
        unsigned char buf[4];
        crc=0xffffffff;
        wanted=~wanted;
        CString res;
        BYTE a0,a1,a2,a3,f0,f1,f2,f3,en1,en2,en3,en4,e3,e2,e1,e0,d3,d2,d1,d0,c3,c2,c1,c0,b3,b2,b1,b0;
        a0=LOBYTE(LOWORD(crc));
        a1=HIBYTE(LOWORD(crc));
        a2=LOBYTE(HIWORD(crc));
        a3=HIBYTE(HIWORD(crc));
        f0=LOBYTE(LOWORD(wanted));
        f1=HIBYTE(LOWORD(wanted));
        f2=LOBYTE(HIWORD(wanted));
        f3=HIBYTE(HIWORD(wanted));
        e3=f3;
        en4=NFindEntry32(e3,&e2,&e1,&e0);
        d3=f2^e2;
        en3=NFindEntry32(d3,&d2,&d1,&d0);
        c3=f1^e1^d2;
        en2=NFindEntry32(c3,&c2,&c1,&c0);
        b3=f0^e0^d1^c2;
        en1=NFindEntry32(b3,&b2,&b1,&b0);
        buf[0]=en1^a0;
        buf[1]=en2^b0^a1;
        buf[2]=en3^c0^b1^a2;
        buf[3]=en4^d0^c1^b2^a3;
        res.Format("Input is 0x%02X 0x%02X 0x%02X 0x%02X",buf[0],buf[1],buf[2],buf[3]);
        AfxMessageBox(res);
}

--
H     H  IIIII  TTTTTTT                 00      00    7777777
H     H    I       T                   0  0    0  0        7
HHHHHHH    I       T      --------    0    0  0    0      7
H     H    I       T                   0  0    0  0      7
H     H    I       T                    00      00      7


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 221.208.115.*]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.813毫秒