summaryrefslogtreecommitdiffhomepage
path: root/src/fs/ntapi_tt_stat.c
blob: cab84c3418a0e39be2e30e5a544d3488ae607a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <ntapi/ntapi.h>
#include <ntapi/nt_file.h>
#include <ntapi/nt_fsctl.h>
#include <ntapi/nt_mount.h>
#include <ntapi/nt_stat.h>
#include "ntapi_impl.h"

int32_t __stdcall __ntapi_tt_stat(
	__in	void *			hfile,
	__out	nt_stat *		stat,
	__in	uint32_t		infolen,
	__out	uintptr_t *		buffer,
	__in	uint32_t		buffer_size,
	__in	uint32_t		flags)
{
	int32_t			status;
	nt_iosb			iosb;
	nt_unicode_string *	sdev;
	nt_fai *		fai;
	wchar16_t *		wch;
	wchar16_t *		wch_mark;
	uint32_t		hash;
	uint32_t		written;

	/* validation */
	if (infolen < sizeof(*stat))
		return NT_STATUS_INFO_LENGTH_MISMATCH;

	/* init */
	__ntapi->tt_aligned_block_memset(
		stat,0,sizeof(*stat));

        stat->hfile           = hfile;
        stat->dev_name_maxlen = (uint16_t)infolen - sizeof(*stat);

	/* sigh */
	if (buffer_size >= 65536)
		buffer_size = 65535;

	/* system-unique device name */
	if ((status = __ntapi->zw_query_information_file(
			hfile,
			&iosb,
			buffer,
			buffer_size,
			NT_FILE_ALL_INFORMATION)))
		return status;

	/* copy file info minus name */
	__ntapi->tt_aligned_block_memcpy(
		(uintptr_t *)stat,
		(uintptr_t *)buffer,
		((size_t)(&((nt_fai *)0)->name_info)));

	/* record volume-relative file name length */
	fai = (nt_fai *)buffer;
	stat->file_name_length = fai->name_info.file_name_length;

	/* file system size information */
	if ((status = __ntapi->zw_query_volume_information_file(
			hfile,
			&iosb,
			&(stat->fssi),
			sizeof(stat->fssi),
			NT_FILE_FS_SIZE_INFORMATION)))
		return status;

	/* system-unique device name */
	if ((status = __ntapi->zw_query_object(
			hfile,
			NT_OBJECT_NAME_INFORMATION,
			buffer,
			buffer_size,
			&written)))
		return status;

	sdev = (nt_unicode_string *)buffer;
	wch  = sdev->buffer;

	if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
		return NT_STATUS_NOT_SUPPORTED;

	if ((wch[0] != '\\')
			|| (wch[1] != 'D')
			|| (wch[2] != 'e')
			|| (wch[3] != 'v')
			|| (wch[4] != 'i')
			|| (wch[5] != 'c')
			|| (wch[6] != 'e')
			|| (wch[7] != '\\'))
		return NT_STATUS_NOT_SUPPORTED;

	if ((sdev->strlen >= __DEVICE_MUP_PREFIX_LEN)
			&& (wch[8]=='M')
			&& (wch[9]=='u')
			&& (wch[10]=='p')
			&& (wch[11]=='\\')) {
		hash = __DEVICE_MUP_PREFIX_HASH;
		stat->flags |= NT_STAT_MUP_DEVICE;

		wch_mark = &wch[12];
		wch      = wch_mark;

		for (; *wch!='\\'; )
			wch++;
		wch++;
	} else {
		wch_mark = &wch[8];
		wch      = wch_mark;
		hash     = __DEVICE_PATH_PREFIX_HASH;
	}

	for (; *wch!='\\'; )
		wch++;

	stat->obj_name_strlen = sdev->strlen;
	stat->obj_name_maxlen = stat->dev_name_maxlen;
	stat->dev_name_strlen = sizeof(uint16_t) * (uint16_t)(wch - sdev->buffer);
	stat->dev_name_hash   = __ntapi->tt_buffer_crc32(
					hash,wch_mark,
					sizeof(wchar16_t) * (wch - wch_mark));

	if (!(flags & NT_STAT_DEV_NAME_COPY))
		return NT_STATUS_SUCCESS;
	else if (stat->dev_name_maxlen < sdev->strlen)
		return NT_STATUS_BUFFER_TOO_SMALL;

	__ntapi->tt_memcpy_utf16(
		(wchar16_t *)stat->dev_name,
		(wchar16_t *)sdev->buffer,
		sdev->strlen);

	return NT_STATUS_SUCCESS;
}