summaryrefslogtreecommitdiffhomepage
path: root/src/debug/ptyc_dbg_cat.c
blob: 4ebb1b2691ccb99c33babca84b0da583bc710b78 (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
/*********************************************************/
/*  ptycon: a pty-console bridge                         */
/*  Copyright (C) 2016--2017  SysDeer Technologies, LLC  */
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntcon/ntcon.h>
#include <ntapi/ntapi.h>

#include <ptycon/ptycon.h>
#include "ptycon_driver_impl.h"

static int32_t ptyc_cat(
	struct ptyc_driver_ctx* dctx,
	void *			hat,
	const char *		unit,
	void *			hevent)
{
	int32_t		status;
	void *		hfile;
	nt_iosb		iosb;
	uintptr_t	nread;
	uintptr_t	buffer[32768/sizeof(uintptr_t)];
	char *		ch;

	if ((status = ptyc_open_file(&hfile,hat,unit,true)))
		return status;

	status = ntapi->zw_read_file(
		hfile,
		0,0,0,
		&iosb,
		buffer,sizeof(buffer),
		0,0);

	while (status == NT_STATUS_SUCCESS) {
		ch    = (char *)buffer;
		nread = iosb.info;

		for ( ; nread; ) {
			status = ntapi->pty_write(
				dctx->cctx->hpts,
				hevent,0,0,&iosb,
				ch,(uint32_t)nread,
				0,0);

			if (status == NT_STATUS_PENDING)
				status = ntapi->zw_wait_for_single_object(
					hevent,NT_SYNC_ALERTABLE,0);

			if (status || iosb.status) {
				ntapi->zw_close(hfile);
				return status ? status : iosb.status;
			}

			ch    += iosb.info;
			nread -= iosb.info;
		}

		status = ntapi->zw_read_file(
			hfile,
			0,0,0,
			&iosb,
			buffer,sizeof(buffer),
			0,0);
	}

	ntapi->zw_close(hfile);

	return (status == NT_STATUS_END_OF_FILE)
		? NT_STATUS_SUCCESS
		: status;
}

int __stdcall ptyc_dbg_cat(struct ptyc_driver_ctx * dctx)
{
	int32_t		status;
	void *		hevent;
	const char **	punit;
	nt_rtdata *	rtdata;
	nt_peb * 	peb;
	void *		hat;

	if (!dctx->units[0])
		return 0;

	if ((status = ntapi->tt_create_private_event(
			&hevent,
			NT_NOTIFICATION_EVENT,
			NT_EVENT_NOT_SIGNALED)))
		return status;

	if ((status = ntapi->tt_get_runtime_data(&rtdata,0)))
		return status;

	if (!(peb = (nt_peb *)pe_get_peb_address()))
		return NT_STATUS_INTERNAL_ERROR;

	if (!peb->process_params)
		return NT_STATUS_INTERNAL_ERROR;

	hat = rtdata->hcwd
		? rtdata->hcwd
		: peb->process_params->cwd_handle;

	for (punit=dctx->units, status=0; *punit && !status; punit++)
		status = ptyc_cat(
			dctx,hat,
			*punit,
			hevent);

	ntapi->zw_close(hevent);
	return status;
}