Blame src/string/ntapi_tt_uintptr_to_dec.c

aac1ec
/********************************************************/
aac1ec
/*  ntapi: Native API core library                      */
aac1ec
/*  Copyright (C) 2013--2018  Z. Gilboa                 */
aac1ec
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
aac1ec
/********************************************************/
aac1ec
aac1ec
#include <psxtypes/psxtypes.h>
aac1ec
#include <ntapi/nt_status.h>
766a63
#include "ntapi_impl.h"
aac1ec
aac1ec
static void __tt_uintptr_to_mirrored_dec_utf8(
aac1ec
	uintptr_t	key,
aac1ec
	unsigned char *	buf,
aac1ec
	ssize_t *	arrlen)
aac1ec
{
aac1ec
	unsigned i;
aac1ec
aac1ec
	buf[0] = '0' + (key % 10);
aac1ec
	key   /= 10;
aac1ec
aac1ec
	for (i=1; key; i++) {
aac1ec
		buf[i] = '0' + (key % 10);
aac1ec
		key   /= 10;
aac1ec
	}
aac1ec
aac1ec
	*arrlen = i;
aac1ec
}
aac1ec
aac1ec
static int32_t __tt_uintptr_to_dec_utf16(
aac1ec
	uintptr_t	key,
aac1ec
	wchar16_t *	wstrbuf,
aac1ec
	size_t		buflen,
aac1ec
	ssize_t *	arrlen)
aac1ec
{
aac1ec
	ssize_t		i;
aac1ec
	ssize_t		slen;
aac1ec
	unsigned char	buf[24];
aac1ec
aac1ec
	__tt_uintptr_to_mirrored_dec_utf8(
aac1ec
		key,buf,arrlen);
aac1ec
aac1ec
	if ((i = *arrlen) > (slen = buflen))
aac1ec
		return NT_STATUS_BUFFER_TOO_SMALL;
aac1ec
aac1ec
	for (--i; i>=0; i--)
aac1ec
		*wstrbuf++ = buf[i];
aac1ec
aac1ec
	return NT_STATUS_SUCCESS;
aac1ec
}
aac1ec
aac1ec
static int32_t __tt_uintptr_to_dec_utf8(
aac1ec
	uintptr_t	key,
aac1ec
	unsigned char *	strbuf,
aac1ec
	size_t		buflen,
aac1ec
	ssize_t *	arrlen)
aac1ec
{
aac1ec
	ssize_t		i;
aac1ec
	ssize_t		slen;
aac1ec
	unsigned char	buf[24];
aac1ec
aac1ec
	__tt_uintptr_to_mirrored_dec_utf8(
aac1ec
		key,buf,arrlen);
aac1ec
aac1ec
	if ((i = *arrlen) > (slen = buflen))
aac1ec
		return NT_STATUS_BUFFER_TOO_SMALL;
aac1ec
aac1ec
	for (--i; i>=0; i--)
aac1ec
		*strbuf++ = buf[i];
aac1ec
aac1ec
	return NT_STATUS_SUCCESS;
aac1ec
}
aac1ec
aac1ec
int32_t __stdcall __ntapi_tt_uintptr_to_dec_utf16(
aac1ec
	__in	uintptr_t	key,
aac1ec
	__out	wchar16_t *	wstrbuf,
aac1ec
	__in	size_t		buflen,
aac1ec
	__out	ssize_t *	length)
aac1ec
{
aac1ec
	int32_t status;
aac1ec
	ssize_t	arrlen;
aac1ec
aac1ec
	buflen /= sizeof(wchar16_t);
aac1ec
aac1ec
	status = __tt_uintptr_to_dec_utf16(
aac1ec
		key,wstrbuf,
aac1ec
		buflen,&arrlen);
aac1ec
aac1ec
	*length = arrlen * sizeof(wchar16_t);
aac1ec
aac1ec
	return status;
aac1ec
}
aac1ec
aac1ec
int32_t __stdcall __ntapi_tt_uintptr_to_dec_utf8(
aac1ec
	__in	uintptr_t	key,
aac1ec
	__out	unsigned char *	strbuf,
aac1ec
	__in	size_t		buflen,
aac1ec
	__out	ssize_t *	length)
aac1ec
{
aac1ec
	return __tt_uintptr_to_dec_utf8(
aac1ec
		key,strbuf,
aac1ec
		buflen,length);
aac1ec
}
aac1ec
aac1ec
aac1ec
int32_t __stdcall __ntapi_tt_uintptr_to_dec_null_utf16(
aac1ec
	__in	uintptr_t	key,
aac1ec
	__out	wchar16_t *	wstrbuf,
aac1ec
	__in	size_t		buflen,
aac1ec
	__out	ssize_t *	length)
aac1ec
{
aac1ec
	int32_t status;
aac1ec
	ssize_t	arrlen;
aac1ec
aac1ec
	buflen -= sizeof(wchar16_t);
aac1ec
	buflen /= sizeof(wchar16_t);
aac1ec
aac1ec
	status = __tt_uintptr_to_dec_utf16(
aac1ec
		key,wstrbuf,
aac1ec
		buflen,&arrlen);
aac1ec
aac1ec
	if (status == NT_STATUS_SUCCESS)
aac1ec
		wstrbuf[arrlen] = 0;
aac1ec
aac1ec
	*length = ++arrlen * sizeof(wchar16_t);
aac1ec
aac1ec
	return status;
aac1ec
}
aac1ec
aac1ec
aac1ec
int32_t __stdcall __ntapi_tt_uintptr_to_dec_null_utf8(
aac1ec
	__in	uintptr_t	key,
aac1ec
	__out	unsigned char *	strbuf,
aac1ec
	__in	size_t		buflen,
aac1ec
	__out	ssize_t *	length)
aac1ec
{
aac1ec
	int32_t status;
aac1ec
	ssize_t	arrlen;
aac1ec
aac1ec
	status = __tt_uintptr_to_dec_utf8(
aac1ec
		key,strbuf,
aac1ec
		--buflen,&arrlen);
aac1ec
aac1ec
	if (status == NT_STATUS_SUCCESS)
aac1ec
		strbuf[arrlen] = 0;
aac1ec
aac1ec
	*length = ++arrlen;
aac1ec
aac1ec
	return status;
aac1ec
}