Blame src/fs/ntapi_tt_stat.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
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
	__out	nt_stat *		stat,
d8b407
	__in	uint32_t		infolen,
dd89bb
	__out	uintptr_t *		buffer,
dd89bb
	__in	uint32_t		buffer_size,
dd89bb
	__in	uint32_t		flags)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
	nt_iosb			iosb;
dd89bb
	nt_unicode_string *	sdev;
dd89bb
	nt_fai *		fai;
0cd9ef
	wchar16_t *		wch;
0cd9ef
	wchar16_t *		wch_mark;
7bb8ac
	wchar16_t *		wch_cap;
0cd9ef
	uint32_t		hash;
d8b407
	uint32_t		written;
dd89bb
dd89bb
	/* validation */
d8b407
	if (infolen < sizeof(*stat))
d8b407
		return NT_STATUS_INFO_LENGTH_MISMATCH;
dd89bb
d8b407
	/* init */
d8b407
	__ntapi->tt_aligned_block_memset(
d8b407
		stat,0,sizeof(*stat));
dd89bb
d8b407
        stat->hfile           = hfile;
e47608
        stat->dev_name_maxlen = (uint16_t)infolen - sizeof(*stat);
dd89bb
85b392
	/* sigh */
85b392
	if (buffer_size >= 65536)
85b392
		buffer_size = 65535;
85b392
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,
a73815
		__offsetof(nt_fai,name_info));
dd89bb
d8b407
	/* record volume-relative file name length */
921d0b
	fai = (nt_fai *)buffer;
dd89bb
	stat->file_name_length = fai->name_info.file_name_length;
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,
d8b407
			&written)))
dd89bb
		return status;
dd89bb
dd89bb
	sdev = (nt_unicode_string *)buffer;
0cd9ef
	wch  = sdev->buffer;
0cd9ef
0cd9ef
	if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
d8b407
		return NT_STATUS_NOT_SUPPORTED;
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] != '\\'))
d8b407
		return NT_STATUS_NOT_SUPPORTED;
0cd9ef
0cd9ef
	if ((sdev->strlen >= __DEVICE_MUP_PREFIX_LEN)
0cd9ef
			&& (wch[8]=='M')
0cd9ef
			&& (wch[9]=='u')
0cd9ef
			&& (wch[10]=='p')
0cd9ef
			&& (wch[11]=='\\')) {
d8b407
		hash = __DEVICE_MUP_PREFIX_HASH;
921d0b
		stat->flags |= NT_STAT_MUP_DEVICE;
fd484f
d8b407
		wch_mark = &wch[12];
d8b407
		wch      = wch_mark;
d8b407
d8b407
		for (; *wch!='\\'; )
d8b407
			wch++;
fd484f
		wch++;
0cd9ef
	} else {
0cd9ef
		wch_mark = &wch[8];
fd484f
		wch      = wch_mark;
0cd9ef
		hash     = __DEVICE_PATH_PREFIX_HASH;
0cd9ef
	}
0cd9ef
7bb8ac
	wch_cap = sdev->buffer + (sdev->strlen / sizeof(wchar16_t));
7bb8ac
7bb8ac
	for (; wch
fd484f
		wch++;
dd89bb
1d3e99
	stat->obj_name_strlen = sdev->strlen;
1d3e99
	stat->obj_name_maxlen = stat->dev_name_maxlen;
d8b407
	stat->dev_name_strlen = sizeof(uint16_t) * (uint16_t)(wch - sdev->buffer);
0cd9ef
	stat->dev_name_hash   = __ntapi->tt_buffer_crc32(
0cd9ef
					hash,wch_mark,
d8b407
					sizeof(wchar16_t) * (wch - wch_mark));
dd89bb
31ee70
	if (!(flags & NT_STAT_DEV_NAME_COPY))
921d0b
		return NT_STATUS_SUCCESS;
31ee70
	else if (stat->dev_name_maxlen < sdev->strlen)
921d0b
		return NT_STATUS_BUFFER_TOO_SMALL;
921d0b
921d0b
	__ntapi->tt_memcpy_utf16(
921d0b
		(wchar16_t *)stat->dev_name,
921d0b
		(wchar16_t *)sdev->buffer,
31ee70
		sdev->strlen);
dd89bb
921d0b
	return NT_STATUS_SUCCESS;
dd89bb
}