Blob Blame History Raw
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2018  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_status.h>
#include "ntapi_impl.h"

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;
}