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.

295 lines
5.4 KiB

  1. //
  2. // Driver Verifier Control Applet
  3. // Copyright (c) Microsoft Corporation, 1999
  4. //
  5. //
  6. // module: image.cxx
  7. // author: silviuc
  8. // created: Thu Jan 07 20:05:09 1999
  9. //
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <tchar.h>
  17. #include <windows.h>
  18. #include "image.hxx"
  19. #include "verify.hxx"
  20. //
  21. // Function:
  22. //
  23. // ImgInitializeBrowseInfo
  24. //
  25. // Description:
  26. //
  27. // This functions fills oout the `Info' structure with
  28. // various pointers to PE data from the mapped image file.
  29. //
  30. // Note. Even if the function returned false the destructor
  31. // `ImgDeleteBrowseInfo' should be called because it does some
  32. // cleanup.
  33. //
  34. // Return:
  35. //
  36. // True if all the PE data pointers have been obtained.
  37. //
  38. BOOL
  39. ImgInitializeBrowseInfo (
  40. LPCTSTR FilePath,
  41. PIMAGE_BROWSE_INFO Info)
  42. {
  43. DWORD Index;
  44. if (Info == NULL) {
  45. return FALSE;
  46. }
  47. ZeroMemory (Info, sizeof *Info);
  48. Info->File = CreateFile (
  49. FilePath,
  50. GENERIC_READ,
  51. FILE_SHARE_READ | FILE_SHARE_WRITE,
  52. NULL,
  53. OPEN_EXISTING,
  54. 0,
  55. NULL);
  56. if (Info->File == INVALID_HANDLE_VALUE) {
  57. return FALSE;
  58. }
  59. Info->Section = CreateFileMapping (
  60. Info->File,
  61. NULL,
  62. PAGE_READONLY,
  63. 0,
  64. 0,
  65. NULL);
  66. if (Info->Section == NULL) {
  67. return FALSE;
  68. }
  69. Info->ImageBase = (LPBYTE) MapViewOfFile (
  70. Info->Section,
  71. FILE_MAP_READ,
  72. 0,
  73. 0,
  74. 0);
  75. if (Info->ImageBase == NULL) {
  76. return FALSE;
  77. }
  78. //
  79. // Check the signature
  80. //
  81. Info->DosHeader = (PIMAGE_DOS_HEADER)Info->ImageBase;
  82. if (Info->DosHeader->e_magic != 'ZM') {
  83. return FALSE;
  84. }
  85. Info->FileHeader = (PIMAGE_FILE_HEADER)
  86. (Info->ImageBase + Info->DosHeader->e_lfanew + sizeof(DWORD));
  87. Info->FileSignature = *((DWORD *)Info->FileHeader - 1);
  88. if (Info->FileSignature != IMAGE_NT_SIGNATURE) {
  89. return FALSE;
  90. }
  91. Info->OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(Info->FileHeader + 1);
  92. Info->ImportDirectory = & (Info->OptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]);
  93. Info->SectionHeader = (PIMAGE_SECTION_HEADER)(Info->OptionalHeader + 1);
  94. Info->ImportSection = NULL;
  95. //
  96. // Find the section containing the import table
  97. //
  98. for (Index = 0; Index < Info->FileHeader->NumberOfSections; Index++) {
  99. DWORD Start = (Info->SectionHeader + Index)->VirtualAddress;
  100. DWORD Size = (Info->SectionHeader + Index)->SizeOfRawData;
  101. DWORD Import = Info->ImportDirectory->VirtualAddress;
  102. if (Start <= Import && Start + Size > Import) {
  103. Info->ImportSection = &(Info->SectionHeader[Index]);
  104. break;
  105. }
  106. }
  107. if (Info->ImportSection == NULL) {
  108. return FALSE;
  109. }
  110. //
  111. // Find the address of import data in the section body.
  112. //
  113. Info->AddressCorrection = (DWORD_PTR)Info->ImageBase
  114. + Info->ImportSection->PointerToRawData
  115. - Info->ImportSection->VirtualAddress;
  116. Info->ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(Info->AddressCorrection
  117. + Info->ImportDirectory->VirtualAddress);
  118. //
  119. // Finish
  120. //
  121. return TRUE;
  122. }
  123. //
  124. // Function:
  125. //
  126. // ImgDeleteBrowseInfo
  127. //
  128. // Description:
  129. //
  130. // This function cleans up the `Info' structure, unmaps views,
  131. // closes handles, etc.
  132. //
  133. BOOL
  134. ImgDeleteBrowseInfo (
  135. PIMAGE_BROWSE_INFO Info)
  136. {
  137. if (Info == NULL)
  138. return FALSE;
  139. UnmapViewOfFile (Info->ImageBase);
  140. CloseHandle (Info->Section);
  141. CloseHandle (Info->File);
  142. ZeroMemory (Info, sizeof *Info);
  143. return TRUE;
  144. }
  145. //
  146. // Function:
  147. //
  148. // ImgSearchDriverName
  149. //
  150. // Description:
  151. //
  152. // This function checks if a driver is in `system32' or
  153. // `drivers\system32' directory. If it is then the full
  154. // path to the driver is written in `DriverPath'.
  155. //
  156. // Return:
  157. //
  158. // True if driver found in system 32 or system32\drivers.
  159. //
  160. BOOL
  161. ImgSearchDriverImage (
  162. LPCTSTR DriverName,
  163. LPTSTR DriverPath,
  164. UINT DriverPathBufferLength )
  165. {
  166. HANDLE File;
  167. UINT SysDirPathLength;
  168. if (DriverName == NULL)
  169. return FALSE;
  170. //
  171. // Search in `system32\drivers'
  172. //
  173. SysDirPathLength = GetSystemDirectory (DriverPath, DriverPathBufferLength );
  174. if( 0 == SysDirPathLength || SysDirPathLength > DriverPathBufferLength ) {
  175. //
  176. // Bad luck - we couldn't read the %windir%\system32 value
  177. //
  178. return FALSE;
  179. }
  180. _tcscat (DriverPath, TEXT("\\drivers\\"));
  181. _tcscat (DriverPath, DriverName);
  182. File = CreateFile (
  183. DriverPath,
  184. GENERIC_READ,
  185. FILE_SHARE_READ | FILE_SHARE_WRITE,
  186. NULL,
  187. OPEN_EXISTING,
  188. 0,
  189. NULL);
  190. if (File != INVALID_HANDLE_VALUE) {
  191. CloseHandle (File);
  192. return TRUE;
  193. }
  194. //
  195. // Search in `system32'
  196. //
  197. GetSystemDirectory (DriverPath, MAX_PATH);
  198. _tcscat (DriverPath, TEXT("\\"));
  199. _tcscat (DriverPath, DriverName);
  200. File = CreateFile (
  201. DriverPath,
  202. GENERIC_READ,
  203. FILE_SHARE_READ | FILE_SHARE_WRITE,
  204. NULL,
  205. OPEN_EXISTING,
  206. 0,
  207. NULL);
  208. if (File != INVALID_HANDLE_VALUE) {
  209. CloseHandle (File);
  210. return TRUE;
  211. }
  212. //
  213. // Nothing found.
  214. //
  215. return FALSE;
  216. }
  217. //
  218. // end of module: image.cxx
  219. //