Blame src/internal/toksvc_open_impl.c

2a7aec
/*********************************************************/
2a7aec
/*  toksvc: a framework-native token broker service      */
2a7aec
/*  Copyright (C) 2020  Z. Gilboa                        */
2a7aec
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
2a7aec
/*********************************************************/
2a7aec
2a7aec
#include <psxtypes/psxtypes.h>
2a7aec
#include <ntapi/ntapi.h>
2a7aec
2a7aec
#include <toksvc/toksvc.h>
2a7aec
#include "toksvc_driver_impl.h"
2a7aec
2a7aec
static int32_t toks_open(
2a7aec
	void **		hfile,
2a7aec
	void *		hat,
2a7aec
	const char *	arg,
2a7aec
	uint32_t	options,
2a7aec
	bool		fprivate)
2a7aec
{
2a7aec
	int32_t						status;
2a7aec
	nt_oa						oa;
2a7aec
	nt_iosb						iosb;
2a7aec
	nt_unicode_string				path;
2a7aec
	nt_unicode_conversion_params_utf8_to_utf16	params = {0,0,0,0,0,0,0,0,0};
2a7aec
	wchar16_t					buffer[4096];
2a7aec
	wchar16_t *					wch;
2a7aec
	size_t						nbytes;
2a7aec
2a7aec
	/* utf-8 --> utf-16 */
2a7aec
	params.src		= (const unsigned char *)arg;
2a7aec
	params.src_size_in_bytes= ntapi->tt_string_null_offset_multibyte(arg);
2a7aec
	params.dst		= buffer;
2a7aec
	params.dst_size_in_bytes= sizeof(buffer);
2a7aec
2a7aec
	if ((status = ntapi->uc_convert_unicode_stream_utf8_to_utf16(&params)))
2a7aec
		return status;
2a7aec
2a7aec
	/* convenience */
2a7aec
	for (wch=buffer, nbytes=params.bytes_written; nbytes; ) {
2a7aec
		if (*wch == '/')
2a7aec
			*wch = '\\';
2a7aec
2a7aec
		nbytes -= sizeof(wchar16_t);
2a7aec
		wch++;
2a7aec
	}
2a7aec
2a7aec
	/* path */
2a7aec
	path.maxlen = 0;
2a7aec
	path.strlen = (uint16_t)params.bytes_written;
2a7aec
	path.buffer = buffer;
2a7aec
2a7aec
	/* oa */
2a7aec
	oa.len      = sizeof(nt_oa);
2a7aec
	oa.root_dir = (buffer[0]=='\\') ? 0 : hat;
2a7aec
	oa.obj_name = &pat;;
2a7aec
	oa.obj_attr = fprivate ? 0 : NT_OBJ_INHERIT;
2a7aec
	oa.sec_desc = 0;
2a7aec
	oa.sec_qos  = 0;
2a7aec
2a7aec
	/* open */
2a7aec
	return ntapi->zw_open_file(
2a7aec
		hfile,
2a7aec
		NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_DATA,
2a7aec
		&oa,&iosb,
2a7aec
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE,
2a7aec
		options | NT_FILE_SYNCHRONOUS_IO_ALERT);
2a7aec
}
2a7aec
2a7aec
static int32_t toks_open_volume(
2a7aec
	void **		hfile,
2a7aec
	void *		hat,
2a7aec
	const char *	arg,
2a7aec
	uint32_t	options,
2a7aec
	bool		fprivate)
2a7aec
{
2a7aec
	int32_t   status;
2a7aec
	void *    hvolume = 0;
2a7aec
	wchar16_t drive   = arg[0];
2a7aec
2a7aec
	if ((arg[1]==':') && ((arg[2]=='\\') || (arg[2]=='/'))) {
2a7aec
		if ((status = ntapi->tt_get_dos_drive_root_handle(
2a7aec
				&hvolume,drive)))
2a7aec
			return status;
2a7aec
2a7aec
		hat = hvolume;
2a7aec
		arg = &arg[3];
2a7aec
	}
2a7aec
2a7aec
	status = toks_open(
2a7aec
		hfile,hat,arg,
2a7aec
		options,fprivate);
2a7aec
2a7aec
	if (hvolume)
2a7aec
		ntapi->zw_close(hvolume);
2a7aec
2a7aec
	return status;
2a7aec
}
2a7aec
2a7aec
int32_t toks_open_file(void ** hfile, void * hat, const char * arg, bool fprivate)
2a7aec
{
2a7aec
	return toks_open_volume(hfile,hat,arg,NT_FILE_NON_DIRECTORY_FILE,fprivate);
2a7aec
}
2a7aec
2a7aec
int32_t toks_open_dir(void ** hfile, void * hat, const char * arg, bool fprivate)
2a7aec
{
2a7aec
	return toks_open_volume(hfile,hat,arg,NT_FILE_DIRECTORY_FILE,fprivate);
2a7aec
}