summaryrefslogtreecommitdiffhomepage
path: root/src/argv/ntapi_tt_array_utf8.c
blob: 8cea00b11f55f297558641b16bfabc9a6a5e6ced (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  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"

int32_t __stdcall __ntapi_tt_array_copy_utf8(
	__out	int *			argc,
	__in	const char **		argv,
	__in	const char **		envp,
	__in	const char *		image_name	__optional,
	__in	const char *		interpreter	__optional,
	__in	const char *		optarg		__optional,
	__in	void *			base,
	__out	void *			buffer,
	__in	size_t			buflen,
	__out	size_t *		blklen)
{
	const char **	parg;
	const char *	arg;
	const char *	dummy;
	char *		ch;
	ptrdiff_t	diff;
	ptrdiff_t	ptrs;
	size_t		needed;

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

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

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

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

	ptrs += (parg - argv);
	*argc = (int)ptrs;

	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;

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

	/* argv */
	for (; *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)
{
	return NT_STATUS_SUCCESS;
}