summaryrefslogtreecommitdiffhomepage
path: root/src/argv/ntapi_tt_array_utf8.c
blob: b1c95b5ecfff9fca2342ef62daed7b79031a94b7 (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
/********************************************************/
/*  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 <pemagine/pemagine.h>
#include <ntapi/nt_argv.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

int32_t __stdcall __ntapi_tt_array_copy_utf8(
	__out	int *			argc,
	__in	const char **		argv,
	__in	const char **		envp,
	__in	const char *		interp,
	__in	const char *		optarg,
	__in	const char *		script,
	__in	void *			base,
	__out	void *			buffer,
	__in	size_t			buflen,
	__out	size_t *		blklen)
{
	const char **	parg;
	const char *	arg;
	const char *	mark;
	char *		ch;
	ptrdiff_t	diff;
	ptrdiff_t	ptrs;
	size_t		needed;
	const char *	dummy[2] = {0,0};

	/* fallback */
	argv = argv ? argv : dummy;
	envp = envp ? envp : dummy;

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

	/* interpr */
	if (interp) {
		ptrs++;
		needed += sizeof(char *)
			+ __ntapi->tt_string_null_offset_multibyte(interp)
			+ sizeof(char);
	}

	/* optarg */
	if (optarg) {
		ptrs++;
		needed += sizeof(char *)
			+ __ntapi->tt_string_null_offset_multibyte(optarg)
			+ sizeof(char);
	}

	/* script / argv[0] */
	if ((mark = script ? script : argv[0])) {
		ptrs++;
		needed += sizeof(char *)
			+ __ntapi->tt_string_null_offset_multibyte(mark)
			+ sizeof(char);
	}

	/* argv */
	for (parg=&argv[1]; *parg; parg++)
		needed += sizeof(char *)
			+ __ntapi->tt_string_null_offset_multibyte(*parg)
			+ sizeof(char);

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

	/* envp */
	for (parg=envp; *parg; parg++)
		needed += sizeof(char *)
			+ __ntapi->tt_string_null_offset_multibyte(*parg)
			+ sizeof(char);

	ptrs += (parg - envp);

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

	if (buflen < needed)
		return NT_STATUS_BUFFER_TOO_SMALL;

	/* init */
	parg = (const char **)buffer;
	ch   = (char *)(parg+ptrs);
	diff = (ptrdiff_t)base;

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

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

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

	/* argv */
	for (++argv; *argv; argv++) {
		*parg++=ch-diff;
		for (arg=*argv; *arg; arg++,ch++)
			*ch = *arg;
		*ch++ = '\0';
	}

	*parg++ = 0;

	/* envp */
	for (; *envp; envp++) {
		*parg++=ch-diff;
		for (arg=*envp; *arg; arg++,ch++)
			*ch = *arg;
		*ch++ = '\0';
	}

	*parg++ = 0;

	return NT_STATUS_SUCCESS;
}

int32_t __stdcall __ntapi_tt_array_convert_utf8_to_utf16(
	__in		char **		arrv,
	__in		wchar16_t **	arra,
	__in		void *		base,
	__in		wchar16_t *	buffer,
	__in		size_t		buffer_len,
	__out		size_t *	bytes_written)
{
	(void)arrv;
	(void)arra;
	(void)base;
	(void)buffer;
	(void)buffer_len;

	*bytes_written = 0;

	return NT_STATUS_SUCCESS;
}