From a413881baa6fe317b2e33fc611460a4576bf9d66 Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 7 Jun 2020 18:40:10 +0000 Subject: string functions: added ntapi_tt_hex_utf8_to_uint{16|32|64|ptr}. --- include/ntapi/nt_string.h | 18 +++++ include/ntapi/ntapi.h | 7 ++ project/common.mk | 1 + src/internal/ntapi.c | 7 ++ src/internal/ntapi_fnapi.h | 7 ++ src/string/ntapi_tt_hex_utf8_to_uintptr.c | 125 ++++++++++++++++++++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 src/string/ntapi_tt_hex_utf8_to_uintptr.c diff --git a/include/ntapi/nt_string.h b/include/ntapi/nt_string.h index f85887d..6650d60 100644 --- a/include/ntapi/nt_string.h +++ b/include/ntapi/nt_string.h @@ -157,6 +157,24 @@ typedef int32_t __fastcall ntapi_tt_hex_utf16_to_uintptr( __in const wchar16_t hex_key_utf16[], __out uintptr_t * key); +typedef int32_t __fastcall ntapi_tt_hex_utf8_to_uint16( + __in const unsigned char hex_key_utf16[4], + __out uint16_t * key); + + +typedef int32_t __fastcall ntapi_tt_hex_utf8_to_uint32( + __in const unsigned char hex_key_utf16[8], + __out uint32_t * key); + + +typedef int32_t __fastcall ntapi_tt_hex_utf8_to_uint64( + __in const unsigned char hex_key_utf16[16], + __out uint64_t * key); + + +typedef int32_t __fastcall ntapi_tt_hex_utf8_to_uintptr( + __in const unsigned char hex_key_utf16[], + __out uintptr_t * key); typedef void __fastcall ntapi_tt_uint16_to_hex_utf8( __in const uint32_t key, diff --git a/include/ntapi/ntapi.h b/include/ntapi/ntapi.h index 63ff63f..71d8ffb 100644 --- a/include/ntapi/ntapi.h +++ b/include/ntapi/ntapi.h @@ -479,10 +479,17 @@ typedef struct _ntapi_vtbl { ntapi_tt_uint32_to_hex_utf16 * tt_uint32_to_hex_utf16; ntapi_tt_uint64_to_hex_utf16 * tt_uint64_to_hex_utf16; ntapi_tt_uintptr_to_hex_utf16 * tt_uintptr_to_hex_utf16; + ntapi_tt_hex_utf16_to_uint16 * tt_hex_utf16_to_uint16; ntapi_tt_hex_utf16_to_uint32 * tt_hex_utf16_to_uint32; ntapi_tt_hex_utf16_to_uint64 * tt_hex_utf16_to_uint64; ntapi_tt_hex_utf16_to_uintptr * tt_hex_utf16_to_uintptr; + + ntapi_tt_hex_utf8_to_uint16 * tt_hex_utf8_to_uint16; + ntapi_tt_hex_utf8_to_uint32 * tt_hex_utf8_to_uint32; + ntapi_tt_hex_utf8_to_uint64 * tt_hex_utf8_to_uint64; + ntapi_tt_hex_utf8_to_uintptr * tt_hex_utf8_to_uintptr; + ntapi_tt_uint16_to_hex_utf8 * tt_uint16_to_hex_utf8; ntapi_tt_uint32_to_hex_utf8 * tt_uint32_to_hex_utf8; ntapi_tt_uint64_to_hex_utf8 * tt_uint64_to_hex_utf8; diff --git a/project/common.mk b/project/common.mk index 84749be..d72ab20 100644 --- a/project/common.mk +++ b/project/common.mk @@ -106,6 +106,7 @@ COMMON_SRCS = \ src/string/ntapi_tt_aligned_memcpy_utf16.c \ src/string/ntapi_tt_dec_utf16_to_uintptr.c \ src/string/ntapi_tt_hex_utf16_to_uintptr.c \ + src/string/ntapi_tt_hex_utf8_to_uintptr.c \ src/string/ntapi_tt_init_unicode_string_from_utf16.c \ src/string/ntapi_tt_memcpy_utf16.c \ src/string/ntapi_tt_strcmp_multibyte.c \ diff --git a/src/internal/ntapi.c b/src/internal/ntapi.c index 1cb7dfa..b1d282d 100644 --- a/src/internal/ntapi.c +++ b/src/internal/ntapi.c @@ -303,10 +303,17 @@ static int32_t __fastcall __ntapi_init_once(ntapi_vtbl ** pvtbl) __ntapi->tt_uint32_to_hex_utf16 = __ntapi_tt_uint32_to_hex_utf16; __ntapi->tt_uint64_to_hex_utf16 = __ntapi_tt_uint64_to_hex_utf16; __ntapi->tt_uintptr_to_hex_utf16 = __ntapi_tt_uintptr_to_hex_utf16; + __ntapi->tt_hex_utf16_to_uint16 = __ntapi_tt_hex_utf16_to_uint16; __ntapi->tt_hex_utf16_to_uint32 = __ntapi_tt_hex_utf16_to_uint32; __ntapi->tt_hex_utf16_to_uint64 = __ntapi_tt_hex_utf16_to_uint64; __ntapi->tt_hex_utf16_to_uintptr = __ntapi_tt_hex_utf16_to_uintptr; + + __ntapi->tt_hex_utf8_to_uint16 = __ntapi_tt_hex_utf8_to_uint16; + __ntapi->tt_hex_utf8_to_uint32 = __ntapi_tt_hex_utf8_to_uint32; + __ntapi->tt_hex_utf8_to_uint64 = __ntapi_tt_hex_utf8_to_uint64; + __ntapi->tt_hex_utf8_to_uintptr = __ntapi_tt_hex_utf8_to_uintptr; + __ntapi->tt_init_unicode_string_from_utf16 = __ntapi_tt_init_unicode_string_from_utf16; __ntapi->tt_uint16_to_hex_utf8 = __ntapi_tt_uint16_to_hex_utf8; __ntapi->tt_uint32_to_hex_utf8 = __ntapi_tt_uint32_to_hex_utf8; diff --git a/src/internal/ntapi_fnapi.h b/src/internal/ntapi_fnapi.h index 4121883..ca888c4 100644 --- a/src/internal/ntapi_fnapi.h +++ b/src/internal/ntapi_fnapi.h @@ -130,10 +130,17 @@ NTAPI_UFN(tt_uint16_to_hex_utf16); NTAPI_UFN(tt_uint32_to_hex_utf16); NTAPI_UFN(tt_uint64_to_hex_utf16); NTAPI_UFN(tt_uintptr_to_hex_utf16); + NTAPI_UFN(tt_hex_utf16_to_uint16); NTAPI_UFN(tt_hex_utf16_to_uint32); NTAPI_UFN(tt_hex_utf16_to_uint64); NTAPI_UFN(tt_hex_utf16_to_uintptr); + +NTAPI_UFN(tt_hex_utf8_to_uint16); +NTAPI_UFN(tt_hex_utf8_to_uint32); +NTAPI_UFN(tt_hex_utf8_to_uint64); +NTAPI_UFN(tt_hex_utf8_to_uintptr); + NTAPI_UFN(tt_uint16_to_hex_utf8); NTAPI_UFN(tt_uint32_to_hex_utf8); NTAPI_UFN(tt_uint64_to_hex_utf8); diff --git a/src/string/ntapi_tt_hex_utf8_to_uintptr.c b/src/string/ntapi_tt_hex_utf8_to_uintptr.c new file mode 100644 index 0000000..1f0fe13 --- /dev/null +++ b/src/string/ntapi_tt_hex_utf8_to_uintptr.c @@ -0,0 +1,125 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013--2017 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include +#include +#include "ntapi_impl.h" + +int32_t __fastcall __ntapi_tt_hex_utf8_to_uint32( + __in const unsigned char hex_key_utf8[8], + __out uint32_t * key) +{ + int i; + unsigned char uch[8]; + unsigned char ubytes[4]; + uint32_t * key_ret; + + /* input validation */ + i = 0; + do { + if (/* [a-f],[[A-F],[0-9] */ + ((hex_key_utf8[i] >= 'a') && (hex_key_utf8[i] <= 'f')) + || ((hex_key_utf8[i] >= 'A') && (hex_key_utf8[i] <= 'F')) + || ((hex_key_utf8[i] >= '0') && (hex_key_utf8[i] <= '9'))) + /* valid hex character */ + i++; + else + return NT_STATUS_ILLEGAL_CHARACTER; + } while (i < 8); + + /* intermediate step: little endian byte order */ + uch[0] = hex_key_utf8[6]; + uch[1] = hex_key_utf8[7]; + uch[2] = hex_key_utf8[4]; + uch[3] = hex_key_utf8[5]; + uch[4] = hex_key_utf8[2]; + uch[5] = hex_key_utf8[3]; + uch[6] = hex_key_utf8[0]; + uch[7] = hex_key_utf8[1]; + + for (i=0; i<8; i++) { + /* 'a' > 'A' > '0' */ + if (uch[i] >= 'a') + uch[i] -= ('a' - 0x0a); + else if (uch[i] >= 'A') + uch[i] -= ('A' - 0x0a); + else + uch[i] -= '0'; + } + + ubytes[0] = uch[0] * 0x10 + uch[1]; + ubytes[1] = uch[2] * 0x10 + uch[3]; + ubytes[2] = uch[4] * 0x10 + uch[5]; + ubytes[3] = uch[6] * 0x10 + uch[7]; + + key_ret = (uint32_t *)ubytes; + *key = *key_ret; + + return NT_STATUS_SUCCESS; +} + + +int32_t __fastcall __ntapi_tt_hex_utf8_to_uint64( + __in const unsigned char hex_key_utf8[16], + __out uint64_t * key) +{ + int32_t status; + uint32_t x64_key[2]; + uint64_t * key_ret; + + status = __ntapi_tt_hex_utf8_to_uint32( + &hex_key_utf8[0], + &x64_key[1]); + + if (status != NT_STATUS_SUCCESS) + return status; + + status = __ntapi_tt_hex_utf8_to_uint32( + &hex_key_utf8[8], + &x64_key[0]); + + if (status != NT_STATUS_SUCCESS) + return status; + + key_ret = (uint64_t *)x64_key; + *key = *key_ret; + + return NT_STATUS_SUCCESS; +} + + +int32_t __fastcall __ntapi_tt_hex_utf8_to_uintptr( + __in const unsigned char hex_key_utf8[], + __out uintptr_t * key) +{ + #if (__SIZEOF_POINTER__ == 4) + return __ntapi_tt_hex_utf8_to_uint32(hex_key_utf8,key); + #elif (__SIZEOF_POINTER__ == 8) + return __ntapi_tt_hex_utf8_to_uint64(hex_key_utf8,key); + #endif +} + + +int32_t __fastcall __ntapi_tt_hex_utf8_to_uint16( + __in const unsigned char hex_key_utf8[4], + __out uint16_t * key) +{ + int32_t ret; + uint32_t dword_key; + unsigned char hex_buf[8] = {'0','0','0','0'}; + + hex_buf[4] = hex_key_utf8[0]; + hex_buf[5] = hex_key_utf8[1]; + hex_buf[6] = hex_key_utf8[2]; + hex_buf[7] = hex_key_utf8[3]; + + ret = __ntapi_tt_hex_utf8_to_uint32(hex_buf,&dword_key); + + if (ret == NT_STATUS_SUCCESS) + *key = (uint16_t)dword_key; + + return ret; +} -- cgit v1.2.3