1*c2c66affSColin Finck ////////////////////////////////////////////////////////////////////
2*c2c66affSColin Finck // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3*c2c66affSColin Finck // All rights reserved
4*c2c66affSColin Finck // This file was released under the GPLv2 on June 2015.
5*c2c66affSColin Finck ////////////////////////////////////////////////////////////////////
6*c2c66affSColin Finck 
7*c2c66affSColin Finck #ifdef NT_NATIVE_MODE
8*c2c66affSColin Finck 
9*c2c66affSColin Finck #include "regtools.h"
10*c2c66affSColin Finck #include <stdarg.h>
11*c2c66affSColin Finck 
12*c2c66affSColin Finck /*typedef BOOLEAN (*PPsGetVersion) (
13*c2c66affSColin Finck     PULONG MajorVersion OPTIONAL,
14*c2c66affSColin Finck     PULONG MinorVersion OPTIONAL,
15*c2c66affSColin Finck     PULONG BuildNumber OPTIONAL,
16*c2c66affSColin Finck     PUNICODE_STRING CSDVersion OPTIONAL
17*c2c66affSColin Finck     );
18*c2c66affSColin Finck 
19*c2c66affSColin Finck //PPsGetVersion _PsGetVersion = PsGetVersion;
20*c2c66affSColin Finck 
21*c2c66affSColin Finck /*NTSTATUS
22*c2c66affSColin Finck KernelGetProcAddress(
23*c2c66affSColin Finck     PWCHAR DllName,
24*c2c66affSColin Finck     PUCHAR ProcName,
25*c2c66affSColin Finck     PVOID* ProcAddr
26*c2c66affSColin Finck     )
27*c2c66affSColin Finck {
28*c2c66affSColin Finck   NTSTATUS RC;
29*c2c66affSColin Finck   HANDLE h;
30*c2c66affSColin Finck   UNICODE_STRING uname;
31*c2c66affSColin Finck   ANSI_STRING aname;
32*c2c66affSColin Finck 
33*c2c66affSColin Finck   RtlInitUnicodeString(&uname, DllName);
34*c2c66affSColin Finck   *ProcAddr = NULL;
35*c2c66affSColin Finck 
36*c2c66affSColin Finck  // RC = LdrGetDllHandle(NULL, NULL, &uname, &h);
37*c2c66affSColin Finck   if(!NT_SUCCESS(RC))
38*c2c66affSColin Finck     return RC;
39*c2c66affSColin Finck 
40*c2c66affSColin Finck   RtlInitAnsiString(&aname, ProcName);
41*c2c66affSColin Finck 
42*c2c66affSColin Finck //  RC = LdrGetProcedureAddress(h, &aname, 0, ProcAddr);
43*c2c66affSColin Finck   return RC;
44*c2c66affSColin Finck } */
45*c2c66affSColin Finck 
46*c2c66affSColin Finck 
47*c2c66affSColin Finck BOOLEAN
GetOsVersion(PULONG MajorVersion OPTIONAL,PULONG MinorVersion OPTIONAL,PULONG BuildNumber OPTIONAL,PUNICODE_STRING CSDVersion OPTIONAL)48*c2c66affSColin Finck GetOsVersion(
49*c2c66affSColin Finck     PULONG MajorVersion OPTIONAL,
50*c2c66affSColin Finck     PULONG MinorVersion OPTIONAL,
51*c2c66affSColin Finck     PULONG BuildNumber OPTIONAL,
52*c2c66affSColin Finck     PUNICODE_STRING CSDVersion OPTIONAL
53*c2c66affSColin Finck     )
54*c2c66affSColin Finck {
55*c2c66affSColin Finck   WCHAR Str[32];
56*c2c66affSColin Finck   ULONG mn=0, mj=0, bld=0;
57*c2c66affSColin Finck 
58*c2c66affSColin Finck //  if(_PsGetVersion)
59*c2c66affSColin Finck //    return _PsGetVersion(MajorVersion, MinorVersion, BuildNumber, CSDVersion);
60*c2c66affSColin Finck 
61*c2c66affSColin Finck   RtlZeroMemory(Str, sizeof(Str));
62*c2c66affSColin Finck   if(RegTGetStringValue(NULL, L"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
63*c2c66affSColin Finck                                    L"CurrentVersion",
64*c2c66affSColin Finck                                    &Str[0], sizeof(Str)-sizeof(WCHAR))) {
65*c2c66affSColin Finck     ULONG i=0;
66*c2c66affSColin Finck     WCHAR a;
67*c2c66affSColin Finck     while(a = Str[i]) {
68*c2c66affSColin Finck       if(a == '.')
69*c2c66affSColin Finck         break;
70*c2c66affSColin Finck       if(a < '0' || a > '9')
71*c2c66affSColin Finck         break;
72*c2c66affSColin Finck       mj = mj*16 + (a-'0');
73*c2c66affSColin Finck       i++;
74*c2c66affSColin Finck     }
75*c2c66affSColin Finck     i++;
76*c2c66affSColin Finck     while(a = Str[i]) {
77*c2c66affSColin Finck       if(a == '.')
78*c2c66affSColin Finck         break;
79*c2c66affSColin Finck       if(a < '0' || a > '9')
80*c2c66affSColin Finck         break;
81*c2c66affSColin Finck       mn = mn*16 + (a-'0');
82*c2c66affSColin Finck       i++;
83*c2c66affSColin Finck     }
84*c2c66affSColin Finck   }
85*c2c66affSColin Finck 
86*c2c66affSColin Finck   if(RegTGetStringValue(NULL, L"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
87*c2c66affSColin Finck                                    L"CurrentBuildNumber",
88*c2c66affSColin Finck                                    &Str[0], sizeof(Str)-sizeof(WCHAR))) {
89*c2c66affSColin Finck     ULONG i=0;
90*c2c66affSColin Finck     WCHAR a;
91*c2c66affSColin Finck     while(a = Str[i]) {
92*c2c66affSColin Finck       if(a < '0' || a > '9')
93*c2c66affSColin Finck         break;
94*c2c66affSColin Finck       bld = bld*10 + (a-'0');
95*c2c66affSColin Finck       i++;
96*c2c66affSColin Finck     }
97*c2c66affSColin Finck   }
98*c2c66affSColin Finck   if(MajorVersion)
99*c2c66affSColin Finck     *MajorVersion = mj;
100*c2c66affSColin Finck   if(MinorVersion)
101*c2c66affSColin Finck     *MinorVersion = mn;
102*c2c66affSColin Finck   if(BuildNumber)
103*c2c66affSColin Finck     *BuildNumber = bld;
104*c2c66affSColin Finck   return TRUE;
105*c2c66affSColin Finck }
106*c2c66affSColin Finck 
107*c2c66affSColin Finck BOOLEAN
MyDeviceIoControl(HANDLE h,DWORD dwIoControlCode,PVOID lpInBuffer,DWORD nInBufferSize,PVOID lpOutBuffer,DWORD nOutBufferSize,DWORD * lpBytesReturned,PVOID lpOverlapped)108*c2c66affSColin Finck MyDeviceIoControl(
109*c2c66affSColin Finck     HANDLE h,
110*c2c66affSColin Finck     DWORD  dwIoControlCode,
111*c2c66affSColin Finck     PVOID  lpInBuffer,
112*c2c66affSColin Finck     DWORD  nInBufferSize,
113*c2c66affSColin Finck     PVOID  lpOutBuffer,
114*c2c66affSColin Finck     DWORD  nOutBufferSize,
115*c2c66affSColin Finck     DWORD* lpBytesReturned,
116*c2c66affSColin Finck     PVOID  lpOverlapped
117*c2c66affSColin Finck     )
118*c2c66affSColin Finck {
119*c2c66affSColin Finck 
120*c2c66affSColin Finck     NTSTATUS RC;
121*c2c66affSColin Finck     BOOLEAN DevIoCtl = TRUE;
122*c2c66affSColin Finck     IO_STATUS_BLOCK Iosb;
123*c2c66affSColin Finck 
124*c2c66affSColin Finck     if ( dwIoControlCode >> 16 == FILE_DEVICE_FILE_SYSTEM ) {
125*c2c66affSColin Finck         DevIoCtl = FALSE;
126*c2c66affSColin Finck     } else {
127*c2c66affSColin Finck         DevIoCtl = TRUE;
128*c2c66affSColin Finck     }
129*c2c66affSColin Finck 
130*c2c66affSColin Finck     if ( DevIoCtl ) {
131*c2c66affSColin Finck         RC = NtDeviceIoControlFile(
132*c2c66affSColin Finck                     h,
133*c2c66affSColin Finck                     NULL,
134*c2c66affSColin Finck                     NULL,             // APC routine
135*c2c66affSColin Finck                     NULL,             // APC Context
136*c2c66affSColin Finck                     &Iosb,
137*c2c66affSColin Finck                     dwIoControlCode,  // IoControlCode
138*c2c66affSColin Finck                     lpInBuffer,       // Buffer for data to the FS
139*c2c66affSColin Finck                     nInBufferSize,
140*c2c66affSColin Finck                     lpOutBuffer,      // OutputBuffer for data from the FS
141*c2c66affSColin Finck                     nOutBufferSize    // OutputBuffer Length
142*c2c66affSColin Finck                     );
143*c2c66affSColin Finck     } else {
144*c2c66affSColin Finck         RC = NtFsControlFile(
145*c2c66affSColin Finck                     h,
146*c2c66affSColin Finck                     NULL,
147*c2c66affSColin Finck                     NULL,             // APC routine
148*c2c66affSColin Finck                     NULL,             // APC Context
149*c2c66affSColin Finck                     &Iosb,
150*c2c66affSColin Finck                     dwIoControlCode,  // IoControlCode
151*c2c66affSColin Finck                     lpInBuffer,       // Buffer for data to the FS
152*c2c66affSColin Finck                     nInBufferSize,
153*c2c66affSColin Finck                     lpOutBuffer,      // OutputBuffer for data from the FS
154*c2c66affSColin Finck                     nOutBufferSize    // OutputBuffer Length
155*c2c66affSColin Finck                     );
156*c2c66affSColin Finck     }
157*c2c66affSColin Finck 
158*c2c66affSColin Finck     if ( RC == STATUS_PENDING) {
159*c2c66affSColin Finck         // Operation must complete before return & Iosb destroyed
160*c2c66affSColin Finck         RC = NtWaitForSingleObject( h, FALSE, NULL );
161*c2c66affSColin Finck         if ( NT_SUCCESS(RC)) {
162*c2c66affSColin Finck             RC = Iosb.Status;
163*c2c66affSColin Finck         }
164*c2c66affSColin Finck     }
165*c2c66affSColin Finck 
166*c2c66affSColin Finck     if ( NT_SUCCESS(RC) ) {
167*c2c66affSColin Finck         *lpBytesReturned = Iosb.Information;
168*c2c66affSColin Finck         return TRUE;
169*c2c66affSColin Finck     } else {
170*c2c66affSColin Finck         // handle warning value STATUS_BUFFER_OVERFLOW somewhat correctly
171*c2c66affSColin Finck         if ( !NT_ERROR(RC) ) {
172*c2c66affSColin Finck             *lpBytesReturned = Iosb.Information;
173*c2c66affSColin Finck         }
174*c2c66affSColin Finck         return FALSE;
175*c2c66affSColin Finck     }
176*c2c66affSColin Finck }
177*c2c66affSColin Finck 
178*c2c66affSColin Finck VOID
Sleep(ULONG t)179*c2c66affSColin Finck Sleep(
180*c2c66affSColin Finck     ULONG t
181*c2c66affSColin Finck     )
182*c2c66affSColin Finck {
183*c2c66affSColin Finck     LARGE_INTEGER delay = {0,0};
184*c2c66affSColin Finck     delay.QuadPart = -10I64*1000*t;
185*c2c66affSColin Finck     NtDelayExecution(FALSE, &delay);
186*c2c66affSColin Finck }
187*c2c66affSColin Finck 
188*c2c66affSColin Finck HANDLE hGlobalHeap = NULL;
189*c2c66affSColin Finck 
190*c2c66affSColin Finck extern "C"
191*c2c66affSColin Finck PVOID
MyGlobalAlloc(ULONG Size)192*c2c66affSColin Finck MyGlobalAlloc(
193*c2c66affSColin Finck     ULONG Size
194*c2c66affSColin Finck     )
195*c2c66affSColin Finck {
196*c2c66affSColin Finck     if(!hGlobalHeap) {
197*c2c66affSColin Finck         // Initialize some heap
198*c2c66affSColin Finck         hGlobalHeap = RtlCreateHeap( HEAP_GROWABLE,    // Flags
199*c2c66affSColin Finck                                          NULL,              // HeapBase
200*c2c66affSColin Finck                                          0,                 // ReserveSize
201*c2c66affSColin Finck                                          0,                 // CommitSize
202*c2c66affSColin Finck                                          NULL,              // Lock
203*c2c66affSColin Finck                                          NULL );            // Parameters
204*c2c66affSColin Finck         if(!hGlobalHeap || hGlobalHeap == (HANDLE)(-1)) {
205*c2c66affSColin Finck             hGlobalHeap = NULL;
206*c2c66affSColin Finck             return NULL;
207*c2c66affSColin Finck         }
208*c2c66affSColin Finck     }
209*c2c66affSColin Finck     return RtlAllocateHeap( hGlobalHeap, 0, Size );
210*c2c66affSColin Finck }
211*c2c66affSColin Finck 
212*c2c66affSColin Finck extern "C"
213*c2c66affSColin Finck VOID
MyGlobalFree(PVOID Addr)214*c2c66affSColin Finck MyGlobalFree(
215*c2c66affSColin Finck     PVOID Addr
216*c2c66affSColin Finck     )
217*c2c66affSColin Finck {
218*c2c66affSColin Finck     if(!hGlobalHeap) {
219*c2c66affSColin Finck //        BrutePoint();
220*c2c66affSColin Finck         return;
221*c2c66affSColin Finck     }
222*c2c66affSColin Finck     RtlFreeHeap( hGlobalHeap, 0, Addr );
223*c2c66affSColin Finck     return;
224*c2c66affSColin Finck }
225*c2c66affSColin Finck 
226*c2c66affSColin Finck CHAR dbg_print_tmp_buff[2048];
227*c2c66affSColin Finck WCHAR dbg_stringBuffer[2048];
228*c2c66affSColin Finck 
229*c2c66affSColin Finck BOOLEAN was_enter = TRUE;
230*c2c66affSColin Finck 
231*c2c66affSColin Finck extern "C"
232*c2c66affSColin Finck VOID
PrintNtConsole(PCHAR DebugMessage,...)233*c2c66affSColin Finck PrintNtConsole(
234*c2c66affSColin Finck     PCHAR DebugMessage,
235*c2c66affSColin Finck     ...
236*c2c66affSColin Finck     )
237*c2c66affSColin Finck {
238*c2c66affSColin Finck     int len;
239*c2c66affSColin Finck     UNICODE_STRING msgBuff;
240*c2c66affSColin Finck     va_list ap;
241*c2c66affSColin Finck     va_start(ap, DebugMessage);
242*c2c66affSColin Finck 
243*c2c66affSColin Finck     if(was_enter) {
244*c2c66affSColin Finck         strcpy(&dbg_print_tmp_buff[0], NT_DBG_PREFIX);
245*c2c66affSColin Finck         len = _vsnprintf(&dbg_print_tmp_buff[sizeof(NT_DBG_PREFIX)-1], 2047-sizeof(NT_DBG_PREFIX), DebugMessage, ap);
246*c2c66affSColin Finck     } else {
247*c2c66affSColin Finck         len = _vsnprintf(&dbg_print_tmp_buff[0], 2047, DebugMessage, ap);
248*c2c66affSColin Finck     }
249*c2c66affSColin Finck     dbg_print_tmp_buff[2047] = 0;
250*c2c66affSColin Finck     if(len > 0 &&
251*c2c66affSColin Finck        (dbg_print_tmp_buff[len-1] == '\n' ||
252*c2c66affSColin Finck         dbg_print_tmp_buff[len-1] == '\r') ) {
253*c2c66affSColin Finck         was_enter = TRUE;
254*c2c66affSColin Finck     } else {
255*c2c66affSColin Finck         was_enter = FALSE;
256*c2c66affSColin Finck     }
257*c2c66affSColin Finck 
258*c2c66affSColin Finck     len = swprintf( dbg_stringBuffer, L"%S", dbg_print_tmp_buff );
259*c2c66affSColin Finck     msgBuff.Buffer = dbg_stringBuffer;
260*c2c66affSColin Finck     msgBuff.Length = len * sizeof(WCHAR);
261*c2c66affSColin Finck     msgBuff.MaximumLength = msgBuff.Length + sizeof(WCHAR);
262*c2c66affSColin Finck     NtDisplayString( &msgBuff );
263*c2c66affSColin Finck 
264*c2c66affSColin Finck     va_end(ap);
265*c2c66affSColin Finck 
266*c2c66affSColin Finck } // end PrintNtConsole()
267*c2c66affSColin Finck 
268*c2c66affSColin Finck extern "C"
269*c2c66affSColin Finck NTSTATUS
EnvFileOpenW(PWCHAR Name,HANDLE * ph)270*c2c66affSColin Finck EnvFileOpenW(
271*c2c66affSColin Finck     PWCHAR Name,
272*c2c66affSColin Finck     HANDLE* ph
273*c2c66affSColin Finck     )
274*c2c66affSColin Finck {
275*c2c66affSColin Finck     OBJECT_ATTRIBUTES ObjectAttributes;
276*c2c66affSColin Finck     IO_STATUS_BLOCK   IoStatus;
277*c2c66affSColin Finck     NTSTATUS Status;
278*c2c66affSColin Finck     UNICODE_STRING fName;
279*c2c66affSColin Finck 
280*c2c66affSColin Finck     RtlInitUnicodeString(&fName, Name);
281*c2c66affSColin Finck 
282*c2c66affSColin Finck     InitializeObjectAttributes(&ObjectAttributes, &fName, OBJ_CASE_INSENSITIVE, NULL, NULL);
283*c2c66affSColin Finck 
284*c2c66affSColin Finck     Status = NtCreateFile(ph,
285*c2c66affSColin Finck                              GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
286*c2c66affSColin Finck                              &ObjectAttributes,
287*c2c66affSColin Finck                              &IoStatus,
288*c2c66affSColin Finck                              NULL,
289*c2c66affSColin Finck                              FILE_ATTRIBUTE_NORMAL,
290*c2c66affSColin Finck                              FILE_SHARE_READ | FILE_SHARE_WRITE,
291*c2c66affSColin Finck                              FILE_OPEN,
292*c2c66affSColin Finck                              FILE_SYNCHRONOUS_IO_NONALERT | FILE_COMPLETE_IF_OPLOCKED /*| FILE_WRITE_THROUGH*/,
293*c2c66affSColin Finck                              NULL,
294*c2c66affSColin Finck                              0);
295*c2c66affSColin Finck 
296*c2c66affSColin Finck     return Status;
297*c2c66affSColin Finck } // end EnvFileOpenW()
298*c2c66affSColin Finck 
299*c2c66affSColin Finck extern "C"
300*c2c66affSColin Finck NTSTATUS
EnvFileOpenA(PCHAR Name,HANDLE * ph)301*c2c66affSColin Finck EnvFileOpenA(
302*c2c66affSColin Finck     PCHAR Name,
303*c2c66affSColin Finck     HANDLE* ph
304*c2c66affSColin Finck     )
305*c2c66affSColin Finck {
306*c2c66affSColin Finck     ULONG len;
307*c2c66affSColin Finck     PWCHAR NameW;
308*c2c66affSColin Finck     NTSTATUS Status;
309*c2c66affSColin Finck 
310*c2c66affSColin Finck     len = strlen(Name);
311*c2c66affSColin Finck 
312*c2c66affSColin Finck     NameW = (PWCHAR)MyAllocatePool__(NonPagedPool, (len+1)*sizeof(WCHAR));
313*c2c66affSColin Finck     if(!NameW)
314*c2c66affSColin Finck         return STATUS_INSUFFICIENT_RESOURCES;
315*c2c66affSColin Finck 
316*c2c66affSColin Finck     swprintf(NameW, L"%S", Name);
317*c2c66affSColin Finck 
318*c2c66affSColin Finck     Status = EnvFileOpenW(NameW, ph);
319*c2c66affSColin Finck 
320*c2c66affSColin Finck     MyFreePool__(NameW);
321*c2c66affSColin Finck 
322*c2c66affSColin Finck     return Status;
323*c2c66affSColin Finck } // end EnvFileOpenA()
324*c2c66affSColin Finck 
325*c2c66affSColin Finck extern "C"
326*c2c66affSColin Finck NTSTATUS
EnvFileClose(HANDLE hFile)327*c2c66affSColin Finck EnvFileClose(
328*c2c66affSColin Finck     HANDLE hFile
329*c2c66affSColin Finck     )
330*c2c66affSColin Finck {
331*c2c66affSColin Finck     return NtClose(hFile);
332*c2c66affSColin Finck } // end EnvFileClose()
333*c2c66affSColin Finck 
334*c2c66affSColin Finck extern "C"
335*c2c66affSColin Finck NTSTATUS
EnvFileGetSizeByHandle(HANDLE hFile,PLONGLONG lpFileSize)336*c2c66affSColin Finck EnvFileGetSizeByHandle(
337*c2c66affSColin Finck     HANDLE hFile,
338*c2c66affSColin Finck     PLONGLONG lpFileSize
339*c2c66affSColin Finck     )
340*c2c66affSColin Finck {
341*c2c66affSColin Finck     NTSTATUS Status;
342*c2c66affSColin Finck     IO_STATUS_BLOCK IoStatusBlock;
343*c2c66affSColin Finck     FILE_STANDARD_INFORMATION StandardInfo;
344*c2c66affSColin Finck 
345*c2c66affSColin Finck     Status = NtQueryInformationFile(
346*c2c66affSColin Finck                 hFile,
347*c2c66affSColin Finck                 &IoStatusBlock,
348*c2c66affSColin Finck                 &StandardInfo,
349*c2c66affSColin Finck                 sizeof(StandardInfo),
350*c2c66affSColin Finck                 FileStandardInformation
351*c2c66affSColin Finck                 );
352*c2c66affSColin Finck     if (NT_SUCCESS(Status)) {
353*c2c66affSColin Finck         *lpFileSize = StandardInfo.EndOfFile.QuadPart;
354*c2c66affSColin Finck     }
355*c2c66affSColin Finck     return Status;
356*c2c66affSColin Finck } // end EnvFileGetSizeByHandle()
357*c2c66affSColin Finck 
358*c2c66affSColin Finck extern "C"
359*c2c66affSColin Finck NTSTATUS
EnvFileGetSizeA(PCHAR Name,PLONGLONG lpFileSize)360*c2c66affSColin Finck EnvFileGetSizeA(
361*c2c66affSColin Finck     PCHAR Name,
362*c2c66affSColin Finck     PLONGLONG lpFileSize
363*c2c66affSColin Finck     )
364*c2c66affSColin Finck {
365*c2c66affSColin Finck     NTSTATUS Status;
366*c2c66affSColin Finck     HANDLE hFile;
367*c2c66affSColin Finck 
368*c2c66affSColin Finck     (*lpFileSize) = -1I64;
369*c2c66affSColin Finck 
370*c2c66affSColin Finck     Status = EnvFileOpenA(Name, &hFile);
371*c2c66affSColin Finck 
372*c2c66affSColin Finck     if(!NT_SUCCESS(Status))
373*c2c66affSColin Finck         return Status;
374*c2c66affSColin Finck 
375*c2c66affSColin Finck     Status = EnvFileGetSizeByHandle(hFile, lpFileSize);
376*c2c66affSColin Finck 
377*c2c66affSColin Finck     NtClose(hFile);
378*c2c66affSColin Finck 
379*c2c66affSColin Finck     return Status;
380*c2c66affSColin Finck } // end EnvFileGetSizeA()
381*c2c66affSColin Finck 
382*c2c66affSColin Finck extern "C"
383*c2c66affSColin Finck NTSTATUS
EnvFileGetSizeW(PWCHAR Name,PLONGLONG lpFileSize)384*c2c66affSColin Finck EnvFileGetSizeW(
385*c2c66affSColin Finck     PWCHAR Name,
386*c2c66affSColin Finck     PLONGLONG lpFileSize
387*c2c66affSColin Finck     )
388*c2c66affSColin Finck {
389*c2c66affSColin Finck     NTSTATUS Status;
390*c2c66affSColin Finck     HANDLE hFile;
391*c2c66affSColin Finck 
392*c2c66affSColin Finck     (*lpFileSize) = -1I64;
393*c2c66affSColin Finck 
394*c2c66affSColin Finck     Status = EnvFileOpenW(Name, &hFile);
395*c2c66affSColin Finck 
396*c2c66affSColin Finck     if(!NT_SUCCESS(Status))
397*c2c66affSColin Finck         return Status;
398*c2c66affSColin Finck 
399*c2c66affSColin Finck     Status = EnvFileGetSizeByHandle(hFile, lpFileSize);
400*c2c66affSColin Finck 
401*c2c66affSColin Finck     NtClose(hFile);
402*c2c66affSColin Finck 
403*c2c66affSColin Finck     return Status;
404*c2c66affSColin Finck } // end EnvFileGetSizeW()
405*c2c66affSColin Finck 
406*c2c66affSColin Finck extern "C"
407*c2c66affSColin Finck BOOLEAN
EnvFileExistsA(PCHAR Name)408*c2c66affSColin Finck EnvFileExistsA(PCHAR Name) {
409*c2c66affSColin Finck     LONGLONG Size;
410*c2c66affSColin Finck     EnvFileGetSizeA(Name, &Size);
411*c2c66affSColin Finck     return Size != -1;
412*c2c66affSColin Finck }
413*c2c66affSColin Finck 
414*c2c66affSColin Finck extern "C"
415*c2c66affSColin Finck BOOLEAN
EnvFileExistsW(PWCHAR Name)416*c2c66affSColin Finck EnvFileExistsW(PWCHAR Name) {
417*c2c66affSColin Finck     LONGLONG Size;
418*c2c66affSColin Finck     EnvFileGetSizeW(Name, &Size);
419*c2c66affSColin Finck     return Size != -1;
420*c2c66affSColin Finck }
421*c2c66affSColin Finck 
422*c2c66affSColin Finck extern "C"
423*c2c66affSColin Finck NTSTATUS
EnvFileWrite(HANDLE h,PVOID ioBuffer,ULONG Length,PULONG bytesWritten)424*c2c66affSColin Finck EnvFileWrite(
425*c2c66affSColin Finck     HANDLE h,
426*c2c66affSColin Finck     PVOID ioBuffer,
427*c2c66affSColin Finck     ULONG Length,
428*c2c66affSColin Finck     PULONG bytesWritten
429*c2c66affSColin Finck     )
430*c2c66affSColin Finck {
431*c2c66affSColin Finck     IO_STATUS_BLOCK   IoStatus;
432*c2c66affSColin Finck     NTSTATUS Status;
433*c2c66affSColin Finck 
434*c2c66affSColin Finck     Status = NtWriteFile(
435*c2c66affSColin Finck                       h,
436*c2c66affSColin Finck                       NULL,               // Event
437*c2c66affSColin Finck                       NULL,               // ApcRoutine
438*c2c66affSColin Finck                       NULL,               // ApcContext
439*c2c66affSColin Finck                       &IoStatus,
440*c2c66affSColin Finck                       ioBuffer,
441*c2c66affSColin Finck                       Length,
442*c2c66affSColin Finck                       NULL,               // ByteOffset
443*c2c66affSColin Finck                       NULL                // Key
444*c2c66affSColin Finck                       );
445*c2c66affSColin Finck     (*bytesWritten) = IoStatus.Information;
446*c2c66affSColin Finck 
447*c2c66affSColin Finck     return Status;
448*c2c66affSColin Finck } // end EnvFileWrite()
449*c2c66affSColin Finck 
450*c2c66affSColin Finck extern "C"
451*c2c66affSColin Finck NTSTATUS
EnvFileRead(HANDLE h,PVOID ioBuffer,ULONG Length,PULONG bytesRead)452*c2c66affSColin Finck EnvFileRead(
453*c2c66affSColin Finck     HANDLE h,
454*c2c66affSColin Finck     PVOID ioBuffer,
455*c2c66affSColin Finck     ULONG Length,
456*c2c66affSColin Finck     PULONG bytesRead
457*c2c66affSColin Finck     )
458*c2c66affSColin Finck {
459*c2c66affSColin Finck     IO_STATUS_BLOCK   IoStatus;
460*c2c66affSColin Finck     NTSTATUS Status;
461*c2c66affSColin Finck 
462*c2c66affSColin Finck     Status = NtReadFile(
463*c2c66affSColin Finck                       h,
464*c2c66affSColin Finck                       NULL,               // Event
465*c2c66affSColin Finck                       NULL,               // ApcRoutine
466*c2c66affSColin Finck                       NULL,               // ApcContext
467*c2c66affSColin Finck                       &IoStatus,
468*c2c66affSColin Finck                       ioBuffer,
469*c2c66affSColin Finck                       Length,
470*c2c66affSColin Finck                       NULL,               // ByteOffset
471*c2c66affSColin Finck                       NULL                // Key
472*c2c66affSColin Finck                       );
473*c2c66affSColin Finck     (*bytesRead) = IoStatus.Information;
474*c2c66affSColin Finck 
475*c2c66affSColin Finck     return Status;
476*c2c66affSColin Finck } // end EnvFileRead()
477*c2c66affSColin Finck 
478*c2c66affSColin Finck extern "C"
479*c2c66affSColin Finck NTSTATUS
EnvFileSetPointer(HANDLE hFile,LONGLONG lDistanceToMove,LONGLONG * lResultPointer,DWORD dwMoveMethod)480*c2c66affSColin Finck EnvFileSetPointer(
481*c2c66affSColin Finck     HANDLE hFile,
482*c2c66affSColin Finck     LONGLONG lDistanceToMove,
483*c2c66affSColin Finck     LONGLONG* lResultPointer,
484*c2c66affSColin Finck     DWORD dwMoveMethod
485*c2c66affSColin Finck     )
486*c2c66affSColin Finck {
487*c2c66affSColin Finck     NTSTATUS Status;
488*c2c66affSColin Finck     IO_STATUS_BLOCK IoStatus;
489*c2c66affSColin Finck     FILE_POSITION_INFORMATION CurrentPosition;
490*c2c66affSColin Finck     FILE_STANDARD_INFORMATION FileInfo;
491*c2c66affSColin Finck 
492*c2c66affSColin Finck     switch (dwMoveMethod) {
493*c2c66affSColin Finck         case ENV_FILE_BEGIN :
494*c2c66affSColin Finck             CurrentPosition.CurrentByteOffset.QuadPart = lDistanceToMove;
495*c2c66affSColin Finck                 break;
496*c2c66affSColin Finck 
497*c2c66affSColin Finck         case ENV_FILE_CURRENT :
498*c2c66affSColin Finck 
499*c2c66affSColin Finck             // Get the current position of the file pointer
500*c2c66affSColin Finck             Status = NtQueryInformationFile(
501*c2c66affSColin Finck                         hFile,
502*c2c66affSColin Finck                         &IoStatus,
503*c2c66affSColin Finck                         &CurrentPosition,
504*c2c66affSColin Finck                         sizeof(CurrentPosition),
505*c2c66affSColin Finck                         FilePositionInformation
506*c2c66affSColin Finck                         );
507*c2c66affSColin Finck             if(!NT_SUCCESS(Status)) {
508*c2c66affSColin Finck                 return Status;
509*c2c66affSColin Finck             }
510*c2c66affSColin Finck             CurrentPosition.CurrentByteOffset.QuadPart += lDistanceToMove;
511*c2c66affSColin Finck             break;
512*c2c66affSColin Finck 
513*c2c66affSColin Finck         case ENV_FILE_END :
514*c2c66affSColin Finck             Status = NtQueryInformationFile(
515*c2c66affSColin Finck                         hFile,
516*c2c66affSColin Finck                         &IoStatus,
517*c2c66affSColin Finck                         &FileInfo,
518*c2c66affSColin Finck                         sizeof(FileInfo),
519*c2c66affSColin Finck                         FileStandardInformation
520*c2c66affSColin Finck                         );
521*c2c66affSColin Finck             if (!NT_SUCCESS(Status)) {
522*c2c66affSColin Finck                 return Status;
523*c2c66affSColin Finck             }
524*c2c66affSColin Finck             CurrentPosition.CurrentByteOffset.QuadPart =
525*c2c66affSColin Finck                                 FileInfo.EndOfFile.QuadPart + lDistanceToMove;
526*c2c66affSColin Finck             break;
527*c2c66affSColin Finck 
528*c2c66affSColin Finck         default:
529*c2c66affSColin Finck             return STATUS_INVALID_PARAMETER;
530*c2c66affSColin Finck         }
531*c2c66affSColin Finck 
532*c2c66affSColin Finck     if ( CurrentPosition.CurrentByteOffset.QuadPart < 0 ) {
533*c2c66affSColin Finck         return Status;
534*c2c66affSColin Finck     }
535*c2c66affSColin Finck 
536*c2c66affSColin Finck     Status = NtSetInformationFile(
537*c2c66affSColin Finck                 hFile,
538*c2c66affSColin Finck                 &IoStatus,
539*c2c66affSColin Finck                 &CurrentPosition,
540*c2c66affSColin Finck                 sizeof(CurrentPosition),
541*c2c66affSColin Finck                 FilePositionInformation
542*c2c66affSColin Finck                 );
543*c2c66affSColin Finck 
544*c2c66affSColin Finck     if(!NT_SUCCESS(Status)) {
545*c2c66affSColin Finck         return Status;
546*c2c66affSColin Finck     }
547*c2c66affSColin Finck     if(lResultPointer) {
548*c2c66affSColin Finck         *lResultPointer = CurrentPosition.CurrentByteOffset.QuadPart;
549*c2c66affSColin Finck     }
550*c2c66affSColin Finck     return STATUS_SUCCESS;
551*c2c66affSColin Finck } // end EnvFileSetPointer()
552*c2c66affSColin Finck 
EnvFileDeleteW(PWCHAR Name)553*c2c66affSColin Finck NTSTATUS EnvFileDeleteW(PWCHAR Name) {
554*c2c66affSColin Finck 
555*c2c66affSColin Finck     OBJECT_ATTRIBUTES ObjectAttributes;
556*c2c66affSColin Finck     IO_STATUS_BLOCK   IoStatus;
557*c2c66affSColin Finck     NTSTATUS Status;
558*c2c66affSColin Finck     UNICODE_STRING fName;
559*c2c66affSColin Finck     HANDLE Handle;
560*c2c66affSColin Finck     FILE_DISPOSITION_INFORMATION Disposition;
561*c2c66affSColin Finck 
562*c2c66affSColin Finck     RtlInitUnicodeString(&fName, Name);
563*c2c66affSColin Finck 
564*c2c66affSColin Finck     InitializeObjectAttributes(&ObjectAttributes, &fName, OBJ_CASE_INSENSITIVE, NULL, NULL);
565*c2c66affSColin Finck 
566*c2c66affSColin Finck     Status = NtOpenFile(
567*c2c66affSColin Finck                  &Handle,
568*c2c66affSColin Finck                  (ACCESS_MASK)DELETE,
569*c2c66affSColin Finck                  &ObjectAttributes,
570*c2c66affSColin Finck                  &IoStatus,
571*c2c66affSColin Finck                  FILE_SHARE_DELETE |
572*c2c66affSColin Finck                  FILE_SHARE_READ |
573*c2c66affSColin Finck                  FILE_SHARE_WRITE,
574*c2c66affSColin Finck                  FILE_NON_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT
575*c2c66affSColin Finck              );
576*c2c66affSColin Finck 
577*c2c66affSColin Finck 
578*c2c66affSColin Finck     if ( !NT_SUCCESS(Status) ) {
579*c2c66affSColin Finck         return Status;
580*c2c66affSColin Finck     }
581*c2c66affSColin Finck 
582*c2c66affSColin Finck     Disposition.DeleteFile = TRUE;
583*c2c66affSColin Finck 
584*c2c66affSColin Finck     Status = NtSetInformationFile(
585*c2c66affSColin Finck                  Handle,
586*c2c66affSColin Finck                  &IoStatus,
587*c2c66affSColin Finck                  &Disposition,
588*c2c66affSColin Finck                  sizeof(Disposition),
589*c2c66affSColin Finck                  FileDispositionInformation
590*c2c66affSColin Finck              );
591*c2c66affSColin Finck 
592*c2c66affSColin Finck     NtClose(Handle);
593*c2c66affSColin Finck 
594*c2c66affSColin Finck     return Status;
595*c2c66affSColin Finck }
596*c2c66affSColin Finck #endif //NT_NATIVE_MODE
597