-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHookStdio.cpp
More file actions
181 lines (138 loc) · 4.25 KB
/
HookStdio.cpp
File metadata and controls
181 lines (138 loc) · 4.25 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// HookStdio.cpp: implementation of the CHookStdio class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "io.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHookStdio::CHookStdio( DWORD dwStreamId, DWORD dwBufferSize )
{
m_hRead = NULL;
m_hWrite = NULL;
m_hOriginal = NULL;
// Hook stream if needed
if ( dwStreamId ) Start( dwStreamId, dwBufferSize );
}
CHookStdio::~CHookStdio()
{ Stop();
}
BOOL CHookStdio::Start( DWORD dwStreamId, DWORD dwBufferSize )
{
// Kill any current hook
Stop();
// Verify valid stream
if ( STD_INPUT_HANDLE != dwStreamId &&
STD_OUTPUT_HANDLE != dwStreamId &&
STD_ERROR_HANDLE != dwStreamId )
return FALSE;
// Save stream id
m_dwStreamId = dwStreamId;
// Create pipe
if ( !CreatePipe( &m_hRead, &m_hWrite, NULL, dwBufferSize ) )
{ Stop(); return FALSE; }
// Save current stream handle
m_hOriginal = GetStdHandle( dwStreamId );
// Set new stream handle
if ( !SetStdHandle( dwStreamId, m_hWrite ) )
{ Stop(); return FALSE; }
// Associate stream handle
int nStream = _open_osfhandle( RUPTR2INT(GetStdHandle( dwStreamId )), 0 );
// Associate stream handle
FILE* hfStream = _fdopen( nStream, "w" );
// Hook appropriate stream
switch ( dwStreamId )
{
case STD_INPUT_HANDLE :
// Switch streams
*stdin = *hfStream;
// Disable stream buffering
setvbuf( stdin, NULL, _IONBF, 0 );
break;
case STD_OUTPUT_HANDLE :
// Switch streams
*stdout = *hfStream;
// Disable stream buffering
setvbuf( stdout, NULL, _IONBF, 0 );
break;
case STD_ERROR_HANDLE :
// Switch streams
*stderr = *hfStream;
// Disable stream buffering
setvbuf( stderr, NULL, _IONBF, 0 );
break;
} // end switch
return TRUE;
}
void CHookStdio::Stop()
{
if ( m_hOriginal )
{ SetStdHandle( m_dwStreamId, m_hOriginal );
m_hOriginal = NULL;
} // end if
if ( m_hWrite )
{ CloseHandle( m_hWrite );
m_hWrite = NULL;
} // end if
if ( m_hRead )
{ CloseHandle( m_hRead );
m_hRead = NULL;
} // end if
}
DWORD CHookStdio::Read(LPVOID pBuf, DWORD dwSize)
{
// Punt if no read handle
if ( NULL == m_hRead || !pBuf || !dwSize ) return 0;
DWORD dwRead = 0;
// Ensure there is data to read
if ( !PeekNamedPipe( m_hRead, NULL, NULL, NULL, &dwRead, NULL ) || !dwRead )
return 0;
// Attempt to read from the stream
if ( !ReadFile( m_hRead, pBuf, dwSize, &dwRead, NULL ) )
return 0;
// Check for error
if ( !dwRead || dwRead > dwSize )
return 0;
// Return the number of bytes read
return dwRead;
}
DWORD CHookStdio::Write(LPVOID pBuf, DWORD dwSize)
{
// Punt if no write handle
if ( NULL == m_hWrite || !pBuf || !dwSize ) return 0;
DWORD dwWritten = 0;
// Attempt to write to the stream
if ( !WriteFile( m_hWrite, pBuf, dwSize, &dwWritten, NULL ) )
return 0;
// Check for error
if ( !dwWritten || dwWritten > dwSize )
return 0;
// Return the number of bytes written
return dwWritten;
}