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.

216 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. tracelib.c
  5. Abstract:
  6. Private trace libraries and stubs that allows user-mode to reside in NTDLL.
  7. Author:
  8. 15-Aug-2000 JeePang
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include "trcapi.h"
  16. #include "tracelib.h"
  17. /*ONG
  18. WmipSetDosError(
  19. IN ULONG DosError
  20. )
  21. {
  22. WmipSetLastError(DosError);
  23. return DosError;
  24. }*/
  25. ULONG
  26. WmipSetNtStatus(
  27. IN NTSTATUS Status
  28. )
  29. {
  30. ULONG ErrorCode = RtlNtStatusToDosError(Status);
  31. WmipSetLastError(ErrorCode);
  32. return ErrorCode;
  33. }
  34. PVOID
  35. WmipMemReserve(
  36. IN SIZE_T Size
  37. )
  38. {
  39. NTSTATUS Status;
  40. PVOID lpAddress = NULL;
  41. try {
  42. Status = NtAllocateVirtualMemory(
  43. NtCurrentProcess(),
  44. &lpAddress,
  45. 0,
  46. &Size,
  47. MEM_RESERVE,
  48. PAGE_READWRITE);
  49. }
  50. except (EXCEPTION_EXECUTE_HANDLER) {
  51. Status = GetExceptionCode();
  52. }
  53. if (NT_SUCCESS(Status)) {
  54. return lpAddress;
  55. }
  56. else {
  57. WmipSetNtStatus(Status);
  58. return NULL;
  59. }
  60. }
  61. PVOID
  62. WmipMemCommit(
  63. IN PVOID Buffer,
  64. IN SIZE_T Size
  65. )
  66. {
  67. NTSTATUS Status;
  68. try {
  69. Status = NtAllocateVirtualMemory(
  70. NtCurrentProcess(),
  71. &Buffer,
  72. 0,
  73. &Size,
  74. MEM_COMMIT,
  75. PAGE_READWRITE);
  76. }
  77. except (EXCEPTION_EXECUTE_HANDLER) {
  78. Status = GetExceptionCode();
  79. }
  80. if (NT_SUCCESS(Status)) {
  81. return Buffer;
  82. }
  83. else {
  84. WmipSetNtStatus(Status);
  85. return NULL;
  86. }
  87. }
  88. ULONG
  89. WmipMemFree(
  90. IN PVOID Buffer
  91. )
  92. {
  93. NTSTATUS Status;
  94. SIZE_T Size = 0;
  95. HANDLE hProcess = NtCurrentProcess();
  96. if (Buffer == NULL) {
  97. WmipSetDosError(ERROR_INVALID_PARAMETER);
  98. return FALSE;
  99. }
  100. try {
  101. Status = NtFreeVirtualMemory( hProcess, &Buffer, &Size, MEM_RELEASE);
  102. }
  103. except (EXCEPTION_EXECUTE_HANDLER) {
  104. Status = GetExceptionCode();
  105. }
  106. if (NT_SUCCESS(Status)) {
  107. return TRUE;
  108. }
  109. else {
  110. if (Status == STATUS_INVALID_PAGE_PROTECTION) {
  111. if (RtlFlushSecureMemoryCache(Buffer, Size)) {
  112. Status = NtFreeVirtualMemory(
  113. hProcess, Buffer, &Size, MEM_RELEASE);
  114. if (NT_SUCCESS(Status)) {
  115. return TRUE;
  116. }
  117. }
  118. }
  119. WmipSetNtStatus(Status);
  120. return FALSE;
  121. }
  122. }
  123. HANDLE
  124. WmipCreateFile(
  125. LPCWSTR lpFileName,
  126. DWORD dwDesiredAccess,
  127. DWORD dwShareMode,
  128. DWORD dwCreationDisposition,
  129. DWORD dwCreateFlags
  130. )
  131. {
  132. UNICODE_STRING FileName;
  133. RTL_RELATIVE_NAME RelativeName;
  134. PVOID FreeBuffer;
  135. OBJECT_ATTRIBUTES ObjectAttributes;
  136. IO_STATUS_BLOCK Iosb;
  137. NTSTATUS Status;
  138. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  139. SECURITY_QUALITY_OF_SERVICE SQos;
  140. RtlInitUnicodeString(&FileName, lpFileName);
  141. if (!RtlDosPathNameToNtPathName_U(
  142. lpFileName,
  143. &FileName,
  144. NULL,
  145. &RelativeName)) {
  146. WmipSetDosError(ERROR_PATH_NOT_FOUND);
  147. return INVALID_HANDLE_VALUE;
  148. }
  149. FreeBuffer = FileName.Buffer;
  150. if (RelativeName.RelativeName.Length) {
  151. FileName = *(PUNICODE_STRING) &RelativeName.RelativeName;
  152. }
  153. else {
  154. RelativeName.ContainingDirectory = NULL;
  155. }
  156. InitializeObjectAttributes(
  157. &ObjectAttributes,
  158. &FileName,
  159. OBJ_CASE_INSENSITIVE,
  160. RelativeName.ContainingDirectory,
  161. NULL,
  162. );
  163. SQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  164. SQos.ImpersonationLevel = SecurityImpersonation;
  165. SQos.EffectiveOnly = TRUE;
  166. SQos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  167. ObjectAttributes.SecurityQualityOfService = &SQos;
  168. Status = NtCreateFile(
  169. &FileHandle,
  170. (ACCESS_MASK) dwDesiredAccess
  171. | SYNCHRONIZE | FILE_READ_ATTRIBUTES,
  172. &ObjectAttributes,
  173. &Iosb,
  174. NULL,
  175. FILE_ATTRIBUTE_NORMAL
  176. & (FILE_ATTRIBUTE_VALID_FLAGS & ~FILE_ATTRIBUTE_DIRECTORY),
  177. dwShareMode,
  178. dwCreationDisposition,
  179. dwCreateFlags | FILE_SYNCHRONOUS_IO_NONALERT,
  180. NULL,
  181. 0);
  182. if (!NT_SUCCESS(Status)) {
  183. if (Status == STATUS_OBJECT_NAME_COLLISION) {
  184. WmipSetDosError(ERROR_FILE_EXISTS);
  185. }
  186. else {
  187. WmipSetNtStatus(Status);
  188. }
  189. return INVALID_HANDLE_VALUE;
  190. }
  191. if (lpFileName != FreeBuffer) {
  192. RtlFreeHeap(RtlProcessHeap(), 0, FreeBuffer);
  193. }
  194. return FileHandle;
  195. }