2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel Streaming
4 * FILE: drivers/ksfilter/ks/allocators.c
5 * PURPOSE: KS Allocator functions
6 * PROGRAMMER: Johannes Anderwald
12 #include <ndk/ldrfuncs.h>
25 IN POOL_TYPE PoolType
,
26 IN ULONG_PTR ResourceName
,
27 IN ULONG ResourceType
,
29 OUT PULONG ResourceSize
)
32 LDR_RESOURCE_INFO ResourceInfo
;
33 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry
;
36 PVOID _SEH2_VOLATILE Result
= NULL
;
38 /* set up resource info */
39 ResourceInfo
.Type
= ResourceType
;
40 ResourceInfo
.Name
= ResourceName
;
41 ResourceInfo
.Language
= 0;
45 /* find the resource */
46 Status
= LdrFindResource_U(ImageBase
, &ResourceInfo
, RESOURCE_DATA_LEVEL
, &ResourceDataEntry
);
47 if (NT_SUCCESS(Status
))
49 /* try accessing it */
50 Status
= LdrAccessResource(ImageBase
, ResourceDataEntry
, &Data
, &Size
);
51 if (NT_SUCCESS(Status
))
53 /* allocate resource buffer */
54 Result
= AllocateItem(PoolType
, Size
);
58 RtlMoveMemory(Result
, Data
, Size
);
64 /* resource size is optional */
70 /* not enough memory */
71 Status
= STATUS_INSUFFICIENT_RESOURCES
;
76 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
78 /* Exception, get the error code */
79 Status
= _SEH2_GetExceptionCode();
83 if (!NT_SUCCESS(Status
))
88 /* free resource buffer in case of a failure */
102 IN OUT PULONG BufferLength
,
106 return STATUS_NOT_IMPLEMENTED
;
115 KsGetImageNameAndResourceId(
117 OUT PUNICODE_STRING ImageName
,
118 OUT PULONG_PTR ResourceId
,
119 OUT PULONG ValueType
)
123 WCHAR ImagePath
[] = {L
"\\SystemRoot\\system32\\drivers\\"};
125 /* first clear the provided ImageName */
126 ImageName
->Buffer
= NULL
;
127 ImageName
->Length
= ImageName
->MaximumLength
= 0;
130 /* retrieve length of image name */
131 Status
= KspQueryRegValue(RegKey
, L
"Image", NULL
, &ImageLength
, NULL
);
133 if (Status
!= STATUS_BUFFER_OVERFLOW
)
135 /* key value doesnt exist */
139 /* allocate image name buffer */
140 ImageName
->MaximumLength
= (USHORT
)(sizeof(ImagePath
) + ImageLength
);
141 ImageName
->Buffer
= AllocateItem(PagedPool
, ImageName
->MaximumLength
);
143 /* check for success */
144 if (!ImageName
->Buffer
)
146 /* insufficient memory */
147 return STATUS_INSUFFICIENT_RESOURCES
;
150 /* copy image name */
151 RtlCopyMemory(ImageName
->Buffer
, ImagePath
, sizeof(ImagePath
));
153 /* retrieve image name */
154 Status
= KspQueryRegValue(RegKey
,
156 &ImageName
->Buffer
[sizeof(ImagePath
) / sizeof(WCHAR
)],
160 if (!NT_SUCCESS(Status
))
162 /* unexpected error */
163 FreeItem(ImageName
->Buffer
);
167 /* now query for resource id length*/
169 Status
= KspQueryRegValue(RegKey
, L
"ResourceId", NULL
, &ImageLength
, ValueType
);
171 /* allocate resource id buffer*/
172 *ResourceId
= (ULONG_PTR
)AllocateItem(PagedPool
, ImageLength
);
174 /* check for success */
177 /* insufficient memory */
178 FreeItem(ImageName
->Buffer
);
179 return STATUS_INSUFFICIENT_RESOURCES
;
181 /* now query for resource id */
182 Status
= KspQueryRegValue(RegKey
, L
"ResourceId", (PVOID
)*ResourceId
, &ImageLength
, ValueType
);
184 if (!NT_SUCCESS(Status
))
186 /* unexpected error */
187 FreeItem(ImageName
->Buffer
);
188 FreeItem((PVOID
)*ResourceId
);
202 IN PDEVICE_OBJECT PhysicalDeviceObject
,
203 IN PUNICODE_STRING ModuleName
,
204 OUT PUNICODE_STRING ImageName
,
205 OUT PULONG_PTR ResourceId
,
206 OUT PULONG ValueType
)
209 UNICODE_STRING SubKeyName
;
210 UNICODE_STRING Modules
= RTL_CONSTANT_STRING(L
"Modules\\");
211 HANDLE hKey
, hSubKey
;
212 OBJECT_ATTRIBUTES ObjectAttributes
;
214 /* first open device key */
215 Status
= IoOpenDeviceRegistryKey(PhysicalDeviceObject
, PLUGPLAY_REGKEY_DEVICE
, GENERIC_READ
, &hKey
);
217 /* check for success */
218 if (!NT_SUCCESS(Status
))
220 /* invalid parameter */
221 return STATUS_INVALID_PARAMETER
;
224 /* initialize subkey buffer */
225 SubKeyName
.Length
= 0;
226 SubKeyName
.MaximumLength
= Modules
.MaximumLength
+ ModuleName
->MaximumLength
;
227 SubKeyName
.Buffer
= AllocateItem(PagedPool
, SubKeyName
.MaximumLength
);
229 /* check for success */
230 if (!SubKeyName
.Buffer
)
232 /* not enough memory */
234 return STATUS_NO_MEMORY
;
237 /* build subkey string */
238 RtlAppendUnicodeStringToString(&SubKeyName
, &Modules
);
239 RtlAppendUnicodeStringToString(&SubKeyName
, ModuleName
);
241 /* initialize subkey attributes */
242 InitializeObjectAttributes(&ObjectAttributes
, &SubKeyName
, OBJ_CASE_INSENSITIVE
, hKey
, NULL
);
244 /* now open the subkey */
245 Status
= ZwOpenKey(&hSubKey
, GENERIC_READ
, &ObjectAttributes
);
247 /* check for success */
248 if (NT_SUCCESS(Status
))
251 Status
= KsGetImageNameAndResourceId(hSubKey
, ImageName
, ResourceId
, ValueType
);
257 /* free subkey string */
258 FreeItem(SubKeyName
.Buffer
);
260 /* close device key */