KD System Rewrite:
[reactos.git] / reactos / ntoskrnl / rtl / capture.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/capture.c
6 * PURPOSE: Helper routines for system calls.
7 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19 NTSTATUS
20 RtlCaptureUnicodeString(OUT PUNICODE_STRING Dest,
21 IN KPROCESSOR_MODE CurrentMode,
22 IN POOL_TYPE PoolType,
23 IN BOOLEAN CaptureIfKernel,
24 IN PUNICODE_STRING UnsafeSrc)
25 {
26 UNICODE_STRING Src;
27 NTSTATUS Status = STATUS_SUCCESS;
28
29 ASSERT(Dest != NULL);
30
31 /*
32 * Copy the source string structure to kernel space.
33 */
34
35 if(CurrentMode == UserMode)
36 {
37 _SEH_TRY
38 {
39 ProbeForRead(UnsafeSrc,
40 sizeof(UNICODE_STRING),
41 sizeof(ULONG));
42 Src = *UnsafeSrc;
43 if(Src.Length > 0)
44 {
45 ProbeForRead(Src.Buffer,
46 Src.Length,
47 sizeof(WCHAR));
48 }
49 }
50 _SEH_HANDLE
51 {
52 Status = _SEH_GetExceptionCode();
53 }
54 _SEH_END;
55
56 if(!NT_SUCCESS(Status))
57 {
58 return Status;
59 }
60 }
61 else if(!CaptureIfKernel)
62 {
63 /* just copy the UNICODE_STRING structure, the pointers are considered valid */
64 *Dest = *UnsafeSrc;
65 return STATUS_SUCCESS;
66 }
67 else
68 {
69 /* capture the string even though it is considered to be valid */
70 Src = *UnsafeSrc;
71 }
72
73 /*
74 * Initialize the destination string.
75 */
76 Dest->Length = Src.Length;
77 if(Src.Length > 0)
78 {
79 Dest->MaximumLength = Src.Length + sizeof(WCHAR);
80 Dest->Buffer = ExAllocatePool(PoolType, Dest->MaximumLength);
81 if (Dest->Buffer == NULL)
82 {
83 Dest->Length = Dest->MaximumLength = 0;
84 Dest->Buffer = NULL;
85 return STATUS_INSUFFICIENT_RESOURCES;
86 }
87 /*
88 * Copy the source string to kernel space.
89 */
90 _SEH_TRY
91 {
92 RtlCopyMemory(Dest->Buffer, Src.Buffer, Src.Length);
93 Dest->Buffer[Src.Length / sizeof(WCHAR)] = L'\0';
94 }
95 _SEH_HANDLE
96 {
97 Status = _SEH_GetExceptionCode();
98 }
99 _SEH_END;
100
101 if(!NT_SUCCESS(Status))
102 {
103 ExFreePool(Dest->Buffer);
104 Dest->Buffer = NULL;
105 Dest->Length = Dest->MaximumLength = 0;
106 }
107 }
108 else
109 {
110 Dest->MaximumLength = 0;
111 Dest->Buffer = NULL;
112 }
113
114 return Status;
115 }
116
117 VOID
118 RtlReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString,
119 IN KPROCESSOR_MODE CurrentMode,
120 IN BOOLEAN CaptureIfKernel)
121 {
122 if(CurrentMode != KernelMode || CaptureIfKernel )
123 {
124 RtlFreeUnicodeString(CapturedString);
125 }
126 }
127
128 /*
129 * @unimplemented
130 */
131 VOID
132 STDCALL
133 RtlCaptureContext (
134 OUT PCONTEXT ContextRecord
135 )
136 {
137 UNIMPLEMENTED;
138 }
139
140 /*
141 * @unimplemented
142 */
143 USHORT
144 STDCALL
145 RtlCaptureStackBackTrace (
146 IN ULONG FramesToSkip,
147 IN ULONG FramesToCapture,
148 OUT PVOID *BackTrace,
149 OUT PULONG BackTraceHash OPTIONAL
150 )
151 {
152 UNIMPLEMENTED;
153 return 0;
154 }
155
156 /* EOF */
157