Blame src/string/ntapi_tt_string_null_offset.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dd89bb
/*  Copyright (C) 2013,2014,2015  Z. Gilboa             */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_string.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
size_t __cdecl __ntapi_tt_string_null_offset_multibyte(
dd89bb
	__in	const char *	str)
dd89bb
{
dd89bb
	const char *		cap;
dd89bb
	const uintptr_t *	ptr;
dd89bb
dd89bb
	#define HIGH_BIT_TEST	(uintptr_t)0x0101010101010101
dd89bb
	#define	AND_BITS	(uintptr_t)0x8080808080808080
dd89bb
dd89bb
	cap = str;
dd89bb
	while ((uintptr_t)cap % sizeof(uintptr_t)) {
dd89bb
		if (!(*cap))
dd89bb
			return cap - str;
dd89bb
		cap++;
dd89bb
	}
dd89bb
dd89bb
	ptr = (uintptr_t *)cap;
dd89bb
	while (!((*ptr - HIGH_BIT_TEST) & ~(*ptr) & AND_BITS))
dd89bb
		ptr++;
dd89bb
dd89bb
	cap = (const char *)ptr;
dd89bb
	while (*cap)
dd89bb
		cap++;
dd89bb
dd89bb
	return cap - str;
dd89bb
}
dd89bb
dd89bb
dd89bb
size_t __cdecl __ntapi_tt_string_null_offset_short(
dd89bb
	__in	const int16_t *	str)
dd89bb
{
dd89bb
	const int16_t *	cap;
dd89bb
dd89bb
	cap = str;
dd89bb
	while (*cap)
dd89bb
		cap++;
dd89bb
dd89bb
	return (size_t)cap - (size_t)str;
dd89bb
}
dd89bb
dd89bb
dd89bb
size_t __cdecl __ntapi_tt_string_null_offset_dword(
dd89bb
	__in	const int32_t *	str)
dd89bb
{
dd89bb
	const int32_t *	cap;
dd89bb
dd89bb
	cap = str;
dd89bb
	while (*cap)
dd89bb
		cap++;
dd89bb
dd89bb
	return (size_t)cap - (size_t)str;
dd89bb
}
dd89bb
dd89bb
size_t __cdecl __ntapi_tt_string_null_offset_qword(
dd89bb
	__in	const int64_t *	str)
dd89bb
{
dd89bb
	const int64_t *	cap;
dd89bb
dd89bb
	cap = str;
dd89bb
	while (*cap)
dd89bb
		cap++;
dd89bb
dd89bb
	return (size_t)cap - (size_t)str;
dd89bb
}
dd89bb
dd89bb
size_t __cdecl __ntapi_tt_string_null_offset_ptrsize(
dd89bb
	__in	const intptr_t *str)
dd89bb
{
dd89bb
	const intptr_t * cap;
dd89bb
dd89bb
	cap = str;
dd89bb
	while (*cap)
dd89bb
		cap++;
dd89bb
dd89bb
	return (size_t)cap - (size_t)str;
dd89bb
}
dd89bb
dd89bb
size_t __cdecl __ntapi_wcslen(const wchar16_t * str)
dd89bb
{
dd89bb
	size_t len;
dd89bb
	len = __ntapi_tt_string_null_offset_short((const int16_t *)str);
dd89bb
	return len / 2;
dd89bb
}