ReactOS 0.4.16-dev-340-g0540c21
file.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS win32 kernel mode subsystem server
4 * PURPOSE: File access support routines
5 * FILE: win32ss/user/ntuser/misc/file.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9#include <win32k.h>
10
11#define NDEBUG
12#include <debug.h>
13
14BOOL
17 IN PCWSTR pwszDosPathName,
18 OUT PUNICODE_STRING pustrNtPathName)
19{
21
22 /* Prepend "\??\" */
23 pustrNtPathName->Length = 0;
24 Status = RtlAppendUnicodeToString(pustrNtPathName, L"\\??\\");
25 if (!NT_SUCCESS(Status))
26 {
27 return FALSE;
28 }
29
30 /* Append the dos name */
31 Status = RtlAppendUnicodeToString(pustrNtPathName, pwszDosPathName);
32 if (!NT_SUCCESS(Status))
33 {
34 return FALSE;
35 }
36
37 return TRUE;
38}
39
40
43W32kOpenFile(PCWSTR pwszFileName, DWORD dwDesiredAccess)
44{
45 UNICODE_STRING ustrFile;
50
51 DPRINT("W32kOpenFile(%S)\n", pwszFileName);
52
53 RtlInitUnicodeString(&ustrFile, pwszFileName);
54
56 &ustrFile,
58 NULL,
59 NULL);
60
61 Status = ZwCreateFile(&hFile,
62 dwDesiredAccess,
65 NULL,
67 0,
70 NULL,
71 0);
72 if (!NT_SUCCESS(Status))
73 {
75 hFile = NULL;
76 }
77
78 DPRINT("Leaving W32kOpenFile, Status=0x%lx, hFile=0x%p\n", Status, hFile);
79 return hFile;
80}
81
85 ULONG flAllocation,
86 DWORD flPageProtection,
87 ULONGLONG ullMaxSize)
88{
91 ACCESS_MASK amDesiredAccess;
92
93 /* Set access mask */
95
96 /* Check if write access is requested */
97 if (flPageProtection == PAGE_READWRITE)
98 {
99 /* Add it to access mask */
100 amDesiredAccess |= SECTION_MAP_WRITE;
101 }
102
103 /* Now create the actual section */
104 Status = ZwCreateSection(&hSection,
105 amDesiredAccess,
106 NULL,
107 NULL,
108 flPageProtection,
109 flAllocation,
110 hFile);
111 if (!NT_SUCCESS(Status))
112 {
114 hSection = NULL;
115 }
116
117 DPRINT("Leaving W32kCreateFileSection, Status=0x%lx, hSection=0x%p\n", Status, hSection);
118
119 /* Return section handle */
120 return hSection;
121}
122
123PVOID
124NTAPI
127 DWORD dwPageProtect,
128 ULONG_PTR ulSectionOffset)
129{
131 LARGE_INTEGER liSectionOffset;
132 ULONG_PTR ulViewSize;
133 PVOID pvBase = NULL;
134
135 liSectionOffset.QuadPart = ulViewSize = ulSectionOffset;
136 Status = ZwMapViewOfSection(hSection,
138 &pvBase,
139 0,
140 0,
141 &liSectionOffset,
142 &ulViewSize,
143 ViewShare,
144 0,
145 dwPageProtect);
146 if (!NT_SUCCESS(Status))
147 {
149 pvBase = NULL;
150 }
151
152 DPRINT("Leaving W32kMapViewOfSection, Status=0x%lx, pvBase=0x%p\n", Status, pvBase);
153
154 return pvBase;
155}
156
158NTAPI
160{
163 BITMAPFILEHEADER *pbmfh;
165 PVOID pvBits;
166 HBITMAP hbmp = 0;
167
168 DPRINT("Enter UserLoadImage(%ls)\n", pwszName);
169
170 /* Open the file */
171 hFile = W32kOpenFile(pwszName, FILE_READ_DATA);
172 if (!hFile)
173 {
174 return NULL;
175 }
176
177 /* Create a section */
179 ZwClose(hFile);
180 if (!hSection)
181 {
182 return NULL;
183 }
184
185 /* Map the section */
188 if (!pbmfh)
189 {
190 return NULL;
191 }
192
193 /* Get a pointer to the BITMAPINFO */
194 pbmi = (LPBITMAPINFO)(pbmfh + 1);
195
197 {
198 ProbeForRead(&pbmfh->bfSize, sizeof(DWORD), 1);
199 ProbeForRead(pbmfh, pbmfh->bfSize, 1);
200 }
202 {
204 }
206
207 if(!NT_SUCCESS(Status))
208 {
209 DPRINT1("Bad File?\n");
210 goto leave;
211 }
212
213 if (pbmfh->bfType == 0x4D42 /* 'BM' */)
214 {
215 /* Could be BITMAPCOREINFO */
216 BITMAPINFO* pConvertedInfo;
217 HDC hdc;
218
219 pvBits = (PVOID)((PCHAR)pbmfh + pbmfh->bfOffBits);
220
221 pConvertedInfo = DIB_ConvertBitmapInfo(pbmi, DIB_RGB_COLORS);
222 if(!pConvertedInfo)
223 {
224 DPRINT1("Unable to convert the bitmap Info\n");
225 goto leave;
226 }
227
229
231 pConvertedInfo->bmiHeader.biWidth,
232 pConvertedInfo->bmiHeader.biHeight,
233 CBM_INIT,
234 pvBits,
235 pConvertedInfo,
237 0,
238 pbmfh->bfSize - pbmfh->bfOffBits,
239 0);
240
242 DIB_FreeConvertedBitmapInfo(pConvertedInfo, pbmi, -1);
243 }
244 else
245 {
246 DPRINT1("Unknown file type!\n");
247 }
248
249leave:
250 /* Unmap our section, we don't need it anymore */
251 ZwUnmapViewOfSection(NtCurrentProcess(), pbmfh);
252
253 DPRINT("Leaving UserLoadImage, hbmp = %p\n", hbmp);
254 return hbmp;
255}
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define DPRINT1
Definition: precomp.h:8
HBITMAP NTAPI GreCreateDIBitmapInternal(IN HDC hDc, IN INT cx, IN INT cy, IN DWORD fInit, IN OPTIONAL LPBYTE pjInit, IN OPTIONAL PBITMAPINFO pbmi, IN DWORD iUsage, IN FLONG fl, IN UINT cjMaxBits, IN HANDLE hcmXform)
Definition: dibobj.c:1718
#define leave
Definition: btrfs_drv.h:138
HBITMAP hbmp
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
HDC FASTCALL IntGdiCreateDC(PUNICODE_STRING Driver, PUNICODE_STRING pustrDevice, PVOID pUMdhpdev, CONST PDEVMODEW pdmInit, BOOL CreateAsIC)
Definition: dclife.c:1040
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define PAGE_READONLY
Definition: compat.h:138
#define SECTION_MAP_READ
Definition: compat.h:139
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_OPEN
Definition: from_kernel.h:54
Status
Definition: gdiplustypes.h:25
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
HDC hdc
Definition: main.c:9
static HBITMAP
Definition: button.c:44
static HDC
Definition: imagelist.c:88
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_In_ HANDLE hFile
Definition: mswsock.h:90
#define SEC_COMMIT
Definition: mmtypes.h:100
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define SECTION_MAP_WRITE
Definition: nt_native.h:1288
#define FILE_READ_DATA
Definition: nt_native.h:628
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_QUERY
Definition: nt_native.h:1287
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define NtCurrentProcess()
Definition: nt_native.h:1657
@ ViewShare
Definition: nt_native.h:1278
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
_In_ HBITMAP _In_ UINT _In_ UINT _Inout_ LPBITMAPINFO pbmi
Definition: ntgdi.h:2780
__kernel_entry W32KAPI BOOL APIENTRY NtGdiDeleteObjectApp(_In_ HANDLE hobj)
#define L(x)
Definition: ntvdm.h:50
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
#define _SEH2_END
Definition: pseh2_64.h:155
#define _SEH2_TRY
Definition: pseh2_64.h:55
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1476
const uint16_t * PCWSTR
Definition: typedefs.h:57
#define NTAPI
Definition: typedefs.h:36
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
LONGLONG QuadPart
Definition: typedefs.h:114
VOID FASTCALL SetLastNtError(_In_ NTSTATUS Status)
Definition: error.c:31
BITMAPINFO *FASTCALL DIB_ConvertBitmapInfo(CONST BITMAPINFO *bmi, DWORD Usage)
Definition: dibobj.c:2238
VOID FASTCALL DIB_FreeConvertedBitmapInfo(BITMAPINFO *converted, BITMAPINFO *orig, DWORD Usage)
Definition: dibobj.c:2302
BOOL NTAPI W32kDosPathNameToNtPathName(IN PCWSTR pwszDosPathName, OUT PUNICODE_STRING pustrNtPathName)
Definition: file.c:16
HANDLE NTAPI W32kOpenFile(PCWSTR pwszFileName, DWORD dwDesiredAccess)
Definition: file.c:43
HBITMAP NTAPI UserLoadImage(PCWSTR pwszName)
Definition: file.c:159
PVOID NTAPI W32kMapViewOfSection(HANDLE hSection, DWORD dwPageProtect, ULONG_PTR ulSectionOffset)
Definition: file.c:125
HANDLE NTAPI W32kCreateFileSection(HANDLE hFile, ULONG flAllocation, DWORD flPageProtection, ULONGLONG ullMaxSize)
Definition: file.c:84
_In_ const BITMAPINFO _In_ UINT _In_opt_ HANDLE hSection
Definition: wingdi.h:3239
#define DIB_RGB_COLORS
Definition: wingdi.h:367
struct tagBITMAPINFO * LPBITMAPINFO
#define CBM_INIT
Definition: wingdi.h:365