From aac1ecb7acc5196664ef65a2467cbe45c097841c Mon Sep 17 00:00:00 2001 From: midipix Date: Thu, 14 Jun 2018 07:51:51 +0000 Subject: string helper api's: added ntapi_tt_uintptr_to_dec{_null}_utf{8|16}(). --- include/ntapi/nt_string.h | 28 +++++++ include/ntapi/ntapi.h | 4 + project/common.mk | 1 + src/internal/ntapi.c | 4 + src/internal/ntapi_fnapi.h | 4 + src/string/ntapi_tt_uintptr_to_dec.c | 148 +++++++++++++++++++++++++++++++++++ 6 files changed, 189 insertions(+) create mode 100644 src/string/ntapi_tt_uintptr_to_dec.c diff --git a/include/ntapi/nt_string.h b/include/ntapi/nt_string.h index 059a7ee..b24de3d 100644 --- a/include/ntapi/nt_string.h +++ b/include/ntapi/nt_string.h @@ -177,4 +177,32 @@ typedef void __fastcall ntapi_tt_uintptr_to_hex_utf8( __in const uintptr_t key, __out unsigned char * buffer); + +typedef int32_t __stdcall ntapi_tt_uintptr_to_dec_utf16( + __in uintptr_t key, + __out wchar16_t * wstrbuf, + __in size_t buflen, + __out ssize_t * length); + + +typedef int32_t __stdcall ntapi_tt_uintptr_to_dec_utf8( + __in uintptr_t key, + __out unsigned char * strbuf, + __in size_t buflen, + __out ssize_t * length); + + +typedef int32_t __stdcall ntapi_tt_uintptr_to_dec_null_utf16( + __in uintptr_t key, + __out wchar16_t * wstrbuf, + __in size_t buflen, + __out ssize_t * length); + + +typedef int32_t __stdcall ntapi_tt_uintptr_to_dec_null_utf8( + __in uintptr_t key, + __out unsigned char * strbuf, + __in size_t buflen, + __out ssize_t * length); + #endif diff --git a/include/ntapi/ntapi.h b/include/ntapi/ntapi.h index 8de63d3..c92a7ac 100644 --- a/include/ntapi/ntapi.h +++ b/include/ntapi/ntapi.h @@ -475,6 +475,10 @@ typedef struct _ntapi_vtbl { ntapi_tt_uint32_to_hex_utf8 * tt_uint32_to_hex_utf8; ntapi_tt_uint64_to_hex_utf8 * tt_uint64_to_hex_utf8; ntapi_tt_uintptr_to_hex_utf8 * tt_uintptr_to_hex_utf8; + ntapi_tt_uintptr_to_dec_utf16 * tt_uintptr_to_dec_utf16; + ntapi_tt_uintptr_to_dec_utf8 * tt_uintptr_to_dec_utf8; + ntapi_tt_uintptr_to_dec_null_utf16 * tt_uintptr_to_dec_null_utf16; + ntapi_tt_uintptr_to_dec_null_utf8 * tt_uintptr_to_dec_null_utf8; ntapi_tt_init_unicode_string_from_utf16* tt_init_unicode_string_from_utf16; /* nt_guid.h */ diff --git a/project/common.mk b/project/common.mk index 7be6a79..e993277 100644 --- a/project/common.mk +++ b/project/common.mk @@ -108,6 +108,7 @@ COMMON_SRCS = \ src/string/ntapi_tt_string_null_offset.c \ src/string/ntapi_tt_strncmp_multibyte.c \ src/string/ntapi_tt_strncmp_utf16.c \ + src/string/ntapi_tt_uintptr_to_dec.c \ src/string/ntapi_tt_uintptr_to_hex_utf16.c \ src/string/ntapi_tt_uintptr_to_hex_utf8.c \ src/sync/ntapi_tt_create_event.c \ diff --git a/src/internal/ntapi.c b/src/internal/ntapi.c index a53ca80..6f6b9ce 100644 --- a/src/internal/ntapi.c +++ b/src/internal/ntapi.c @@ -271,6 +271,10 @@ static int32_t __fastcall __ntapi_init_once(ntapi_vtbl ** pvtbl) __ntapi->tt_uint32_to_hex_utf8 = __ntapi_tt_uint32_to_hex_utf8; __ntapi->tt_uint64_to_hex_utf8 = __ntapi_tt_uint64_to_hex_utf8; __ntapi->tt_uintptr_to_hex_utf8 = __ntapi_tt_uintptr_to_hex_utf8; + __ntapi->tt_uintptr_to_dec_utf16 = __ntapi_tt_uintptr_to_dec_utf16; + __ntapi->tt_uintptr_to_dec_utf8 = __ntapi_tt_uintptr_to_dec_utf8; + __ntapi->tt_uintptr_to_dec_null_utf16 = __ntapi_tt_uintptr_to_dec_null_utf16; + __ntapi->tt_uintptr_to_dec_null_utf8 = __ntapi_tt_uintptr_to_dec_null_utf8; /* nt_guid.h */ __ntapi->tt_guid_copy = __ntapi_tt_guid_copy; diff --git a/src/internal/ntapi_fnapi.h b/src/internal/ntapi_fnapi.h index 8280f55..9157dc1 100644 --- a/src/internal/ntapi_fnapi.h +++ b/src/internal/ntapi_fnapi.h @@ -133,6 +133,10 @@ ntapi_tt_uint16_to_hex_utf8 __ntapi_tt_uint16_to_hex_utf8; ntapi_tt_uint32_to_hex_utf8 __ntapi_tt_uint32_to_hex_utf8; ntapi_tt_uint64_to_hex_utf8 __ntapi_tt_uint64_to_hex_utf8; ntapi_tt_uintptr_to_hex_utf8 __ntapi_tt_uintptr_to_hex_utf8; +ntapi_tt_uintptr_to_dec_utf16 __ntapi_tt_uintptr_to_dec_utf16; +ntapi_tt_uintptr_to_dec_utf8 __ntapi_tt_uintptr_to_dec_utf8; +ntapi_tt_uintptr_to_dec_null_utf16 __ntapi_tt_uintptr_to_dec_null_utf16; +ntapi_tt_uintptr_to_dec_null_utf8 __ntapi_tt_uintptr_to_dec_null_utf8; ntapi_wcslen __ntapi_tt_wcslen; /* nt_guid.h */ diff --git a/src/string/ntapi_tt_uintptr_to_dec.c b/src/string/ntapi_tt_uintptr_to_dec.c new file mode 100644 index 0000000..c496247 --- /dev/null +++ b/src/string/ntapi_tt_uintptr_to_dec.c @@ -0,0 +1,148 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013--2018 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include +#include + +static void __tt_uintptr_to_mirrored_dec_utf8( + uintptr_t key, + unsigned char * buf, + ssize_t * arrlen) +{ + unsigned i; + + buf[0] = '0' + (key % 10); + key /= 10; + + for (i=1; key; i++) { + buf[i] = '0' + (key % 10); + key /= 10; + } + + *arrlen = i; +} + +static int32_t __tt_uintptr_to_dec_utf16( + uintptr_t key, + wchar16_t * wstrbuf, + size_t buflen, + ssize_t * arrlen) +{ + ssize_t i; + ssize_t slen; + unsigned char buf[24]; + + __tt_uintptr_to_mirrored_dec_utf8( + key,buf,arrlen); + + if ((i = *arrlen) > (slen = buflen)) + return NT_STATUS_BUFFER_TOO_SMALL; + + for (--i; i>=0; i--) + *wstrbuf++ = buf[i]; + + return NT_STATUS_SUCCESS; +} + +static int32_t __tt_uintptr_to_dec_utf8( + uintptr_t key, + unsigned char * strbuf, + size_t buflen, + ssize_t * arrlen) +{ + ssize_t i; + ssize_t slen; + unsigned char buf[24]; + + __tt_uintptr_to_mirrored_dec_utf8( + key,buf,arrlen); + + if ((i = *arrlen) > (slen = buflen)) + return NT_STATUS_BUFFER_TOO_SMALL; + + for (--i; i>=0; i--) + *strbuf++ = buf[i]; + + return NT_STATUS_SUCCESS; +} + +int32_t __stdcall __ntapi_tt_uintptr_to_dec_utf16( + __in uintptr_t key, + __out wchar16_t * wstrbuf, + __in size_t buflen, + __out ssize_t * length) +{ + int32_t status; + ssize_t arrlen; + + buflen /= sizeof(wchar16_t); + + status = __tt_uintptr_to_dec_utf16( + key,wstrbuf, + buflen,&arrlen); + + *length = arrlen * sizeof(wchar16_t); + + return status; +} + +int32_t __stdcall __ntapi_tt_uintptr_to_dec_utf8( + __in uintptr_t key, + __out unsigned char * strbuf, + __in size_t buflen, + __out ssize_t * length) +{ + return __tt_uintptr_to_dec_utf8( + key,strbuf, + buflen,length); +} + + +int32_t __stdcall __ntapi_tt_uintptr_to_dec_null_utf16( + __in uintptr_t key, + __out wchar16_t * wstrbuf, + __in size_t buflen, + __out ssize_t * length) +{ + int32_t status; + ssize_t arrlen; + + buflen -= sizeof(wchar16_t); + buflen /= sizeof(wchar16_t); + + status = __tt_uintptr_to_dec_utf16( + key,wstrbuf, + buflen,&arrlen); + + if (status == NT_STATUS_SUCCESS) + wstrbuf[arrlen] = 0; + + *length = ++arrlen * sizeof(wchar16_t); + + return status; +} + + +int32_t __stdcall __ntapi_tt_uintptr_to_dec_null_utf8( + __in uintptr_t key, + __out unsigned char * strbuf, + __in size_t buflen, + __out ssize_t * length) +{ + int32_t status; + ssize_t arrlen; + + status = __tt_uintptr_to_dec_utf8( + key,strbuf, + --buflen,&arrlen); + + if (status == NT_STATUS_SUCCESS) + strbuf[arrlen] = 0; + + *length = ++arrlen; + + return status; +} -- cgit v1.2.3