能力值:
( LV2,RANK:10 )
3 楼
这个windows api有现成的函数可以直接调用,效率很高。不用自己编写。
自己编写的往往没人家效率高,而且还容易有错误和漏洞。
能力值:
( LV13,RANK:357 )
15 楼
收到邮件问c++源码的,以前刚好写过. #include <stdio.h> #include <string.h> #pragma pack(push, 1) struct t_CRCdata { unsigned char Num1; unsigned char Num2; unsigned char Num3; unsigned char Num4; }; #pragma pack(pop) //CRC32编辑 //输入 //长度 //偏移: <0 表示末尾添加, 其他值表示在某位偏移位置覆盖 //自己要的crc值 //填充的4字节值,把这个值覆盖到偏移位置就可以实现你要的crc int CRC32Edit(unsigned char *byt,int bytLen,int lOffset, int lcrc,int &retlCRC) { int crc32Result = 0; int i,j,dwCrc,iLookup,CRC32Edit2 = 0; int k; t_CRCdata cr1; //常数 #define NumNum0 0 #define NumNum1 0x1 #define NumNum2 0x2 #define NumNum8 0x8 #define Num255 0xFF #define Num256 0x100 #define Num16777215 0xFFFFFF #define dwPolynomial 0xEDB88320 #define Num2147483647 0x7FFFFFFF #define NumNegative1 0xFFFFFFFF #define NumNegative2 0xFFFFFFFE #define NumNegative256 0xFFFFFF00 //CRC32表 int crc32Table[0x100]; t_CRCdata CRCdata[256]; //初始化CRC32表***************************** for(i = NumNum0 ; i <= Num255;i++) { dwCrc = i; for(j = NumNum8; j >= NumNum1; j+=NumNegative1) { if (dwCrc & NumNum1) { dwCrc = ((dwCrc & NumNegative2) / NumNum2) & Num2147483647; dwCrc = dwCrc ^ dwPolynomial; } else{ dwCrc = ((dwCrc & NumNegative2) / NumNum2) & Num2147483647; } } crc32Table[i] = dwCrc; memcpy (&CRCdata[i], &crc32Table[i],4); } crc32Result = NumNegative1;// '初始化 if( lOffset < 0) { lOffset = bytLen; }else{ if(lOffset > bytLen -1) { lOffset = bytLen; } } if( lOffset > 0) { //'计算CRC32码******************************* for(i = 0; i<= lOffset - 1; i++){ iLookup = (crc32Result & Num255) ^ byt[i] ;// '第四位 xor 新字节 crc32Result = ((crc32Result & NumNegative256) / Num256) & Num16777215;// '前三位 crc32Result = crc32Result ^ crc32Table[iLookup];// '前三位*3 xor 表(第四位*1 xor 新字节*1)*4 } } CRC32Edit2 = crc32Result ^ 0xffffffff;// '计算前面的crc值,返回是什么样的 // //'计算后面的 k = bytLen - 1 - lOffset - 3; if(k > 0 ) {// '后面是否有数据 int k2 = k; k = lcrc ^ 0xffffffff; memcpy (&cr1, &k,4); // //'反向CRC计算 for(int j = k2 - 1; j>=0; j--) { for(int i = 0; i<=255; i++) { if(cr1.Num4 == CRCdata[i].Num4) { break; } } cr1.Num4 = cr1.Num3 ^ CRCdata[i].Num3; cr1.Num3 = cr1.Num2 ^ CRCdata[i].Num2; cr1.Num2 = cr1.Num1 ^ CRCdata[i].Num1; cr1.Num1 = i ^ byt[j + lOffset + 4]; } } else { k = lcrc ^ 0xffffffff; memcpy(&cr1, &k,4); } // //'要得出文件CRC值=lCRC32 之前部分的CRC必须满足的条件:cr1的值 // //'计算 for (j = 0; j <=3; j++){ for (int i = 0; i <=255; i++) { if(cr1.Num4 == CRCdata[i].Num4) break; } cr1.Num4 = cr1.Num3 ^ CRCdata[i].Num3; cr1.Num3 = cr1.Num2 ^ CRCdata[i].Num2; cr1.Num2 = cr1.Num1 ^ CRCdata[i].Num1; cr1.Num1 = i; } memcpy( &k, &cr1,4); k = k ^ crc32Result; retlCRC = k; //'计算出来的是4字节覆盖的值,不是crc return CRC32Edit2; }
能力值:
( LV13,RANK:357 )
16 楼
int CRC32(unsigned char *byt,int bytLen) { int lcrc = 0 ; #define Limit 0xEDB88320 bytLen--; int i,x,crc; int crcTable[256]; for(i=0;i<=255;i++) { for(crc=i,x=0;x<8;x++) { if(crc & 1) crc=(((crc & 0xFFFFFFFE) / 2) & 0x7FFFFFFF) ^ Limit; else crc=(crc & 0xFFFFFFFE) / 2; } crcTable[i] = crc; } if(bytLen<0)return 0; unsigned int crcResult = (~lcrc); for(i = 0;i<= bytLen;i++) { crcResult=(crcResult >> 8 ^ crcTable[((crcResult & 255) ^ byt[i]) & 255]); } return ~crcResult; } void main() { unsigned char p1[] = {0x00,0x2C,0x00,0x01,0x45,0x36,0x30,0x0C,0x68,0x65,0x6C,0x6C,0x5F,0x64,0x65,0x66,0x61,0x75,0x6C,0x74,0x0C,0x68,0x65,0x6C,0x6C,0x5F,0x64,0x65,0x66,0x61,0x75,0x6C, 0x74,0x0C,0x68,0x65,0x6C,0x6C,0x5F,0x64,0x65,0x66,0x61,0x75,0x6C,0x74,0x00,0x20,0,0,0,0}; int crcout; int CRC_I_WANT = 0; CRC32Edit(p1,48,-1,CRC_I_WANT,crcout); //这里的填充位置是-1,末尾填充, 也就是 p[48]的位置填充 printf("填充值为 %08X\n",crcout); memcpy(p1 + 48, &crcout,4); int crc = CRC32(p1,48 + 4); printf("CRC的值 = %08X \n",crc); }