-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase_operations.cpp
More file actions
83 lines (76 loc) · 1.73 KB
/
Copy pathbase_operations.cpp
File metadata and controls
83 lines (76 loc) · 1.73 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
#include "pe_parser.h"
HANDLE GetFileFromArguments( int argc, char** argv )
{
HANDLE fileHandle = NULL;
if( 0x02 == argc )
{
fileHandle = CreateFileA( argv[ 0x01 ], GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if( INVALID_HANDLE_VALUE == fileHandle )
{
PrintError( "CreateFileA" );
}
}
else
{
PrintHelp( argv[ 0x00 ] );
}
return fileHandle;
}
DWORD ReadFileToBuffer( HANDLE fileHandle, char* buffer, DWORD bufferSize )
{
DWORD returnValue = 0x00;
if( NULL != fileHandle )
{
DWORD bytesRead;
if( true == ReadFile( fileHandle, buffer, bufferSize, &bytesRead, NULL ) )
{
returnValue = bytesRead;
}
else
{
PrintError( "ReadFile" );
}
}
return returnValue;
}
DWORD WriteFileFromBuffer( char* filename, char* buffer, DWORD bufferSize )
{
DWORD returnValue = 0x00;
HANDLE fileHandle = CreateFileA( filename, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if( INVALID_HANDLE_VALUE == fileHandle )
{
PrintError( "CreateFileA" );
}
if( NULL != fileHandle )
{
DWORD bytesWritten;
if( true == WriteFile( fileHandle, buffer, bufferSize, &bytesWritten, NULL ) )
{
returnValue = bytesWritten;
}
else
{
PrintError( "WriteFile" );
}
CloseHandle( fileHandle );
}
return returnValue;
}
DWORD CheckFileSizeForCorrectness( DWORD fileSize )
{
if( INVALID_FILE_SIZE == fileSize )
{
PrintError( "GetFileSize" );
}
else if( MAX_FILE_SIZE_ALLOWED_TO_READ < fileSize )
{
printf( TOO_LARGE_FILE );
fileSize = INVALID_FILE_SIZE;
}
else if( 0x00 == fileSize )
{
printf( NULL_FILE_SIZE );
fileSize = INVALID_FILE_SIZE;
}
return fileSize;
}