Windows NT 4.0 source code leak
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.

352 lines
7.9 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. imagedir.c
  5. Abstract:
  6. The module contains the code to translate an image directory type to
  7. the address of the data for that entry.
  8. Author:
  9. Steve Wood (stevewo) 18-Aug-1989
  10. Environment:
  11. User Mode or Kernel Mode
  12. Revision History:
  13. --*/
  14. #include "ntrtlp.h"
  15. PIMAGE_NT_HEADERS
  16. RtlImageNtHeader (
  17. IN PVOID Base
  18. )
  19. /*++
  20. Routine Description:
  21. This function returns the address of the NT Header.
  22. Arguments:
  23. Base - Supplies the base of the image.
  24. Return Value:
  25. Returns the address of the NT Header.
  26. --*/
  27. {
  28. PIMAGE_NT_HEADERS NtHeaders;
  29. if (Base != NULL && Base != (PVOID)-1) {
  30. try {
  31. if (((PIMAGE_DOS_HEADER)Base)->e_magic == IMAGE_DOS_SIGNATURE) {
  32. NtHeaders = (PIMAGE_NT_HEADERS)((PCHAR)Base + ((PIMAGE_DOS_HEADER)Base)->e_lfanew);
  33. if (NtHeaders->Signature == IMAGE_NT_SIGNATURE) {
  34. return NtHeaders;
  35. }
  36. }
  37. }
  38. except(EXCEPTION_EXECUTE_HANDLER) {
  39. return NULL;
  40. }
  41. }
  42. return NULL;
  43. }
  44. PIMAGE_SECTION_HEADER
  45. RtlSectionTableFromVirtualAddress (
  46. IN PIMAGE_NT_HEADERS NtHeaders,
  47. IN PVOID Base,
  48. IN PVOID Address
  49. )
  50. /*++
  51. Routine Description:
  52. This function locates a VirtualAddress within the image header
  53. of a file that is mapped as a file and returns a pointer to the
  54. section table entry for that virtual address
  55. Arguments:
  56. NtHeaders - Supplies the pointer to the image or data file.
  57. Base - Supplies the base of the image or data file.
  58. Address - Supplies the virtual address to locate.
  59. Return Value:
  60. NULL - The file does not contain data for the specified directory entry.
  61. NON-NULL - Returns the pointer of the section entry containing the data.
  62. --*/
  63. {
  64. ULONG i;
  65. PIMAGE_SECTION_HEADER NtSection;
  66. NtSection = IMAGE_FIRST_SECTION( NtHeaders );
  67. for (i=0; i<NtHeaders->FileHeader.NumberOfSections; i++) {
  68. if ((ULONG)Address >= NtSection->VirtualAddress &&
  69. (ULONG)Address < NtSection->VirtualAddress + NtSection->SizeOfRawData
  70. ) {
  71. return NtSection;
  72. }
  73. ++NtSection;
  74. }
  75. return NULL;
  76. }
  77. PVOID
  78. RtlAddressInSectionTable (
  79. IN PIMAGE_NT_HEADERS NtHeaders,
  80. IN PVOID Base,
  81. IN PVOID Address
  82. )
  83. /*++
  84. Routine Description:
  85. This function locates a VirtualAddress within the image header
  86. of a file that is mapped as a file and returns the seek address
  87. of the data the Directory describes.
  88. Arguments:
  89. NtHeaders - Supplies the pointer to the image or data file.
  90. Base - Supplies the base of the image or data file.
  91. Address - Supplies the virtual address to locate.
  92. Return Value:
  93. NULL - The file does not contain data for the specified directory entry.
  94. NON-NULL - Returns the address of the raw data the directory describes.
  95. --*/
  96. {
  97. PIMAGE_SECTION_HEADER NtSection;
  98. NtSection = RtlSectionTableFromVirtualAddress( NtHeaders,
  99. Base,
  100. Address
  101. );
  102. if (NtSection != NULL) {
  103. return( (PVOID)((ULONG)Base + ((ULONG)Address - NtSection->VirtualAddress) + NtSection->PointerToRawData) );
  104. }
  105. else {
  106. return( NULL );
  107. }
  108. }
  109. PVOID
  110. RtlImageDirectoryEntryToData (
  111. IN PVOID Base,
  112. IN BOOLEAN MappedAsImage,
  113. IN USHORT DirectoryEntry,
  114. OUT PULONG Size
  115. )
  116. /*++
  117. Routine Description:
  118. This function locates a Directory Entry within the image header
  119. and returns either the virtual address or seek address of the
  120. data the Directory describes.
  121. Arguments:
  122. Base - Supplies the base of the image or data file.
  123. MappedAsImage - FALSE if the file is mapped as a data file.
  124. - TRUE if the file is mapped as an image.
  125. DirectoryEntry - Supplies the directory entry to locate.
  126. Size - Return the size of the directory.
  127. Return Value:
  128. NULL - The file does not contain data for the specified directory entry.
  129. NON-NULL - Returns the address of the raw data the directory describes.
  130. --*/
  131. {
  132. ULONG i, DirectoryAddress;
  133. PIMAGE_NT_HEADERS NtHeaders;
  134. if ((ULONG)Base & 0x00000001) {
  135. Base = (PVOID)((ULONG)Base & ~0x00000001);
  136. MappedAsImage = FALSE;
  137. }
  138. NtHeaders = RtlImageNtHeader(Base);
  139. if ((!NtHeaders) ||
  140. (DirectoryEntry >= NtHeaders->OptionalHeader.NumberOfRvaAndSizes)) {
  141. return( NULL );
  142. }
  143. if (!(DirectoryAddress = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].VirtualAddress)) {
  144. return( NULL );
  145. }
  146. *Size = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].Size;
  147. if (MappedAsImage || DirectoryAddress < NtHeaders->OptionalHeader.SizeOfHeaders) {
  148. return( (PVOID)((ULONG)Base + DirectoryAddress) );
  149. }
  150. return( RtlAddressInSectionTable(NtHeaders, Base, (PVOID)DirectoryAddress ));
  151. }
  152. #if !defined(NTOS_KERNEL_RUNTIME) && !defined(BLDR_KERNEL_RUNTIME)
  153. PIMAGE_SECTION_HEADER
  154. RtlImageRvaToSection(
  155. IN PIMAGE_NT_HEADERS NtHeaders,
  156. IN PVOID Base,
  157. IN ULONG Rva
  158. )
  159. /*++
  160. Routine Description:
  161. This function locates an RVA within the image header of a file
  162. that is mapped as a file and returns a pointer to the section
  163. table entry for that virtual address
  164. Arguments:
  165. NtHeaders - Supplies the pointer to the image or data file.
  166. Base - Supplies the base of the image or data file. The image
  167. was mapped as a data file.
  168. Rva - Supplies the relative virtual address (RVA) to locate.
  169. Return Value:
  170. NULL - The RVA was not found within any of the sections of the image.
  171. NON-NULL - Returns the pointer to the image section that contains
  172. the RVA
  173. --*/
  174. {
  175. ULONG i;
  176. PIMAGE_SECTION_HEADER NtSection;
  177. NtSection = IMAGE_FIRST_SECTION( NtHeaders );
  178. for (i=0; i<NtHeaders->FileHeader.NumberOfSections; i++) {
  179. if (Rva >= NtSection->VirtualAddress &&
  180. Rva < NtSection->VirtualAddress + NtSection->SizeOfRawData
  181. ) {
  182. return NtSection;
  183. }
  184. ++NtSection;
  185. }
  186. return NULL;
  187. }
  188. PVOID
  189. RtlImageRvaToVa(
  190. IN PIMAGE_NT_HEADERS NtHeaders,
  191. IN PVOID Base,
  192. IN ULONG Rva,
  193. IN OUT PIMAGE_SECTION_HEADER *LastRvaSection OPTIONAL
  194. )
  195. /*++
  196. Routine Description:
  197. This function locates an RVA within the image header of a file that
  198. is mapped as a file and returns the virtual addrees of the
  199. corresponding byte in the file.
  200. Arguments:
  201. NtHeaders - Supplies the pointer to the image or data file.
  202. Base - Supplies the base of the image or data file. The image
  203. was mapped as a data file.
  204. Rva - Supplies the relative virtual address (RVA) to locate.
  205. LastRvaSection - Optional parameter that if specified, points
  206. to a variable that contains the last section value used for
  207. the specified image to translate and RVA to a VA.
  208. Return Value:
  209. NULL - The file does not contain the specified RVA
  210. NON-NULL - Returns the virtual addrees in the mapped file.
  211. --*/
  212. {
  213. PIMAGE_SECTION_HEADER NtSection;
  214. if (!ARGUMENT_PRESENT( LastRvaSection ) ||
  215. (NtSection = *LastRvaSection) == NULL ||
  216. Rva < NtSection->VirtualAddress ||
  217. Rva >= NtSection->VirtualAddress + NtSection->SizeOfRawData
  218. ) {
  219. NtSection = RtlImageRvaToSection( NtHeaders,
  220. Base,
  221. Rva
  222. );
  223. }
  224. if (NtSection != NULL) {
  225. if (LastRvaSection != NULL) {
  226. *LastRvaSection = NtSection;
  227. }
  228. return (PVOID)((ULONG)Base +
  229. (Rva - NtSection->VirtualAddress) +
  230. NtSection->PointerToRawData
  231. );
  232. }
  233. else {
  234. return NULL;
  235. }
  236. }
  237. #endif