diff options
author | midipix <writeonce@midipix.org> | 2015-07-27 04:01:18 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2015-07-27 04:01:18 -0400 |
commit | dd89bb8ad4fe184a34b5dbdda237e640fc82121b (patch) | |
tree | 5e80d2da35f5892f92be29f57982b2708e6bd99b /src/string | |
parent | dcdadc2702712fa750ed255ed1dfa354522797a0 (diff) | |
download | ntapi-dd89bb8ad4fe184a34b5dbdda237e640fc82121b.tar.bz2 ntapi-dd89bb8ad4fe184a34b5dbdda237e640fc82121b.tar.xz |
entered advanced internal development stage.
Diffstat (limited to 'src/string')
-rw-r--r-- | src/string/ntapi_tt_aligned_block_memcpy.c | 50 | ||||
-rw-r--r-- | src/string/ntapi_tt_aligned_block_memset.c | 57 | ||||
-rw-r--r-- | src/string/ntapi_tt_aligned_memcpy_utf16.c | 70 | ||||
-rw-r--r-- | src/string/ntapi_tt_hex_utf16_to_uintptr.c | 124 | ||||
-rw-r--r-- | src/string/ntapi_tt_init_unicode_string_from_utf16.c | 26 | ||||
-rw-r--r-- | src/string/ntapi_tt_memcpy_utf16.c | 28 | ||||
-rw-r--r-- | src/string/ntapi_tt_string_null_offset.c | 93 | ||||
-rw-r--r-- | src/string/ntapi_tt_uintptr_to_hex_utf16.c | 87 | ||||
-rw-r--r-- | src/string/ntapi_tt_uintptr_to_hex_utf8.c | 73 |
9 files changed, 608 insertions, 0 deletions
diff --git a/src/string/ntapi_tt_aligned_block_memcpy.c b/src/string/ntapi_tt_aligned_block_memcpy.c new file mode 100644 index 0000000..43e1a49 --- /dev/null +++ b/src/string/ntapi_tt_aligned_block_memcpy.c @@ -0,0 +1,50 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> +#include <ntapi/nt_string.h> + +uintptr_t * __cdecl __ntapi_tt_aligned_block_memcpy( + __in uintptr_t * dst, + __in uintptr_t * src, + __in size_t bytes) +{ + uintptr_t * ptr = (uintptr_t *)dst; + + for (bytes/=sizeof(uintptr_t); bytes; bytes--) + *dst++ = *src++; + + return ptr; +} + + +void * __cdecl __ntapi_tt_generic_memcpy( + __in void * dst, + __in const void * src, + __in size_t bytes) +{ + char * ch_dst; + const char * ch_src; + + if (!bytes) + return dst; + + else if (!(bytes % sizeof(size_t)) + && (!(uintptr_t)dst % sizeof(size_t)) + && (!(uintptr_t)src % sizeof(size_t))) + return __ntapi_tt_aligned_block_memcpy( + (uintptr_t *)dst, + (uintptr_t *)src, + bytes); + + ch_dst = (char *)dst; + ch_src = (const char *)src; + + for (; bytes; bytes--) + *ch_dst++ = *ch_src++; + + return dst; +} diff --git a/src/string/ntapi_tt_aligned_block_memset.c b/src/string/ntapi_tt_aligned_block_memset.c new file mode 100644 index 0000000..8e64360 --- /dev/null +++ b/src/string/ntapi_tt_aligned_block_memset.c @@ -0,0 +1,57 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> + +void * __cdecl __ntapi_tt_aligned_block_memset( + __in void * block, + __in uintptr_t val, + __in size_t bytes) +{ + uintptr_t * ptr = (uintptr_t *)block; + + for (bytes/=sizeof(uintptr_t); bytes; bytes--) + *ptr++=val; + + return block; +} + +void * __cdecl __ntapi_tt_generic_memset( + __in void * dst, + __in uintptr_t val, + __in size_t bytes) +{ + char c; + char * ch; + int i; + size_t abytes; + + if (!bytes) + return dst; + + else if (!(bytes % sizeof(size_t)) + && (!(uintptr_t)dst % sizeof(size_t))) + return __ntapi_tt_aligned_block_memset( + dst,val,bytes); + + c = (char)val; + for (i=0; i<sizeof(size_t); i++, val <<= 8) + val += c; + + for (ch=(char *)dst; (size_t)ch % sizeof(size_t); ch++, bytes--) + *ch = c; + + abytes = bytes / sizeof(size_t) * sizeof(size_t); + __ntapi_tt_aligned_block_memset(ch,val,abytes); + + bytes -= abytes; + ch += abytes; + + for (; bytes; ch++, bytes--) + *ch = c; + + return dst; +} diff --git a/src/string/ntapi_tt_aligned_memcpy_utf16.c b/src/string/ntapi_tt_aligned_memcpy_utf16.c new file mode 100644 index 0000000..2035814 --- /dev/null +++ b/src/string/ntapi_tt_aligned_memcpy_utf16.c @@ -0,0 +1,70 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> + +wchar16_t * __cdecl __ntapi_tt_aligned_memcpy_utf16( + __in uintptr_t * dst, + __in uintptr_t * src, + __in size_t bytes) +{ + size_t aligned_block; + size_t copied; + + wchar16_t * wch_src; + wchar16_t * wch_dst; + + #if defined (__X86_64_MODEL) + uint32_t * uint32_src; + uint32_t * uint32_dst; + #endif + + aligned_block = bytes; + aligned_block /= sizeof(uintptr_t); + aligned_block *= sizeof(uintptr_t); + + copied = 0; + + while (copied < aligned_block) { + *dst = *src; + src++; + dst++; + copied += sizeof(uintptr_t); + } + + #if defined (__X86_64_MODEL) + switch (bytes % sizeof(uintptr_t)) { + case 6: + uint32_src = (uint32_t *)src; + uint32_dst = (uint32_t *)dst; + *uint32_dst = *uint32_src; + + uint32_src++; + uint32_dst++; + + /* make the compiler happy */ + wch_src = (wchar16_t *)uint32_src; + wch_dst = (wchar16_t *)uint32_dst; + *wch_dst = *wch_src; + break; + + case 4: + uint32_src = (uint32_t *)src; + uint32_dst = (uint32_t *)dst; + *uint32_dst = *uint32_src; + break; + } + #endif + + if (bytes % sizeof(uintptr_t)) { + /* the remainder must be 2 */ + wch_src = (wchar16_t *)src; + wch_dst = (wchar16_t *)dst; + *wch_dst = *wch_src; + } + + return (wchar16_t *)dst; +} diff --git a/src/string/ntapi_tt_hex_utf16_to_uintptr.c b/src/string/ntapi_tt_hex_utf16_to_uintptr.c new file mode 100644 index 0000000..3b1f354 --- /dev/null +++ b/src/string/ntapi_tt_hex_utf16_to_uintptr.c @@ -0,0 +1,124 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> +#include <ntapi/nt_status.h> + +int32_t __fastcall __ntapi_tt_hex_utf16_to_uint32( + __in wchar16_t hex_key_utf16[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_utf16[i] >= 'a') && (hex_key_utf16[i] <= 'f')) + || ((hex_key_utf16[i] >= 'A') && (hex_key_utf16[i] <= 'F')) + || ((hex_key_utf16[i] >= '0') && (hex_key_utf16[i] <= '9'))) + /* valid hex character */ + i++; + else + return NT_STATUS_ILLEGAL_CHARACTER; + } while (i < 8); + + /* intermediate step: little endian byte order */ + uch[0] = (unsigned char)hex_key_utf16[6]; + uch[1] = (unsigned char)hex_key_utf16[7]; + uch[2] = (unsigned char)hex_key_utf16[4]; + uch[3] = (unsigned char)hex_key_utf16[5]; + uch[4] = (unsigned char)hex_key_utf16[2]; + uch[5] = (unsigned char)hex_key_utf16[3]; + uch[6] = (unsigned char)hex_key_utf16[0]; + uch[7] = (unsigned char)hex_key_utf16[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_utf16_to_uint64( + __in wchar16_t hex_key_utf16[16], + __out uint64_t * key) +{ + int32_t status; + uint32_t x64_key[2]; + uint64_t * key_ret; + + status = __ntapi_tt_hex_utf16_to_uint32( + &hex_key_utf16[0], + &x64_key[1]); + + if (status != NT_STATUS_SUCCESS) + return status; + + status = __ntapi_tt_hex_utf16_to_uint32( + &hex_key_utf16[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_utf16_to_uintptr( + __in wchar16_t hex_key_utf16[], + __out uintptr_t * key) +{ + #if defined (__NT32) + return __ntapi_tt_hex_utf16_to_uint32(hex_key_utf16,key); + #elif defined (__NT64) + return __ntapi_tt_hex_utf16_to_uint64(hex_key_utf16,key); + #endif +} + + +int32_t __fastcall __ntapi_tt_hex_utf16_to_uint16( + __in wchar16_t hex_key_utf16[4], + __out uint16_t * key) +{ + int32_t ret; + uint32_t dword_key; + wchar16_t hex_buf[8] = {'0','0','0','0'}; + + hex_buf[4] = hex_key_utf16[0]; + hex_buf[5] = hex_key_utf16[1]; + hex_buf[6] = hex_key_utf16[2]; + hex_buf[7] = hex_key_utf16[3]; + + ret = __ntapi_tt_hex_utf16_to_uint32(hex_buf,&dword_key); + + if (ret == NT_STATUS_SUCCESS) + *key = (uint16_t)dword_key; + + return ret; +} diff --git a/src/string/ntapi_tt_init_unicode_string_from_utf16.c b/src/string/ntapi_tt_init_unicode_string_from_utf16.c new file mode 100644 index 0000000..96673b4 --- /dev/null +++ b/src/string/ntapi_tt_init_unicode_string_from_utf16.c @@ -0,0 +1,26 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> +#include <pemagine/pemagine.h> +#include <ntapi/nt_object.h> +#include <ntapi/ntapi.h> +#include "ntapi_impl.h" + +void __ntapi_tt_init_unicode_string_from_utf16( + __out nt_unicode_string * str_dest, + __in wchar16_t * str_src) +{ + if ((intptr_t)str_src) { + str_dest->strlen = (uint16_t)__ntapi->tt_string_null_offset_short((const int16_t *)str_src); + str_dest->maxlen = str_dest->strlen + sizeof(uint16_t); + str_dest->buffer = (uint16_t *)str_src; + } else { + str_dest->strlen = 0; + str_dest->maxlen = 0; + str_dest->buffer = (uint16_t *)0; + } +}
\ No newline at end of file diff --git a/src/string/ntapi_tt_memcpy_utf16.c b/src/string/ntapi_tt_memcpy_utf16.c new file mode 100644 index 0000000..0a2b7af --- /dev/null +++ b/src/string/ntapi_tt_memcpy_utf16.c @@ -0,0 +1,28 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> +#include <pemagine/pemagine.h> + +wchar16_t * __cdecl __ntapi_tt_memcpy_utf16( + __in wchar16_t * dst, + __in wchar16_t * src, + __in size_t bytes) +{ + wchar16_t * wch_cap; + wchar16_t * wch_ret; + + wch_cap = (wchar16_t *)((uintptr_t)src + bytes); + wch_ret = dst; + + while (src < wch_cap) { + *dst = *src; + src++; + dst++; + } + + return wch_ret; +} diff --git a/src/string/ntapi_tt_string_null_offset.c b/src/string/ntapi_tt_string_null_offset.c new file mode 100644 index 0000000..3565acb --- /dev/null +++ b/src/string/ntapi_tt_string_null_offset.c @@ -0,0 +1,93 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> +#include <ntapi/nt_string.h> +#include "ntapi_impl.h" + +size_t __cdecl __ntapi_tt_string_null_offset_multibyte( + __in const char * str) +{ + const char * cap; + const uintptr_t * ptr; + + #define HIGH_BIT_TEST (uintptr_t)0x0101010101010101 + #define AND_BITS (uintptr_t)0x8080808080808080 + + cap = str; + while ((uintptr_t)cap % sizeof(uintptr_t)) { + if (!(*cap)) + return cap - str; + cap++; + } + + ptr = (uintptr_t *)cap; + while (!((*ptr - HIGH_BIT_TEST) & ~(*ptr) & AND_BITS)) + ptr++; + + cap = (const char *)ptr; + while (*cap) + cap++; + + return cap - str; +} + + +size_t __cdecl __ntapi_tt_string_null_offset_short( + __in const int16_t * str) +{ + const int16_t * cap; + + cap = str; + while (*cap) + cap++; + + return (size_t)cap - (size_t)str; +} + + +size_t __cdecl __ntapi_tt_string_null_offset_dword( + __in const int32_t * str) +{ + const int32_t * cap; + + cap = str; + while (*cap) + cap++; + + return (size_t)cap - (size_t)str; +} + +size_t __cdecl __ntapi_tt_string_null_offset_qword( + __in const int64_t * str) +{ + const int64_t * cap; + + cap = str; + while (*cap) + cap++; + + return (size_t)cap - (size_t)str; +} + +size_t __cdecl __ntapi_tt_string_null_offset_ptrsize( + __in const intptr_t *str) +{ + const intptr_t * cap; + + cap = str; + while (*cap) + cap++; + + return (size_t)cap - (size_t)str; +} + +size_t __cdecl __ntapi_wcslen(const wchar16_t * str) +{ + size_t len; + len = __ntapi_tt_string_null_offset_short((const int16_t *)str); + return len / 2; +} diff --git a/src/string/ntapi_tt_uintptr_to_hex_utf16.c b/src/string/ntapi_tt_uintptr_to_hex_utf16.c new file mode 100644 index 0000000..59e063f --- /dev/null +++ b/src/string/ntapi_tt_uintptr_to_hex_utf16.c @@ -0,0 +1,87 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> + +static void __fastcall __ntapi_tt_uint_to_hex_utf16( + __in uint64_t key, + __out wchar16_t * buffer, + __in unsigned bits) +{ + unsigned i; + uint32_t hex_buf[4]; + unsigned char * hex_chars; + unsigned char * uch; + unsigned offset; + unsigned bytes; + + hex_buf[0] = ('3' << 24) | ('2' << 16) | ('1' << 8) | '0'; + hex_buf[1] = ('7' << 24) | ('6' << 16) | ('5' << 8) | '4'; + hex_buf[2] = ('b' << 24) | ('a' << 16) | ('9' << 8) | '8'; + hex_buf[3] = ('f' << 24) | ('e' << 16) | ('d' << 8) | 'c'; + + uch = (unsigned char *)&key; + hex_chars = (unsigned char *)&hex_buf; + + bytes = bits / 8; + offset = bits / 4; + + for (i = 0; i < bytes; i++) { + buffer[offset - 1 - (i*2)] = hex_chars[uch[i] % 16]; + buffer[offset - 2 - (i*2)] = hex_chars[uch[i] / 16]; + } +} + + +void __fastcall __ntapi_tt_uint16_to_hex_utf16( + __in uint32_t key, + __out wchar16_t * formatted_key) +{ + __ntapi_tt_uint_to_hex_utf16( + key, + formatted_key, + 16); +} + + +void __fastcall __ntapi_tt_uint32_to_hex_utf16( + __in uint32_t key, + __out wchar16_t * formatted_key) +{ + __ntapi_tt_uint_to_hex_utf16( + key, + formatted_key, + 32); +} + + +void __fastcall __ntapi_tt_uint64_to_hex_utf16( + __in uint64_t key, + __out wchar16_t * formatted_key) +{ + __ntapi_tt_uint_to_hex_utf16( + key, + formatted_key, + 64); +} + + +void __fastcall __ntapi_tt_uintptr_to_hex_utf16( + __in uintptr_t key, + __out wchar16_t * formatted_key) +{ + #if defined (__NT32) + __ntapi_tt_uint_to_hex_utf16( + key, + formatted_key, + 32); + #elif defined (__NT64) + __ntapi_tt_uint_to_hex_utf16( + key, + formatted_key, + 64); + #endif +} diff --git a/src/string/ntapi_tt_uintptr_to_hex_utf8.c b/src/string/ntapi_tt_uintptr_to_hex_utf8.c new file mode 100644 index 0000000..b1e3141 --- /dev/null +++ b/src/string/ntapi_tt_uintptr_to_hex_utf8.c @@ -0,0 +1,73 @@ +/********************************************************/ +/* ntapi: Native API core library */ +/* Copyright (C) 2013,2014,2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */ +/********************************************************/ + +#include <psxtypes/psxtypes.h> + +static void __fastcall __ntapi_tt_uint_to_hex_utf8( + __in uint64_t key, + __out unsigned char * buffer, + __in unsigned bits) +{ + unsigned i; + uint32_t hex_buf[4]; + unsigned char * hex_chars; + unsigned char * uch; + unsigned offset; + unsigned bytes; + + /* avoid using .rdata for that one */ + hex_buf[0] = ('3' << 24) | ('2' << 16) | ('1' << 8) | '0'; + hex_buf[1] = ('7' << 24) | ('6' << 16) | ('5' << 8) | '4'; + hex_buf[2] = ('B' << 24) | ('A' << 16) | ('9' << 8) | '8'; + hex_buf[3] = ('F' << 24) | ('E' << 16) | ('D' << 8) | 'C'; + + uch = (unsigned char *)&key; + hex_chars = (unsigned char *)&hex_buf; + + bytes = bits / 8; + offset = bits / 4; + + for (i = 0; i < bytes; i++) { + buffer[offset - 1 - (i*2)] = hex_chars[uch[i] % 16]; + buffer[offset - 2 - (i*2)] = hex_chars[uch[i] / 16]; + } +} + + +void __fastcall __ntapi_tt_uint16_to_hex_utf8( + __in uint32_t key, + __out unsigned char * buffer) +{ + __ntapi_tt_uint_to_hex_utf8(key,buffer,16); +} + + +void __fastcall __ntapi_tt_uint32_to_hex_utf8( + __in uint32_t key, + __out unsigned char * buffer) +{ + __ntapi_tt_uint_to_hex_utf8(key,buffer,32); +} + + +void __fastcall __ntapi_tt_uint64_to_hex_utf8( + __in uint64_t key, + __out unsigned char * buffer) +{ + __ntapi_tt_uint_to_hex_utf8(key,buffer,64); +} + + +void __fastcall __ntapi_tt_uintptr_to_hex_utf8( + __in uintptr_t key, + __out unsigned char * buffer) +{ + #if defined (__NT32) + __ntapi_tt_uint_to_hex_utf8(key,buffer,32); + #elif defined (__NT64) + __ntapi_tt_uint_to_hex_utf8(key,buffer,64); + #endif +} |