ReactOS 0.4.15-dev-7788-g1ad9096
image.c
Go to the documentation of this file.
1/*
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
7 */
8
9#include "precomp.h"
10
11#include <ntimage.h>
12#include <ndk/ldrfuncs.h>
13
14#define NDEBUG
15#include <debug.h>
16
17/*
18 @implemented
19*/
24 IN PVOID ImageBase,
26 IN ULONG_PTR ResourceName,
27 IN ULONG ResourceType,
29 OUT PULONG ResourceSize)
30{
32 LDR_RESOURCE_INFO ResourceInfo;
33 PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry;
34 PVOID Data;
35 ULONG Size;
37
38 /* set up resource info */
39 ResourceInfo.Type = ResourceType;
40 ResourceInfo.Name = ResourceName;
41 ResourceInfo.Language = 0;
42
44 {
45 /* find the resource */
46 Status = LdrFindResource_U(ImageBase, &ResourceInfo, RESOURCE_DATA_LEVEL, &ResourceDataEntry);
47 if (NT_SUCCESS(Status))
48 {
49 /* try accessing it */
50 Status = LdrAccessResource(ImageBase, ResourceDataEntry, &Data, &Size);
51 if (NT_SUCCESS(Status))
52 {
53 /* allocate resource buffer */
55 if (Result)
56 {
57 /* copy resource */
59 /* store result */
61
62 if (ResourceSize)
63 {
64 /* resource size is optional */
65 *ResourceSize = Size;
66 }
67 }
68 else
69 {
70 /* not enough memory */
72 }
73 }
74 }
75 }
77 {
78 /* Exception, get the error code */
80 }
82
83 if (!NT_SUCCESS(Status))
84 {
85 /* failed */
86 if (Result)
87 {
88 /* free resource buffer in case of a failure */
90 }
91 }
92 /* done */
93 return Status;
94}
95
96
104{
107}
108
109/*
110 @implemented
111*/
114NTAPI
116 IN HANDLE RegKey,
120{
122 ULONG ImageLength;
123 WCHAR ImagePath[] = {L"\\SystemRoot\\system32\\drivers\\"};
124
125 /* first clear the provided ImageName */
126 ImageName->Buffer = NULL;
127 ImageName->Length = ImageName->MaximumLength = 0;
128
129 ImageLength = 0;
130 /* retrieve length of image name */
131 Status = KspQueryRegValue(RegKey, L"Image", NULL, &ImageLength, NULL);
132
134 {
135 /* key value doesnt exist */
136 return Status;
137 }
138
139 /* allocate image name buffer */
140 ImageName->MaximumLength = (USHORT)(sizeof(ImagePath) + ImageLength);
141 ImageName->Buffer = AllocateItem(PagedPool, ImageName->MaximumLength);
142
143 /* check for success */
144 if (!ImageName->Buffer)
145 {
146 /* insufficient memory */
148 }
149
150 /* copy image name */
151 RtlCopyMemory(ImageName->Buffer, ImagePath, sizeof(ImagePath));
152
153 /* retrieve image name */
154 Status = KspQueryRegValue(RegKey,
155 L"Image",
156 &ImageName->Buffer[sizeof(ImagePath) / sizeof(WCHAR)],
157 &ImageLength,
158 NULL);
159
160 if (!NT_SUCCESS(Status))
161 {
162 /* unexpected error */
163 FreeItem(ImageName->Buffer);
164 return Status;
165 }
166
167 /* now query for resource id length*/
168 ImageLength = 0;
169 Status = KspQueryRegValue(RegKey, L"ResourceId", NULL, &ImageLength, ValueType);
170
171 /* allocate resource id buffer*/
172 *ResourceId = (ULONG_PTR)AllocateItem(PagedPool, ImageLength);
173
174 /* check for success */
175 if (!*ResourceId)
176 {
177 /* insufficient memory */
178 FreeItem(ImageName->Buffer);
180 }
181 /* now query for resource id */
182 Status = KspQueryRegValue(RegKey, L"ResourceId", (PVOID)*ResourceId, &ImageLength, ValueType);
183
184 if (!NT_SUCCESS(Status))
185 {
186 /* unexpected error */
187 FreeItem(ImageName->Buffer);
189 }
190
191 /* return result */
192 return Status;
193}
194
195/*
196 @implemented
197*/
200NTAPI
207{
210 UNICODE_STRING Modules = RTL_CONSTANT_STRING(L"Modules\\");
211 HANDLE hKey, hSubKey;
213
214 /* first open device key */
216
217 /* check for success */
218 if (!NT_SUCCESS(Status))
219 {
220 /* invalid parameter */
222 }
223
224 /* initialize subkey buffer */
225 SubKeyName.Length = 0;
226 SubKeyName.MaximumLength = Modules.MaximumLength + ModuleName->MaximumLength;
227 SubKeyName.Buffer = AllocateItem(PagedPool, SubKeyName.MaximumLength);
228
229 /* check for success */
230 if (!SubKeyName.Buffer)
231 {
232 /* not enough memory */
233 ZwClose(hKey);
234 return STATUS_NO_MEMORY;
235 }
236
237 /* build subkey string */
240
241 /* initialize subkey attributes */
243
244 /* now open the subkey */
245 Status = ZwOpenKey(&hSubKey, GENERIC_READ, &ObjectAttributes);
246
247 /* check for success */
248 if (NT_SUCCESS(Status))
249 {
250 /* defer work */
252
253 /* close subkey */
254 ZwClose(hSubKey);
255 }
256
257 /* free subkey string */
258 FreeItem(SubKeyName.Buffer);
259
260 /* close device key */
261 ZwClose(hKey);
262
263 /* return status */
264 return Status;
265}
INT ResourceId
Type
Definition: Type.h:7
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char * ModuleName
Definition: acpixf.h:1280
LONG NTSTATUS
Definition: precomp.h:26
#define UNIMPLEMENTED
Definition: debug.h:115
PDEVICE_OBJECT PhysicalDeviceObject
Definition: btrfs_drv.h:1157
_Acquires_exclusive_lock_ Resource _Acquires_shared_lock_ Resource _Inout_ PERESOURCE Resource
Definition: cdprocs.h:843
Definition: bufpool.h:45
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define GENERIC_READ
Definition: compat.h:135
KSDDKAPI NTSTATUS NTAPI KsMapModuleName(IN PDEVICE_OBJECT PhysicalDeviceObject, IN PUNICODE_STRING ModuleName, OUT PUNICODE_STRING ImageName, OUT PULONG_PTR ResourceId, OUT PULONG ValueType)
Definition: image.c:201
KSDDKAPI NTSTATUS NTAPI KsLoadResource(IN PVOID ImageBase, IN POOL_TYPE PoolType, IN ULONG_PTR ResourceName, IN ULONG ResourceType, OUT PVOID *Resource, OUT PULONG ResourceSize)
Definition: image.c:23
NTSTATUS KspQueryRegValue(IN HANDLE KeyHandle, IN LPWSTR KeyName, IN PVOID Buffer, IN OUT PULONG BufferLength, OUT PULONG Type)
Definition: image.c:98
KSDDKAPI NTSTATUS NTAPI KsGetImageNameAndResourceId(IN HANDLE RegKey, OUT PUNICODE_STRING ImageName, OUT PULONG_PTR ResourceId, OUT PULONG ValueType)
Definition: image.c:115
#define ULONG_PTR
Definition: config.h:101
#define PagedPool
Definition: env_spec_w32.h:308
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
FxAutoRegKey hKey
Status
Definition: gdiplustypes.h:25
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define KSDDKAPI
Definition: ks.h:40
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:29
VOID FreeItem(IN PVOID Item)
Definition: misc.c:37
NTSTATUS NTAPI LdrAccessResource(_In_ PVOID BaseAddress, _In_ PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry, _Out_opt_ PVOID *Resource, _Out_opt_ PULONG Size)
NTSTATUS NTAPI LdrFindResource_U(_In_ PVOID BaseAddress, _In_ PLDR_RESOURCE_INFO ResourceInfo, _In_ ULONG Level, _Out_ PIMAGE_RESOURCE_DATA_ENTRY *ResourceDataEntry)
#define RESOURCE_DATA_LEVEL
Definition: ldrtypes.h:33
static const char * ImageName
Definition: image.c:34
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ PNDIS_STRING SubKeyName
Definition: ndis.h:4725
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4715
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239
#define L(x)
Definition: ntvdm.h:50
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS NTAPI IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject, IN ULONG DevInstKeyType, IN ACCESS_MASK DesiredAccess, OUT PHANDLE DevInstRegKey)
Definition: pnpmgr.c:1621
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:159
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define _SEH2_VOLATILE
Definition: pseh2_64.h:163
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
Definition: pedump.c:458
ULONG_PTR Language
Definition: ldrtypes.h:183
ULONG_PTR Name
Definition: ldrtypes.h:182
ULONG_PTR Type
Definition: ldrtypes.h:181
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
uint32_t * PULONG_PTR
Definition: typedefs.h:65
uint32_t * PULONG
Definition: typedefs.h:59
INT POOL_TYPE
Definition: typedefs.h:78
#define NTAPI
Definition: typedefs.h:36
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3815
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2699
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG _Out_opt_ PULONG _Out_opt_ PULONG ValueType
Definition: wdfregistry.h:282
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
#define PLUGPLAY_REGKEY_DEVICE
Definition: iofuncs.h:2786
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184