Source code of Windows XP (NT5)
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.

868 lines
21 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <errno.h>
  6. #include <malloc.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "pperf.h"
  10. #include "..\pstat.h"
  11. extern HANDLE DriverHandle;
  12. extern UCHAR Buffer[];
  13. #define BufferSize 60000
  14. typedef struct NameList {
  15. struct NameList *Next;
  16. ULONG Parm;
  17. struct NameList *ChildList;
  18. PUCHAR Name;
  19. } NAME_LIST, *PNAME_LIST;
  20. PNAME_LIST DriverList;
  21. PNAME_LIST ActiveThunks;
  22. PNAME_LIST SourceModule, ImportModule;
  23. #define COMBOCMD(a,b) ((a << 16) | b)
  24. NTSTATUS
  25. openfile (
  26. IN PHANDLE filehandle,
  27. IN PUCHAR BasePath,
  28. IN PUCHAR Name
  29. );
  30. VOID
  31. readfile (
  32. HANDLE handle,
  33. ULONG offset,
  34. ULONG len,
  35. PVOID buffer
  36. );
  37. ULONG
  38. ConvertImportAddress (
  39. IN ULONG ImageRelativeAddress,
  40. IN ULONG PoolAddress,
  41. IN PIMAGE_SECTION_HEADER SectionHeader
  42. );
  43. VOID ThunkCreateDriverList (VOID);
  44. #define IMPADDRESS(a) ConvertImportAddress((ULONG)a, (ULONG)Buffer, &SectionHeader)
  45. ULONG HookThunk (PNAME_LIST, PNAME_LIST, PNAME_LIST);
  46. VOID SnapPrivateInfo (PDISPLAY_ITEM);
  47. VOID NameList2ComboBox (HWND hDlg, ULONG id, PNAME_LIST List);
  48. PNAME_LIST AddNameEntry (PNAME_LIST *head, PUCHAR name, ULONG Parm);
  49. VOID FreeNameList (PNAME_LIST List);
  50. PNAME_LIST GetComboSelection (HWND h, ULONG id);
  51. VOID NameList2ListBox (HWND hDlg, ULONG id, PNAME_LIST List);
  52. VOID loadimagedir (PUCHAR, ULONG, PIMAGE_SECTION_HEADER);
  53. VOID RemoveHook (HWND hDlg);
  54. VOID ClearAllHooks (HWND hDlg);
  55. VOID AddThunk (HWND hDlg);
  56. VOID loadexports (PNAME_LIST Driver, PNAME_LIST Item);
  57. //#define IDM_THUNK_LIST 301
  58. //#define IDM_THUNK_SOURCE 302
  59. //#define IDM_THUNK_IMPORT 303
  60. //#define IDM_THUNK_FUNCTION 304
  61. //#define IDM_THUNK_ADD 305
  62. //#define IDM_THUNK_REMOVE 306
  63. BOOL
  64. APIENTRY ThunkDlgProc(
  65. HWND hDlg,
  66. unsigned message,
  67. DWORD wParam,
  68. LONG lParam
  69. )
  70. {
  71. PNAME_LIST Item;
  72. switch (message) {
  73. case WM_INITDIALOG:
  74. SourceModule = NULL;
  75. ImportModule = NULL;
  76. ThunkCreateDriverList ();
  77. NameList2ComboBox (hDlg, IDM_THUNK_SOURCE, DriverList);
  78. NameList2ListBox (hDlg, IDM_THUNK_LIST, ActiveThunks);
  79. return (TRUE);
  80. case WM_COMMAND:
  81. switch(wParam) {
  82. //
  83. // end function
  84. //
  85. case COMBOCMD (CBN_SELCHANGE, IDM_THUNK_SOURCE):
  86. case COMBOCMD (CBN_SELCHANGE, IDM_THUNK_IMPORT):
  87. Item = GetComboSelection (hDlg, IDM_THUNK_SOURCE);
  88. if (Item && Item != SourceModule) {
  89. SourceModule = Item;
  90. NameList2ComboBox (hDlg, IDM_THUNK_IMPORT, Item->ChildList);
  91. }
  92. Item = GetComboSelection (hDlg, IDM_THUNK_IMPORT);
  93. if (Item && Item != ImportModule) {
  94. ImportModule = Item;
  95. NameList2ComboBox (hDlg, IDM_THUNK_FUNCTION, Item->ChildList);
  96. }
  97. break;
  98. case IDM_THUNK_REMOVE:
  99. RemoveHook (hDlg);
  100. break;
  101. case IDM_THUNK_CLEAR_ALL:
  102. ClearAllHooks (hDlg);
  103. break;
  104. case IDM_THUNK_ADD:
  105. AddThunk (hDlg);
  106. break;
  107. case IDOK:
  108. case IDCANCEL:
  109. //DlgThunkData (hDlg);
  110. FreeNameList (DriverList);
  111. DriverList = NULL;
  112. EndDialog(hDlg, DIALOG_SUCCESS);
  113. return (TRUE);
  114. }
  115. }
  116. return (FALSE);
  117. }
  118. VOID AddThunk (HWND hDlg)
  119. {
  120. PDISPLAY_ITEM pPerf;
  121. PNAME_LIST Item;
  122. ULONG id, i;
  123. PUCHAR p;
  124. HWND thunklist;
  125. id = 0;
  126. Item = GetComboSelection (hDlg, IDM_THUNK_FUNCTION);
  127. if (Item && SourceModule && ImportModule) {
  128. id = HookThunk (SourceModule, ImportModule, Item);
  129. }
  130. if (!id) {
  131. MessageBox(hDlg,"Thunk was not hooked","Hook error",MB_OK);
  132. return;
  133. }
  134. pPerf = AllocateDisplayItem();
  135. //
  136. // build name (the hard way?)
  137. //
  138. strcpy (pPerf->PerfName, Item->Name);
  139. strcat (pPerf->PerfName, "(");
  140. strcat (pPerf->PerfName, SourceModule->Name);
  141. for (p=pPerf->PerfName; *p; p++) {
  142. if (*p == '.')
  143. *p = 0;
  144. }
  145. strcat (pPerf->PerfName, ">");
  146. strcat (pPerf->PerfName, ImportModule->Name);
  147. for (p=pPerf->PerfName; *p; p++) {
  148. if (*p == '.')
  149. *p = 0;
  150. }
  151. strcat (pPerf->PerfName, ")");
  152. //
  153. // Add to thunk list
  154. //
  155. Item = malloc (sizeof (NAME_LIST));
  156. if (Item == NULL) {
  157. printf("Memory allocation failed.\n");
  158. exit(1);
  159. }
  160. Item->Name = _strdup (pPerf->PerfName);
  161. Item->Parm = (ULONG) pPerf;
  162. Item->ChildList = NULL;
  163. Item->Next = ActiveThunks;
  164. ActiveThunks = Item;
  165. pPerf->SnapParam2 = id;
  166. // bugbug
  167. NameList2ListBox (hDlg, IDM_THUNK_LIST, ActiveThunks);
  168. //
  169. // Add graph to windows
  170. //
  171. pPerf->SnapData = SnapPrivateInfo; // generic snap
  172. pPerf->SnapParam1 = OFFSET(PSTATS, ThunkCounters[id-1]);
  173. SetDisplayToTrue (pPerf, 99);
  174. RefitWindows(NULL, NULL);
  175. UpdateInternalStats ();
  176. pPerf->SnapData (pPerf);
  177. UpdateInternalStats ();
  178. pPerf->SnapData (pPerf);
  179. }
  180. VOID ClearAllHooks (HWND hDlg)
  181. {
  182. PDISPLAY_ITEM pPerf;
  183. IO_STATUS_BLOCK IOSB;
  184. ULONG id;
  185. PNAME_LIST Item;
  186. while (ActiveThunks) {
  187. pPerf = (PDISPLAY_ITEM) ActiveThunks->Parm;
  188. Item = ActiveThunks;
  189. ActiveThunks = ActiveThunks->Next;
  190. free (Item->Name);
  191. free (Item);
  192. id = pPerf->SnapParam2;
  193. SetDisplayToFalse (pPerf); // remove window
  194. FreeDisplayItem (pPerf);
  195. // notify driver
  196. NtDeviceIoControlFile(
  197. DriverHandle,
  198. (HANDLE) NULL, // event
  199. (PIO_APC_ROUTINE) NULL,
  200. (PVOID) NULL,
  201. &IOSB,
  202. PSTAT_REMOVE_HOOK,
  203. &id, // input buffer
  204. sizeof (ULONG),
  205. NULL, // output buffer
  206. 0
  207. );
  208. }
  209. NameList2ListBox (hDlg, IDM_THUNK_LIST, ActiveThunks);
  210. RefitWindows (NULL, NULL);
  211. }
  212. VOID RemoveHook (HWND hDlg)
  213. {
  214. ULONG i, id;
  215. HWND ListBox;
  216. PNAME_LIST Item, *pp;
  217. PDISPLAY_ITEM pPerf;
  218. IO_STATUS_BLOCK IOSB;
  219. ListBox = GetDlgItem(hDlg, IDM_THUNK_LIST);
  220. i = SendMessage(ListBox, LB_GETCURSEL, 0, 0);
  221. if (i == -1) {
  222. return;
  223. }
  224. pPerf = (PDISPLAY_ITEM) SendMessage(ListBox, LB_GETITEMDATA, i, 0);
  225. Item = NULL;
  226. for (pp = &ActiveThunks; *pp; pp = &(*pp)->Next) {
  227. if ((*pp)->Parm == (ULONG)pPerf) {
  228. Item = *pp;
  229. *pp = (*pp)->Next; // remove from list
  230. break ;
  231. }
  232. }
  233. if (!Item) {
  234. return ;
  235. }
  236. free (Item->Name);
  237. free (Item);
  238. id = pPerf->SnapParam2;
  239. SetDisplayToFalse (pPerf); // remove window
  240. FreeDisplayItem (pPerf);
  241. // notify driver
  242. NtDeviceIoControlFile(
  243. DriverHandle,
  244. (HANDLE) NULL, // event
  245. (PIO_APC_ROUTINE) NULL,
  246. (PVOID) NULL,
  247. &IOSB,
  248. PSTAT_REMOVE_HOOK,
  249. &id, // input buffer
  250. sizeof (ULONG),
  251. NULL, // output buffer
  252. 0
  253. );
  254. NameList2ListBox (hDlg, IDM_THUNK_LIST, ActiveThunks);
  255. RefitWindows (NULL, NULL);
  256. }
  257. VOID
  258. NameList2ListBox (HWND hDlg, ULONG id, PNAME_LIST List)
  259. {
  260. HWND ListBox;
  261. ULONG nIndex;
  262. ListBox = GetDlgItem(hDlg, id);
  263. SendMessage(ListBox, LB_RESETCONTENT, 0, 0);
  264. SendMessage(ListBox, LB_SETITEMDATA, 0L, 0L);
  265. while (List) {
  266. nIndex = SendMessage(ListBox, LB_ADDSTRING, 0, (LPARAM)List->Name);
  267. SendMessage(ListBox, LB_SETITEMDATA, nIndex, List->Parm);
  268. List = List->Next;
  269. }
  270. }
  271. VOID
  272. NameList2ComboBox (HWND hDlg, ULONG id, PNAME_LIST List)
  273. {
  274. HWND ComboList;
  275. ULONG nIndex;
  276. ComboList = GetDlgItem(hDlg, id);
  277. SendMessage(ComboList, CB_RESETCONTENT, 0, 0);
  278. SendMessage(ComboList, CB_SETITEMDATA, 0L, 0L);
  279. while (List) {
  280. nIndex = SendMessage(ComboList, CB_ADDSTRING, 0, (LPARAM)List->Name);
  281. SendMessage(ComboList, CB_SETITEMDATA, nIndex, (ULONG) List);
  282. List = List->Next;
  283. }
  284. SendMessage(ComboList, CB_SETCURSEL, 0, 0L);
  285. }
  286. PNAME_LIST
  287. GetComboSelection (HWND hDlg, ULONG id)
  288. {
  289. ULONG i;
  290. HWND ComboList;
  291. ComboList = GetDlgItem(hDlg, id);
  292. i = SendMessage(ComboList, CB_GETCURSEL, 0, 0);
  293. if (i == -1) {
  294. return NULL;
  295. }
  296. return (PNAME_LIST) SendMessage(ComboList, CB_GETITEMDATA, i, 0);
  297. }
  298. VOID
  299. FreeNameList (PNAME_LIST List)
  300. {
  301. PNAME_LIST p1;
  302. while (List) {
  303. if (List->ChildList)
  304. FreeNameList (List->ChildList);
  305. p1 = List->Next;
  306. free (List->Name);
  307. free (List);
  308. List = p1;
  309. }
  310. }
  311. ULONG
  312. HookThunk (PNAME_LIST HookSource, PNAME_LIST TargetModule, PNAME_LIST Function)
  313. {
  314. PNAME_LIST SourceModule;
  315. IO_STATUS_BLOCK IOSB;
  316. HOOKTHUNK HookData;
  317. ULONG TracerId;
  318. NTSTATUS status;
  319. TracerId = 0;
  320. for (SourceModule=DriverList; SourceModule; SourceModule = SourceModule->Next) {
  321. if (SourceModule->Parm == -1) {
  322. continue;
  323. }
  324. if (SourceModule->Parm != HookSource->Parm &&
  325. HookSource->Parm != -1) {
  326. continue;
  327. }
  328. HookData.SourceModule = SourceModule->Name;
  329. HookData.ImageBase = SourceModule->Parm;
  330. HookData.TargetModule = TargetModule->Name;
  331. HookData.Function = Function->Name;
  332. HookData.TracerId = TracerId;
  333. //
  334. // Ask driver to hook this thunk
  335. //
  336. status = NtDeviceIoControlFile(
  337. DriverHandle,
  338. (HANDLE) NULL, // event
  339. (PIO_APC_ROUTINE) NULL,
  340. (PVOID) NULL,
  341. &IOSB,
  342. PSTAT_HOOK_THUNK,
  343. &HookData, // input buffer
  344. sizeof (HookData),
  345. NULL, // output buffer
  346. 0
  347. );
  348. if (NT_SUCCESS(status)) {
  349. TracerId = HookData.TracerId;
  350. }
  351. }
  352. return TracerId;
  353. }
  354. VOID
  355. ThunkCreateDriverList ()
  356. {
  357. ULONG i;
  358. PRTL_PROCESS_MODULES Modules;
  359. PRTL_PROCESS_MODULE_INFORMATION Module;
  360. NTSTATUS status;
  361. PNAME_LIST Driver, Import, Item, AbortState;
  362. PIMAGE_IMPORT_DESCRIPTOR ImpDescriptor;
  363. IMAGE_SECTION_HEADER SectionHeader;
  364. ULONG ThunkAddr, ThunkData;
  365. //
  366. // Query driver list
  367. //
  368. status = NtQuerySystemInformation (
  369. SystemModuleInformation,
  370. Buffer,
  371. BufferSize,
  372. &i);
  373. if (!NT_SUCCESS(status)) {
  374. return;
  375. }
  376. //
  377. // Add drivers
  378. //
  379. Modules = (PRTL_PROCESS_MODULES) Buffer;
  380. Module = &Modules->Modules[ 0 ];
  381. for (i = 0; i < Modules->NumberOfModules; i++) {
  382. Driver = AddNameEntry (
  383. &DriverList,
  384. Module->FullPathName + Module->OffsetToFileName,
  385. (ULONG) Module->ImageBase
  386. );
  387. Module++;
  388. }
  389. //
  390. // Add imports for each driver
  391. //
  392. for (Driver = DriverList; Driver; Driver = Driver->Next) {
  393. try {
  394. //
  395. // Read in source image's headers
  396. //
  397. AbortState = Driver;
  398. loadimagedir (Driver->Name, IMAGE_DIRECTORY_ENTRY_IMPORT, &SectionHeader);
  399. //
  400. // Go through each import module
  401. //
  402. ImpDescriptor = (PIMAGE_IMPORT_DESCRIPTOR) Buffer;
  403. while (ImpDescriptor->Characteristics) {
  404. AbortState = Driver;
  405. //
  406. // Add this import to driver's list
  407. //
  408. Import = AddNameEntry (
  409. &Driver->ChildList,
  410. (PUCHAR) IMPADDRESS(ImpDescriptor->Name),
  411. 1
  412. );
  413. AbortState = Import;
  414. //
  415. // Go through each function for the import module
  416. //
  417. ThunkAddr = IMPADDRESS (ImpDescriptor->OriginalFirstThunk);
  418. for (; ;) {
  419. ThunkData = (ULONG)((PIMAGE_THUNK_DATA) ThunkAddr)->u1.AddressOfData;
  420. if (ThunkData == 0) {
  421. // end of table
  422. break;
  423. }
  424. //
  425. // Add this function to import list
  426. //
  427. AddNameEntry (
  428. &Import->ChildList,
  429. ((PIMAGE_IMPORT_BY_NAME) IMPADDRESS(ThunkData))->Name,
  430. 0
  431. );
  432. // next thunk
  433. ThunkAddr += sizeof (IMAGE_THUNK_DATA);
  434. }
  435. // next import table
  436. ImpDescriptor++;
  437. }
  438. } except(EXCEPTION_EXECUTE_HANDLER) {
  439. AddNameEntry(&AbortState->ChildList, "* ERROR *", 1);
  440. }
  441. // next driver
  442. }
  443. //
  444. // Add "Any driver" selection
  445. //
  446. Driver = AddNameEntry(&DriverList, "*Any", (ULONG)-1);
  447. //
  448. // For child module list use complete driver list, which is
  449. // now on the next pointer of Driver.
  450. //
  451. for (Item = Driver->Next; Item; Item = Item->Next) {
  452. // bogus compiler - need to make a subfunction here to keep
  453. // the compiler happy
  454. loadexports (Driver, Item);
  455. }
  456. }
  457. VOID loadexports (PNAME_LIST Driver, PNAME_LIST Item)
  458. {
  459. IMAGE_SECTION_HEADER SectionHeader;
  460. PIMAGE_EXPORT_DIRECTORY ExpDirectory;
  461. PULONG ExpNameAddr;
  462. PNAME_LIST Import;
  463. ULONG i;
  464. try {
  465. loadimagedir (
  466. Item->Name,
  467. IMAGE_DIRECTORY_ENTRY_EXPORT,
  468. &SectionHeader
  469. );
  470. } except(EXCEPTION_EXECUTE_HANDLER) {
  471. return ;
  472. }
  473. Import = AddNameEntry (&Driver->ChildList, Item->Name, Item->Parm);
  474. try {
  475. ExpDirectory = (PIMAGE_EXPORT_DIRECTORY) Buffer;
  476. ExpNameAddr = (PULONG)IMPADDRESS (ExpDirectory->AddressOfNames);
  477. for (i=0; i < ExpDirectory->NumberOfNames; i++) {
  478. AddNameEntry (
  479. &Import->ChildList,
  480. (PUCHAR) IMPADDRESS(*ExpNameAddr),
  481. 0
  482. );
  483. ExpNameAddr++;
  484. }
  485. } except(EXCEPTION_EXECUTE_HANDLER) {
  486. AddNameEntry(&Import->ChildList, "* ERROR *", 1);
  487. }
  488. }
  489. VOID
  490. loadimagedir (
  491. IN PUCHAR filename,
  492. IN ULONG dirno,
  493. OUT PIMAGE_SECTION_HEADER SectionHeader
  494. )
  495. {
  496. HANDLE filehandle;
  497. ULONG i, j, Dir;
  498. NTSTATUS status;
  499. IMAGE_DOS_HEADER DosImageHeader;
  500. IMAGE_NT_HEADERS NtImageHeader;
  501. PIMAGE_SECTION_HEADER pSectionHeader;
  502. status = openfile (&filehandle, "\\SystemRoot\\", filename);
  503. if (!NT_SUCCESS(status)) {
  504. status = openfile (&filehandle, "\\SystemRoot\\System32\\", filename);
  505. }
  506. if (!NT_SUCCESS(status)) {
  507. status = openfile (&filehandle, "\\SystemRoot\\System32\\Drivers\\", filename);
  508. }
  509. if (!NT_SUCCESS(status)) {
  510. RtlRaiseStatus (1);
  511. }
  512. try {
  513. readfile (
  514. filehandle,
  515. 0,
  516. sizeof (DosImageHeader),
  517. (PVOID) &DosImageHeader
  518. );
  519. if (DosImageHeader.e_magic != IMAGE_DOS_SIGNATURE) {
  520. RtlRaiseStatus (1);
  521. }
  522. readfile (
  523. filehandle,
  524. DosImageHeader.e_lfanew,
  525. sizeof (NtImageHeader),
  526. (PVOID) &NtImageHeader
  527. );
  528. if (NtImageHeader.Signature != IMAGE_NT_SIGNATURE) {
  529. RtlRaiseStatus (1);
  530. }
  531. //
  532. // read in complete sections headers from image
  533. //
  534. i = NtImageHeader.FileHeader.NumberOfSections
  535. * sizeof (IMAGE_SECTION_HEADER);
  536. j = ((ULONG) IMAGE_FIRST_SECTION (&NtImageHeader)) -
  537. ((ULONG) &NtImageHeader) +
  538. DosImageHeader.e_lfanew;
  539. if (i > BufferSize) {
  540. RtlRaiseStatus (1);
  541. }
  542. readfile (
  543. filehandle,
  544. j, // file offset
  545. i, // length
  546. Buffer
  547. );
  548. //
  549. // Find section with import directory
  550. //
  551. Dir = NtImageHeader.OptionalHeader.DataDirectory[dirno].VirtualAddress;
  552. i = 0;
  553. pSectionHeader = (PIMAGE_SECTION_HEADER)Buffer;
  554. for (; ;) {
  555. if (i >= NtImageHeader.FileHeader.NumberOfSections) {
  556. RtlRaiseStatus (1);
  557. }
  558. if (pSectionHeader->VirtualAddress <= Dir &&
  559. pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData > Dir) {
  560. break;
  561. }
  562. i += 1;
  563. pSectionHeader += 1;
  564. }
  565. Dir -= pSectionHeader->VirtualAddress;
  566. pSectionHeader->VirtualAddress += Dir;
  567. pSectionHeader->PointerToRawData += Dir;
  568. pSectionHeader->SizeOfRawData -= Dir;
  569. *SectionHeader = *pSectionHeader;
  570. //
  571. // read in complete export section from image
  572. //
  573. if (SectionHeader->SizeOfRawData > BufferSize) {
  574. RtlRaiseStatus (1);
  575. }
  576. readfile (
  577. filehandle,
  578. SectionHeader->PointerToRawData,
  579. SectionHeader->SizeOfRawData,
  580. Buffer
  581. );
  582. } finally {
  583. //
  584. // Clean up
  585. //
  586. NtClose (filehandle);
  587. }
  588. }
  589. PNAME_LIST
  590. AddNameEntry (PNAME_LIST *head, PUCHAR name, ULONG Parm)
  591. {
  592. PNAME_LIST Entry;
  593. Entry = malloc (sizeof (NAME_LIST));
  594. if (Entry == NULL) {
  595. printf("Memory allocation failed.\n");
  596. exit(1);
  597. }
  598. Entry->Name = _strdup (name);
  599. if (Entry->Name == NULL) {
  600. printf("Memory allocation failed.\n");
  601. exit(1);
  602. }
  603. Entry->Parm = Parm;
  604. Entry->ChildList = NULL;
  605. if (Parm) {
  606. _strlwr (Entry->Name);
  607. }
  608. Entry->Next = *head;
  609. *head = Entry;
  610. return Entry;
  611. }
  612. NTSTATUS
  613. openfile (
  614. IN PHANDLE filehandle,
  615. IN PUCHAR BasePath,
  616. IN PUCHAR Name
  617. )
  618. {
  619. ANSI_STRING AscBasePath, AscName;
  620. UNICODE_STRING UniPathName, UniName;
  621. NTSTATUS status;
  622. OBJECT_ATTRIBUTES ObjA;
  623. IO_STATUS_BLOCK IOSB;
  624. UCHAR StringBuf[500];
  625. //
  626. // Build name
  627. //
  628. UniPathName.Buffer = (PWCHAR)StringBuf;
  629. UniPathName.Length = 0;
  630. UniPathName.MaximumLength = sizeof( StringBuf );
  631. RtlInitString(&AscBasePath, BasePath);
  632. status = RtlAnsiStringToUnicodeString( &UniPathName, &AscBasePath, FALSE );
  633. if (!NT_SUCCESS(status)) {
  634. return status;
  635. }
  636. RtlInitString(&AscName, Name);
  637. status = RtlAnsiStringToUnicodeString( &UniName, &AscName, TRUE );
  638. if (!NT_SUCCESS(status)) {
  639. return status;
  640. }
  641. status = RtlAppendUnicodeStringToString (&UniPathName, &UniName);
  642. if (!NT_SUCCESS(status)) {
  643. RtlFreeUnicodeString (&UniName);
  644. return status;
  645. }
  646. InitializeObjectAttributes(
  647. &ObjA,
  648. &UniPathName,
  649. OBJ_CASE_INSENSITIVE,
  650. 0,
  651. 0 );
  652. //
  653. // open file
  654. //
  655. status = NtOpenFile (
  656. filehandle, // return handle
  657. SYNCHRONIZE | FILE_READ_DATA, // desired access
  658. &ObjA, // Object
  659. &IOSB, // io status block
  660. FILE_SHARE_READ | FILE_SHARE_WRITE, // share access
  661. FILE_SYNCHRONOUS_IO_ALERT // open options
  662. );
  663. RtlFreeUnicodeString (&UniName);
  664. return status;
  665. }
  666. VOID
  667. readfile (
  668. HANDLE handle,
  669. ULONG offset,
  670. ULONG len,
  671. PVOID buffer
  672. )
  673. {
  674. NTSTATUS status;
  675. IO_STATUS_BLOCK iosb;
  676. LARGE_INTEGER foffset;
  677. foffset = RtlConvertUlongToLargeInteger(offset);
  678. status = NtReadFile (
  679. handle,
  680. NULL, // event
  681. NULL, // apc routine
  682. NULL, // apc context
  683. &iosb,
  684. buffer,
  685. len,
  686. &foffset,
  687. NULL
  688. );
  689. if (!NT_SUCCESS(status)) {
  690. RtlRaiseStatus (1);
  691. }
  692. }
  693. ULONG
  694. ConvertImportAddress (
  695. IN ULONG ImageRelativeAddress,
  696. IN ULONG PoolAddress,
  697. IN PIMAGE_SECTION_HEADER SectionHeader
  698. )
  699. {
  700. ULONG EffectiveAddress;
  701. EffectiveAddress = PoolAddress + ImageRelativeAddress -
  702. SectionHeader->VirtualAddress;
  703. if (EffectiveAddress < PoolAddress ||
  704. EffectiveAddress > PoolAddress + SectionHeader->SizeOfRawData) {
  705. RtlRaiseStatus (1);
  706. }
  707. return EffectiveAddress;
  708. }