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.

487 lines
12 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ntrtlp.h
  5. Abstract:
  6. Include file for NT runtime routines that are callable by both
  7. kernel mode code in the executive and user mode code in various
  8. NT subsystems, but which are private interfaces.
  9. Author:
  10. David N. Cutler (davec) 15-Aug-1989
  11. Environment:
  12. These routines are dynamically linked in the caller's executable and
  13. are callable in either kernel mode or user mode.
  14. Revision History:
  15. --*/
  16. #ifndef _NTRTLP_
  17. #define _NTRTLP_
  18. #include <ntos.h>
  19. #include <nturtl.h>
  20. #include <zwapi.h>
  21. #include <sxstypes.h>
  22. #if defined(_AMD64_)
  23. #include "amd64\ntrtlamd64.h"
  24. #elif defined(_X86_)
  25. #include "i386\ntrtl386.h"
  26. #elif defined(_IA64_)
  27. #include "ia64\ntrtli64.h"
  28. #else
  29. #error "no target architecture"
  30. #endif
  31. #ifdef BLDR_KERNEL_RUNTIME
  32. #undef try
  33. #define try if(1)
  34. #undef except
  35. #define except(a) else if (0)
  36. #undef finally
  37. #define finally if (1)
  38. #undef GetExceptionCode
  39. #define GetExceptionCode() 1
  40. #define finally if (1)
  41. #endif
  42. #include "string.h"
  43. #include "wchar.h"
  44. //
  45. // Machine state reporting. See machine specific includes for more.
  46. //
  47. VOID
  48. RtlpGetStackLimits (
  49. OUT PULONG_PTR LowLimit,
  50. OUT PULONG_PTR HighLimit
  51. );
  52. #if defined(_WIN64)
  53. PRUNTIME_FUNCTION
  54. RtlpLookupFunctionTable (
  55. IN PVOID ControlPc,
  56. OUT PVOID *ImageBase,
  57. #if defined(_IA64_)
  58. OUT PULONG64 Gp,
  59. #endif
  60. OUT PULONG SizeOfTable
  61. );
  62. extern PVOID RtlpFunctionAddressTable[];
  63. extern UNWIND_HISTORY_TABLE RtlpUnwindHistoryTable;
  64. #endif
  65. LONG
  66. LdrpCompareResourceNames(
  67. IN ULONG ResourceName,
  68. IN const IMAGE_RESOURCE_DIRECTORY* ResourceDirectory,
  69. IN const IMAGE_RESOURCE_DIRECTORY_ENTRY* ResourceDirectoryEntry
  70. );
  71. NTSTATUS
  72. LdrpSearchResourceSection(
  73. IN PVOID DllHandle,
  74. IN const ULONG_PTR* ResourceIdPath,
  75. IN ULONG ResourceIdPathLength,
  76. IN BOOLEAN FindDirectoryEntry,
  77. OUT PVOID *ResourceDirectoryOrData
  78. );
  79. LONG
  80. LdrpCompareResourceNames_U(
  81. IN ULONG_PTR ResourceName,
  82. IN const IMAGE_RESOURCE_DIRECTORY* ResourceDirectory,
  83. IN const IMAGE_RESOURCE_DIRECTORY_ENTRY* ResourceDirectoryEntry
  84. );
  85. NTSTATUS
  86. LdrpSearchResourceSection_U(
  87. IN PVOID DllHandle,
  88. IN const ULONG_PTR* ResourceIdPath,
  89. IN ULONG ResourceIdPathLength,
  90. IN ULONG Flags,
  91. OUT PVOID *ResourceDirectoryOrData
  92. );
  93. NTSTATUS
  94. LdrpAccessResourceData(
  95. IN PVOID DllHandle,
  96. IN const IMAGE_RESOURCE_DATA_ENTRY* ResourceDataEntry,
  97. OUT PVOID *Address OPTIONAL,
  98. OUT PULONG Size OPTIONAL
  99. );
  100. NTSTATUS
  101. LdrpAccessResourceDataNoMultipleLanguage(
  102. IN PVOID DllHandle,
  103. IN const IMAGE_RESOURCE_DATA_ENTRY* ResourceDataEntry,
  104. OUT PVOID *Address OPTIONAL,
  105. OUT PULONG Size OPTIONAL
  106. );
  107. VOID
  108. RtlpAnsiPszToUnicodePsz(
  109. IN PCHAR AnsiString,
  110. IN WCHAR *UnicodeString,
  111. IN USHORT AnsiStringLength
  112. );
  113. BOOLEAN
  114. RtlpDidUnicodeToOemWork(
  115. IN PCOEM_STRING OemString,
  116. IN PCUNICODE_STRING UnicodeString
  117. );
  118. extern CONST CCHAR RtlpBitsClearAnywhere[256];
  119. extern CONST CCHAR RtlpBitsClearLow[256];
  120. extern CONST CCHAR RtlpBitsClearHigh[256];
  121. extern CONST CCHAR RtlpBitsClearTotal[256];
  122. //
  123. // Macro that tells how many contiguous bits are set (i.e., 1) in
  124. // a byte
  125. //
  126. #define RtlpBitSetAnywhere( Byte ) RtlpBitsClearAnywhere[ (~(Byte) & 0xFF) ]
  127. //
  128. // Macro that tells how many contiguous LOW order bits are set
  129. // (i.e., 1) in a byte
  130. //
  131. #define RtlpBitsSetLow( Byte ) RtlpBitsClearLow[ (~(Byte) & 0xFF) ]
  132. //
  133. // Macro that tells how many contiguous HIGH order bits are set
  134. // (i.e., 1) in a byte
  135. //
  136. #define RtlpBitsSetHigh( Byte ) RtlpBitsClearHigh[ (~(Byte) & 0xFF) ]
  137. //
  138. // Macro that tells how many set bits (i.e., 1) there are in a byte
  139. //
  140. #define RtlpBitsSetTotal( Byte ) RtlpBitsClearTotal[ (~(Byte) & 0xFF) ]
  141. //
  142. // Upcase data table
  143. //
  144. extern PUSHORT Nls844UnicodeUpcaseTable;
  145. extern PUSHORT Nls844UnicodeLowercaseTable;
  146. //
  147. // Macros for Upper Casing a Unicode Code Point.
  148. //
  149. #define LOBYTE(w) ((UCHAR)((w)))
  150. #define HIBYTE(w) ((UCHAR)(((USHORT)((w)) >> 8) & 0xFF))
  151. #define GET8(w) ((ULONG)(((w) >> 8) & 0xff))
  152. #define GETHI4(w) ((ULONG)(((w) >> 4) & 0xf))
  153. #define GETLO4(w) ((ULONG)((w) & 0xf))
  154. /***************************************************************************\
  155. * TRAVERSE844W
  156. *
  157. * Traverses the 8:4:4 translation table for the given wide character. It
  158. * returns the final value of the 8:4:4 table, which is a WORD in length.
  159. *
  160. * Broken Down Version:
  161. * --------------------
  162. * Incr = pTable[GET8(wch)];
  163. * Incr = pTable[Incr + GETHI4(wch)];
  164. * Value = pTable[Incr + GETLO4(wch)];
  165. *
  166. * DEFINED AS A MACRO.
  167. *
  168. * 05-31-91 JulieB Created.
  169. \***************************************************************************/
  170. #define TRAVERSE844W(pTable, wch) \
  171. ( (pTable)[(pTable)[(pTable)[GET8((wch))] + GETHI4((wch))] + GETLO4((wch))] )
  172. //
  173. // NLS_UPCASE - Based on julieb's macros in nls.h
  174. //
  175. // We will have this upcase macro quickly shortcircuit out if the value
  176. // is within the normal ANSI range (i.e., < 127). We actually won't bother
  177. // with the 5 values above 'z' because they won't happen very often and
  178. // coding it this way lets us get out after 1 compare for value less than
  179. // 'a' and 2 compares for lowercase a-z.
  180. //
  181. #define NLS_UPCASE(wch) ( \
  182. ((wch) < 'a' ? \
  183. (wch) \
  184. : \
  185. ((wch) <= 'z' ? \
  186. (wch) - ('a'-'A') \
  187. : \
  188. ((WCHAR)((wch) + TRAVERSE844W(Nls844UnicodeUpcaseTable,(wch)))) \
  189. ) \
  190. ) \
  191. )
  192. #define NLS_DOWNCASE(wch) ( \
  193. ((wch) < 'A' ? \
  194. (wch) \
  195. : \
  196. ((wch) <= 'Z' ? \
  197. (wch) + ('a'-'A') \
  198. : \
  199. ((WCHAR)((wch) + TRAVERSE844W(Nls844UnicodeLowercaseTable,(wch)))) \
  200. ) \
  201. ) \
  202. )
  203. #if DBG && defined(NTOS_KERNEL_RUNTIME)
  204. #define RTL_PAGED_CODE() PAGED_CODE()
  205. #else
  206. #define RTL_PAGED_CODE()
  207. #endif
  208. //
  209. // The follow definition is used to support the Rtl compression engine
  210. // Every compression format that NT supports will need to supply
  211. // these set of routines in order to be called by NtRtl.
  212. //
  213. typedef NTSTATUS (*PRTL_COMPRESS_WORKSPACE_SIZE) (
  214. IN USHORT CompressionEngine,
  215. OUT PULONG CompressBufferWorkSpaceSize,
  216. OUT PULONG CompressFragmentWorkSpaceSize
  217. );
  218. typedef NTSTATUS (*PRTL_COMPRESS_BUFFER) (
  219. IN USHORT CompressionEngine,
  220. IN PUCHAR UncompressedBuffer,
  221. IN ULONG UncompressedBufferSize,
  222. OUT PUCHAR CompressedBuffer,
  223. IN ULONG CompressedBufferSize,
  224. IN ULONG UncompressedChunkSize,
  225. OUT PULONG FinalCompressedSize,
  226. IN PVOID WorkSpace
  227. );
  228. typedef NTSTATUS (*PRTL_DECOMPRESS_BUFFER) (
  229. OUT PUCHAR UncompressedBuffer,
  230. IN ULONG UncompressedBufferSize,
  231. IN PUCHAR CompressedBuffer,
  232. IN ULONG CompressedBufferSize,
  233. OUT PULONG FinalUncompressedSize
  234. );
  235. typedef NTSTATUS (*PRTL_DECOMPRESS_FRAGMENT) (
  236. OUT PUCHAR UncompressedFragment,
  237. IN ULONG UncompressedFragmentSize,
  238. IN PUCHAR CompressedBuffer,
  239. IN ULONG CompressedBufferSize,
  240. IN ULONG FragmentOffset,
  241. OUT PULONG FinalUncompressedSize,
  242. IN PVOID WorkSpace
  243. );
  244. typedef NTSTATUS (*PRTL_DESCRIBE_CHUNK) (
  245. IN OUT PUCHAR *CompressedBuffer,
  246. IN PUCHAR EndOfCompressedBufferPlus1,
  247. OUT PUCHAR *ChunkBuffer,
  248. OUT PULONG ChunkSize
  249. );
  250. typedef NTSTATUS (*PRTL_RESERVE_CHUNK) (
  251. IN OUT PUCHAR *CompressedBuffer,
  252. IN PUCHAR EndOfCompressedBufferPlus1,
  253. OUT PUCHAR *ChunkBuffer,
  254. IN ULONG ChunkSize
  255. );
  256. //
  257. // Here is the declarations of the LZNT1 routines
  258. //
  259. NTSTATUS
  260. RtlCompressWorkSpaceSizeLZNT1 (
  261. IN USHORT CompressionEngine,
  262. OUT PULONG CompressBufferWorkSpaceSize,
  263. OUT PULONG CompressFragmentWorkSpaceSize
  264. );
  265. NTSTATUS
  266. RtlCompressBufferLZNT1 (
  267. IN USHORT CompressionEngine,
  268. IN PUCHAR UncompressedBuffer,
  269. IN ULONG UncompressedBufferSize,
  270. OUT PUCHAR CompressedBuffer,
  271. IN ULONG CompressedBufferSize,
  272. IN ULONG UncompressedChunkSize,
  273. OUT PULONG FinalCompressedSize,
  274. IN PVOID WorkSpace
  275. );
  276. NTSTATUS
  277. RtlDecompressBufferLZNT1 (
  278. OUT PUCHAR UncompressedBuffer,
  279. IN ULONG UncompressedBufferSize,
  280. IN PUCHAR CompressedBuffer,
  281. IN ULONG CompressedBufferSize,
  282. OUT PULONG FinalUncompressedSize
  283. );
  284. NTSTATUS
  285. RtlDecompressFragmentLZNT1 (
  286. OUT PUCHAR UncompressedFragment,
  287. IN ULONG UncompressedFragmentSize,
  288. IN PUCHAR CompressedBuffer,
  289. IN ULONG CompressedBufferSize,
  290. IN ULONG FragmentOffset,
  291. OUT PULONG FinalUncompressedSize,
  292. IN PVOID WorkSpace
  293. );
  294. NTSTATUS
  295. RtlDescribeChunkLZNT1 (
  296. IN OUT PUCHAR *CompressedBuffer,
  297. IN PUCHAR EndOfCompressedBufferPlus1,
  298. OUT PUCHAR *ChunkBuffer,
  299. OUT PULONG ChunkSize
  300. );
  301. NTSTATUS
  302. RtlReserveChunkLZNT1 (
  303. IN OUT PUCHAR *CompressedBuffer,
  304. IN PUCHAR EndOfCompressedBufferPlus1,
  305. OUT PUCHAR *ChunkBuffer,
  306. IN ULONG ChunkSize
  307. );
  308. NTSTATUS
  309. RtlpSecMemFreeVirtualMemory(
  310. IN HANDLE ProcessHandle,
  311. IN OUT PVOID *BaseAddress,
  312. IN OUT PSIZE_T RegionSize,
  313. IN ULONG FreeType
  314. );
  315. //
  316. // Define procedure prototypes for architecture specific debug support routines.
  317. //
  318. NTSTATUS
  319. DebugPrint(
  320. IN PSTRING Output,
  321. IN ULONG ComponentId,
  322. IN ULONG Level
  323. );
  324. ULONG
  325. DebugPrompt(
  326. IN PSTRING Output,
  327. IN PSTRING Input
  328. );
  329. #endif // _NTRTLP_
  330. //
  331. // Procedure prototype for exception logging routines.
  332. ULONG
  333. RtlpLogExceptionHandler(
  334. IN PEXCEPTION_RECORD ExceptionRecord,
  335. IN PCONTEXT ContextRecord,
  336. IN ULONG_PTR ControlPc,
  337. IN PVOID HandlerData,
  338. IN ULONG Size
  339. );
  340. VOID
  341. RtlpLogLastExceptionDisposition(
  342. IN ULONG LogIndex,
  343. IN EXCEPTION_DISPOSITION Disposition
  344. );
  345. #ifndef NTOS_KERNEL_RUNTIME
  346. BOOLEAN
  347. RtlCallVectoredExceptionHandlers(
  348. IN PEXCEPTION_RECORD ExceptionRecord,
  349. IN PCONTEXT ContextRecord
  350. );
  351. #define NO_ALTERNATE_RESOURCE_MODULE ((PVOID)(LONG_PTR)-1)
  352. typedef struct _ALT_RESOURCE_MODULE {
  353. //
  354. // Module handle for module known to application,
  355. // whose resource accesses we want to redirect.
  356. //
  357. PVOID ModuleBase;
  358. //
  359. // Module handle for module we loaded under the covers,
  360. // to which resource access will be redirected; will be
  361. // NO_ALTERNATE_RESOURCE_MODULE if we tried and failed to load
  362. // the alternate resource module for the module represented by
  363. // ModuleBase.
  364. //
  365. PVOID AlternateModule;
  366. } ALT_RESOURCE_MODULE, *PALT_RESOURCE_MODULE;
  367. BOOLEAN
  368. LdrpVerifyAlternateResourceModule(
  369. IN PWSTR LangID,
  370. IN PVOID Module,
  371. IN PVOID AlternateModule,
  372. IN LPWSTR BaseDllName
  373. );
  374. BOOLEAN
  375. LdrpSetAlternateResourceModuleHandle(
  376. IN PVOID Module,
  377. IN PVOID AlternateModule
  378. );
  379. #endif
  380. #define NUMBER_OF(x) (sizeof(x) / sizeof((x)[0]))
  381. #if !defined(NTOS_KERNEL_RUNTIME)
  382. #if DBG
  383. PCUNICODE_STRING RtlpGetImagePathName(VOID);
  384. #define RtlpGetCurrentProcessId() (HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess))
  385. #define RtlpGetCurrentThreadId() (HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread))
  386. #endif
  387. NTSTATUS
  388. RtlpThreadPoolGetActiveActivationContext(
  389. PACTIVATION_CONTEXT* ActivationContext
  390. );
  391. #endif // !defined(NTOS_KERNEL_RUNTIME)