Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.0 KiB

  1. /*** crc32.h - 32-bit CRC generator
  2. *
  3. * Microsoft Confidential
  4. * Copyright (C) Microsoft Corporation 1993-1994
  5. * All Rights Reserved.
  6. *
  7. * Author:
  8. * Mike Sliger
  9. *
  10. * History:
  11. * 10-Aug-1993 bens Copied from IMG2DMF directory.
  12. *
  13. * Usage:
  14. * ULONG crc;
  15. * //** Compute first block
  16. * crc = CRC32Compute(pb,cb,CRC32_INITIAL_VALUE);
  17. * //** Compute remaining blocks
  18. * crc = CRC32Compute(pb,cb,crc);
  19. * ...
  20. * //** When you're done, crc has the CRC.
  21. *
  22. * NOTE: See example function getFileChecksum() below that
  23. * computes the checksum of a file!
  24. */
  25. // ** Must use this as initial value for CRC
  26. #define CRC32_INITIAL_VALUE 0L
  27. /*** CRC32Compute - Compute 32-bit
  28. *
  29. * Entry:
  30. * pb - Pointer to buffer to computer CRC on
  31. * cb - Count of bytes in buffer to CRC
  32. * crc32 - Result from previous CRC32Compute call (on first call
  33. * to CRC32Compute, must be CRC32_INITIAL_VALUE!!!!).
  34. *
  35. * Exit:
  36. * Returns updated CRC value.
  37. */
  38. DWORD CRC32Compute(BYTE *pb,unsigned cb,ULONG crc32);
  39. //** Include nice sample -- don't compile it
  40. //
  41. #ifdef HERE_IS_A_SAMPLE
  42. /*** getFileChecksum - Compute file checksum
  43. *
  44. * Entry:
  45. * pszFile - Filespec
  46. * pchecksum - Receives 32-bit checksum of file
  47. * perr - ERROR structure
  48. *
  49. * Exit-Success:
  50. * Returns TRUE, *pchecksum filled in.
  51. *
  52. * Exit-Failure:
  53. * Returns FALSE; perr filled in with error.
  54. */
  55. BOOL getFileChecksum(char *pszFile, ULONG *pchecksum, PERROR perr)
  56. {
  57. #define cbCSUM_BUFFER 4096 // File buffer size
  58. int cb; // Amount of data in read buffer
  59. ULONG csum=CRC32_INITIAL_VALUE; // Initialize CRC
  60. char *pb=NULL; // Read buffer
  61. int hf=-1; // File handle
  62. int rc;
  63. BOOL result=FALSE; // Assume failure
  64. //** Initialize returned checksum (assume failure)
  65. *pchecksum = csum;
  66. //** Allocate file buffer
  67. if (!(pb = MemAlloc(cbCSUM_BUFFER))) {
  68. ErrSet(perr,pszDIAERR_NO_MEMORY_CRC,"%s",pszFile);
  69. return FALSE;
  70. }
  71. //** Open file
  72. hf = _open(pszFile,_O_RDONLY | _O_BINARY,&rc);
  73. if (hf == -1) {
  74. ErrSet(perr,pszDIAERR_OPEN_FAILED,"%s",pszFile);
  75. goto Exit;
  76. }
  77. //** Compute checksum
  78. while (_eof(hf) == 0) {
  79. cb = _read(hf,pb,cbCSUM_BUFFER);
  80. if (cb == -1) {
  81. ErrSet(perr,pszDIAERR_READ_FAIL_CRC,"%s",pszFile);
  82. goto Exit;
  83. }
  84. if (cb != 0) {
  85. csum = CRC32Compute(pb,cb,csum); // Accumulate CRC
  86. }
  87. }
  88. //** Success
  89. result = TRUE;
  90. *pchecksum = csum; // Store checksum for caller
  91. Exit:
  92. if (hf != -1) {
  93. _close(hf);
  94. }
  95. if (pb != NULL) {
  96. MemFree(pb);
  97. }
  98. return result;
  99. } /* getFileChecksum() */
  100. #endif // HERE_IS_A_SAMPLE