summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ntapi_open.c
blob: a542ffaefc85d49f2c0fe6062524898385231ceb (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static int32_t __ntapi_tt_open_utf8(
	void **		hfile,
	void *		hat,
	const char *	arg,
	uint32_t	options,
	int		fprivate,
	wchar16_t *	buffer,
	uint32_t	buflen)
{
	int32_t						status;
	nt_oa						oa;
	nt_iosb						iosb;
	nt_unicode_string				path;
	nt_unicode_conversion_params_utf8_to_utf16	params = {0,0,0,0,0,0,0,0,0};
	wchar16_t *					wch;
	size_t						nbytes;

	/* utf-8 --> utf-16 */
	params.src		 = (const unsigned char *)arg;
	params.src_size_in_bytes = __ntapi->tt_string_null_offset_multibyte(arg);
	params.dst		 = buffer;
	params.dst_size_in_bytes = buflen;

	if ((status = __ntapi->uc_convert_unicode_stream_utf8_to_utf16(&params)))
		return status;

	/* convenience */
	for (wch=buffer, nbytes=params.bytes_written; nbytes; ) {
		if (*wch == '/')
			*wch = '\\';

		nbytes -= sizeof(wchar16_t);
		wch++;
	}

	/* path */
	path.maxlen = 0;
	path.strlen = (uint16_t)params.bytes_written;
	path.buffer = buffer;

	/* oa */
	oa.len      = sizeof(nt_oa);
	oa.root_dir = (buffer[0]=='\\') ? 0 : hat;
	oa.obj_name = &path;
	oa.obj_attr = fprivate ? 0 : NT_OBJ_INHERIT;
	oa.sec_desc = 0;
	oa.sec_qos  = 0;

	/* open */
	return __ntapi->zw_open_file(
		hfile,
		NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_DATA,
		&oa,&iosb,
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE,
		options | NT_FILE_SYNCHRONOUS_IO_ALERT);
}

int32_t __ntapi_tt_open_file_utf8(
	void **		hfile,
	void *		hat,
	const char *	arg,
	int		fprivate,
	wchar16_t *	buffer,
	uint32_t	buflen)
{
	return __ntapi_tt_open_utf8(
		hfile,hat,arg,
		NT_FILE_NON_DIRECTORY_FILE,
		fprivate,buffer,buflen);
}

int32_t __ntapi_tt_open_dir_utf8(
	void **		hfile,
	void *		hat,
	const char *	arg,
	int		fprivate,
	wchar16_t *	buffer,
	uint32_t	buflen)
{
	return __ntapi_tt_open_utf8(
		hfile,hat,arg,
		NT_FILE_DIRECTORY_FILE,
		fprivate,buffer,buflen);
}