| 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 "mednafen.h" |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdarg.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <string> |
| 26 | |
| 27 | #include "general.h" |
| 28 | #include "state.h" |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | static bool IsAbsolutePath(const char *path) |
| 33 | { |
| 34 | if (path[0] == '\\' || path[0] == '/') |
| 35 | return(TRUE); |
| 36 | |
| 37 | #if defined(WIN32) || defined(DOS) |
| 38 | if((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')) |
| 39 | { |
| 40 | if(path[1] == ':') |
| 41 | return(TRUE); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | return(FALSE); |
| 46 | } |
| 47 | |
| 48 | bool MDFN_IsFIROPSafe(const std::string &path) |
| 49 | { |
| 50 | // We could make this more OS-specific, but it shouldn't hurt to try to weed out usage of characters that are path |
| 51 | // separators in one OS but not in another, and we'd also run more of a risk of missing a special path separator case |
| 52 | // in some OS. |
| 53 | |
| 54 | if(!MDFN_GetSettingB("filesys.untrusted_fip_check")) |
| 55 | return(true); |
| 56 | |
| 57 | if(path.find('\0') != string::npos) |
| 58 | return(false); |
| 59 | |
| 60 | if(path.find(':') != string::npos) |
| 61 | return(false); |
| 62 | |
| 63 | if(path.find('\\') != string::npos) |
| 64 | return(false); |
| 65 | |
| 66 | if(path.find('/') != string::npos) |
| 67 | return(false); |
| 68 | |
| 69 | return(true); |
| 70 | } |
| 71 | |
| 72 | void MDFN_GetFilePathComponents(const std::string &file_path, |
| 73 | std::string *dir_path_out, std::string *file_base_out, |
| 74 | std::string *file_ext_out) |
| 75 | { |
| 76 | size_t final_ds; // in file_path |
| 77 | string file_name; |
| 78 | size_t fn_final_dot; // in local var file_name |
| 79 | string dir_path, file_base, file_ext; // Temporary output |
| 80 | |
| 81 | #ifdef _WIN32 |
| 82 | final_ds = file_path.find_last_of('\\'); |
| 83 | |
| 84 | size_t alt_final_ds = file_path.find_last_of('/'); |
| 85 | |
| 86 | if(final_ds == string::npos || (alt_final_ds != string::npos && alt_final_ds > final_ds)) |
| 87 | final_ds = alt_final_ds; |
| 88 | #else |
| 89 | final_ds = file_path.find_last_of('/'); |
| 90 | #endif |
| 91 | |
| 92 | if(final_ds == string::npos) |
| 93 | { |
| 94 | dir_path = string("."); |
| 95 | file_name = file_path; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | dir_path = file_path.substr(0, final_ds); |
| 100 | file_name = file_path.substr(final_ds + 1); |
| 101 | } |
| 102 | |
| 103 | fn_final_dot = file_name.find_last_of('.'); |
| 104 | |
| 105 | if(fn_final_dot != string::npos) |
| 106 | { |
| 107 | file_base = file_name.substr(0, fn_final_dot); |
| 108 | file_ext = file_name.substr(fn_final_dot); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | file_base = file_name; |
| 113 | file_ext = string(""); |
| 114 | } |
| 115 | |
| 116 | if(dir_path_out) |
| 117 | *dir_path_out = dir_path; |
| 118 | |
| 119 | if(file_base_out) |
| 120 | *file_base_out = file_base; |
| 121 | |
| 122 | if(file_ext_out) |
| 123 | *file_ext_out = file_ext; |
| 124 | } |
| 125 | |
| 126 | std::string MDFN_EvalFIP(const std::string &dir_path, const std::string &rel_path, bool skip_safety_check) |
| 127 | { |
| 128 | char slash; |
| 129 | #ifdef _WIN32 |
| 130 | slash = '\\'; |
| 131 | #else |
| 132 | slash = '/'; |
| 133 | #endif |
| 134 | |
| 135 | if(!skip_safety_check && !MDFN_IsFIROPSafe(rel_path)) |
| 136 | throw MDFN_Error(0, _("Referenced path \"%s\" is potentially unsafe. See \"filesys.untrusted_fip_check\" setting.\n"), rel_path.c_str()); |
| 137 | |
| 138 | if(IsAbsolutePath(rel_path.c_str())) |
| 139 | return(rel_path); |
| 140 | else |
| 141 | return(dir_path + slash + rel_path); |
| 142 | } |
| 143 | |
| 144 | const char * GetFNComponent(const char *str) |
| 145 | { |
| 146 | const char *tp1; |
| 147 | |
| 148 | #ifdef _WIN32 |
| 149 | tp1 = ((char *)strrchr(str,'\\')); |
| 150 | |
| 151 | const char *tp3; |
| 152 | |
| 153 | tp3 = ((char *)strrchr(str,'/')); |
| 154 | |
| 155 | if (tp1<tp3) |
| 156 | tp1 = tp3; |
| 157 | #else |
| 158 | tp1 = ((char *)strrchr(str,'/')); |
| 159 | #endif |
| 160 | |
| 161 | if (tp1) |
| 162 | return (tp1+1); |
| 163 | else |
| 164 | return (str); |
| 165 | } |
| 166 | |
| 167 | // Remove whitespace from beginning of string |
| 168 | void MDFN_ltrim(std::string &string) |
| 169 | { |
| 170 | size_t len = string.length(); |
| 171 | size_t di, si; |
| 172 | bool InWhitespace = TRUE; |
| 173 | |
| 174 | di = si = 0; |
| 175 | |
| 176 | while(si < len) |
| 177 | { |
| 178 | if(InWhitespace && (string[si] == ' ' || string[si] == '\r' || string[si] == '\n' || string[si] == '\t' || string[si] == 0x0b)) |
| 179 | { |
| 180 | |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | InWhitespace = FALSE; |
| 185 | string[di] = string[si]; |
| 186 | di++; |
| 187 | } |
| 188 | si++; |
| 189 | } |
| 190 | |
| 191 | string.resize(di); |
| 192 | } |
| 193 | |
| 194 | // Remove whitespace from end of string |
| 195 | void MDFN_rtrim(std::string &string) |
| 196 | { |
| 197 | size_t len = string.length(); |
| 198 | |
| 199 | if(len) |
| 200 | { |
| 201 | size_t x = len; |
| 202 | size_t new_len = len; |
| 203 | |
| 204 | do |
| 205 | { |
| 206 | x--; |
| 207 | |
| 208 | if(!(string[x] == ' ' || string[x] == '\r' || string[x] == '\n' || string[x] == '\t' || string[x] == 0x0b)) |
| 209 | break; |
| 210 | |
| 211 | new_len--; |
| 212 | } while(x); |
| 213 | |
| 214 | string.resize(new_len); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void MDFN_trim(std::string &string) |
| 220 | { |
| 221 | MDFN_rtrim(string); |
| 222 | MDFN_ltrim(string); |
| 223 | } |
| 224 |