ReactOS  0.4.15-dev-2704-gd5265b0
capture.c File Reference
#include <ntdll.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 (IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN ULONG MessageLength, OUT PVOID *CapturedData)
 
VOID NTAPI CsrCaptureMessageBuffer (IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN PVOID MessageBuffer OPTIONAL, IN ULONG MessageLength, OUT PVOID *CapturedData)
 
VOID NTAPI CsrFreeCaptureBuffer (IN PCSR_CAPTURE_BUFFER CaptureBuffer)
 
VOID NTAPI CsrCaptureMessageString (IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN PCSTR String OPTIONAL, IN ULONG StringLength, IN ULONG MaximumLength, OUT PSTRING CapturedString)
 
static VOID CsrCaptureMessageUnicodeStringInPlace (IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN PUNICODE_STRING String)
 
NTSTATUS NTAPI CsrCaptureMessageMultiUnicodeStringsInPlace (OUT PCSR_CAPTURE_BUFFER *CaptureBuffer, IN ULONG StringsCount, IN PUNICODE_STRING *MessageStrings)
 
PLARGE_INTEGER NTAPI CsrCaptureTimeout (IN ULONG Milliseconds, OUT PLARGE_INTEGER Timeout)
 

Variables

HANDLE CsrPortHeap
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file capture.c.

Function Documentation

◆ CsrAllocateCaptureBuffer()

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

Definition at line 90 of file capture.c.

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

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(), IntSetConsoleNumberOfCommands(), IntSetConsoleTitle(), IntWriteConsole(), IntWriteConsoleInput(), IntWriteConsoleOutput(), IntWriteConsoleOutputCode(), RegisterConsoleVDM(), and SetVDMCurrentDirectories().

◆ CsrAllocateMessagePointer()

ULONG NTAPI CsrAllocateMessagePointer ( IN OUT PCSR_CAPTURE_BUFFER  CaptureBuffer,
IN ULONG  MessageLength,
OUT PVOID CapturedData 
)

Definition at line 154 of file capture.c.

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

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

◆ CsrCaptureMessageBuffer()

VOID NTAPI CsrCaptureMessageBuffer ( IN OUT PCSR_CAPTURE_BUFFER  CaptureBuffer,
IN PVOID MessageBuffer  OPTIONAL,
IN ULONG  MessageLength,
OUT PVOID CapturedData 
)

Definition at line 190 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(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN ULONG MessageLength, OUT PVOID *CapturedData)
Definition: capture.c:154
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264

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

◆ CsrCaptureMessageMultiUnicodeStringsInPlace()

NTSTATUS NTAPI CsrCaptureMessageMultiUnicodeStringsInPlace ( OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
IN ULONG  StringsCount,
IN PUNICODE_STRING MessageStrings 
)

Definition at line 292 of file capture.c.

295 {
296  ULONG Count;
297 
298  if (!CaptureBuffer) return STATUS_INVALID_PARAMETER;
299 
300  /* Allocate a new capture buffer if we don't have one already */
301  if (!*CaptureBuffer)
302  {
303  /* Compute the required size for the capture buffer */
304  ULONG Size = 0;
305 
306  Count = 0;
307  while (Count < StringsCount)
308  {
309  if (MessageStrings[Count])
310  Size += MessageStrings[Count]->MaximumLength;
311 
312  ++Count;
313  }
314 
315  /* Allocate the capture buffer */
316  *CaptureBuffer = CsrAllocateCaptureBuffer(StringsCount, Size);
317  if (!*CaptureBuffer) return STATUS_NO_MEMORY;
318  }
319 
320  /* Now capture each UNICODE string */
321  Count = 0;
322  while (Count < StringsCount)
323  {
324  if (MessageStrings[Count])
325  CsrCaptureMessageUnicodeStringInPlace(*CaptureBuffer, MessageStrings[Count]);
326 
327  ++Count;
328  }
329 
330  return STATUS_SUCCESS;
331 }
PCSR_CAPTURE_BUFFER NTAPI CsrAllocateCaptureBuffer(IN ULONG ArgumentCount, IN ULONG BufferSize)
Definition: capture.c:90
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
int Count
Definition: noreturn.cpp:7
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
static VOID CsrCaptureMessageUnicodeStringInPlace(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN PUNICODE_STRING String)
Definition: capture.c:268
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ CsrCaptureMessageString()

VOID NTAPI CsrCaptureMessageString ( IN OUT PCSR_CAPTURE_BUFFER  CaptureBuffer,
IN PCSTR String  OPTIONAL,
IN ULONG  StringLength,
IN ULONG  MaximumLength,
OUT PSTRING  CapturedString 
)

Definition at line 221 of file capture.c.

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

Referenced by CsrCaptureMessageUnicodeStringInPlace().

◆ CsrCaptureMessageUnicodeStringInPlace()

static VOID CsrCaptureMessageUnicodeStringInPlace ( IN OUT PCSR_CAPTURE_BUFFER  CaptureBuffer,
IN PUNICODE_STRING  String 
)
static

Definition at line 268 of file capture.c.

270 {
271  ASSERT(String != NULL);
272 
273  /* This is a way to capture the UNICODE string, since (Maximum)Length are also in bytes */
274  CsrCaptureMessageString(CaptureBuffer,
275  (PCSTR)String->Buffer,
276  String->Length,
277  String->MaximumLength,
278  (PSTRING)String);
279 
280  /* Null-terminate the string */
281  if (String->MaximumLength >= String->Length + sizeof(WCHAR))
282  {
283  String->Buffer[String->Length / sizeof(WCHAR)] = L'\0';
284  }
285 }
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2430
#define ASSERT(a)
Definition: mode.c:44
__wchar_t WCHAR
Definition: xmlstorage.h:180
VOID NTAPI CsrCaptureMessageString(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer, IN PCSTR String OPTIONAL, IN ULONG StringLength, IN ULONG MaximumLength, OUT PSTRING CapturedString)
Definition: capture.c:221
static const WCHAR L[]
Definition: oid.c:1250
#define NULL
Definition: types.h:112
const char * PCSTR
Definition: typedefs.h:52

Referenced by CsrCaptureMessageMultiUnicodeStringsInPlace().

◆ CsrCaptureTimeout()

PLARGE_INTEGER NTAPI CsrCaptureTimeout ( IN ULONG  Milliseconds,
OUT PLARGE_INTEGER  Timeout 
)

Definition at line 338 of file capture.c.

340 {
341  /* Validate the time */
342  if (Milliseconds == -1) return NULL;
343 
344  /* Convert to relative ticks */
345  Timeout->QuadPart = Milliseconds * -10000LL;
346  return Timeout;
347 }
#define LL
Definition: tui.h:84
static ULONG Timeout
Definition: ping.c:61
#define NULL
Definition: types.h:112

◆ CsrFreeCaptureBuffer()

◆ CsrProbeForRead()

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

Definition at line 28 of file capture.c.

31 {
32  volatile UCHAR *Pointer;
33  UCHAR Data;
34 
35  /* Validate length */
36  if (Length == 0) return;
37 
38  /* Validate alignment */
39  if ((ULONG_PTR)Address & (Alignment - 1))
40  {
41  /* Raise exception if it doesn't match */
43  }
44 
45  /* Probe first byte */
46  Pointer = Address;
47  Data = *Pointer;
48 
49  /* Probe last byte */
50  Pointer = (PUCHAR)Address + Length - 1;
51  Data = *Pointer;
52  (void)Data;
53 }
DECLSPEC_NORETURN NTSYSAPI VOID NTAPI RtlRaiseStatus(_In_ NTSTATUS Status)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
unsigned char * PUCHAR
Definition: retypes.h:3
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG _In_opt_ PVOID Data
Definition: wdfdevice.h:4527
uint32_t ULONG_PTR
Definition: typedefs.h:65
static WCHAR Address[46]
Definition: ping.c:68
unsigned char UCHAR
Definition: xmlstorage.h:181
#define STATUS_DATATYPE_MISALIGNMENT
Definition: ntstatus.h:183

◆ CsrProbeForWrite()

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

Definition at line 60 of file capture.c.

63 {
64  volatile UCHAR *Pointer;
65 
66  /* Validate length */
67  if (Length == 0) return;
68 
69  /* Validate alignment */
70  if ((ULONG_PTR)Address & (Alignment - 1))
71  {
72  /* Raise exception if it doesn't match */
74  }
75 
76  /* Probe first byte */
77  Pointer = Address;
78  *Pointer = *Pointer;
79 
80  /* Probe last byte */
81  Pointer = (PUCHAR)Address + Length - 1;
82  *Pointer = *Pointer;
83 }
DECLSPEC_NORETURN NTSYSAPI VOID NTAPI RtlRaiseStatus(_In_ NTSTATUS Status)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
unsigned char * PUCHAR
Definition: retypes.h:3
uint32_t ULONG_PTR
Definition: typedefs.h:65
static WCHAR Address[46]
Definition: ping.c:68
unsigned char UCHAR
Definition: xmlstorage.h:181
#define STATUS_DATATYPE_MISALIGNMENT
Definition: ntstatus.h:183

Variable Documentation

◆ CsrPortHeap