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.

605 lines
13 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <string.h>
  8. #define ONEK 1000
  9. #define FIVEK 5000
  10. #define TENK 10000
  11. #define ONEHUNK 100000
  12. #define ONEMIL 1000000
  13. //
  14. // Define local types.
  15. //
  16. typedef struct _PERFINFO {
  17. DWORD StartTime;
  18. DWORD StopTime;
  19. LPSTR Title;
  20. DWORD Iterations;
  21. } PERFINFO, *PPERFINFO;
  22. VOID
  23. FinishBenchMark (
  24. IN PPERFINFO PerfInfo
  25. )
  26. {
  27. DWORD ContextSwitches;
  28. DWORD Duration;
  29. DWORD Performance;
  30. //
  31. // Print results and announce end of test.
  32. //
  33. PerfInfo->StopTime = GetTickCount();
  34. Duration = PerfInfo->StopTime - PerfInfo->StartTime;
  35. printf(" Test time in milliseconds %d\n", Duration);
  36. printf(" Number of iterations %d\n", PerfInfo->Iterations);
  37. Performance = PerfInfo->Iterations * 1000 / Duration;
  38. printf(" Iterations per second %d\n", Performance);
  39. printf("*** End of Test ***\n\n");
  40. return;
  41. }
  42. VOID
  43. StartBenchMark (
  44. IN PCHAR Title,
  45. IN DWORD Iterations,
  46. IN PPERFINFO PerfInfo
  47. )
  48. {
  49. //
  50. // Announce start of test and the number of iterations.
  51. //
  52. printf("*** Start of test ***\n %s\n", Title);
  53. PerfInfo->Title = Title;
  54. PerfInfo->Iterations = Iterations;
  55. PerfInfo->StartTime = GetTickCount();
  56. return;
  57. }
  58. HANDLE
  59. APIENTRY
  60. FastFindOpenDir(
  61. LPCWSTR lpFileName
  62. )
  63. {
  64. HANDLE hFindFile;
  65. NTSTATUS Status;
  66. OBJECT_ATTRIBUTES Obja;
  67. UNICODE_STRING FileName;
  68. UNICODE_STRING PathName;
  69. IO_STATUS_BLOCK IoStatusBlock;
  70. BOOLEAN TranslationStatus;
  71. PVOID FreeBuffer;
  72. UNICODE_STRING UnicodeInput;
  73. BOOLEAN EndsInDot;
  74. RtlInitUnicodeString(&UnicodeInput,lpFileName);
  75. //
  76. // Bogus code to workaround ~* problem
  77. //
  78. if ( UnicodeInput.Buffer[(UnicodeInput.Length>>1)-1] == (WCHAR)'.' ) {
  79. EndsInDot = TRUE;
  80. }
  81. else {
  82. EndsInDot = FALSE;
  83. }
  84. TranslationStatus = RtlDosPathNameToNtPathName_U(
  85. lpFileName,
  86. &PathName,
  87. &FileName.Buffer,
  88. NULL
  89. );
  90. if ( !TranslationStatus ) {
  91. return NULL;
  92. }
  93. FreeBuffer = PathName.Buffer;
  94. InitializeObjectAttributes(
  95. &Obja,
  96. &PathName,
  97. OBJ_CASE_INSENSITIVE,
  98. NULL,
  99. NULL
  100. );
  101. //
  102. // Open the directory for list access
  103. //
  104. Status = NtOpenFile(
  105. &hFindFile,
  106. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  107. &Obja,
  108. &IoStatusBlock,
  109. FILE_SHARE_READ | FILE_SHARE_WRITE,
  110. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  111. );
  112. if ( !NT_SUCCESS(Status) ) {
  113. return NULL;
  114. }
  115. return hFindFile;
  116. }
  117. BOOL
  118. FastFind(
  119. HANDLE hFindFile,
  120. LPCWSTR lpFileName,
  121. LPWIN32_FIND_DATAW lpFindFileData
  122. )
  123. {
  124. NTSTATUS Status;
  125. OBJECT_ATTRIBUTES Obja;
  126. UNICODE_STRING FileName;
  127. UNICODE_STRING PathName;
  128. IO_STATUS_BLOCK IoStatusBlock;
  129. PVOID FreeBuffer;
  130. UNICODE_STRING UnicodeInput;
  131. BOOLEAN EndsInDot;
  132. PFILE_BOTH_DIR_INFORMATION DirectoryInfo;
  133. CHAR Buffer[MAX_PATH*2 + sizeof(FILE_BOTH_DIR_INFORMATION)];
  134. RtlInitUnicodeString(&FileName,lpFileName);
  135. DirectoryInfo = (PFILE_BOTH_DIR_INFORMATION)Buffer;
  136. Status = NtQueryDirectoryFile(
  137. hFindFile,
  138. NULL,
  139. NULL,
  140. NULL,
  141. &IoStatusBlock,
  142. DirectoryInfo,
  143. sizeof(Buffer),
  144. FileBothDirectoryInformation,
  145. TRUE,
  146. &FileName,
  147. TRUE
  148. );
  149. if ( !NT_SUCCESS(Status) ) {
  150. return FALSE;
  151. }
  152. //
  153. // Attributes are composed of the attributes returned by NT.
  154. //
  155. lpFindFileData->dwFileAttributes = DirectoryInfo->FileAttributes;
  156. lpFindFileData->ftCreationTime = *(LPFILETIME)&DirectoryInfo->CreationTime;
  157. lpFindFileData->ftLastAccessTime = *(LPFILETIME)&DirectoryInfo->LastAccessTime;
  158. lpFindFileData->ftLastWriteTime = *(LPFILETIME)&DirectoryInfo->LastWriteTime;
  159. lpFindFileData->nFileSizeHigh = DirectoryInfo->EndOfFile.HighPart;
  160. lpFindFileData->nFileSizeLow = DirectoryInfo->EndOfFile.LowPart;
  161. RtlMoveMemory( lpFindFileData->cFileName,
  162. DirectoryInfo->FileName,
  163. DirectoryInfo->FileNameLength );
  164. lpFindFileData->cFileName[DirectoryInfo->FileNameLength >> 1] = UNICODE_NULL;
  165. RtlMoveMemory( lpFindFileData->cAlternateFileName,
  166. DirectoryInfo->ShortName,
  167. DirectoryInfo->ShortNameLength );
  168. lpFindFileData->cAlternateFileName[DirectoryInfo->ShortNameLength >> 1] = UNICODE_NULL;
  169. return TRUE;
  170. }
  171. VOID
  172. FastFindTest(
  173. VOID
  174. )
  175. {
  176. PERFINFO PerfInfo;
  177. int i;
  178. HANDLE hFind;
  179. WIN32_FIND_DATAW FindFileData;
  180. BOOL b;
  181. hFind = FastFindOpenDir(L"d:\\testdir\\client1\\bench");
  182. if ( !hFind ) {
  183. printf("Failed\n");
  184. return;
  185. }
  186. StartBenchMark(
  187. "FastFind Test",
  188. ONEK,
  189. &PerfInfo
  190. );
  191. for ( i=0;i<5*ONEK;i++) {
  192. //
  193. // do 5 calls 3 work, 2 don't
  194. //
  195. b = FastFind(hFind,L"a",&FindFileData);
  196. if ( !b ) {
  197. printf("Test Failure a\n");
  198. ExitProcess(0);
  199. }
  200. b = FastFind(hFind,L"ab",&FindFileData);
  201. if ( b ) {
  202. printf("Test Failure ab\n");
  203. ExitProcess(0);
  204. }
  205. b = FastFind(hFind,L"abc",&FindFileData);
  206. if ( !b ) {
  207. printf("Test Failure abc\n");
  208. ExitProcess(0);
  209. }
  210. b = FastFind(hFind,L"da",&FindFileData);
  211. if ( b ) {
  212. printf("Test Failure da\n");
  213. ExitProcess(0);
  214. }
  215. b = FastFind(hFind,L"dxxa",&FindFileData);
  216. if ( !b ) {
  217. printf("Test Failure dxxa\n");
  218. ExitProcess(0);
  219. }
  220. }
  221. FinishBenchMark(&PerfInfo);
  222. }
  223. VOID
  224. FindFirstTest(
  225. VOID
  226. )
  227. {
  228. PERFINFO PerfInfo;
  229. int i;
  230. HANDLE hFind;
  231. WIN32_FIND_DATAW FindFileData;
  232. BOOL b;
  233. StartBenchMark(
  234. "Stock FindFirst Test",
  235. ONEK,
  236. &PerfInfo
  237. );
  238. for ( i=0;i<5*ONEK;i++) {
  239. //
  240. // do 5 calls 3 work, 2 don't
  241. //
  242. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\a",&FindFileData);
  243. if ( hFind == INVALID_HANDLE_VALUE ) {
  244. printf("Test Failure a\n");
  245. ExitProcess(0);
  246. }
  247. FindClose(hFind);
  248. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\ab",&FindFileData);
  249. if ( hFind != INVALID_HANDLE_VALUE ) {
  250. printf("Test Failure ab\n");
  251. ExitProcess(0);
  252. }
  253. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\abc",&FindFileData);
  254. if ( hFind == INVALID_HANDLE_VALUE ) {
  255. printf("Test Failure abc\n");
  256. ExitProcess(0);
  257. }
  258. FindClose(hFind);
  259. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\da",&FindFileData);
  260. if ( hFind != INVALID_HANDLE_VALUE ) {
  261. printf("Test Failure da\n");
  262. ExitProcess(0);
  263. }
  264. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\dxxa",&FindFileData);
  265. if ( hFind == INVALID_HANDLE_VALUE ) {
  266. printf("Test Failure dxxa\n");
  267. ExitProcess(0);
  268. }
  269. FindClose(hFind);
  270. }
  271. FinishBenchMark(&PerfInfo);
  272. }
  273. VOID
  274. APIENTRY
  275. CreateOpenDirObja(
  276. LPCWSTR lpFileName,
  277. POBJECT_ATTRIBUTES Obja,
  278. PUNICODE_STRING PathName,
  279. LPCWSTR DirName
  280. )
  281. {
  282. UNICODE_STRING FileName;
  283. IO_STATUS_BLOCK IoStatusBlock;
  284. BOOLEAN TranslationStatus;
  285. PVOID FreeBuffer;
  286. HANDLE hDir;
  287. NTSTATUS Status;
  288. if ( ARGUMENT_PRESENT(DirName) ) {
  289. TranslationStatus = RtlDosPathNameToNtPathName_U(
  290. DirName,
  291. PathName,
  292. &FileName.Buffer,
  293. NULL
  294. );
  295. if ( TranslationStatus ) {
  296. //
  297. // Open the directory for list access
  298. //
  299. InitializeObjectAttributes(
  300. Obja,
  301. PathName,
  302. OBJ_CASE_INSENSITIVE,
  303. NULL,
  304. NULL
  305. );
  306. Status = NtOpenFile(
  307. &hDir,
  308. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  309. Obja,
  310. &IoStatusBlock,
  311. FILE_SHARE_READ | FILE_SHARE_WRITE,
  312. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  313. );
  314. if ( !NT_SUCCESS(Status) ) {
  315. printf("Open faild %x\n",Status);
  316. ExitProcess(1);
  317. }
  318. }
  319. }
  320. else {
  321. hDir = NULL;
  322. }
  323. TranslationStatus = RtlDosPathNameToNtPathName_U(
  324. lpFileName,
  325. PathName,
  326. &FileName.Buffer,
  327. NULL
  328. );
  329. if ( !TranslationStatus ) {
  330. return;
  331. }
  332. if ( hDir ) {
  333. PathName->Buffer = PathName->Buffer + 15;
  334. PathName->Length -= 30;
  335. PathName->MaximumLength -= 30;
  336. }
  337. FreeBuffer = PathName->Buffer;
  338. InitializeObjectAttributes(
  339. Obja,
  340. PathName,
  341. OBJ_CASE_INSENSITIVE,
  342. hDir,
  343. NULL
  344. );
  345. }
  346. VOID
  347. APIENTRY
  348. OpenCloseDir(
  349. POBJECT_ATTRIBUTES Obja
  350. )
  351. {
  352. HANDLE hDir;
  353. NTSTATUS Status;
  354. IO_STATUS_BLOCK IoStatusBlock;
  355. //
  356. // Open the directory for list access
  357. //
  358. Status = NtOpenFile(
  359. &hDir,
  360. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  361. Obja,
  362. &IoStatusBlock,
  363. FILE_SHARE_READ | FILE_SHARE_WRITE,
  364. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  365. );
  366. if ( !NT_SUCCESS(Status) ) {
  367. printf("Open faild %x\n",Status);
  368. ExitProcess(1);
  369. }
  370. NtClose(hDir);
  371. }
  372. VOID
  373. OpenDirTest(
  374. VOID
  375. )
  376. {
  377. PERFINFO PerfInfo;
  378. int i;
  379. HANDLE hDir;
  380. OBJECT_ATTRIBUTES Obja;
  381. UNICODE_STRING PathName;
  382. FILE_BASIC_INFORMATION BasicInfo;
  383. #if 0
  384. CreateOpenDirObja(
  385. L"d:\\testdir\\client1\\bench",
  386. &Obja,
  387. &PathName,
  388. NULL
  389. );
  390. StartBenchMark(
  391. "Open Dir NTFS d:\\testdir\\client1\\bench",
  392. FIVEK,
  393. &PerfInfo
  394. );
  395. for ( i=0;i<FIVEK;i++) {
  396. OpenCloseDir(&Obja);
  397. }
  398. FinishBenchMark(&PerfInfo);
  399. StartBenchMark(
  400. "NtQueryAttributes Dir NTFS d:\\testdir\\client1\\bench",
  401. FIVEK,
  402. &PerfInfo
  403. );
  404. for ( i=0;i<FIVEK;i++) {
  405. NtQueryAttributesFile( &Obja, &BasicInfo );
  406. }
  407. FinishBenchMark(&PerfInfo);
  408. CreateOpenDirObja(
  409. L"c:\\testdir\\client1\\bench",
  410. &Obja,
  411. &PathName,
  412. NULL
  413. );
  414. StartBenchMark(
  415. "Open Dir FAT c:\\testdir\\client1\\bench",
  416. FIVEK,
  417. &PerfInfo
  418. );
  419. for ( i=0;i<FIVEK;i++) {
  420. OpenCloseDir(&Obja);
  421. }
  422. FinishBenchMark(&PerfInfo);
  423. #endif
  424. CreateOpenDirObja(
  425. L"d:\\testdir\\client1\\bench",
  426. &Obja,
  427. &PathName,
  428. L"d:\\"
  429. );
  430. StartBenchMark(
  431. "VOL Rel Open Dir NTFS d:\\testdir\\client1\\bench",
  432. FIVEK,
  433. &PerfInfo
  434. );
  435. for ( i=0;i<FIVEK;i++) {
  436. OpenCloseDir(&Obja);
  437. }
  438. FinishBenchMark(&PerfInfo);
  439. CreateOpenDirObja(
  440. L"c:\\testdir\\client1\\bench",
  441. &Obja,
  442. &PathName,
  443. L"c:\\"
  444. );
  445. StartBenchMark(
  446. "Vol Rel Open Dir FAT c:\\testdir\\client1\\bench",
  447. FIVEK,
  448. &PerfInfo
  449. );
  450. for ( i=0;i<FIVEK;i++) {
  451. OpenCloseDir(&Obja);
  452. }
  453. FinishBenchMark(&PerfInfo);
  454. }
  455. DWORD
  456. _cdecl
  457. main(
  458. int argc,
  459. char *argv[],
  460. char *envp[]
  461. )
  462. {
  463. DWORD CryptoKey;
  464. PDWORD p;
  465. printf("sixeof teb %x\n",sizeof(TEB));
  466. CryptoKey = USER_SHARED_DATA->CryptoExponent;
  467. printf("Key %x\n",CryptoKey);
  468. p = &(USER_SHARED_DATA->CryptoExponent);
  469. *p = 1;
  470. //OpenDirTest();
  471. //FastFindTest();
  472. //FindFirstTest();
  473. return 0;
  474. }