ReactOS 0.4.15-dev-7918-g2a2556c
capture.c File Reference
#include "csrlib.h"
#include <debug.h>
Include dependency graph for capture.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

VOID NTAPI CsrProbeForRead (_In_ PVOID Address, _In_ ULONG Length, _In_ ULONG Alignment)
 
VOID NTAPI CsrProbeForWrite (_In_ PVOID Address, _In_ ULONG Length, _In_ ULONG Alignment)
 
PCSR_CAPTURE_BUFFER NTAPI CsrAllocateCaptureBuffer (_In_ ULONG ArgumentCount, _In_ ULONG BufferSize)
 
ULONG NTAPI CsrAllocateMessagePointer (_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_ ULONG MessageLength, _Out_ PVOID *CapturedData)
 
VOID NTAPI CsrCaptureMessageBuffer (_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_opt_ PVOID MessageBuffer, _In_ ULONG MessageLength, _Out_ PVOID *CapturedData)
 
VOID NTAPI CsrFreeCaptureBuffer (_In_ _Frees_ptr_ PCSR_CAPTURE_BUFFER CaptureBuffer)
 
VOID NTAPI CsrCaptureMessageString (_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_opt_ PCSTR String, _In_ ULONG StringLength, _In_ ULONG MaximumLength, _Out_ PSTRING CapturedString)
 
VOID NTAPI CsrCaptureMessageUnicodeStringInPlace (_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _Inout_ PUNICODE_STRING String)
 
NTSTATUS NTAPI CsrCaptureMessageMultiUnicodeStringsInPlace (_Inout_ PCSR_CAPTURE_BUFFER *CaptureBuffer, _In_ ULONG StringsCount, _In_ PUNICODE_STRING *MessageStrings)
 
PLARGE_INTEGER NTAPI CsrCaptureTimeout (_In_ ULONG Milliseconds, _Out_ PLARGE_INTEGER Timeout)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file capture.c.

Function Documentation

◆ CsrAllocateCaptureBuffer()

PCSR_CAPTURE_BUFFER NTAPI CsrAllocateCaptureBuffer ( _In_ ULONG  ArgumentCount,
_In_ ULONG  BufferSize 
)

Definition at line 87 of file capture.c.

90{
91 PCSR_CAPTURE_BUFFER CaptureBuffer;
92 ULONG OffsetsArraySize;
94
95 /* Validate the argument count. Note that on server side, CSRSRV
96 * limits the count to MAXUSHORT; here we are a bit more lenient. */
97 if (ArgumentCount > (MAXLONG / sizeof(ULONG_PTR)))
98 return NULL;
99
100 OffsetsArraySize = ArgumentCount * sizeof(ULONG_PTR);
101
102 /*
103 * Validate the total buffer size.
104 * The total size of the header plus the pointer-offset array and the
105 * provided buffer, together with the alignment padding for each argument,
106 * must be less than MAXLONG aligned to 4-byte boundary.
107 */
108 MaximumSize = (MAXLONG & ~3) - FIELD_OFFSET(CSR_CAPTURE_BUFFER, PointerOffsetsArray);
109 if (OffsetsArraySize >= MaximumSize)
110 return NULL;
111 MaximumSize -= OffsetsArraySize;
112 if (BufferSize >= MaximumSize)
113 return NULL;
115 if ((ArgumentCount * 3) + 3 >= MaximumSize)
116 return NULL;
117
118 /* Add the size of the header and of the pointer-offset array */
119 BufferSize += FIELD_OFFSET(CSR_CAPTURE_BUFFER, PointerOffsetsArray) +
120 OffsetsArraySize;
121
122 /* Add the size of the alignment padding for each argument */
123 BufferSize += ArgumentCount * 3;
124
125 /* Align it to a 4-byte boundary */
126 BufferSize = (BufferSize + 3) & ~3;
127
128 /* Allocate memory from the port heap */
130 if (CaptureBuffer == NULL) return NULL;
131
132 /* Initialize the header */
133 CaptureBuffer->Size = BufferSize;
134 CaptureBuffer->PointerCount = 0;
135
136 /* Initialize the pointer-offset array */
137 RtlZeroMemory(CaptureBuffer->PointerOffsetsArray, OffsetsArraySize);
138
139 /* Point to the start of the free buffer */
140 CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->PointerOffsetsArray +
141 OffsetsArraySize);
142
143 /* Return the address of the buffer */
144 return CaptureBuffer;
145}
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
#define BufferSize
Definition: mmc.h:75
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define ULONG_PTR
Definition: config.h:101
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
ULONG PointerCount
Definition: csrmsg.h:94
ULONG_PTR PointerOffsetsArray[ANYSIZE_ARRAY]
Definition: csrmsg.h:96
HANDLE CsrPortHeap
Definition: connect.c:29
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
void * PVOID
Definition: typedefs.h:50
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define MAXLONG
Definition: umtypes.h:116
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254

Referenced by BaseCheckVDM(), CreateConsoleScreenBuffer(), CsrCaptureMessageMultiUnicodeStringsInPlace(), CsrClientConnectToServer(), DefineDosDeviceW(), DeviceEventWorker(), GetConsoleFontInfo(), GetConsoleProcessList(), GetNextVDMCommand(), GetVDMCurrentDirectories(), IntAddConsoleAlias(), IntAllocConsole(), IntAttachConsole(), IntExpungeConsoleCommandHistory(), IntGetConsoleAlias(), IntGetConsoleAliases(), IntGetConsoleAliasesLength(), IntGetConsoleAliasExes(), IntGetConsoleCommandHistory(), IntGetConsoleCommandHistoryLength(), IntGetConsoleInput(), IntGetConsoleTitle(), IntReadConsole(), IntReadConsoleOutput(), IntReadConsoleOutputCode(), IntRegisterConsoleIME(), IntSetConsoleNumberOfCommands(), IntSetConsoleTitle(), IntWriteConsole(), IntWriteConsoleInput(), IntWriteConsoleOutput(), IntWriteConsoleOutputCode(), RegisterConsoleVDM(), and SetVDMCurrentDirectories().

◆ CsrAllocateMessagePointer()

ULONG NTAPI CsrAllocateMessagePointer ( _Inout_ PCSR_CAPTURE_BUFFER  CaptureBuffer,
_In_ ULONG  MessageLength,
_Out_ PVOID CapturedData 
)

Definition at line 152 of file capture.c.

156{
157 if (MessageLength == 0)
158 {
159 *CapturedData = NULL;
160 CapturedData = NULL;
161 }
162 else
163 {
164 /* Set the capture data at our current available buffer */
165 *CapturedData = CaptureBuffer->BufferEnd;
166
167 /* Validate the size */
168 if (MessageLength >= MAXLONG) return 0;
169
170 /* Align it to a 4-byte boundary */
171 MessageLength = (MessageLength + 3) & ~3;
172
173 /* Move our available buffer beyond this space */
174 CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->BufferEnd + MessageLength);
175 }
176
177 /* Write down this pointer in the array and increase the count */
178 CaptureBuffer->PointerOffsetsArray[CaptureBuffer->PointerCount++] = (ULONG_PTR)CapturedData;
179
180 /* Return the aligned length */
181 return MessageLength;
182}

Referenced by CsrCaptureMessageBuffer(), CsrCaptureMessageString(), DefineDosDeviceW(), GetConsoleFontInfo(), GetConsoleProcessList(), GetNextVDMCommand(), GetVDMCurrentDirectories(), IntGetConsoleAlias(), IntGetConsoleAliases(), IntGetConsoleAliasExes(), IntGetConsoleCommandHistory(), IntGetConsoleInput(), IntGetConsoleTitle(), IntReadConsole(), IntReadConsoleOutput(), IntReadConsoleOutputCode(), and IntWriteConsoleOutput().

◆ CsrCaptureMessageBuffer()

VOID NTAPI CsrCaptureMessageBuffer ( _Inout_ PCSR_CAPTURE_BUFFER  CaptureBuffer,
_In_opt_ PVOID  MessageBuffer,
_In_ ULONG  MessageLength,
_Out_ PVOID CapturedData 
)

Definition at line 189 of file capture.c.

194{
195 /* Simply allocate a message pointer in the buffer */
196 CsrAllocateMessagePointer(CaptureBuffer, MessageLength, CapturedData);
197
198 /* Check if there was any data */
199 if (!MessageBuffer || !MessageLength) return;
200
201 /* Copy the data into the buffer */
202 RtlMoveMemory(*CapturedData, MessageBuffer, MessageLength);
203}
ULONG NTAPI CsrAllocateMessagePointer(_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_ ULONG MessageLength, _Out_ PVOID *CapturedData)
Definition: capture.c:152
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264

Referenced by BaseCheckVDM(), CreateConsoleScreenBuffer(), CsrClientConnectToServer(), DeviceEventWorker(), IntAddConsoleAlias(), IntAllocConsole(), IntAttachConsole(), IntExpungeConsoleCommandHistory(), IntGetConsoleAlias(), IntGetConsoleAliases(), IntGetConsoleAliasesLength(), IntGetConsoleCommandHistory(), IntGetConsoleCommandHistoryLength(), IntRegisterConsoleIME(), IntSetConsoleNumberOfCommands(), IntSetConsoleTitle(), IntWriteConsole(), IntWriteConsoleInput(), IntWriteConsoleOutputCode(), RegisterConsoleVDM(), and SetVDMCurrentDirectories().

◆ CsrCaptureMessageMultiUnicodeStringsInPlace()

NTSTATUS NTAPI CsrCaptureMessageMultiUnicodeStringsInPlace ( _Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer,
_In_ ULONG  StringsCount,
_In_ PUNICODE_STRING MessageStrings 
)

Definition at line 294 of file capture.c.

298{
299 ULONG Count;
300
301 if (!CaptureBuffer) return STATUS_INVALID_PARAMETER;
302
303 /* Allocate a new capture buffer if we don't have one already */
304 if (!*CaptureBuffer)
305 {
306 /* Compute the required size for the capture buffer */
307 ULONG Size = 0;
308
309 Count = 0;
310 while (Count < StringsCount)
311 {
312 if (MessageStrings[Count])
313 Size += MessageStrings[Count]->MaximumLength;
314
315 ++Count;
316 }
317
318 /* Allocate the capture buffer */
319 *CaptureBuffer = CsrAllocateCaptureBuffer(StringsCount, Size);
320 if (!*CaptureBuffer) return STATUS_NO_MEMORY;
321 }
322
323 /* Now capture each UNICODE string */
324 Count = 0;
325 while (Count < StringsCount)
326 {
327 if (MessageStrings[Count])
328 CsrCaptureMessageUnicodeStringInPlace(*CaptureBuffer, MessageStrings[Count]);
329
330 ++Count;
331 }
332
333 return STATUS_SUCCESS;
334}
int Count
Definition: noreturn.cpp:7
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_SUCCESS
Definition: shellext.h:65
PCSR_CAPTURE_BUFFER NTAPI CsrAllocateCaptureBuffer(_In_ ULONG ArgumentCount, _In_ ULONG BufferSize)
Definition: capture.c:87
VOID NTAPI CsrCaptureMessageUnicodeStringInPlace(_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _Inout_ PUNICODE_STRING String)
Definition: capture.c:271
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533

◆ CsrCaptureMessageString()

VOID NTAPI CsrCaptureMessageString ( _Inout_ PCSR_CAPTURE_BUFFER  CaptureBuffer,
_In_opt_ PCSTR  String,
_In_ ULONG  StringLength,
_In_ ULONG  MaximumLength,
_Out_ PSTRING  CapturedString 
)

Definition at line 222 of file capture.c.

228{
229 ASSERT(CapturedString != NULL);
230
231 /*
232 * If we don't have a string, initialize an empty one,
233 * otherwise capture the given string.
234 */
235 if (!String)
236 {
237 CapturedString->Length = 0;
238 CapturedString->MaximumLength = (USHORT)MaximumLength;
239
240 /* Allocate a pointer for it */
241 CsrAllocateMessagePointer(CaptureBuffer,
243 (PVOID*)&CapturedString->Buffer);
244 }
245 else
246 {
247 /* Cut-off the string length if needed */
248 if (StringLength > MaximumLength)
249 StringLength = MaximumLength;
250
251 CapturedString->Length = (USHORT)StringLength;
252
253 /* Allocate a buffer and get its size */
254 CapturedString->MaximumLength =
255 (USHORT)CsrAllocateMessagePointer(CaptureBuffer,
257 (PVOID*)&CapturedString->Buffer);
258
259 /* If the string has data, copy it into the buffer */
260 if (StringLength)
261 RtlMoveMemory(CapturedString->Buffer, String, StringLength);
262 }
263
264 /* Null-terminate the string if we don't take up the whole space */
265 if (CapturedString->Length < CapturedString->MaximumLength)
266 CapturedString->Buffer[CapturedString->Length] = ANSI_NULL;
267}
#define ASSERT(a)
Definition: mode.c:44
#define ANSI_NULL
unsigned short USHORT
Definition: pedump.c:61
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
_In_ WDFDMATRANSACTION _In_ size_t MaximumLength

Referenced by CsrCaptureMessageUnicodeStringInPlace().

◆ CsrCaptureMessageUnicodeStringInPlace()

VOID NTAPI CsrCaptureMessageUnicodeStringInPlace ( _Inout_ PCSR_CAPTURE_BUFFER  CaptureBuffer,
_Inout_ PUNICODE_STRING  String 
)

Definition at line 271 of file capture.c.

274{
275 ASSERT(String != NULL);
276
277 /* This is a way to capture the UNICODE string, since (Maximum)Length are also in bytes */
278 CsrCaptureMessageString(CaptureBuffer,
279 (PCSTR)String->Buffer,
280 String->Length,
281 String->MaximumLength,
282 (PSTRING)String);
283
284 /* Null-terminate the string if we don't take up the whole space */
285 if (String->Length + sizeof(WCHAR) <= String->MaximumLength)
286 String->Buffer[String->Length / sizeof(WCHAR)] = UNICODE_NULL;
287}
#define UNICODE_NULL
VOID NTAPI CsrCaptureMessageString(_Inout_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_opt_ PCSTR String, _In_ ULONG StringLength, _In_ ULONG MaximumLength, _Out_ PSTRING CapturedString)
Definition: capture.c:222
const char * PCSTR
Definition: typedefs.h:52
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by CsrCaptureMessageMultiUnicodeStringsInPlace().

◆ CsrCaptureTimeout()

PLARGE_INTEGER NTAPI CsrCaptureTimeout ( _In_ ULONG  Milliseconds,
_Out_ PLARGE_INTEGER  Timeout 
)

Definition at line 341 of file capture.c.

344{
345 /* Validate the time */
346 if (Milliseconds == -1) return NULL;
347
348 /* Convert to relative ticks */
349 Timeout->QuadPart = Milliseconds * -10000LL;
350 return Timeout;
351}
static ULONG Timeout
Definition: ping.c:61

◆ CsrFreeCaptureBuffer()

◆ CsrProbeForRead()

VOID NTAPI CsrProbeForRead ( _In_ PVOID  Address,
_In_ ULONG  Length,
_In_ ULONG  Alignment 
)

Definition at line 23 of file capture.c.

27{
28 volatile UCHAR *Pointer;
29 UCHAR Data;
30
31 /* Validate length */
32 if (Length == 0) return;
33
34 /* Validate alignment */
35 if ((ULONG_PTR)Address & (Alignment - 1))
36 {
37 /* Raise exception if it doesn't match */
39 }
40
41 /* Probe first byte */
42 Pointer = Address;
43 Data = *Pointer;
44
45 /* Probe last byte */
46 Pointer = (PUCHAR)Address + Length - 1;
47 Data = *Pointer;
48 (void)Data;
49}
DECLSPEC_NORETURN NTSYSAPI VOID NTAPI RtlRaiseStatus(_In_ NTSTATUS Status)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_DATATYPE_MISALIGNMENT
Definition: ntstatus.h:183
static WCHAR Address[46]
Definition: ping.c:68
unsigned char * PUCHAR
Definition: typedefs.h:53
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ CsrProbeForWrite()

VOID NTAPI CsrProbeForWrite ( _In_ PVOID  Address,
_In_ ULONG  Length,
_In_ ULONG  Alignment 
)

Definition at line 56 of file capture.c.

60{
61 volatile UCHAR *Pointer;
62
63 /* Validate length */
64 if (Length == 0) return;
65
66 /* Validate alignment */
67 if ((ULONG_PTR)Address & (Alignment - 1))
68 {
69 /* Raise exception if it doesn't match */
71 }
72
73 /* Probe first byte */
74 Pointer = Address;
75 *Pointer = *Pointer;
76
77 /* Probe last byte */
78 Pointer = (PUCHAR)Address + Length - 1;
79 *Pointer = *Pointer;
80}