Browse code

update for C++17 compliance, update to latest 2sf, add WINE cross-compile makefiles

Adam Higerd authored on 2021/02/11 15:36:17
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,507 +0,0 @@
1
-/*
2
-    dlditool - Dynamically Linked Disk Interface patch tool
3
-    Copyright (C) 2006  Michael Chisholm (Chishm)
4
-
5
-	Send all queries to chishm@hotmail.com
6
-
7
-    This program is free software; you can redistribute it and/or modify
8
-    it under the terms of the GNU General Public License as published by
9
-    the Free Software Foundation; either version 2 of the License, or
10
-    (at your option) any later version.
11
-
12
-    This program is distributed in the hope that it will be useful,
13
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
-    GNU General Public License for more details.
16
-
17
-    You should have received a copy of the GNU General Public License along
18
-    with this program; if not, write to the Free Software Foundation, Inc.,
19
-    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
-
21
-	* v1.24d - 2010-06-14 - zeromus
22
-		* Modified for inclusion in desmume
23
-
24
-	* v1.24 - 2007-08-02 - SmileyDude
25
-		* Now using EXIT_SUCCESS and EXIT_FAILURE at the suggestion of MachinShin.
26
-		* Defined EXIT_NO_DLDI_SECTION for when there is no DLDI section in a file.
27
-		* Added cast to strcmp() call to appease the compiler.
28
-
29
-	* v1.23 - 2007-01-23 - Chishm
30
-		* Fixed bug when DLDI section doesn't exist
31
-		* addr_t is now a signed int
32
-
33
-	* v1.22 - 2007-01-12 - WinterMute
34
-		* add search paths for dldi files
35
-
36
-	* v1.21 - 2007-01-12 - Chishm
37
-		* Improved error messages
38
-
39
-	* v1.20 - 2007-01-11 - Chishm
40
-		* Changed offset calculation method
41
-
42
-	* v1.10 - 2007-01-07 - Chishm
43
-		* Removed assumptions about endianess of computer
44
-			* Word size shouldn't matter now either, except that an int type must be at least 32 bits long
45
-		* Added *.sc.nds and *.gba.nds file extensions
46
-		* Improved command line argument parsing
47
-
48
-	* v1.01 - 2006-12-30 - Chishm
49
-		* Minor bugfix parsing 3 arguments
50
-
51
-	* v1.00 - 2006-12-25 - Chishm
52
-		* Original release
53
-*/
54
-//#define VERSION "v1.24"
55
-
56
-#include <cstdio>
57
-#include <cstring>
58
-#include <cctype>
59
-#include <cerrno>
60
-#include <cstdlib>
61
-#include <cstdarg>
62
-
63
-#include <cstdint>
64
-#ifndef _MSC_VER
65
-# include <unistd.h>
66
-# include <sys/param.h>
67
-#else
68
-#define MAXPATHLEN 1024
69
-#endif
70
-
71
-#include <sys/stat.h>
72
-
73
-namespace DLDI
74
-{
75
-
76
-typedef int32_t addr_t;
77
-typedef unsigned char data_t;
78
-
79
-#define FIX_ALL 0x01
80
-#define FIX_GLUE 0x02
81
-#define FIX_GOT 0x04
82
-#define FIX_BSS 0x08
83
-
84
-enum DldiOffsets
85
-{
86
-	DO_magicString = 0x00, // "\xED\xA5\x8D\xBF Chishm"
87
-	DO_magicToken = 0x00, // 0xBF8DA5ED
88
-	DO_magicShortString = 0x04, // " Chishm"
89
-	DO_version = 0x0C,
90
-	DO_driverSize = 0x0D,
91
-	DO_fixSections = 0x0E,
92
-	DO_allocatedSpace = 0x0F,
93
-
94
-	DO_friendlyName = 0x10,
95
-
96
-	DO_text_start = 0x40, // Data start
97
-	DO_data_end = 0x44, // Data end
98
-	DO_glue_start = 0x48, // Interworking glue start -- Needs address fixing
99
-	DO_glue_end = 0x4C, // Interworking glue end
100
-	DO_got_start = 0x50, // GOT start -- Needs address fixing
101
-	DO_got_end = 0x54, // GOT end
102
-	DO_bss_start = 0x58, // bss start -- Needs setting to zero
103
-	DO_bss_end = 0x5C, // bss end
104
-
105
-	// IO_INTERFACE data
106
-	DO_ioType = 0x60,
107
-	DO_features = 0x64,
108
-	DO_startup = 0x68,
109
-	DO_isInserted = 0x6C,
110
-	DO_readSectors = 0x70,
111
-	DO_writeSectors = 0x74,
112
-	DO_clearStatus = 0x78,
113
-	DO_shutdown = 0x7C,
114
-	DO_code = 0x80
115
-};
116
-
117
-const data_t dldiMagicString[] = "\xED\xA5\x8D\xBF Chishm";
118
-const char dldiFileExtension[] = ".dldi";
119
-
120
-addr_t readAddr(data_t *mem, addr_t offset)
121
-{
122
-	return mem[offset + 0] | (mem[offset + 1] << 8) | (mem[offset + 2] << 16) | (mem[offset + 3] << 24);
123
-}
124
-
125
-void writeAddr(data_t *mem, addr_t offset, addr_t value)
126
-{
127
-	mem[offset + 0] = value & 0xFF;
128
-	mem[offset + 1] = (value >> 8) & 0xFF;
129
-	mem[offset + 2] = (value >> 16) & 0xFF;
130
-	mem[offset + 3] = (value >> 24) & 0xFF;
131
-}
132
-
133
-int stringCaseInsensitiveCompare(const char *str1, const char *str2)
134
-{
135
-	while (tolower(*str1) == tolower(*str2))
136
-	{
137
-		if (!*str1)
138
-			return 0;
139
-		++str1;
140
-		++str2;
141
-	}
142
-	return tolower(*str1) - tolower(*str2);
143
-}
144
-
145
-bool stringEndsWith(const char *str, const char *end)
146
-{
147
-	if (strlen(str) < strlen(end))
148
-		return false;
149
-	const char *strEnd = &str[strlen (str) - strlen(end)];
150
-	return !stringCaseInsensitiveCompare(strEnd, end);
151
-}
152
-
153
-bool stringStartsWith(const char *str, const char *start)
154
-{
155
-	return strstr(str, start) == str;
156
-}
157
-
158
-addr_t quickFind(const data_t *data, const data_t *search, size_t dataLen, size_t searchLen)
159
-{
160
-	const int32_t *dataChunk = reinterpret_cast<const int32_t *>(data);
161
-	int searchChunk = reinterpret_cast<const int32_t *>(search)[0];
162
-	addr_t dataChunkEnd = static_cast<addr_t>(dataLen / sizeof(int32_t));
163
-
164
-	for (addr_t i = 0; i < dataChunkEnd; ++i)
165
-	{
166
-		if (dataChunk[i] == searchChunk)
167
-		{
168
-			if (i * sizeof(int32_t) + searchLen > dataLen)
169
-				return -1;
170
-			if (!memcmp(&data[i * sizeof(int32_t)], search, searchLen))
171
-				return i * sizeof(int32_t);
172
-		}
173
-	}
174
-
175
-	return -1;
176
-}
177
-
178
-FILE *openDLDIFile(const char *argv0, char *dldiFileName )
179
-{
180
-	// add .dldi extension to filename
181
-	if (!stringEndsWith(dldiFileName, dldiFileExtension))
182
-		strcat(dldiFileName, dldiFileExtension);
183
-
184
-	printf("Trying \"%s\"\n", dldiFileName);
185
-	// try opening from current directory
186
-	FILE *dldiFile = fopen(dldiFileName, "rb");
187
-
188
-	if (dldiFile)
189
-		return dldiFile;
190
-
191
-	// check if the filename has a path component
192
-	// check both slash varieties, win32 understands both
193
-	// if we have a directory separator don't bother with search paths
194
-	if (strstr(dldiFileName, "\\"))
195
-		return nullptr;
196
-	if (strstr(dldiFileName ,"/"))
197
-		return nullptr;
198
-
199
-	// check for DLDIPATH in environment
200
-	char *dldiPATH = getenv("DLDIPATH");
201
-
202
-	char appPath[MAXPATHLEN];
203
-	if (dldiPATH)
204
-	{
205
-		strcpy(appPath, dldiPATH);
206
-		if (appPath[strlen(appPath)] != '\\' &&  appPath[strlen(appPath)] != '/')
207
-			strcat(appPath, "/");
208
-		strcat(appPath, dldiFileName);
209
-
210
-		printf("Trying \"%s\"\n", appPath);
211
-		dldiFile = fopen(appPath,"rb");
212
-
213
-		if (dldiFile)
214
-			return dldiFile;
215
-	}
216
-
217
-	char *lastSlash = nullptr;
218
-	char *ptr = const_cast<char *>(argv0);
219
-
220
-	while (*(ptr++))
221
-		if (*ptr == '\\' || * ptr == '/')
222
-			lastSlash = ptr;
223
-
224
-	char appName[MAXPATHLEN];
225
-	char appPathName[MAXPATHLEN];
226
-	if (lastSlash)
227
-	{
228
-		*(lastSlash++) = '\0';
229
-		strcpy(appPath, argv0);
230
-		strcpy(appName, lastSlash);
231
-		strcat(appPath, "/");
232
-	}
233
-	else
234
-	{
235
-		strcpy(appPath, "");
236
-		strcpy(appName, argv0);
237
-	}
238
-
239
-	// finally try in the application path
240
-	// if argv0 contains a directory separator we have a path component
241
-
242
-	if (!strstr(appPath,"\\") && !strstr(appPath,"/"))
243
-	{
244
-		// no path in argv0 so search system path
245
-		char *sysPATH = getenv("PATH");
246
-		char *thisPATH = sysPATH;
247
-		printf("Searching system path\n%s\n", sysPATH);
248
-
249
-		while (1)
250
-		{
251
-			char *nextPATH = strstr(thisPATH, ":" ); // find next PATH separator
252
-
253
-			if (nextPATH)
254
-				*(nextPATH++) = '\0'; // terminate string, point to next component
255
-
256
-			strcpy(appPath, thisPATH);
257
-			strcat(appPath, "/");
258
-			strcpy(appPathName, appPath);
259
-			strcat(appPathName, appName); // add application name
260
-
261
-			struct stat buf;
262
-			if (!stat(appPathName, &buf)) // if it exists we found the path
263
-				break;
264
-
265
-			thisPATH = nextPATH;
266
-			strcpy(appPath, "");// empty path
267
-			if (!thisPATH)
268
-				break;
269
-		}
270
-	}
271
-
272
-	strcat(appPath, "dldi/"); // add dldi folder
273
-	strcat(appPath, dldiFileName); // add dldi filename to path
274
-	printf("Trying \"%s\"\n", appPath);
275
-
276
-	return fopen(appPath, "rb"); // no more places to check, just return this handle
277
-}
278
-
279
-//  Source File: mpcf.dldi
280
-//         Time: 6/14/2010 9:38 PM
281
-// Orig. Offset: 0 / 0x00000000
282
-//       Length: 1876 / 0x00000754 (bytes)
283
-static data_t mpcf_dldi[] =
284
-{
285
-    0xED, 0xA5, 0x8D, 0xBF, 0x20, 0x43, 0x68, 0x69, 0x73, 0x68, 0x6D, 0x00, 0x01, 0x0B, 0x0C, 0x00,
286
-    0x47, 0x42, 0x41, 0x20, 0x4D, 0x6F, 0x76, 0x69, 0x65, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72,
287
-    0x20, 0x28, 0x43, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x20, 0x46, 0x6C, 0x61, 0x73, 0x68, 0x29,
288
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289
-    0x00, 0x00, 0x80, 0xBF, 0x54, 0x07, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF,
290
-    0x50, 0x07, 0x80, 0xBF, 0x50, 0x07, 0x80, 0xBF, 0x54, 0x07, 0x80, 0xBF, 0x70, 0x07, 0x80, 0xBF,
291
-    0x4D, 0x50, 0x43, 0x46, 0x13, 0x00, 0x00, 0x00, 0x3C, 0x01, 0x80, 0xBF, 0x98, 0x01, 0x80, 0xBF,
292
-    0x78, 0x02, 0x80, 0xBF, 0xC8, 0x04, 0x80, 0xBF, 0xC8, 0x01, 0x80, 0xBF, 0x10, 0x07, 0x80, 0xBF,
293
-    0x0D, 0xC0, 0xA0, 0xE1, 0xF8, 0xDF, 0x2D, 0xE9, 0x04, 0xB0, 0x4C, 0xE2, 0x28, 0xD0, 0x4B, 0xE2,
294
-    0xF0, 0x6F, 0x9D, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1, 0x10, 0x40, 0x2D, 0xE9, 0x2C, 0x40, 0x9F, 0xE5,
295
-    0x00, 0x30, 0xD4, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x24, 0x20, 0x9F, 0xE5, 0x05, 0x00, 0x00, 0x1A,
296
-    0x00, 0x00, 0x52, 0xE3, 0x1C, 0x00, 0x9F, 0xE5, 0x0F, 0xE0, 0xA0, 0x11, 0x12, 0xFF, 0x2F, 0x11,
297
-    0x01, 0x30, 0xA0, 0xE3, 0x00, 0x30, 0xC4, 0xE5, 0x10, 0x40, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
298
-    0x54, 0x07, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x48, 0x07, 0x80, 0xBF, 0x04, 0xE0, 0x2D, 0xE5,
299
-    0x40, 0x30, 0x9F, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x04, 0xD0, 0x4D, 0xE2, 0x38, 0x00, 0x9F, 0xE5,
300
-    0x38, 0x10, 0x9F, 0xE5, 0x0F, 0xE0, 0xA0, 0x11, 0x13, 0xFF, 0x2F, 0x11, 0x30, 0x00, 0x9F, 0xE5,
301
-    0x00, 0x30, 0x90, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x28, 0x30, 0x9F, 0xE5, 0x02, 0x00, 0x00, 0x0A,
302
-    0x00, 0x00, 0x53, 0xE3, 0x0F, 0xE0, 0xA0, 0x11, 0x13, 0xFF, 0x2F, 0x11, 0x04, 0xD0, 0x8D, 0xE2,
303
-    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x48, 0x07, 0x80, 0xBF,
304
-    0x58, 0x07, 0x80, 0xBF, 0x4C, 0x07, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x09, 0x14, 0xA0, 0xE3,
305
-    0x06, 0x18, 0x81, 0xE2, 0xB0, 0x30, 0xD1, 0xE1, 0x03, 0x30, 0xE0, 0xE1, 0x03, 0x38, 0xA0, 0xE1,
306
-    0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0xB0, 0x30, 0xC1, 0xE1, 0xB0, 0x20, 0xD1, 0xE1,
307
-    0x04, 0xE0, 0x2D, 0xE5, 0xAA, 0xCC, 0xA0, 0xE3, 0x55, 0xEC, 0xE0, 0xE3, 0x03, 0x00, 0x52, 0xE1,
308
-    0xAA, 0xE0, 0x4E, 0xE2, 0x55, 0xC0, 0x8C, 0xE2, 0x00, 0x00, 0xA0, 0xE3, 0x03, 0x00, 0x00, 0x1A,
309
-    0xB0, 0xE0, 0xC1, 0xE1, 0xB0, 0x30, 0xD1, 0xE1, 0x0C, 0x00, 0x53, 0xE0, 0x01, 0x00, 0xA0, 0x13,
310
-    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x26, 0x35, 0xA0, 0xE3, 0x03, 0x37, 0x83, 0xE2,
311
-    0x50, 0x20, 0xA0, 0xE3, 0xB0, 0x20, 0xC3, 0xE1, 0xB0, 0x00, 0xD3, 0xE1, 0x00, 0x08, 0xA0, 0xE1,
312
-    0x20, 0x08, 0xA0, 0xE1, 0xFF, 0x00, 0x00, 0xE2, 0x50, 0x00, 0x50, 0xE3, 0x00, 0x00, 0xA0, 0x13,
313
-    0x01, 0x00, 0xA0, 0x03, 0x1E, 0xFF, 0x2F, 0xE1, 0x09, 0x34, 0xA0, 0xE3, 0x0E, 0x38, 0x83, 0xE2,
314
-    0xB0, 0x20, 0xD3, 0xE1, 0x80, 0x00, 0x12, 0xE3, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0x00, 0xA0, 0xE3,
315
-    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x50, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
316
-    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
317
-    0x80, 0x00, 0x12, 0xE3, 0x01, 0x00, 0x80, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
318
-    0x26, 0x15, 0xA0, 0xE3, 0x03, 0x17, 0x81, 0xE2, 0xB0, 0x30, 0xD1, 0xE1, 0x50, 0x30, 0x13, 0xE2,
319
-    0x01, 0x00, 0xA0, 0x13, 0x1E, 0xFF, 0x2F, 0x11, 0x26, 0x27, 0xA0, 0xE3, 0x96, 0x2C, 0x82, 0xE2,
320
-    0x03, 0x00, 0xA0, 0xE1, 0x80, 0x20, 0x82, 0xE2, 0x01, 0x00, 0x00, 0xEA, 0x02, 0x00, 0x50, 0xE1,
321
-    0x0A, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD1, 0xE1, 0x50, 0x30, 0x13, 0xE2, 0x01, 0x00, 0x80, 0xE2,
322
-    0xF9, 0xFF, 0xFF, 0x0A, 0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2,
323
-    0x03, 0x00, 0x50, 0xE1, 0x00, 0x00, 0xA0, 0xC3, 0x01, 0x00, 0xA0, 0xD3, 0x1E, 0xFF, 0x2F, 0xE1,
324
-    0x03, 0x00, 0xA0, 0xE1, 0x1E, 0xFF, 0x2F, 0xE1, 0xF0, 0x4B, 0x2D, 0xE9, 0x09, 0x34, 0xA0, 0xE3,
325
-    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0xC0, 0xD3, 0xE1, 0x80, 0x00, 0x1C, 0xE3, 0x10, 0xD0, 0x4D, 0xE2,
326
-    0x01, 0x90, 0xA0, 0xE1, 0x02, 0xE0, 0xA0, 0xE1, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0xC0, 0xA0, 0xE3,
327
-    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x5C, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
328
-    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
329
-    0x80, 0x00, 0x12, 0xE3, 0x01, 0xC0, 0x8C, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
330
-    0x26, 0xC5, 0xA0, 0xE3, 0x03, 0xC7, 0x8C, 0xE2, 0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x30, 0x13, 0xE2,
331
-    0x26, 0x17, 0xA0, 0x03, 0x96, 0x1C, 0x81, 0x02, 0x03, 0x20, 0xA0, 0x01, 0x80, 0x10, 0x81, 0x02,
332
-    0x02, 0x00, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1, 0x67, 0x00, 0x00, 0x0A,
333
-    0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x00, 0x13, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF9, 0xFF, 0xFF, 0x0A,
334
-    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
335
-    0x5E, 0x00, 0x00, 0xCA, 0xFF, 0x00, 0x59, 0xE3, 0x00, 0x30, 0xA0, 0x83, 0x0C, 0x30, 0x8D, 0x85,
336
-    0x5E, 0x00, 0x00, 0x9A, 0x00, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2,
337
-    0x09, 0x54, 0xA0, 0xE3, 0x04, 0x30, 0x8D, 0xE5, 0x0C, 0x30, 0x9D, 0xE5, 0x20, 0x4C, 0xA0, 0xE1,
338
-    0x0E, 0xB0, 0xA0, 0xE1, 0x05, 0x60, 0xA0, 0xE1, 0x20, 0x74, 0xA0, 0xE1, 0x05, 0x20, 0xA0, 0xE1,
339
-    0x20, 0x18, 0xA0, 0xE1, 0x05, 0xC0, 0xA0, 0xE1, 0x05, 0x00, 0xA0, 0xE1, 0x05, 0xE0, 0xA0, 0xE1,
340
-    0x01, 0x57, 0x85, 0xE2, 0xB0, 0x30, 0xC5, 0xE1, 0x04, 0x30, 0x9D, 0xE5, 0x0F, 0x40, 0x04, 0xE2,
341
-    0x06, 0x68, 0x86, 0xE2, 0xB0, 0x30, 0xC6, 0xE1, 0xFF, 0x70, 0x07, 0xE2, 0x02, 0x27, 0x82, 0xE2,
342
-    0xFF, 0x10, 0x01, 0xE2, 0x0A, 0x08, 0x80, 0xE2, 0xE0, 0x40, 0x84, 0xE3, 0x03, 0xC7, 0x8C, 0xE2,
343
-    0x0E, 0xE8, 0x8E, 0xE2, 0x20, 0x30, 0xA0, 0xE3, 0xB0, 0x70, 0xC2, 0xE1, 0xB0, 0x10, 0xC0, 0xE1,
344
-    0xB0, 0x40, 0xCC, 0xE1, 0xB0, 0x30, 0xCE, 0xE1, 0x0B, 0x80, 0xA0, 0xE1, 0x01, 0x90, 0x59, 0xE2,
345
-    0x3E, 0x00, 0x00, 0x3A, 0x26, 0x25, 0xA0, 0xE3, 0x03, 0x27, 0x82, 0xE2, 0xB0, 0x30, 0xD2, 0xE1,
346
-    0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3,
347
-    0x26, 0x17, 0xA0, 0x13, 0x96, 0x1C, 0x81, 0x12, 0x02, 0x00, 0xA0, 0x11, 0x80, 0x10, 0x81, 0x12,
348
-    0x00, 0x20, 0xA0, 0x13, 0x02, 0x00, 0x00, 0x1A, 0x0D, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1,
349
-    0x26, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD0, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
350
-    0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF6, 0xFF, 0xFF, 0x1A,
351
-    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
352
-    0x1A, 0x00, 0x00, 0xCA, 0x01, 0x00, 0x18, 0xE3, 0x0B, 0x10, 0xA0, 0x01, 0xFF, 0x20, 0xA0, 0x03,
353
-    0x09, 0x04, 0xA0, 0x03, 0x0E, 0x00, 0x00, 0x0A, 0x08, 0x10, 0xA0, 0xE1, 0xFF, 0x00, 0xA0, 0xE3,
354
-    0x09, 0xC4, 0xA0, 0xE3, 0xB0, 0x30, 0xDC, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
355
-    0x01, 0x00, 0x40, 0xE2, 0x43, 0x24, 0xA0, 0xE1, 0x01, 0x00, 0x70, 0xE3, 0x01, 0x20, 0xC1, 0xE5,
356
-    0x00, 0x30, 0xC1, 0xE5, 0x02, 0x10, 0x81, 0xE2, 0xF5, 0xFF, 0xFF, 0x1A, 0x02, 0x8C, 0x88, 0xE2,
357
-    0xCD, 0xFF, 0xFF, 0xEA, 0x01, 0x20, 0x42, 0xE2, 0xB0, 0x30, 0xD0, 0xE1, 0x01, 0x00, 0x72, 0xE3,
358
-    0xB2, 0x30, 0xC1, 0xE0, 0xFA, 0xFF, 0xFF, 0x1A, 0x02, 0xBC, 0x8B, 0xE2, 0xC6, 0xFF, 0xFF, 0xEA,
359
-    0x00, 0x00, 0xA0, 0xE3, 0x10, 0xD0, 0x8D, 0xE2, 0xF0, 0x4B, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
360
-    0x09, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0x0C, 0x30, 0x8D, 0xE5, 0x9C, 0xFF, 0xFF, 0xEA,
361
-    0x01, 0x00, 0xA0, 0xE3, 0xF6, 0xFF, 0xFF, 0xEA, 0xF0, 0x4B, 0x2D, 0xE9, 0x09, 0x34, 0xA0, 0xE3,
362
-    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0xC0, 0xD3, 0xE1, 0x80, 0x00, 0x1C, 0xE3, 0x10, 0xD0, 0x4D, 0xE2,
363
-    0x01, 0x90, 0xA0, 0xE1, 0x02, 0xE0, 0xA0, 0xE1, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0xC0, 0xA0, 0xE3,
364
-    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x5C, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
365
-    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
366
-    0x80, 0x00, 0x12, 0xE3, 0x01, 0xC0, 0x8C, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
367
-    0x26, 0xC5, 0xA0, 0xE3, 0x03, 0xC7, 0x8C, 0xE2, 0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x30, 0x13, 0xE2,
368
-    0x26, 0x17, 0xA0, 0x03, 0x96, 0x1C, 0x81, 0x02, 0x03, 0x20, 0xA0, 0x01, 0x80, 0x10, 0x81, 0x02,
369
-    0x02, 0x00, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1, 0x65, 0x00, 0x00, 0x0A,
370
-    0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x00, 0x13, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF9, 0xFF, 0xFF, 0x0A,
371
-    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
372
-    0x5C, 0x00, 0x00, 0xCA, 0xFF, 0x00, 0x59, 0xE3, 0x00, 0x30, 0xA0, 0x83, 0x0C, 0x30, 0x8D, 0x85,
373
-    0x5C, 0x00, 0x00, 0x9A, 0x00, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2,
374
-    0x09, 0x54, 0xA0, 0xE3, 0x04, 0x30, 0x8D, 0xE5, 0x0C, 0x30, 0x9D, 0xE5, 0x20, 0x4C, 0xA0, 0xE1,
375
-    0x0E, 0xB0, 0xA0, 0xE1, 0x05, 0x60, 0xA0, 0xE1, 0x20, 0x74, 0xA0, 0xE1, 0x05, 0x20, 0xA0, 0xE1,
376
-    0x20, 0x18, 0xA0, 0xE1, 0x05, 0xC0, 0xA0, 0xE1, 0x05, 0x00, 0xA0, 0xE1, 0x05, 0xE0, 0xA0, 0xE1,
377
-    0x01, 0x57, 0x85, 0xE2, 0xB0, 0x30, 0xC5, 0xE1, 0x04, 0x30, 0x9D, 0xE5, 0x0F, 0x40, 0x04, 0xE2,
378
-    0x06, 0x68, 0x86, 0xE2, 0xB0, 0x30, 0xC6, 0xE1, 0xFF, 0x70, 0x07, 0xE2, 0x02, 0x27, 0x82, 0xE2,
379
-    0xFF, 0x10, 0x01, 0xE2, 0x0A, 0x08, 0x80, 0xE2, 0xE0, 0x40, 0x84, 0xE3, 0x03, 0xC7, 0x8C, 0xE2,
380
-    0x0E, 0xE8, 0x8E, 0xE2, 0x30, 0x30, 0xA0, 0xE3, 0xB0, 0x70, 0xC2, 0xE1, 0xB0, 0x10, 0xC0, 0xE1,
381
-    0xB0, 0x40, 0xCC, 0xE1, 0xB0, 0x30, 0xCE, 0xE1, 0x0B, 0x80, 0xA0, 0xE1, 0x01, 0x90, 0x59, 0xE2,
382
-    0x3C, 0x00, 0x00, 0x3A, 0x26, 0x25, 0xA0, 0xE3, 0x03, 0x27, 0x82, 0xE2, 0xB0, 0x30, 0xD2, 0xE1,
383
-    0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3,
384
-    0x26, 0x17, 0xA0, 0x13, 0x96, 0x1C, 0x81, 0x12, 0x02, 0x00, 0xA0, 0x11, 0x80, 0x10, 0x81, 0x12,
385
-    0x00, 0x20, 0xA0, 0x13, 0x02, 0x00, 0x00, 0x1A, 0x0D, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1,
386
-    0x24, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD0, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
387
-    0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF6, 0xFF, 0xFF, 0x1A,
388
-    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
389
-    0x18, 0x00, 0x00, 0xCA, 0x01, 0x00, 0x18, 0xE3, 0x0B, 0x10, 0xA0, 0x01, 0xFF, 0x20, 0xA0, 0x03,
390
-    0x09, 0x04, 0xA0, 0x03, 0x0C, 0x00, 0x00, 0x0A, 0x08, 0x10, 0xA0, 0xE1, 0xFF, 0x00, 0xA0, 0xE3,
391
-    0x09, 0xC4, 0xA0, 0xE3, 0x00, 0x30, 0xD1, 0xE5, 0x01, 0x20, 0xD1, 0xE5, 0x01, 0x00, 0x40, 0xE2,
392
-    0x02, 0x34, 0x83, 0xE1, 0x01, 0x00, 0x70, 0xE3, 0xB0, 0x30, 0xCC, 0xE1, 0x02, 0x10, 0x81, 0xE2,
393
-    0xF7, 0xFF, 0xFF, 0x1A, 0x02, 0x8C, 0x88, 0xE2, 0xCF, 0xFF, 0xFF, 0xEA, 0x01, 0x20, 0x42, 0xE2,
394
-    0xB2, 0x30, 0xD1, 0xE0, 0x01, 0x00, 0x72, 0xE3, 0xB0, 0x30, 0xC0, 0xE1, 0xFA, 0xFF, 0xFF, 0x1A,
395
-    0x02, 0xBC, 0x8B, 0xE2, 0xC8, 0xFF, 0xFF, 0xEA, 0x00, 0x00, 0xA0, 0xE3, 0x10, 0xD0, 0x8D, 0xE2,
396
-    0xF0, 0x4B, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1, 0x09, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
397
-    0x0C, 0x30, 0x8D, 0xE5, 0x9E, 0xFF, 0xFF, 0xEA, 0x01, 0x00, 0xA0, 0xE3, 0xF6, 0xFF, 0xFF, 0xEA,
398
-    0x04, 0xE0, 0x2D, 0xE5, 0x04, 0xD0, 0x4D, 0xE2, 0xAA, 0xFE, 0xFF, 0xEB, 0x04, 0xD0, 0x8D, 0xE2,
399
-    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x0D, 0xC0, 0xA0, 0xE1, 0xF8, 0xDF, 0x2D, 0xE9,
400
-    0x04, 0xB0, 0x4C, 0xE2, 0x28, 0xD0, 0x4B, 0xE2, 0xF0, 0x6F, 0x9D, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
401
-    0xDC, 0x00, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402
-    0x00, 0x00, 0x00, 0x00,
403
-}; //unsigned char mpcf_dldi[1876]
404
-
405
-bool tryPatch(void *data, size_t size)
406
-{
407
-	// Find the DSDI reserved space in the file
408
-	addr_t patchOffset = quickFind(static_cast<data_t *>(data), dldiMagicString, size, sizeof(dldiMagicString) / sizeof(char));
409
-
410
-	// no DLDI section
411
-	if (patchOffset < 0)
412
-		return false;
413
-
414
-	data_t *pDH = mpcf_dldi;
415
-	data_t *pAH = static_cast<data_t *>(data) + patchOffset;
416
-
417
-	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace])
418
-	{
419
-		printf("Not enough space for patch. Available %d bytes, need %d bytes\n", 1 << pAH[DO_allocatedSpace], 1 << pDH[DO_driverSize]);
420
-		return false;
421
-	}
422
-
423
-	if (memcmp(&pAH[DO_friendlyName], "Default (No interface)", 22))
424
-	{
425
-		printf("Would have been a candidate for auto-patch DLDI, but there was already a patch installed.");
426
-		return false;
427
-	}
428
-
429
-	//----should be able to patch OK-----
430
-
431
-	addr_t memOffset; // Offset of DLDI after the file is loaded into memory
432
-	addr_t relocationOffset; // Value added to all offsets within the patch to fix it properly
433
-	addr_t ddmemOffset; // Original offset used in the DLDI file
434
-	addr_t ddmemStart; // Start of range that offsets can be in the DLDI file
435
-	addr_t ddmemEnd; // End of range that offsets can be in the DLDI file
436
-	addr_t ddmemSize; // Size of range that offsets can be in the DLDI file
437
-
438
-	addr_t addrIter;
439
-
440
-	memOffset = readAddr(pAH, DO_text_start);
441
-	if (!memOffset)
442
-		memOffset = readAddr(pAH, DO_startup) - DO_code;
443
-	ddmemOffset = readAddr(pDH, DO_text_start);
444
-	relocationOffset = memOffset - ddmemOffset;
445
-
446
-	printf("AUTO-PATCHING DLDI to MPCF! Lucky you!\n\n");
447
-	printf("Old driver:          %s\n", &pAH[DO_friendlyName]);
448
-	printf("New driver:          %s\n", &pDH[DO_friendlyName]);
449
-	printf("\n");
450
-	printf("Position in file:    0x%08X\n", patchOffset);
451
-	printf("Position in memory:  0x%08X\n", memOffset);
452
-	printf("Patch base address:  0x%08X\n", ddmemOffset);
453
-	printf("Relocation offset:   0x%08X\n", relocationOffset);
454
-	printf("\n");
455
-
456
-	ddmemStart = readAddr(pDH, DO_text_start);
457
-	ddmemSize = 1 << pDH[DO_driverSize];
458
-	ddmemEnd = ddmemStart + ddmemSize;
459
-
460
-	// Remember how much space is actually reserved
461
-	pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
462
-	// Copy the DLDI patch into the application
463
-	memcpy(pAH, pDH, sizeof(mpcf_dldi));
464
-
465
-	// Fix the section pointers in the header
466
-	writeAddr(pAH, DO_text_start, readAddr(pAH, DO_text_start) + relocationOffset);
467
-	writeAddr(pAH, DO_data_end, readAddr(pAH, DO_data_end) + relocationOffset);
468
-	writeAddr(pAH, DO_glue_start, readAddr(pAH, DO_glue_start) + relocationOffset);
469
-	writeAddr(pAH, DO_glue_end, readAddr(pAH, DO_glue_end) + relocationOffset);
470
-	writeAddr(pAH, DO_got_start, readAddr(pAH, DO_got_start) + relocationOffset);
471
-	writeAddr(pAH, DO_got_end, readAddr(pAH, DO_got_end) + relocationOffset);
472
-	writeAddr(pAH, DO_bss_start, readAddr(pAH, DO_bss_start) + relocationOffset);
473
-	writeAddr(pAH, DO_bss_end, readAddr(pAH, DO_bss_end) + relocationOffset);
474
-	// Fix the function pointers in the header
475
-	writeAddr(pAH, DO_startup, readAddr(pAH, DO_startup) + relocationOffset);
476
-	writeAddr(pAH, DO_isInserted, readAddr(pAH, DO_isInserted) + relocationOffset);
477
-	writeAddr(pAH, DO_readSectors, readAddr(pAH, DO_readSectors) + relocationOffset);
478
-	writeAddr(pAH, DO_writeSectors, readAddr(pAH, DO_writeSectors) + relocationOffset);
479
-	writeAddr(pAH, DO_clearStatus, readAddr(pAH, DO_clearStatus) + relocationOffset);
480
-	writeAddr(pAH, DO_shutdown, readAddr(pAH, DO_shutdown) + relocationOffset);
481
-
482
-	if (pDH[DO_fixSections] & FIX_ALL)
483
-		// Search through and fix pointers within the data section of the file
484
-		for (addrIter = readAddr(pDH, DO_text_start) - ddmemStart; addrIter < readAddr(pDH, DO_data_end) - ddmemStart; ++addrIter)
485
-			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
486
-				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
487
-
488
-	if (pDH[DO_fixSections] & FIX_GLUE)
489
-		// Search through and fix pointers within the glue section of the file
490
-		for (addrIter = readAddr(pDH, DO_glue_start) - ddmemStart; addrIter < readAddr(pDH, DO_glue_end) - ddmemStart; ++addrIter)
491
-			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
492
-				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
493
-
494
-	if (pDH[DO_fixSections] & FIX_GOT)
495
-		// Search through and fix pointers within the Global Offset Table section of the file
496
-		for (addrIter = readAddr(pDH, DO_got_start) - ddmemStart; addrIter < readAddr(pDH, DO_got_end) - ddmemStart; ++addrIter)
497
-			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
498
-				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
499
-
500
-	if (pDH[DO_fixSections] & FIX_BSS)
501
-		// Initialise the BSS to 0
502
-		memset(&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
503
-
504
-	return true;
505
-}
506
-
507
-} //namespace DLDI
Browse code

* Small Makefile changes.

* Removed a couple files that were not being used.
* Cleanups found with clang's -Weverything (and shutting it up about a
lot of things that were ridiculous, as well as ignoring some of the
warnings still coming up).

Naram Qashat authored on 2014/09/25 12:28:21
Showing 1 changed files
... ...
@@ -76,22 +76,11 @@ namespace DLDI
76 76
 typedef int32_t addr_t;
77 77
 typedef unsigned char data_t;
78 78
 
79
-#define FEATURE_MEDIUM_CANREAD 0x00000001
80
-#define FEATURE_MEDIUM_CANWRITE 0x00000002
81
-#define FEATURE_SLOT_GBA 0x00000010
82
-#define FEATURE_SLOT_NDS 0x00000020
83
-
84
-#define MAGIC_TOKEN 0xBF8DA5ED
85
-
86 79
 #define FIX_ALL 0x01
87 80
 #define FIX_GLUE 0x02
88 81
 #define FIX_GOT 0x04
89 82
 #define FIX_BSS 0x08
90 83
 
91
-#define DLDI_VERSION 1
92
-
93
-#define EXIT_NO_DLDI_SECTION	2
94
-
95 84
 enum DldiOffsets
96 85
 {
97 86
 	DO_magicString = 0x00, // "\xED\xA5\x8D\xBF Chishm"
... ...
@@ -291,7 +280,7 @@ FILE *openDLDIFile(const char *argv0, char *dldiFileName )
291 280
 //         Time: 6/14/2010 9:38 PM
292 281
 // Orig. Offset: 0 / 0x00000000
293 282
 //       Length: 1876 / 0x00000754 (bytes)
294
-data_t mpcf_dldi[] =
283
+static data_t mpcf_dldi[] =
295 284
 {
296 285
     0xED, 0xA5, 0x8D, 0xBF, 0x20, 0x43, 0x68, 0x69, 0x73, 0x68, 0x6D, 0x00, 0x01, 0x0B, 0x0C, 0x00,
297 286
     0x47, 0x42, 0x41, 0x20, 0x4D, 0x6F, 0x76, 0x69, 0x65, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72,
Browse code

Added Lanczos interpolation to the NCSF plugin, and cleaned up a bit of the other code, as well as removing pstdint.h since it's no longer needed.

Naram Qashat authored on 2013/04/23 20:29:21
Showing 1 changed files
... ...
@@ -60,7 +60,7 @@
60 60
 #include <cstdlib>
61 61
 #include <cstdarg>
62 62
 
63
-#include "pstdint.h"
63
+#include <cstdint>
64 64
 #ifndef _MSC_VER
65 65
 # include <unistd.h>
66 66
 # include <sys/param.h>
Browse code

Removed a bunch of casts, they seem to be fine without them in most cases.

Naram Qashat authored on 2013/04/18 23:22:54
Showing 1 changed files
... ...
@@ -130,15 +130,15 @@ const char dldiFileExtension[] = ".dldi";
130 130
 
131 131
 addr_t readAddr(data_t *mem, addr_t offset)
132 132
 {
133
-	return static_cast<addr_t>(mem[offset + 0] | (mem[offset + 1] << 8) | (mem[offset + 2] << 16) | (mem[offset + 3] << 24));
133
+	return mem[offset + 0] | (mem[offset + 1] << 8) | (mem[offset + 2] << 16) | (mem[offset + 3] << 24);
134 134
 }
135 135
 
136 136
 void writeAddr(data_t *mem, addr_t offset, addr_t value)
137 137
 {
138
-	mem[offset + 0] = static_cast<data_t>(value);
139
-	mem[offset + 1] = static_cast<data_t>(value >> 8);
140
-	mem[offset + 2] = static_cast<data_t>(value >> 16);
141
-	mem[offset + 3] = static_cast<data_t>(value >> 24);
138
+	mem[offset + 0] = value & 0xFF;
139
+	mem[offset + 1] = (value >> 8) & 0xFF;
140
+	mem[offset + 2] = (value >> 16) & 0xFF;
141
+	mem[offset + 3] = (value >> 24) & 0xFF;
142 142
 }
143 143
 
144 144
 int stringCaseInsensitiveCompare(const char *str1, const char *str2)
Browse code

Updating in_2sf to use a newish version of DeSmuME, 0.9.9 from SVN. Somewhat cleaned up as well, but not everything because it's a pain in the ass.

Naram Qashat authored on 2013/04/18 17:22:55
Showing 1 changed files
... ...
@@ -62,12 +62,10 @@
62 62
 
63 63
 #include "pstdint.h"
64 64
 #ifndef _MSC_VER
65
-//#include <stdint.h>
66
-#include <unistd.h>
67
-#include <sys/param.h>
65
+# include <unistd.h>
66
+# include <sys/param.h>
68 67
 #else
69
-//typedef int int32_t;
70
-#define MAXPATHLEN      1024
68
+#define MAXPATHLEN 1024
71 69
 #endif
72 70
 
73 71
 #include <sys/stat.h>
... ...
@@ -75,33 +73,30 @@
75 73
 namespace DLDI
76 74
 {
77 75
 
78
-//#ifndef bool
79
-// typedef enum {false = 0, true = !0} bool;
80
-//#endif
81
-
82 76
 typedef int32_t addr_t;
83 77
 typedef unsigned char data_t;
84 78
 
85
-#define FEATURE_MEDIUM_CANREAD	0x00000001
86
-#define FEATURE_MEDIUM_CANWRITE	0x00000002
87
-#define FEATURE_SLOT_GBA	0x00000010
88
-#define FEATURE_SLOT_NDS	0x00000020
79
+#define FEATURE_MEDIUM_CANREAD 0x00000001
80
+#define FEATURE_MEDIUM_CANWRITE 0x00000002
81
+#define FEATURE_SLOT_GBA 0x00000010
82
+#define FEATURE_SLOT_NDS 0x00000020
89 83
 
90 84
 #define MAGIC_TOKEN 0xBF8DA5ED
91 85
 
92
-#define FIX_ALL	0x01
93
-#define FIX_GLUE	0x02
94
-#define FIX_GOT	0x04
95
-#define FIX_BSS	0x08
86
+#define FIX_ALL 0x01
87
+#define FIX_GLUE 0x02
88
+#define FIX_GOT 0x04
89
+#define FIX_BSS 0x08
96 90
 
97 91
 #define DLDI_VERSION 1
98 92
 
99 93
 #define EXIT_NO_DLDI_SECTION	2
100 94
 
101
-enum DldiOffsets {
102
-	DO_magicString = 0x00,			// "\xED\xA5\x8D\xBF Chishm"
103
-	DO_magicToken = 0x00,			// 0xBF8DA5ED
104
-	DO_magicShortString = 0x04,		// " Chishm"
95
+enum DldiOffsets
96
+{
97
+	DO_magicString = 0x00, // "\xED\xA5\x8D\xBF Chishm"
98
+	DO_magicToken = 0x00, // 0xBF8DA5ED
99
+	DO_magicShortString = 0x04, // " Chishm"
105 100
 	DO_version = 0x0C,
106 101
 	DO_driverSize = 0x0D,
107 102
 	DO_fixSections = 0x0E,
... ...
@@ -109,14 +104,14 @@ enum DldiOffsets {
109 104
 
110 105
 	DO_friendlyName = 0x10,
111 106
 
112
-	DO_text_start = 0x40,			// Data start
113
-	DO_data_end = 0x44,				// Data end
114
-	DO_glue_start = 0x48,			// Interworking glue start	-- Needs address fixing
115
-	DO_glue_end = 0x4C,				// Interworking glue end
116
-	DO_got_start = 0x50,			// GOT start					-- Needs address fixing
117
-	DO_got_end = 0x54,				// GOT end
118
-	DO_bss_start = 0x58,			// bss start					-- Needs setting to zero
119
-	DO_bss_end = 0x5C,				// bss end
107
+	DO_text_start = 0x40, // Data start
108
+	DO_data_end = 0x44, // Data end
109
+	DO_glue_start = 0x48, // Interworking glue start -- Needs address fixing
110
+	DO_glue_end = 0x4C, // Interworking glue end
111
+	DO_got_start = 0x50, // GOT start -- Needs address fixing
112
+	DO_got_end = 0x54, // GOT end
113
+	DO_bss_start = 0x58, // bss start -- Needs setting to zero
114
+	DO_bss_end = 0x5C, // bss end
120 115
 
121 116
 	// IO_INTERFACE data
122 117
 	DO_ioType = 0x60,
... ...
@@ -133,385 +128,170 @@ enum DldiOffsets {
133 128
 const data_t dldiMagicString[] = "\xED\xA5\x8D\xBF Chishm";
134 129
 const char dldiFileExtension[] = ".dldi";
135 130
 
136
-
137
-void printUsage (char* programName) {
138
-	printf ("Usage:\n");
139
-	printf ("%s <dldi> <app>\n", programName);
140
-	printf ("   <dldi>        the dldi patch file to apply\n");
141
-	printf ("   <app>         the application binary to apply the patch to\n");
142
-	return;
143
-}
144
-
145
-addr_t readAddr (data_t *mem, addr_t offset) {
146
-	return (addr_t)(
147
-			(mem[offset + 0] << 0) |
148
-			(mem[offset + 1] << 8) |
149
-			(mem[offset + 2] << 16) |
150
-			(mem[offset + 3] << 24)
151
-		);
131
+addr_t readAddr(data_t *mem, addr_t offset)
132
+{
133
+	return static_cast<addr_t>(mem[offset + 0] | (mem[offset + 1] << 8) | (mem[offset + 2] << 16) | (mem[offset + 3] << 24));
152 134
 }
153 135
 
154
-void writeAddr (data_t *mem, addr_t offset, addr_t value) {
155
-	mem[offset + 0] = (data_t)(value >> 0);
156
-	mem[offset + 1] = (data_t)(value >> 8);
157
-	mem[offset + 2] = (data_t)(value >> 16);
158
-	mem[offset + 3] = (data_t)(value >> 24);
136
+void writeAddr(data_t *mem, addr_t offset, addr_t value)
137
+{
138
+	mem[offset + 0] = static_cast<data_t>(value);
139
+	mem[offset + 1] = static_cast<data_t>(value >> 8);
140
+	mem[offset + 2] = static_cast<data_t>(value >> 16);
141
+	mem[offset + 3] = static_cast<data_t>(value >> 24);
159 142
 }
160 143
 
161
-int stringCaseInsensitiveCompare (const char *str1, const char *str2) {
162
-	while (tolower(*str1) == tolower(*str2)) {
163
-		if (*str1 == '\0') {
144
+int stringCaseInsensitiveCompare(const char *str1, const char *str2)
145
+{
146
+	while (tolower(*str1) == tolower(*str2))
147
+	{
148
+		if (!*str1)
164 149
 			return 0;
165
-		}
166
-		str1++;
167
-		str2++;
150
+		++str1;
151
+		++str2;
168 152
 	}
169 153
 	return tolower(*str1) - tolower(*str2);
170 154
 }
171 155
 
172
-bool stringEndsWith (const char *str, const char *end) {
173
-	const char* strEnd;
174
-	if (strlen (str) < strlen(end)) {
156
+bool stringEndsWith(const char *str, const char *end)
157
+{
158
+	if (strlen(str) < strlen(end))
175 159
 		return false;
176
-	}
177
-	strEnd = &str[strlen (str) - strlen(end)];
178
-	return stringCaseInsensitiveCompare (strEnd, end) == 0;
160
+	const char *strEnd = &str[strlen (str) - strlen(end)];
161
+	return !stringCaseInsensitiveCompare(strEnd, end);
179 162
 }
180 163
 
181
-bool stringStartsWith (const char *str, const char *start) {
182
-	return strstr (str, start) == str;
164
+bool stringStartsWith(const char *str, const char *start)
165
+{
166
+	return strstr(str, start) == str;
183 167
 }
184 168
 
185
-addr_t quickFind (const data_t* data, const data_t* search, size_t dataLen, size_t searchLen) {
186
-	const int32_t* dataChunk = (const int32_t*) data;
187
-	int searchChunk = ((const int32_t*)search)[0];
188
-	addr_t i;
189
-	addr_t dataChunkEnd = (addr_t)(dataLen / sizeof(int32_t));
169
+addr_t quickFind(const data_t *data, const data_t *search, size_t dataLen, size_t searchLen)
170
+{
171
+	const int32_t *dataChunk = reinterpret_cast<const int32_t *>(data);
172
+	int searchChunk = reinterpret_cast<const int32_t *>(search)[0];
173
+	addr_t dataChunkEnd = static_cast<addr_t>(dataLen / sizeof(int32_t));
190 174
 
191
-	for ( i = 0; i < dataChunkEnd; i++) {
192
-		if (dataChunk[i] == searchChunk) {
193
-			if ((i*sizeof(int32_t) + searchLen) > dataLen) {
175
+	for (addr_t i = 0; i < dataChunkEnd; ++i)
176
+	{
177
+		if (dataChunk[i] == searchChunk)
178
+		{
179
+			if (i * sizeof(int32_t) + searchLen > dataLen)
194 180
 				return -1;
195
-			}
196
-			if (memcmp (&data[i*sizeof(int32_t)], search, searchLen) == 0) {
197
-				return i*sizeof(int32_t);
198
-			}
181
+			if (!memcmp(&data[i * sizeof(int32_t)], search, searchLen))
182
+				return i * sizeof(int32_t);
199 183
 		}
200 184
 	}
201 185
 
202 186
 	return -1;
203 187
 }
204 188
 
205
-FILE *openDLDIFile(const char *argv0, char *dldiFileName ) {
206
-
207
-
208
-	FILE *dldiFile;
209
-	char *dldiPATH;
210
-	char appPath[MAXPATHLEN];
211
-	char appName[MAXPATHLEN];
212
-	char appPathName[MAXPATHLEN];
213
-
214
-	char *ptr, *lastSlash;
215
-	struct stat buf;
216
-
189
+FILE *openDLDIFile(const char *argv0, char *dldiFileName )
190
+{
217 191
 	// add .dldi extension to filename
218
-	if (!stringEndsWith (dldiFileName, dldiFileExtension)) {
219
-		strcat (dldiFileName, dldiFileExtension);
220
-	}
192
+	if (!stringEndsWith(dldiFileName, dldiFileExtension))
193
+		strcat(dldiFileName, dldiFileExtension);
221 194
 
222
-	printf ("Trying \"%s\"\n", dldiFileName);
195
+	printf("Trying \"%s\"\n", dldiFileName);
223 196
 	// try opening from current directory
224
-	dldiFile = fopen(dldiFileName,"rb");
197
+	FILE *dldiFile = fopen(dldiFileName, "rb");
225 198
 
226
-	if ( NULL != dldiFile ) return dldiFile;
199
+	if (dldiFile)
200
+		return dldiFile;
227 201
 
228 202
 	// check if the filename has a path component
229 203
 	// check both slash varieties, win32 understands both
230 204
 	// if we have a directory separator don't bother with search paths
231
-	if ( NULL != strstr(dldiFileName,"\\") ) return NULL;
232
-	if ( NULL != strstr(dldiFileName,"/") ) return NULL;
205
+	if (strstr(dldiFileName, "\\"))
206
+		return nullptr;
207
+	if (strstr(dldiFileName ,"/"))
208
+		return nullptr;
233 209
 
234 210
 	// check for DLDIPATH in environment
235
-	dldiPATH = getenv("DLDIPATH");
236
-
211
+	char *dldiPATH = getenv("DLDIPATH");
237 212
 
238
-	if ( NULL != dldiPATH ) {
239
-		strcpy(appPath,dldiPATH);
240
-		if ( appPath[strlen(appPath)] != '\\' &&  appPath[strlen(appPath)] != '/' )
241
-			strcat(appPath,"/");
242
-		strcat ( appPath, dldiFileName );
213
+	char appPath[MAXPATHLEN];
214
+	if (dldiPATH)
215
+	{
216
+		strcpy(appPath, dldiPATH);
217
+		if (appPath[strlen(appPath)] != '\\' &&  appPath[strlen(appPath)] != '/')
218
+			strcat(appPath, "/");
219
+		strcat(appPath, dldiFileName);
243 220
 
244
-		printf ("Trying \"%s\"\n", appPath);
221
+		printf("Trying \"%s\"\n", appPath);
245 222
 		dldiFile = fopen(appPath,"rb");
246 223
 
247
-		if ( NULL != dldiFile ) return dldiFile;
248
-
224
+		if (dldiFile)
225
+			return dldiFile;
249 226
 	}
250 227
 
228
+	char *lastSlash = nullptr;
229
+	char *ptr = const_cast<char *>(argv0);
251 230
 
252
-	lastSlash = NULL;
253
-	ptr = const_cast<char *>(argv0);
254
-
255
-	while ( *(ptr++) != 0 ) {
256
-		if ( *ptr == '\\' || * ptr == '/' )
231
+	while (*(ptr++))
232
+		if (*ptr == '\\' || * ptr == '/')
257 233
 			lastSlash = ptr;
258
-	}
259 234
 
260
-	if ( NULL != lastSlash ) {
235
+	char appName[MAXPATHLEN];
236
+	char appPathName[MAXPATHLEN];
237
+	if (lastSlash)
238
+	{
261 239
 		*(lastSlash++) = '\0';
262 240
 		strcpy(appPath, argv0);
263 241
 		strcpy(appName, lastSlash);
264 242
 		strcat(appPath, "/");
265
-	} else {
243
+	}
244
+	else
245
+	{
266 246
 		strcpy(appPath, "");
267 247
 		strcpy(appName, argv0);
268 248
 	}
269 249
 
270
-
271 250
 	// finally try in the application path
272 251
 	// if argv0 contains a directory separator we have a path component
273 252
 
274
-	if ( NULL == strstr(appPath,"\\") &&  NULL == strstr(appPath,"/") ) {
275
-
253
+	if (!strstr(appPath,"\\") && !strstr(appPath,"/"))
254
+	{
276 255
 		// no path in argv0 so search system path
277 256
 		char *sysPATH = getenv("PATH");
278
-		char *nextPATH;
279 257
 		char *thisPATH = sysPATH;
280
-		printf("Searching system path\n%s\n",sysPATH);
258
+		printf("Searching system path\n%s\n", sysPATH);
281 259
 
282
-		while(1) {
283
-			nextPATH = strstr(thisPATH, ":" ); // find next PATH separator
260
+		while (1)
261
+		{
262
+			char *nextPATH = strstr(thisPATH, ":" ); // find next PATH separator
284 263
 
285
-			if ( NULL != nextPATH )
264
+			if (nextPATH)
286 265
 				*(nextPATH++) = '\0'; // terminate string, point to next component
287 266
 
288
-			strcpy(appPath,thisPATH);
289
-			strcat(appPath,"/");
290
-			strcpy(appPathName,appPath);
291
-			strcat(appPathName,appName);		// add application name
267
+			strcpy(appPath, thisPATH);
268
+			strcat(appPath, "/");
269
+			strcpy(appPathName, appPath);
270
+			strcat(appPathName, appName); // add application name
292 271
 
293
-			if ( stat(appPathName,&buf) == 0 )	// if it exists we found the path
272
+			struct stat buf;
273
+			if (!stat(appPathName, &buf)) // if it exists we found the path
294 274
 				break;
295 275
 
296 276
 			thisPATH = nextPATH;
297
-			strcpy(appPath,"");		// empty path
298
-			if ( thisPATH == NULL) break;
277
+			strcpy(appPath, "");// empty path
278
+			if (!thisPATH)
279
+				break;
299 280
 		}
300 281
 	}
301 282
 
302
-	strcat(appPath,"dldi/");		// add dldi folder
303
-	strcat(appPath,dldiFileName);	// add dldi filename to path
304
-	printf ("Trying \"%s\"\n", appPath);
283
+	strcat(appPath, "dldi/"); // add dldi folder
284
+	strcat(appPath, dldiFileName); // add dldi filename to path
285
+	printf("Trying \"%s\"\n", appPath);
305 286
 
306
-	return fopen(appPath,"rb");		// no more places to check, just return this handle
287
+	return fopen(appPath, "rb"); // no more places to check, just return this handle
307 288
 }
308 289
 
309
-//int main(int argc, char* argv[])
310
-//{
311
-//
312
-//	char *dldiFileName = NULL;
313
-//	char *appFileName = NULL;
314
-//
315
-//	addr_t memOffset;			// Offset of DLDI after the file is loaded into memory
316
-//	addr_t patchOffset;			// Position of patch destination in the file
317
-//	addr_t relocationOffset;	// Value added to all offsets within the patch to fix it properly
318
-//	addr_t ddmemOffset;			// Original offset used in the DLDI file
319
-//	addr_t ddmemStart;			// Start of range that offsets can be in the DLDI file
320
-//	addr_t ddmemEnd;			// End of range that offsets can be in the DLDI file
321
-//	addr_t ddmemSize;			// Size of range that offsets can be in the DLDI file
322
-//
323
-//	addr_t addrIter;
324
-//
325
-//	FILE* dldiFile;
326
-//	FILE* appFile;
327
-//
328
-//	data_t *pDH;
329
-//	data_t *pAH;
330
-//
331
-//	data_t *appFileData = NULL;
332
-//	size_t appFileSize = 0;
333
-//	data_t *dldiFileData = NULL;
334
-//	size_t dldiFileSize = 0;
335
-//
336
-//	int i;
337
-//
338
-//	printf ("Dynamically Linked Disk Interface patch tool " VERSION " by Michael Chisholm (Chishm)\n\n");
339
-//
340
-//	for (i = 1; i < argc; i++) {
341
-//		if (dldiFileName == NULL) {
342
-//			dldiFileName = (char*) malloc (strlen (argv[i]) + 1 + sizeof(dldiFileExtension));
343
-//			if (!dldiFileName) {
344
-//				return EXIT_FAILURE;
345
-//			}
346
-//			strcpy (dldiFileName, argv[i]);
347
-//		} else if (appFileName == NULL) {
348
-//			appFileName = (char*) malloc (strlen (argv[i]) + 1);
349
-//			if (!appFileName) {
350
-//				return EXIT_FAILURE;
351
-//			}
352
-//			strcpy (appFileName, argv[i]);
353
-//		} else {
354
-//			printUsage (argv[0]);
355
-//			return EXIT_FAILURE;
356
-//		}
357
-//	}
358
-//
359
-//	if ((dldiFileName == NULL) || (appFileName == NULL)) {
360
-//		printUsage (argv[0]);
361
-//		return EXIT_FAILURE;
362
-//	}
363
-//
364
-//	if (!(dldiFile = openDLDIFile(argv[0],dldiFileName))) {
365
-//		printf ("Cannot open \"%s\" - %s\n", dldiFileName, strerror(errno));
366
-//			return EXIT_FAILURE;
367
-//	}
368
-//
369
-//	if (!(appFile = fopen (appFileName, "rb+"))) {
370
-//		printf ("Cannot open \"%s\" - %s\n", appFileName, strerror(errno));
371
-//		return EXIT_FAILURE;
372
-//	}
373
-//
374
-//	// Load the app file and the DLDI patch file
375
-//	fseek (appFile, 0, SEEK_END);
376
-//	appFileSize = ftell(appFile);
377
-//	appFileData = (data_t*) malloc (appFileSize);
378
-//	fseek (appFile, 0, SEEK_SET);
379
-//
380
-//	fseek (dldiFile, 0, SEEK_END);
381
-//	dldiFileSize = ftell(dldiFile);
382
-//	dldiFileData = (data_t*) malloc (dldiFileSize);
383
-//	fseek (dldiFile, 0, SEEK_SET);
384
-//
385
-//	if (!appFileData || !dldiFileData) {
386
-//		fclose (appFile);
387
-//		fclose (dldiFile);
388
-//		if (appFileData) free (appFileData);
389
-//		if (dldiFileData) free (dldiFileData);
390
-//		printf ("Out of memory\n");
391
-//		return EXIT_FAILURE;
392
-//	}
393
-//
394
-//	fread (appFileData, 1, appFileSize, appFile);
395
-//	fread (dldiFileData, 1, dldiFileSize, dldiFile);
396
-//	fclose (dldiFile);
397
-//
398
-//	// Find the DSDI reserved space in the file
399
-//	patchOffset = quickFind (appFileData, dldiMagicString, appFileSize, sizeof(dldiMagicString)/sizeof(char));
400
-//
401
-//	if (patchOffset < 0) {
402
-//		printf ("%s does not have a DLDI section\n", appFileName);
403
-//		return EXIT_NO_DLDI_SECTION;
404
-//	}
405
-//
406
-//	pDH = dldiFileData;
407
-//	pAH = &appFileData[patchOffset];
408
-//
409
-//	// Make sure the DLDI file is valid and usable
410
-//	if (strcmp ((char*)dldiMagicString, (char*)&pDH[DO_magicString]) != 0) {
411
-//		printf ("Invalid DLDI file\n");
412
-//		return EXIT_FAILURE;
413
-//	}
414
-//	if (pDH[DO_version] != DLDI_VERSION) {
415
-//		printf ("Incorrect DLDI file version. Expected %d, found %d.\n", DLDI_VERSION, pDH[DO_version]);
416
-//		return EXIT_FAILURE;
417
-//	}
418
-//	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace]) {
419
-//		printf ("Not enough space for patch. Available %d bytes, need %d bytes\n", ( 1 << pAH[DO_allocatedSpace]), ( 1 << pDH[DO_driverSize]) );
420
-//		return EXIT_FAILURE;
421
-//	}
422
-//
423
-//	memOffset = readAddr (pAH, DO_text_start);
424
-//	if (memOffset == 0) {
425
-//			memOffset = readAddr (pAH, DO_startup) - DO_code;
426
-//	}
427
-//	ddmemOffset = readAddr (pDH, DO_text_start);
428
-//	relocationOffset = memOffset - ddmemOffset;
429
-//
430
-//	printf ("Old driver:          %s\n", &pAH[DO_friendlyName]);
431
-//	printf ("New driver:          %s\n", &pDH[DO_friendlyName]);
432
-//	printf ("\n");
433
-//	printf ("Position in file:    0x%08X\n", patchOffset);
434
-//	printf ("Position in memory:  0x%08X\n", memOffset);
435
-//	printf ("Patch base address:  0x%08X\n", ddmemOffset);
436
-//	printf ("Relocation offset:   0x%08X\n", relocationOffset);
437
-//	printf ("\n");
438
-//
439
-//	ddmemStart = readAddr (pDH, DO_text_start);
440
-//	ddmemSize = (1 << pDH[DO_driverSize]);
441
-//	ddmemEnd = ddmemStart + ddmemSize;
442
-//
443
-//	// Remember how much space is actually reserved
444
-//	pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
445
-//	// Copy the DLDI patch into the application
446
-//	memcpy (pAH, pDH, dldiFileSize);
447
-//
448
-//	// Fix the section pointers in the header
449
-//	writeAddr (pAH, DO_text_start, readAddr (pAH, DO_text_start) + relocationOffset);
450
-//	writeAddr (pAH, DO_data_end, readAddr (pAH, DO_data_end) + relocationOffset);
451
-//	writeAddr (pAH, DO_glue_start, readAddr (pAH, DO_glue_start) + relocationOffset);
452
-//	writeAddr (pAH, DO_glue_end, readAddr (pAH, DO_glue_end) + relocationOffset);
453
-//	writeAddr (pAH, DO_got_start, readAddr (pAH, DO_got_start) + relocationOffset);
454
-//	writeAddr (pAH, DO_got_end, readAddr (pAH, DO_got_end) + relocationOffset);
455
-//	writeAddr (pAH, DO_bss_start, readAddr (pAH, DO_bss_start) + relocationOffset);
456
-//	writeAddr (pAH, DO_bss_end, readAddr (pAH, DO_bss_end) + relocationOffset);
457
-//	// Fix the function pointers in the header
458
-//	writeAddr (pAH, DO_startup, readAddr (pAH, DO_startup) + relocationOffset);
459
-//	writeAddr (pAH, DO_isInserted, readAddr (pAH, DO_isInserted) + relocationOffset);
460
-//	writeAddr (pAH, DO_readSectors, readAddr (pAH, DO_readSectors) + relocationOffset);
461
-//	writeAddr (pAH, DO_writeSectors, readAddr (pAH, DO_writeSectors) + relocationOffset);
462
-//	writeAddr (pAH, DO_clearStatus, readAddr (pAH, DO_clearStatus) + relocationOffset);
463
-//	writeAddr (pAH, DO_shutdown, readAddr (pAH, DO_shutdown) + relocationOffset);
464
-//
465
-//	if (pDH[DO_fixSections] & FIX_ALL) {
466
-//		// Search through and fix pointers within the data section of the file
467
-//		for (addrIter = (readAddr(pDH, DO_text_start) - ddmemStart); addrIter < (readAddr(pDH, DO_data_end) - ddmemStart); addrIter++) {
468
-//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
469
-//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
470
-//			}
471
-//		}
472
-//	}
473
-//
474
-//	if (pDH[DO_fixSections] & FIX_GLUE) {
475
-//		// Search through and fix pointers within the glue section of the file
476
-//		for (addrIter = (readAddr(pDH, DO_glue_start) - ddmemStart); addrIter < (readAddr(pDH, DO_glue_end) - ddmemStart); addrIter++) {
477
-//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
478
-//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
479
-//			}
480
-//		}
481
-//	}
482
-//
483
-//	if (pDH[DO_fixSections] & FIX_GOT) {
484
-//		// Search through and fix pointers within the Global Offset Table section of the file
485
-//		for (addrIter = (readAddr(pDH, DO_got_start) - ddmemStart); addrIter < (readAddr(pDH, DO_got_end) - ddmemStart); addrIter++) {
486
-//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
487
-//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
488
-//			}
489
-//		}
490
-//	}
491
-//
492
-//	if (pDH[DO_fixSections] & FIX_BSS) {
493
-//		// Initialise the BSS to 0
494
-//		memset (&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
495
-//	}
496
-//
497
-//	// Write the patch back to the file
498
-//	fseek (appFile, patchOffset, SEEK_SET);
499
-//	fwrite (pAH, 1, ddmemSize, appFile);
500
-//	fclose (appFile);
501
-//
502
-//	free (appFileData);
503
-//	free (dldiFileData);
504
-//
505
-//	printf ("Patched successfully\n");
506
-//
507
-//	return EXIT_SUCCESS;
508
-//}
509
-
510 290
 //  Source File: mpcf.dldi
511 291
 //         Time: 6/14/2010 9:38 PM
512 292
 // Orig. Offset: 0 / 0x00000000
513 293
 //       Length: 1876 / 0x00000754 (bytes)
514
-data_t mpcf_dldi[1876] =
294
+data_t mpcf_dldi[] =
515 295
 {
516 296
     0xED, 0xA5, 0x8D, 0xBF, 0x20, 0x43, 0x68, 0x69, 0x73, 0x68, 0x6D, 0x00, 0x01, 0x0B, 0x0C, 0x00,
517 297
     0x47, 0x42, 0x41, 0x20, 0x4D, 0x6F, 0x76, 0x69, 0x65, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72,
... ...
@@ -633,24 +413,25 @@ data_t mpcf_dldi[1876] =
633 413
     0x00, 0x00, 0x00, 0x00,
634 414
 }; //unsigned char mpcf_dldi[1876]
635 415
 
636
-bool tryPatch(void* data, size_t size)
416
+bool tryPatch(void *data, size_t size)
637 417
 {
638 418
 	// Find the DSDI reserved space in the file
639
-	addr_t patchOffset = quickFind ((data_t*)data, dldiMagicString, size, sizeof(dldiMagicString)/sizeof(char));
419
+	addr_t patchOffset = quickFind(static_cast<data_t *>(data), dldiMagicString, size, sizeof(dldiMagicString) / sizeof(char));
640 420
 
641
-	//no DLDI section
421
+	// no DLDI section
642 422
 	if (patchOffset < 0)
643 423
 		return false;
644 424
 
645 425
 	data_t *pDH = mpcf_dldi;
646
-	data_t *pAH = (data_t*)data + patchOffset;
426
+	data_t *pAH = static_cast<data_t *>(data) + patchOffset;
647 427
 
648
-	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace]) {
649
-		printf ("Not enough space for patch. Available %d bytes, need %d bytes\n", ( 1 << pAH[DO_allocatedSpace]), ( 1 << pDH[DO_driverSize]) );
428
+	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace])
429
+	{
430
+		printf("Not enough space for patch. Available %d bytes, need %d bytes\n", 1 << pAH[DO_allocatedSpace], 1 << pDH[DO_driverSize]);
650 431
 		return false;
651 432
 	}
652 433
 
653
-	if(memcmp(&pAH[DO_friendlyName],"Default (No interface)",22))
434
+	if (memcmp(&pAH[DO_friendlyName], "Default (No interface)", 22))
654 435
 	{
655 436
 		printf("Would have been a candidate for auto-patch DLDI, but there was already a patch installed.");
656 437
 		return false;
... ...
@@ -658,90 +439,78 @@ bool tryPatch(void* data, size_t size)
658 439
 
659 440
 	//----should be able to patch OK-----
660 441
 
661
-	addr_t memOffset;			// Offset of DLDI after the file is loaded into memory
662
-	addr_t relocationOffset;	// Value added to all offsets within the patch to fix it properly
663
-	addr_t ddmemOffset;			// Original offset used in the DLDI file
664
-	addr_t ddmemStart;			// Start of range that offsets can be in the DLDI file
665
-	addr_t ddmemEnd;			// End of range that offsets can be in the DLDI file
666
-	addr_t ddmemSize;			// Size of range that offsets can be in the DLDI file
442
+	addr_t memOffset; // Offset of DLDI after the file is loaded into memory
443
+	addr_t relocationOffset; // Value added to all offsets within the patch to fix it properly
444
+	addr_t ddmemOffset; // Original offset used in the DLDI file
445
+	addr_t ddmemStart; // Start of range that offsets can be in the DLDI file
446
+	addr_t ddmemEnd; // End of range that offsets can be in the DLDI file
447
+	addr_t ddmemSize; // Size of range that offsets can be in the DLDI file
667 448
 
668 449
 	addr_t addrIter;
669 450
 
670
-
671
-	memOffset = readAddr (pAH, DO_text_start);
672
-	if (memOffset == 0) {
673
-			memOffset = readAddr (pAH, DO_startup) - DO_code;
674
-	}
675
-	ddmemOffset = readAddr (pDH, DO_text_start);
451
+	memOffset = readAddr(pAH, DO_text_start);
452
+	if (!memOffset)
453
+		memOffset = readAddr(pAH, DO_startup) - DO_code;
454
+	ddmemOffset = readAddr(pDH, DO_text_start);
676 455
 	relocationOffset = memOffset - ddmemOffset;
677 456
 
678
-	printf ("AUTO-PATCHING DLDI to MPCF! Lucky you!\n\n");
679
-	printf ("Old driver:          %s\n", &pAH[DO_friendlyName]);
680
-	printf ("New driver:          %s\n", &pDH[DO_friendlyName]);
681
-	printf ("\n");
682
-	printf ("Position in file:    0x%08X\n", patchOffset);
683
-	printf ("Position in memory:  0x%08X\n", memOffset);
684
-	printf ("Patch base address:  0x%08X\n", ddmemOffset);
685
-	printf ("Relocation offset:   0x%08X\n", relocationOffset);
686
-	printf ("\n");
687
-
688
-	ddmemStart = readAddr (pDH, DO_text_start);
689
-	ddmemSize = (1 << pDH[DO_driverSize]);
457
+	printf("AUTO-PATCHING DLDI to MPCF! Lucky you!\n\n");
458
+	printf("Old driver:          %s\n", &pAH[DO_friendlyName]);
459
+	printf("New driver:          %s\n", &pDH[DO_friendlyName]);
460
+	printf("\n");
461
+	printf("Position in file:    0x%08X\n", patchOffset);
462
+	printf("Position in memory:  0x%08X\n", memOffset);
463
+	printf("Patch base address:  0x%08X\n", ddmemOffset);
464
+	printf("Relocation offset:   0x%08X\n", relocationOffset);
465
+	printf("\n");
466
+
467
+	ddmemStart = readAddr(pDH, DO_text_start);
468
+	ddmemSize = 1 << pDH[DO_driverSize];
690 469
 	ddmemEnd = ddmemStart + ddmemSize;
691 470
 
692 471
 	// Remember how much space is actually reserved
693 472
 	pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
694 473
 	// Copy the DLDI patch into the application
695
-	memcpy (pAH, pDH, sizeof(mpcf_dldi));
474
+	memcpy(pAH, pDH, sizeof(mpcf_dldi));
696 475
 
697 476
 	// Fix the section pointers in the header
698
-	writeAddr (pAH, DO_text_start, readAddr (pAH, DO_text_start) + relocationOffset);
699
-	writeAddr (pAH, DO_data_end, readAddr (pAH, DO_data_end) + relocationOffset);
700
-	writeAddr (pAH, DO_glue_start, readAddr (pAH, DO_glue_start) + relocationOffset);
701
-	writeAddr (pAH, DO_glue_end, readAddr (pAH, DO_glue_end) + relocationOffset);
702
-	writeAddr (pAH, DO_got_start, readAddr (pAH, DO_got_start) + relocationOffset);
703
-	writeAddr (pAH, DO_got_end, readAddr (pAH, DO_got_end) + relocationOffset);
704
-	writeAddr (pAH, DO_bss_start, readAddr (pAH, DO_bss_start) + relocationOffset);
705
-	writeAddr (pAH, DO_bss_end, readAddr (pAH, DO_bss_end) + relocationOffset);
477
+	writeAddr(pAH, DO_text_start, readAddr(pAH, DO_text_start) + relocationOffset);
478
+	writeAddr(pAH, DO_data_end, readAddr(pAH, DO_data_end) + relocationOffset);
479
+	writeAddr(pAH, DO_glue_start, readAddr(pAH, DO_glue_start) + relocationOffset);
480
+	writeAddr(pAH, DO_glue_end, readAddr(pAH, DO_glue_end) + relocationOffset);
481
+	writeAddr(pAH, DO_got_start, readAddr(pAH, DO_got_start) + relocationOffset);
482
+	writeAddr(pAH, DO_got_end, readAddr(pAH, DO_got_end) + relocationOffset);
483
+	writeAddr(pAH, DO_bss_start, readAddr(pAH, DO_bss_start) + relocationOffset);
484
+	writeAddr(pAH, DO_bss_end, readAddr(pAH, DO_bss_end) + relocationOffset);
706 485
 	// Fix the function pointers in the header
707
-	writeAddr (pAH, DO_startup, readAddr (pAH, DO_startup) + relocationOffset);
708
-	writeAddr (pAH, DO_isInserted, readAddr (pAH, DO_isInserted) + relocationOffset);
709
-	writeAddr (pAH, DO_readSectors, readAddr (pAH, DO_readSectors) + relocationOffset);
710
-	writeAddr (pAH, DO_writeSectors, readAddr (pAH, DO_writeSectors) + relocationOffset);
711
-	writeAddr (pAH, DO_clearStatus, readAddr (pAH, DO_clearStatus) + relocationOffset);
712
-	writeAddr (pAH, DO_shutdown, readAddr (pAH, DO_shutdown) + relocationOffset);
713
-
714
-	if (pDH[DO_fixSections] & FIX_ALL) {
486
+	writeAddr(pAH, DO_startup, readAddr(pAH, DO_startup) + relocationOffset);
487
+	writeAddr(pAH, DO_isInserted, readAddr(pAH, DO_isInserted) + relocationOffset);
488
+	writeAddr(pAH, DO_readSectors, readAddr(pAH, DO_readSectors) + relocationOffset);
489
+	writeAddr(pAH, DO_writeSectors, readAddr(pAH, DO_writeSectors) + relocationOffset);
490
+	writeAddr(pAH, DO_clearStatus, readAddr(pAH, DO_clearStatus) + relocationOffset);
491
+	writeAddr(pAH, DO_shutdown, readAddr(pAH, DO_shutdown) + relocationOffset);
492
+
493
+	if (pDH[DO_fixSections] & FIX_ALL)
715 494
 		// Search through and fix pointers within the data section of the file
716
-		for (addrIter = (readAddr(pDH, DO_text_start) - ddmemStart); addrIter < (readAddr(pDH, DO_data_end) - ddmemStart); addrIter++) {
717
-			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
718
-				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
719
-			}
720
-		}
721
-	}
495
+		for (addrIter = readAddr(pDH, DO_text_start) - ddmemStart; addrIter < readAddr(pDH, DO_data_end) - ddmemStart; ++addrIter)
496
+			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
497
+				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
722 498
 
723
-	if (pDH[DO_fixSections] & FIX_GLUE) {
499
+	if (pDH[DO_fixSections] & FIX_GLUE)
724 500
 		// Search through and fix pointers within the glue section of the file
725
-		for (addrIter = (readAddr(pDH, DO_glue_start) - ddmemStart); addrIter < (readAddr(pDH, DO_glue_end) - ddmemStart); addrIter++) {
726
-			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
727
-				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
728
-			}
729
-		}
730
-	}
501
+		for (addrIter = readAddr(pDH, DO_glue_start) - ddmemStart; addrIter < readAddr(pDH, DO_glue_end) - ddmemStart; ++addrIter)
502
+			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
503
+				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
731 504
 
732
-	if (pDH[DO_fixSections] & FIX_GOT) {
505
+	if (pDH[DO_fixSections] & FIX_GOT)
733 506
 		// Search through and fix pointers within the Global Offset Table section of the file
734
-		for (addrIter = (readAddr(pDH, DO_got_start) - ddmemStart); addrIter < (readAddr(pDH, DO_got_end) - ddmemStart); addrIter++) {
735
-			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
736
-				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
737
-			}
738
-		}
739
-	}
507
+		for (addrIter = readAddr(pDH, DO_got_start) - ddmemStart; addrIter < readAddr(pDH, DO_got_end) - ddmemStart; ++addrIter)
508
+			if (ddmemStart <= readAddr(pAH, addrIter) && readAddr(pAH, addrIter) < ddmemEnd)
509
+				writeAddr(pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
740 510
 
741
-	if (pDH[DO_fixSections] & FIX_BSS) {
511
+	if (pDH[DO_fixSections] & FIX_BSS)
742 512
 		// Initialise the BSS to 0
743
-		memset (&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
744
-	}
513
+		memset(&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
745 514
 
746 515
 	return true;
747 516
 }
Browse code

Import actual code.

Naram Qashat authored on 2013/03/26 02:41:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,749 @@
1
+/*
2
+    dlditool - Dynamically Linked Disk Interface patch tool
3
+    Copyright (C) 2006  Michael Chisholm (Chishm)
4
+
5
+	Send all queries to chishm@hotmail.com
6
+
7
+    This program is free software; you can redistribute it and/or modify
8
+    it under the terms of the GNU General Public License as published by
9
+    the Free Software Foundation; either version 2 of the License, or
10
+    (at your option) any later version.
11
+
12
+    This program is distributed in the hope that it will be useful,
13
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+    GNU General Public License for more details.
16
+
17
+    You should have received a copy of the GNU General Public License along
18
+    with this program; if not, write to the Free Software Foundation, Inc.,
19
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
+
21
+	* v1.24d - 2010-06-14 - zeromus
22
+		* Modified for inclusion in desmume
23
+
24
+	* v1.24 - 2007-08-02 - SmileyDude
25
+		* Now using EXIT_SUCCESS and EXIT_FAILURE at the suggestion of MachinShin.
26
+		* Defined EXIT_NO_DLDI_SECTION for when there is no DLDI section in a file.
27
+		* Added cast to strcmp() call to appease the compiler.
28
+
29
+	* v1.23 - 2007-01-23 - Chishm
30
+		* Fixed bug when DLDI section doesn't exist
31
+		* addr_t is now a signed int
32
+
33
+	* v1.22 - 2007-01-12 - WinterMute
34
+		* add search paths for dldi files
35
+
36
+	* v1.21 - 2007-01-12 - Chishm
37
+		* Improved error messages
38
+
39
+	* v1.20 - 2007-01-11 - Chishm
40
+		* Changed offset calculation method
41
+
42
+	* v1.10 - 2007-01-07 - Chishm
43
+		* Removed assumptions about endianess of computer
44
+			* Word size shouldn't matter now either, except that an int type must be at least 32 bits long
45
+		* Added *.sc.nds and *.gba.nds file extensions
46
+		* Improved command line argument parsing
47
+
48
+	* v1.01 - 2006-12-30 - Chishm
49
+		* Minor bugfix parsing 3 arguments
50
+
51
+	* v1.00 - 2006-12-25 - Chishm
52
+		* Original release
53
+*/
54
+//#define VERSION "v1.24"
55
+
56
+#include <cstdio>
57
+#include <cstring>
58
+#include <cctype>
59
+#include <cerrno>
60
+#include <cstdlib>
61
+#include <cstdarg>
62
+
63
+#include "pstdint.h"
64
+#ifndef _MSC_VER
65
+//#include <stdint.h>
66
+#include <unistd.h>
67
+#include <sys/param.h>
68
+#else
69
+//typedef int int32_t;
70
+#define MAXPATHLEN      1024
71
+#endif
72
+
73
+#include <sys/stat.h>
74
+
75
+namespace DLDI
76
+{
77
+
78
+//#ifndef bool
79
+// typedef enum {false = 0, true = !0} bool;
80
+//#endif
81
+
82
+typedef int32_t addr_t;
83
+typedef unsigned char data_t;
84
+
85
+#define FEATURE_MEDIUM_CANREAD	0x00000001
86
+#define FEATURE_MEDIUM_CANWRITE	0x00000002
87
+#define FEATURE_SLOT_GBA	0x00000010
88
+#define FEATURE_SLOT_NDS	0x00000020
89
+
90
+#define MAGIC_TOKEN 0xBF8DA5ED
91
+
92
+#define FIX_ALL	0x01
93
+#define FIX_GLUE	0x02
94
+#define FIX_GOT	0x04
95
+#define FIX_BSS	0x08
96
+
97
+#define DLDI_VERSION 1
98
+
99
+#define EXIT_NO_DLDI_SECTION	2
100
+
101
+enum DldiOffsets {
102
+	DO_magicString = 0x00,			// "\xED\xA5\x8D\xBF Chishm"
103
+	DO_magicToken = 0x00,			// 0xBF8DA5ED
104
+	DO_magicShortString = 0x04,		// " Chishm"
105
+	DO_version = 0x0C,
106
+	DO_driverSize = 0x0D,
107
+	DO_fixSections = 0x0E,
108
+	DO_allocatedSpace = 0x0F,
109
+
110
+	DO_friendlyName = 0x10,
111
+
112
+	DO_text_start = 0x40,			// Data start
113
+	DO_data_end = 0x44,				// Data end
114
+	DO_glue_start = 0x48,			// Interworking glue start	-- Needs address fixing
115
+	DO_glue_end = 0x4C,				// Interworking glue end
116
+	DO_got_start = 0x50,			// GOT start					-- Needs address fixing
117
+	DO_got_end = 0x54,				// GOT end
118
+	DO_bss_start = 0x58,			// bss start					-- Needs setting to zero
119
+	DO_bss_end = 0x5C,				// bss end
120
+
121
+	// IO_INTERFACE data
122
+	DO_ioType = 0x60,
123
+	DO_features = 0x64,
124
+	DO_startup = 0x68,
125
+	DO_isInserted = 0x6C,
126
+	DO_readSectors = 0x70,
127
+	DO_writeSectors = 0x74,
128
+	DO_clearStatus = 0x78,
129
+	DO_shutdown = 0x7C,
130
+	DO_code = 0x80
131
+};
132
+
133
+const data_t dldiMagicString[] = "\xED\xA5\x8D\xBF Chishm";
134
+const char dldiFileExtension[] = ".dldi";
135
+
136
+
137
+void printUsage (char* programName) {
138
+	printf ("Usage:\n");
139
+	printf ("%s <dldi> <app>\n", programName);
140
+	printf ("   <dldi>        the dldi patch file to apply\n");
141
+	printf ("   <app>         the application binary to apply the patch to\n");
142
+	return;
143
+}
144
+
145
+addr_t readAddr (data_t *mem, addr_t offset) {
146
+	return (addr_t)(
147
+			(mem[offset + 0] << 0) |
148
+			(mem[offset + 1] << 8) |
149
+			(mem[offset + 2] << 16) |
150
+			(mem[offset + 3] << 24)
151
+		);
152
+}
153
+
154
+void writeAddr (data_t *mem, addr_t offset, addr_t value) {
155
+	mem[offset + 0] = (data_t)(value >> 0);
156
+	mem[offset + 1] = (data_t)(value >> 8);
157
+	mem[offset + 2] = (data_t)(value >> 16);
158
+	mem[offset + 3] = (data_t)(value >> 24);
159
+}
160
+
161
+int stringCaseInsensitiveCompare (const char *str1, const char *str2) {
162
+	while (tolower(*str1) == tolower(*str2)) {
163
+		if (*str1 == '\0') {
164
+			return 0;
165
+		}
166
+		str1++;
167
+		str2++;
168
+	}
169
+	return tolower(*str1) - tolower(*str2);
170
+}
171
+
172
+bool stringEndsWith (const char *str, const char *end) {
173
+	const char* strEnd;
174
+	if (strlen (str) < strlen(end)) {
175
+		return false;
176
+	}
177
+	strEnd = &str[strlen (str) - strlen(end)];
178
+	return stringCaseInsensitiveCompare (strEnd, end) == 0;
179
+}
180
+
181
+bool stringStartsWith (const char *str, const char *start) {
182
+	return strstr (str, start) == str;
183
+}
184
+
185
+addr_t quickFind (const data_t* data, const data_t* search, size_t dataLen, size_t searchLen) {
186
+	const int32_t* dataChunk = (const int32_t*) data;
187
+	int searchChunk = ((const int32_t*)search)[0];
188
+	addr_t i;
189
+	addr_t dataChunkEnd = (addr_t)(dataLen / sizeof(int32_t));
190
+
191
+	for ( i = 0; i < dataChunkEnd; i++) {
192
+		if (dataChunk[i] == searchChunk) {
193
+			if ((i*sizeof(int32_t) + searchLen) > dataLen) {
194
+				return -1;
195
+			}
196
+			if (memcmp (&data[i*sizeof(int32_t)], search, searchLen) == 0) {
197
+				return i*sizeof(int32_t);
198
+			}
199
+		}
200
+	}
201
+
202
+	return -1;
203
+}
204
+
205
+FILE *openDLDIFile(const char *argv0, char *dldiFileName ) {
206
+
207
+
208
+	FILE *dldiFile;
209
+	char *dldiPATH;
210
+	char appPath[MAXPATHLEN];
211
+	char appName[MAXPATHLEN];
212
+	char appPathName[MAXPATHLEN];
213
+
214
+	char *ptr, *lastSlash;
215
+	struct stat buf;
216
+
217
+	// add .dldi extension to filename
218
+	if (!stringEndsWith (dldiFileName, dldiFileExtension)) {
219
+		strcat (dldiFileName, dldiFileExtension);
220
+	}
221
+
222
+	printf ("Trying \"%s\"\n", dldiFileName);
223
+	// try opening from current directory
224
+	dldiFile = fopen(dldiFileName,"rb");
225
+
226
+	if ( NULL != dldiFile ) return dldiFile;
227
+
228
+	// check if the filename has a path component
229
+	// check both slash varieties, win32 understands both
230
+	// if we have a directory separator don't bother with search paths
231
+	if ( NULL != strstr(dldiFileName,"\\") ) return NULL;
232
+	if ( NULL != strstr(dldiFileName,"/") ) return NULL;
233
+
234
+	// check for DLDIPATH in environment
235
+	dldiPATH = getenv("DLDIPATH");
236
+
237
+
238
+	if ( NULL != dldiPATH ) {
239
+		strcpy(appPath,dldiPATH);
240
+		if ( appPath[strlen(appPath)] != '\\' &&  appPath[strlen(appPath)] != '/' )
241
+			strcat(appPath,"/");
242
+		strcat ( appPath, dldiFileName );
243
+
244
+		printf ("Trying \"%s\"\n", appPath);
245
+		dldiFile = fopen(appPath,"rb");
246
+
247
+		if ( NULL != dldiFile ) return dldiFile;
248
+
249
+	}
250
+
251
+
252
+	lastSlash = NULL;
253
+	ptr = const_cast<char *>(argv0);
254
+
255
+	while ( *(ptr++) != 0 ) {
256
+		if ( *ptr == '\\' || * ptr == '/' )
257
+			lastSlash = ptr;
258
+	}
259
+
260
+	if ( NULL != lastSlash ) {
261
+		*(lastSlash++) = '\0';
262
+		strcpy(appPath, argv0);
263
+		strcpy(appName, lastSlash);
264
+		strcat(appPath, "/");
265
+	} else {
266
+		strcpy(appPath, "");
267
+		strcpy(appName, argv0);
268
+	}
269
+
270
+
271
+	// finally try in the application path
272
+	// if argv0 contains a directory separator we have a path component
273
+
274
+	if ( NULL == strstr(appPath,"\\") &&  NULL == strstr(appPath,"/") ) {
275
+
276
+		// no path in argv0 so search system path
277
+		char *sysPATH = getenv("PATH");
278
+		char *nextPATH;
279
+		char *thisPATH = sysPATH;
280
+		printf("Searching system path\n%s\n",sysPATH);
281
+
282
+		while(1) {
283
+			nextPATH = strstr(thisPATH, ":" ); // find next PATH separator
284
+
285
+			if ( NULL != nextPATH )
286
+				*(nextPATH++) = '\0'; // terminate string, point to next component
287
+
288
+			strcpy(appPath,thisPATH);
289
+			strcat(appPath,"/");
290
+			strcpy(appPathName,appPath);
291
+			strcat(appPathName,appName);		// add application name
292
+
293
+			if ( stat(appPathName,&buf) == 0 )	// if it exists we found the path
294
+				break;
295
+
296
+			thisPATH = nextPATH;
297
+			strcpy(appPath,"");		// empty path
298
+			if ( thisPATH == NULL) break;
299
+		}
300
+	}
301
+
302
+	strcat(appPath,"dldi/");		// add dldi folder
303
+	strcat(appPath,dldiFileName);	// add dldi filename to path
304
+	printf ("Trying \"%s\"\n", appPath);
305
+
306
+	return fopen(appPath,"rb");		// no more places to check, just return this handle
307
+}
308
+
309
+//int main(int argc, char* argv[])
310
+//{
311
+//
312
+//	char *dldiFileName = NULL;
313
+//	char *appFileName = NULL;
314
+//
315
+//	addr_t memOffset;			// Offset of DLDI after the file is loaded into memory
316
+//	addr_t patchOffset;			// Position of patch destination in the file
317
+//	addr_t relocationOffset;	// Value added to all offsets within the patch to fix it properly
318
+//	addr_t ddmemOffset;			// Original offset used in the DLDI file
319
+//	addr_t ddmemStart;			// Start of range that offsets can be in the DLDI file
320
+//	addr_t ddmemEnd;			// End of range that offsets can be in the DLDI file
321
+//	addr_t ddmemSize;			// Size of range that offsets can be in the DLDI file
322
+//
323
+//	addr_t addrIter;
324
+//
325
+//	FILE* dldiFile;
326
+//	FILE* appFile;
327
+//
328
+//	data_t *pDH;
329
+//	data_t *pAH;
330
+//
331
+//	data_t *appFileData = NULL;
332
+//	size_t appFileSize = 0;
333
+//	data_t *dldiFileData = NULL;
334
+//	size_t dldiFileSize = 0;
335
+//
336
+//	int i;
337
+//
338
+//	printf ("Dynamically Linked Disk Interface patch tool " VERSION " by Michael Chisholm (Chishm)\n\n");
339
+//
340
+//	for (i = 1; i < argc; i++) {
341
+//		if (dldiFileName == NULL) {
342
+//			dldiFileName = (char*) malloc (strlen (argv[i]) + 1 + sizeof(dldiFileExtension));
343
+//			if (!dldiFileName) {
344
+//				return EXIT_FAILURE;
345
+//			}
346
+//			strcpy (dldiFileName, argv[i]);
347
+//		} else if (appFileName == NULL) {
348
+//			appFileName = (char*) malloc (strlen (argv[i]) + 1);
349
+//			if (!appFileName) {
350
+//				return EXIT_FAILURE;
351
+//			}
352
+//			strcpy (appFileName, argv[i]);
353
+//		} else {
354
+//			printUsage (argv[0]);
355
+//			return EXIT_FAILURE;
356
+//		}
357
+//	}
358
+//
359
+//	if ((dldiFileName == NULL) || (appFileName == NULL)) {
360
+//		printUsage (argv[0]);
361
+//		return EXIT_FAILURE;
362
+//	}
363
+//
364
+//	if (!(dldiFile = openDLDIFile(argv[0],dldiFileName))) {
365
+//		printf ("Cannot open \"%s\" - %s\n", dldiFileName, strerror(errno));
366
+//			return EXIT_FAILURE;
367
+//	}
368
+//
369
+//	if (!(appFile = fopen (appFileName, "rb+"))) {
370
+//		printf ("Cannot open \"%s\" - %s\n", appFileName, strerror(errno));
371
+//		return EXIT_FAILURE;
372
+//	}
373
+//
374
+//	// Load the app file and the DLDI patch file
375
+//	fseek (appFile, 0, SEEK_END);
376
+//	appFileSize = ftell(appFile);
377
+//	appFileData = (data_t*) malloc (appFileSize);
378
+//	fseek (appFile, 0, SEEK_SET);
379
+//
380
+//	fseek (dldiFile, 0, SEEK_END);
381
+//	dldiFileSize = ftell(dldiFile);
382
+//	dldiFileData = (data_t*) malloc (dldiFileSize);
383
+//	fseek (dldiFile, 0, SEEK_SET);
384
+//
385
+//	if (!appFileData || !dldiFileData) {
386
+//		fclose (appFile);
387
+//		fclose (dldiFile);
388
+//		if (appFileData) free (appFileData);
389
+//		if (dldiFileData) free (dldiFileData);
390
+//		printf ("Out of memory\n");
391
+//		return EXIT_FAILURE;
392
+//	}
393
+//
394
+//	fread (appFileData, 1, appFileSize, appFile);
395
+//	fread (dldiFileData, 1, dldiFileSize, dldiFile);
396
+//	fclose (dldiFile);
397
+//
398
+//	// Find the DSDI reserved space in the file
399
+//	patchOffset = quickFind (appFileData, dldiMagicString, appFileSize, sizeof(dldiMagicString)/sizeof(char));
400
+//
401
+//	if (patchOffset < 0) {
402
+//		printf ("%s does not have a DLDI section\n", appFileName);
403
+//		return EXIT_NO_DLDI_SECTION;
404
+//	}
405
+//
406
+//	pDH = dldiFileData;
407
+//	pAH = &appFileData[patchOffset];
408
+//
409
+//	// Make sure the DLDI file is valid and usable
410
+//	if (strcmp ((char*)dldiMagicString, (char*)&pDH[DO_magicString]) != 0) {
411
+//		printf ("Invalid DLDI file\n");
412
+//		return EXIT_FAILURE;
413
+//	}
414
+//	if (pDH[DO_version] != DLDI_VERSION) {
415
+//		printf ("Incorrect DLDI file version. Expected %d, found %d.\n", DLDI_VERSION, pDH[DO_version]);
416
+//		return EXIT_FAILURE;
417
+//	}
418
+//	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace]) {
419
+//		printf ("Not enough space for patch. Available %d bytes, need %d bytes\n", ( 1 << pAH[DO_allocatedSpace]), ( 1 << pDH[DO_driverSize]) );
420
+//		return EXIT_FAILURE;
421
+//	}
422
+//
423
+//	memOffset = readAddr (pAH, DO_text_start);
424
+//	if (memOffset == 0) {
425
+//			memOffset = readAddr (pAH, DO_startup) - DO_code;
426
+//	}
427
+//	ddmemOffset = readAddr (pDH, DO_text_start);
428
+//	relocationOffset = memOffset - ddmemOffset;
429
+//
430
+//	printf ("Old driver:          %s\n", &pAH[DO_friendlyName]);
431
+//	printf ("New driver:          %s\n", &pDH[DO_friendlyName]);
432
+//	printf ("\n");
433
+//	printf ("Position in file:    0x%08X\n", patchOffset);
434
+//	printf ("Position in memory:  0x%08X\n", memOffset);
435
+//	printf ("Patch base address:  0x%08X\n", ddmemOffset);
436
+//	printf ("Relocation offset:   0x%08X\n", relocationOffset);
437
+//	printf ("\n");
438
+//
439
+//	ddmemStart = readAddr (pDH, DO_text_start);
440
+//	ddmemSize = (1 << pDH[DO_driverSize]);
441
+//	ddmemEnd = ddmemStart + ddmemSize;
442
+//
443
+//	// Remember how much space is actually reserved
444
+//	pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
445
+//	// Copy the DLDI patch into the application
446
+//	memcpy (pAH, pDH, dldiFileSize);
447
+//
448
+//	// Fix the section pointers in the header
449
+//	writeAddr (pAH, DO_text_start, readAddr (pAH, DO_text_start) + relocationOffset);
450
+//	writeAddr (pAH, DO_data_end, readAddr (pAH, DO_data_end) + relocationOffset);
451
+//	writeAddr (pAH, DO_glue_start, readAddr (pAH, DO_glue_start) + relocationOffset);
452
+//	writeAddr (pAH, DO_glue_end, readAddr (pAH, DO_glue_end) + relocationOffset);
453
+//	writeAddr (pAH, DO_got_start, readAddr (pAH, DO_got_start) + relocationOffset);
454
+//	writeAddr (pAH, DO_got_end, readAddr (pAH, DO_got_end) + relocationOffset);
455
+//	writeAddr (pAH, DO_bss_start, readAddr (pAH, DO_bss_start) + relocationOffset);
456
+//	writeAddr (pAH, DO_bss_end, readAddr (pAH, DO_bss_end) + relocationOffset);
457
+//	// Fix the function pointers in the header
458
+//	writeAddr (pAH, DO_startup, readAddr (pAH, DO_startup) + relocationOffset);
459
+//	writeAddr (pAH, DO_isInserted, readAddr (pAH, DO_isInserted) + relocationOffset);
460
+//	writeAddr (pAH, DO_readSectors, readAddr (pAH, DO_readSectors) + relocationOffset);
461
+//	writeAddr (pAH, DO_writeSectors, readAddr (pAH, DO_writeSectors) + relocationOffset);
462
+//	writeAddr (pAH, DO_clearStatus, readAddr (pAH, DO_clearStatus) + relocationOffset);
463
+//	writeAddr (pAH, DO_shutdown, readAddr (pAH, DO_shutdown) + relocationOffset);
464
+//
465
+//	if (pDH[DO_fixSections] & FIX_ALL) {
466
+//		// Search through and fix pointers within the data section of the file
467
+//		for (addrIter = (readAddr(pDH, DO_text_start) - ddmemStart); addrIter < (readAddr(pDH, DO_data_end) - ddmemStart); addrIter++) {
468
+//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
469
+//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
470
+//			}
471
+//		}
472
+//	}
473
+//
474
+//	if (pDH[DO_fixSections] & FIX_GLUE) {
475
+//		// Search through and fix pointers within the glue section of the file
476
+//		for (addrIter = (readAddr(pDH, DO_glue_start) - ddmemStart); addrIter < (readAddr(pDH, DO_glue_end) - ddmemStart); addrIter++) {
477
+//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
478
+//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
479
+//			}
480
+//		}
481
+//	}
482
+//
483
+//	if (pDH[DO_fixSections] & FIX_GOT) {
484
+//		// Search through and fix pointers within the Global Offset Table section of the file
485
+//		for (addrIter = (readAddr(pDH, DO_got_start) - ddmemStart); addrIter < (readAddr(pDH, DO_got_end) - ddmemStart); addrIter++) {
486
+//			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
487
+//				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
488
+//			}
489
+//		}
490
+//	}
491
+//
492
+//	if (pDH[DO_fixSections] & FIX_BSS) {
493
+//		// Initialise the BSS to 0
494
+//		memset (&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
495
+//	}
496
+//
497
+//	// Write the patch back to the file
498
+//	fseek (appFile, patchOffset, SEEK_SET);
499
+//	fwrite (pAH, 1, ddmemSize, appFile);
500
+//	fclose (appFile);
501
+//
502
+//	free (appFileData);
503
+//	free (dldiFileData);
504
+//
505
+//	printf ("Patched successfully\n");
506
+//
507
+//	return EXIT_SUCCESS;
508
+//}
509
+
510
+//  Source File: mpcf.dldi
511
+//         Time: 6/14/2010 9:38 PM
512
+// Orig. Offset: 0 / 0x00000000
513
+//       Length: 1876 / 0x00000754 (bytes)
514
+data_t mpcf_dldi[1876] =
515
+{
516
+    0xED, 0xA5, 0x8D, 0xBF, 0x20, 0x43, 0x68, 0x69, 0x73, 0x68, 0x6D, 0x00, 0x01, 0x0B, 0x0C, 0x00,
517
+    0x47, 0x42, 0x41, 0x20, 0x4D, 0x6F, 0x76, 0x69, 0x65, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72,
518
+    0x20, 0x28, 0x43, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x20, 0x46, 0x6C, 0x61, 0x73, 0x68, 0x29,
519
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
520
+    0x00, 0x00, 0x80, 0xBF, 0x54, 0x07, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF,
521
+    0x50, 0x07, 0x80, 0xBF, 0x50, 0x07, 0x80, 0xBF, 0x54, 0x07, 0x80, 0xBF, 0x70, 0x07, 0x80, 0xBF,
522
+    0x4D, 0x50, 0x43, 0x46, 0x13, 0x00, 0x00, 0x00, 0x3C, 0x01, 0x80, 0xBF, 0x98, 0x01, 0x80, 0xBF,
523
+    0x78, 0x02, 0x80, 0xBF, 0xC8, 0x04, 0x80, 0xBF, 0xC8, 0x01, 0x80, 0xBF, 0x10, 0x07, 0x80, 0xBF,
524
+    0x0D, 0xC0, 0xA0, 0xE1, 0xF8, 0xDF, 0x2D, 0xE9, 0x04, 0xB0, 0x4C, 0xE2, 0x28, 0xD0, 0x4B, 0xE2,
525
+    0xF0, 0x6F, 0x9D, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1, 0x10, 0x40, 0x2D, 0xE9, 0x2C, 0x40, 0x9F, 0xE5,
526
+    0x00, 0x30, 0xD4, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x24, 0x20, 0x9F, 0xE5, 0x05, 0x00, 0x00, 0x1A,
527
+    0x00, 0x00, 0x52, 0xE3, 0x1C, 0x00, 0x9F, 0xE5, 0x0F, 0xE0, 0xA0, 0x11, 0x12, 0xFF, 0x2F, 0x11,
528
+    0x01, 0x30, 0xA0, 0xE3, 0x00, 0x30, 0xC4, 0xE5, 0x10, 0x40, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
529
+    0x54, 0x07, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x48, 0x07, 0x80, 0xBF, 0x04, 0xE0, 0x2D, 0xE5,
530
+    0x40, 0x30, 0x9F, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x04, 0xD0, 0x4D, 0xE2, 0x38, 0x00, 0x9F, 0xE5,
531
+    0x38, 0x10, 0x9F, 0xE5, 0x0F, 0xE0, 0xA0, 0x11, 0x13, 0xFF, 0x2F, 0x11, 0x30, 0x00, 0x9F, 0xE5,
532
+    0x00, 0x30, 0x90, 0xE5, 0x00, 0x00, 0x53, 0xE3, 0x28, 0x30, 0x9F, 0xE5, 0x02, 0x00, 0x00, 0x0A,
533
+    0x00, 0x00, 0x53, 0xE3, 0x0F, 0xE0, 0xA0, 0x11, 0x13, 0xFF, 0x2F, 0x11, 0x04, 0xD0, 0x8D, 0xE2,
534
+    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x48, 0x07, 0x80, 0xBF,
535
+    0x58, 0x07, 0x80, 0xBF, 0x4C, 0x07, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x09, 0x14, 0xA0, 0xE3,
536
+    0x06, 0x18, 0x81, 0xE2, 0xB0, 0x30, 0xD1, 0xE1, 0x03, 0x30, 0xE0, 0xE1, 0x03, 0x38, 0xA0, 0xE1,
537
+    0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0xB0, 0x30, 0xC1, 0xE1, 0xB0, 0x20, 0xD1, 0xE1,
538
+    0x04, 0xE0, 0x2D, 0xE5, 0xAA, 0xCC, 0xA0, 0xE3, 0x55, 0xEC, 0xE0, 0xE3, 0x03, 0x00, 0x52, 0xE1,
539
+    0xAA, 0xE0, 0x4E, 0xE2, 0x55, 0xC0, 0x8C, 0xE2, 0x00, 0x00, 0xA0, 0xE3, 0x03, 0x00, 0x00, 0x1A,
540
+    0xB0, 0xE0, 0xC1, 0xE1, 0xB0, 0x30, 0xD1, 0xE1, 0x0C, 0x00, 0x53, 0xE0, 0x01, 0x00, 0xA0, 0x13,
541
+    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x26, 0x35, 0xA0, 0xE3, 0x03, 0x37, 0x83, 0xE2,
542
+    0x50, 0x20, 0xA0, 0xE3, 0xB0, 0x20, 0xC3, 0xE1, 0xB0, 0x00, 0xD3, 0xE1, 0x00, 0x08, 0xA0, 0xE1,
543
+    0x20, 0x08, 0xA0, 0xE1, 0xFF, 0x00, 0x00, 0xE2, 0x50, 0x00, 0x50, 0xE3, 0x00, 0x00, 0xA0, 0x13,
544
+    0x01, 0x00, 0xA0, 0x03, 0x1E, 0xFF, 0x2F, 0xE1, 0x09, 0x34, 0xA0, 0xE3, 0x0E, 0x38, 0x83, 0xE2,
545
+    0xB0, 0x20, 0xD3, 0xE1, 0x80, 0x00, 0x12, 0xE3, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0x00, 0xA0, 0xE3,
546
+    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x50, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
547
+    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
548
+    0x80, 0x00, 0x12, 0xE3, 0x01, 0x00, 0x80, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
549
+    0x26, 0x15, 0xA0, 0xE3, 0x03, 0x17, 0x81, 0xE2, 0xB0, 0x30, 0xD1, 0xE1, 0x50, 0x30, 0x13, 0xE2,
550
+    0x01, 0x00, 0xA0, 0x13, 0x1E, 0xFF, 0x2F, 0x11, 0x26, 0x27, 0xA0, 0xE3, 0x96, 0x2C, 0x82, 0xE2,
551
+    0x03, 0x00, 0xA0, 0xE1, 0x80, 0x20, 0x82, 0xE2, 0x01, 0x00, 0x00, 0xEA, 0x02, 0x00, 0x50, 0xE1,
552
+    0x0A, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD1, 0xE1, 0x50, 0x30, 0x13, 0xE2, 0x01, 0x00, 0x80, 0xE2,
553
+    0xF9, 0xFF, 0xFF, 0x0A, 0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2,
554
+    0x03, 0x00, 0x50, 0xE1, 0x00, 0x00, 0xA0, 0xC3, 0x01, 0x00, 0xA0, 0xD3, 0x1E, 0xFF, 0x2F, 0xE1,
555
+    0x03, 0x00, 0xA0, 0xE1, 0x1E, 0xFF, 0x2F, 0xE1, 0xF0, 0x4B, 0x2D, 0xE9, 0x09, 0x34, 0xA0, 0xE3,
556
+    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0xC0, 0xD3, 0xE1, 0x80, 0x00, 0x1C, 0xE3, 0x10, 0xD0, 0x4D, 0xE2,
557
+    0x01, 0x90, 0xA0, 0xE1, 0x02, 0xE0, 0xA0, 0xE1, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0xC0, 0xA0, 0xE3,
558
+    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x5C, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
559
+    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
560
+    0x80, 0x00, 0x12, 0xE3, 0x01, 0xC0, 0x8C, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
561
+    0x26, 0xC5, 0xA0, 0xE3, 0x03, 0xC7, 0x8C, 0xE2, 0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x30, 0x13, 0xE2,
562
+    0x26, 0x17, 0xA0, 0x03, 0x96, 0x1C, 0x81, 0x02, 0x03, 0x20, 0xA0, 0x01, 0x80, 0x10, 0x81, 0x02,
563
+    0x02, 0x00, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1, 0x67, 0x00, 0x00, 0x0A,
564
+    0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x00, 0x13, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF9, 0xFF, 0xFF, 0x0A,
565
+    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
566
+    0x5E, 0x00, 0x00, 0xCA, 0xFF, 0x00, 0x59, 0xE3, 0x00, 0x30, 0xA0, 0x83, 0x0C, 0x30, 0x8D, 0x85,
567
+    0x5E, 0x00, 0x00, 0x9A, 0x00, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2,
568
+    0x09, 0x54, 0xA0, 0xE3, 0x04, 0x30, 0x8D, 0xE5, 0x0C, 0x30, 0x9D, 0xE5, 0x20, 0x4C, 0xA0, 0xE1,
569
+    0x0E, 0xB0, 0xA0, 0xE1, 0x05, 0x60, 0xA0, 0xE1, 0x20, 0x74, 0xA0, 0xE1, 0x05, 0x20, 0xA0, 0xE1,
570
+    0x20, 0x18, 0xA0, 0xE1, 0x05, 0xC0, 0xA0, 0xE1, 0x05, 0x00, 0xA0, 0xE1, 0x05, 0xE0, 0xA0, 0xE1,
571
+    0x01, 0x57, 0x85, 0xE2, 0xB0, 0x30, 0xC5, 0xE1, 0x04, 0x30, 0x9D, 0xE5, 0x0F, 0x40, 0x04, 0xE2,
572
+    0x06, 0x68, 0x86, 0xE2, 0xB0, 0x30, 0xC6, 0xE1, 0xFF, 0x70, 0x07, 0xE2, 0x02, 0x27, 0x82, 0xE2,
573
+    0xFF, 0x10, 0x01, 0xE2, 0x0A, 0x08, 0x80, 0xE2, 0xE0, 0x40, 0x84, 0xE3, 0x03, 0xC7, 0x8C, 0xE2,
574
+    0x0E, 0xE8, 0x8E, 0xE2, 0x20, 0x30, 0xA0, 0xE3, 0xB0, 0x70, 0xC2, 0xE1, 0xB0, 0x10, 0xC0, 0xE1,
575
+    0xB0, 0x40, 0xCC, 0xE1, 0xB0, 0x30, 0xCE, 0xE1, 0x0B, 0x80, 0xA0, 0xE1, 0x01, 0x90, 0x59, 0xE2,
576
+    0x3E, 0x00, 0x00, 0x3A, 0x26, 0x25, 0xA0, 0xE3, 0x03, 0x27, 0x82, 0xE2, 0xB0, 0x30, 0xD2, 0xE1,
577
+    0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3,
578
+    0x26, 0x17, 0xA0, 0x13, 0x96, 0x1C, 0x81, 0x12, 0x02, 0x00, 0xA0, 0x11, 0x80, 0x10, 0x81, 0x12,
579
+    0x00, 0x20, 0xA0, 0x13, 0x02, 0x00, 0x00, 0x1A, 0x0D, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1,
580
+    0x26, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD0, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
581
+    0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF6, 0xFF, 0xFF, 0x1A,
582
+    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
583
+    0x1A, 0x00, 0x00, 0xCA, 0x01, 0x00, 0x18, 0xE3, 0x0B, 0x10, 0xA0, 0x01, 0xFF, 0x20, 0xA0, 0x03,
584
+    0x09, 0x04, 0xA0, 0x03, 0x0E, 0x00, 0x00, 0x0A, 0x08, 0x10, 0xA0, 0xE1, 0xFF, 0x00, 0xA0, 0xE3,
585
+    0x09, 0xC4, 0xA0, 0xE3, 0xB0, 0x30, 0xDC, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
586
+    0x01, 0x00, 0x40, 0xE2, 0x43, 0x24, 0xA0, 0xE1, 0x01, 0x00, 0x70, 0xE3, 0x01, 0x20, 0xC1, 0xE5,
587
+    0x00, 0x30, 0xC1, 0xE5, 0x02, 0x10, 0x81, 0xE2, 0xF5, 0xFF, 0xFF, 0x1A, 0x02, 0x8C, 0x88, 0xE2,
588
+    0xCD, 0xFF, 0xFF, 0xEA, 0x01, 0x20, 0x42, 0xE2, 0xB0, 0x30, 0xD0, 0xE1, 0x01, 0x00, 0x72, 0xE3,
589
+    0xB2, 0x30, 0xC1, 0xE0, 0xFA, 0xFF, 0xFF, 0x1A, 0x02, 0xBC, 0x8B, 0xE2, 0xC6, 0xFF, 0xFF, 0xEA,
590
+    0x00, 0x00, 0xA0, 0xE3, 0x10, 0xD0, 0x8D, 0xE2, 0xF0, 0x4B, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
591
+    0x09, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0x0C, 0x30, 0x8D, 0xE5, 0x9C, 0xFF, 0xFF, 0xEA,
592
+    0x01, 0x00, 0xA0, 0xE3, 0xF6, 0xFF, 0xFF, 0xEA, 0xF0, 0x4B, 0x2D, 0xE9, 0x09, 0x34, 0xA0, 0xE3,
593
+    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0xC0, 0xD3, 0xE1, 0x80, 0x00, 0x1C, 0xE3, 0x10, 0xD0, 0x4D, 0xE2,
594
+    0x01, 0x90, 0xA0, 0xE1, 0x02, 0xE0, 0xA0, 0xE1, 0x0C, 0x00, 0x00, 0x0A, 0x00, 0xC0, 0xA0, 0xE3,
595
+    0x01, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x5C, 0xE1, 0x08, 0x00, 0x00, 0x0A, 0x09, 0x34, 0xA0, 0xE3,
596
+    0x0E, 0x38, 0x83, 0xE2, 0xB0, 0x20, 0xD3, 0xE1, 0x26, 0x17, 0xA0, 0xE3, 0x96, 0x1C, 0x81, 0xE2,
597
+    0x80, 0x00, 0x12, 0xE3, 0x01, 0xC0, 0x8C, 0xE2, 0x80, 0x10, 0x81, 0xE2, 0xF4, 0xFF, 0xFF, 0x1A,
598
+    0x26, 0xC5, 0xA0, 0xE3, 0x03, 0xC7, 0x8C, 0xE2, 0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x30, 0x13, 0xE2,
599
+    0x26, 0x17, 0xA0, 0x03, 0x96, 0x1C, 0x81, 0x02, 0x03, 0x20, 0xA0, 0x01, 0x80, 0x10, 0x81, 0x02,
600
+    0x02, 0x00, 0x00, 0x0A, 0x0A, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1, 0x65, 0x00, 0x00, 0x0A,
601
+    0xB0, 0x30, 0xDC, 0xE1, 0x50, 0x00, 0x13, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF9, 0xFF, 0xFF, 0x0A,
602
+    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
603
+    0x5C, 0x00, 0x00, 0xCA, 0xFF, 0x00, 0x59, 0xE3, 0x00, 0x30, 0xA0, 0x83, 0x0C, 0x30, 0x8D, 0x85,
604
+    0x5C, 0x00, 0x00, 0x9A, 0x00, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2,
605
+    0x09, 0x54, 0xA0, 0xE3, 0x04, 0x30, 0x8D, 0xE5, 0x0C, 0x30, 0x9D, 0xE5, 0x20, 0x4C, 0xA0, 0xE1,
606
+    0x0E, 0xB0, 0xA0, 0xE1, 0x05, 0x60, 0xA0, 0xE1, 0x20, 0x74, 0xA0, 0xE1, 0x05, 0x20, 0xA0, 0xE1,
607
+    0x20, 0x18, 0xA0, 0xE1, 0x05, 0xC0, 0xA0, 0xE1, 0x05, 0x00, 0xA0, 0xE1, 0x05, 0xE0, 0xA0, 0xE1,
608
+    0x01, 0x57, 0x85, 0xE2, 0xB0, 0x30, 0xC5, 0xE1, 0x04, 0x30, 0x9D, 0xE5, 0x0F, 0x40, 0x04, 0xE2,
609
+    0x06, 0x68, 0x86, 0xE2, 0xB0, 0x30, 0xC6, 0xE1, 0xFF, 0x70, 0x07, 0xE2, 0x02, 0x27, 0x82, 0xE2,
610
+    0xFF, 0x10, 0x01, 0xE2, 0x0A, 0x08, 0x80, 0xE2, 0xE0, 0x40, 0x84, 0xE3, 0x03, 0xC7, 0x8C, 0xE2,
611
+    0x0E, 0xE8, 0x8E, 0xE2, 0x30, 0x30, 0xA0, 0xE3, 0xB0, 0x70, 0xC2, 0xE1, 0xB0, 0x10, 0xC0, 0xE1,
612
+    0xB0, 0x40, 0xCC, 0xE1, 0xB0, 0x30, 0xCE, 0xE1, 0x0B, 0x80, 0xA0, 0xE1, 0x01, 0x90, 0x59, 0xE2,
613
+    0x3C, 0x00, 0x00, 0x3A, 0x26, 0x25, 0xA0, 0xE3, 0x03, 0x27, 0x82, 0xE2, 0xB0, 0x30, 0xD2, 0xE1,
614
+    0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1, 0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3,
615
+    0x26, 0x17, 0xA0, 0x13, 0x96, 0x1C, 0x81, 0x12, 0x02, 0x00, 0xA0, 0x11, 0x80, 0x10, 0x81, 0x12,
616
+    0x00, 0x20, 0xA0, 0x13, 0x02, 0x00, 0x00, 0x1A, 0x0D, 0x00, 0x00, 0xEA, 0x01, 0x00, 0x52, 0xE1,
617
+    0x24, 0x00, 0x00, 0x0A, 0xB0, 0x30, 0xD0, 0xE1, 0x03, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
618
+    0xFF, 0x30, 0x03, 0xE2, 0x58, 0x00, 0x53, 0xE3, 0x01, 0x20, 0x82, 0xE2, 0xF6, 0xFF, 0xFF, 0x1A,
619
+    0x26, 0x37, 0xA0, 0xE3, 0x96, 0x3C, 0x83, 0xE2, 0x7F, 0x30, 0x83, 0xE2, 0x03, 0x00, 0x52, 0xE1,
620
+    0x18, 0x00, 0x00, 0xCA, 0x01, 0x00, 0x18, 0xE3, 0x0B, 0x10, 0xA0, 0x01, 0xFF, 0x20, 0xA0, 0x03,
621
+    0x09, 0x04, 0xA0, 0x03, 0x0C, 0x00, 0x00, 0x0A, 0x08, 0x10, 0xA0, 0xE1, 0xFF, 0x00, 0xA0, 0xE3,
622
+    0x09, 0xC4, 0xA0, 0xE3, 0x00, 0x30, 0xD1, 0xE5, 0x01, 0x20, 0xD1, 0xE5, 0x01, 0x00, 0x40, 0xE2,
623
+    0x02, 0x34, 0x83, 0xE1, 0x01, 0x00, 0x70, 0xE3, 0xB0, 0x30, 0xCC, 0xE1, 0x02, 0x10, 0x81, 0xE2,
624
+    0xF7, 0xFF, 0xFF, 0x1A, 0x02, 0x8C, 0x88, 0xE2, 0xCF, 0xFF, 0xFF, 0xEA, 0x01, 0x20, 0x42, 0xE2,
625
+    0xB2, 0x30, 0xD1, 0xE0, 0x01, 0x00, 0x72, 0xE3, 0xB0, 0x30, 0xC0, 0xE1, 0xFA, 0xFF, 0xFF, 0x1A,
626
+    0x02, 0xBC, 0x8B, 0xE2, 0xC8, 0xFF, 0xFF, 0xEA, 0x00, 0x00, 0xA0, 0xE3, 0x10, 0xD0, 0x8D, 0xE2,
627
+    0xF0, 0x4B, 0xBD, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1, 0x09, 0x38, 0xA0, 0xE1, 0x23, 0x38, 0xA0, 0xE1,
628
+    0x0C, 0x30, 0x8D, 0xE5, 0x9E, 0xFF, 0xFF, 0xEA, 0x01, 0x00, 0xA0, 0xE3, 0xF6, 0xFF, 0xFF, 0xEA,
629
+    0x04, 0xE0, 0x2D, 0xE5, 0x04, 0xD0, 0x4D, 0xE2, 0xAA, 0xFE, 0xFF, 0xEB, 0x04, 0xD0, 0x8D, 0xE2,
630
+    0x04, 0xE0, 0x9D, 0xE4, 0x1E, 0xFF, 0x2F, 0xE1, 0x0D, 0xC0, 0xA0, 0xE1, 0xF8, 0xDF, 0x2D, 0xE9,
631
+    0x04, 0xB0, 0x4C, 0xE2, 0x28, 0xD0, 0x4B, 0xE2, 0xF0, 0x6F, 0x9D, 0xE8, 0x1E, 0xFF, 0x2F, 0xE1,
632
+    0xDC, 0x00, 0x80, 0xBF, 0x98, 0x00, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
633
+    0x00, 0x00, 0x00, 0x00,
634
+}; //unsigned char mpcf_dldi[1876]
635
+
636
+bool tryPatch(void* data, size_t size)
637
+{
638
+	// Find the DSDI reserved space in the file
639
+	addr_t patchOffset = quickFind ((data_t*)data, dldiMagicString, size, sizeof(dldiMagicString)/sizeof(char));
640
+
641
+	//no DLDI section
642
+	if (patchOffset < 0)
643
+		return false;
644
+
645
+	data_t *pDH = mpcf_dldi;
646
+	data_t *pAH = (data_t*)data + patchOffset;
647
+
648
+	if (pDH[DO_driverSize] > pAH[DO_allocatedSpace]) {
649
+		printf ("Not enough space for patch. Available %d bytes, need %d bytes\n", ( 1 << pAH[DO_allocatedSpace]), ( 1 << pDH[DO_driverSize]) );
650
+		return false;
651
+	}
652
+
653
+	if(memcmp(&pAH[DO_friendlyName],"Default (No interface)",22))
654
+	{
655
+		printf("Would have been a candidate for auto-patch DLDI, but there was already a patch installed.");
656
+		return false;
657
+	}
658
+
659
+	//----should be able to patch OK-----
660
+
661
+	addr_t memOffset;			// Offset of DLDI after the file is loaded into memory
662
+	addr_t relocationOffset;	// Value added to all offsets within the patch to fix it properly
663
+	addr_t ddmemOffset;			// Original offset used in the DLDI file
664
+	addr_t ddmemStart;			// Start of range that offsets can be in the DLDI file
665
+	addr_t ddmemEnd;			// End of range that offsets can be in the DLDI file
666
+	addr_t ddmemSize;			// Size of range that offsets can be in the DLDI file
667
+
668
+	addr_t addrIter;
669
+
670
+
671
+	memOffset = readAddr (pAH, DO_text_start);
672
+	if (memOffset == 0) {
673
+			memOffset = readAddr (pAH, DO_startup) - DO_code;
674
+	}
675
+	ddmemOffset = readAddr (pDH, DO_text_start);
676
+	relocationOffset = memOffset - ddmemOffset;
677
+
678
+	printf ("AUTO-PATCHING DLDI to MPCF! Lucky you!\n\n");
679
+	printf ("Old driver:          %s\n", &pAH[DO_friendlyName]);
680
+	printf ("New driver:          %s\n", &pDH[DO_friendlyName]);
681
+	printf ("\n");
682
+	printf ("Position in file:    0x%08X\n", patchOffset);
683
+	printf ("Position in memory:  0x%08X\n", memOffset);
684
+	printf ("Patch base address:  0x%08X\n", ddmemOffset);
685
+	printf ("Relocation offset:   0x%08X\n", relocationOffset);
686
+	printf ("\n");
687
+
688
+	ddmemStart = readAddr (pDH, DO_text_start);
689
+	ddmemSize = (1 << pDH[DO_driverSize]);
690
+	ddmemEnd = ddmemStart + ddmemSize;
691
+
692
+	// Remember how much space is actually reserved
693
+	pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
694
+	// Copy the DLDI patch into the application
695
+	memcpy (pAH, pDH, sizeof(mpcf_dldi));
696
+
697
+	// Fix the section pointers in the header
698
+	writeAddr (pAH, DO_text_start, readAddr (pAH, DO_text_start) + relocationOffset);
699
+	writeAddr (pAH, DO_data_end, readAddr (pAH, DO_data_end) + relocationOffset);
700
+	writeAddr (pAH, DO_glue_start, readAddr (pAH, DO_glue_start) + relocationOffset);
701
+	writeAddr (pAH, DO_glue_end, readAddr (pAH, DO_glue_end) + relocationOffset);
702
+	writeAddr (pAH, DO_got_start, readAddr (pAH, DO_got_start) + relocationOffset);
703
+	writeAddr (pAH, DO_got_end, readAddr (pAH, DO_got_end) + relocationOffset);
704
+	writeAddr (pAH, DO_bss_start, readAddr (pAH, DO_bss_start) + relocationOffset);
705
+	writeAddr (pAH, DO_bss_end, readAddr (pAH, DO_bss_end) + relocationOffset);
706
+	// Fix the function pointers in the header
707
+	writeAddr (pAH, DO_startup, readAddr (pAH, DO_startup) + relocationOffset);
708
+	writeAddr (pAH, DO_isInserted, readAddr (pAH, DO_isInserted) + relocationOffset);
709
+	writeAddr (pAH, DO_readSectors, readAddr (pAH, DO_readSectors) + relocationOffset);
710
+	writeAddr (pAH, DO_writeSectors, readAddr (pAH, DO_writeSectors) + relocationOffset);
711
+	writeAddr (pAH, DO_clearStatus, readAddr (pAH, DO_clearStatus) + relocationOffset);
712
+	writeAddr (pAH, DO_shutdown, readAddr (pAH, DO_shutdown) + relocationOffset);
713
+
714
+	if (pDH[DO_fixSections] & FIX_ALL) {
715
+		// Search through and fix pointers within the data section of the file
716
+		for (addrIter = (readAddr(pDH, DO_text_start) - ddmemStart); addrIter < (readAddr(pDH, DO_data_end) - ddmemStart); addrIter++) {
717
+			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
718
+				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
719
+			}
720
+		}
721
+	}
722
+
723
+	if (pDH[DO_fixSections] & FIX_GLUE) {
724
+		// Search through and fix pointers within the glue section of the file
725
+		for (addrIter = (readAddr(pDH, DO_glue_start) - ddmemStart); addrIter < (readAddr(pDH, DO_glue_end) - ddmemStart); addrIter++) {
726
+			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
727
+				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
728
+			}
729
+		}
730
+	}
731
+
732
+	if (pDH[DO_fixSections] & FIX_GOT) {
733
+		// Search through and fix pointers within the Global Offset Table section of the file
734
+		for (addrIter = (readAddr(pDH, DO_got_start) - ddmemStart); addrIter < (readAddr(pDH, DO_got_end) - ddmemStart); addrIter++) {
735
+			if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
736
+				writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
737
+			}
738
+		}
739
+	}
740
+
741
+	if (pDH[DO_fixSections] & FIX_BSS) {
742
+		// Initialise the BSS to 0
743
+		memset (&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
744
+	}
745
+
746
+	return true;
747
+}
748
+
749
+} //namespace DLDI