-
Notifications
You must be signed in to change notification settings - Fork 6
/
rawcopy.singlebuffer.cpp
303 lines (264 loc) · 8 KB
/
rawcopy.singlebuffer.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
#include <strstream>
#include "winstrct.h"
#include <winioctl>
#define STDBUFSIZ 512
#define MAXBUFSIZ 2949120
char buffer[STDBUFSIZ];
unsigned long copylength = 0,
written = 0;
unsigned long bufsiz = STDBUFSIZ;
HANDLE hIn = INVALID_HANDLE_VALUE,
hOut = INVALID_HANDLE_VALUE;
int main(int argc, char **argv)
{
bool buffermode = false, lockmode = true, verbosemode = false, displayhelp = false;
// Nice argument parse loop :)
while( argv[1] ? argv[1][0] ? (argv[1][0]|0x02) == '/' : false : false )
{
while( (++argv[1])[0] )
switch( argv[1][0]|0x20 )
{
case 'm':
buffermode = true;
break;
case 'l':
lockmode = false;
break;
case 'v':
verbosemode = true;
break;
default:
displayhelp = true;
}
--argc;
++argv;
}
if( displayhelp )
{
cout << "Utility to read/write shared objects in Windows," << endl <<
"i. e. direct access to disk devices." << endl <<
endl <<
"Written by Olof Lagerkvist 2000" << endl <<
"http://here.is/olof" << endl <<
endl <<
"Usage:" << endl <<
"RAWCOPY [-l] [-m] [[[[[skipforward] copylength] infile] outfile]" << endl <<
endl <<
"Default infile/outfile if none or blank given is" << endl <<
"standard input/output device." << endl <<
"-l Access devices without locking access to them. Use" << endl <<
" this switch when you are reading/writing physical" << endl <<
" drives and other processes are using the drives." << endl <<
" Note! This may destroy your data!" << endl <<
endl <<
"-m Try to buffer more of input file into memory" << endl <<
" before writing to output file. It is not possible" << endl <<
" to ignore read/write failures if this switch is" << endl <<
" used." << endl <<
endl <<
"Examples for Windows NT:" << endl <<
"RAWCOPY diskimage.img \\\\.\\A:" << endl <<
" Writes a diskette image file called \"diskimage.img\" to" << endl <<
" a physical diskette in drive A:." << endl <<
"RAWCOPY \\\\.\\PIPE\\MYPIPE \"\"" << endl <<
" Reads data from named pipe \"MYPIPE\" on the local" << endl <<
" machine and write on standard output." << endl <<
"RAWCOPY 1 \\\\.\\PhysicalDrive0 parttable" << endl <<
" Copies sector 1 (partition table) from first physical" << endl <<
" harddrive to file called \"parttable\"." << endl;
return 0;
}
unsigned long skipforward = 0;
if( argc > 4 )
{
skipforward = strtol(argv[1], NULL, 0);
argv++;
argc--;
}
if( argc > 3 )
{
if( (copylength = strtoul(argv[1], NULL, 0)) == 0 )
{
cerr << "Invalid length." << endl;
return -1;
}
argv++;
argc--;
}
if( argc > 2 ? argv[1][0] : false )
hIn = CreateFile(argv[1], GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
else
hIn = hStdIn;
if( hIn == INVALID_HANDLE_VALUE )
{
win_perror(argc > 1 ? argv[1] : "stdin");
return -1;
}
if( skipforward )
if(SetFilePointer( hIn, skipforward, &(*new LONG = 0), FILE_CURRENT)
== 0xFFFFFFFF )
{
win_perror("SetFilePointer()");
return -1;
}
char *bufptr;
// If -m option is used we use big buffer
if( buffermode )
{
if( (bufptr = (char*)LocalAlloc(LPTR, MAXBUFSIZ)) != NULL )
bufsiz = LocalSize(bufptr);
else
{
bufptr = buffer;
win_perror();
}
if( verbosemode )
cerr << "Buffering " << bufsiz << " bytes." << endl;
}
// -m is not used
else
{
bufptr = buffer;
if( verbosemode )
cerr << "Sequential scan mode used, max read/write buffer is " <<
bufsiz << " bytes." << endl;
}
// Jump to next parameter if input file was given
if( argc > 2 )
{
argv++;
argc--;
}
if( argc > 1 ? argv[1][0] : false )
hOut = CreateFile(argv[1], GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, hIn);
else
hOut = hStdOut;
if( hOut == INVALID_HANDLE_VALUE )
{
win_perror(argc > 1 ? argv[1] : "stdout");
return -1;
}
DWORD dwZ;
win_errno = NO_ERROR;
DeviceIoControl(hIn, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwZ, NULL);
switch(win_errno)
{
case NO_ERROR:
if( verbosemode )
cerr << "Source device locked during data transfer." << endl;
case ERROR_NOT_SUPPORTED:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_PARAMETER:
break;
default:
if( lockmode )
{
if( verbosemode )
cerr << "System Error " << win_errno << '.' << endl;
win_perror("Cannot lock source device (use -l)");
return -1;
}
else
win_perror("Warning! Source device not locked");
}
win_errno = NO_ERROR;
DeviceIoControl(hOut, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwZ, NULL);
switch(win_errno)
{
case NO_ERROR:
if( verbosemode )
cerr << "Target device locked during data transfer." << endl;
case ERROR_NOT_SUPPORTED:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_PARAMETER:
break;
default:
if( lockmode )
{
if( verbosemode )
cerr << "System Error " << win_errno << '.' << endl;
win_perror("Cannot lock target device (use -l)");
return -1;
}
else
win_perror("Warning! Target device not locked");
}
while(true)
{
Sleep( 0 );
if( verbosemode )
cerr << "Reading block " << written << '\r' << flush;
// Read next block
DWORD dwReadSize;
while( !ReadFile(hIn, bufptr, bufsiz, &dwReadSize, NULL) )
{
// End of pipe?
if( win_errno == ERROR_BROKEN_PIPE )
return 0;
char *errmsg = win_error;
strstream msgbuf;
msgbuf << "Read failed: " << errmsg;
LocalFree( errmsg );
switch( MessageBox(NULL, msgbuf.str(), "RawCopy Error",
MB_ABORTRETRYIGNORE|MB_ICONEXCLAMATION|MB_DEFBUTTON2|
MB_TASKMODAL) )
{
case IDABORT:
exit( -1 );
case IDRETRY:
continue;
case IDIGNORE:
dwReadSize = bufsiz;
ZeroMemory( bufptr, bufsiz );
if( SetFilePointer(hIn, bufsiz, NULL, FILE_CURRENT) == 0xFFFFFFFF )
{
win_perror("SetFilePointer()");
exit(-1);
}
}
break;
}
// Check for EOF condition
if( !dwReadSize )
return 0;
if( verbosemode )
cerr << "Writing block " << written << '\r' << flush;
// Write next block
DWORD dwWriteSize;
while( !WriteFile(hOut, bufptr, dwReadSize, &dwWriteSize, NULL) )
{
char *errmsg = win_error;
strstream msgbuf;
msgbuf << "Write failed: " << errmsg;
LocalFree( errmsg );
switch( MessageBox(NULL, msgbuf.str(), "RawCopy Error",
MB_ABORTRETRYIGNORE|MB_ICONEXCLAMATION|MB_DEFBUTTON2|
MB_TASKMODAL) )
{
case IDABORT:
exit(-1);
case IDRETRY:
continue;
case IDIGNORE:
if( SetFilePointer(hOut, bufsiz, NULL, FILE_CURRENT) == 0xFFFFFFFF )
{
win_perror( "Can't ignore" );
exit(-1);
}
}
break;
}
++written;
if( copylength )
if( written >= copylength )
exit(0);
if( dwWriteSize < dwReadSize )
cerr << "Warning: " << (dwWriteSize - dwReadSize) << " bytes lost." << endl;
}
}