Blame src/fs/ntapi_tt_istat.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_istat.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_istat(
dd89bb
	__in	void *			hfile,
dd89bb
	__out	nt_istat *		istat,
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
dd89bb
	nt_iosb			iosb;
dd89bb
	nt_unicode_string *	sdev;
dd89bb
	wchar16_t *		wch;
dd89bb
	wchar16_t *		wch_mark;
7bb8ac
	wchar16_t *		wch_cap;
030926
	uint32_t		hash;
d8b407
	uint32_t		written;
b061d6
dd89bb
	/* validaton */
d8b407
	if (infolen < sizeof(*istat))
d8b407
		return NT_STATUS_INFO_LENGTH_MISMATCH;
dd89bb
d8b407
	/* init */
d8b407
	__ntapi->tt_aligned_block_memset(
d8b407
		istat,0,sizeof(*istat));
dd89bb
d8b407
	istat->hfile           = hfile;
e47608
	istat->dev_name_maxlen = (uint16_t)infolen - sizeof(*istat);
dd89bb
85b392
	/* sigh */
85b392
	if (buffer_size >= 65536)
85b392
		buffer_size = 65535;
85b392
dd89bb
	/* file index number */
576543
	if (!(flags & NT_ISTAT_DEV_NAME_ONLY))
576543
		if ((status = __ntapi->zw_query_information_file(
576543
				hfile,
576543
				&iosb,
576543
				&istat->fii,
576543
				sizeof(istat->fii),
576543
				NT_FILE_INTERNAL_INFORMATION)))
576543
			return status;
dd89bb
dd89bb
	/* attributes & reparse tag information */
576543
	if (!(flags & NT_ISTAT_DEV_NAME_ONLY))
576543
		if ((status = __ntapi->zw_query_information_file(
576543
				hfile,
576543
				&iosb,
576543
				&istat->ftagi,
576543
				sizeof(istat->ftagi),
576543
				NT_FILE_ATTRIBUTE_TAG_INFORMATION)))
576543
			return status;
dd89bb
dd89bb
	/* system-unique device name */
900289
	if ((status = __ntapi->zw_query_object(
900289
			hfile,
900289
			NT_OBJECT_NAME_INFORMATION,
900289
			buffer,
900289
			buffer_size,
d8b407
			&written)))
dd89bb
		return status;
dd89bb
dd89bb
	sdev = (nt_unicode_string *)buffer;
900289
	wch  = sdev->buffer;
dd89bb
dd89bb
	if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
d8b407
		return NT_STATUS_NOT_SUPPORTED;
dd89bb
900289
	if ((wch[0] != '\\')
900289
			|| (wch[1] != 'D')
900289
			|| (wch[2] != 'e')
900289
			|| (wch[3] != 'v')
900289
			|| (wch[4] != 'i')
900289
			|| (wch[5] != 'c')
900289
			|| (wch[6] != 'e')
900289
			|| (wch[7] != '\\'))
d8b407
		return NT_STATUS_NOT_SUPPORTED;
dd89bb
030926
	if ((sdev->strlen >= __DEVICE_MUP_PREFIX_LEN)
030926
			&& (wch[8]=='M')
030926
			&& (wch[9]=='u')
030926
			&& (wch[10]=='p')
030926
			&& (wch[11]=='\\')) {
d8b407
		hash = __DEVICE_MUP_PREFIX_HASH;
b061d6
		istat->flags |= NT_STATFS_MUP_DEVICE;
fd484f
d8b407
		wch_mark = &wch[12];
d8b407
		wch      = wch_mark;
d8b407
d8b407
		for (; *wch!='\\'; )
d8b407
			wch++;
fd484f
		wch++;
030926
	} else {
030926
		wch_mark = &wch[8];
fd484f
		wch      = wch_mark;
030926
		hash     = __DEVICE_PATH_PREFIX_HASH;
030926
	}
dd89bb
7bb8ac
	wch_cap = sdev->buffer + (sdev->strlen / sizeof(wchar16_t));
7bb8ac
7bb8ac
	for (; wch
fd484f
		wch++;
900289
1d3e99
	istat->obj_name_strlen = sdev->strlen;
1d3e99
	istat->obj_name_maxlen = istat->dev_name_maxlen;
d8b407
	istat->dev_name_strlen = sizeof(uint16_t) * (uint16_t)(wch - sdev->buffer);
900289
	istat->dev_name_hash   = __ntapi->tt_buffer_crc32(
1d3e99
					hash,wch_mark,
d8b407
					sizeof(wchar16_t) * (wch - wch_mark));
dd89bb
31ee70
	if (!(flags & NT_ISTAT_DEV_NAME_COPY))
fc83d8
		return NT_STATUS_SUCCESS;
31ee70
	else if (istat->dev_name_maxlen < istat->dev_name_strlen)
fc83d8
		return NT_STATUS_BUFFER_TOO_SMALL;
fc83d8
fc83d8
	__ntapi->tt_memcpy_utf16(
fc83d8
		istat->dev_name,
fc83d8
		sdev->buffer,
fc83d8
		istat->dev_name_strlen);
fc83d8
dd89bb
	return status;
dd89bb
}