summaryrefslogtreecommitdiffhomepage
path: root/src/guid/ntapi_tt_guid.c
blob: 121210742fb6fd9c522bb689a23c587118fd83d4 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_time.h>
#include <ntapi/nt_guid.h>
#include "ntapi_impl.h"


void __fastcall __ntapi_tt_guid_copy(
	__out	nt_guid *	pguid_dst,
	__in	const nt_guid *	pguid_src)
{
	uint64_t *	dst;
	uint64_t *	src;

	dst = (uint64_t *)pguid_dst;
	src = (uint64_t *)pguid_src;

	*dst = *src;
	src++;	dst++;
	*dst = *src;
}


void __fastcall __ntapi_tt_guid_to_string_utf16(
	__in	const nt_guid *		guid,
	__out	nt_guid_str_utf16 *	guid_str)
{
	uint16_t	key;
	wchar16_t *	wch;

	wch = &(guid_str->group5[0]);

	__ntapi_tt_uint32_to_hex_utf16(
		guid->data1,
		&guid_str->group1[0]);

	__ntapi_tt_uint16_to_hex_utf16(
		guid->data2,
		&guid_str->group2[0]);

	__ntapi_tt_uint16_to_hex_utf16(
		guid->data3,
		&guid_str->group3[0]);

	key = guid->data4[0] * 0x100 + guid->data4[1];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&guid_str->group4[0]);

	key = guid->data4[2] * 0x100 + guid->data4[3];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&guid_str->group5[0]);

	key = guid->data4[4] * 0x100 + guid->data4[5];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&(wch[4]));

	key = guid->data4[6] * 0x100 + guid->data4[7];

	__ntapi_tt_uint16_to_hex_utf16(
		key,
		&(wch[8]));

	guid_str->lbrace = '{';
	guid_str->rbrace = '}';
	guid_str->dash1 = '-';
	guid_str->dash2 = '-';
	guid_str->dash3 = '-';
	guid_str->dash4 = '-';

	return;
}


int32_t __fastcall __ntapi_tt_guid_compare(
	__in	const nt_guid *	pguid_dst,
	__in	const nt_guid *	pguid_src)
{
	uint64_t *	dst;
	uint64_t *	src;

	dst = (uint64_t *)pguid_dst;
	src = (uint64_t *)pguid_src;

	if ((*dst != *src) || (*(++dst) != *(++src)))
		return NT_STATUS_OBJECT_TYPE_MISMATCH;

	return NT_STATUS_SUCCESS;
}


int32_t __fastcall __ntapi_tt_string_to_guid_utf16(
	__in	const nt_guid_str_utf16 *	guid_str,
	__out	nt_guid *			guid)
{
	int32_t			status;
	const wchar16_t *	wch;
	uint16_t		key;

	/* outer braces / outer underscores / outer dashes */
	if ((guid_str->lbrace == '{') && (guid_str->rbrace == '}'))
		(void)0;

	else if ((guid_str->lbrace == '_') && (guid_str->rbrace == '_'))
		(void)0;

	else if ((guid_str->lbrace == '-') && (guid_str->rbrace == '-'))
		(void)0;

	else
		return NT_STATUS_INVALID_PARAMETER;

	/* inner dashes */
	if ((guid_str->dash1 != '-')
			|| (guid_str->dash2 != '-')
			|| (guid_str->dash3 != '-')
			|| (guid_str->dash4 != '-'))
		return NT_STATUS_INVALID_PARAMETER;

	wch = &(guid_str->group5[0]);

	status = __ntapi_tt_hex_utf16_to_uint32(
		guid_str->group1,
		&guid->data1);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group2,
		&guid->data2);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group3,
		&guid->data3);

	if (status != NT_STATUS_SUCCESS)
		return status;

	status = __ntapi_tt_hex_utf16_to_uint16(
		guid_str->group4,
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[0] = key >> 8;
	guid->data4[1] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[0]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[2] = key >> 8;
	guid->data4[3] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[4]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[4] = key >> 8;
	guid->data4[5] = key % 0x100;

	status = __ntapi_tt_hex_utf16_to_uint16(
		&(wch[8]),
		&key);

	if (status != NT_STATUS_SUCCESS)
		return status;

	guid->data4[6] = key >> 8;
	guid->data4[7] = key % 0x100;

	return NT_STATUS_SUCCESS;
}

int32_t __fastcall __ntapi_tt_string_to_guid_utf8(
	__in	const char *	guid_str,
	__out	nt_guid *	guid)
{
	const char *		ch;
	const char *		cap;
	wchar16_t *		wch;
	nt_guid_str_utf16	guid_str_utf16;

	ch  = guid_str;
	cap = &ch[sizeof(guid_str_utf16) / sizeof(wchar16_t)];
	wch = &guid_str_utf16.lbrace;

	for (; *ch && (ch<cap); )
		*wch++ = *ch++;

	return (ch < cap)
		? NT_STATUS_INVALID_PARAMETER
		: __ntapi_tt_string_to_guid_utf16(
			&guid_str_utf16,guid);
}

void __fastcall __ntapi_tt_guid_to_string_utf8(
	__in	const nt_guid *		guid,
	__out	nt_guid_str_utf8 *	guid_str)
{
	uint16_t	key;
	uint8_t *	ch;

	ch = &(guid_str->group5[0]);

	__ntapi_tt_uint32_to_hex_utf8(
		guid->data1,
		&guid_str->group1[0]);

	__ntapi_tt_uint16_to_hex_utf8(
		guid->data2,
		&guid_str->group2[0]);

	__ntapi_tt_uint16_to_hex_utf8(
		guid->data3,
		&guid_str->group3[0]);

	key = guid->data4[0] * 0x100 + guid->data4[1];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&guid_str->group4[0]);

	key = guid->data4[2] * 0x100 + guid->data4[3];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&guid_str->group5[0]);

	key = guid->data4[4] * 0x100 + guid->data4[5];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&(ch[4]));

	key = guid->data4[6] * 0x100 + guid->data4[7];

	__ntapi_tt_uint16_to_hex_utf8(
		key,
		&(ch[8]));

	guid_str->lbrace = '{';
	guid_str->rbrace = '}';
	guid_str->dash1 = '-';
	guid_str->dash2 = '-';
	guid_str->dash3 = '-';
	guid_str->dash4 = '-';
	guid_str->null  = 0;
}