xref: /reactos/drivers/ksfilter/ks/misc.c (revision c03b2ed9)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Kernel Streaming
4  * FILE:            drivers/ksfilter/ks/misc.c
5  * PURPOSE:         KS Allocator functions
6  * PROGRAMMER:      Johannes Anderwald
7  */
8 
9 #include "precomp.h"
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 #define TAG_KS 'ssKK'
15 
16 VOID
CompleteRequest(PIRP Irp,CCHAR PriorityBoost)17 CompleteRequest(
18     PIRP Irp,
19     CCHAR PriorityBoost)
20 {
21     DPRINT("Completing IRP %p Status %x\n", Irp, Irp->IoStatus.Status);
22 
23     ASSERT(Irp->IoStatus.Status != STATUS_PENDING);
24 
25     IoCompleteRequest(Irp, PriorityBoost);
26 }
27 
28 PVOID
AllocateItem(IN POOL_TYPE PoolType,IN SIZE_T NumberOfBytes)29 AllocateItem(
30     IN POOL_TYPE PoolType,
31     IN SIZE_T NumberOfBytes)
32 {
33     return ExAllocatePoolZero(PoolType, NumberOfBytes, TAG_KS);
34 }
35 
36 VOID
FreeItem(IN PVOID Item)37 FreeItem(
38     IN PVOID Item)
39 {
40     ExFreePoolWithTag(Item, TAG_KS);
41 }
42 
43 NTSTATUS
KspCopyCreateRequest(IN PIRP Irp,IN LPWSTR ObjectClass,IN OUT PULONG Size,OUT PVOID * Result)44 KspCopyCreateRequest(
45     IN PIRP Irp,
46     IN LPWSTR ObjectClass,
47     IN OUT PULONG Size,
48     OUT PVOID * Result)
49 {
50     PIO_STACK_LOCATION IoStack;
51     SIZE_T ObjectLength, ParametersLength;
52     PVOID Buffer;
53 
54     /* get current irp stack */
55     IoStack = IoGetCurrentIrpStackLocation(Irp);
56 
57     /* get object class length */
58     ObjectLength = (wcslen(ObjectClass) + 1) * sizeof(WCHAR);
59 
60     /* check for minium length requirement */
61     if (ObjectLength  + *Size > IoStack->FileObject->FileName.MaximumLength)
62         return STATUS_UNSUCCESSFUL;
63 
64     /* extract parameters length */
65     ParametersLength = IoStack->FileObject->FileName.MaximumLength - ObjectLength;
66 
67     /* allocate buffer */
68     Buffer = AllocateItem(NonPagedPool, ParametersLength);
69     if (!Buffer)
70         return STATUS_INSUFFICIENT_RESOURCES;
71 
72     /* copy parameters */
73     RtlMoveMemory(Buffer, &IoStack->FileObject->FileName.Buffer[ObjectLength / sizeof(WCHAR)], ParametersLength);
74 
75     /* store result */
76     *Result = Buffer;
77     *Size = (ULONG)ParametersLength;
78 
79     return STATUS_SUCCESS;
80 }
81 
82 /*
83     @implemented
84 */
85 KSDDKAPI
86 PVOID
87 NTAPI
KsGetObjectFromFileObject(IN PFILE_OBJECT FileObject)88 KsGetObjectFromFileObject(
89     IN PFILE_OBJECT FileObject)
90 {
91     PKSIOBJECT_HEADER ObjectHeader;
92 
93     /* get object header */
94     ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext2;
95 
96     /* return associated object */
97     return ObjectHeader->ObjectType;
98 }
99 
100 /*
101     @implemented
102 */
103 KSDDKAPI
104 KSOBJECTTYPE
105 NTAPI
KsGetObjectTypeFromFileObject(IN PFILE_OBJECT FileObject)106 KsGetObjectTypeFromFileObject(
107     IN PFILE_OBJECT FileObject)
108 {
109     PKSIOBJECT_HEADER ObjectHeader;
110 
111     /* get object header */
112     ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext2;
113     /* return type */
114     return ObjectHeader->Type;
115 }
116 
117 /*
118     @implemented
119 */
120 KSOBJECTTYPE
121 NTAPI
KsGetObjectTypeFromIrp(IN PIRP Irp)122 KsGetObjectTypeFromIrp(
123     IN PIRP  Irp)
124 {
125     PKSIOBJECT_HEADER ObjectHeader;
126     PIO_STACK_LOCATION IoStack;
127 
128     /* get current irp stack */
129     IoStack = IoGetCurrentIrpStackLocation(Irp);
130     /* get object header */
131     ObjectHeader = (PKSIOBJECT_HEADER)IoStack->FileObject->FsContext2;
132     /* return type */
133     return ObjectHeader->Type;
134 }
135 
136 /*
137     @implemented
138 */
139 PUNKNOWN
140 NTAPI
KsGetOuterUnknown(IN PVOID Object)141 KsGetOuterUnknown(
142     IN PVOID  Object)
143 {
144     PKSBASIC_HEADER BasicHeader = (PKSBASIC_HEADER)((ULONG_PTR)Object - sizeof(KSBASIC_HEADER));
145 
146     /* sanity check */
147     ASSERT(BasicHeader->Type == KsObjectTypeDevice || BasicHeader->Type == KsObjectTypeFilterFactory ||
148            BasicHeader->Type == KsObjectTypeFilter || BasicHeader->Type == KsObjectTypePin);
149 
150     /* return objects outer unknown */
151     return BasicHeader->OuterUnknown;
152 }
153 
154 /*
155     @implemented
156 */
157 KSDDKAPI
158 PVOID
159 NTAPI
KsGetParent(IN PVOID Object)160 KsGetParent(
161     IN PVOID Object)
162 {
163     PKSBASIC_HEADER BasicHeader = (PKSBASIC_HEADER)((ULONG_PTR)Object - sizeof(KSBASIC_HEADER));
164     /* sanity check */
165     ASSERT(BasicHeader->Parent.KsDevice != NULL);
166     /* return object type */
167     return (PVOID)BasicHeader->Parent.KsDevice;
168 }
169