Blame src/string/ntapi_tt_hex_utf16_to_uintptr.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dd89bb
/*  Copyright (C) 2013,2014,2015  Z. Gilboa             */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_status.h>
dd89bb
dd89bb
int32_t __fastcall __ntapi_tt_hex_utf16_to_uint32(
dd89bb
	__in	wchar16_t	hex_key_utf16[8],
dd89bb
	__out	uint32_t *	key)
dd89bb
{
dd89bb
	int		i;
dd89bb
	unsigned char	uch[8];
dd89bb
	unsigned char	ubytes[4];
dd89bb
	uint32_t *	key_ret;
dd89bb
dd89bb
	/* input validation */
dd89bb
	i = 0;
dd89bb
	do {
dd89bb
		if (/* [a-f],[[A-F],[0-9] */
dd89bb
			((hex_key_utf16[i] >= 'a') && (hex_key_utf16[i] <= 'f'))
dd89bb
			|| ((hex_key_utf16[i] >= 'A') && (hex_key_utf16[i] <= 'F'))
dd89bb
			|| ((hex_key_utf16[i] >= '0') && (hex_key_utf16[i] <= '9')))
dd89bb
			/* valid hex character */
dd89bb
			i++;
dd89bb
		else
dd89bb
			return NT_STATUS_ILLEGAL_CHARACTER;
dd89bb
	} while (i < 8);
dd89bb
dd89bb
	/* intermediate step: little endian byte order */
dd89bb
	uch[0] = (unsigned char)hex_key_utf16[6];
dd89bb
	uch[1] = (unsigned char)hex_key_utf16[7];
dd89bb
	uch[2] = (unsigned char)hex_key_utf16[4];
dd89bb
	uch[3] = (unsigned char)hex_key_utf16[5];
dd89bb
	uch[4] = (unsigned char)hex_key_utf16[2];
dd89bb
	uch[5] = (unsigned char)hex_key_utf16[3];
dd89bb
	uch[6] = (unsigned char)hex_key_utf16[0];
dd89bb
	uch[7] = (unsigned char)hex_key_utf16[1];
dd89bb
dd89bb
	for (i=0; i<8; i++) {
dd89bb
		/* 'a' > 'A' > '0' */
dd89bb
		if (uch[i] >= 'a')
dd89bb
			uch[i] -= ('a' - 0x0a);
dd89bb
		else if (uch[i] >= 'A')
dd89bb
			uch[i] -= ('A' - 0x0a);
dd89bb
		else
dd89bb
			uch[i] -= '0';
dd89bb
	}
dd89bb
dd89bb
	ubytes[0] = uch[0] * 0x10 + uch[1];
dd89bb
	ubytes[1] = uch[2] * 0x10 + uch[3];
dd89bb
	ubytes[2] = uch[4] * 0x10 + uch[5];
dd89bb
	ubytes[3] = uch[6] * 0x10 + uch[7];
dd89bb
dd89bb
	key_ret = (uint32_t *)ubytes;
dd89bb
	*key = *key_ret;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __fastcall __ntapi_tt_hex_utf16_to_uint64(
dd89bb
	__in	wchar16_t	hex_key_utf16[16],
dd89bb
	__out	uint64_t *	key)
dd89bb
{
dd89bb
	int32_t		status;
dd89bb
	uint32_t	x64_key[2];
dd89bb
	uint64_t *	key_ret;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint32(
dd89bb
			&hex_key_utf16[0],
dd89bb
			&x64_key[1]);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	status = __ntapi_tt_hex_utf16_to_uint32(
dd89bb
			&hex_key_utf16[8],
dd89bb
			&x64_key[0]);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	key_ret = (uint64_t *)x64_key;
dd89bb
	*key = *key_ret;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __fastcall __ntapi_tt_hex_utf16_to_uintptr(
dd89bb
	__in	wchar16_t	hex_key_utf16[],
dd89bb
	__out	uintptr_t *	key)
dd89bb
{
dd89bb
	#if defined (__NT32)
dd89bb
		return __ntapi_tt_hex_utf16_to_uint32(hex_key_utf16,key);
dd89bb
	#elif defined (__NT64)
dd89bb
		return __ntapi_tt_hex_utf16_to_uint64(hex_key_utf16,key);
dd89bb
	#endif
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __fastcall __ntapi_tt_hex_utf16_to_uint16(
dd89bb
	__in	wchar16_t	hex_key_utf16[4],
dd89bb
	__out	uint16_t *	key)
dd89bb
{
dd89bb
	int32_t		ret;
dd89bb
	uint32_t	dword_key;
dd89bb
	wchar16_t	hex_buf[8] = {'0','0','0','0'};
dd89bb
dd89bb
	hex_buf[4] = hex_key_utf16[0];
dd89bb
	hex_buf[5] = hex_key_utf16[1];
dd89bb
	hex_buf[6] = hex_key_utf16[2];
dd89bb
	hex_buf[7] = hex_key_utf16[3];
dd89bb
dd89bb
	ret = __ntapi_tt_hex_utf16_to_uint32(hex_buf,&dword_key);
dd89bb
dd89bb
	if (ret == NT_STATUS_SUCCESS)
dd89bb
		*key = (uint16_t)dword_key;
dd89bb
dd89bb
	return ret;
dd89bb
}