This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdll_funcs.cpp
405 lines (373 loc) · 12.3 KB
/
dll_funcs.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* dll_funcs.cpp
*
* Created on: 13.01.2015
* Author: Forsaken
*/
//#define DEBUG_LOG_RAW
#include "dll_funcs.h"
#include "PrinterIntercept.h"
#include "PrinterSettings.h"
#include "SimpleLogWriter.h"
#include <stdio.h>
#include <iostream>
#include <direct.h>
#include <Shlobj.h>
#include <tchar.h>
WINUSB_INTERFACE_HANDLE hLatestInterfaceHandle = NULL;
Core::SimpleLogWriter* log = NULL;
Core::SimpleLogWriter* logRaw = NULL;
ULONG debugDumpIndex = 0;
BOOL InitWinUsbWrapper()
{
#if defined(DEBUG_LOG) || defined(DEBUG_LOG_RAW)
if (log == NULL) {
TCHAR sHomeDir[MAX_PATH];
TCHAR sFilename[MAX_PATH];
if(SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, sHomeDir))) {
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept"), sHomeDir);
// Ensure directory exists
_mkdir(sFilename);
// Detailed debug.log
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept\\debug.log"), sHomeDir);
log = new Core::SimpleLogWriter(sFilename);
// Raw read/write data
_stprintf(sFilename, TEXT("%s\\UpUsbIntercept\\debug_raw.log"), sHomeDir);
logRaw = new Core::SimpleLogWriter(sFilename);
}
}
#endif
// Load original winusb lib
if( !DynLoadSystemWinUSB() )
return FALSE;
#ifdef DEBUG_LOG_EX
log->writeString("Loaded original 'winusb.dll'.\r\n");
#endif
return TRUE;
}
BOOL __stdcall WinUsb_Wrapper_AbortPipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID
)
{
return WinUsb_AbortPipe( InterfaceHandle, PipeID );
}
BOOL __stdcall WinUsb_Wrapper_ControlTransfer(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
WINUSB_SETUP_PACKET SetupPacket,
PUCHAR Buffer,
ULONG BufferLength,
PULONG LengthTransferred,
LPOVERLAPPED Overlapped
)
{
return WinUsb_ControlTransfer( InterfaceHandle, SetupPacket, Buffer, BufferLength, LengthTransferred, Overlapped );
}
BOOL __stdcall WinUsb_Wrapper_FlushPipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID
)
{
return WinUsb_FlushPipe( InterfaceHandle, PipeID );
}
BOOL __stdcall WinUsb_Wrapper_Free(
WINUSB_INTERFACE_HANDLE InterfaceHandle
)
{
return WinUsb_Free( InterfaceHandle );
}
BOOL __stdcall WinUsb_Wrapper_GetAssociatedInterface(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR AssociatedInterfaceIndex,
PWINUSB_INTERFACE_HANDLE AssociatedInterfaceHandle
)
{
return WinUsb_GetAssociatedInterface( InterfaceHandle, AssociatedInterfaceIndex, AssociatedInterfaceHandle );
}
BOOL __stdcall WinUsb_Wrapper_GetCurrentAlternateSetting(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
PUCHAR AlternateSetting
)
{
return WinUsb_GetCurrentAlternateSetting( InterfaceHandle, AlternateSetting );
}
BOOL __stdcall WinUsb_Wrapper_GetDescriptor(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR DescriptorType,
UCHAR Index,
USHORT LanguageID,
PUCHAR Buffer,
ULONG BufferLength,
PULONG LengthTransferred
) {
#ifdef DEBUG_LOG_EX
log->writeString("WinUsb_GetDescriptor(0x")->writeBinaryBuffer(&InterfaceHandle, sizeof(WINUSB_INTERFACE_HANDLE))
->writeString(", 0x")->writeBinaryBuffer(&DescriptorType, 1)->writeString(", 0x")->writeBinaryBuffer(&Index, 1)
->writeString(", 0x")->writeBinaryBuffer(&LanguageID, 2)->writeString(", [OutBuffer], ")->writeLong(BufferLength)->writeString(", [OutLen])\r\n");
#endif
if( WinUsb_GetDescriptor( InterfaceHandle, DescriptorType, Index, LanguageID, Buffer, BufferLength, LengthTransferred ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Length transferred: ")->writeLong(*LengthTransferred)->writeString("\r\n");
log->writeString(" Buffer content: 0x")->writeBinaryBuffer(Buffer, *LengthTransferred)->writeString("\r\n");
#endif
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}
BOOL __stdcall WinUsb_Wrapper_GetOverlappedResult(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
LPOVERLAPPED lpOverlapped,
LPDWORD lpNumberOfBytesTransferred,
BOOL bWait
)
{
return WinUsb_GetOverlappedResult( InterfaceHandle, lpOverlapped, lpNumberOfBytesTransferred, bWait );
}
BOOL __stdcall WinUsb_Wrapper_GetPipePolicy(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID,
ULONG PolicyType,
PULONG ValueLength,
PVOID Value
)
{
return WinUsb_GetPipePolicy( InterfaceHandle, PipeID, PolicyType, ValueLength, Value );
}
BOOL __stdcall WinUsb_Wrapper_GetPowerPolicy(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
ULONG PolicyType,
PULONG ValueLength,
PVOID Value
)
{
return WinUsb_GetPowerPolicy( InterfaceHandle, PolicyType, ValueLength, Value );
}
BOOL __stdcall WinUsb_Wrapper_Initialize(
HANDLE DeviceHandle,
PWINUSB_INTERFACE_HANDLE InterfaceHandle
)
{
#ifdef DEBUG_LOG_EX
log->writeString("WinUsb_Initialize(0x")->writeBinaryBuffer(&DeviceHandle, sizeof(HANDLE))->writeString(")\r\n");
#endif
if( WinUsb_Initialize( DeviceHandle, InterfaceHandle ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! InterfaceHandle: 0x")->writeBinaryBuffer(InterfaceHandle, sizeof(PWINUSB_INTERFACE_HANDLE))->writeString("\r\n");
#endif
Core::PrinterSettings::getInstance()->setUsbHandle(*InterfaceHandle);
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}
PUSB_INTERFACE_DESCRIPTOR __stdcall WinUsb_Wrapper_ParseConfigurationDescriptor(
PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
PVOID StartPosition,
LONG InterfaceNumber,
LONG AlternateSetting,
LONG InterfaceClass,
LONG InterfaceSubClass,
LONG InterfaceProtocol
)
{
return WinUsb_ParseConfigurationDescriptor( ConfigurationDescriptor, StartPosition, InterfaceNumber, AlternateSetting, InterfaceClass, InterfaceSubClass, InterfaceProtocol );
}
PUSB_COMMON_DESCRIPTOR __stdcall WinUsb_Wrapper_ParseDescriptors(
PVOID DescriptorBuffer,
ULONG TotalLength,
PVOID StartPosition,
LONG DescriptorType
)
{
return WinUsb_ParseDescriptors( DescriptorBuffer, TotalLength, StartPosition, DescriptorType );
}
BOOL __stdcall WinUsb_Wrapper_QueryDeviceInformation(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
ULONG InformationType,
PULONG BufferLength,
PVOID Buffer
) {
#ifdef DEBUG_LOG_EX
log->writeString("WinUsb_QueryDeviceInformation(0x")->writeBinaryBuffer(&InterfaceHandle, sizeof(WINUSB_INTERFACE_HANDLE))
->writeString(", 0x")->writeBinaryBuffer(&InformationType, 4)->writeString(", ")->writeLong(*BufferLength)->writeString(", [OutBuffer])\r\n");
#endif
if( WinUsb_QueryDeviceInformation( InterfaceHandle, InformationType, BufferLength, Buffer ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Length transferred: ")->writeLong(*BufferLength)->writeString("\r\n");
log->writeString(" Buffer content: 0x")->writeBinaryBuffer(Buffer, *BufferLength)->writeString("\r\n");
#endif
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}
BOOL __stdcall WinUsb_Wrapper_QueryInterfaceSettings(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR AlternateSettingNumber,
PUSB_INTERFACE_DESCRIPTOR UsbAltInterfaceDescriptor
)
{
return WinUsb_QueryInterfaceSettings( InterfaceHandle, AlternateSettingNumber, UsbAltInterfaceDescriptor );
}
BOOL __stdcall WinUsb_Wrapper_QueryPipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR AlternateInterfaceNumber,
UCHAR PipeIndex,
PWINUSB_PIPE_INFORMATION PipeInformation
) {
#ifdef DEBUG_LOG_EX
log->writeString("WinUsb_QueryPipe(0x")->writeBinaryBuffer(&InterfaceHandle, sizeof(WINUSB_INTERFACE_HANDLE))
->writeString(", 0x")->writeBinaryBuffer(&AlternateInterfaceNumber, 1)->writeString(", 0x")->writeBinaryBuffer(&PipeIndex, 1)->writeString(", [OutBuffer])\r\n");
#endif
if( WinUsb_QueryPipe( InterfaceHandle, AlternateInterfaceNumber, PipeIndex, PipeInformation ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Pipe information: ")->writeBinaryBuffer(PipeInformation, sizeof(WINUSB_PIPE_INFORMATION))->writeString("\r\n");
#endif
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}
BOOL __stdcall WinUsb_Wrapper_ReadPipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID,
PUCHAR Buffer,
ULONG BufferLength,
PULONG LengthTransferred,
LPOVERLAPPED Overlapped
) {
#ifdef DEBUG_LOG
log->writeString("WinUsb_ReadPipe(0x")->writeBinaryBuffer(&InterfaceHandle, sizeof(WINUSB_INTERFACE_HANDLE))
->writeString(", 0x")->writeBinaryBuffer(&PipeID, 1)->writeString(", [OutBuffer], 0x")->writeLong(BufferLength)
->writeString(", [OutLen], 0x")->writeBinaryBuffer(&Overlapped, 4)->writeString(")\r\n");
#endif
*LengthTransferred = Core::PrinterIntercept::getInstance()->handleUsbPreRead(InterfaceHandle, PipeID, Buffer, BufferLength);
if (*LengthTransferred > 0) {
// Prevent reading and supply a custom response instead
return TRUE;
}
if( WinUsb_ReadPipe( InterfaceHandle, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Length transferred: ")->writeLong(*LengthTransferred)->writeString("\r\n");
log->writeString(" Buffer content: 0x")->writeBinaryBuffer(Buffer, *LengthTransferred)->writeString("\r\n");
#endif
#ifdef DEBUG_LOG_RAW
logRaw->writeString("<")->writeBinaryBuffer(Buffer, *LengthTransferred)->writeString("\r\n");
#endif
Core::PrinterIntercept::getInstance()->handleUsbRead(InterfaceHandle, PipeID, Buffer, *LengthTransferred);
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}
BOOL __stdcall WinUsb_Wrapper_ResetPipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID
)
{
return WinUsb_ResetPipe( InterfaceHandle, PipeID );
}
BOOL __stdcall WinUsb_Wrapper_SetCurrentAlternateSetting(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR AlternateSetting
)
{
return WinUsb_SetCurrentAlternateSetting( InterfaceHandle, AlternateSetting );
}
BOOL __stdcall WinUsb_Wrapper_SetPipePolicy(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID,
ULONG PolicyType,
ULONG ValueLength,
PVOID Value
)
{
return WinUsb_SetPipePolicy( InterfaceHandle, PipeID, PolicyType, ValueLength, Value );
}
BOOL __stdcall WinUsb_Wrapper_SetPowerPolicy(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
ULONG PolicyType,
ULONG ValueLength,
PVOID Value
)
{
return WinUsb_SetPowerPolicy( InterfaceHandle, PolicyType, ValueLength, Value );
}
BOOL __stdcall WinUsb_Wrapper_WritePipe(
WINUSB_INTERFACE_HANDLE InterfaceHandle,
UCHAR PipeID,
PUCHAR Buffer,
ULONG BufferLength,
PULONG LengthTransferred,
LPOVERLAPPED Overlapped
) {
#ifdef DEBUG_LOG
log->writeString("WinUsb_WritePipe(0x")->writeBinaryBuffer(&InterfaceHandle, sizeof(WINUSB_INTERFACE_HANDLE))
->writeString(", 0x")->writeBinaryBuffer(&PipeID, 1)->writeString(", [OutBuffer], 0x")->writeLong(BufferLength)
->writeString(", [OutLen], 0x")->writeBinaryBuffer(&Overlapped, 4)->writeString(")\r\n");
#endif
#ifdef DEBUG_LOG_RAW
logRaw->writeString(">")->writeBinaryBuffer(Buffer, BufferLength)->writeString("\r\n");
#endif
if (!Core::PrinterIntercept::getInstance()->handleUsbWrite(InterfaceHandle, PipeID, Buffer, BufferLength)) {
// Prevent writing!
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Length transferred: ")->writeLong(*LengthTransferred)->writeString("\r\n");
//log->writeString(" Buffer sent: 0x")->writeBinaryBuffer(Buffer, *LengthTransferred)->writeString("\r\n");
#endif
*LengthTransferred = BufferLength;
return TRUE;
}
if( WinUsb_WritePipe( InterfaceHandle, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped ) )
{
#ifdef DEBUG_LOG_EX
log->writeString(" => Success! Length transferred: ")->writeLong(*LengthTransferred)->writeString("\r\n");
//log->writeString(" Buffer sent: 0x")->writeBinaryBuffer(Buffer, *LengthTransferred)->writeString("\r\n");
#endif
return TRUE;
}
else
{
#ifdef DEBUG_LOG_EX
DWORD dwError = GetLastError();
log->writeString(" => Failed! Error code: 0x")->writeBinaryBuffer(&dwError, sizeof(DWORD))->writeString("\r\n");
#endif
return FALSE;
}
}