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}(). --- src/string/ntapi_tt_uintptr_to_dec.c | 148 +++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/string/ntapi_tt_uintptr_to_dec.c (limited to 'src/string') 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