summaryrefslogtreecommitdiffhomepage
path: root/src/argv/ntapi_tt_array_utf16.c
blob: 5914657359d19db8c5dd97f42e6b395fafe18f71 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2021  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>
#include <ntapi/nt_argv.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

/**
 * scenario: program -e app [arg1 arg2 ... argn]
 * input:    a utf-16 argument vector
 * output:   a utf-16 cmd_line string
 * example:  tty_pipe_create_child_process
**/

int32_t __stdcall __ntapi_tt_array_copy_utf16(
	__out	int *			argc,
	__in	const wchar16_t **	wargv,
	__in	const wchar16_t **	wenvp,
	__in	const wchar16_t *	interp,
	__in	const wchar16_t *	optarg,
	__in	const wchar16_t *	script,
	__in	void *			base,
	__out	void *			buffer,
	__in	size_t			buflen,
	__out	size_t *		blklen)
{
	const wchar16_t **	parg;
	const wchar16_t *	warg;
	const wchar16_t *	mark;
	wchar16_t *		wch;
	ptrdiff_t		diff;
	ptrdiff_t		ptrs;
	size_t			needed;
	const wchar16_t *	dummy[2] = {0,0};

	/* fallback */
	wargv = wargv ? wargv : dummy;
	wenvp = wenvp ? wenvp : dummy;

	/* ptrs, needed */
	ptrs   = 0;
	needed = 0;

	/* interp */
	if (interp) {
		ptrs++;
		needed += sizeof(wchar16_t *)
			+ __ntapi->tt_string_null_offset_short((const int16_t *)interp)
			+ sizeof(wchar16_t);
	}

	/* optarg */
	if (interp) {
		ptrs++;
		needed += sizeof(wchar16_t *)
			+ __ntapi->tt_string_null_offset_short((const int16_t *)optarg)
			+ sizeof(wchar16_t);
	}

	/* script / wargv[0] */
	 if ((mark = script ? script : wargv[0])) {
		ptrs++;
		needed += sizeof(wchar16_t *)
			+ __ntapi->tt_string_null_offset_short((const int16_t *)mark)
			+ sizeof(wchar16_t);
	}

	/* wargv */
	for (parg=&wargv[1]; *parg; parg++)
		needed += sizeof(wchar16_t *)
			+ __ntapi->tt_string_null_offset_short((const int16_t *)*parg)
			+ sizeof(wchar16_t);

	ptrs += (parg - &wargv[1]);
	*argc = (int)ptrs;

	/* wenvp */
	for (parg=wenvp; *parg; parg++)
		needed += sizeof(wchar16_t *)
			+ __ntapi->tt_string_null_offset_short((const int16_t *)*parg)
			+ sizeof(wchar16_t);

	ptrs += (parg - wenvp);

	ptrs   += 2;
	needed += 2*sizeof(wchar16_t *);
	blklen  = blklen ? blklen : &needed;
	*blklen = needed;

	if (buflen < needed)
		return NT_STATUS_BUFFER_TOO_SMALL;

	/* init */
	parg = (const wchar16_t **)buffer;
	wch  = (wchar16_t *)(parg+ptrs);
	diff = (uintptr_t)base / sizeof(wchar16_t);

	/* interp */
	if (interp) {
		*parg++ = wch-diff;
		for (warg=interp; *warg; warg++,wch++)
			*wch = *warg;
		*wch++ = '\0';
	}

	/* optarg */
	if (optarg) {
		*parg++ = wch-diff;
		for (warg=optarg; *warg; warg++,wch++)
			*wch = *warg;
		*wch++ = '\0';
	}

	/* script / wargv[0] */
	if ((mark = script ? script : wargv[0])) {
		*parg++ = wch-diff;
		for (warg=mark; *warg; warg++,wch++)
			*wch = *warg;
		*wch++ = '\0';
	}

	/* wargv */
	for (++wargv; *wargv; wargv++) {
		*parg++=wch-diff;
		for (warg=*wargv; *warg; warg++,wch++)
			*wch = *warg;
		*wch++ = '\0';
	}

	*parg++ = 0;

	/* wenvp */
	for (; *wenvp; wenvp++) {
		*parg++=wch-diff;
		for (warg=*wenvp; *warg; warg++,wch++)
			*wch = *warg;
		*wch++ = '\0';
	}

	*parg++ = 0;

	return NT_STATUS_SUCCESS;
}

int32_t __stdcall __ntapi_tt_array_convert_utf16_to_utf8(
	__in		wchar16_t **			warrv,
	__in		char **				arrv,
	__in		void *				base,
	__in		char *				buffer,
	__in		size_t				buffer_len,
	__out		size_t *			bytes_written)
{
	uint8_t *	ubound;
	uint8_t *	ch;
	wchar16_t *	wch;
	wchar16_t	wx;
	wchar16_t	wy;
	wchar16_t	wz;
	wchar16_t	wy_low;
	wchar16_t	wy_high;
	wchar16_t	ww;
	wchar16_t	uuuuu;
	wchar16_t	u_low;
	wchar16_t	u_high;
	ptrdiff_t	diff;

	ch	= (uint8_t *)buffer;
	ubound	= (uint8_t *)buffer + buffer_len - 5;
	diff	= (uintptr_t)base / sizeof(wchar16_t);

	for (; warrv && *warrv; arrv++,warrv++) {
		*arrv	= (char *)(ch-(uintptr_t)base);
		wch	= *warrv + diff;

		/* ubound already accounts for null termination, see above */
		for (; *wch && (ch<ubound); ) {
			if (*wch <= 0x7F) {
				/* from: 00000000 0xxxxxxx (little endian) */
				/* to:   0xxxxxxx          (utf-8)    */
				*ch = (char)(*wch);
			} else if (*wch <= 0x7FF) {
				/* from: 00000yyy yyxxxxxx (little endian) */
				/* to:   110yyyyy 10xxxxxx (utf-8)    */
				wy  = *wch;
				wy >>= 6;

				wx  = *wch;
				wx <<= 10;
				wx >>= 10;

				/* write the y part */
				*ch = (char)(0xC0 | wy);
				ch++;

				/* write the x part */
				*ch = (char)(0x80 | wx);
			} else if ((*wch < 0xD800) || (*wch >= 0xE000)) {
				/* from: zzzzyyyy yyxxxxxx (little endian)  */
				/* to:   1110zzzz 10yyyyyy 10xxxxxx (utf-8) */
				wz  = *wch;
				wz >>= 12;

				wy  = *wch;
				wy <<= 4;
				wy >>= 10;

				wx  = *wch;
				wx <<= 10;
				wx >>= 10;

				/* write the z part */
				*ch = (char)(0xE0 | wz);
				ch++;

				/* write the y part */
				*ch = (char)(0x80 | wy);
				ch++;

				/* write the x part */
				*ch = (char)(0x80 | wx);
			} else if (wch[0] >= 0xDC00) {
				return NT_STATUS_ILLEGAL_CHARACTER;
			} else if (wch[1] < 0xDC00) {
				return NT_STATUS_ILLEGAL_CHARACTER;
			} else if (wch[1] >= 0xE000) {
				return NT_STATUS_ILLEGAL_CHARACTER;
			} else {
				/* from: 110110ww  wwzzzzyy  110111yy  yyxxxxxx (little endian) */
				/* to:   11110uuu  10uuzzzz  10yyyyyy  10xxxxxx (utf-8)         */

				/* low two bytes */
				wy_high   = *wch;
				wy_high <<= 14;
				wy_high >>= 10;

				wz   = *wch;
				wz <<= 10;
				wz >>= 12;

				ww   = *wch;
				ww <<= 6;
				ww >>= 12;

				/* (surrogate pair) */
				wch++;

				/* high two bytes */
				wx   = *wch;
				wx <<= 10;
				wx >>= 10;

				wy_low   = *wch;
				wy_low <<= 6;
				wy_low >>= 12;

				/* uuuuu */
				uuuuu    = ww + 1;
				u_low    = uuuuu;
				u_low  >>= 2;

				u_high  = uuuuu;
				u_high <<= 14;
				u_high >>= 10;

				/* 1st byte: 11110uuu */
				*ch++ = (char)(0xF0 | u_low);

				/* 2nd byte: 10uuzzzz */
				*ch++ = (char)(0x80 | u_high | wz);

				/* 3rd byte: 10yyyyyy */
				*ch++ = (char)(0x80 | wy_low | wy_high);

				/* 4th byte: 10xxxxxx */
				*ch = (char)(0x80 | wx);
			}

			ch++;
			wch++;
		}

		if (*wch)
			return NT_STATUS_BUFFER_TOO_SMALL;

		*ch++ = 0;
	}

	*arrv = 0;
	*bytes_written = (size_t)(ch - (uint8_t *)buffer);

	return NT_STATUS_SUCCESS;
}