[KERNEL32] WaitNamedPipeW: Free Unicode buffer when leaving the function
[reactos.git] / reactos / dll / win32 / kernel32 / file / npipe.c
index 017f569..55aeed9 100644 (file)
 /* INCLUDES *****************************************************************/
 
 #include <k32.h>
+#include <wine/debug.h>
+
+WINE_DEFAULT_DEBUG_CHANNEL(kernel32file);
 
-#define NDEBUG
 //#define USING_PROPER_NPFS_WAIT_SEMANTICS
-#include "../include/debug.h"
 
 /* FUNCTIONS ****************************************************************/
 
@@ -49,11 +50,12 @@ CreateNamedPipeA(LPCSTR lpName,
                             lpSecurityAttributes);
 }
 
+
 /*
  * @implemented
  */
 HANDLE
-STDCALL
+WINAPI
 CreateNamedPipeW(LPCWSTR lpName,
                  DWORD dwOpenMode,
                  DWORD dwPipeMode,
@@ -87,7 +89,8 @@ CreateNamedPipeW(LPCWSTR lpName,
     }
 
     /* Convert to NT syntax */
-    if (nMaxInstances == PIPE_UNLIMITED_INSTANCES) nMaxInstances = -1;
+    if (nMaxInstances == PIPE_UNLIMITED_INSTANCES)
+        nMaxInstances = -1;
 
     /* Convert the name */
     Result = RtlDosPathNameToNtPathName_U(lpName,
@@ -97,12 +100,12 @@ CreateNamedPipeW(LPCWSTR lpName,
     if (!Result)
     {
         /* Conversion failed */
-           SetLastError(ERROR_PATH_NOT_FOUND);
-        return(INVALID_HANDLE_VALUE);
+        SetLastError(ERROR_PATH_NOT_FOUND);
+        return INVALID_HANDLE_VALUE;
     }
 
-    DPRINT("Pipe name: %wZ\n", &NamedPipeName);
-    DPRINT("Pipe name: %S\n", NamedPipeName.Buffer);
+    TRACE("Pipe name: %wZ\n", &NamedPipeName);
+    TRACE("Pipe name: %S\n", NamedPipeName.Buffer);
 
     /* Always case insensitive, check if we got extra attributes */
     Attributes = OBJ_CASE_INSENSITIVE;
@@ -112,7 +115,8 @@ CreateNamedPipeW(LPCWSTR lpName,
         SecurityDescriptor = lpSecurityAttributes->lpSecurityDescriptor;
 
         /* And check if this is pipe's handle will beinheritable */
-        if(lpSecurityAttributes->bInheritHandle) Attributes |= OBJ_INHERIT;
+        if (lpSecurityAttributes->bInheritHandle)
+            Attributes |= OBJ_INHERIT;
     }
 
     /* Now we can initialize the object attributes */
@@ -132,17 +136,19 @@ CreateNamedPipeW(LPCWSTR lpName,
     {
         CreateOptions |= FILE_WRITE_THROUGH;
     }
+
     if (!(dwOpenMode & FILE_FLAG_OVERLAPPED))
     {
         CreateOptions |= FILE_SYNCHRONOUS_IO_NONALERT;
     }
 
-    /* Handle all open modes */ 
+    /* Handle all open modes */
     if (dwOpenMode & PIPE_ACCESS_OUTBOUND)
     {
         ShareAccess |= FILE_SHARE_READ;
         DesiredAccess |= GENERIC_WRITE;
     }
+
     if (dwOpenMode & PIPE_ACCESS_INBOUND)
     {
         ShareAccess |= FILE_SHARE_WRITE;
@@ -223,7 +229,7 @@ CreateNamedPipeW(LPCWSTR lpName,
     if (!NT_SUCCESS(Status))
     {
         /* Failed to create it */
-        DPRINT1("NtCreateNamedPipe failed (Status %x)!\n", Status);
+        WARN("NtCreateNamedPipe failed (Status %x)!\n", Status);
         SetLastErrorByStatus (Status);
         return INVALID_HANDLE_VALUE;
     }
@@ -232,6 +238,7 @@ CreateNamedPipeW(LPCWSTR lpName,
     return PipeHandle;
 }
 
+
 /*
  * @implemented
  */
@@ -244,7 +251,7 @@ WaitNamedPipeA(LPCSTR lpNamedPipeName,
     UNICODE_STRING NameU;
 
     /* Convert the name to Unicode */
-    Basep8BitStringToLiveUnicodeString(&NameU, lpNamedPipeName);
+    Basep8BitStringToHeapUnicodeString(&NameU, lpNamedPipeName);
 
     /* Call the Unicode API */
     r = WaitNamedPipeW(NameU.Buffer, nTimeOut);
@@ -256,13 +263,14 @@ WaitNamedPipeA(LPCSTR lpNamedPipeName,
     return r;
 }
 
+
 /*
  * When NPFS will work properly, use this code instead. It is compatible with
  * Microsoft's NPFS.SYS. The main difference is that:
  *      - This code actually respects the timeout instead of ignoring it!
  *      - This code validates and creates the proper names for both UNC and local pipes
- *      - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or 
- *        \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the 
+ *      - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or
+ *        \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the
  *        FILE_PIPE_WAIT_FOR_BUFFER structure.
  */
 #ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
@@ -287,7 +295,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
     PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo;
 
     /* Start by making a unicode string of the name */
-    DPRINT("Sent path: %S\n", lpNamedPipeName);
+    TRACE("Sent path: %S\n", lpNamedPipeName);
     RtlCreateUnicodeString(&NamedPipeName, lpNamedPipeName);
     NameLength = NamedPipeName.Length / sizeof(WCHAR);
 
@@ -301,7 +309,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
     /* Find the path type of the name we were given */
     NewName = NamedPipeName;
     Type = RtlDetermineDosPathNameType_U(lpNamedPipeName);
+
     /* Check if this was a device path, ie : "\\.\pipe\name" */
     if (Type == RtlPathTypeLocalDevice)
     {
@@ -314,7 +322,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
         NewName.Length -= 9 * sizeof(WCHAR);
 
         /* Initialize the Dos Devices name */
-        DPRINT("NewName: %wZ\n", &NewName);
+        TRACE("NewName: %wZ\n", &NewName);
         RtlInitUnicodeString(&DevicePath, L"\\DosDevices\\pipe\\");
     }
     else if (Type == RtlPathTypeRootLocalDevice)
@@ -341,16 +349,16 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
         else
         {
             /* The name is invalid */
-            DPRINT1("Invalid name!\n");
-               SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD);
-               return FALSE;
+            WARN("Invalid name!\n");
+            SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD);
+            return FALSE;
         }
 
         /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
     }
     else
     {
-        DPRINT1("Invalid path type\n");
+        WARN("Invalid path type\n");
         SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD);
         return FALSE;
     }
@@ -366,7 +374,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
     }
 
     /* Initialize the object attributes */
-    DPRINT("Opening: %wZ\n", &DevicePath);
+    TRACE("Opening: %wZ\n", &DevicePath);
     InitializeObjectAttributes(&ObjectAttributes,
                                &DevicePath,
                                OBJ_CASE_INSENSITIVE,
@@ -383,11 +391,11 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
     if (!NT_SUCCESS(Status))
     {
         /* Fail; couldn't open */
-        DPRINT1("Status: %lx\n", Status);
+        WARN("Status: %lx\n", Status);
         SetLastErrorByStatus(Status);
         RtlFreeUnicodeString(&NamedPipeName);
         RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo);
-        return(FALSE);
+        return FALSE;
     }
 
     /* Check what timeout we got */
@@ -412,7 +420,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
         }
 
         /* In both cases, we do have a timeout */
-        WaitPipeInfo->TimeoutSpecified = FALSE;
+        WaitPipeInfo->TimeoutSpecified = TRUE;
     }
 
     /* Set the length and copy the name */
@@ -442,7 +450,7 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
     if (!NT_SUCCESS(Status))
     {
         /* Failure to wait on the pipe */
-        DPRINT1("Status: %lx\n", Status);
+        WARN("Status: %lx\n", Status);
         SetLastErrorByStatus (Status);
         return FALSE;
      }
@@ -454,78 +462,105 @@ WaitNamedPipeW(LPCWSTR lpNamedPipeName,
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 WaitNamedPipeW(LPCWSTR lpNamedPipeName,
-              DWORD nTimeOut)
+               DWORD nTimeOut)
 {
-   UNICODE_STRING NamedPipeName;
-   BOOL r;
-   NTSTATUS Status;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   FILE_PIPE_WAIT_FOR_BUFFER WaitPipe;
-   HANDLE FileHandle;
-   IO_STATUS_BLOCK Iosb;
-
-   r = RtlDosPathNameToNtPathName_U(lpNamedPipeName,
-                                   &NamedPipeName,
-                                   NULL,
-                                   NULL);
-   if (!r)
-     {
-       return(FALSE);
-     }
+    UNICODE_STRING NamedPipeName;
+    NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    FILE_PIPE_WAIT_FOR_BUFFER WaitPipe;
+    HANDLE FileHandle;
+    IO_STATUS_BLOCK Iosb;
 
-   InitializeObjectAttributes(&ObjectAttributes,
-                             &NamedPipeName,
-                             OBJ_CASE_INSENSITIVE,
-                             NULL,
-                             NULL);
-   Status = NtOpenFile(&FileHandle,
-                      FILE_READ_ATTRIBUTES | SYNCHRONIZE,
-                      &ObjectAttributes,
-                      &Iosb,
-                      FILE_SHARE_READ | FILE_SHARE_WRITE,
-                      FILE_SYNCHRONOUS_IO_NONALERT);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus (Status);
-       return(FALSE);
-     }
+    if (RtlDosPathNameToNtPathName_U(lpNamedPipeName,
+                                     &NamedPipeName,
+                                     NULL,
+                                     NULL) == FALSE)
+    {
+        return FALSE;
+    }
 
-   WaitPipe.Timeout.QuadPart = nTimeOut * -10000LL;
-
-   Status = NtFsControlFile(FileHandle,
-                           NULL,
-                           NULL,
-                           NULL,
-                           &Iosb,
-                           FSCTL_PIPE_WAIT,
-                           &WaitPipe,
-                           sizeof(WaitPipe),
-                           NULL,
-                           0);
-   NtClose(FileHandle);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus (Status);
-       return(FALSE);
-     }
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &NamedPipeName,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+    Status = NtOpenFile(&FileHandle,
+                        FILE_READ_ATTRIBUTES | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &Iosb,
+                        FILE_SHARE_READ | FILE_SHARE_WRITE,
+                        FILE_SYNCHRONOUS_IO_NONALERT);
+    if (!NT_SUCCESS(Status))
+    {
+        SetLastErrorByStatus(Status);
+        RtlFreeUnicodeString(&NamedPipeName);
+        return FALSE;
+    }
+
+    /* Check what timeout we got */
+    if (nTimeOut == NMPWAIT_USE_DEFAULT_WAIT)
+    {
+        /* Don't use a timeout */
+        WaitPipe.TimeoutSpecified = FALSE;
+    }
+    else
+    {
+        /* Check if we should wait forever */
+        if (nTimeOut == NMPWAIT_WAIT_FOREVER)
+        {
+            /* Set the max */
+            WaitPipe.Timeout.LowPart = 0;
+            WaitPipe.Timeout.HighPart = 0x80000000;
+        }
+        else
+        {
+            /* Convert to NT format */
+            WaitPipe.Timeout.QuadPart = UInt32x32To64(-10000, nTimeOut);
+        }
+
+        /* In both cases, we do have a timeout */
+        WaitPipe.TimeoutSpecified = TRUE;
+    }
+
+    Status = NtFsControlFile(FileHandle,
+                             NULL,
+                             NULL,
+                             NULL,
+                             &Iosb,
+                             FSCTL_PIPE_WAIT,
+                             &WaitPipe,
+                             sizeof(WaitPipe),
+                             NULL,
+                             0);
+    NtClose(FileHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        SetLastErrorByStatus(Status);
+        RtlFreeUnicodeString(&NamedPipeName);
+        return FALSE;
+    }
 
-   return(TRUE);
+    RtlFreeUnicodeString(&NamedPipeName);
+    return TRUE;
 }
 #endif
 
+
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 ConnectNamedPipe(IN HANDLE hNamedPipe,
                  IN LPOVERLAPPED lpOverlapped)
 {
-   NTSTATUS Status;
+    NTSTATUS Status;
 
-   if (lpOverlapped != NULL)
-     {
+    if (lpOverlapped != NULL)
+    {
         PVOID ApcContext;
 
         lpOverlapped->Internal = STATUS_PENDING;
@@ -544,13 +579,13 @@ ConnectNamedPipe(IN HANDLE hNamedPipe,
 
         /* return FALSE in case of failure and pending operations! */
         if (!NT_SUCCESS(Status) || Status == STATUS_PENDING)
-          {
-             SetLastErrorByStatus(Status);
-             return FALSE;
-          }
-     }
-   else
-     {
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
+    }
+    else
+    {
         IO_STATUS_BLOCK Iosb;
 
         Status = NtFsControlFile(hNamedPipe,
@@ -566,31 +601,32 @@ ConnectNamedPipe(IN HANDLE hNamedPipe,
 
         /* wait in case operation is pending */
         if (Status == STATUS_PENDING)
-          {
+        {
              Status = NtWaitForSingleObject(hNamedPipe,
                                             FALSE,
                                             NULL);
              if (NT_SUCCESS(Status))
-               {
-                  Status = Iosb.Status;
-               }
-          }
+             {
+                 Status = Iosb.Status;
+             }
+        }
 
         if (!NT_SUCCESS(Status))
-          {
-             SetLastErrorByStatus(Status);
-             return FALSE;
-          }
-     }
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
+    }
 
-   return TRUE;
+    return TRUE;
 }
 
+
 /*
  * @implemented
  */
 BOOL
-STDCALL
+WINAPI
 SetNamedPipeHandleState(HANDLE hNamedPipe,
                         LPDWORD lpMode,
                         LPDWORD lpMaxCollectionCount,
@@ -621,7 +657,7 @@ SetNamedPipeHandleState(HANDLE hNamedPipe,
         if (!NT_SUCCESS(Status))
         {
             SetLastErrorByStatus(Status);
-            return(FALSE);
+            return FALSE;
         }
     }
 
@@ -638,11 +674,10 @@ SetNamedPipeHandleState(HANDLE hNamedPipe,
                                             &RemoteSettings,
                                             sizeof(FILE_PIPE_REMOTE_INFORMATION),
                                             FilePipeRemoteInformation);
-
             if (!NT_SUCCESS(Status))
             {
                 SetLastErrorByStatus(Status);
-                return(FALSE);
+                return FALSE;
             }
         }
 
@@ -653,9 +688,8 @@ SetNamedPipeHandleState(HANDLE hNamedPipe,
         if (lpCollectDataTimeout)
         {
             /* Convert it to Quad */
-            RemoteSettings.CollectDataTime.QuadPart = -(LONGLONG)
-                                                       UInt32x32To64(10000,
-                                                                     *lpCollectDataTimeout);
+            RemoteSettings.CollectDataTime.QuadPart =
+                -(LONGLONG)UInt32x32To64(10000, *lpCollectDataTimeout);
         }
 
         /* Tell the driver to change them */
@@ -664,18 +698,17 @@ SetNamedPipeHandleState(HANDLE hNamedPipe,
                                       &RemoteSettings,
                                       sizeof(FILE_PIPE_REMOTE_INFORMATION),
                                       FilePipeRemoteInformation);
-
         if (!NT_SUCCESS(Status))
         {
             SetLastErrorByStatus(Status);
-            return(FALSE);
+            return FALSE;
         }
     }
 
-    /* All done */
     return TRUE;
 }
 
+
 /*
  * @implemented
  */
@@ -706,6 +739,7 @@ CallNamedPipeA(LPCSTR lpNamedPipeName,
                           nTimeOut);
 }
 
+
 /*
  * @implemented
  */
@@ -736,10 +770,12 @@ CallNamedPipeW(LPCWSTR lpNamedPipeName,
                             NULL);
 
         /* Success, break out */
-        if (hPipe != INVALID_HANDLE_VALUE) break;
+        if (hPipe != INVALID_HANDLE_VALUE)
+            break;
 
         /* Already tried twice, give up */
-        if (bRetry == FALSE) return FALSE;
+        if (bRetry == FALSE)
+            return FALSE;
 
         /* Wait on it */
         WaitNamedPipeW(lpNamedPipeName, nTimeOut);
@@ -766,16 +802,18 @@ CallNamedPipeW(LPCWSTR lpNamedPipeName,
                                nOutBufferSize,
                                lpBytesRead,
                                NULL);
-    
-    /* Close the handle and return */
+
+    /* Close the handle */
     CloseHandle(hPipe);
+
     return bError;
 }
 
+
 /*
  * @implemented
  */
-BOOL 
+BOOL
 WINAPI
 DisconnectNamedPipe(HANDLE hNamedPipe)
 {
@@ -797,7 +835,8 @@ DisconnectNamedPipe(HANDLE hNamedPipe)
     {
         /* Wait on NPFS to finish and get updated status */
         Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
-        if (NT_SUCCESS(Status)) Status = Iosb.Status;
+        if (NT_SUCCESS(Status))
+            Status = Iosb.Status;
     }
 
     /* Check for error */
@@ -806,210 +845,214 @@ DisconnectNamedPipe(HANDLE hNamedPipe)
         /* Fail */
         SetLastErrorByStatus(Status);
         return FALSE;
-       }
-    
+    }
+
     return TRUE;
 }
 
+
 /*
  * @unimplemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 GetNamedPipeHandleStateW(HANDLE hNamedPipe,
-                        LPDWORD lpState,
-                        LPDWORD lpCurInstances,
-                        LPDWORD lpMaxCollectionCount,
-                        LPDWORD lpCollectDataTimeout,
-                        LPWSTR lpUserName,
-                        DWORD nMaxUserNameSize)
+                         LPDWORD lpState,
+                         LPDWORD lpCurInstances,
+                         LPDWORD lpMaxCollectionCount,
+                         LPDWORD lpCollectDataTimeout,
+                         LPWSTR lpUserName,
+                         DWORD nMaxUserNameSize)
 {
-  IO_STATUS_BLOCK StatusBlock;
-  NTSTATUS Status;
-
-  if (lpState != NULL)
-  {
-    FILE_PIPE_INFORMATION PipeInfo;
+    IO_STATUS_BLOCK StatusBlock;
+    NTSTATUS Status;
 
-    Status = NtQueryInformationFile(hNamedPipe,
-                                    &StatusBlock,
-                                    &PipeInfo,
-                                    sizeof(FILE_PIPE_INFORMATION),
-                                    FilePipeInformation);
-    if (!NT_SUCCESS(Status))
+    if (lpState != NULL)
     {
-      SetLastErrorByStatus(Status);
-      return FALSE;
-    }
-
-    *lpState = ((PipeInfo.CompletionMode != FILE_PIPE_QUEUE_OPERATION) ? PIPE_NOWAIT : PIPE_WAIT);
-    *lpState |= ((PipeInfo.ReadMode != FILE_PIPE_BYTE_STREAM_MODE) ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE);
-  }
+        FILE_PIPE_INFORMATION PipeInfo;
 
-  if(lpCurInstances != NULL)
-  {
-    FILE_PIPE_LOCAL_INFORMATION LocalInfo;
+        Status = NtQueryInformationFile(hNamedPipe,
+                                        &StatusBlock,
+                                        &PipeInfo,
+                                        sizeof(FILE_PIPE_INFORMATION),
+                                        FilePipeInformation);
+        if (!NT_SUCCESS(Status))
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
 
-    Status = NtQueryInformationFile(hNamedPipe,
-                                    &StatusBlock,
-                                    &LocalInfo,
-                                    sizeof(FILE_PIPE_LOCAL_INFORMATION),
-                                    FilePipeLocalInformation);
-    if(!NT_SUCCESS(Status))
-    {
-      SetLastErrorByStatus(Status);
-      return FALSE;
+        *lpState = ((PipeInfo.CompletionMode != FILE_PIPE_QUEUE_OPERATION) ? PIPE_NOWAIT : PIPE_WAIT);
+        *lpState |= ((PipeInfo.ReadMode != FILE_PIPE_BYTE_STREAM_MODE) ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE);
     }
 
-    *lpCurInstances = min(LocalInfo.CurrentInstances, PIPE_UNLIMITED_INSTANCES);
-  }
+    if(lpCurInstances != NULL)
+    {
+        FILE_PIPE_LOCAL_INFORMATION LocalInfo;
 
-  if(lpMaxCollectionCount != NULL || lpCollectDataTimeout != NULL)
-  {
-    FILE_PIPE_REMOTE_INFORMATION RemoteInfo;
+        Status = NtQueryInformationFile(hNamedPipe,
+                                        &StatusBlock,
+                                        &LocalInfo,
+                                        sizeof(FILE_PIPE_LOCAL_INFORMATION),
+                                        FilePipeLocalInformation);
+        if (!NT_SUCCESS(Status))
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
 
-    Status = NtQueryInformationFile(hNamedPipe,
-                                    &StatusBlock,
-                                    &RemoteInfo,
-                                    sizeof(FILE_PIPE_REMOTE_INFORMATION),
-                                    FilePipeRemoteInformation);
-    if(!NT_SUCCESS(Status))
-    {
-      SetLastErrorByStatus(Status);
-      return FALSE;
+        *lpCurInstances = min(LocalInfo.CurrentInstances, PIPE_UNLIMITED_INSTANCES);
     }
 
-    if(lpMaxCollectionCount != NULL)
+    if (lpMaxCollectionCount != NULL || lpCollectDataTimeout != NULL)
     {
-      *lpMaxCollectionCount = RemoteInfo.MaximumCollectionCount;
+        FILE_PIPE_REMOTE_INFORMATION RemoteInfo;
+
+        Status = NtQueryInformationFile(hNamedPipe,
+                                        &StatusBlock,
+                                        &RemoteInfo,
+                                        sizeof(FILE_PIPE_REMOTE_INFORMATION),
+                                        FilePipeRemoteInformation);
+        if (!NT_SUCCESS(Status))
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
+
+        if (lpMaxCollectionCount != NULL)
+        {
+            *lpMaxCollectionCount = RemoteInfo.MaximumCollectionCount;
+        }
+
+        if(lpCollectDataTimeout != NULL)
+        {
+            /* FIXME */
+           *lpCollectDataTimeout = 0;
+        }
     }
 
-    if(lpCollectDataTimeout != NULL)
+    if (lpUserName != NULL)
     {
-      /* FIXME */
-      *lpCollectDataTimeout = 0;
+      /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
+                 retreive the user name with GetUserName(), revert the impersonation
+                 and finally restore the thread token */
     }
-  }
 
-  if(lpUserName != NULL)
-  {
-    /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
-               retreive the user name with GetUserName(), revert the impersonation
-               and finally restore the thread token */
-  }
-
-  return TRUE;
+    return TRUE;
 }
 
 
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 GetNamedPipeHandleStateA(HANDLE hNamedPipe,
-                        LPDWORD lpState,
-                        LPDWORD lpCurInstances,
-                        LPDWORD lpMaxCollectionCount,
-                        LPDWORD lpCollectDataTimeout,
-                        LPSTR lpUserName,
-                        DWORD nMaxUserNameSize)
+                         LPDWORD lpState,
+                         LPDWORD lpCurInstances,
+                         LPDWORD lpMaxCollectionCount,
+                         LPDWORD lpCollectDataTimeout,
+                         LPSTR lpUserName,
+                         DWORD nMaxUserNameSize)
 {
-  UNICODE_STRING UserNameW = {0};
-  ANSI_STRING UserNameA;
-  BOOL Ret;
-
-  if(lpUserName != NULL)
-  {
-    UserNameW.MaximumLength = (USHORT)nMaxUserNameSize * sizeof(WCHAR);
-    UserNameW.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW.MaximumLength);
-    if (UserNameW.Buffer == NULL)
+    UNICODE_STRING UserNameW = { 0, 0, NULL };
+    ANSI_STRING UserNameA;
+    BOOL Ret;
+
+    if(lpUserName != NULL)
     {
-      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-      return FALSE;
-    }
+        UserNameW.MaximumLength = (USHORT)nMaxUserNameSize * sizeof(WCHAR);
+        UserNameW.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW.MaximumLength);
+        if (UserNameW.Buffer == NULL)
+        {
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
 
-    UserNameA.Buffer = lpUserName;
-    UserNameA.Length = 0;
-    UserNameA.MaximumLength = (USHORT)nMaxUserNameSize;
-  }
-
-  Ret = GetNamedPipeHandleStateW(hNamedPipe,
-                                 lpState,
-                                 lpCurInstances,
-                                 lpMaxCollectionCount,
-                                 lpCollectDataTimeout,
-                                 UserNameW.Buffer,
-                                 nMaxUserNameSize);
-
-  if(Ret && lpUserName != NULL)
-  {
-    NTSTATUS Status;
+        UserNameA.Buffer = lpUserName;
+        UserNameA.Length = 0;
+        UserNameA.MaximumLength = (USHORT)nMaxUserNameSize;
+    }
 
-    RtlInitUnicodeString(&UserNameW, UserNameW.Buffer);
-    Status = RtlUnicodeStringToAnsiString(&UserNameA, &UserNameW, FALSE);
-    if(!NT_SUCCESS(Status))
+    Ret = GetNamedPipeHandleStateW(hNamedPipe,
+                                   lpState,
+                                   lpCurInstances,
+                                   lpMaxCollectionCount,
+                                   lpCollectDataTimeout,
+                                   UserNameW.Buffer,
+                                   nMaxUserNameSize);
+    if (Ret && lpUserName != NULL)
     {
-      SetLastErrorByStatus(Status);
-      Ret = FALSE;
+        NTSTATUS Status;
+
+        RtlInitUnicodeString(&UserNameW, UserNameW.Buffer);
+        Status = RtlUnicodeStringToAnsiString(&UserNameA, &UserNameW, FALSE);
+        if (!NT_SUCCESS(Status))
+        {
+            SetLastErrorByStatus(Status);
+            Ret = FALSE;
+        }
     }
-  }
 
-  if(UserNameW.Buffer != NULL)
-  {
-    RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW.Buffer);
-  }
+    if (UserNameW.Buffer != NULL)
+    {
+        RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW.Buffer);
+    }
 
-  return Ret;
+    return Ret;
 }
 
 
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 GetNamedPipeInfo(HANDLE hNamedPipe,
-                LPDWORD lpFlags,
-                LPDWORD lpOutBufferSize,
-                LPDWORD lpInBufferSize,
-                LPDWORD lpMaxInstances)
+                 LPDWORD lpFlags,
+                 LPDWORD lpOutBufferSize,
+                 LPDWORD lpInBufferSize,
+                 LPDWORD lpMaxInstances)
 {
-  FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation;
-  IO_STATUS_BLOCK StatusBlock;
-  NTSTATUS Status;
-
-  Status = NtQueryInformationFile(hNamedPipe,
-                                 &StatusBlock,
-                                 &PipeLocalInformation,
-                                 sizeof(FILE_PIPE_LOCAL_INFORMATION),
-                                 FilePipeLocalInformation);
-  if (!NT_SUCCESS(Status))
+    FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation;
+    IO_STATUS_BLOCK StatusBlock;
+    NTSTATUS Status;
+
+    Status = NtQueryInformationFile(hNamedPipe,
+                                    &StatusBlock,
+                                    &PipeLocalInformation,
+                                    sizeof(FILE_PIPE_LOCAL_INFORMATION),
+                                    FilePipeLocalInformation);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
 
-  if (lpFlags != NULL)
+    if (lpFlags != NULL)
     {
-      *lpFlags = (PipeLocalInformation.NamedPipeEnd == FILE_PIPE_SERVER_END) ? PIPE_SERVER_END : PIPE_CLIENT_END;
-      *lpFlags |= (PipeLocalInformation.NamedPipeType == 1) ? PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
+        *lpFlags = (PipeLocalInformation.NamedPipeEnd == FILE_PIPE_SERVER_END) ? PIPE_SERVER_END : PIPE_CLIENT_END;
+        *lpFlags |= (PipeLocalInformation.NamedPipeType == 1) ? PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
     }
 
-  if (lpOutBufferSize != NULL)
-    *lpOutBufferSize = PipeLocalInformation.OutboundQuota;
+    if (lpOutBufferSize != NULL)
+        *lpOutBufferSize = PipeLocalInformation.OutboundQuota;
 
-  if (lpInBufferSize != NULL)
-    *lpInBufferSize = PipeLocalInformation.InboundQuota;
+    if (lpInBufferSize != NULL)
+        *lpInBufferSize = PipeLocalInformation.InboundQuota;
 
-  if (lpMaxInstances != NULL)
+    if (lpMaxInstances != NULL)
     {
-      if (PipeLocalInformation.MaximumInstances >= 255)
-       *lpMaxInstances = PIPE_UNLIMITED_INSTANCES;
-      else
-       *lpMaxInstances = PipeLocalInformation.MaximumInstances;
+        if (PipeLocalInformation.MaximumInstances >= 255)
+            *lpMaxInstances = PIPE_UNLIMITED_INSTANCES;
+        else
+            *lpMaxInstances = PipeLocalInformation.MaximumInstances;
     }
 
-  return(TRUE);
+    return TRUE;
 }
 
+
 /*
  * @implemented
  */
@@ -1025,10 +1068,11 @@ PeekNamedPipe(HANDLE hNamedPipe,
     PFILE_PIPE_PEEK_BUFFER Buffer;
     IO_STATUS_BLOCK Iosb;
     ULONG BufferSize;
+    ULONG BytesRead;
     NTSTATUS Status;
 
     /* Calculate the buffer space that we'll need and allocate it */
-    BufferSize = nBufferSize + FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]);
+    BufferSize = nBufferSize + sizeof(FILE_PIPE_PEEK_BUFFER);
     Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize);
     if (Buffer == NULL)
     {
@@ -1051,11 +1095,13 @@ PeekNamedPipe(HANDLE hNamedPipe,
     {
         /* Wait for npfs to be done, and update the status */
         Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
-        if (NT_SUCCESS(Status)) Status = Iosb.Status;
+        if (NT_SUCCESS(Status))
+            Status = Iosb.Status;
     }
 
     /* Overflow is success for us */
-    if (Status == STATUS_BUFFER_OVERFLOW) Status = STATUS_SUCCESS;
+    if (Status == STATUS_BUFFER_OVERFLOW)
+        Status = STATUS_SUCCESS;
 
     /* If we failed */
     if (!NT_SUCCESS(Status))
@@ -1067,23 +1113,25 @@ PeekNamedPipe(HANDLE hNamedPipe,
     }
 
     /* Check if caller requested bytes available */
-    if (lpTotalBytesAvail) *lpTotalBytesAvail = Buffer->ReadDataAvailable;
+    if (lpTotalBytesAvail)
+        *lpTotalBytesAvail = Buffer->ReadDataAvailable;
+
+    /* Calculate the bytes returned, minus our structure overhead */
+    BytesRead = (ULONG)(Iosb.Information -
+                        FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
 
     /* Check if caller requested bytes read */
     if (lpBytesRead)
     {
-        /* Calculate the bytes returned, minus our structure overhead */
-        *lpBytesRead = (ULONG)(Iosb.Information -
-                               FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
+        /* Return the bytes read */
+        *lpBytesRead = BytesRead;
     }
 
     /* Check if caller requested bytes left */
     if (lpBytesLeftThisMessage)
     {
         /* Calculate total minus what we returned and our structure overhead */
-        *lpBytesLeftThisMessage = Buffer->MessageLength -
-                                  (ULONG)(Iosb.Information -
-                                          FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
+        *lpBytesLeftThisMessage = Buffer->MessageLength - BytesRead;
     }
 
     /* Check if the caller wanted to see the actual data */
@@ -1092,19 +1140,21 @@ PeekNamedPipe(HANDLE hNamedPipe,
         /* Give him what he wants */
         RtlCopyMemory(lpBuffer,
                       Buffer->Data,
-                         Iosb.Information -
-                      FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER, Data[0]));
+                      BytesRead);
     }
 
-    /* Free the buffer and return success */
+    /* Free the buffer */
     RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
+
     return TRUE;
 }
 
+
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+WINAPI
 TransactNamedPipe(IN HANDLE hNamedPipe,
                   IN LPVOID lpInBuffer,
                   IN DWORD nInBufferSize,
@@ -1113,15 +1163,15 @@ TransactNamedPipe(IN HANDLE hNamedPipe,
                   OUT LPDWORD lpBytesRead  OPTIONAL,
                   IN LPOVERLAPPED lpOverlapped  OPTIONAL)
 {
-   NTSTATUS Status;
+    NTSTATUS Status;
 
-   if (lpBytesRead != NULL)
-     {
+    if (lpBytesRead != NULL)
+    {
         *lpBytesRead = 0;
-     }
+    }
 
-   if (lpOverlapped != NULL)
-     {
+    if (lpOverlapped != NULL)
+    {
         PVOID ApcContext;
 
         ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
@@ -1137,21 +1187,19 @@ TransactNamedPipe(IN HANDLE hNamedPipe,
                                  nInBufferSize,
                                  lpOutBuffer,
                                  nOutBufferSize);
-
-        /* return FALSE in case of failure and pending operations! */
         if (!NT_SUCCESS(Status) || Status == STATUS_PENDING)
-          {
-             SetLastErrorByStatus(Status);
-             return FALSE;
-          }
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
 
         if (lpBytesRead != NULL)
-          {
-             *lpBytesRead = lpOverlapped->InternalHigh;
-          }
-     }
-   else
-     {
+        {
+            *lpBytesRead = lpOverlapped->InternalHigh;
+        }
+    }
+    else
+    {
 #if 0 /* We don't implement FSCTL_PIPE_TRANSCEIVE yet */
         IO_STATUS_BLOCK Iosb;
 
@@ -1165,59 +1213,58 @@ TransactNamedPipe(IN HANDLE hNamedPipe,
                                  nInBufferSize,
                                  lpOutBuffer,
                                  nOutBufferSize);
-
-        /* wait in case operation is pending */
         if (Status == STATUS_PENDING)
-          {
-             Status = NtWaitForSingleObject(hNamedPipe,
-                                            FALSE,
-                                            NULL);
-             if (NT_SUCCESS(Status))
-               {
-                  Status = Iosb.Status;
-               }
-          }
+        {
+            Status = NtWaitForSingleObject(hNamedPipe,
+                                           FALSE,
+                                           NULL);
+            if (NT_SUCCESS(Status))
+                Status = Iosb.Status;
+        }
 
         if (NT_SUCCESS(Status))
-          {
-             /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
-                check that case either and crashes (only after the operation
-                completed) */
-             *lpBytesRead = Iosb.Information;
-          }
+        {
+            /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
+               check that case either and crashes (only after the operation
+               completed) */
+            *lpBytesRead = Iosb.Information;
+        }
         else
-          {
-             SetLastErrorByStatus(Status);
-             return FALSE;
-          }
+        {
+            SetLastErrorByStatus(Status);
+            return FALSE;
+        }
 #else /* Workaround while FSCTL_PIPE_TRANSCEIVE not available */
         DWORD nActualBytes;
 
         while (0 != nInBufferSize &&
                WriteFile(hNamedPipe, lpInBuffer, nInBufferSize, &nActualBytes,
                          NULL))
-          {
+        {
              lpInBuffer = (LPVOID)((char *) lpInBuffer + nActualBytes);
              nInBufferSize -= nActualBytes;
-          }
+        }
+
         if (0 != nInBufferSize)
-          {
+        {
              /* Must have dropped out of the while 'cause WriteFile failed */
              return FALSE;
-          }
-        if (! ReadFile(hNamedPipe, lpOutBuffer, nOutBufferSize, &nActualBytes,
-                       NULL))
-          {
+        }
+
+        if (!ReadFile(hNamedPipe, lpOutBuffer, nOutBufferSize, &nActualBytes,
+                      NULL))
+        {
              return FALSE;
-          }
+        }
+
         if (NULL != lpBytesRead)
-          {
+        {
             *lpBytesRead = nActualBytes;
-          }
+        }
 #endif
-     }
+    }
 
-   return TRUE;
+    return TRUE;
 }
 
 /* EOF */