CRC verification functions found on the internet and verification file examples

Source: Internet
Author: User

// Vs2005 and vc6 are compiled and approved. The CRC verification function and related content are found online and changed slightly, call example for writing a verification file </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 hprevinstance, ptchar SZ Cmdline, int icmdshow) <br/>{< br/> tchar sztemp [256]; <br/> initializetablecrc32 (); </P> <p> wsprintf (sztemp, text ("% 08x"), getfilecrc32 (text (". // test file .txt "); <br/> MessageBox (null, sztemp, text (" title "), 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 >>= 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/>}</P> <p> unsigned long getcrc32 (char * buffer, unsigned long length) <br/> {<br/> unsigned long CRC; <br/> CRC = 0 xffffffff; <br/> while (length --) <br/>{< br/> CRC = (CRC> 8) ^ tablecrc32 [(CRC ^ * buffer ++) & 0xff]; <br/>}< br/> return CRC ^ 0 xffffffff; <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 ); // open the file <br/> If (hfile = invalid_handle_value) <br/>{< br/> return-1; <br/>}< br/> length = getfilesize (hfile, null); <br/> buffer = (char *) V Irtualalloc (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); // close the file <br/> CRC = getcrc32 (buffer, length); <br/> virtualfree (buffer, 0, mem_release); </P> <p> return CRC; <br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.