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.

260 lines
6.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: hshstrss.cpp
  8. //
  9. // Contents: Hashing Stress
  10. //
  11. // History: 21-Nov-1997 pberkman created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include "crypthlp.h"
  16. BOOL _HashFile(HANDLE hFile, HCRYPTPROV hProv, char *pszFile);
  17. BOOL fVerbose = FALSE;
  18. DWORD dwTotalBytes = 0;
  19. extern "C" int __cdecl wmain(int argc, WCHAR **wargv)
  20. {
  21. cWArgv_ *pArgs;
  22. WCHAR *pwszFile;
  23. BOOL fFailed;
  24. DWORD dwTotal;
  25. HANDLE hFile;
  26. int iRet;
  27. BOOL fFind;
  28. HANDLE hFind;
  29. WIN32_FIND_DATA sFindData;
  30. HCRYPTPROV hProv;
  31. COleDateTime tStart;
  32. COleDateTime tEnd;
  33. COleDateTimeSpan tsTotal;
  34. iRet = 1; // cmd fail
  35. dwTotal = 0;
  36. hFind = INVALID_HANDLE_VALUE;
  37. if (!(pArgs = new cWArgv_((HINSTANCE)GetModuleHandle(NULL), &fFailed)))
  38. {
  39. goto MemoryError;
  40. }
  41. if (fFailed)
  42. {
  43. goto MemoryError;
  44. }
  45. pArgs->AddUsageText(IDS_USAGETEXT_USAGE, IDS_USAGETEXT_OPTIONS,
  46. IDS_USAGETEXT_CMDFILE, IDS_USAGETEXT_ADD,
  47. IDS_USAGETEXT_OPTPARAM);
  48. pArgs->Add2List(IDS_PARAM_HELP, IDS_PARAMTEXT_HELP, WARGV_VALUETYPE_BOOL, (void *)FALSE);
  49. pArgs->Add2List(IDS_PARAM_VERBOSE, IDS_PARAMTEXT_VERBOSE, WARGV_VALUETYPE_BOOL, (void *)FALSE);
  50. if (!(pArgs->Fill(argc, wargv)) ||
  51. (pArgs->GetValue(IDS_PARAM_HELP)))
  52. {
  53. wprintf(L"%s", pArgs->GetUsageString());
  54. goto NeededHelp;
  55. }
  56. fVerbose = (BOOL)((DWORD_PTR)pArgs->GetValue(IDS_PARAM_VERBOSE));
  57. if (!(pwszFile = pArgs->GetFileName()))
  58. {
  59. wprintf(L"%s", pArgs->GetUsageString());
  60. goto ParamError;
  61. }
  62. //
  63. // start our timer
  64. //
  65. tStart = COleDateTime::GetCurrentTime();
  66. if (fVerbose)
  67. {
  68. printf("\nProcessing:");
  69. }
  70. char szFile[MAX_PATH];
  71. szFile[0] = NULL;
  72. WideCharToMultiByte(0, 0, pwszFile, -1, &szFile[0], MAX_PATH, NULL, NULL);
  73. if ((hFind = FindFirstFile(&szFile[0], &sFindData)) == INVALID_HANDLE_VALUE)
  74. {
  75. goto FileFindError;
  76. }
  77. fFind = TRUE;
  78. hProv = I_CryptGetDefaultCryptProv(0);
  79. while (fFind)
  80. {
  81. if (!(sFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  82. {
  83. hFile = CreateFile(sFindData.cFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  84. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  85. if (hFile != INVALID_HANDLE_VALUE)
  86. {
  87. if (_HashFile(hFile, hProv, sFindData.cFileName))
  88. {
  89. dwTotal++;
  90. }
  91. }
  92. }
  93. fFind = FindNextFile(hFind, &sFindData);
  94. }
  95. //
  96. // end timer
  97. //
  98. tEnd = COleDateTime::GetCurrentTime();
  99. tsTotal = tEnd - tStart;
  100. printf("\n");
  101. printf("\nTiming:");
  102. printf("\nTotal files: %ld", dwTotal);
  103. printf("\nProcessing time: %s", (LPCSTR)tsTotal.Format("%D:%H:%M:%S"));
  104. printf("\nAverage per file: %f", (double)tsTotal.GetTotalSeconds() / (double)dwTotal);
  105. printf("\nTotal bytes: %ld (k)", dwTotalBytes / 1000L);
  106. printf("\nAverage per (k): %f", (double)tsTotal.GetTotalSeconds() / (double)(dwTotalBytes / 1000L));
  107. printf("\nAverage bytes per file (k): %f", (double)(dwTotalBytes / 1000L) / (double)dwTotal);
  108. printf("\n");
  109. iRet = 0;
  110. CommonReturn:
  111. DELETE_OBJECT(pArgs);
  112. if (hFind != INVALID_HANDLE_VALUE)
  113. {
  114. FindClose(hFind);
  115. }
  116. return(iRet);
  117. ErrorReturn:
  118. iRet = 1;
  119. goto CommonReturn;
  120. TRACE_ERROR_EX(DBG_SS_APP, MemoryError);
  121. TRACE_ERROR_EX(DBG_SS_APP, ParamError);
  122. TRACE_ERROR_EX(DBG_SS_APP, FileFindError);
  123. TRACE_ERROR_EX(DBG_SS_APP, NeededHelp);
  124. }
  125. BOOL _HashFile(HANDLE hFile, HCRYPTPROV hProv, char *pszFile)
  126. {
  127. HCRYPTHASH hHash;
  128. DWORD cbHash;
  129. BYTE bHash[30];
  130. BYTE *pbFile;
  131. DWORD cbFile;
  132. HANDLE hMappedFile;
  133. BOOL fRet;
  134. pbFile = NULL;
  135. hMappedFile = NULL;
  136. hHash = NULL;
  137. if (fVerbose)
  138. {
  139. printf("\n %s ", pszFile);
  140. }
  141. hMappedFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  142. if (!(hMappedFile) || (hMappedFile == INVALID_HANDLE_VALUE))
  143. {
  144. hMappedFile = NULL;
  145. goto CreateMapError;
  146. }
  147. if (!(pbFile = (BYTE *)MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0)))
  148. {
  149. goto MapViewError;
  150. }
  151. cbFile = GetFileSize(hFile, NULL);
  152. if (!(CryptCreateHash(hProv, CALG_SHA1, NULL, 0, &hHash)))
  153. {
  154. goto CreateHashError;
  155. }
  156. if (!(CryptHashData(hHash, pbFile, cbFile, 0)))
  157. {
  158. goto HashDataError;
  159. }
  160. cbHash = 30;
  161. if (!(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &cbHash, 0)))
  162. {
  163. goto HashParamError;
  164. }
  165. dwTotalBytes += cbFile;
  166. if (fVerbose)
  167. {
  168. DWORD i;
  169. for (i = 0; i < cbHash; i++)
  170. {
  171. printf("%02.2X", bHash[i]);
  172. }
  173. }
  174. fRet = TRUE;
  175. CommonReturn:
  176. if (hHash)
  177. {
  178. CryptDestroyHash(hHash);
  179. }
  180. if (hMappedFile)
  181. {
  182. CloseHandle(hMappedFile);
  183. }
  184. if (pbFile)
  185. {
  186. UnmapViewOfFile(pbFile);
  187. }
  188. return(fRet);
  189. ErrorReturn:
  190. if (fVerbose)
  191. {
  192. printf("*failed*");
  193. }
  194. fRet = FALSE;
  195. goto CommonReturn;
  196. TRACE_ERROR_EX(DBG_SS_APP, CreateMapError);
  197. TRACE_ERROR_EX(DBG_SS_APP, MapViewError);
  198. TRACE_ERROR_EX(DBG_SS_APP, CreateHashError);
  199. TRACE_ERROR_EX(DBG_SS_APP, HashDataError);
  200. TRACE_ERROR_EX(DBG_SS_APP, HashParamError);
  201. }