/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
- * FILE: lib/ntdll/csr/capture.c
+ * FILE: dll/ntdll/csr/capture.c
* PURPOSE: Routines for probing and capturing CSR API Messages
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
*/
-/* INCLUDES *****************************************************************/
+/* INCLUDES *******************************************************************/
#include <ntdll.h>
+
#define NDEBUG
#include <debug.h>
-/* GLOBALS *******************************************************************/
+/* GLOBALS ********************************************************************/
+
extern HANDLE CsrPortHeap;
-/* FUNCTIONS *****************************************************************/
+/* FUNCTIONS ******************************************************************/
/*
* @implemented
IN ULONG Length,
IN ULONG Alignment)
{
- PUCHAR Pointer;
+ volatile UCHAR *Pointer;
UCHAR Data;
/* Validate length */
RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
}
- /* Do the probe */
- Pointer = (PUCHAR)Address;
+ /* Probe first byte */
+ Pointer = Address;
Data = *Pointer;
- Pointer = (PUCHAR)((ULONG)Address + Length -1);
+
+ /* Probe last byte */
+ Pointer = (PUCHAR)Address + Length - 1;
Data = *Pointer;
+ (void)Data;
}
/*
IN ULONG Length,
IN ULONG Alignment)
{
- PUCHAR Pointer;
- UCHAR Data;
+ volatile UCHAR *Pointer;
/* Validate length */
if (Length == 0) return;
RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
}
- /* Do the probe */
- Pointer = (PUCHAR)Address;
- Data = *Pointer;
- *Pointer = Data;
- Pointer = (PUCHAR)((ULONG)Address + Length -1);
- Data = *Pointer;
- *Pointer = Data;
+ /* Probe first byte */
+ Pointer = Address;
+ *Pointer = *Pointer;
+
+ /* Probe last byte */
+ Pointer = (PUCHAR)Address + Length - 1;
+ *Pointer = *Pointer;
}
/*
* @implemented
*/
-PVOID
+PCSR_CAPTURE_BUFFER
NTAPI
-CsrAllocateCaptureBuffer(ULONG ArgumentCount,
- ULONG BufferSize)
+CsrAllocateCaptureBuffer(IN ULONG ArgumentCount,
+ IN ULONG BufferSize)
{
PCSR_CAPTURE_BUFFER CaptureBuffer;
/* Validate size */
if (BufferSize >= MAXLONG) return NULL;
- /* Add the size of the header and for each pointer to the pointers */
- BufferSize += sizeof(CSR_CAPTURE_BUFFER) + (ArgumentCount * sizeof(PVOID));
+ /* Add the size of the header and for each offset to the pointers */
+ BufferSize += FIELD_OFFSET(CSR_CAPTURE_BUFFER, PointerOffsetsArray) +
+ (ArgumentCount * sizeof(ULONG_PTR));
+
+ /* Align it to a 4-byte boundary */
+ BufferSize = (BufferSize + 3) & ~3;
+
+ /* Add the size of the alignment padding for each argument */
+ BufferSize += ArgumentCount * 3;
/* Allocate memory from the port heap */
- CaptureBuffer = RtlAllocateHeap(CsrPortHeap, 0, BufferSize);
+ CaptureBuffer = RtlAllocateHeap(CsrPortHeap, HEAP_ZERO_MEMORY, BufferSize);
if (CaptureBuffer == NULL) return NULL;
/* Initialize the header */
CaptureBuffer->Size = BufferSize;
CaptureBuffer->PointerCount = 0;
- /* Initialize all the pointers */
- RtlZeroMemory(CaptureBuffer->PointerArray,
+ /* Initialize all the offsets */
+ RtlZeroMemory(CaptureBuffer->PointerOffsetsArray,
ArgumentCount * sizeof(ULONG_PTR));
- /* Point the start of the free buffer */
- CaptureBuffer->BufferEnd = (ULONG_PTR)CaptureBuffer->PointerArray +
- ArgumentCount * sizeof(ULONG_PTR);
+ /* Point to the start of the free buffer */
+ CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->PointerOffsetsArray +
+ ArgumentCount * sizeof(ULONG_PTR));
/* Return the address of the buffer */
return CaptureBuffer;
*/
ULONG
NTAPI
-CsrAllocateMessagePointer(PCSR_CAPTURE_BUFFER CaptureBuffer,
- ULONG MessageLength,
- PVOID *CaptureData)
+CsrAllocateMessagePointer(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
+ IN ULONG MessageLength,
+ OUT PVOID *CapturedData)
{
- /* If there's no data, our job is easy. */
if (MessageLength == 0)
{
- *CaptureData = NULL;
- CaptureData = NULL;
+ *CapturedData = NULL;
+ CapturedData = NULL;
}
else
{
/* Set the capture data at our current available buffer */
- *CaptureData = (PVOID)CaptureBuffer->BufferEnd;
+ *CapturedData = CaptureBuffer->BufferEnd;
/* Validate the size */
if (MessageLength >= MAXLONG) return 0;
MessageLength = (MessageLength + 3) & ~3;
/* Move our available buffer beyond this space */
- CaptureBuffer->BufferEnd += MessageLength;
+ CaptureBuffer->BufferEnd = (PVOID)((ULONG_PTR)CaptureBuffer->BufferEnd + MessageLength);
}
- /* Write down this pointer in the array */
- CaptureBuffer->PointerArray[CaptureBuffer->PointerCount] = (ULONG_PTR)CaptureData;
-
- /* Increase the pointer count */
- CaptureBuffer->PointerCount++;
+ /* Write down this pointer in the array and increase the count */
+ CaptureBuffer->PointerOffsetsArray[CaptureBuffer->PointerCount++] = (ULONG_PTR)CapturedData;
/* Return the aligned length */
return MessageLength;
*/
VOID
NTAPI
-CsrCaptureMessageBuffer(PCSR_CAPTURE_BUFFER CaptureBuffer,
- PVOID MessageString,
- ULONG StringLength,
- PVOID *CapturedData)
+CsrCaptureMessageBuffer(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
+ IN PVOID MessageBuffer OPTIONAL,
+ IN ULONG MessageLength,
+ OUT PVOID *CapturedData)
{
/* Simply allocate a message pointer in the buffer */
- CsrAllocateMessagePointer(CaptureBuffer, StringLength, CapturedData);
+ CsrAllocateMessagePointer(CaptureBuffer, MessageLength, CapturedData);
/* Check if there was any data */
- if (!MessageString || !StringLength) return;
+ if (!MessageBuffer || !MessageLength) return;
/* Copy the data into the buffer */
- RtlMoveMemory(*CapturedData, MessageString, StringLength);
+ RtlMoveMemory(*CapturedData, MessageBuffer, MessageLength);
}
/*
*/
VOID
NTAPI
-CsrFreeCaptureBuffer(PCSR_CAPTURE_BUFFER CaptureBuffer)
+CsrFreeCaptureBuffer(IN PCSR_CAPTURE_BUFFER CaptureBuffer)
{
/* Free it from the heap */
RtlFreeHeap(CsrPortHeap, 0, CaptureBuffer);
}
/*
- * @implemented
+ * @unimplemented
*/
NTSTATUS
NTAPI
*/
VOID
NTAPI
-CsrCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer,
- LPSTR String,
+CsrCaptureMessageString(IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer,
+ IN LPSTR String OPTIONAL,
IN ULONG StringLength,
IN ULONG MaximumLength,
OUT PANSI_STRING CapturedString)
if (!String)
{
CapturedString->Length = 0;
- CapturedString->MaximumLength = MaximumLength;
+ CapturedString->MaximumLength = (USHORT)MaximumLength;
/* Allocate a pointer for it */
CsrAllocateMessagePointer(CaptureBuffer,
}
/* Initialize this string */
- CapturedString->Length = StringLength;
-
+ CapturedString->Length = (USHORT)StringLength;
+
/* Allocate a buffer and get its size */
ReturnedLength = CsrAllocateMessagePointer(CaptureBuffer,
MaximumLength,
(PVOID*)&CapturedString->Buffer);
- CapturedString->MaximumLength = ReturnedLength;
+ CapturedString->MaximumLength = (USHORT)ReturnedLength;
/* If the string had data */
if (StringLength)
*/
PLARGE_INTEGER
NTAPI
-CsrCaptureTimeout(LONG Milliseconds,
- PLARGE_INTEGER Timeout)
+CsrCaptureTimeout(IN ULONG Milliseconds,
+ OUT PLARGE_INTEGER Timeout)
{
/* Validate the time */
if (Milliseconds == -1) return NULL;
/* Convert to relative ticks */
- Timeout->QuadPart = Milliseconds * -100000;
+ Timeout->QuadPart = Int32x32To64(Milliseconds, -10000);
return Timeout;
}