網上找的CRC校正的函數及校正檔案樣本

來源:互聯網
上載者:User

//VS2005、VC6下編譯通過,CRC校正的函數及相關內容是網上找的,自己稍微改動下,寫了個校正檔案用的調用樣本</p><p>#include<windows.h></p><p>#define CRC32_POLYNOMIAL 0xEDB88320</p><p>unsigned long tableCRC32[256];</p><p>void InitializeTableCRC32();<br />unsigned long GetCRC32(PTCHAR buffer, unsigned long length);<br />unsigned long GetFileCRC32(const PTCHAR szFileName);</p><p>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PTCHAR szCmdLine,int iCmdShow)<br />{<br /> TCHAR szTemp[256];<br /> InitializeTableCRC32();</p><p> wsprintf(szTemp,TEXT("%08X"),GetFileCRC32(TEXT(".//測試檔案.txt")));<br /> MessageBox(NULL,szTemp,TEXT("標題"),0);<br />}</p><p>void InitializeTableCRC32()<br />{<br /> int i, j;<br /> unsigned long h = 1;<br /> tableCRC32[0] = 0;<br /> for (i = 128; i; i >>= 1)<br /> {<br /> h = (h >> 1) ^ ((h & 1) ? CRC32_POLYNOMIAL : 0);<br /> for (j = 0; j < 256; j += 2 * i)<br /> {<br /> tableCRC32[i + j] = tableCRC32[j] ^ h;<br /> }<br /> }<br />}</p><p>unsigned long GetCRC32(char *buffer, unsigned long length)<br />{<br /> unsigned long crc;<br /> crc = 0xFFFFFFFF;<br /> while (length--)<br /> {<br /> crc = (crc >> 8) ^ tableCRC32[(crc ^ *buffer++) & 0xFF];<br /> }<br /> return crc ^ 0xFFFFFFFF;<br />}</p><p>unsigned long GetFileCRC32(const PTCHAR szFileName)<br />{<br /> HANDLE hFile;<br /> char *buffer;<br /> unsigned long length,bytesReadWrite,crc;</p><p> hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//開啟檔案<br /> if(hFile==INVALID_HANDLE_VALUE)<br /> {<br /> return -1;<br /> }<br /> length=GetFileSize(hFile,NULL);<br /> buffer=(char *)VirtualAlloc(NULL,length,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE);<br /> if(!ReadFile(hFile,buffer,length,&bytesReadWrite,NULL))<br /> {<br /> return -1;<br /> }<br /> CloseHandle(hFile);//關閉檔案<br /> crc=GetCRC32(buffer,length);<br /> VirtualFree(buffer,0,MEM_RELEASE);</p><p> return crc;<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.