- Silence TCPIP.
[reactos.git] / reactos / ntoskrnl / ob / object.c
index 9b2db7b..aaa6336 100644 (file)
@@ -15,6 +15,9 @@
 #define NDEBUG
 #include <internal/debug.h>
 
+#define UNICODE_PATH_SEP L'\\'
+#define UNICODE_NO_PATH L"..."
+#define OB_NAME_TAG TAG('O','b','N','m')
 
 typedef struct _RETENTION_CHECK_PARAMS
 {
@@ -31,78 +34,85 @@ ObpCaptureObjectName(IN OUT PUNICODE_STRING CapturedName,
                      IN KPROCESSOR_MODE AccessMode)
 {
     NTSTATUS Status = STATUS_SUCCESS;
-    UNICODE_STRING LocalName;
+    ULONG StringLength;
+    PWCHAR StringBuffer = NULL;
+    UNICODE_STRING LocalName = {}; /* <= GCC 4.0 + Optimizer */
     
-    /* First Probe the String */
-    DPRINT("ObpCaptureObjectName: %wZ\n", ObjectName);
-    if (AccessMode != KernelMode)
+    /* Initialize the Input String */
+    RtlInitUnicodeString(CapturedName, NULL);
+
+    /* Protect everything */
+    _SEH_TRY
     {
-        DPRINT("Probing Struct\n");
-        _SEH_TRY
+        /* First Probe the String */
+        DPRINT("ObpCaptureObjectName: %wZ\n", ObjectName);
+        if (AccessMode != KernelMode)
         {
-            /* FIXME: Explorer or win32 broken I think */
-            #if 0
             ProbeForRead(ObjectName,
                          sizeof(UNICODE_STRING),
                          sizeof(USHORT));
-            #endif
             LocalName = *ObjectName;
+
+            ProbeForRead(LocalName.Buffer,
+                         LocalName.Length,
+                         sizeof(WCHAR));
         }
-        _SEH_HANDLE
+        else
         {
-            Status = _SEH_GetExceptionCode();
+            /* No probing needed */
+            LocalName = *ObjectName;
         }
-        _SEH_END;
-        
-        if (NT_SUCCESS(Status))
+
+        /* Make sure there really is a string */
+        DPRINT("Probing OK\n");
+        if ((StringLength = LocalName.Length))
         {
-            DPRINT("Probing OK\n");
-             _SEH_TRY
+            /* Check that the size is a valid WCHAR multiple */
+            if ((StringLength & (sizeof(WCHAR) - 1)) ||
+                /* Check that the NULL-termination below will work */
+                (StringLength == (MAXUSHORT - sizeof(WCHAR) + 1)))
             {
-                #if 0
-                DPRINT("Probing buffer\n");
-                ProbeForRead(LocalName.Buffer,
-                             LocalName.Length,
-                             sizeof(USHORT));
-                #endif
+                /* PS: Please keep the checks above expanded for clarity */
+                DPRINT1("Invalid String Length\n");
+                Status = STATUS_OBJECT_NAME_INVALID;
             }
-            _SEH_HANDLE
+            else
             {
-                Status = _SEH_GetExceptionCode();
+                /* Allocate a non-paged buffer for this string */
+                DPRINT("Capturing String\n");
+                CapturedName->Length = StringLength;
+                CapturedName->MaximumLength = StringLength + sizeof(WCHAR);
+                if ((StringBuffer = ExAllocatePoolWithTag(NonPagedPool, 
+                                                          StringLength + sizeof(WCHAR),
+                                                          OB_NAME_TAG)))
+                {                                    
+                    /* Copy the string and null-terminate it */
+                    RtlMoveMemory(StringBuffer, LocalName.Buffer, StringLength);
+                    StringBuffer[StringLength / sizeof(WCHAR)] = UNICODE_NULL;
+                    CapturedName->Buffer = StringBuffer;
+                    DPRINT("String Captured: %wZ\n", CapturedName);
+                }
+                else
+                {
+                    /* Fail */
+                    DPRINT1("Out of Memory!\n");
+                    Status = STATUS_INSUFFICIENT_RESOURCES;
+                }
             }
-            _SEH_END;
-        }
-        
-        /* Fail if anything up to here died */
-        if (!NT_SUCCESS(Status)) 
-        {
-            DPRINT1("Probing failed\n");
-            return Status;
         }
     }
-    else
+    _SEH_EXCEPT(_SEH_ExSystemExceptionFilter)
     {
-        LocalName = *ObjectName;
-    }
-    
-    /* Make sure there really is a string */
-    DPRINT("Probing OK\n");
-    if (LocalName.Length)
-    {
-        /* Allocate a non-paged buffer for this string */
-        DPRINT("Capturing String\n");
-        CapturedName->Length = LocalName.Length;
-        CapturedName->MaximumLength = LocalName.Length + sizeof(WCHAR);
-        CapturedName->Buffer = ExAllocatePoolWithTag(NonPagedPool, 
-                                                     CapturedName->MaximumLength,
-                                                     TAG('O','b','N','m'));
-                                                     
-        /* Copy the string and null-terminate it */
-        RtlMoveMemory(CapturedName->Buffer, LocalName.Buffer, LocalName.Length);
-        CapturedName->Buffer[LocalName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-        DPRINT("String Captured: %p, %wZ\n", CapturedName, CapturedName);
+        Status = _SEH_GetExceptionCode();
+
+        /* Remember to free the buffer in case of failure */
+        DPRINT1("Failed\n");
+        if (StringBuffer) ExFreePool(StringBuffer);
     }
+    _SEH_END;
     
+    /* Return */
+    DPRINT("Returning: %lx\n", Status);
     return Status;
 }
 
@@ -123,131 +133,118 @@ ObpCaptureObjectAttributes(IN POBJECT_ATTRIBUTES ObjectAttributes,
     DPRINT("ObpCaptureObjectAttributes\n");
     RtlZeroMemory(ObjectCreateInfo, sizeof(OBJECT_CREATE_INFORMATION));
     
-    /* Check if we got Oba */
-    if (ObjectAttributes)
+    /* SEH everything here for protection */
+    _SEH_TRY
     {
-        if (AccessMode != KernelMode)
+        /* Check if we got Oba */
+        if (ObjectAttributes)
         {
-            DPRINT("Probing OBA\n");
-            _SEH_TRY
+            if (AccessMode != KernelMode)
             {
-                /* FIXME: SMSS SENDS BULLSHIT. */
-                #if 0
+                DPRINT("Probing OBA\n");
                 ProbeForRead(ObjectAttributes,
-                             sizeof(ObjectAttributes),
+                             sizeof(OBJECT_ATTRIBUTES),
                              sizeof(ULONG));
-                #endif
             }
-            _SEH_HANDLE
+        
+            /* Validate the Size and Attributes */
+            DPRINT("Validating OBA\n");
+            if ((ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES)) ||
+                (ObjectAttributes->Attributes & ~OBJ_VALID_ATTRIBUTES))
             {
-                Status = _SEH_GetExceptionCode();
+                Status = STATUS_INVALID_PARAMETER;
+                DPRINT1("Invalid Size: %lx or Attributes: %lx\n",
+                       ObjectAttributes->Length, ObjectAttributes->Attributes); 
+                _SEH_LEAVE;
             }
-            _SEH_END;
-        }
-        
-        /* Validate the Size */
-        DPRINT("Validating OBA\n");
-        if (ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES))
-        {
-            Status = STATUS_INVALID_PARAMETER;
-        }
-
-        /* Fail if SEH or Size Validation failed */
-        if(!NT_SUCCESS(Status))
-        {
-            DPRINT1("ObpCaptureObjectAttributes failed to probe object attributes\n");
-            goto fail;
-        }
         
-        /* Set some Create Info */
-        DPRINT("Creating OBCI\n");
-        ObjectCreateInfo->RootDirectory = ObjectAttributes->RootDirectory;
-        ObjectCreateInfo->Attributes = ObjectAttributes->Attributes;
-        LocalObjectName = ObjectAttributes->ObjectName;
-        SecurityDescriptor = ObjectAttributes->SecurityDescriptor;
-        SecurityQos = ObjectAttributes->SecurityQualityOfService;
+            /* Set some Create Info */
+            DPRINT("Creating OBCI\n");
+            ObjectCreateInfo->RootDirectory = ObjectAttributes->RootDirectory;
+            ObjectCreateInfo->Attributes = ObjectAttributes->Attributes;
+            LocalObjectName = ObjectAttributes->ObjectName;
+            SecurityDescriptor = ObjectAttributes->SecurityDescriptor;
+            SecurityQos = ObjectAttributes->SecurityQualityOfService;
         
-        /* Validate the SD */
-        if (SecurityDescriptor)
-        {
-            DPRINT("Probing SD: %x\n", SecurityDescriptor);
-            Status = SeCaptureSecurityDescriptor(SecurityDescriptor,
-                                                 AccessMode,
-                                                 NonPagedPool,
-                                                 TRUE,
-                                                 &ObjectCreateInfo->SecurityDescriptor);
-            if(!NT_SUCCESS(Status))
+            /* Validate the SD */
+            if (SecurityDescriptor)
             {
-                DPRINT1("Unable to capture the security descriptor!!!\n");
-                ObjectCreateInfo->SecurityDescriptor = NULL;
-                goto fail;
-            }
+                DPRINT("Probing SD: %x\n", SecurityDescriptor);
+                Status = SeCaptureSecurityDescriptor(SecurityDescriptor,
+                                                     AccessMode,
+                                                     NonPagedPool,
+                                                     TRUE,
+                                                     &ObjectCreateInfo->SecurityDescriptor);
+                if(!NT_SUCCESS(Status))
+                {
+                    DPRINT1("Unable to capture the security descriptor!!!\n");
+                    ObjectCreateInfo->SecurityDescriptor = NULL;
+                    _SEH_LEAVE;
+                }
             
-            DPRINT("Probe done\n");
-            ObjectCreateInfo->SecurityDescriptorCharge = 0; /* FIXME */
-            ObjectCreateInfo->ProbeMode = AccessMode;
-        }
+                DPRINT("Probe done\n");
+                ObjectCreateInfo->SecurityDescriptorCharge = 2048; /* FIXME */
+                ObjectCreateInfo->ProbeMode = AccessMode;
+            }
         
-        /* Validate the QoS */
-        if (SecurityQos)
-        {
-            if (AccessMode != KernelMode)
+            /* Validate the QoS */
+            if (SecurityQos)
             {
-                DPRINT("Probing QoS\n");
-                _SEH_TRY
+                if (AccessMode != KernelMode)
                 {
+                    DPRINT("Probing QoS\n");
                     ProbeForRead(SecurityQos,
                                  sizeof(SECURITY_QUALITY_OF_SERVICE),
                                  sizeof(ULONG));
                 }
-                _SEH_HANDLE
-                {
-                    Status = _SEH_GetExceptionCode();
-                }
-                _SEH_END;
-            }
-
-            if(!NT_SUCCESS(Status))
-            {
-                DPRINT1("Unable to capture QoS!!!\n");
-                goto fail;
-            }
             
-            ObjectCreateInfo->SecurityQualityOfService = *SecurityQos;
-            ObjectCreateInfo->SecurityQos = &ObjectCreateInfo->SecurityQualityOfService;
+                /* Save Info */
+                ObjectCreateInfo->SecurityQualityOfService = *SecurityQos;
+                ObjectCreateInfo->SecurityQos = &ObjectCreateInfo->SecurityQualityOfService;
+            }
+        }
+        else
+        {
+            LocalObjectName = NULL;
         }
     }
-    
-    /* Clear Local Object Name */
-    DPRINT("Clearing name\n");
-    RtlZeroMemory(ObjectName, sizeof(UNICODE_STRING));
-    
-    /* Now check if the Object Attributes had an Object Name */
-    if (LocalObjectName)
+    _SEH_EXCEPT(_SEH_ExSystemExceptionFilter)
     {
-        DPRINT("Name Buffer: %x\n", LocalObjectName->Buffer);
-        Status = ObpCaptureObjectName(ObjectName,
-                                      LocalObjectName,
-                                      AccessMode);
+        Status = _SEH_GetExceptionCode();
+        DPRINT1("Failed\n");
     }
-    else
+    _SEH_END;
+
+    if (NT_SUCCESS(Status))
     {
-        /* He can't have specified a Root Directory */
-        if (ObjectCreateInfo->RootDirectory)
+        /* Now check if the Object Attributes had an Object Name */
+        if (LocalObjectName)
+        {
+            DPRINT("Name Buffer: %wZ\n", LocalObjectName);
+            Status = ObpCaptureObjectName(ObjectName,
+                                          LocalObjectName,
+                                          AccessMode);
+        }
+        else
         {
-            DPRINT1("Invalid name\n");
-            Status = STATUS_OBJECT_NAME_INVALID;
+            /* Clear the string */
+            RtlInitUnicodeString(ObjectName, NULL);
+
+            /* He can't have specified a Root Directory */
+            if (ObjectCreateInfo->RootDirectory)
+            {
+                DPRINT1("Invalid name\n");
+                Status = STATUS_OBJECT_NAME_INVALID;
+            }
         }
     }
-    
-fail:
-    if (!NT_SUCCESS(Status))
+    else
     {
         DPRINT1("Failed to capture, cleaning up\n");
         ObpReleaseCapturedAttributes(ObjectCreateInfo);
     }
     
-    DPRINT("Return to caller\n");
+    DPRINT("Return to caller %x\n", Status);
     return Status;
 }
 
@@ -291,6 +288,7 @@ ObpReleaseCapturedAttributes(IN POBJECT_CREATE_INFORMATION ObjectCreateInfo)
  * RETURN VALUE
  */
 NTSTATUS
+NTAPI
 ObFindObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo,
             PUNICODE_STRING ObjectName,
             PVOID* ReturnedObject,
@@ -379,14 +377,14 @@ ObFindObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo,
        CurrentHeader = BODY_TO_HEADER(CurrentObject);
 
        DPRINT("Current ObjectType %wZ\n",
-              &CurrentHeader->ObjectType->Name);
+              &CurrentHeader->Type->Name);
 
-       if (CurrentHeader->ObjectType->TypeInfo.ParseProcedure == NULL)
+       if (CurrentHeader->Type->TypeInfo.ParseProcedure == NULL)
          {
             DPRINT("Current object can't parse\n");
             break;
          }
-       Status = CurrentHeader->ObjectType->TypeInfo.ParseProcedure(CurrentObject,
+       Status = CurrentHeader->Type->TypeInfo.ParseProcedure(CurrentObject,
                                                  &NextObject,
                                                  &PathString,
                                                  &current,
@@ -435,109 +433,183 @@ ObFindObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo,
  *
  * @implemented
  */
-NTSTATUS STDCALL
-ObQueryNameString (IN PVOID Object,
-                  OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
-                  IN ULONG Length,
-                  OUT PULONG ReturnLength)
+NTSTATUS 
+STDCALL
+ObQueryNameString(IN  PVOID Object,
+                  OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
+                  IN  ULONG Length,
+                  OUT PULONG ReturnLength)
 {
-  POBJECT_NAME_INFORMATION LocalInfo;
-  POBJECT_HEADER ObjectHeader;
-  ULONG LocalReturnLength;
-  NTSTATUS Status;
-
-  PAGED_CODE();
+    POBJECT_HEADER_NAME_INFO LocalInfo;
+    POBJECT_HEADER ObjectHeader;
+    PDIRECTORY_OBJECT ParentDirectory;
+    ULONG NameSize;
+    PWCH ObjectName;
+    NTSTATUS Status;
 
-  *ReturnLength = 0;
+    DPRINT("ObQueryNameString: %x, %x\n", Object, ObjectNameInfo);
 
-  if (Length < sizeof(OBJECT_NAME_INFORMATION) + sizeof(WCHAR))
-    return STATUS_INVALID_BUFFER_SIZE;
+    /* Get the Kernel Meta-Structures */
+    ObjectHeader = BODY_TO_HEADER(Object);
+    LocalInfo = HEADER_TO_OBJECT_NAME(ObjectHeader);
 
-  ObjectNameInfo->Name.MaximumLength = (USHORT)(Length - sizeof(OBJECT_NAME_INFORMATION));
-  ObjectNameInfo->Name.Length = 0;
-  ObjectNameInfo->Name.Buffer =
-    (PWCHAR)((ULONG_PTR)ObjectNameInfo + sizeof(OBJECT_NAME_INFORMATION));
-  ObjectNameInfo->Name.Buffer[0] = 0;
+    /* Check if a Query Name Procedure is available */
+    if (ObjectHeader->Type->TypeInfo.QueryNameProcedure) 
+    {  
+        /* Call the procedure */
+        DPRINT("Calling Object's Procedure\n");
+        Status = ObjectHeader->Type->TypeInfo.QueryNameProcedure(Object,
+                                                                 ObjectNameInfo,
+                                                                 Length,
+                                                                 ReturnLength);
 
-  ObjectHeader = BODY_TO_HEADER(Object);
+        /* Return the status */
+        return Status;
+    }
 
-  if (ObjectHeader->ObjectType != NULL &&
-      ObjectHeader->ObjectType->TypeInfo.QueryNameProcedure != NULL)
+    /* Check if the object doesn't even have a name */
+    if (!LocalInfo || !LocalInfo->Name.Buffer) 
     {
-      DPRINT ("Calling %x\n", ObjectHeader->ObjectType->TypeInfo.QueryNameProcedure);
-      Status = ObjectHeader->ObjectType->TypeInfo.QueryNameProcedure (Object,
-                                                   ObjectNameInfo,
-                                                   Length,
-                                                   ReturnLength);
+        /* We're returning the name structure */
+        DPRINT("Nameless Object\n");
+        *ReturnLength = sizeof(OBJECT_NAME_INFORMATION);
+
+        /* Check if we were given enough space */
+        if (*ReturnLength > Length) 
+        {
+            DPRINT1("Not enough buffer space\n");
+            return STATUS_INFO_LENGTH_MISMATCH;
+        }
+
+        /* Return an empty buffer */
+        ObjectNameInfo->Name.Length = 0;
+        ObjectNameInfo->Name.MaximumLength = 0;
+        ObjectNameInfo->Name.Buffer = NULL;
+
+        return STATUS_SUCCESS;
     }
-  else if (ObjectHeader->NameInfo->Name.Length > 0 && ObjectHeader->NameInfo->Name.Buffer != NULL)
+
+    /* 
+     * Find the size needed for the name. We won't do
+     * this during the Name Creation loop because we want
+     * to let the caller know that the buffer isn't big
+     * enough right at the beginning, not work our way through
+     * and find out at the end
+     */
+    if (Object == NameSpaceRoot) 
     {
-      DPRINT ("Object does not have a 'QueryName' function\n");
+        /* Size of the '\' string */
+        DPRINT("Object is Root\n");
+        NameSize = sizeof(UNICODE_PATH_SEP);
+    } 
+    else 
+    {
+        /* Get the Object Directory and add name of Object */
+        ParentDirectory = LocalInfo->Directory;
+        NameSize = sizeof(UNICODE_PATH_SEP) + LocalInfo->Name.Length;
 
-      if (ObjectHeader->NameInfo->Directory == NameSpaceRoot)
-       {
-         DPRINT ("Reached the root directory\n");
-         ObjectNameInfo->Name.Length = 0;
-         ObjectNameInfo->Name.Buffer[0] = 0;
-         Status = STATUS_SUCCESS;
-       }
-      else if (ObjectHeader->NameInfo->Directory != NULL)
-       {
-         LocalInfo = ExAllocatePool (NonPagedPool,
-                                     sizeof(OBJECT_NAME_INFORMATION) +
-                                     MAX_PATH * sizeof(WCHAR));
-         if (LocalInfo == NULL)
-           return STATUS_INSUFFICIENT_RESOURCES;
-
-         Status = ObQueryNameString (ObjectHeader->NameInfo->Directory,
-                                     LocalInfo,
-                                     MAX_PATH * sizeof(WCHAR),
-                                     &LocalReturnLength);
-         if (!NT_SUCCESS (Status))
-           {
-             ExFreePool (LocalInfo);
-             return Status;
-           }
-
-         Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
-                                                  &LocalInfo->Name);
-
-         ExFreePool (LocalInfo);
-
-         if (!NT_SUCCESS (Status))
-           return Status;
-       }
+        /* Loop inside the directory to get the top-most one (meaning root) */
+        while ((ParentDirectory != NameSpaceRoot) && (ParentDirectory)) 
+        {
+            /* Get the Name Information */
+            LocalInfo = HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(ParentDirectory));
 
-      DPRINT ("Object path %wZ\n", &ObjectHeader->NameInfo->Name);
-      Status = RtlAppendUnicodeToString (&ObjectNameInfo->Name,
-                                        L"\\");
-      if (!NT_SUCCESS (Status))
-       return Status;
+            /* Add the size of the Directory Name */
+            if (LocalInfo && LocalInfo->Directory) 
+            {
+                /* Size of the '\' string + Directory Name */
+                NameSize += sizeof(UNICODE_PATH_SEP) + LocalInfo->Name.Length;
 
-      Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
-                                              &ObjectHeader->NameInfo->Name);
+                /* Move to next parent Directory */
+                ParentDirectory = LocalInfo->Directory;
+            } 
+            else 
+            {
+                /* Directory with no name. We append "...\" */
+                DPRINT("Nameless Directory\n");
+                NameSize += sizeof(UNICODE_NO_PATH) + sizeof(UNICODE_PATH_SEP);
+                break;
+            }
+        }
     }
-  else
-    {
-      DPRINT ("Object is unnamed\n");
 
-      ObjectNameInfo->Name.MaximumLength = 0;
-      ObjectNameInfo->Name.Length = 0;
-      ObjectNameInfo->Name.Buffer = NULL;
+    /* Finally, add the name of the structure and the null char */
+    *ReturnLength = NameSize + sizeof(OBJECT_NAME_INFORMATION) + sizeof(UNICODE_NULL);
+    DPRINT("Final Length: %x\n", *ReturnLength);
 
-      Status = STATUS_SUCCESS;
+    /* Check if we were given enough space */
+    if (*ReturnLength > Length) 
+    {
+        DPRINT1("Not enough buffer space\n");
+        return STATUS_INFO_LENGTH_MISMATCH;
     }
 
-  if (NT_SUCCESS (Status))
+    /* 
+     * Now we will actually create the name. We work backwards because
+     * it's easier to start off from the Name we have and walk up the
+     * parent directories. We use the same logic as Name Length calculation.
+     */
+    LocalInfo = HEADER_TO_OBJECT_NAME(ObjectHeader);
+    ObjectName = (PWCH)((ULONG_PTR)ObjectNameInfo + *ReturnLength);
+    *--ObjectName = UNICODE_NULL;
+
+    if (Object == NameSpaceRoot) 
+    {
+        /* This is already the Root Directory, return "\\" */
+        DPRINT("Returning Root Dir\n");
+        *--ObjectName = UNICODE_PATH_SEP;
+        ObjectNameInfo->Name.Length = (USHORT)NameSize;
+        ObjectNameInfo->Name.MaximumLength = (USHORT)(NameSize + sizeof(UNICODE_NULL));
+        ObjectNameInfo->Name.Buffer = ObjectName;
+
+        return STATUS_SUCCESS;
+    } 
+    else 
     {
-      ObjectNameInfo->Name.MaximumLength =
-       (ObjectNameInfo->Name.Length) ? ObjectNameInfo->Name.Length + sizeof(WCHAR) : 0;
-      *ReturnLength =
-       sizeof(OBJECT_NAME_INFORMATION) + ObjectNameInfo->Name.MaximumLength;
-      DPRINT ("Returned object path: %wZ\n", &ObjectNameInfo->Name);
+        /* Start by adding the Object's Name */
+        ObjectName = (PWCH)((ULONG_PTR)ObjectName - LocalInfo->Name.Length);
+        RtlMoveMemory(ObjectName, LocalInfo->Name.Buffer, LocalInfo->Name.Length);
+
+        /* Now parse the Parent directories until we reach the top */
+        ParentDirectory = LocalInfo->Directory;
+        while ((ParentDirectory != NameSpaceRoot) && (ParentDirectory)) 
+        {
+            /* Get the name information */
+            LocalInfo = HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(ParentDirectory));
+
+            /* Add the "\" */
+            *(--ObjectName) = UNICODE_PATH_SEP;
+
+            /* Add the Parent Directory's Name */
+            if (LocalInfo && LocalInfo->Name.Buffer) 
+            {
+                /* Add the name */
+                ObjectName = (PWCH)((ULONG_PTR)ObjectName - LocalInfo->Name.Length);
+                RtlMoveMemory(ObjectName, LocalInfo->Name.Buffer, LocalInfo->Name.Length);
+
+                /* Move to next parent */
+                ParentDirectory = LocalInfo->Directory;
+            } 
+            else 
+            {
+                /* Directory without a name, we add "..." */
+                DPRINT("Nameless Directory\n");
+                ObjectName -= sizeof(UNICODE_NO_PATH);
+                ObjectName = UNICODE_NO_PATH;
+                break;
+            }
+        }
+
+        /* Add Root Directory Name */
+        *(--ObjectName) = UNICODE_PATH_SEP;
+        DPRINT("Current Buffer: %S\n", ObjectName);
+        ObjectNameInfo->Name.Length = (USHORT)NameSize;
+        ObjectNameInfo->Name.MaximumLength = (USHORT)(NameSize + sizeof(UNICODE_NULL));
+        ObjectNameInfo->Name.Buffer = ObjectName;
+        DPRINT("Complete: %wZ\n", ObjectNameInfo);
     }
 
-  return Status;
+    return STATUS_SUCCESS;
 }
 
 NTSTATUS
@@ -549,8 +621,14 @@ ObpAllocateObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo,
                   POBJECT_HEADER *ObjectHeader)
 {
     POBJECT_HEADER Header;
-    POBJECT_HEADER_NAME_INFO ObjectNameInfo;
+    BOOLEAN HasHandleInfo = FALSE;
+    BOOLEAN HasNameInfo = FALSE;
+    BOOLEAN HasCreatorInfo = FALSE;
+    POBJECT_HEADER_HANDLE_INFO HandleInfo;
+    POBJECT_HEADER_NAME_INFO NameInfo;
+    POBJECT_HEADER_CREATOR_INFO CreatorInfo;
     POOL_TYPE PoolType;
+    ULONG FinalSize = ObjectSize;
     ULONG Tag;
         
     /* If we don't have an Object Type yet, force NonPaged */
@@ -566,35 +644,104 @@ ObpAllocateObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo,
         Tag = ObjectType->Key;
     }
     
-    /* Allocate memory for the Object */
-    Header = ExAllocatePoolWithTag(PoolType, ObjectSize, Tag);
+    DPRINT("Checking ObjectName: %x\n", ObjectName);
+    /* Check if the Object has a name */
+    if (ObjectName->Buffer) 
+    {
+        FinalSize += sizeof(OBJECT_HEADER_NAME_INFO);
+        HasNameInfo = TRUE;
+    }
+    
+    if (ObjectType)
+    {
+        /* Check if the Object maintains handle counts */
+        DPRINT("Checking ObjectType->TypeInfo: %x\n", &ObjectType->TypeInfo);
+        if (ObjectType->TypeInfo.MaintainHandleCount)
+        {
+            FinalSize += sizeof(OBJECT_HEADER_HANDLE_INFO);
+            HasHandleInfo = TRUE;
+        }
+        
+        /* Check if the Object maintains type lists */
+        if (ObjectType->TypeInfo.MaintainTypeList) 
+        {
+            FinalSize += sizeof(OBJECT_HEADER_CREATOR_INFO);
+            HasCreatorInfo = TRUE;
+        }
+    }
+
+    /* Allocate memory for the Object and Header */
+    DPRINT("Allocating: %x %x\n", FinalSize, Tag);
+    Header = ExAllocatePoolWithTag(PoolType, FinalSize, Tag);
     if (!Header) {
         DPRINT1("Not enough memory!\n");
         return STATUS_INSUFFICIENT_RESOURCES;
     }
+           
+    /* Initialize Handle Info */
+    if (HasHandleInfo)
+    {
+        HandleInfo = (POBJECT_HEADER_HANDLE_INFO)Header;
+        DPRINT("Info: %x\n", HandleInfo);
+        HandleInfo->SingleEntry.HandleCount = 0;
+        Header = (POBJECT_HEADER)(HandleInfo + 1);
+    }
+       
+    /* Initialize the Object Name Info */
+    if (HasNameInfo) 
+    {
+        NameInfo = (POBJECT_HEADER_NAME_INFO)Header;
+        DPRINT("Info: %x %wZ\n", NameInfo, ObjectName);
+        NameInfo->Name = *ObjectName;
+        NameInfo->Directory = NULL;
+        Header = (POBJECT_HEADER)(NameInfo + 1);
+    }
+    
+    /* Initialize Creator Info */
+    if (HasCreatorInfo)
+    {
+        CreatorInfo = (POBJECT_HEADER_CREATOR_INFO)Header;
+        DPRINT("Info: %x\n", CreatorInfo);
+        /* FIXME: Needs Alex's Init patch
+         * CreatorInfo->CreatorUniqueProcess = PsGetCurrentProcessId();
+         */
+        InitializeListHead(&CreatorInfo->TypeList);
+        Header = (POBJECT_HEADER)(CreatorInfo + 1);
+    }
     
     /* Initialize the object header */
     RtlZeroMemory(Header, ObjectSize);
-    DPRINT("Initalizing header %p\n", Header);
+    DPRINT("Initalized header %p\n", Header);
     Header->HandleCount = 0;
-    Header->RefCount = 1;
-    Header->ObjectType = ObjectType;
+    Header->PointerCount = 1;
+    Header->Type = ObjectType;
+    Header->Flags = OB_FLAG_CREATE_INFO;
+    
+    /* Set the Offsets for the Info */
+    if (HasHandleInfo)
+    {
+        Header->HandleInfoOffset = HasNameInfo * sizeof(OBJECT_HEADER_NAME_INFO) + 
+                                   sizeof(OBJECT_HEADER_HANDLE_INFO) +
+                                   HasCreatorInfo * sizeof(OBJECT_HEADER_CREATOR_INFO);
+        Header->Flags |= OB_FLAG_SINGLE_PROCESS;
+    }
+    if (HasNameInfo)
+    {
+        Header->NameInfoOffset = sizeof(OBJECT_HEADER_NAME_INFO) + 
+                                 HasCreatorInfo * sizeof(OBJECT_HEADER_CREATOR_INFO);
+    }
+    if (HasCreatorInfo) Header->Flags |= OB_FLAG_CREATOR_INFO;
+    
     if (ObjectCreateInfo && ObjectCreateInfo->Attributes & OBJ_PERMANENT)
     {
-        Header->Permanent = TRUE;
+        Header->Flags |= OB_FLAG_PERMANENT;
     }
-    if (ObjectCreateInfo && ObjectCreateInfo->Attributes & OBJ_INHERIT)
+    if (ObjectCreateInfo && ObjectCreateInfo->Attributes & OBJ_EXCLUSIVE)
     {
-        Header->Inherit = TRUE;
+        Header->Flags |= OB_FLAG_EXCLUSIVE;
     }
-       
-    /* Initialize the Object Name Info [part of header in OB 2.0] */
-    ObjectNameInfo = ExAllocatePool(PoolType, ObjectSize);
-    ObjectNameInfo->Name = *ObjectName;
-    ObjectNameInfo->Directory = NULL;
     
     /* Link stuff to Object Header */
-    Header->NameInfo = ObjectNameInfo;
     Header->ObjectCreateInfo = ObjectCreateInfo;
     
     /* Return Header */
@@ -644,7 +791,7 @@ ObCreateObject(IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
     /* Capture all the info */
     DPRINT("Capturing Create Info\n");
     Status = ObpCaptureObjectAttributes(ObjectAttributes,
-                                        AccessMode,
+                                        ObjectAttributesAccessMode,
                                         Type,
                                         ObjectCreateInfo,
                                         &ObjectName);
@@ -663,7 +810,7 @@ ObCreateObject(IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
         {
             /* Return the Object */
             DPRINT("Returning Object\n");
-            *Object = HEADER_TO_BODY(Header);
+            *Object = &Header->Body;
             
             /* Return to caller, leave the Capture Info Alive for ObInsert */
             return Status;
@@ -707,43 +854,43 @@ ObReferenceObjectByPointer(IN PVOID Object,
 
    Header = BODY_TO_HEADER(Object);
 
-   if (ObjectType != NULL && Header->ObjectType != ObjectType)
+   if (ObjectType != NULL && Header->Type != ObjectType)
      {
        DPRINT("Failed %p (type was %x %wZ) should be %x %wZ\n",
                Header,
-               Header->ObjectType,
-               &BODY_TO_HEADER(Header->ObjectType)->NameInfo,
+               Header->Type,
+               &HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(Header->Type))->Name,
                ObjectType,
-               &BODY_TO_HEADER(ObjectType)->NameInfo);
+               &HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(ObjectType))->Name);
        return(STATUS_UNSUCCESSFUL);
      }
-   if (Header->ObjectType == PsProcessType)
+   if (Header->Type == PsProcessType)
      {
-       DPRINT("Ref p 0x%x refcount %d type %x ",
-               Object, Header->RefCount, PsProcessType);
+       DPRINT("Ref p 0x%x PointerCount %d type %x ",
+               Object, Header->PointerCount, PsProcessType);
        DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
      }
-   if (Header->ObjectType == PsThreadType)
+   if (Header->Type == PsThreadType)
      {
-       DPRINT("Deref t 0x%x with refcount %d type %x ",
-               Object, Header->RefCount, PsThreadType);
+       DPRINT("Deref t 0x%x with PointerCount %d type %x ",
+               Object, Header->PointerCount, PsThreadType);
        DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
      }
 
-   if (Header->RefCount == 0 && !Header->Permanent)
+   if (Header->PointerCount == 0 && !(Header->Flags & OB_FLAG_PERMANENT))
    {
-      if (Header->ObjectType == PsProcessType)
+      if (Header->Type == PsProcessType)
         {
          return STATUS_PROCESS_IS_TERMINATING;
        }
-      if (Header->ObjectType == PsThreadType)
+      if (Header->Type == PsThreadType)
         {
          return STATUS_THREAD_IS_TERMINATING;
        }
       return(STATUS_UNSUCCESSFUL);
    }
 
-   if (1 == InterlockedIncrement(&Header->RefCount) && !Header->Permanent)
+   if (1 == InterlockedIncrement(&Header->PointerCount) && !(Header->Flags & OB_FLAG_PERMANENT))
    {
       KEBUGCHECK(0);
    }
@@ -756,7 +903,7 @@ ObReferenceObjectByPointer(IN PVOID Object,
  * @implemented
  */
 NTSTATUS STDCALL
-ObOpenObjectByPointer(IN POBJECT Object,
+ObOpenObjectByPointer(IN PVOID Object,
                      IN ULONG HandleAttributes,
                      IN PACCESS_STATE PassedAccessState,
                      IN ACCESS_MASK DesiredAccess,
@@ -794,6 +941,11 @@ ObOpenObjectByPointer(IN POBJECT Object,
 static NTSTATUS
 ObpDeleteObject(POBJECT_HEADER Header)
 {
+  PVOID HeaderLocation = Header;
+  POBJECT_HEADER_HANDLE_INFO HandleInfo;
+  POBJECT_HEADER_NAME_INFO NameInfo;
+  POBJECT_HEADER_CREATOR_INFO CreatorInfo;
+  
   DPRINT("ObpDeleteObject(Header %p)\n", Header);
   if (KeGetCurrentIrql() != PASSIVE_LEVEL)
     {
@@ -801,10 +953,10 @@ ObpDeleteObject(POBJECT_HEADER Header)
       KEBUGCHECK(0);
     }
 
-  if (Header->ObjectType != NULL &&
-      Header->ObjectType->TypeInfo.DeleteProcedure != NULL)
+  if (Header->Type != NULL &&
+      Header->Type->TypeInfo.DeleteProcedure != NULL)
     {
-      Header->ObjectType->TypeInfo.DeleteProcedure(HEADER_TO_BODY(Header));
+      Header->Type->TypeInfo.DeleteProcedure(&Header->Body);
     }
 
   if (Header->SecurityDescriptor != NULL)
@@ -812,22 +964,35 @@ ObpDeleteObject(POBJECT_HEADER Header)
       ObpRemoveSecurityDescriptor(Header->SecurityDescriptor);
     }
     
-  if (Header->NameInfo)
+  if (HEADER_TO_OBJECT_NAME(Header))
     {
-      if(Header->NameInfo->Name.Buffer)
+      if(HEADER_TO_OBJECT_NAME(Header)->Name.Buffer)
         {
-          ExFreePool(Header->NameInfo->Name.Buffer);
+          ExFreePool(HEADER_TO_OBJECT_NAME(Header)->Name.Buffer);
         }
-      ExFreePool(Header->NameInfo);
     }
   if (Header->ObjectCreateInfo)
     {
       ObpReleaseCapturedAttributes(Header->ObjectCreateInfo);
       ExFreePool(Header->ObjectCreateInfo);
     }
+    
+  /* To find the header, walk backwards from how we allocated */
+  if ((CreatorInfo = HEADER_TO_CREATOR_INFO(Header)))
+  {
+      HeaderLocation = CreatorInfo;
+  }   
+  if ((NameInfo = HEADER_TO_OBJECT_NAME(Header)))
+  {
+      HeaderLocation = NameInfo;
+  }
+  if ((HandleInfo = HEADER_TO_HANDLE_INFO(Header)))
+  {
+      HeaderLocation = HandleInfo;
+  }
 
   DPRINT("ObPerformRetentionChecks() = Freeing object\n");
-  ExFreePool(Header);
+  ExFreePool(HeaderLocation);
 
   return(STATUS_SUCCESS);
 }
@@ -843,7 +1008,7 @@ ObpDeleteObjectWorkRoutine (IN PVOID Parameter)
   ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL); /* We need PAGED_CODE somewhere... */
 
   /* Turn this on when we have ExFreePoolWithTag
-  Tag = Params->ObjectHeader->ObjectType->Tag; */
+  Tag = Params->ObjectHeader->Type->Tag; */
   ObpDeleteObject(Params->ObjectHeader);
   ExFreePool(Params);
   /* ExFreePoolWithTag(Params, Tag); */
@@ -852,14 +1017,14 @@ ObpDeleteObjectWorkRoutine (IN PVOID Parameter)
 
 STATIC NTSTATUS
 ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader,
-                       IN LONG OldRefCount)
+                       IN LONG OldPointerCount)
 {
 #if 0
-  if (ObjectHeader->RefCount < 0)
+  if (ObjectHeader->PointerCount < 0)
     {
       CPRINT("Object %p/%p has invalid reference count (%d)\n",
             ObjectHeader, HEADER_TO_BODY(ObjectHeader),
-            ObjectHeader->RefCount);
+            ObjectHeader->PointerCount);
       KEBUGCHECK(0);
     }
 
@@ -890,7 +1055,7 @@ ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader,
        Params = (PRETENTION_CHECK_PARAMS)
          ExAllocatePoolWithTag(NonPagedPoolMustSucceed,
                                sizeof(RETENTION_CHECK_PARAMS),
-                               ObjectHeader->ObjectType->Key);
+                               ObjectHeader->Type->Key);
        Params->ObjectHeader = ObjectHeader;
        ExInitializeWorkItem(&Params->WorkItem,
                             ObpDeleteObjectWorkRoutine,
@@ -937,7 +1102,7 @@ ObfReferenceObject(IN PVOID Object)
   Header = BODY_TO_HEADER(Object);
 
   /* No one should be referencing an object once we are deleting it. */
-  if (InterlockedIncrement(&Header->RefCount) == 1 && !Header->Permanent)
+  if (InterlockedIncrement(&Header->PointerCount) == 1 && !(Header->Flags & OB_FLAG_PERMANENT))
   {
      KEBUGCHECK(0);
   }
@@ -964,28 +1129,28 @@ VOID FASTCALL
 ObfDereferenceObject(IN PVOID Object)
 {
   POBJECT_HEADER Header;
-  LONG NewRefCount;
+  LONG NewPointerCount;
   BOOL Permanent;
 
   ASSERT(Object);
 
   /* Extract the object header. */
   Header = BODY_TO_HEADER(Object);
-  Permanent = Header->Permanent;
+  Permanent = Header->Flags & OB_FLAG_PERMANENT;
 
   /*
      Drop our reference and get the new count so we can tell if this was the
      last reference.
   */
-  NewRefCount = InterlockedDecrement(&Header->RefCount);
-  DPRINT("ObfDereferenceObject(0x%x)==%d\n", Object, NewRefCount);
-  ASSERT(NewRefCount >= 0);
+  NewPointerCount = InterlockedDecrement(&Header->PointerCount);
+  DPRINT("ObfDereferenceObject(0x%x)==%d\n", Object, NewPointerCount);
+  ASSERT(NewPointerCount >= 0);
 
   /* Check whether the object can now be deleted. */
-  if (NewRefCount == 0 &&
+  if (NewPointerCount == 0 &&
       !Permanent)
     {
-      ObpDeleteObjectDpcLevel(Header, NewRefCount);
+      ObpDeleteObjectDpcLevel(Header, NewPointerCount);
     }
 }
 
@@ -1065,7 +1230,7 @@ ObGetObjectPointerCount(PVOID Object)
   ASSERT(Object);
   Header = BODY_TO_HEADER(Object);
 
-  return Header->RefCount;
+  return Header->PointerCount;
 }
 
 
@@ -1083,6 +1248,7 @@ ObGetObjectPointerCount(PVOID Object)
  *     Reference count.
  */
 ULONG
+NTAPI
 ObGetObjectHandleCount(PVOID Object)
 {
   POBJECT_HEADER Header;