|
|
dd89bb |
/********************************************************/
|
|
|
dd89bb |
/* ntapi: Native API core library */
|
|
|
4256e2 |
/* Copyright (C) 2013--2016 Z. Gilboa */
|
|
|
dd89bb |
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
|
|
|
dd89bb |
/********************************************************/
|
|
|
dd89bb |
|
|
|
dd89bb |
#include <ntapi/ntapi.h>
|
|
|
dd89bb |
#include <ntapi/nt_file.h>
|
|
|
dd89bb |
#include <ntapi/nt_fsctl.h>
|
|
|
dd89bb |
#include <ntapi/nt_mount.h>
|
|
|
dd89bb |
#include <ntapi/nt_stat.h>
|
|
|
dd89bb |
#include "ntapi_impl.h"
|
|
|
dd89bb |
|
|
|
dd89bb |
int32_t __stdcall __ntapi_tt_stat(
|
|
|
dd89bb |
__in void * hfile,
|
|
|
dd89bb |
__in void * hroot __optional,
|
|
|
dd89bb |
__in nt_unicode_string * path,
|
|
|
dd89bb |
__out nt_stat * stat,
|
|
|
dd89bb |
__out uintptr_t * buffer,
|
|
|
dd89bb |
__in uint32_t buffer_size,
|
|
|
dd89bb |
__in uint32_t open_options,
|
|
|
dd89bb |
__in uint32_t flags)
|
|
|
dd89bb |
{
|
|
|
dd89bb |
int32_t status;
|
|
|
dd89bb |
nt_oa oa;
|
|
|
dd89bb |
nt_iosb iosb;
|
|
|
dd89bb |
nt_unicode_string * sdev;
|
|
|
dd89bb |
nt_fai * fai;
|
|
|
0cd9ef |
wchar16_t * wch;
|
|
|
0cd9ef |
wchar16_t * wch_mark;
|
|
|
0cd9ef |
uint32_t hash;
|
|
|
dd89bb |
|
|
|
dd89bb |
/* validation */
|
|
|
dd89bb |
if (!hfile && !path)
|
|
|
921d0b |
return NT_STATUS_INVALID_PARAMETER;
|
|
|
dd89bb |
|
|
|
dd89bb |
/* hfile */
|
|
|
921d0b |
if (hfile) {
|
|
|
921d0b |
stat->flags = 0;
|
|
|
921d0b |
} else {
|
|
|
dd89bb |
/* oa */
|
|
|
dd89bb |
oa.len = sizeof(nt_oa);
|
|
|
dd89bb |
oa.root_dir = hroot;
|
|
|
dd89bb |
oa.obj_name = path;
|
|
|
dd89bb |
oa.obj_attr = 0;
|
|
|
dd89bb |
oa.sec_desc = 0;
|
|
|
dd89bb |
oa.sec_qos = 0;
|
|
|
dd89bb |
|
|
|
dd89bb |
/* open file/folder */
|
|
|
921d0b |
if ((status = __ntapi->zw_open_file(
|
|
|
921d0b |
&hfile,
|
|
|
921d0b |
NT_SEC_SYNCHRONIZE
|
|
|
921d0b |
| NT_FILE_READ_ATTRIBUTES
|
|
|
921d0b |
| NT_FILE_READ_ACCESS,
|
|
|
921d0b |
&oa,
|
|
|
921d0b |
&iosb,
|
|
|
921d0b |
NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
|
|
|
921d0b |
open_options | NT_FILE_SYNCHRONOUS_IO_ALERT)))
|
|
|
dd89bb |
return status;
|
|
|
dd89bb |
|
|
|
921d0b |
stat->flags = NT_STAT_NEW_HANDLE;
|
|
|
dd89bb |
}
|
|
|
dd89bb |
|
|
|
921d0b |
stat->hfile = hfile;
|
|
|
dd89bb |
|
|
|
dd89bb |
/* system-unique device name */
|
|
|
921d0b |
if ((status = __ntapi->zw_query_information_file(
|
|
|
921d0b |
hfile,
|
|
|
921d0b |
&iosb,
|
|
|
921d0b |
buffer,
|
|
|
921d0b |
buffer_size,
|
|
|
921d0b |
NT_FILE_ALL_INFORMATION)))
|
|
|
dd89bb |
return status;
|
|
|
dd89bb |
|
|
|
dd89bb |
/* copy file info minus name */
|
|
|
dd89bb |
__ntapi->tt_aligned_block_memcpy(
|
|
|
dd89bb |
(uintptr_t *)stat,
|
|
|
921d0b |
(uintptr_t *)buffer,
|
|
|
dd89bb |
((size_t)(&((nt_fai *)0)->name_info)));
|
|
|
dd89bb |
|
|
|
dd89bb |
/* record the file name length, but do not hash */
|
|
|
921d0b |
fai = (nt_fai *)buffer;
|
|
|
dd89bb |
stat->file_name_length = fai->name_info.file_name_length;
|
|
|
dd89bb |
stat->file_name_hash = 0;
|
|
|
dd89bb |
|
|
|
dd89bb |
|
|
|
dd89bb |
/* file system size information */
|
|
|
921d0b |
if ((status = __ntapi->zw_query_volume_information_file(
|
|
|
921d0b |
hfile,
|
|
|
921d0b |
&iosb,
|
|
|
921d0b |
&(stat->fssi),
|
|
|
921d0b |
sizeof(stat->fssi),
|
|
|
921d0b |
NT_FILE_FS_SIZE_INFORMATION)))
|
|
|
dd89bb |
return status;
|
|
|
dd89bb |
|
|
|
0cd9ef |
/* system-unique device name */
|
|
|
0cd9ef |
if ((status = __ntapi->zw_query_object(
|
|
|
0cd9ef |
hfile,
|
|
|
0cd9ef |
NT_OBJECT_NAME_INFORMATION,
|
|
|
0cd9ef |
buffer,
|
|
|
0cd9ef |
buffer_size,
|
|
|
0cd9ef |
(uint32_t *)&iosb.info)))
|
|
|
dd89bb |
return status;
|
|
|
dd89bb |
|
|
|
dd89bb |
sdev = (nt_unicode_string *)buffer;
|
|
|
0cd9ef |
wch = sdev->buffer;
|
|
|
0cd9ef |
|
|
|
0cd9ef |
if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
|
|
|
0cd9ef |
return NT_STATUS_INVALID_HANDLE;
|
|
|
0cd9ef |
|
|
|
0cd9ef |
if ((wch[0] != '\\')
|
|
|
0cd9ef |
|| (wch[1] != 'D')
|
|
|
0cd9ef |
|| (wch[2] != 'e')
|
|
|
0cd9ef |
|| (wch[3] != 'v')
|
|
|
0cd9ef |
|| (wch[4] != 'i')
|
|
|
0cd9ef |
|| (wch[5] != 'c')
|
|
|
0cd9ef |
|| (wch[6] != 'e')
|
|
|
0cd9ef |
|| (wch[7] != '\\'))
|
|
|
0cd9ef |
return NT_STATUS_INVALID_HANDLE;
|
|
|
0cd9ef |
|
|
|
0cd9ef |
if ((sdev->strlen >= __DEVICE_MUP_PREFIX_LEN)
|
|
|
0cd9ef |
&& (wch[8]=='M')
|
|
|
0cd9ef |
&& (wch[9]=='u')
|
|
|
0cd9ef |
&& (wch[10]=='p')
|
|
|
0cd9ef |
&& (wch[11]=='\\')) {
|
|
|
0cd9ef |
wch_mark = &wch[12];
|
|
|
0cd9ef |
hash = __DEVICE_MUP_PREFIX_HASH;
|
|
|
921d0b |
stat->flags |= NT_STAT_MUP_DEVICE;
|
|
|
0cd9ef |
} else {
|
|
|
0cd9ef |
wch_mark = &wch[8];
|
|
|
0cd9ef |
hash = __DEVICE_PATH_PREFIX_HASH;
|
|
|
0cd9ef |
}
|
|
|
0cd9ef |
|
|
|
0cd9ef |
for (wch=wch_mark; *wch!='\\'; wch++)
|
|
|
0cd9ef |
(void)0;
|
|
|
dd89bb |
|
|
|
0cd9ef |
stat->dev_name_strlen = (uint16_t)((wch - sdev->buffer) * sizeof(uint16_t));
|
|
|
0cd9ef |
stat->dev_name_hash = __ntapi->tt_buffer_crc32(
|
|
|
0cd9ef |
hash,wch_mark,
|
|
|
0cd9ef |
sizeof(wchar16_t)*(wch-wch_mark));
|
|
|
dd89bb |
|
|
|
921d0b |
if (!(flags & NT_STAT_DEV_NAME_COPY)) {
|
|
|
dd89bb |
*stat->dev_name = 0;
|
|
|
921d0b |
return NT_STATUS_SUCCESS;
|
|
|
921d0b |
} else if (stat->dev_name_maxlen < sdev->strlen) {
|
|
|
921d0b |
*stat->dev_name = 0;
|
|
|
921d0b |
return NT_STATUS_BUFFER_TOO_SMALL;
|
|
|
921d0b |
}
|
|
|
921d0b |
|
|
|
921d0b |
__ntapi->tt_memcpy_utf16(
|
|
|
921d0b |
(wchar16_t *)stat->dev_name,
|
|
|
921d0b |
(wchar16_t *)sdev->buffer,
|
|
|
921d0b |
stat->dev_name_strlen);
|
|
|
dd89bb |
|
|
|
921d0b |
return NT_STATUS_SUCCESS;
|
|
|
dd89bb |
}
|