-
Notifications
You must be signed in to change notification settings - Fork 23
/
dll.cpp
211 lines (185 loc) · 5.06 KB
/
dll.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
#include "stdafx.h"
#ifdef _WINDLL
#include "spasm.h"
#include "utils.h"
#include "storage.h"
#include "list.h"
list_t *default_defines;
typedef struct {
char name[64];
char value[64];
} default_define_pair_t;
#ifdef LOG
#include <stdio.h>
FILE *logfile;
#endif
extern "C"
{
__declspec (dllexport) BOOL __stdcall DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
#ifdef LOG
logfile = fopen("logfile.txt", "a");
fprintf(logfile, "Log opened\n");
fflush(logfile);
#endif
/*
AllocConsole();
SetConsoleTitle("SPASM");
// The following is a really disgusting hack to make stdin and stdout attach
// to the newly created console using the MSVC++ libraries. I hope other
// operating systems don't need this kind of kludge.. :)
stdout->_file = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
stdin->_file = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
*/
output_contents = (unsigned char *) malloc (output_buf_size);
setvbuf(stdout, output_text, _IOFBF, 65536);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
#ifdef LOG
fprintf(logfile, "Log closed\n");
fflush(logfile);
fclose(logfile);
#endif
//stdout->_file = -1;
//stdin->_file = -1;
//FreeConsole();
free(output_contents);
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
break;
}
return TRUE;
}
__declspec (dllexport) int __stdcall
SetInputFile(const char *lpFilename) {
curr_input_file = strdup(lpFilename);
#ifdef LOG
fprintf(logfile, "SetInputFile: %s\n", curr_input_file);
fflush(logfile);
#endif
return EXIT_NORMAL;
}
__declspec (dllexport) int __stdcall
SetOutputFile(const char *lpFilename) {
output_filename = strdup(lpFilename);
#ifdef LOG
fprintf(logfile, "SetInputFile: %s\n", output_filename);
fflush(logfile);
#endif
return EXIT_NORMAL;
}
__declspec (dllexport) int __stdcall
ClearDefines() {
list_free(default_defines, true, NULL);
default_defines = NULL;
#ifdef LOG
fprintf(logfile, "ClearDefines");
fflush(logfile);
#endif
return 0;
}
__declspec (dllexport) int __stdcall
AddDefine(const char *lpName, const char *lpValue) {
default_define_pair_t *ddp = (default_define_pair_t*)malloc (sizeof(*ddp));
strncpy(ddp->name, lpName, sizeof(ddp->name));
if (lpValue == NULL) {
strcpy(ddp->value, "1");
} else {
strncpy(ddp->value, lpValue, sizeof(ddp->value));
}
default_defines = list_append(default_defines, ddp);
#ifdef LOG
fprintf(logfile, "AddDefine: %s %d\n", ddp->name, ddp->value);
fflush(logfile);
#endif
return 0;
}
__declspec (dllexport) int __stdcall
ClearIncludes() {
list_free(include_dirs, true, NULL);
include_dirs = NULL;
#ifdef LOG
fprintf(logfile, "ClearIncludes\n");
fflush(logfile);
#endif
return 0;
}
__declspec (dllexport) int __stdcall
AddInclude(const char *lpDirectory) {
include_dirs = list_append(include_dirs, strdup (lpDirectory));
#ifdef LOG
fprintf(logfile, "AddInclude: %s\n", lpDirectory);
fflush(logfile);
#endif
return 0;
}
__declspec (dllexport) int __stdcall
RunAssembly() {
list_t *list = default_defines;
int result;
mode = MODE_NORMAL;
init_storage();
while (list) {
char *name = ((default_define_pair_t *) list->data)->name;
char *value = ((default_define_pair_t *) list->data)->value;
add_define (strdup (name), NULL)->contents = strdup (value);
list = list->next;
}
printf("Input file: %s\n", curr_input_file);
printf("Output file: %s\n", output_filename);
list = include_dirs;
while (list) {
printf("Include dir: %s\n", (char *) list->data);
list = list->next;
}
if (curr_input_file == NULL || output_filename == NULL) {
#ifdef LOG
fprintf(logfile, "Unable to assemble file\n");
fflush(logfile);
#endif
return EXIT_FATAL_ERROR;
}
result = run_assembly();
fflush(stdout);
#ifdef LOG
fprintf(logfile, "File assembled with return value: %d\n", result);
fflush(logfile);
#endif
free_storage();
return result;
}
__declspec (dllexport)
char* __stdcall GetStdOut()
{
return output_text;
}
__declspec (dllexport)
int __stdcall RunAssemblyWithArguments(char *szCommand, BYTE *lpResult, int cbResult) {
output_filename = NULL;
mode = MODE_NORMAL | MODE_COMMANDLINE;
input_contents = (char *) malloc (strlen(szCommand) + 1 + 2);
strcpy(input_contents, " ");
strcat(input_contents, szCommand);
strcat(input_contents, "\n");
curr_input_file = NULL;
int result = run_assembly();
if (lpResult != NULL) {
memcpy(lpResult, output_contents, min(out_ptr - output_contents, cbResult));
}
return min(out_ptr - output_contents, cbResult);
}
__declspec (dllexport) int __stdcall
ShowMessage() {
MessageBox(NULL, "Hello", "Hello", MB_OK);
return 0;
}
}
#endif