[NTOS:LPC]: Improve the lisibility of some functions:
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Mon, 7 Nov 2016 00:55:34 +0000 (00:55 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Mon, 7 Nov 2016 00:55:34 +0000 (00:55 +0000)
- Group together the local parameters used for capturing user variables;
- Add some space between the list of parameters and the beginning of the code proper;
- Rename some variables or move some code to make some different functions look more similar (helps in finding functionality-similar functions in the code);
- Improve some comments;
- Whitespace fixes;
- Few code formatting.
- Use a ExFreePoolWithTag instead of ExFreePool, but otherwise no other functional code changes.

svn path=/trunk/; revision=73162

reactos/ntoskrnl/lpc/close.c
reactos/ntoskrnl/lpc/complete.c
reactos/ntoskrnl/lpc/connect.c
reactos/ntoskrnl/lpc/create.c
reactos/ntoskrnl/lpc/listen.c
reactos/ntoskrnl/lpc/port.c
reactos/ntoskrnl/lpc/reply.c
reactos/ntoskrnl/lpc/send.c

index f94e320..6ac3f62 100644 (file)
@@ -116,6 +116,8 @@ LpcpDestroyPortQueue(IN PLPCP_PORT_OBJECT Port,
     PLPCP_MESSAGE Message;
     PLPCP_PORT_OBJECT ConnectionPort = NULL;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
     PLPCP_MESSAGE Message;
     PLPCP_PORT_OBJECT ConnectionPort = NULL;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
+    PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
+
     PAGED_CODE();
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
     PAGED_CODE();
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
@@ -229,9 +231,10 @@ LpcpDestroyPortQueue(IN PLPCP_PORT_OBJECT Port,
         if (Port->MsgQueue.Semaphore)
         {
             /* Use the semaphore to find the port queue and free it */
         if (Port->MsgQueue.Semaphore)
         {
             /* Use the semaphore to find the port queue and free it */
-            ExFreePool(CONTAINING_RECORD(Port->MsgQueue.Semaphore,
-                                         LPCP_NONPAGED_PORT_QUEUE,
-                                         Semaphore));
+            MessageQueue = CONTAINING_RECORD(Port->MsgQueue.Semaphore,
+                                             LPCP_NONPAGED_PORT_QUEUE,
+                                             Semaphore);
+            ExFreePoolWithTag(MessageQueue, 'troP');
         }
     }
 }
         }
     }
 }
@@ -245,6 +248,7 @@ LpcpClosePort(IN PEPROCESS Process OPTIONAL,
               IN ULONG SystemHandleCount)
 {
     PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)Object;
               IN ULONG SystemHandleCount)
 {
     PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)Object;
+
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
     /* Only Server-side Connection Ports need clean up*/
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
     /* Only Server-side Connection Ports need clean up*/
@@ -305,10 +309,12 @@ LpcpDeletePort(IN PVOID ObjectBody)
     PLIST_ENTRY ListHead, NextEntry;
     HANDLE Pid;
     CLIENT_DIED_MSG ClientDiedMsg;
     PLIST_ENTRY ListHead, NextEntry;
     HANDLE Pid;
     CLIENT_DIED_MSG ClientDiedMsg;
-    Timeout.QuadPart = -1000000;
+
     PAGED_CODE();
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
     PAGED_CODE();
     LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
 
+    Timeout.QuadPart = -1000000;
+
     /* Check if this is a communication port */
     if ((Port->Flags & LPCP_PORT_TYPE_MASK) == LPCP_COMMUNICATION_PORT)
     {
     /* Check if this is a communication port */
     if ((Port->Flags & LPCP_PORT_TYPE_MASK) == LPCP_COMMUNICATION_PORT)
     {
@@ -347,8 +353,8 @@ LpcpDeletePort(IN PVOID ObjectBody)
         for (;;)
         {
             /* Send the message */
         for (;;)
         {
             /* Send the message */
-            if (LpcRequestPort(Port,
-                               &ClientDiedMsg.h) != STATUS_NO_MEMORY) break;
+            if (LpcRequestPort(Port, &ClientDiedMsg.h) != STATUS_NO_MEMORY)
+                break;
 
             /* Wait until trying again */
             KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
 
             /* Wait until trying again */
             KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
index 0f9f7f2..ac71133 100644 (file)
@@ -44,19 +44,20 @@ NtAcceptConnectPort(OUT PHANDLE PortHandle,
                     IN PPORT_VIEW ServerView,
                     IN PREMOTE_PORT_VIEW ClientView)
 {
                     IN PPORT_VIEW ServerView,
                     IN PREMOTE_PORT_VIEW ClientView)
 {
-    PLPCP_PORT_OBJECT ConnectionPort, ServerPort, ClientPort;
-    PVOID ClientSectionToMap = NULL;
-    HANDLE Handle;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     NTSTATUS Status;
     NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     ULONG ConnectionInfoLength;
     ULONG ConnectionInfoLength;
-    PLPCP_MESSAGE Message;
+    PLPCP_PORT_OBJECT ConnectionPort, ServerPort, ClientPort;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
+    PLPCP_MESSAGE Message;
+    PVOID ClientSectionToMap = NULL;
+    HANDLE Handle;
     PEPROCESS ClientProcess;
     PETHREAD ClientThread;
     LARGE_INTEGER SectionOffset;
     CLIENT_ID ClientId;
     ULONG MessageId;
     PEPROCESS ClientProcess;
     PETHREAD ClientThread;
     LARGE_INTEGER SectionOffset;
     CLIENT_ID ClientId;
     ULONG MessageId;
+
     PAGED_CODE();
     LPCTRACE(LPC_COMPLETE_DEBUG,
              "Context: %p. Message: %p. Accept: %lx. Views: %p/%p\n",
     PAGED_CODE();
     LPCTRACE(LPC_COMPLETE_DEBUG,
              "Context: %p. Message: %p. Accept: %lx. Views: %p/%p\n",
@@ -407,9 +408,10 @@ NTAPI
 NtCompleteConnectPort(IN HANDLE PortHandle)
 {
     NTSTATUS Status;
 NtCompleteConnectPort(IN HANDLE PortHandle)
 {
     NTSTATUS Status;
-    PLPCP_PORT_OBJECT Port;
     KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
+    PLPCP_PORT_OBJECT Port;
     PETHREAD Thread;
     PETHREAD Thread;
+
     PAGED_CODE();
     LPCTRACE(LPC_COMPLETE_DEBUG, "Handle: %p\n", PortHandle);
 
     PAGED_CODE();
     LPCTRACE(LPC_COMPLETE_DEBUG, "Handle: %p\n", PortHandle);
 
@@ -462,7 +464,7 @@ NtCompleteConnectPort(IN HANDLE PortHandle)
     KeReleaseGuardedMutex(&LpcpLock);
     LpcpCompleteWait(&Thread->LpcReplySemaphore);
 
     KeReleaseGuardedMutex(&LpcpLock);
     LpcpCompleteWait(&Thread->LpcReplySemaphore);
 
-    /* Dereference the Thread and Port  and return */
+    /* Dereference the Thread and Port and return */
     ObDereferenceObject(Port);
     ObDereferenceObject(Thread);
     LPCTRACE(LPC_COMPLETE_DEBUG, "Port: %p. Thread: %p\n", Port, Thread);
     ObDereferenceObject(Port);
     ObDereferenceObject(Thread);
     LPCTRACE(LPC_COMPLETE_DEBUG, "Port: %p. Thread: %p\n", Port, Thread);
index b53910f..b81afbd 100644 (file)
@@ -87,19 +87,20 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
                     IN OUT PVOID ConnectionInformation OPTIONAL,
                     IN OUT PULONG ConnectionInformationLength OPTIONAL)
 {
                     IN OUT PVOID ConnectionInformation OPTIONAL,
                     IN OUT PULONG ConnectionInformationLength OPTIONAL)
 {
+    NTSTATUS Status = STATUS_SUCCESS;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
+    PETHREAD Thread = PsGetCurrentThread();
     ULONG ConnectionInfoLength = 0;
     PLPCP_PORT_OBJECT Port, ClientPort;
     ULONG ConnectionInfoLength = 0;
     PLPCP_PORT_OBJECT Port, ClientPort;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
-    NTSTATUS Status = STATUS_SUCCESS;
-    HANDLE Handle;
-    PVOID SectionToMap;
     PLPCP_MESSAGE Message;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
     PLPCP_MESSAGE Message;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
-    PETHREAD Thread = PsGetCurrentThread();
     ULONG PortMessageLength;
     ULONG PortMessageLength;
+    HANDLE Handle;
+    PVOID SectionToMap;
     LARGE_INTEGER SectionOffset;
     PTOKEN Token;
     PTOKEN_USER TokenUserInfo;
     LARGE_INTEGER SectionOffset;
     PTOKEN Token;
     PTOKEN_USER TokenUserInfo;
+
     PAGED_CODE();
     LPCTRACE(LPC_CONNECT_DEBUG,
              "Name: %wZ. Qos: %p. Views: %p/%p. Sid: %p\n",
     PAGED_CODE();
     LPCTRACE(LPC_CONNECT_DEBUG,
              "Name: %wZ. Qos: %p. Views: %p/%p. Sid: %p\n",
@@ -138,7 +139,7 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
                                      LpcPortObjectType,
                                      PreviousMode,
                                      NULL,
                                      LpcPortObjectType,
                                      PreviousMode,
                                      NULL,
-                                     (PVOID *)&Port);
+                                     (PVOID*)&Port);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Failed to reference port '%wZ': 0x%lx\n", PortName, Status);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Failed to reference port '%wZ': 0x%lx\n", PortName, Status);
@@ -206,7 +207,7 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
                             sizeof(LPCP_PORT_OBJECT),
                             0,
                             0,
                             sizeof(LPCP_PORT_OBJECT),
                             0,
                             0,
-                            (PVOID *)&ClientPort);
+                            (PVOID*)&ClientPort);
     if (!NT_SUCCESS(Status))
     {
         /* Failed, dereference the server port and return */
     if (!NT_SUCCESS(Status))
     {
         /* Failed, dereference the server port and return */
@@ -395,7 +396,7 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
         InsertTailList(&Port->LpcReplyChainHead, &Thread->LpcReplyChain);
         Thread->LpcReplyMessage = Message;
 
         InsertTailList(&Port->LpcReplyChainHead, &Thread->LpcReplyChain);
         Thread->LpcReplyMessage = Message;
 
-        /* Now we can finally reference the client port and link it*/
+        /* Now we can finally reference the client port and link it */
         ObReferenceObject(ClientPort);
         ConnectMessage->ClientPort = ClientPort;
 
         ObReferenceObject(ClientPort);
         ConnectMessage->ClientPort = ClientPort;
 
@@ -421,15 +422,14 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
                  Status);
 
         /* If this is a waitable port, set the event */
                  Status);
 
         /* If this is a waitable port, set the event */
-        if (Port->Flags & LPCP_WAITABLE_PORT) KeSetEvent(&Port->WaitEvent,
-                                                         1,
-                                                         FALSE);
+        if (Port->Flags & LPCP_WAITABLE_PORT)
+            KeSetEvent(&Port->WaitEvent, 1, FALSE);
 
         /* Release the queue semaphore and leave the critical region */
         LpcpCompleteWait(Port->MsgQueue.Semaphore);
         KeLeaveCriticalRegion();
 
 
         /* Release the queue semaphore and leave the critical region */
         LpcpCompleteWait(Port->MsgQueue.Semaphore);
         KeLeaveCriticalRegion();
 
-        /* Now wait for a reply */
+        /* Now wait for a reply and set 'Status' */
         LpcpConnectWait(&Thread->LpcReplySemaphore, PreviousMode);
     }
 
         LpcpConnectWait(&Thread->LpcReplySemaphore, PreviousMode);
     }
 
@@ -478,7 +478,7 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
                                     NULL,
                                     PORT_ALL_ACCESS,
                                     0,
                                     NULL,
                                     PORT_ALL_ACCESS,
                                     0,
-                                    (PVOID *)NULL,
+                                    NULL,
                                     &Handle);
             if (NT_SUCCESS(Status))
             {
                                     &Handle);
             if (NT_SUCCESS(Status))
             {
index 10ab790..067f0a7 100644 (file)
@@ -19,11 +19,12 @@ NTAPI
 LpcpInitializePortQueue(IN PLPCP_PORT_OBJECT Port)
 {
     PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
 LpcpInitializePortQueue(IN PLPCP_PORT_OBJECT Port)
 {
     PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
+
     PAGED_CODE();
 
     /* Allocate the queue */
     MessageQueue = ExAllocatePoolWithTag(NonPagedPool,
     PAGED_CODE();
 
     /* Allocate the queue */
     MessageQueue = ExAllocatePoolWithTag(NonPagedPool,
-                                         sizeof(LPCP_NONPAGED_PORT_QUEUE),
+                                         sizeof(*MessageQueue),
                                          'troP');
     if (!MessageQueue) return STATUS_INSUFFICIENT_RESOURCES;
 
                                          'troP');
     if (!MessageQueue) return STATUS_INSUFFICIENT_RESOURCES;
 
@@ -46,12 +47,13 @@ LpcpCreatePort(OUT PHANDLE PortHandle,
                IN ULONG MaxPoolUsage,
                IN BOOLEAN Waitable)
 {
                IN ULONG MaxPoolUsage,
                IN BOOLEAN Waitable)
 {
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     NTSTATUS Status;
     NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PLPCP_PORT_OBJECT Port;
     HANDLE Handle;
     PUNICODE_STRING ObjectName;
     BOOLEAN NoName;
     PLPCP_PORT_OBJECT Port;
     HANDLE Handle;
     PUNICODE_STRING ObjectName;
     BOOLEAN NoName;
+
     PAGED_CODE();
     LPCTRACE(LPC_CREATE_DEBUG, "Name: %wZ\n", ObjectAttributes->ObjectName);
 
     PAGED_CODE();
     LPCTRACE(LPC_CREATE_DEBUG, "Name: %wZ\n", ObjectAttributes->ObjectName);
 
@@ -170,7 +172,7 @@ LpcpCreatePort(OUT PHANDLE PortHandle,
     Port->MaxMessageLength = MaxMessageLength;
 
     /* Insert it now */
     Port->MaxMessageLength = MaxMessageLength;
 
     /* Insert it now */
-    Status = ObInsertObject((PVOID)Port,
+    Status = ObInsertObject(Port,
                             NULL,
                             PORT_ALL_ACCESS,
                             0,
                             NULL,
                             PORT_ALL_ACCESS,
                             0,
index aa3f5be..c4fc1ca 100644 (file)
@@ -23,10 +23,11 @@ NtListenPort(IN HANDLE PortHandle,
              OUT PPORT_MESSAGE ConnectMessage)
 {
     NTSTATUS Status;
              OUT PPORT_MESSAGE ConnectMessage)
 {
     NTSTATUS Status;
+
     PAGED_CODE();
     LPCTRACE(LPC_LISTEN_DEBUG, "Handle: %p\n", PortHandle);
 
     PAGED_CODE();
     LPCTRACE(LPC_LISTEN_DEBUG, "Handle: %p\n", PortHandle);
 
-    /* Wait forever for a connection request. */
+    /* Wait forever for a connection request */
     for (;;)
     {
         /* Do the wait */
     for (;;)
     {
         /* Do the wait */
@@ -35,7 +36,7 @@ NtListenPort(IN HANDLE PortHandle,
                                         NULL,
                                         ConnectMessage);
 
                                         NULL,
                                         ConnectMessage);
 
-        /* Accept only LPC_CONNECTION_REQUEST requests. */
+        /* Accept only LPC_CONNECTION_REQUEST requests */
         if ((Status != STATUS_SUCCESS) ||
             (LpcpGetMessageType(ConnectMessage) == LPC_CONNECTION_REQUEST))
         {
         if ((Status != STATUS_SUCCESS) ||
             (LpcpGetMessageType(ConnectMessage) == LPC_CONNECTION_REQUEST))
         {
@@ -48,5 +49,4 @@ NtListenPort(IN HANDLE PortHandle,
     return Status;
 }
 
     return Status;
 }
 
-
 /* EOF */
 /* EOF */
index 6414d02..28cca60 100644 (file)
@@ -60,6 +60,7 @@ LpcInitSystem(VOID)
                        NULL,
                        &LpcPortObjectType);
 
                        NULL,
                        &LpcPortObjectType);
 
+    /* Create the Waitable Port Object Type */
     RtlInitUnicodeString(&Name, L"WaitablePort");
     ObjectTypeInitializer.PoolType = NonPagedPool;
     ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT);
     RtlInitUnicodeString(&Name, L"WaitablePort");
     ObjectTypeInitializer.PoolType = NonPagedPool;
     ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT);
@@ -125,13 +126,14 @@ NTAPI
 NtImpersonateClientOfPort(IN HANDLE PortHandle,
                           IN PPORT_MESSAGE ClientMessage)
 {
 NtImpersonateClientOfPort(IN HANDLE PortHandle,
                           IN PPORT_MESSAGE ClientMessage)
 {
-    KPROCESSOR_MODE PreviousMode;
+    NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     CLIENT_ID ClientId;
     ULONG MessageId;
     PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL;
     PETHREAD ClientThread = NULL;
     SECURITY_CLIENT_CONTEXT ClientContext;
     CLIENT_ID ClientId;
     ULONG MessageId;
     PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL;
     PETHREAD ClientThread = NULL;
     SECURITY_CLIENT_CONTEXT ClientContext;
-    NTSTATUS Status;
+
     PAGED_CODE();
 
     /* Check the previous mode */
     PAGED_CODE();
 
     /* Check the previous mode */
@@ -192,8 +194,7 @@ NtImpersonateClientOfPort(IN HANDLE PortHandle,
 
     /* Get the connected port and try to reference it */
     ConnectedPort = Port->ConnectedPort;
 
     /* Get the connected port and try to reference it */
     ConnectedPort = Port->ConnectedPort;
-    if ((ConnectedPort == NULL) ||
-        !ObReferenceObjectSafe(ConnectedPort))
+    if ((ConnectedPort == NULL) || !ObReferenceObjectSafe(ConnectedPort))
     {
         DPRINT1("Failed to reference the connected port\n");
         ConnectedPort = NULL;
     {
         DPRINT1("Failed to reference the connected port\n");
         ConnectedPort = NULL;
@@ -254,13 +255,6 @@ NtImpersonateClientOfPort(IN HANDLE PortHandle,
     /* Get rid of the security context */
     SeDeleteClientSecurity(&ClientContext);
 
     /* Get rid of the security context */
     SeDeleteClientSecurity(&ClientContext);
 
-    goto Cleanup;
-
-CleanupWithLock:
-
-    /* Release the lock */
-    KeReleaseGuardedMutex(&LpcpLock);
-
 Cleanup:
 
     if (ConnectedPort != NULL)
 Cleanup:
 
     if (ConnectedPort != NULL)
@@ -272,6 +266,12 @@ Cleanup:
     ObDereferenceObject(Port);
 
     return Status;
     ObDereferenceObject(Port);
 
     return Status;
+
+CleanupWithLock:
+
+    /* Release the lock */
+    KeReleaseGuardedMutex(&LpcpLock);
+    goto Cleanup;
 }
 
 NTSTATUS
 }
 
 NTSTATUS
index b2fce63..910d0cd 100644 (file)
@@ -99,6 +99,7 @@ LpcpFindDataInfoMessage(
 {
     PLPCP_MESSAGE Message;
     PLIST_ENTRY ListEntry;
 {
     PLPCP_MESSAGE Message;
     PLIST_ENTRY ListEntry;
+
     PAGED_CODE();
 
     /* Check if the port we want is the connection port */
     PAGED_CODE();
 
     /* Check if the port we want is the connection port */
@@ -141,13 +142,14 @@ LpcpMoveMessage(IN PPORT_MESSAGE Destination,
                 IN ULONG MessageType,
                 IN PCLIENT_ID ClientId)
 {
                 IN ULONG MessageType,
                 IN PCLIENT_ID ClientId)
 {
-    /* Set the Message size */
     LPCTRACE((LPC_REPLY_DEBUG | LPC_SEND_DEBUG),
              "Destination/Origin: %p/%p. Data: %p. Length: %lx\n",
              Destination,
              Origin,
              Data,
              Origin->u1.Length);
     LPCTRACE((LPC_REPLY_DEBUG | LPC_SEND_DEBUG),
              "Destination/Origin: %p/%p. Data: %p. Length: %lx\n",
              Destination,
              Origin,
              Data,
              Origin->u1.Length);
+
+    /* Set the Message size */
     Destination->u1.Length = Origin->u1.Length;
 
     /* Set the Message Type */
     Destination->u1.Length = Origin->u1.Length;
 
     /* Set the Message Type */
@@ -188,12 +190,12 @@ NTAPI
 NtReplyPort(IN HANDLE PortHandle,
             IN PPORT_MESSAGE ReplyMessage)
 {
 NtReplyPort(IN HANDLE PortHandle,
             IN PPORT_MESSAGE ReplyMessage)
 {
-    PLPCP_PORT_OBJECT Port;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     NTSTATUS Status;
     NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
+    // PORT_MESSAGE CapturedReplyMessage;
+    PLPCP_PORT_OBJECT Port;
     PLPCP_MESSAGE Message;
     PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
     PLPCP_MESSAGE Message;
     PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
-    //PORT_MESSAGE CapturedReplyMessage;
 
     PAGED_CODE();
     LPCTRACE(LPC_REPLY_DEBUG,
 
     PAGED_CODE();
     LPCTRACE(LPC_REPLY_DEBUG,
@@ -274,8 +276,8 @@ NtReplyPort(IN HANDLE PortHandle,
     /* Make sure this is the reply the thread is waiting for */
     if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
         ((LpcpGetMessageFromThread(WakeupThread)) &&
     /* Make sure this is the reply the thread is waiting for */
     if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
         ((LpcpGetMessageFromThread(WakeupThread)) &&
-        (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)->
-        Request) != LPC_REQUEST)))
+        (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)-> Request)
+            != LPC_REQUEST)))
     {
         /* It isn't, fail */
         LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
     {
         /* It isn't, fail */
         LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
@@ -295,7 +297,7 @@ NtReplyPort(IN HANDLE PortHandle,
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        /* Fail */
+        /* Cleanup and return the exception code */
         LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
         ObDereferenceObject(WakeupThread);
         ObDereferenceObject(Port);
         LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
         ObDereferenceObject(WakeupThread);
         ObDereferenceObject(Port);
@@ -358,15 +360,15 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
                          OUT PPORT_MESSAGE ReceiveMessage,
                          IN PLARGE_INTEGER Timeout OPTIONAL)
 {
                          OUT PPORT_MESSAGE ReceiveMessage,
                          IN PLARGE_INTEGER Timeout OPTIONAL)
 {
-    PLPCP_PORT_OBJECT Port, ReceivePort, ConnectionPort = NULL;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(), WaitMode = PreviousMode;
     NTSTATUS Status;
     NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(), WaitMode = PreviousMode;
+    // PORT_MESSAGE CapturedReplyMessage;
+    LARGE_INTEGER CapturedTimeout;
+    PLPCP_PORT_OBJECT Port, ReceivePort, ConnectionPort = NULL;
     PLPCP_MESSAGE Message;
     PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
     ULONG ConnectionInfoLength;
     PLPCP_MESSAGE Message;
     PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
     PLPCP_CONNECTION_MESSAGE ConnectMessage;
     ULONG ConnectionInfoLength;
-    //PORT_MESSAGE CapturedReplyMessage;
-    LARGE_INTEGER CapturedTimeout;
 
     PAGED_CODE();
     LPCTRACE(LPC_REPLY_DEBUG,
 
     PAGED_CODE();
     LPCTRACE(LPC_REPLY_DEBUG,
@@ -516,8 +518,8 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
         /* Make sure this is the reply the thread is waiting for */
         if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
             ((LpcpGetMessageFromThread(WakeupThread)) &&
         /* Make sure this is the reply the thread is waiting for */
         if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
             ((LpcpGetMessageFromThread(WakeupThread)) &&
-             (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)->
-                                 Request) != LPC_REQUEST)))
+             (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)->Request)
+                != LPC_REQUEST)))
         {
             /* It isn't, fail */
             LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
         {
             /* It isn't, fail */
             LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
@@ -599,8 +601,7 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
     }
 
     /* Get the message on the queue */
     }
 
     /* Get the message on the queue */
-    Message = CONTAINING_RECORD(RemoveHeadList(&ReceivePort->
-                                               MsgQueue.ReceiveHead),
+    Message = CONTAINING_RECORD(RemoveHeadList(&ReceivePort->MsgQueue.ReceiveHead),
                                 LPCP_MESSAGE,
                                 Entry);
 
                                 LPCP_MESSAGE,
                                 Entry);
 
@@ -756,17 +757,18 @@ LpcpCopyRequestData(
     IN ULONG Index,
     IN PVOID Buffer,
     IN ULONG BufferLength,
     IN ULONG Index,
     IN PVOID Buffer,
     IN ULONG BufferLength,
-    OUT PULONG Returnlength)
+    OUT PULONG ReturnLength)
 {
 {
-    KPROCESSOR_MODE PreviousMode;
+    NTSTATUS Status;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PORT_MESSAGE CapturedMessage;
     PLPCP_PORT_OBJECT Port = NULL;
     PETHREAD ClientThread = NULL;
     PORT_MESSAGE CapturedMessage;
     PLPCP_PORT_OBJECT Port = NULL;
     PETHREAD ClientThread = NULL;
-    SIZE_T LocalReturnlength;
+    SIZE_T LocalReturnLength;
     PLPCP_MESSAGE InfoMessage;
     PLPCP_DATA_INFO DataInfo;
     PVOID DataInfoBaseAddress;
     PLPCP_MESSAGE InfoMessage;
     PLPCP_DATA_INFO DataInfo;
     PVOID DataInfoBaseAddress;
-    NTSTATUS Status;
+
     PAGED_CODE();
 
     /* Check the previous mode */
     PAGED_CODE();
 
     /* Check the previous mode */
@@ -889,7 +891,7 @@ LpcpCopyRequestData(
                                      DataInfoBaseAddress,
                                      BufferLength,
                                      PreviousMode,
                                      DataInfoBaseAddress,
                                      BufferLength,
                                      PreviousMode,
-                                     &LocalReturnlength);
+                                     &LocalReturnLength);
     }
     else
     {
     }
     else
     {
@@ -900,7 +902,7 @@ LpcpCopyRequestData(
                                      Buffer,
                                      BufferLength,
                                      PreviousMode,
                                      Buffer,
                                      BufferLength,
                                      PreviousMode,
-                                     &LocalReturnlength);
+                                     &LocalReturnLength);
     }
 
     if (!NT_SUCCESS(Status))
     }
 
     if (!NT_SUCCESS(Status))
@@ -910,16 +912,16 @@ LpcpCopyRequestData(
     }
 
     /* Check if the caller asked to return the copied length */
     }
 
     /* Check if the caller asked to return the copied length */
-    if (Returnlength != NULL)
+    if (ReturnLength != NULL)
     {
         _SEH2_TRY
         {
     {
         _SEH2_TRY
         {
-            *Returnlength = LocalReturnlength;
+            *ReturnLength = LocalReturnLength;
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
             /* Ignore */
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
             /* Ignore */
-            DPRINT1("Exception writing Returnlength, ignoring\n");
+            DPRINT1("Exception writing ReturnLength, ignoring\n");
         }
         _SEH2_END;
     }
         }
         _SEH2_END;
     }
@@ -941,7 +943,7 @@ CleanupWithLock:
 }
 
 /*
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 NTSTATUS
 NTAPI
  */
 NTSTATUS
 NTAPI
@@ -963,7 +965,7 @@ NtReadRequestData(IN HANDLE PortHandle,
 }
 
 /*
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 NTSTATUS
 NTAPI
  */
 NTSTATUS
 NTAPI
index be7ef1b..be5189d 100644 (file)
@@ -179,17 +179,17 @@ LpcRequestWaitReplyPort(IN PVOID PortObject,
                         IN PPORT_MESSAGE LpcRequest,
                         OUT PPORT_MESSAGE LpcReply)
 {
                         IN PPORT_MESSAGE LpcRequest,
                         OUT PPORT_MESSAGE LpcReply)
 {
-    PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     NTSTATUS Status = STATUS_SUCCESS;
     NTSTATUS Status = STATUS_SUCCESS;
-    PLPCP_MESSAGE Message;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PETHREAD Thread = PsGetCurrentThread();
     PETHREAD Thread = PsGetCurrentThread();
+    PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)PortObject;
+    PLPCP_PORT_OBJECT QueuePort, ReplyPort, ConnectionPort = NULL;
+    USHORT MessageType;
+    PLPCP_MESSAGE Message;
     BOOLEAN Callback = FALSE;
     PKSEMAPHORE Semaphore;
     BOOLEAN Callback = FALSE;
     PKSEMAPHORE Semaphore;
-    USHORT MessageType;
-    PAGED_CODE();
 
 
-    Port = (PLPCP_PORT_OBJECT)PortObject;
+    PAGED_CODE();
 
     LPCTRACE(LPC_SEND_DEBUG,
              "Port: %p. Messages: %p/%p. Type: %lx\n",
 
     LPCTRACE(LPC_SEND_DEBUG,
              "Port: %p. Messages: %p/%p. Type: %lx\n",
@@ -205,33 +205,26 @@ LpcRequestWaitReplyPort(IN PVOID PortObject,
     MessageType = LpcpGetMessageType(LpcRequest);
     switch (MessageType)
     {
     MessageType = LpcpGetMessageType(LpcRequest);
     switch (MessageType)
     {
-        /* No type */
+        /* No type, assume LPC request */
         case 0:
         case 0:
-
-            /* Assume LPC request */
             MessageType = LPC_REQUEST;
             break;
 
         /* LPC request callback */
         case LPC_REQUEST:
             MessageType = LPC_REQUEST;
             break;
 
         /* LPC request callback */
         case LPC_REQUEST:
-
-            /* This is a callback */
             Callback = TRUE;
             break;
 
             Callback = TRUE;
             break;
 
-        /* Anything else */
+        /* Anything else, nothing to do */
         case LPC_CLIENT_DIED:
         case LPC_PORT_CLOSED:
         case LPC_EXCEPTION:
         case LPC_DEBUG_EVENT:
         case LPC_ERROR_EVENT:
         case LPC_CLIENT_DIED:
         case LPC_PORT_CLOSED:
         case LPC_EXCEPTION:
         case LPC_DEBUG_EVENT:
         case LPC_ERROR_EVENT:
-
-            /* Nothing to do */
             break;
 
             break;
 
+        /* Invalid message type */
         default:
         default:
-
-            /* Invalid message type */
             return STATUS_INVALID_PARAMETER;
     }
 
             return STATUS_INVALID_PARAMETER;
     }
 
@@ -448,15 +441,14 @@ NtRequestPort(IN HANDLE PortHandle,
               IN PPORT_MESSAGE LpcRequest)
 {
     NTSTATUS Status;
               IN PPORT_MESSAGE LpcRequest)
 {
     NTSTATUS Status;
-    PLPCP_PORT_OBJECT Port, QueuePort, ConnectionPort = NULL;
-    ULONG MessageType;
-    PLPCP_MESSAGE Message;
     KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PETHREAD Thread = PsGetCurrentThread();
     PORT_MESSAGE CapturedLpcRequest;
     KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PETHREAD Thread = PsGetCurrentThread();
     PORT_MESSAGE CapturedLpcRequest;
+    PLPCP_PORT_OBJECT Port, QueuePort, ConnectionPort = NULL;
+    ULONG MessageType;
+    PLPCP_MESSAGE Message;
 
     PAGED_CODE();
 
     PAGED_CODE();
-
     LPCTRACE(LPC_SEND_DEBUG,
              "Handle: %p. Message: %p. Type: %lx\n",
              PortHandle,
     LPCTRACE(LPC_SEND_DEBUG,
              "Handle: %p. Message: %p. Type: %lx\n",
              PortHandle,
@@ -538,7 +530,7 @@ NtRequestPort(IN HANDLE PortHandle,
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        /* Fail */
+        /* Cleanup and return the exception code */
         LpcpFreeToPortZone(Message, 0);
         ObDereferenceObject(Port);
         _SEH2_YIELD(return _SEH2_GetExceptionCode());
         LpcpFreeToPortZone(Message, 0);
         ObDereferenceObject(Port);
         _SEH2_YIELD(return _SEH2_GetExceptionCode());
@@ -578,8 +570,7 @@ NtRequestPort(IN HANDLE PortHandle,
                 return STATUS_PORT_DISCONNECTED;
             }
         }
                 return STATUS_PORT_DISCONNECTED;
             }
         }
-        else if ((Port->Flags & LPCP_PORT_TYPE_MASK) !=
-            LPCP_COMMUNICATION_PORT)
+        else if ((Port->Flags & LPCP_PORT_TYPE_MASK) != LPCP_COMMUNICATION_PORT)
         {
             /* Use the connection port for anything but communication ports */
             ConnectionPort = QueuePort = Port->ConnectionPort;
         {
             /* Use the connection port for anything but communication ports */
             ConnectionPort = QueuePort = Port->ConnectionPort;
@@ -706,17 +697,18 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
                        IN PPORT_MESSAGE LpcRequest,
                        IN OUT PPORT_MESSAGE LpcReply)
 {
                        IN PPORT_MESSAGE LpcRequest,
                        IN OUT PPORT_MESSAGE LpcReply)
 {
-    PORT_MESSAGE LocalLpcRequest;
+    NTSTATUS Status;
+    PORT_MESSAGE CapturedLpcRequest;
     ULONG NumberOfDataEntries;
     PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL;
     ULONG NumberOfDataEntries;
     PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL;
-    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
-    NTSTATUS Status;
     PLPCP_MESSAGE Message;
     PLPCP_MESSAGE Message;
+    KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
     PETHREAD Thread = PsGetCurrentThread();
     BOOLEAN Callback;
     PKSEMAPHORE Semaphore;
     ULONG MessageType;
     PLPCP_DATA_INFO DataInfo;
     PETHREAD Thread = PsGetCurrentThread();
     BOOLEAN Callback;
     PKSEMAPHORE Semaphore;
     ULONG MessageType;
     PLPCP_DATA_INFO DataInfo;
+
     PAGED_CODE();
     LPCTRACE(LPC_SEND_DEBUG,
              "Handle: %p. Messages: %p/%p. Type: %lx\n",
     PAGED_CODE();
     LPCTRACE(LPC_SEND_DEBUG,
              "Handle: %p. Messages: %p/%p. Type: %lx\n",
@@ -736,7 +728,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
             /* Probe the full request message and copy the base structure */
             ProbeForRead(LpcRequest, sizeof(*LpcRequest), sizeof(ULONG));
             ProbeForRead(LpcRequest, LpcRequest->u1.s1.TotalLength, sizeof(ULONG));
             /* Probe the full request message and copy the base structure */
             ProbeForRead(LpcRequest, sizeof(*LpcRequest), sizeof(ULONG));
             ProbeForRead(LpcRequest, LpcRequest->u1.s1.TotalLength, sizeof(ULONG));
-            LocalLpcRequest = *LpcRequest;
+            CapturedLpcRequest = *LpcRequest;
 
             /* Probe the reply message for write */
             ProbeForWrite(LpcReply, sizeof(*LpcReply), sizeof(ULONG));
 
             /* Probe the reply message for write */
             ProbeForWrite(LpcReply, sizeof(*LpcReply), sizeof(ULONG));
@@ -758,7 +750,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
     }
     else
     {
     }
     else
     {
-        LocalLpcRequest = *LpcRequest;
+        CapturedLpcRequest = *LpcRequest;
         Status = LpcpVerifyMessageDataInfo(LpcRequest, &NumberOfDataEntries);
         if (!NT_SUCCESS(Status))
         {
         Status = LpcpVerifyMessageDataInfo(LpcRequest, &NumberOfDataEntries);
         if (!NT_SUCCESS(Status))
         {
@@ -768,15 +760,15 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
     }
 
     /* This flag is undocumented. Remove it before continuing */
     }
 
     /* This flag is undocumented. Remove it before continuing */
-    LocalLpcRequest.u2.s2.Type &= ~0x4000;
+    CapturedLpcRequest.u2.s2.Type &= ~0x4000;
 
     /* Check if this is an LPC Request */
 
     /* Check if this is an LPC Request */
-    if (LpcpGetMessageType(&LocalLpcRequest) == LPC_REQUEST)
+    if (LpcpGetMessageType(&CapturedLpcRequest) == LPC_REQUEST)
     {
         /* Then it's a callback */
         Callback = TRUE;
     }
     {
         /* Then it's a callback */
         Callback = TRUE;
     }
-    else if (LpcpGetMessageType(&LocalLpcRequest))
+    else if (LpcpGetMessageType(&CapturedLpcRequest))
     {
         /* This is a not kernel-mode message */
         DPRINT1("Not a kernel-mode message!\n");
     {
         /* This is a not kernel-mode message */
         DPRINT1("Not a kernel-mode message!\n");
@@ -785,24 +777,24 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
     else
     {
         /* This is a kernel-mode message without a callback */
     else
     {
         /* This is a kernel-mode message without a callback */
-        LocalLpcRequest.u2.s2.Type |= LPC_REQUEST;
+        CapturedLpcRequest.u2.s2.Type |= LPC_REQUEST;
         Callback = FALSE;
     }
 
     /* Get the message type */
         Callback = FALSE;
     }
 
     /* Get the message type */
-    MessageType = LocalLpcRequest.u2.s2.Type;
+    MessageType = CapturedLpcRequest.u2.s2.Type;
 
     /* Due to the above probe, we know that TotalLength is positive */
 
     /* Due to the above probe, we know that TotalLength is positive */
-    ASSERT(LocalLpcRequest.u1.s1.TotalLength >= 0);
+    ASSERT(CapturedLpcRequest.u1.s1.TotalLength >= 0);
 
     /* Validate the length */
 
     /* Validate the length */
-    if ((((ULONG)(USHORT)LocalLpcRequest.u1.s1.DataLength + sizeof(PORT_MESSAGE)) >
-         (ULONG)LocalLpcRequest.u1.s1.TotalLength))
+    if ((((ULONG)(USHORT)CapturedLpcRequest.u1.s1.DataLength + sizeof(PORT_MESSAGE)) >
+         (ULONG)CapturedLpcRequest.u1.s1.TotalLength))
     {
         /* Fail */
         DPRINT1("Invalid message length: %u, %u\n",
     {
         /* Fail */
         DPRINT1("Invalid message length: %u, %u\n",
-                LocalLpcRequest.u1.s1.DataLength,
-                LocalLpcRequest.u1.s1.TotalLength);
+                CapturedLpcRequest.u1.s1.DataLength,
+                CapturedLpcRequest.u1.s1.TotalLength);
         return STATUS_INVALID_PARAMETER;
     }
 
         return STATUS_INVALID_PARAMETER;
     }
 
@@ -816,13 +808,13 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
     if (!NT_SUCCESS(Status)) return Status;
 
     /* Validate the message length */
     if (!NT_SUCCESS(Status)) return Status;
 
     /* Validate the message length */
-    if (((ULONG)LocalLpcRequest.u1.s1.TotalLength > Port->MaxMessageLength) ||
-        ((ULONG)LocalLpcRequest.u1.s1.TotalLength <= (ULONG)LocalLpcRequest.u1.s1.DataLength))
+    if (((ULONG)CapturedLpcRequest.u1.s1.TotalLength > Port->MaxMessageLength) ||
+        ((ULONG)CapturedLpcRequest.u1.s1.TotalLength <= (ULONG)CapturedLpcRequest.u1.s1.DataLength))
     {
         /* Fail */
         DPRINT1("Invalid message length: %u, %u\n",
     {
         /* Fail */
         DPRINT1("Invalid message length: %u, %u\n",
-                LocalLpcRequest.u1.s1.DataLength,
-                LocalLpcRequest.u1.s1.TotalLength);
+                CapturedLpcRequest.u1.s1.DataLength,
+                CapturedLpcRequest.u1.s1.TotalLength);
         ObDereferenceObject(Port);
         return STATUS_PORT_MESSAGE_TOO_LONG;
     }
         ObDereferenceObject(Port);
         return STATUS_PORT_MESSAGE_TOO_LONG;
     }
@@ -874,7 +866,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Fail */
+            /* Cleanup and return the exception code */
             DPRINT1("Got exception!\n");
             LpcpFreeToPortZone(Message, 0);
             ObDereferenceObject(Port);
             DPRINT1("Got exception!\n");
             LpcpFreeToPortZone(Message, 0);
             ObDereferenceObject(Port);