d22644b599235f447f4408118688f71547beabc4
[reactos.git] / dll / ntdll / csr / capture.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: dll/ntdll/csr/capture.c
5 * PURPOSE: Routines for probing and capturing CSR API Messages
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntdll.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* GLOBALS ********************************************************************/
17
18 extern HANDLE CsrPortHeap;
19
20 /* FUNCTIONS ******************************************************************/
21
22 /*
23 * @implemented
24 */
25 VOID
26 NTAPI
27 CsrProbeForRead(IN PVOID Address,
28 IN ULONG Length,
29 IN ULONG Alignment)
30 {
31 volatile UCHAR *Pointer;
32 UCHAR Data;
33
34 /* Validate length */
35 if (Length == 0) return;
36
37 /* Validate alignment */
38 if ((ULONG_PTR)Address & (Alignment - 1))
39 {
40 /* Raise exception if it doesn't match */
41 RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
42 }
43
44 /* Probe first byte */
45 Pointer = Address;
46 Data = *Pointer;
47
48 /* Probe last byte */
49 Pointer = (PUCHAR)Address + Length - 1;
50 Data = *Pointer;
51 (void)Data;
52 }
53
54 /*
55 * @implemented
56 */
57 VOID
58 NTAPI
59 CsrProbeForWrite(IN PVOID Address,
60 IN ULONG Length,
61 IN ULONG Alignment)
62 {
63 volatile UCHAR *Pointer;
64
65 /* Validate length */
66 if (Length == 0) return;
67
68 /* Validate alignment */
69 if ((ULONG_PTR)Address & (Alignment - 1))
70 {
71 /* Raise exception if it doesn't match */
72 RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
73 }
74
75 /* Probe first byte */
76 Pointer = Address;
77 *Pointer = *Pointer;
78
79 /* Probe last byte */
80 Pointer = (PUCHAR)Address + Length - 1;
81 *Pointer = *Pointer;
82 }
83
84 /*
85 * @implemented
86 */
87 PCSR_CAPTURE_BUFFER
88 NTAPI
89 CsrAllocateCaptureBuffer(IN ULONG ArgumentCount,
90 IN ULONG BufferSize)
91 {
92 PCSR_CAPTURE_BUFFER CaptureBuffer;
93
94 /* Validate size */
95 if (BufferSize >= MAXLONG) return NULL;
96
97 /* Add the size of the header and for each offset to the pointers */
98 BufferSize += FIELD_OFFSET(CSR_CAPTURE_BUFFER, PointerOffsetsArray) +
99 (ArgumentCount * sizeof(ULONG_PTR));
100
101 /* Align it to a 4-byte boundary */
102 BufferSize = (BufferSize + 3) & ~3;
103
104 /* Allocate memory from the port heap */
105 CaptureBuffer = RtlAllocateHeap(CsrPortHeap, HEAP_ZERO_MEMORY, BufferSize);
106 if (CaptureBuffer == NULL) return NULL;
107
108 /* Initialize the header */
109 CaptureBuffer->Size = BufferSize;
110 CaptureBuffer->PointerCount = 0;
111
112 /* Initialize all the offsets */
113 RtlZeroMemory(CaptureBuffer->PointerOffsetsArray,
114 ArgumentCount * sizeof(ULONG_PTR));
115
116 /* Point to the start of the free buffer */
117 CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->PointerOffsetsArray +
118 ArgumentCount * sizeof(ULONG_PTR));
119
120 /* Return the address of the buffer */
121 return CaptureBuffer;
122 }
123
124 /*
125 * @implemented
126 */
127 ULONG
128 NTAPI
129 CsrAllocateMessagePointer(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
130 IN ULONG MessageLength,
131 OUT PVOID *CapturedData)
132 {
133 if (MessageLength == 0)
134 {
135 *CapturedData = NULL;
136 CapturedData = NULL;
137 }
138 else
139 {
140 /* Set the capture data at our current available buffer */
141 *CapturedData = CaptureBuffer->BufferEnd;
142
143 /* Validate the size */
144 if (MessageLength >= MAXLONG) return 0;
145
146 /* Align it to a 4-byte boundary */
147 MessageLength = (MessageLength + 3) & ~3;
148
149 /* Move our available buffer beyond this space */
150 CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->BufferEnd + MessageLength);
151 }
152
153 /* Write down this pointer in the array and increase the count */
154 CaptureBuffer->PointerOffsetsArray[CaptureBuffer->PointerCount++] = (ULONG_PTR)CapturedData;
155
156 /* Return the aligned length */
157 return MessageLength;
158 }
159
160 /*
161 * @implemented
162 */
163 VOID
164 NTAPI
165 CsrCaptureMessageBuffer(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
166 IN PVOID MessageBuffer OPTIONAL,
167 IN ULONG MessageLength,
168 OUT PVOID *CapturedData)
169 {
170 /* Simply allocate a message pointer in the buffer */
171 CsrAllocateMessagePointer(CaptureBuffer, MessageLength, CapturedData);
172
173 /* Check if there was any data */
174 if (!MessageBuffer || !MessageLength) return;
175
176 /* Copy the data into the buffer */
177 RtlMoveMemory(*CapturedData, MessageBuffer, MessageLength);
178 }
179
180 /*
181 * @implemented
182 */
183 VOID
184 NTAPI
185 CsrFreeCaptureBuffer(IN PCSR_CAPTURE_BUFFER CaptureBuffer)
186 {
187 /* Free it from the heap */
188 RtlFreeHeap(CsrPortHeap, 0, CaptureBuffer);
189 }
190
191 /*
192 * @unimplemented
193 */
194 NTSTATUS
195 NTAPI
196 CsrCaptureMessageMultiUnicodeStringsInPlace(IN PCSR_CAPTURE_BUFFER *CaptureBuffer,
197 IN ULONG MessageCount,
198 IN PVOID MessageStrings)
199 {
200 /* FIXME: allocate a buffer if we don't have one, and return it */
201 /* FIXME: call CsrCaptureMessageUnicodeStringInPlace for each string */
202 UNIMPLEMENTED;
203 return STATUS_NOT_IMPLEMENTED;
204 }
205
206 /*
207 * @implemented
208 */
209 VOID
210 NTAPI
211 CsrCaptureMessageString(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
212 IN LPSTR String OPTIONAL,
213 IN ULONG StringLength,
214 IN ULONG MaximumLength,
215 OUT PANSI_STRING CapturedString)
216 {
217 ULONG ReturnedLength;
218
219 /* If we don't have a string, initialize an empty one */
220 if (!String)
221 {
222 CapturedString->Length = 0;
223 CapturedString->MaximumLength = (USHORT)MaximumLength;
224
225 /* Allocate a pointer for it */
226 CsrAllocateMessagePointer(CaptureBuffer,
227 MaximumLength,
228 (PVOID*)&CapturedString->Buffer);
229 return;
230 }
231
232 /* Initialize this string */
233 CapturedString->Length = (USHORT)StringLength;
234
235 /* Allocate a buffer and get its size */
236 ReturnedLength = CsrAllocateMessagePointer(CaptureBuffer,
237 MaximumLength,
238 (PVOID*)&CapturedString->Buffer);
239 CapturedString->MaximumLength = (USHORT)ReturnedLength;
240
241 /* If the string had data */
242 if (StringLength)
243 {
244 /* Copy it into the capture buffer */
245 RtlMoveMemory(CapturedString->Buffer, String, MaximumLength);
246
247 /* If we don't take up the whole space */
248 if (CapturedString->Length < CapturedString->MaximumLength)
249 {
250 /* Null-terminate it */
251 CapturedString->Buffer[CapturedString->Length] = '\0';
252 }
253 }
254 }
255
256 /*
257 * @implemented
258 */
259 PLARGE_INTEGER
260 NTAPI
261 CsrCaptureTimeout(IN ULONG Milliseconds,
262 OUT PLARGE_INTEGER Timeout)
263 {
264 /* Validate the time */
265 if (Milliseconds == -1) return NULL;
266
267 /* Convert to relative ticks */
268 Timeout->QuadPart = Int32x32To64(Milliseconds, -10000);
269 return Timeout;
270 }
271
272 /* EOF */