summaryrefslogtreecommitdiffhomepage
path: root/src/string/ntapi_tt_dec_utf16_to_uintptr.c
blob: 3e9b85dc75aa026145328a29bebc84ab39d89e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/********************************************************/
/*  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 int32_t __tt_dec_utf16_to_uint64(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uint64_t *		key)
{
	int32_t			status;
	uint64_t		lkey;
	uint64_t		hkey;
	const wchar16_t *	wch;

	if (wch_cap <= wch_arg)
		return NT_STATUS_INVALID_PARAMETER;

	else if (wch_cap - wch_arg > 20)
		return NT_STATUS_INVALID_PARAMETER;

	lkey = 0;
	hkey = 0;

	if (wch_cap - wch_arg == 20) {
		if ((status = __tt_dec_utf16_to_uint64(
				&wch_arg[6],wch_cap,
				&hkey)))
			return status;

		if (hkey > 18446744073709)
			return NT_STATUS_INTEGER_OVERFLOW;

		hkey   *= 1000000;
		wch_cap = &wch_arg[6];
	}

	for (wch=wch_arg; wch<wch_cap; wch++) {
		if ((*wch < '0') || (*wch > '9'))
			return NT_STATUS_INVALID_PARAMETER;

		lkey *= 10;
		lkey += *wch - '0';
	}

	*key = lkey + hkey;

	return NT_STATUS_SUCCESS;
}

int32_t __stdcall __ntapi_tt_dec_utf16_to_uint16(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uint16_t *		key)
{
	int32_t		status;
	uint64_t	skey;
	uint16_t	rkey;

	if ((status = __tt_dec_utf16_to_uint64(wch_arg,wch_cap,&skey)))
		return status;

	if ((rkey = skey) < skey)
		return NT_STATUS_INTEGER_OVERFLOW;

	*key = rkey;

	return NT_STATUS_SUCCESS;
}

int32_t __stdcall __ntapi_tt_dec_utf16_to_uint32(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uint32_t *		key)
{
	int32_t		status;
	uint64_t	skey;
	uint32_t	rkey;

	if ((status = __tt_dec_utf16_to_uint64(wch_arg,wch_cap,&skey)))
		return status;

	if ((rkey = skey) < skey)
		return NT_STATUS_INTEGER_OVERFLOW;

	*key = rkey;

	return NT_STATUS_SUCCESS;
}



int32_t __stdcall __ntapi_tt_dec_utf16_to_uint64(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uint64_t *		key)
{
	return __tt_dec_utf16_to_uint64(
		wch_arg,wch_cap,key);
}

#ifdef __NT32
int32_t __stdcall __ntapi_tt_dec_utf16_to_uintptr(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uintptr_t *		key)
{
	return __tt_dec_utf16_to_uint32(
		wch_arg,wch_cap,key);
}
#endif

#ifdef __NT64
int32_t __stdcall __ntapi_tt_dec_utf16_to_uintptr(
	__in	const wchar16_t *	wch_arg,
	__in	const wchar16_t *	wch_cap,
	__out	uintptr_t *		key)
{
	return __tt_dec_utf16_to_uint64(
		wch_arg,wch_cap,key);
}
#endif