-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadfiles.cpp
224 lines (196 loc) · 5.28 KB
/
loadfiles.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
/* processdumper: console utility for software analysis
* Copyright(C) 2017 Peter Bohning
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "loadfiles.h"
/* sigh... I should just write a damn c++ object... */
HANDLE hDir = NULL;
WCHAR * currentdir = NULL;
WIN32_FIND_DATA findData;
WCHAR path[500];
int get_next_filename(wchar_t * dir, wchar_t ** filename);
int get_next_filename(wchar_t * dir, wchar_t ** filename)
{
WCHAR * widestring;
int ret;
*filename = NULL;
if(currentdir && dir != currentdir)
{
printf("Switching directory ?!?\n");
currentdir = NULL;
FindClose(hDir);
hDir = NULL;
}
if(!currentdir)
{
//if(dirname[strlen(dirname)] == '/')
// dirname[strlen(dirname)] = 0x00;
widestring = (WCHAR *)calloc(wcslen(dir) + 1, sizeof(WCHAR));
//mbstowcs_s(&converted, widestring, (strlen(dirname) + 1) * sizeof(WCHAR), dirname, strlen(dirname));
wsprintf(&(widestring[wcslen(widestring)]), L"%s\\*", dir);
hDir = FindFirstFile(widestring, &findData);
if(hDir == INVALID_HANDLE_VALUE)
{
wprintf(L"Can't find directory %s %d\n", widestring, GetLastError());
return -1;
}
free(widestring);
if(!hDir)
{
fprintf(stderr, "Null directory handle\n");
return -1;
}
if(!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
fwprintf(stderr, L"%s is not a directory\n", findData.cFileName);
FindClose(hDir);
hDir = NULL;
return -1;
}
currentdir = dir;
}
do
{
if(!(ret = FindNextFile(hDir, &findData)))
{
FindClose(hDir);
hDir = NULL;
currentdir = NULL;
if(ret < 0)
{
printf("findnext failed %d\n", GetLastError());
return -1;
}
//ERROR_NO_MORE_FILES 18
return 0;
}
} while(findData.cFileName[0] == '.');
*filename = (wchar_t *)calloc(wcslen(findData.cFileName) + 1, sizeof(wchar_t));
wcscpy(*filename, findData.cFileName);
return 1;
}
int load_files(wchar_t * dirname, uint32_t * addr, char ** buffer, unsigned long * size)
{
int ret;
wchar_t * filename;
HANDLE hfile;
DWORD bytes_read;
if((ret = get_next_filename(dirname, &filename)) != 1)
return ret;
wprintf(L"%s\n", filename);
*buffer = 0;
if(swscanf_s(filename, L"inmem_%08x_", addr) == 1)
{
printf("Load address: %08x\n", *addr);
wsprintf(path, L"%s//%s", dirname, filename);
free(filename);
hfile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hfile)
{
if(findData.nFileSizeHigh)
fprintf(stderr, "WARNING: file size has highpart!\n");
*size = findData.nFileSizeLow;
*buffer = (char *)malloc(*size);
if(!ReadFile(hfile, *buffer, *size, &bytes_read, NULL))
{
fprintf(stderr, "File read error %d\n", GetLastError());
free(*buffer);
*buffer = 0;
return 1;
}
if(bytes_read != *size)
{
fprintf(stderr, "Only read %d bytes\n", bytes_read);
//this is pretty serious...
free(*buffer);
*buffer = 0;
return 1;
}
CloseHandle(hfile);
}
else
fprintf(stderr, "File open error %d\n", GetLastError());
}
return 1;
}
// *nix ?
/*DIR * dp;
struct dirent * ep;
struct path;
if(dirname)
{
dp = opendir(dirname);
if(dp != NULL)
{
while(ep = readdir(dp))
{
len = strlen(ep->d_name);
printf("FILE: %s\n", ep->d_name);
}
closedir(dp);
}
else
perror("Couldn't open the directory");
}*/
int load_a_file(char * name, char ** buffer, unsigned int * size)
{
WCHAR dllPath[512];
HANDLE hFile;
size_t converted;
WCHAR * wname;
DWORD bytes_read;
wname = (WCHAR *)calloc((strlen(name) + 1), sizeof(wchar_t));
mbstowcs_s(&converted, wname, (strlen(name) + 1), name, strlen(name));
GetFullPathName(wname, 512, dllPath, NULL);
hFile = CreateFileW(dllPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
fwprintf(stderr, L"Can't open %s for loading: %d\n", dllPath, GetLastError());
return -1;
}
else if(!hFile)
{
fwprintf(stderr, L"Can't open %s for loading\n", dllPath);
return -1;
}
*size = GetFileSize(hFile, NULL);
*buffer = (char *)malloc(*size);
if(!ReadFile(hFile, *buffer, *size, &bytes_read, NULL))
{
fprintf(stderr, "Read failed %d\n", GetLastError());
CloseHandle(hFile);
return -1;
}
CloseHandle(hFile);
return 0;
}
//dll exe
int get_next_filename(wchar_t * dirname, char ** outname)
{
wchar_t * filename;
size_t converted;
int ret;
if((ret = get_next_filename(dirname, &filename)) != 1)
return ret;
if(wcscmp(&(filename[wcslen(filename) - 4]), L".dll") == 0 ||
wcscmp(&(filename[wcslen(filename) - 4]), L".exe") == 0)
{
wsprintf(path, L"%s//%s", currentdir, filename);
*outname = (char *)calloc(200, sizeof(char));
converted = wcstombs(*outname, path, 200);
}
return 1;
}