Blame src/process/ntapi_tt_get_runtime_data.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
4256e2
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_memory.h>
dd89bb
#include <ntapi/nt_process.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
#if defined (__NT32)
dd89bb
static wchar16_t runtime_arg[12] = {
dd89bb
	' ','-','r',' ',
dd89bb
	'i','n','t','e','g','r','a','l'};
dd89bb
#elif defined (__NT64)
dd89bb
static wchar16_t runtime_arg[20] = {
dd89bb
	' ','-','r',' ',
dd89bb
	'i','n','t','e','g','r','a','l',
dd89bb
	'-','r','u','n','t','i','m','e'};
dd89bb
#endif
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_get_runtime_data(
dd89bb
	__out		nt_runtime_data **	rtdata,
dd89bb
	__in		wchar16_t **		argv)
dd89bb
{
dd89bb
	int32_t				status;
dd89bb
	nt_process_parameters *		process_params;
dd89bb
	nt_cmd_option_meta_utf16	cmd_opt_meta;
dd89bb
	nt_runtime_data			buffer;
dd89bb
	nt_runtime_data *		prtdata;
dd89bb
	ntapi_internals *		__internals;
dd89bb
dd89bb
	/* init */
dd89bb
	__internals = __ntapi_internals();
dd89bb
dd89bb
	/* once? */
dd89bb
	if (__internals->rtdata) {
dd89bb
		*rtdata = __internals->rtdata;
dd89bb
		return NT_STATUS_SUCCESS;
dd89bb
	}
dd89bb
dd89bb
	if (!(argv = argv ? argv : __internals->ntapi_img_sec_bss->argv_envp_array))
dd89bb
		return NT_STATUS_INVALID_PARAMETER_2;
dd89bb
dd89bb
	/* integral process? */
dd89bb
	if ((status = __ntapi->tt_get_short_option_meta_utf16(
dd89bb
			__ntapi->tt_crc32_table(),
dd89bb
			'r',
dd89bb
			argv,
dd89bb
			&cmd_opt_meta)))
dd89bb
		return status;
dd89bb
dd89bb
	else if (argv[3])
dd89bb
		status = NT_STATUS_INVALID_PARAMETER_MIX;
dd89bb
dd89bb
	if ((status = __ntapi->tt_hex_utf16_to_uintptr(
dd89bb
			cmd_opt_meta.value,
dd89bb
			(uintptr_t *)&prtdata)))
dd89bb
		return status;
dd89bb
dd89bb
	if ((status = __ntapi->zw_read_virtual_memory(
dd89bb
			NT_CURRENT_PROCESS_HANDLE,
dd89bb
			prtdata,
dd89bb
			(char *)&buffer,
dd89bb
			sizeof(buffer),0)))
dd89bb
		return status;
dd89bb
dd89bb
	/* avoid confusion :-) */
dd89bb
	process_params = ((nt_peb *)pe_get_peb_address())->process_params;
dd89bb
dd89bb
	__ntapi->tt_memcpy_utf16(
dd89bb
		(wchar16_t *)pe_va_from_rva(
dd89bb
			process_params->command_line.buffer,
dd89bb
			process_params->command_line.strlen - sizeof(runtime_arg)),
dd89bb
		runtime_arg,
dd89bb
		sizeof(runtime_arg));
dd89bb
dd89bb
	*rtdata = prtdata;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}