Blame src/port/ntapi_port_name_helper.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 <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_guid.h>
dd89bb
#include <ntapi/nt_port.h>
dd89bb
#include <ntapi/nt_string.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
typedef wchar16_t __port_service_prefix[6];
dd89bb
dd89bb
static const __port_service_prefix	__port_service_null = {0};
dd89bb
static const __port_service_prefix	__port_service_prefixes[4][NT_PORT_TYPE_CAP][NT_PORT_SUBTYPE_CAP] = {
dd89bb
					{{{'s','v','c','a','n','y'}}},
dd89bb
					{{{'n','t','c','t','t','y'}}},
dd89bb
					{{{'v','m','o','u','n','t'}}},
dd89bb
					{{{'d','a','e','m','o','n'}}}};
dd89bb
dd89bb
static const nt_guid		__port_guids[NT_PORT_TYPE_CAP][NT_PORT_SUBTYPE_CAP] = {
dd89bb
					{NT_PORT_GUID_DEFAULT},
dd89bb
					{NT_PORT_GUID_SUBSYSTEM},
dd89bb
					{NT_PORT_GUID_VMOUNT},
dd89bb
					{NT_PORT_GUID_DAEMON}};
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_port_guid_from_type(
dd89bb
	__out	nt_guid *		guid,
dd89bb
	__in	nt_port_type		type,
dd89bb
	__in	nt_port_subtype		subtype)
dd89bb
{
dd89bb
	const nt_guid *	src_guid;
dd89bb
dd89bb
	if ((type >= NT_PORT_TYPE_CAP) || (subtype >= NT_PORT_SUBTYPE_CAP))
dd89bb
		return NT_STATUS_INVALID_PARAMETER;
dd89bb
dd89bb
	src_guid = &(__port_guids[type][subtype]);
dd89bb
dd89bb
	__ntapi->tt_aligned_block_memcpy(
dd89bb
		(uintptr_t *)guid,
dd89bb
		(uintptr_t *)src_guid,
dd89bb
		sizeof(nt_guid));
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_port_type_from_guid(
dd89bb
	__out	nt_port_type *		type,
dd89bb
	__out	nt_port_subtype *	subtype,
dd89bb
	__in	nt_guid *		guid)
dd89bb
{
dd89bb
	int		itype;
dd89bb
	int		isubtype;
dd89bb
	const nt_guid *	src_guid;
dd89bb
	uint32_t	guid_hash;
dd89bb
	uint32_t	src_hash;
dd89bb
dd89bb
	guid_hash = __ntapi->tt_buffer_crc32(0,guid,sizeof(nt_guid));
dd89bb
dd89bb
	for (itype=0; itype
dd89bb
		for (isubtype=0; isubtype
dd89bb
			src_guid = &(__port_guids[itype][isubtype]);
dd89bb
			src_hash = __ntapi->tt_buffer_crc32(0,src_guid,sizeof(nt_guid));
dd89bb
dd89bb
			if (guid_hash == src_hash) {
dd89bb
				*type	 = (nt_port_type)itype;
dd89bb
				*subtype = (nt_port_subtype)isubtype;
dd89bb
dd89bb
				return NT_STATUS_SUCCESS;
dd89bb
			}
dd89bb
		}
dd89bb
	}
dd89bb
dd89bb
	return NT_STATUS_INVALID_PARAMETER;
dd89bb
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_port_generate_keys(
dd89bb
	__out	nt_port_keys *		keys)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
	nt_large_integer	systime;
dd89bb
	nt_luid			luid;
dd89bb
dd89bb
	status = __ntapi->zw_query_system_time(&systime);
dd89bb
	if (status) return status;
dd89bb
dd89bb
	status = __ntapi->zw_allocate_locally_unique_id(&luid);
dd89bb
	if (status) return status;
dd89bb
dd89bb
	keys->key[0] = pe_get_current_process_id();
dd89bb
	keys->key[1] = pe_get_current_thread_id();
dd89bb
	keys->key[2] = systime.ihigh;
dd89bb
	keys->key[3] = systime.ulow;
dd89bb
	keys->key[4] = luid.high;
dd89bb
	keys->key[5] = luid.low;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
dd89bb
void __stdcall	__ntapi_tt_port_format_keys(
dd89bb
	__in	nt_port_keys *		keys,
dd89bb
	__out	nt_port_name_keys *	name_keys)
dd89bb
{
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[0],name_keys->key_1st);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[1],name_keys->key_2nd);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[2],name_keys->key_3rd);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[3],name_keys->key_4th);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[4],name_keys->key_5th);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(keys->key[5],name_keys->key_6th);
dd89bb
dd89bb
	return;
dd89bb
}
dd89bb
dd89bb
dd89bb
void __stdcall __ntapi_tt_port_name_from_attributes(
dd89bb
	__out	nt_port_name *		name,
dd89bb
	__in	nt_port_attr *		attr)
dd89bb
{
dd89bb
	wchar16_t bno[] = __NT_BASED_NAMED_OBJECTS;
dd89bb
dd89bb
	/* base named objects */
dd89bb
	__ntapi->tt_memcpy_utf16(
dd89bb
		name->base_named_objects,
dd89bb
		bno,sizeof(bno));
dd89bb
dd89bb
	/* service prefix */
dd89bb
	if (attr && (attr->type < NT_PORT_TYPE_CAP) && (attr->subtype < NT_PORT_SUBTYPE_CAP))
dd89bb
		__ntapi->tt_memcpy_utf16(
dd89bb
			name->svc_prefix,
dd89bb
			&(__port_service_prefixes[attr->type][attr->subtype][0][0]),
dd89bb
			sizeof(name->svc_prefix));
dd89bb
	else
dd89bb
		__ntapi->tt_memcpy_utf16(
dd89bb
			name->svc_prefix,
dd89bb
			__port_service_null,
dd89bb
			sizeof(name->svc_prefix));
dd89bb
dd89bb
	/* port guid */
dd89bb
	__ntapi->tt_guid_to_utf16_string(
dd89bb
		&attr->guid,
dd89bb
		(nt_guid_str_utf16 *)&name->port_guid);
dd89bb
dd89bb
	/* port name keys */
dd89bb
	__ntapi_tt_port_format_keys(
dd89bb
		&attr->keys,
dd89bb
		&name->port_name_keys);
dd89bb
dd89bb
	/* backslash and underscores */
dd89bb
	name->backslash = '\\';
dd89bb
	name->port_guid.uscore_guid = '_';
dd89bb
	name->port_guid.uscore_keys = '_';
dd89bb
	name->port_name_keys.uscore_1st  = '_';
dd89bb
	name->port_name_keys.uscore_2nd  = '_';
dd89bb
	name->port_name_keys.uscore_3rd  = '_';
dd89bb
	name->port_name_keys.uscore_4th  = '_';
dd89bb
	name->port_name_keys.uscore_5th  = '_';
dd89bb
dd89bb
	/* null termination */
dd89bb
	name->null_termination = 0;
dd89bb
dd89bb
	return;
dd89bb
}