Blob Blame History Raw
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

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


void __fastcall __ntapi_tt_guid_copy(
	__out	nt_guid *	pguid_dst,
	__in	const nt_guid *	pguid_src)
{
	uint64_t *	dst;
	uint64_t *	src;

	dst = (uint64_t *)pguid_dst;
	src = (uint64_t *)pguid_src;

	*dst = *src;
	src++;	dst++;
	*dst = *src;
}


void __fastcall __ntapi_tt_guid_to_string_utf16(
	__in	const nt_guid *		guid,
	__out	nt_guid_str_utf16 *	guid_str)
{
	uint16_t	key;
	wchar16_t *	wch;

	wch = &(guid_str->group5[0]);

	__ntapi_tt_uint32_to_hex_utf16(
		guid->data1,
		&guid_str->group1[0]);

	__ntapi_tt_uint16_to_hex_utf16(
		guid->data2,
		&guid_str->group2[0]);

	__ntapi_tt_uint16_to_hex_utf16(
		guid->data3,
		&guid_str->group3[0]);

	key = guid->data4[0] * 0x100 + guid->data4[1];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&guid_str->group4[0]);

	key = guid->data4[2] * 0x100 + guid->data4[3];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&guid_str->group5[0]);

	key = guid->data4[4] * 0x100 + guid->data4[5];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&(wch[4]));

	key = guid->data4[6] * 0x100 + guid->data4[7];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&(wch[8]));

	guid_str->lbrace = '{';
	guid_str->rbrace = '}';
	guid_str->dash1 = '-';
	guid_str->dash2 = '-';
	guid_str->dash3 = '-';
	guid_str->dash4 = '-';

	return;
}


int32_t __fastcall __ntapi_tt_guid_compare(
	__in	const nt_guid *	pguid_dst,
	__in	const nt_guid *	pguid_src)
{
	uint64_t *	dst;
	uint64_t *	src;

	dst = (uint64_t *)pguid_dst;
	src = (uint64_t *)pguid_src;

	if ((*dst != *src) || (*(++dst) != *(++src)))
		return NT_STATUS_OBJECT_TYPE_MISMATCH;

	return NT_STATUS_SUCCESS;
}


int32_t __fastcall __ntapi_tt_string_to_guid_utf16(
	__in	const nt_guid_str_utf16 *	guid_str,
	__out	nt_guid *			guid)
{
	int32_t			status;
	const wchar16_t *	wch;
	uint16_t		key;

	/* outer braces / outer underscores / outer dashes */
	if ((guid_str->lbrace == '{') && (guid_str->rbrace == '}'))
		(void)0;

	else if ((guid_str->lbrace == '_') && (guid_str->rbrace == '_'))
		(void)0;

	else if ((guid_str->lbrace == '-') && (guid_str->rbrace == '-'))
		(void)0;

	else
		return NT_STATUS_INVALID_PARAMETER;

	/* inner dashes */
	if ((guid_str->dash1 != '-')
			|| (guid_str->dash2 != '-')
			|| (guid_str->dash3 != '-')
			|| (guid_str->dash4 != '-'))
		return NT_STATUS_INVALID_PARAMETER;

	wch = &(guid_str->group5[0]);

	status = __ntapi_tt_hex_utf16_to_uint32(
		guid_str->group1,
		&guid->data1);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group2,
		&guid->data2);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group3,
		&guid->data3);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group4,
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[0] = key >> 8;
	guid->data4[1] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[0]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[2] = key >> 8;
	guid->data4[3] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[4]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[4] = key >> 8;
	guid->data4[5] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[8]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[6] = key >> 8;
	guid->data4[7] = key % 0x100;

	return NT_STATUS_SUCCESS;
}

int32_t __fastcall __ntapi_tt_string_to_guid_utf8(
	__in	const char *	guid_str,
	__out	nt_guid *	guid)
{
	const char *		ch;
	const char *		cap;
	wchar16_t *		wch;
	nt_guid_str_utf16	guid_str_utf16;

	ch  = guid_str;
	cap = &ch[sizeof(guid_str_utf16) / sizeof(wchar16_t)];
	wch = &guid_str_utf16.lbrace;

	for (; *ch && (ch<cap); )
		*wch++ = *ch++;

	return (ch < cap)
		? NT_STATUS_INVALID_PARAMETER
		: __ntapi_tt_string_to_guid_utf16(
			&guid_str_utf16,guid);
}

void __fastcall __ntapi_tt_guid_to_string_utf8(
	__in	const nt_guid *		guid,
	__out	nt_guid_str_utf8 *	guid_str)
{
	uint16_t	key;
	uint8_t *	ch;

	ch = &(guid_str->group5[0]);

	__ntapi_tt_uint32_to_hex_utf8(
		guid->data1,
		&guid_str->group1[0]);

	__ntapi_tt_uint16_to_hex_utf8(
		guid->data2,
		&guid_str->group2[0]);

	__ntapi_tt_uint16_to_hex_utf8(
		guid->data3,
		&guid_str->group3[0]);

	key = guid->data4[0] * 0x100 + guid->data4[1];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&guid_str->group4[0]);

	key = guid->data4[2] * 0x100 + guid->data4[3];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&guid_str->group5[0]);

	key = guid->data4[4] * 0x100 + guid->data4[5];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&(ch[4]));

	key = guid->data4[6] * 0x100 + guid->data4[7];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&(ch[8]));

	guid_str->lbrace = '{';
	guid_str->rbrace = '}';
	guid_str->dash1 = '-';
	guid_str->dash2 = '-';
	guid_str->dash3 = '-';
	guid_str->dash4 = '-';
	guid_str->null  = 0;
}