| 1 | /* |
| 2 | * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | vmhosttx.cpp - text-only host interface implementation |
| 10 | Function |
| 11 | Provides a base class for the T3 VM Host Interface for implementing |
| 12 | text-only applications. |
| 13 | Notes |
| 14 | |
| 15 | Modified |
| 16 | 06/16/02 MJRoberts - Creation |
| 17 | */ |
| 18 | |
| 19 | #include "os.h" |
| 20 | #include "t3std.h" |
| 21 | #include "vmhash.h" |
| 22 | #include "vmhost.h" |
| 23 | #include "vmhosttx.h" |
| 24 | |
| 25 | /* ------------------------------------------------------------------------ */ |
| 26 | /* |
| 27 | * Hash table entry for a resource descriptor |
| 28 | */ |
| 29 | class CResEntry: public CVmHashEntryCI |
| 30 | { |
| 31 | public: |
| 32 | CResEntry(const char *resname, size_t resnamelen, int copy, |
| 33 | unsigned long ofs, unsigned long siz, int fileno) |
| 34 | : CVmHashEntryCI(resname, resnamelen, copy) |
| 35 | { |
| 36 | /* remember the file locator information */ |
| 37 | fileno_ = fileno; |
| 38 | ofs_ = ofs; |
| 39 | siz_ = siz; |
| 40 | link_ = 0; |
| 41 | } |
| 42 | |
| 43 | CResEntry(const char *resname, size_t resnamelen, int copy, |
| 44 | const char *fname, size_t fnamelen) |
| 45 | : CVmHashEntryCI(resname, resnamelen, copy) |
| 46 | { |
| 47 | /* save the local filename */ |
| 48 | link_ = lib_copy_str(fname, fnamelen); |
| 49 | |
| 50 | /* it's not in a resource file */ |
| 51 | fileno_ = 0; |
| 52 | ofs_ = 0; |
| 53 | siz_ = 0; |
| 54 | } |
| 55 | |
| 56 | ~CResEntry() |
| 57 | { |
| 58 | lib_free_str(link_); |
| 59 | } |
| 60 | |
| 61 | /* file number (this is an index in the hostifc's ext_ array) */ |
| 62 | int fileno_; |
| 63 | |
| 64 | /* byte offset of the start of the resource data in the file */ |
| 65 | unsigned long ofs_; |
| 66 | |
| 67 | /* byte size of the resource data */ |
| 68 | unsigned long siz_; |
| 69 | |
| 70 | /* local filename, for a resource link (rather than a stored resource) */ |
| 71 | char *link_; |
| 72 | }; |
| 73 | |
| 74 | /* ------------------------------------------------------------------------ */ |
| 75 | /* |
| 76 | * construction |
| 77 | */ |
| 78 | CVmHostIfcText::CVmHostIfcText() |
| 79 | { |
| 80 | /* create our hash table */ |
| 81 | restab_ = new CVmHashTable(128, new CVmHashFuncCI(), TRUE); |
| 82 | |
| 83 | /* allocate an initial set of external resource filename entries */ |
| 84 | ext_max_ = 10; |
| 85 | ext_ = (char **)t3malloc(ext_max_ * sizeof(ext_[0])); |
| 86 | |
| 87 | /* we use slot zero for the image filename, which we don't know yet */ |
| 88 | ext_cnt_ = 1; |
| 89 | ext_[0] = 0; |
| 90 | |
| 91 | /* no resource directory specified yet */ |
| 92 | res_dir_ = 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * deletion |
| 97 | */ |
| 98 | CVmHostIfcText::~CVmHostIfcText() |
| 99 | { |
| 100 | /* delete our hash table */ |
| 101 | delete restab_; |
| 102 | |
| 103 | /* delete our external filenames */ |
| 104 | for (size_t i = 0 ; i < ext_cnt_ ; ++i) |
| 105 | lib_free_str(ext_[i]); |
| 106 | |
| 107 | /* delete our array of external filename entries */ |
| 108 | t3free(ext_); |
| 109 | |
| 110 | /* delete our resource directory path */ |
| 111 | lib_free_str(res_dir_); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * set the image file name - we always use resource file slot zero to |
| 116 | * store the image file |
| 117 | */ |
| 118 | void CVmHostIfcText::set_image_name(const char *fname) |
| 119 | { |
| 120 | /* free any old name string */ |
| 121 | lib_free_str(ext_[0]); |
| 122 | |
| 123 | /* remember the new name */ |
| 124 | ext_[0] = lib_copy_str(fname); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * set the resource directory |
| 129 | */ |
| 130 | void CVmHostIfcText::set_res_dir(const char *dir) |
| 131 | { |
| 132 | /* forget any previous setting, and remember the new path */ |
| 133 | lib_free_str(res_dir_); |
| 134 | res_dir_ = lib_copy_str(dir); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * add a resource file |
| 139 | */ |
| 140 | int CVmHostIfcText::add_resfile(const char *fname) |
| 141 | { |
| 142 | /* expand the resource file list if necessary */ |
| 143 | if (ext_cnt_ == ext_max_) |
| 144 | { |
| 145 | /* bump up the maximum a bit */ |
| 146 | ext_max_ += 10; |
| 147 | |
| 148 | /* reallocate the entry pointer array */ |
| 149 | ext_ = (char **)t3realloc(ext_, ext_max_ * sizeof(ext_[0])); |
| 150 | } |
| 151 | |
| 152 | /* store the new entry */ |
| 153 | ext_[ext_cnt_++] = lib_copy_str(fname); |
| 154 | |
| 155 | /* |
| 156 | * return the new entry's file number (we've already bumped the index, |
| 157 | * so it's the current count minus one) |
| 158 | */ |
| 159 | return ext_cnt_ - 1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * add a resource |
| 164 | */ |
| 165 | void CVmHostIfcText::add_resource(unsigned long ofs, unsigned long siz, |
| 166 | const char *resname, size_t resnamelen, |
| 167 | int fileno) |
| 168 | { |
| 169 | CResEntry *entry; |
| 170 | |
| 171 | /* create a new entry desribing the resource */ |
| 172 | entry = new CResEntry(resname, resnamelen, TRUE, ofs, siz, fileno); |
| 173 | |
| 174 | /* add it to the table */ |
| 175 | restab_->add(entry); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * add a resource |
| 180 | */ |
| 181 | void CVmHostIfcText::add_resource(const char *fname, size_t fnamelen, |
| 182 | const char *resname, size_t resnamelen) |
| 183 | { |
| 184 | /* create a new entry desribing the resource */ |
| 185 | CResEntry *entry = new CResEntry( |
| 186 | resname, resnamelen, TRUE, fname, fnamelen); |
| 187 | |
| 188 | /* add it to the table */ |
| 189 | restab_->add(entry); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * find a resource |
| 194 | */ |
| 195 | osfildef *CVmHostIfcText::find_resource(const char *resname, |
| 196 | size_t resnamelen, |
| 197 | unsigned long *res_size) |
| 198 | { |
| 199 | osfildef *fp; |
| 200 | char buf[OSFNMAX]; |
| 201 | char *fname; |
| 202 | char fname_buf[OSFNMAX]; |
| 203 | char res_dir[OSFNMAX] = ""; |
| 204 | |
| 205 | /* |
| 206 | * get the resource directory - if there's an explicit resource path |
| 207 | * specified, use that, otherwise use the image file folder |
| 208 | */ |
| 209 | if (res_dir_ != 0) |
| 210 | lib_strcpy(res_dir, sizeof(res_dir), res_dir_); |
| 211 | else if (ext_[0] != 0) |
| 212 | os_get_path_name(res_dir, sizeof(res_dir), ext_[0]); |
| 213 | |
| 214 | /* try finding an entry in the resource map */ |
| 215 | CResEntry *entry = (CResEntry *)restab_->find(resname, resnamelen); |
| 216 | if (entry != 0) |
| 217 | { |
| 218 | /* found it - check the type */ |
| 219 | if (entry->link_ == 0) |
| 220 | { |
| 221 | /* it's a stored binary resource - load it */ |
| 222 | fp = osfoprb(ext_[entry->fileno_], OSFTBIN); |
| 223 | |
| 224 | /* if that succeeded, seek to the start of the resource */ |
| 225 | if (fp != 0) |
| 226 | osfseek(fp, entry->ofs_, OSFSK_SET); |
| 227 | |
| 228 | /* tell the caller the size of the resource */ |
| 229 | *res_size = entry->siz_; |
| 230 | |
| 231 | /* return the file handle */ |
| 232 | return fp; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | /* it's a link to a local file */ |
| 237 | fname = entry->link_; |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | /* |
| 243 | * There's no entry in the resource map, so convert the resource |
| 244 | * name from the URL notation to local file system conventions, and |
| 245 | * look for a file with the given name. This is allowed only if |
| 246 | * the file safety level setting is 3 (read only local directory |
| 247 | * access) or lower. |
| 248 | */ |
| 249 | if (get_io_safety_read() > 3) |
| 250 | return 0; |
| 251 | |
| 252 | /* |
| 253 | * Make a null-terminated copy of the resource name, limiting the |
| 254 | * copy to our buffer size. |
| 255 | */ |
| 256 | if (resnamelen > sizeof(buf) - 1) |
| 257 | resnamelen = sizeof(buf) - 1; |
| 258 | memcpy(buf, resname, resnamelen); |
| 259 | buf[resnamelen] = '\0'; |
| 260 | |
| 261 | /* convert the resource name to a URL */ |
| 262 | os_cvt_url_dir(fname_buf, sizeof(fname_buf), buf); |
| 263 | fname = fname_buf; |
| 264 | |
| 265 | /* if that yields an absolute path, it's an error */ |
| 266 | if (os_is_file_absolute(fname)) |
| 267 | return 0; |
| 268 | |
| 269 | /* |
| 270 | * If it's not in the resource file folder, it's also an error - we |
| 271 | * don't allow paths to parent folders via "..", for example. If |
| 272 | * we don't have an resource file folder to compare it to, fail, |
| 273 | * since we can't properly sandbox it. Resource files are always |
| 274 | * sandboxed to the resource directory, even if the file safety |
| 275 | * settings are less restrictive. |
| 276 | */ |
| 277 | if (res_dir[0] == 0 |
| 278 | || !os_is_file_in_dir(fname, res_dir, TRUE, FALSE)) |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * If we get this far, it's because we have a local file name in |
| 284 | * 'fname' that we need to look up. |
| 285 | */ |
| 286 | |
| 287 | /* check the path for relativity */ |
| 288 | if (os_is_file_absolute(fname)) |
| 289 | { |
| 290 | /* it's already an absolute, fully-qualified path - use it as-is */ |
| 291 | lib_strcpy(buf, sizeof(buf), fname); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | /* |
| 296 | * it's a relative path - make sure we have a resource directory |
| 297 | * for it to be relative to |
| 298 | */ |
| 299 | if (res_dir[0] == 0) |
| 300 | return 0; |
| 301 | |
| 302 | /* |
| 303 | * build the full path name by combining the image file path with |
| 304 | * the relative path we got from the resource name URL, as |
| 305 | * converted local file system conventions |
| 306 | */ |
| 307 | os_build_full_path(buf, sizeof(buf), res_dir, fname); |
| 308 | } |
| 309 | |
| 310 | /* try opening the file */ |
| 311 | fp = osfoprb(buf, OSFTBIN); |
| 312 | |
| 313 | /* return failure if we couldn't find the file */ |
| 314 | if (fp == 0) |
| 315 | return 0; |
| 316 | |
| 317 | /* |
| 318 | * the entire file is the resource data, so figure out how big the file |
| 319 | * is, and tell the caller that the resource size is the file size |
| 320 | */ |
| 321 | osfseek(fp, 0, OSFSK_END); |
| 322 | *res_size = osfpos(fp); |
| 323 | |
| 324 | /* |
| 325 | * seek back to the start of the resource data (which is simply the |
| 326 | * start of the file, since the entire file is the resource data) |
| 327 | */ |
| 328 | osfseek(fp, 0, OSFSK_SET); |
| 329 | |
| 330 | /* return the file handle */ |
| 331 | return fp; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * determine if a resource exists |
| 336 | */ |
| 337 | int CVmHostIfcText::resfile_exists(const char *resname, size_t resnamelen) |
| 338 | { |
| 339 | /* try opening the resource file */ |
| 340 | unsigned long res_size; |
| 341 | osfildef *fp = find_resource(resname, resnamelen, &res_size); |
| 342 | |
| 343 | /* check to see if we successfully opened the resource */ |
| 344 | if (fp != 0) |
| 345 | { |
| 346 | /* found it - close the file and return success */ |
| 347 | osfcls(fp); |
| 348 | return TRUE; |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | /* couldn't find it - indicate failure */ |
| 353 | return FALSE; |
| 354 | } |
| 355 | } |
| 356 |