1 ////////////////////////////////////////////////////////////////////
2 // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
4 // This file was released under the GPLv2 on June 2015.
5 ////////////////////////////////////////////////////////////////////
12 /*typedef BOOLEAN (*PPsGetVersion) (
13 PULONG MajorVersion OPTIONAL,
14 PULONG MinorVersion OPTIONAL,
15 PULONG BuildNumber OPTIONAL,
16 PUNICODE_STRING CSDVersion OPTIONAL
19 //PPsGetVersion _PsGetVersion = PsGetVersion;
33 RtlInitUnicodeString(&uname, DllName);
36 // RC = LdrGetDllHandle(NULL, NULL, &uname, &h);
40 RtlInitAnsiString(&aname, ProcName);
42 // RC = LdrGetProcedureAddress(h, &aname, 0, ProcAddr);
49 PULONG MajorVersion OPTIONAL
,
50 PULONG MinorVersion OPTIONAL
,
51 PULONG BuildNumber OPTIONAL
,
52 PUNICODE_STRING CSDVersion OPTIONAL
56 ULONG mn
=0, mj
=0, bld
=0;
59 // return _PsGetVersion(MajorVersion, MinorVersion, BuildNumber, CSDVersion);
61 RtlZeroMemory(Str
, sizeof(Str
));
62 if(RegTGetStringValue(NULL
, L
"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
64 &Str
[0], sizeof(Str
)-sizeof(WCHAR
))) {
70 if(a
< '0' || a
> '9')
79 if(a
< '0' || a
> '9')
86 if(RegTGetStringValue(NULL
, L
"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
87 L
"CurrentBuildNumber",
88 &Str
[0], sizeof(Str
)-sizeof(WCHAR
))) {
92 if(a
< '0' || a
> '9')
94 bld
= bld
*10 + (a
-'0');
110 DWORD dwIoControlCode
,
114 DWORD nOutBufferSize
,
115 DWORD
* lpBytesReturned
,
121 BOOLEAN DevIoCtl
= TRUE
;
122 IO_STATUS_BLOCK Iosb
;
124 if ( dwIoControlCode
>> 16 == FILE_DEVICE_FILE_SYSTEM
) {
131 RC
= NtDeviceIoControlFile(
137 dwIoControlCode
, // IoControlCode
138 lpInBuffer
, // Buffer for data to the FS
140 lpOutBuffer
, // OutputBuffer for data from the FS
141 nOutBufferSize
// OutputBuffer Length
144 RC
= NtFsControlFile(
150 dwIoControlCode
, // IoControlCode
151 lpInBuffer
, // Buffer for data to the FS
153 lpOutBuffer
, // OutputBuffer for data from the FS
154 nOutBufferSize
// OutputBuffer Length
158 if ( RC
== STATUS_PENDING
) {
159 // Operation must complete before return & Iosb destroyed
160 RC
= NtWaitForSingleObject( h
, FALSE
, NULL
);
161 if ( NT_SUCCESS(RC
)) {
166 if ( NT_SUCCESS(RC
) ) {
167 *lpBytesReturned
= Iosb
.Information
;
170 // handle warning value STATUS_BUFFER_OVERFLOW somewhat correctly
171 if ( !NT_ERROR(RC
) ) {
172 *lpBytesReturned
= Iosb
.Information
;
183 LARGE_INTEGER delay
= {0,0};
184 delay
.QuadPart
= -10I64
*1000*t
;
185 NtDelayExecution(FALSE
, &delay
);
188 HANDLE hGlobalHeap
= NULL
;
197 // Initialize some heap
198 hGlobalHeap
= RtlCreateHeap( HEAP_GROWABLE
, // Flags
203 NULL
); // Parameters
204 if(!hGlobalHeap
|| hGlobalHeap
== (HANDLE
)(-1)) {
209 return RtlAllocateHeap( hGlobalHeap
, 0, Size
);
222 RtlFreeHeap( hGlobalHeap
, 0, Addr
);
226 CHAR dbg_print_tmp_buff
[2048];
227 WCHAR dbg_stringBuffer
[2048];
229 BOOLEAN was_enter
= TRUE
;
239 UNICODE_STRING msgBuff
;
241 va_start(ap
, DebugMessage
);
244 strcpy(&dbg_print_tmp_buff
[0], NT_DBG_PREFIX
);
245 len
= _vsnprintf(&dbg_print_tmp_buff
[sizeof(NT_DBG_PREFIX
)-1], 2047-sizeof(NT_DBG_PREFIX
), DebugMessage
, ap
);
247 len
= _vsnprintf(&dbg_print_tmp_buff
[0], 2047, DebugMessage
, ap
);
249 dbg_print_tmp_buff
[2047] = 0;
251 (dbg_print_tmp_buff
[len
-1] == '\n' ||
252 dbg_print_tmp_buff
[len
-1] == '\r') ) {
258 len
= swprintf( dbg_stringBuffer
, L
"%S", dbg_print_tmp_buff
);
259 msgBuff
.Buffer
= dbg_stringBuffer
;
260 msgBuff
.Length
= len
* sizeof(WCHAR
);
261 msgBuff
.MaximumLength
= msgBuff
.Length
+ sizeof(WCHAR
);
262 NtDisplayString( &msgBuff
);
266 } // end PrintNtConsole()
275 OBJECT_ATTRIBUTES ObjectAttributes
;
276 IO_STATUS_BLOCK IoStatus
;
278 UNICODE_STRING fName
;
280 RtlInitUnicodeString(&fName
, Name
);
282 InitializeObjectAttributes(&ObjectAttributes
, &fName
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
284 Status
= NtCreateFile(ph
,
285 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
,
289 FILE_ATTRIBUTE_NORMAL
,
290 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
292 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_COMPLETE_IF_OPLOCKED
/*| FILE_WRITE_THROUGH*/,
297 } // end EnvFileOpenW()
312 NameW
= (PWCHAR
)MyAllocatePool__(NonPagedPool
, (len
+1)*sizeof(WCHAR
));
314 return STATUS_INSUFFICIENT_RESOURCES
;
316 swprintf(NameW
, L
"%S", Name
);
318 Status
= EnvFileOpenW(NameW
, ph
);
323 } // end EnvFileOpenA()
331 return NtClose(hFile
);
332 } // end EnvFileClose()
336 EnvFileGetSizeByHandle(
342 IO_STATUS_BLOCK IoStatusBlock
;
343 FILE_STANDARD_INFORMATION StandardInfo
;
345 Status
= NtQueryInformationFile(
349 sizeof(StandardInfo
),
350 FileStandardInformation
352 if (NT_SUCCESS(Status
)) {
353 *lpFileSize
= StandardInfo
.EndOfFile
.QuadPart
;
356 } // end EnvFileGetSizeByHandle()
368 (*lpFileSize
) = -1I64
;
370 Status
= EnvFileOpenA(Name
, &hFile
);
372 if(!NT_SUCCESS(Status
))
375 Status
= EnvFileGetSizeByHandle(hFile
, lpFileSize
);
380 } // end EnvFileGetSizeA()
392 (*lpFileSize
) = -1I64
;
394 Status
= EnvFileOpenW(Name
, &hFile
);
396 if(!NT_SUCCESS(Status
))
399 Status
= EnvFileGetSizeByHandle(hFile
, lpFileSize
);
404 } // end EnvFileGetSizeW()
408 EnvFileExistsA(PCHAR Name
) {
410 EnvFileGetSizeA(Name
, &Size
);
416 EnvFileExistsW(PWCHAR Name
) {
418 EnvFileGetSizeW(Name
, &Size
);
431 IO_STATUS_BLOCK IoStatus
;
434 Status
= NtWriteFile(
445 (*bytesWritten
) = IoStatus
.Information
;
448 } // end EnvFileWrite()
459 IO_STATUS_BLOCK IoStatus
;
473 (*bytesRead
) = IoStatus
.Information
;
476 } // end EnvFileRead()
482 LONGLONG lDistanceToMove
,
483 LONGLONG
* lResultPointer
,
488 IO_STATUS_BLOCK IoStatus
;
489 FILE_POSITION_INFORMATION CurrentPosition
;
490 FILE_STANDARD_INFORMATION FileInfo
;
492 switch (dwMoveMethod
) {
493 case ENV_FILE_BEGIN
:
494 CurrentPosition
.CurrentByteOffset
.QuadPart
= lDistanceToMove
;
497 case ENV_FILE_CURRENT
:
499 // Get the current position of the file pointer
500 Status
= NtQueryInformationFile(
504 sizeof(CurrentPosition
),
505 FilePositionInformation
507 if(!NT_SUCCESS(Status
)) {
510 CurrentPosition
.CurrentByteOffset
.QuadPart
+= lDistanceToMove
;
514 Status
= NtQueryInformationFile(
519 FileStandardInformation
521 if (!NT_SUCCESS(Status
)) {
524 CurrentPosition
.CurrentByteOffset
.QuadPart
=
525 FileInfo
.EndOfFile
.QuadPart
+ lDistanceToMove
;
529 return STATUS_INVALID_PARAMETER
;
532 if ( CurrentPosition
.CurrentByteOffset
.QuadPart
< 0 ) {
536 Status
= NtSetInformationFile(
540 sizeof(CurrentPosition
),
541 FilePositionInformation
544 if(!NT_SUCCESS(Status
)) {
548 *lResultPointer
= CurrentPosition
.CurrentByteOffset
.QuadPart
;
550 return STATUS_SUCCESS
;
551 } // end EnvFileSetPointer()
553 NTSTATUS
EnvFileDeleteW(PWCHAR Name
) {
555 OBJECT_ATTRIBUTES ObjectAttributes
;
556 IO_STATUS_BLOCK IoStatus
;
558 UNICODE_STRING fName
;
560 FILE_DISPOSITION_INFORMATION Disposition
;
562 RtlInitUnicodeString(&fName
, Name
);
564 InitializeObjectAttributes(&ObjectAttributes
, &fName
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
574 FILE_NON_DIRECTORY_FILE
| FILE_OPEN_FOR_BACKUP_INTENT
578 if ( !NT_SUCCESS(Status
) ) {
582 Disposition
.DeleteFile
= TRUE
;
584 Status
= NtSetInformationFile(
589 FileDispositionInformation
596 #endif //NT_NATIVE_MODE