Blame src/fs/ntapi_tt_istat.c

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_istat.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_istat(
dd89bb
	__in	void *			hfile,
dd89bb
	__in	void *			hroot	__optional,
dd89bb
	__in	nt_unicode_string *	path,
dd89bb
	__out	nt_istat *		istat,
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
dd89bb
	nt_oa			oa;
dd89bb
	nt_iosb			iosb;
dd89bb
	nt_unicode_string *	sdev;
dd89bb
	wchar16_t *		wch;
dd89bb
	wchar16_t *		wch_mark;
dd89bb
dd89bb
	/* validaton */
dd89bb
	if (!hfile && !path)
dd89bb
		return NT_STATUS_INVALID_HANDLE;
dd89bb
dd89bb
	/* hfile */
dd89bb
	if (!hfile) {
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 */
900289
		if ((status = __ntapi->zw_open_file(
900289
				&hfile,
900289
				NT_SEC_SYNCHRONIZE
900289
					| NT_FILE_READ_ATTRIBUTES
900289
					| NT_FILE_READ_ACCESS,
900289
				&oa,
900289
				&iosb,
900289
				NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
900289
				open_options | NT_FILE_SYNCHRONOUS_IO_ALERT)))
dd89bb
			return status;
dd89bb
dd89bb
		istat->flags_out = NT_STAT_NEW_HANDLE;
dd89bb
	}
dd89bb
dd89bb
	istat->hfile    = hfile;
dd89bb
	istat->flags_in = flags;
dd89bb
dd89bb
	/* file index number */
900289
	if ((status = __ntapi->zw_query_information_file(
900289
			hfile,
900289
			&iosb,
900289
			&istat->fii,
900289
			sizeof(istat->fii),
900289
			NT_FILE_INTERNAL_INFORMATION)))
dd89bb
		return status;
dd89bb
dd89bb
	/* attributes & reparse tag information */
900289
	if ((status = __ntapi->zw_query_information_file(
900289
			hfile,
900289
			&iosb,
900289
			&istat->ftagi,
900289
			sizeof(istat->ftagi),
900289
			NT_FILE_ATTRIBUTE_TAG_INFORMATION)))
dd89bb
		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,
900289
			(uint32_t *)&iosb.info)))
dd89bb
		return status;
dd89bb
dd89bb
	sdev = (nt_unicode_string *)buffer;
900289
	wch  = sdev->buffer;
dd89bb
dd89bb
	if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
dd89bb
		return NT_STATUS_INVALID_HANDLE;
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] != '\\'))
dd89bb
		return NT_STATUS_INVALID_HANDLE;
dd89bb
900289
	wch_mark = &wch[8];
900289
	wch      = wch_mark;
dd89bb
900289
	while (*wch != '\\')
900289
		wch++;
900289
900289
	istat->dev_name_strlen = (uint16_t)((wch - sdev->buffer) * sizeof(uint16_t));
900289
	istat->dev_name_hash   = __ntapi->tt_buffer_crc32(
900289
		__DEVICE_PATH_PREFIX_HASH,
dd89bb
		wch_mark,
dd89bb
		(uintptr_t)wch - (uintptr_t)wch_mark);
dd89bb
dd89bb
	return status;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_validate_fs_handle(
dd89bb
	__in	void *		hfile,
dd89bb
	__in	uint32_t	dev_name_hash,
dd89bb
	__in	nt_fii		fii,
dd89bb
	__out	uintptr_t *	buffer,
dd89bb
	__in	uint32_t	buffer_size)
dd89bb
{
dd89bb
	int32_t		status;
dd89bb
	nt_istat	istat;
dd89bb
dd89bb
	status = __ntapi->tt_istat(
dd89bb
		hfile,
dd89bb
		(void *)0,
dd89bb
		(nt_unicode_string *)0,
dd89bb
		&istat,
dd89bb
		buffer,
dd89bb
		buffer_size,
dd89bb
		0,
dd89bb
		NT_ISTAT_DEFAULT);
dd89bb
dd89bb
	if (status) return status;
dd89bb
dd89bb
	if (istat.fii.index_number.quad != fii.index_number.quad)
dd89bb
		return NT_STATUS_CONTEXT_MISMATCH;
dd89bb
	else if (istat.dev_name_hash != dev_name_hash)
dd89bb
		return NT_STATUS_CONTEXT_MISMATCH;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}