Blame src/argv/ntapi_tt_array_utf8.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <pemagine/pemagine.h>
dd89bb
#include <ntapi/nt_argv.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_array_copy_utf8(
dd89bb
	__out	int *			argc,
dd89bb
	__in	const char **		argv,
dd89bb
	__in	const char **		envp,
15812f
	__in	const char *		interp,
15812f
	__in	const char *		optarg,
15812f
	__in	const char *		script,
dd89bb
	__in	void *			base,
dd89bb
	__out	void *			buffer,
dd89bb
	__in	size_t			buflen,
dd89bb
	__out	size_t *		blklen)
dd89bb
{
dd89bb
	const char **	parg;
dd89bb
	const char *	arg;
15812f
	const char *	mark;
dd89bb
	char *		ch;
dd89bb
	ptrdiff_t	diff;
dd89bb
	ptrdiff_t	ptrs;
dd89bb
	size_t		needed;
15812f
	const char *	dummy[2] = {0,0};
c713d8
dd89bb
	/* fallback */
15812f
	argv = argv ? argv : dummy;
15812f
	envp = envp ? envp : dummy;
dd89bb
dd89bb
	/* ptrs, needed */
dd89bb
	ptrs   = 0;
dd89bb
	needed = 0;
dd89bb
15812f
	/* interpr */
15812f
	if (interp) {
15812f
		ptrs++;
15812f
		needed += sizeof(char *)
15812f
			+ __ntapi->tt_string_null_offset_multibyte(interp)
15812f
			+ sizeof(char);
15812f
	}
15812f
15812f
	/* optarg */
15812f
	if (optarg) {
dd89bb
		ptrs++;
dd89bb
		needed += sizeof(char *)
15812f
			+ __ntapi->tt_string_null_offset_multibyte(optarg)
dd89bb
			+ sizeof(char);
dd89bb
	}
dd89bb
15812f
	/* script / argv[0] */
15812f
	if ((mark = script ? script : argv[0])) {
15812f
		ptrs++;
15812f
		needed += sizeof(char *)
15812f
			+ __ntapi->tt_string_null_offset_multibyte(mark)
15812f
			+ sizeof(char);
15812f
	}
15812f
15812f
	/* argv */
15812f
	for (parg=&argv[1]; *parg; parg++)
dd89bb
		needed += sizeof(char *)
dd89bb
			+ __ntapi->tt_string_null_offset_multibyte(*parg)
dd89bb
			+ sizeof(char);
dd89bb
15812f
	ptrs += (parg - &argv[1]);
dd89bb
	*argc = (int)ptrs;
dd89bb
15812f
	/* envp */
dd89bb
	for (parg=envp; *parg; parg++)
dd89bb
		needed += sizeof(char *)
dd89bb
			+ __ntapi->tt_string_null_offset_multibyte(*parg)
dd89bb
			+ sizeof(char);
dd89bb
dd89bb
	ptrs += (parg - envp);
dd89bb
15812f
	ptrs   += 2;
15812f
	needed += 2*sizeof(char *);
dd89bb
	blklen  = blklen ? blklen : &needed;
dd89bb
	*blklen = needed;
dd89bb
dd89bb
	if (buflen < needed)
dd89bb
		return NT_STATUS_BUFFER_TOO_SMALL;
dd89bb
dd89bb
	/* init */
dd89bb
	parg = (const char **)buffer;
15812f
	ch   = (char *)(parg+ptrs);
dd89bb
	diff = (ptrdiff_t)base;
dd89bb
15812f
	/* interp */
15812f
	if (interp) {
15812f
		*parg++ = ch-diff;
15812f
		for (arg=interp; *arg; arg++,ch++)
15812f
			*ch = *arg;
15812f
		*ch++ = '\0';
15812f
	}
15812f
15812f
	/* optarg */
15812f
	if (optarg) {
15812f
		*parg++ = ch-diff;
15812f
		for (arg=optarg; *arg; arg++,ch++)
15812f
			*ch = *arg;
15812f
		*ch++ = '\0';
15812f
	}
15812f
15812f
	/* script / argv[0] */
15812f
	if ((mark = script ? script : argv[0])) {
dd89bb
		*parg++ = ch-diff;
15812f
		for (arg=mark; *arg; arg++,ch++)
dd89bb
			*ch = *arg;
dd89bb
		*ch++ = '\0';
dd89bb
	}
dd89bb
dd89bb
	/* argv */
15812f
	for (++argv; *argv; argv++) {
dd89bb
		*parg++=ch-diff;
dd89bb
		for (arg=*argv; *arg; arg++,ch++)
dd89bb
			*ch = *arg;
dd89bb
		*ch++ = '\0';
dd89bb
	}
dd89bb
dd89bb
	*parg++ = 0;
dd89bb
dd89bb
	/* envp */
dd89bb
	for (; *envp; envp++) {
dd89bb
		*parg++=ch-diff;
dd89bb
		for (arg=*envp; *arg; arg++,ch++)
dd89bb
			*ch = *arg;
dd89bb
		*ch++ = '\0';
dd89bb
	}
dd89bb
dd89bb
	*parg++ = 0;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_array_convert_utf8_to_utf16(
dd89bb
	__in		char **		arrv,
dd89bb
	__in		wchar16_t **	arra,
dd89bb
	__in		void *		base,
dd89bb
	__in		wchar16_t *	buffer,
dd89bb
	__in		size_t		buffer_len,
dd89bb
	__out		size_t *	bytes_written)
dd89bb
{
c713d8
	(void)arrv;
c713d8
	(void)arra;
c713d8
	(void)base;
c713d8
	(void)buffer;
c713d8
	(void)buffer_len;
1cf951
1cf951
	*bytes_written = 0;
c713d8
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}