Blame src/guid/ntapi_tt_guid.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_time.h>
dd89bb
#include <ntapi/nt_guid.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
dd89bb
void __fastcall __ntapi_tt_guid_copy(
dd89bb
	__out	nt_guid *	pguid_dst,
dd89bb
	__in	const nt_guid *	pguid_src)
dd89bb
{
dd89bb
	uint64_t *	dst;
dd89bb
	uint64_t *	src;
dd89bb
dd89bb
	dst = (uint64_t *)pguid_dst;
dd89bb
	src = (uint64_t *)pguid_src;
dd89bb
dd89bb
	*dst = *src;
dd89bb
	src++;	dst++;
dd89bb
	*dst = *src;
dd89bb
}
dd89bb
dd89bb
701eb2
void __fastcall __ntapi_tt_guid_to_string_utf16(
dd89bb
	__in	const nt_guid *		guid,
dd89bb
	__out	nt_guid_str_utf16 *	guid_str)
dd89bb
{
dd89bb
	uint16_t	key;
dd89bb
	wchar16_t *	wch;
dd89bb
dd89bb
	wch = &(guid_str->group5[0]);
dd89bb
dd89bb
	__ntapi_tt_uint32_to_hex_utf16(
dd89bb
		guid->data1,
dd89bb
		&guid_str->group1[0]);
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		guid->data2,
dd89bb
		&guid_str->group2[0]);
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		guid->data3,
dd89bb
		&guid_str->group3[0]);
dd89bb
dd89bb
	key = guid->data4[0] * 0x100 + guid->data4[1];
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		key,
dd89bb
		&guid_str->group4[0]);
dd89bb
dd89bb
	key = guid->data4[2] * 0x100 + guid->data4[3];
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		key,
dd89bb
		&guid_str->group5[0]);
dd89bb
dd89bb
	key = guid->data4[4] * 0x100 + guid->data4[5];
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		key,
dd89bb
		&(wch[4]));
dd89bb
dd89bb
	key = guid->data4[6] * 0x100 + guid->data4[7];
dd89bb
dd89bb
	__ntapi_tt_uint16_to_hex_utf16(
dd89bb
		key,
dd89bb
		&(wch[8]));
dd89bb
dd89bb
	guid_str->lbrace = '{';
dd89bb
	guid_str->rbrace = '}';
dd89bb
	guid_str->dash1 = '-';
dd89bb
	guid_str->dash2 = '-';
dd89bb
	guid_str->dash3 = '-';
dd89bb
	guid_str->dash4 = '-';
dd89bb
dd89bb
	return;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __fastcall __ntapi_tt_guid_compare(
dd89bb
	__in	const nt_guid *	pguid_dst,
dd89bb
	__in	const nt_guid *	pguid_src)
dd89bb
{
dd89bb
	uint64_t *	dst;
dd89bb
	uint64_t *	src;
dd89bb
dd89bb
	dst = (uint64_t *)pguid_dst;
dd89bb
	src = (uint64_t *)pguid_src;
dd89bb
dd89bb
	if ((*dst != *src) || (*(++dst) != *(++src)))
dd89bb
		return NT_STATUS_OBJECT_TYPE_MISMATCH;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
701eb2
int32_t __fastcall __ntapi_tt_string_to_guid_utf16(
a820b0
	__in	const nt_guid_str_utf16 *	guid_str,
a820b0
	__out	nt_guid *			guid)
dd89bb
{
a820b0
	int32_t			status;
a820b0
	const wchar16_t *	wch;
a820b0
	uint16_t		key;
dd89bb
ce6acc
	/* outer braces / outer underscores / outer dashes */
ce6acc
	if ((guid_str->lbrace == '{') && (guid_str->rbrace == '}'))
ce6acc
		(void)0;
ce6acc
ce6acc
	else if ((guid_str->lbrace == '_') && (guid_str->rbrace == '_'))
ce6acc
		(void)0;
ce6acc
ce6acc
	else if ((guid_str->lbrace == '-') && (guid_str->rbrace == '-'))
ce6acc
		(void)0;
ce6acc
ce6acc
	else
ce6acc
		return NT_STATUS_INVALID_PARAMETER;
ce6acc
ce6acc
	/* inner dashes */
ce6acc
	if ((guid_str->dash1 != '-')
dd89bb
			|| (guid_str->dash2 != '-')
dd89bb
			|| (guid_str->dash3 != '-')
dd89bb
			|| (guid_str->dash4 != '-'))
ce6acc
		return NT_STATUS_INVALID_PARAMETER;
dd89bb
dd89bb
	wch = &(guid_str->group5[0]);
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint32(
dd89bb
		guid_str->group1,
dd89bb
		&guid->data1);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		guid_str->group2,
dd89bb
		&guid->data2);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		guid_str->group3,
dd89bb
		&guid->data3);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		guid_str->group4,
dd89bb
		&key);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
3e609e
	guid->data4[0] = key >> 8;
dd89bb
	guid->data4[1] = key % 0x100;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		&(wch[0]),
dd89bb
		&key);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
3e609e
	guid->data4[2] = key >> 8;
dd89bb
	guid->data4[3] = key % 0x100;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		&(wch[4]),
dd89bb
		&key);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
3e609e
	guid->data4[4] = key >> 8;
dd89bb
	guid->data4[5] = key % 0x100;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint16(
dd89bb
		&(wch[8]),
dd89bb
		&key);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
3e609e
	guid->data4[6] = key >> 8;
dd89bb
	guid->data4[7] = key % 0x100;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
5d7f04
5d7f04
int32_t __fastcall __ntapi_tt_string_to_guid_utf8(
5d7f04
	__in	const char *	guid_str,
5d7f04
	__out	nt_guid *	guid)
5d7f04
{
5d7f04
	const char *		ch;
5d7f04
	const char *		cap;
5d7f04
	wchar16_t *		wch;
5d7f04
	nt_guid_str_utf16	guid_str_utf16;
5d7f04
5d7f04
	ch  = guid_str;
5d7f04
	cap = &ch[sizeof(guid_str_utf16) / sizeof(wchar16_t)];
5d7f04
	wch = &guid_str_utf16.lbrace;
5d7f04
5d7f04
	for (; *ch && (ch
5d7f04
		*wch++ = *ch++;
5d7f04
5d7f04
	return (ch < cap)
5d7f04
		? NT_STATUS_INVALID_PARAMETER
5d7f04
		: __ntapi_tt_string_to_guid_utf16(
5d7f04
			&guid_str_utf16,guid);
5d7f04
}
fea48d
fea48d
void __fastcall __ntapi_tt_guid_to_string_utf8(
fea48d
	__in	const nt_guid *		guid,
fea48d
	__out	nt_guid_str_utf8 *	guid_str)
fea48d
{
fea48d
	uint16_t	key;
fea48d
	uint8_t *	ch;
fea48d
fea48d
	ch = &(guid_str->group5[0]);
fea48d
fea48d
	__ntapi_tt_uint32_to_hex_utf8(
fea48d
		guid->data1,
fea48d
		&guid_str->group1[0]);
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		guid->data2,
fea48d
		&guid_str->group2[0]);
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		guid->data3,
fea48d
		&guid_str->group3[0]);
fea48d
fea48d
	key = guid->data4[0] * 0x100 + guid->data4[1];
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		key,
fea48d
		&guid_str->group4[0]);
fea48d
fea48d
	key = guid->data4[2] * 0x100 + guid->data4[3];
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		key,
fea48d
		&guid_str->group5[0]);
fea48d
fea48d
	key = guid->data4[4] * 0x100 + guid->data4[5];
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		key,
fea48d
		&(ch[4]));
fea48d
fea48d
	key = guid->data4[6] * 0x100 + guid->data4[7];
fea48d
fea48d
	__ntapi_tt_uint16_to_hex_utf8(
fea48d
		key,
fea48d
		&(ch[8]));
fea48d
fea48d
	guid_str->lbrace = '{';
fea48d
	guid_str->rbrace = '}';
fea48d
	guid_str->dash1 = '-';
fea48d
	guid_str->dash2 = '-';
fea48d
	guid_str->dash3 = '-';
fea48d
	guid_str->dash4 = '-';
fea48d
	guid_str->null  = 0;
fea48d
}