static image_error_t set_image_filename(mess_image *image, const char *filename, const char *zippath) { image_error_t err = IMAGE_ERROR_SUCCESS; char *alloc_filename = NULL; char *new_name; char *new_dir; int pos; /* create the directory string */ new_dir = image_strdup(image, filename); for (pos = strlen(new_dir); (pos > 0); pos--) { if (strchr(":\\/", new_dir[pos - 1])) { new_dir[pos] = '\0'; break; } } /* do we have to concatenate the names? */ if (zippath) { alloc_filename = assemble_3_strings(filename, PATH_SEPARATOR, zippath); filename = alloc_filename; } /* copy the string */ new_name = image_strdup(image, filename); if (!new_name) { err = IMAGE_ERROR_OUTOFMEMORY; goto done; } /* set the new name and dir */ if (image->name) image_freeptr(image, image->name); if (image->dir) image_freeptr(image, image->dir); image->name = new_name; image->dir = new_dir; done: if (alloc_filename) free(alloc_filename); return err; }
static int read_hash_config(const char *sysname, mess_image *image) { hash_file *hashfile = NULL; const struct hash_info *info = NULL; hashfile = hashfile_open(sysname, FALSE, NULL); if (!hashfile) goto done; info = hashfile_lookup(hashfile, image->hash); if (!info) goto done; image->longname = image_strdup(image, info->longname); image->manufacturer = image_strdup(image, info->manufacturer); image->year = image_strdup(image, info->year); image->playable = image_strdup(image, info->playable); image->extrainfo = image_strdup(image, info->extrainfo); done: if (hashfile) hashfile_close(hashfile); return !hashfile || !info; }
const char *image_basename_noext(mess_image *img) { const char *s; char *ext; if (!img->basename_noext) { s = image_basename(img); if (s) { img->basename_noext = image_strdup(img, s); ext = strrchr(img->basename_noext, '.'); if (ext) *ext = '\0'; } } return img->basename_noext; }
static int image_checkhash(mess_image *image) { const game_driver *drv; const struct IODevice *dev; mame_file *file; char hash_string[HASH_BUF_SIZE]; int rc; /* this call should not be made when the image is not loaded */ assert(image->status & (IMAGE_STATUS_ISLOADING | IMAGE_STATUS_ISLOADED)); /* only calculate CRC if it hasn't been calculated, and the open_mode is read only */ if (!image->hash && !image->writeable && !image->created) { /* initialize key variables */ file = image_fp(image); dev = image_device(image); /* do not cause a linear read of 600 megs please */ /* TODO: use SHA/MD5 in the CHD header as the hash */ if (dev->type == IO_CDROM) return FALSE; if (!run_hash(file, dev->partialhash, hash_string, HASH_CRC | HASH_MD5 | HASH_SHA1)) return FALSE; image->hash = image_strdup(image, hash_string); if (!image->hash) return FALSE; /* now read the hash file */ drv = Machine->gamedrv; do { rc = read_hash_config(drv->name, image); drv = mess_next_compatible_driver(drv); } while(rc && drv); } return TRUE; }
const char *image_filedir(mess_image *img) { char *s; if (!img->dir) { img->dir = image_strdup(img, img->name); if (img->dir) { s = img->dir + strlen(img->dir); while(--s > img->dir) { if (strchr("\\/:", *s)) { *s = '\0'; if (osd_get_path_info(FILETYPE_IMAGE, 0, img->dir) == PATH_IS_DIRECTORY) break; } } } } return img->dir; }
static int image_load_internal(mess_image *img, const char *name, int is_create, int create_format, option_resolution *create_args) { const struct IODevice *dev; const char *s; char *newname; int err = INIT_PASS; mame_file *file = NULL; UINT8 *buffer = NULL; UINT64 size; unsigned int readable, writeable, creatable; /* unload if we are loaded */ if (img->status & IMAGE_STATUS_ISLOADED) image_unload(img); /* clear out the error */ image_clear_error(img); /* if we are attempting to "load" NULL, then exit at this point */ if (!name) return INIT_PASS; dev = image_device(img); assert(dev); img->status |= IMAGE_STATUS_ISLOADING; if (name && *name) { newname = image_strdup(img, name); if (!newname) { err = IMAGE_ERROR_OUTOFMEMORY; goto error; } } else newname = NULL; img->name = newname; img->dir = NULL; osd_image_load_status_changed(img, 0); /* do we need to reset the CPU? */ if ((timer_get_time() > 0) && dev->reset_on_load) machine_reset(); /* prepare to open the file */ img->created = 0; img->writeable = 0; file = NULL; if (dev->getdispositions) { dev->getdispositions(dev, image_index_in_device(img), &readable, &writeable, &creatable); } else { readable = dev->readable; writeable = dev->writeable; creatable = dev->creatable; } /* is this a ZIP file? */ s = strrchr(img->name, '.'); if (s && !mame_stricmp(s, ".ZIP")) { /* ZIP files are writeable */ writeable = 0; creatable = 0; } if (readable && !writeable) { file = image_fopen_custom(img, FILETYPE_IMAGE, OSD_FOPEN_READ); } else if (!readable && writeable) { file = image_fopen_custom(img, FILETYPE_IMAGE, OSD_FOPEN_WRITE); img->writeable = file ? 1 : 0; } else if (readable && writeable) { file = image_fopen_custom(img, FILETYPE_IMAGE, OSD_FOPEN_RW); img->writeable = file ? 1 : 0; if (!file) { file = image_fopen_custom(img, FILETYPE_IMAGE, OSD_FOPEN_READ); if (!file && creatable) { file = image_fopen_custom(img, FILETYPE_IMAGE, OSD_FOPEN_RW_CREATE); img->writeable = file ? 1 : 0; img->created = file ? 1 : 0; } } } /* did this attempt succeed? */ if (!file) { img->err = IMAGE_ERROR_FILENOTFOUND; goto error; } /* if applicable, call device verify */ if (dev->imgverify && !image_has_been_created(img)) { size = mame_fsize(file); buffer = malloc(size); if (!buffer) { img->err = IMAGE_ERROR_OUTOFMEMORY; goto error; } if (mame_fread(file, buffer, (UINT32) size) != size) { img->err = IMAGE_ERROR_INVALIDIMAGE; goto error; } err = dev->imgverify(buffer, size); if (err) { img->err = IMAGE_ERROR_INVALIDIMAGE; goto error; } mame_fseek(file, 0, SEEK_SET); free(buffer); buffer = NULL; } /* call device load or create */ if (image_has_been_created(img) && dev->create) { err = dev->create(img, file, create_format, create_args); if (err) { if (!img->err) img->err = IMAGE_ERROR_UNSPECIFIED; goto error; } } else if (dev->load) { /* using device load */ err = dev->load(img, file); if (err) { if (!img->err) img->err = IMAGE_ERROR_UNSPECIFIED; goto error; } } img->status &= ~IMAGE_STATUS_ISLOADING; img->status |= IMAGE_STATUS_ISLOADED; return INIT_PASS; error: if (file) mame_fclose(file); if (buffer) free(buffer); if (img) { img->fp = NULL; img->name = NULL; img->status &= ~IMAGE_STATUS_ISLOADING|IMAGE_STATUS_ISLOADED; } osd_image_load_status_changed(img, 0); return INIT_FAIL; }