Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/general.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include "emuware/emuware.h"
19
20
#include <string.h>
21
#include <stdarg.h>
22
23
#include <sys/types.h>
24
#include <sys/stat.h>
25
26
#include <string>
27
#include <map>
28
29
#include "general.h"
30
31
#include "error.h"
32
33
#define _(X) X
34
35
#ifdef WIN32
36
#define PSS "\\"
37
#else
38
#define PSS "/"
39
#endif
40
41
static struct {
42
bool untrusted_fip_check;
43
} s_settings;
44
45
46
using namespace std;
47
48
static string BaseDirectory;
49
static string FileBase;
50
static string FileExt; /* Includes the . character, as in ".nes" */
51
static string FileBaseDirectory;
52
53
void MDFN_SetBaseDirectory(const std::string& dir)
54
{
55
BaseDirectory = string(dir);
56
}
57
58
std::string MDFN_GetBaseDirectory(void)
59
{
60
return BaseDirectory;
61
}
62
63
// Really dumb, maybe we should use boost?
64
static bool IsAbsolutePath(const char *path)
65
{
66
#if PSS_STYLE==4
67
if(path[0] == ':')
68
#elif PSS_STYLE==1
69
if(path[0] == '/')
70
#else
71
if(path[0] == '\\'
72
#if PSS_STYLE!=3
73
|| path[0] == '/'
74
#endif
75
)
76
#endif
77
{
78
return(true);
79
}
80
81
#if defined(WIN32) || defined(DOS)
82
if((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z'))
83
{
84
if(path[1] == ':')
85
{
86
return(true);
87
}
88
}
89
#endif
90
91
return(false);
92
}
93
94
static bool IsAbsolutePath(const std::string &path)
95
{
96
return(IsAbsolutePath(path.c_str()));
97
}
98
99
bool MDFN_IsFIROPSafe(const std::string &path)
100
{
101
//
102
// First, check for any 8-bit characters, and print a warning about portability.
103
//
104
for(size_t x = 0; x < path.size(); x++)
105
{
106
if(path[x] & 0x80)
107
{
108
printf(_("WARNING: Referenced path \"%s\" contains at least one 8-bit non-ASCII character; this may cause portability issues.\n"), path.c_str());
109
break;
110
}
111
}
112
113
// We could make this more OS-specific, but it shouldn't hurt to try to weed out usage of characters that are path
114
// separators in one OS but not in another, and we'd also run more of a risk of missing a special path separator case
115
// in some OS.
116
if(!s_settings.untrusted_fip_check)
117
return(true);
118
119
if(path.find('\0') != string::npos)
120
return(false);
121
122
if(path.find(':') != string::npos)
123
return(false);
124
125
if(path.find('\\') != string::npos)
126
return(false);
127
128
if(path.find('/') != string::npos)
129
return(false);
130
131
#if defined(DOS) || defined(WIN32)
132
//
133
// http://support.microsoft.com/kb/74496
134
//
135
{
136
static const char* dev_names[] =
137
{
138
"CON", "PRN", "AUX", "CLOCK$", "NUL", "COM1", "COM2", "COM3", "COM4", "LPT1", "LPT2", "LPT3", NULL
139
};
140
141
for(const char** ls = dev_names; *ls != NULL; ls++)
142
{
143
if(!strcasecmp(*ls, path.c_str()))
144
return(false);
145
}
146
}
147
#endif
148
149
return(true);
150
}
151
152
void MDFN_GetFilePathComponents(const std::string &file_path, std::string *dir_path_out, std::string *file_base_out, std::string *file_ext_out)
153
{
154
size_t final_ds; // in file_path
155
string file_name;
156
size_t fn_final_dot; // in local var file_name
157
// Temporary output:
158
string dir_path, file_base, file_ext;
159
160
#if PSS_STYLE==4
161
final_ds = file_path.find_last_of(':');
162
#elif PSS_STYLE==1
163
final_ds = file_path.find_last_of('/');
164
#else
165
final_ds = file_path.find_last_of('\\');
166
167
#if PSS_STYLE!=3
168
{
169
size_t alt_final_ds = file_path.find_last_of('/');
170
171
if(final_ds == string::npos || (alt_final_ds != string::npos && alt_final_ds > final_ds))
172
final_ds = alt_final_ds;
173
}
174
#endif
175
#endif
176
177
if(final_ds == string::npos)
178
{
179
dir_path = string(".");
180
file_name = file_path;
181
}
182
else
183
{
184
dir_path = file_path.substr(0, final_ds);
185
file_name = file_path.substr(final_ds + 1);
186
}
187
188
fn_final_dot = file_name.find_last_of('.');
189
190
if(fn_final_dot != string::npos)
191
{
192
file_base = file_name.substr(0, fn_final_dot);
193
file_ext = file_name.substr(fn_final_dot);
194
}
195
else
196
{
197
file_base = file_name;
198
file_ext = string("");
199
}
200
201
if(dir_path_out)
202
*dir_path_out = dir_path;
203
204
if(file_base_out)
205
*file_base_out = file_base;
206
207
if(file_ext_out)
208
*file_ext_out = file_ext;
209
}
210
211
std::string MDFN_EvalFIP(const std::string &dir_path, const std::string &rel_path, bool skip_safety_check)
212
{
213
if(!skip_safety_check && !MDFN_IsFIROPSafe(rel_path))
214
throw MDFN_Error(0, _("Referenced path \"%s\" is potentially unsafe. See \"filesys.untrusted_fip_check\" setting.\n"), rel_path.c_str());
215
216
if(IsAbsolutePath(rel_path.c_str()))
217
return(rel_path);
218
else
219
{
220
return(dir_path + std::string(PSS) + rel_path);
221
}
222
}
223
224
225
typedef std::map<char, std::string> FSMap;
226
227
static std::string EvalPathFS(const std::string &fstring, /*const (won't work because entry created if char doesn't exist) */ FSMap &fmap)
228
{
229
std::string ret = "";
230
const char *str = fstring.c_str();
231
bool in_spec = false;
232
233
while(*str)
234
{
235
int c = *str;
236
237
if(!in_spec && c == '%')
238
in_spec = true;
239
else if(in_spec == true)
240
{
241
if(c == '%')
242
ret = ret + std::string("%");
243
else
244
ret = ret + fmap[(char)c];
245
in_spec = false;
246
}
247
else
248
{
249
char ct[2];
250
ct[0] = c;
251
ct[1] = 0;
252
ret += std::string(ct);
253
}
254
255
str++;
256
}
257
258
return(ret);
259
}
260
261
#if 0
262
static void CreateMissingDirs(const char *path)
263
{
264
const char *s = path;
265
bool first_psep = true;
266
char last_char = 0;
267
const char char_test1 = '/', char_test2 = '/';
268
269
270
while(*s)
271
{
272
if(*s == char_test1 || *s == char_test2)
273
{
274
if(last_char != *s) //char_test1 && last_char != char_test2)
275
{
276
if(!first_psep)
277
{
278
char tmpbuf[(s - path) + 1];
279
tmpbuf[s - path] = 0;
280
strncpy(tmpbuf, path, s - path);
281
282
puts(tmpbuf);
283
//MDFN_mkdir(tmpbuf, S_IRWXU);
284
}
285
}
286
287
first_psep = false;
288
}
289
last_char = *s;
290
s++;
291
}
292
}
293
#endif
294
295
const char * GetFNComponent(const char *str)
296
{
297
const char *tp1;
298
299
#if PSS_STYLE==4
300
tp1=((char *)strrchr(str,':'));
301
#elif PSS_STYLE==1
302
tp1=((char *)strrchr(str,'/'));
303
#else
304
tp1=((char *)strrchr(str,'\\'));
305
#if PSS_STYLE!=3
306
{
307
const char *tp3;
308
tp3=((char *)strrchr(str,'/'));
309
if(tp1<tp3) tp1=tp3;
310
}
311
#endif
312
#endif
313
314
if(tp1)
315
return(tp1+1);
316
else
317
return(str);
318
}
319
320
void GetFileBase(const char *f)
321
{
322
const char *tp1,*tp3;
323
324
#if PSS_STYLE==4
325
tp1=((char *)strrchr(f,':'));
326
#elif PSS_STYLE==1
327
tp1=((char *)strrchr(f,'/'));
328
#else
329
tp1=((char *)strrchr(f,'\\'));
330
#if PSS_STYLE!=3
331
tp3=((char *)strrchr(f,'/'));
332
if(tp1<tp3) tp1=tp3;
333
#endif
334
#endif
335
if(!tp1)
336
{
337
tp1=f;
338
FileBaseDirectory = ".";
339
}
340
else
341
{
342
char* tmpfn = (char*)alloca(tp1 - f + 1);
343
344
memcpy(tmpfn,f,tp1-f);
345
tmpfn[tp1-f]=0;
346
FileBaseDirectory = string(tmpfn);
347
348
tp1++;
349
}
350
351
if(((tp3=strrchr(f,'.'))!=NULL) && (tp3>tp1))
352
{
353
char* tmpbase = (char*)alloca(tp3 - tp1 + 1);
354
355
memcpy(tmpbase,tp1,tp3-tp1);
356
tmpbase[tp3-tp1]=0;
357
FileBase = string(tmpbase);
358
FileExt = string(tp3);
359
}
360
else
361
{
362
FileBase = string(tp1);
363
FileExt = "";
364
}
365
}
366
367
368