summaryrefslogtreecommitdiffhomepage
path: root/src/string
diff options
context:
space:
mode:
Diffstat (limited to 'src/string')
-rw-r--r--src/string/ntapi_tt_hex_utf8_to_uintptr.c125
1 files changed, 125 insertions, 0 deletions
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 <psxtypes/psxtypes.h>
+#include <ntapi/nt_status.h>
+#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;
+}