Blame src/pty/ntapi_pty_io.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_port.h>
dd89bb
#include <ntapi/nt_tty.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
#include "ntapi_pty.h"
dd89bb
dd89bb
static int32_t __stdcall __ntapi_pty_read_write(
dd89bb
	nt_pty *		pty,
dd89bb
	void *			hevent,
dd89bb
	nt_io_apc_routine *	apc_routine,
dd89bb
	void *			apc_context,
dd89bb
	nt_iosb *		iosb,
dd89bb
	void *			buffer,
dd89bb
	size_t			nbytes,
dd89bb
	nt_large_integer *	offset,
dd89bb
	uint32_t *		key,
dd89bb
	int32_t			opcode)
dd89bb
{
dd89bb
	int32_t		status;
dd89bb
	nt_pty_io_msg	msg;
dd89bb
	off_t		soffset;
dd89bb
	int		mode;
dd89bb
dd89bb
	mode = opcode - NT_TTY_PTY_READ;
dd89bb
	soffset = mode * pty->section_size / 2;
dd89bb
dd89bb
	if (offset && offset->quad)
dd89bb
		return NT_STATUS_INVALID_PARAMETER;
dd89bb
dd89bb
	nbytes = nbytes <= pty->section_size / 2
dd89bb
		? nbytes
dd89bb
		: pty->section_size / 2;
dd89bb
dd89bb
	__ntapi->tt_aligned_block_memset(
dd89bb
		&msg,0,sizeof(msg));
dd89bb
dd89bb
	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
dd89bb
	msg.header.data_size		= sizeof(msg.data);
dd89bb
	msg.header.msg_size		= sizeof(msg);
dd89bb
	msg.data.ttyinfo.opcode		= opcode;
dd89bb
dd89bb
	msg.data.ioinfo.hpty		= pty->hpty;
dd89bb
	msg.data.ioinfo.hevent		= hevent;
dd89bb
	msg.data.ioinfo.apc_routine	= apc_routine;
dd89bb
	msg.data.ioinfo.apc_context	= apc_context;
dd89bb
	msg.data.ioinfo.key		= key ? *key : 0;
dd89bb
dd89bb
	msg.data.ioinfo.luid.high	= pty->luid.high;
dd89bb
	msg.data.ioinfo.luid.low	= pty->luid.low;
dd89bb
dd89bb
	msg.data.ioinfo.riosb		= iosb;
dd89bb
	msg.data.ioinfo.raddr		= buffer;
dd89bb
dd89bb
	__ntapi->tt_guid_copy(
dd89bb
		&msg.data.ioinfo.guid,
dd89bb
		&pty->guid);
dd89bb
dd89bb
	msg.data.ioinfo.nbytes		= nbytes;
dd89bb
	msg.data.ioinfo.offset		= soffset;
dd89bb
dd89bb
	if (mode == __PTY_WRITE)
dd89bb
		__ntapi->tt_generic_memcpy(
dd89bb
			(char *)pty->section_addr + soffset,
dd89bb
			(char *)buffer,
dd89bb
			nbytes);
dd89bb
dd89bb
	if ((status = __ntapi->zw_request_wait_reply_port(pty->hport,&msg,&msg)))
dd89bb
		return status;
dd89bb
	else if (msg.data.ttyinfo.status)
dd89bb
		return msg.data.ttyinfo.status;
dd89bb
dd89bb
	if (mode == __PTY_READ)
dd89bb
		__ntapi->tt_generic_memcpy(
dd89bb
			(char *)buffer,
dd89bb
			(char *)pty->section_addr + soffset,
dd89bb
			msg.data.ioinfo.iosb.info);
dd89bb
dd89bb
	iosb->info   = msg.data.ioinfo.iosb.info;
dd89bb
	iosb->status = msg.data.ioinfo.iosb.status;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t	__stdcall __ntapi_pty_read(
dd89bb
	__in	nt_pty *		pty,
dd89bb
	__in	void *			hevent		__optional,
dd89bb
	__in	nt_io_apc_routine *	apc_routine	__optional,
dd89bb
	__in	void *			apc_context	__optional,
dd89bb
	__out	nt_iosb *		iosb,
dd89bb
	__out	void *			buffer,
dd89bb
	__in	uint32_t		nbytes,
dd89bb
	__in	nt_large_integer *	offset		__optional,
dd89bb
	__in	uint32_t *		key		__optional)
dd89bb
{
dd89bb
	return __ntapi_pty_read_write(
dd89bb
		pty,
dd89bb
		hevent,apc_routine,apc_context,
dd89bb
		iosb,buffer,nbytes,offset,key,
dd89bb
		NT_TTY_PTY_READ);
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __stdcall __ntapi_pty_write(
dd89bb
	__in	nt_pty *		pty,
dd89bb
	__in 	void *			hevent		__optional,
dd89bb
	__in	nt_io_apc_routine *	apc_routine	__optional,
dd89bb
	__in	void * 			apc_context	__optional,
dd89bb
	__out	nt_iosb *		iosb,
dd89bb
	__in	void * 			buffer,
dd89bb
	__in	uint32_t		nbytes,
dd89bb
	__in	nt_large_integer *	offset		__optional,
dd89bb
	__in	uint32_t *		key		__optional)
dd89bb
{
dd89bb
	return __ntapi_pty_read_write(
dd89bb
		pty,
dd89bb
		hevent,apc_routine,apc_context,
dd89bb
		iosb,buffer,nbytes,offset,key,
dd89bb
		NT_TTY_PTY_WRITE);
dd89bb
}