forked from NytroRST/ShellcodeCompiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.cpp
More file actions
180 lines (122 loc) · 2.83 KB
/
Utils.cpp
File metadata and controls
180 lines (122 loc) · 2.83 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
#include "Utils.h"
// Get current working directory
string Utils::GetCurrentDir()
{
char buffer[1024];
string sContent = "";
DWORD r = GetCurrentDirectory(1024, buffer);
if (r == 0) return "";
sContent = buffer;
return sContent;
}
// Check if a file exists
bool Utils::FileExists(string p_sPath)
{
DWORD dwAttrib = GetFileAttributes(p_sPath.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES);
}
// Delete a file
bool Utils::DeleteSourceFile(string p_sFile)
{
return (bool)DeleteFile(p_sFile.c_str());
}
// Function used to read a file
string Utils::ReadSourceFile(string p_sFilename)
{
string contents = "";
cout << "Read text file: " << p_sFilename << endl;
FILE* f = fopen(p_sFilename.c_str(), "rb");
// Get file size
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
char* cdata = new char[size + 1];
cdata[size] = '\0';
// Read file
rewind(f);
fread(cdata, sizeof(char), size, f);
fclose(f);
// Return data
contents = cdata;
delete[] cdata;
return contents;
}
// Get the size of a file
size_t Utils::GetSize(string p_sFilename)
{
size_t size;
FILE* f = fopen(p_sFilename.c_str(), "rb");
// Get file size
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
fclose(f);
return size;
}
// Read binary file
unsigned char* Utils::ReadBinaryFile(string p_sFilename, size_t *p_rSize)
{
unsigned char *p = NULL;
FILE* f = NULL;
size_t res = 0;
if (!FileExists(p_sFilename))
{
cout << "Binary file does not exists!" << endl;
return NULL;
}
// Get size and allocate space
*p_rSize = GetSize(p_sFilename);
if (*p_rSize == 0) return NULL;
f = fopen(p_sFilename.c_str(), "rb");
p = new unsigned char[*p_rSize];
// Read file
rewind(f);
res = fread(p, sizeof(unsigned char), *p_rSize, f);
fclose(f);
if (res == 0)
{
delete[] p;
return NULL;
}
return p;
}
// Write output to file
void Utils::WriteToFile(string p_sOutputFile, string p_sData)
{
FILE* f = fopen(p_sOutputFile.c_str(), "ab");
// Write
fwrite(p_sData.c_str(), p_sData.length(), sizeof(char), f);
// Close
fclose(f);
}
// Return hex string for a char
string Utils::CharToHexString(char p_cChar)
{
stringstream ss;
ss << hex << (int)p_cChar;
string mystr = ss.str();
return mystr;
}
// Convert int to hex
string Utils::IntToHexString(size_t p_iNumber)
{
std::stringstream stream;
stream << std::hex << p_iNumber;
std::string result(stream.str());
return result;
}
// Function used to check if a character is "string": [a-z][A-Z][0-9][Specail]
bool Utils::IsString(char p_cCharacter)
{
return (p_cCharacter >= 32 && p_cCharacter <= 126 &&
p_cCharacter != '"' && p_cCharacter != ')' && p_cCharacter != '(' && p_cCharacter != ',');
}
// Get TEMP folder
string Utils::GetTemp()
{
char buffer[1024];
string sContent = "";
DWORD r = GetTempPath(1024, buffer);
if (r == 0) return "";
sContent = buffer;
return sContent;
}