Blob Blame History Raw
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  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 *		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;
}