xref: /reactos/drivers/ksfilter/ks/misc.c (revision 9d3c3a75)
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
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 
26     IoCompleteRequest(Irp, PriorityBoost);
27 }
28 
29 PVOID
30 AllocateItem(
31     IN POOL_TYPE PoolType,
32     IN SIZE_T NumberOfBytes)
33 {
34     PVOID Item = ExAllocatePoolWithTag(PoolType, NumberOfBytes, TAG_KS);
35     if (!Item)
36         return Item;
37 
38     RtlZeroMemory(Item, NumberOfBytes);
39     return Item;
40 }
41 
42 VOID
43 FreeItem(
44     IN PVOID Item)
45 {
46     ExFreePool(Item);
47 }
48 
49 NTSTATUS
50 KspCopyCreateRequest(
51     IN PIRP Irp,
52     IN LPWSTR ObjectClass,
53     IN OUT PULONG Size,
54     OUT PVOID * Result)
55 {
56     PIO_STACK_LOCATION IoStack;
57     SIZE_T ObjectLength, ParametersLength;
58     PVOID Buffer;
59 
60     /* get current irp stack */
61     IoStack = IoGetCurrentIrpStackLocation(Irp);
62 
63     /* get object class length */
64     ObjectLength = (wcslen(ObjectClass) + 1) * sizeof(WCHAR);
65 
66     /* check for minium length requirement */
67     if (ObjectLength  + *Size > IoStack->FileObject->FileName.MaximumLength)
68         return STATUS_UNSUCCESSFUL;
69 
70     /* extract parameters length */
71     ParametersLength = IoStack->FileObject->FileName.MaximumLength - ObjectLength;
72 
73     /* allocate buffer */
74     Buffer = AllocateItem(NonPagedPool, ParametersLength);
75     if (!Buffer)
76         return STATUS_INSUFFICIENT_RESOURCES;
77 
78     /* copy parameters */
79     RtlMoveMemory(Buffer, &IoStack->FileObject->FileName.Buffer[ObjectLength / sizeof(WCHAR)], ParametersLength);
80 
81     /* store result */
82     *Result = Buffer;
83     *Size = (ULONG)ParametersLength;
84 
85     return STATUS_SUCCESS;
86 }
87 
88 /*
89     @implemented
90 */
91 KSDDKAPI
92 PVOID
93 NTAPI
94 KsGetObjectFromFileObject(
95     IN PFILE_OBJECT FileObject)
96 {
97     PKSIOBJECT_HEADER ObjectHeader;
98 
99     /* get object header */
100     ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext2;
101 
102     /* return associated object */
103     return ObjectHeader->ObjectType;
104 }
105 
106 /*
107     @implemented
108 */
109 KSDDKAPI
110 KSOBJECTTYPE
111 NTAPI
112 KsGetObjectTypeFromFileObject(
113     IN PFILE_OBJECT FileObject)
114 {
115     PKSIOBJECT_HEADER ObjectHeader;
116 
117     /* get object header */
118     ObjectHeader = (PKSIOBJECT_HEADER)FileObject->FsContext2;
119     /* return type */
120     return ObjectHeader->Type;
121 }
122 
123 /*
124     @implemented
125 */
126 KSOBJECTTYPE
127 NTAPI
128 KsGetObjectTypeFromIrp(
129     IN PIRP  Irp)
130 {
131     PKSIOBJECT_HEADER ObjectHeader;
132     PIO_STACK_LOCATION IoStack;
133 
134     /* get current irp stack */
135     IoStack = IoGetCurrentIrpStackLocation(Irp);
136     /* get object header */
137     ObjectHeader = (PKSIOBJECT_HEADER)IoStack->FileObject->FsContext2;
138     /* return type */
139     return ObjectHeader->Type;
140 }
141 
142 /*
143     @implemented
144 */
145 PUNKNOWN
146 NTAPI
147 KsGetOuterUnknown(
148     IN PVOID  Object)
149 {
150     PKSBASIC_HEADER BasicHeader = (PKSBASIC_HEADER)((ULONG_PTR)Object - sizeof(KSBASIC_HEADER));
151 
152     /* sanity check */
153     ASSERT(BasicHeader->Type == KsObjectTypeDevice || BasicHeader->Type == KsObjectTypeFilterFactory ||
154            BasicHeader->Type == KsObjectTypeFilter || BasicHeader->Type == KsObjectTypePin);
155 
156     /* return objects outer unknown */
157     return BasicHeader->OuterUnknown;
158 }
159 
160 /*
161     @implemented
162 */
163 KSDDKAPI
164 PVOID
165 NTAPI
166 KsGetParent(
167     IN PVOID Object)
168 {
169     PKSBASIC_HEADER BasicHeader = (PKSBASIC_HEADER)((ULONG_PTR)Object - sizeof(KSBASIC_HEADER));
170     /* sanity check */
171     ASSERT(BasicHeader->Parent.KsDevice != NULL);
172     /* return object type */
173     return (PVOID)BasicHeader->Parent.KsDevice;
174 }
175 
176 
177