Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[reactos.git] / drivers / ksfilter / ks / api.c
index 3a037bf..5b50fcf 100644 (file)
@@ -68,18 +68,17 @@ KsAcquireDeviceSecurityLock(
     IN KSDEVICE_HEADER DevHeader,
     IN BOOLEAN Exclusive)
 {
-    NTSTATUS Status;
     PKSIDEVICE_HEADER Header = (PKSIDEVICE_HEADER)DevHeader;
 
     KeEnterCriticalRegion();
 
     if (Exclusive)
     {
-        Status = ExAcquireResourceExclusiveLite(&Header->SecurityLock, TRUE);
+        ExAcquireResourceExclusiveLite(&Header->SecurityLock, TRUE);
     }
     else
     {
-        Status = ExAcquireResourceSharedLite(&Header->SecurityLock, TRUE);
+        ExAcquireResourceSharedLite(&Header->SecurityLock, TRUE);
     }
 }
 
@@ -169,12 +168,12 @@ KsDefaultDispatchPower(
     PDEVICE_EXTENSION DeviceExtension;
     PKSIDEVICE_HEADER DeviceHeader;
     PKSIOBJECT_HEADER ObjectHeader;
-    PIO_STACK_LOCATION IoStack;
+    //PIO_STACK_LOCATION IoStack;
     PLIST_ENTRY ListEntry;
     NTSTATUS Status;
 
     /* get current irp stack */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
+    //IoStack = IoGetCurrentIrpStackLocation(Irp);
 
     /* caller wants to add the target device */
     DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
@@ -229,11 +228,11 @@ KsDefaultForwardIrp(
 {
     PDEVICE_EXTENSION DeviceExtension;
     PKSIDEVICE_HEADER DeviceHeader;
-    PIO_STACK_LOCATION IoStack;
+    //PIO_STACK_LOCATION IoStack;
     NTSTATUS Status;
 
     /* get current irp stack */
-    IoStack = IoGetCurrentIrpStackLocation(Irp);
+    //IoStack = IoGetCurrentIrpStackLocation(Irp);
 
     /* caller wants to add the target device */
     DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
@@ -614,10 +613,10 @@ KsAllocateObjectHeader(
     IN  KSDISPATCH_TABLE* Table)
 {
     PIO_STACK_LOCATION IoStack;
-    PDEVICE_EXTENSION DeviceExtension;
-    PKSIDEVICE_HEADER DeviceHeader;
+    //PDEVICE_EXTENSION DeviceExtension;
+    //PKSIDEVICE_HEADER DeviceHeader;
     PKSIOBJECT_HEADER ObjectHeader;
-    PKSOBJECT_CREATE_ITEM CreateItem;
+    //PKSOBJECT_CREATE_ITEM CreateItem;
     NTSTATUS Status;
 
     if (!Header)
@@ -632,9 +631,9 @@ KsAllocateObjectHeader(
     /* get current stack location */
     IoStack = IoGetCurrentIrpStackLocation(Irp);
     /* get device extension */
-    DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
+    //DeviceExtension = (PDEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
     /* get device header */
-    DeviceHeader = DeviceExtension->DeviceHeader;
+    //DeviceHeader = DeviceExtension->DeviceHeader;
 
     /* sanity check */
     ASSERT(IoStack->FileObject);
@@ -652,7 +651,7 @@ KsAllocateObjectHeader(
     InitializeListHead(&ObjectHeader->ItemList);
 
     /* get create item */
-    CreateItem = KSCREATE_ITEM_IRP_STORAGE(Irp);
+    //CreateItem = KSCREATE_ITEM_IRP_STORAGE(Irp);
 
     /* do we have a name */
     if (IoStack->FileObject->FileName.Buffer)
@@ -2061,7 +2060,7 @@ KspCopyMethodSets(
     /* set counter */
     Count = AutomationTableA->MethodSetsCount;
 
-    /* now copy entries which arent available in the dominant table */
+    /* now copy entries which aren't available in the dominant table */
     for(Index = 0; Index < AutomationTableB->MethodSetsCount; Index++)
     {
         /* set found to false */
@@ -2088,15 +2087,100 @@ KspCopyMethodSets(
     return STATUS_SUCCESS;
 }
 
+VOID
+KspAddPropertyItem(
+    OUT PKSPROPERTY_SET OutPropertySet,
+    IN PKSPROPERTY_ITEM PropertyItem,
+    IN ULONG PropertyItemSize)
+{
+    PKSPROPERTY_ITEM CurrentPropertyItem;
+    ULONG Index;
+
+    // check if the property item is already present
+    CurrentPropertyItem = (PKSPROPERTY_ITEM)OutPropertySet->PropertyItem;
+    for(Index = 0; Index < OutPropertySet->PropertiesCount; Index++)
+    {
+        if (CurrentPropertyItem->PropertyId == PropertyItem->PropertyId)
+        {
+            // item already present
+            return;
+        }
+
+        // next item
+        CurrentPropertyItem = (PKSPROPERTY_ITEM)((ULONG_PTR)CurrentPropertyItem + PropertyItemSize);
+    }
+    // add item
+    RtlCopyMemory(CurrentPropertyItem, PropertyItem, PropertyItemSize);
+    OutPropertySet->PropertiesCount++;
+}
+
+NTSTATUS
+KspMergePropertySet(
+    OUT PKSAUTOMATION_TABLE  Table,
+    OUT PKSPROPERTY_SET OutPropertySet,
+    IN PKSPROPERTY_SET PropertySetA,
+    IN PKSPROPERTY_SET PropertySetB,
+    IN KSOBJECT_BAG  Bag OPTIONAL)
+{
+    ULONG PropertyCount, Index;
+    PKSPROPERTY_ITEM PropertyItem, CurrentPropertyItem;
+    NTSTATUS Status;
+
+    // max properties 
+    PropertyCount = PropertySetA->PropertiesCount + PropertySetB->PropertiesCount;
+
+    // allocate items
+    PropertyItem = AllocateItem(NonPagedPool, Table->PropertyItemSize * PropertyCount);
+    if (!PropertyItem)
+        return STATUS_INSUFFICIENT_RESOURCES;
+
+    if (Bag)
+    {
+        /* add table to object bag */
+        Status = KsAddItemToObjectBag(Bag, PropertyItem, NULL);
+        /* check for success */
+        if (!NT_SUCCESS(Status))
+        {
+            /* free table */
+            FreeItem(Table);
+            return Status;
+        }
+    }
+
+    // copy entries from dominant table
+    RtlCopyMemory(PropertyItem, PropertySetA->PropertyItem, Table->PropertyItemSize * PropertySetA->PropertiesCount);
+
+    // init property set
+    OutPropertySet->PropertiesCount = PropertySetA->PropertiesCount;
+    OutPropertySet->PropertyItem = PropertyItem;
+
+    // copy other entries
+    CurrentPropertyItem = (PKSPROPERTY_ITEM)PropertySetB->PropertyItem;
+    for(Index = 0; Index < PropertySetB->PropertiesCount; Index++)
+    {
+
+        // add entries
+        KspAddPropertyItem(OutPropertySet, CurrentPropertyItem, Table->PropertyItemSize);
+
+        // next entry
+        CurrentPropertyItem = (PKSPROPERTY_ITEM)((ULONG_PTR)CurrentPropertyItem + Table->PropertyItemSize);
+    }
+
+    // done
+    return STATUS_SUCCESS;
+}
+
 
 NTSTATUS
 KspCopyPropertySets(
     OUT PKSAUTOMATION_TABLE  Table,
     IN PKSAUTOMATION_TABLE  AutomationTableA OPTIONAL,
-    IN PKSAUTOMATION_TABLE  AutomationTableB OPTIONAL)
+    IN PKSAUTOMATION_TABLE  AutomationTableB OPTIONAL,
+    IN KSOBJECT_BAG  Bag OPTIONAL)
 {
     ULONG Index, SubIndex, Count;
     BOOL bFound;
+    NTSTATUS Status;
 
     if (!AutomationTableA)
     {
@@ -2116,7 +2200,7 @@ KspCopyPropertySets(
     /* set counter */
     Count = AutomationTableA->PropertySetsCount;
 
-    /* now copy entries which arent available in the dominant table */
+    /* now copy entries which aren't available in the dominant table */
     for(Index = 0; Index < AutomationTableB->PropertySetsCount; Index++)
     {
         /* set found to false */
@@ -2138,6 +2222,17 @@ KspCopyPropertySets(
             RtlMoveMemory((PVOID)&Table->PropertySets[Count], &AutomationTableB->PropertySets[Index], sizeof(KSPROPERTY_SET));
             Count++;
         }
+        else
+        {
+            // merge property sets
+            Status = KspMergePropertySet(Table, (PKSPROPERTY_SET)&Table->PropertySets[SubIndex], (PKSPROPERTY_SET)&AutomationTableA->PropertySets[SubIndex], (PKSPROPERTY_SET)&AutomationTableB->PropertySets[Index], Bag);
+            if (!NT_SUCCESS(Status))
+            {
+                // failed to merge
+                DPRINT1("[KS] Failed to merge %x\n", Status);
+                return Status;
+            }
+        }
     }
 
     return STATUS_SUCCESS;
@@ -2170,7 +2265,7 @@ KspCopyEventSets(
     /* set counter */
     Count = AutomationTableA->EventSetsCount;
 
-    /* now copy entries which arent available in the dominant table */
+    /* now copy entries which aren't available in the dominant table */
     for(Index = 0; Index < AutomationTableB->EventSetsCount; Index++)
     {
         /* set found to false */
@@ -2252,6 +2347,12 @@ KsMergeAutomationTables(
             Table->PropertyItemSize = AutomationTableB->PropertyItemSize;
         }
 
+        if (AutomationTableA && AutomationTableB)
+        {
+            // FIXME handle different propery item sizes
+            ASSERT(AutomationTableA->PropertyItemSize == AutomationTableB->PropertyItemSize);
+        }
+
         /* now allocate the property sets */
         Table->PropertySets = AllocateItem(NonPagedPool, sizeof(KSPROPERTY_SET) * Table->PropertySetsCount);
 
@@ -2273,7 +2374,7 @@ KsMergeAutomationTables(
             }
         }
         /* now copy the property sets */
-        Status = KspCopyPropertySets(Table, AutomationTableA, AutomationTableB);
+        Status = KspCopyPropertySets(Table, AutomationTableA, AutomationTableB, Bag);
         if(!NT_SUCCESS(Status))
             goto cleanup;