static void command_verify_image(void) { const UINT8 *verify_data; size_t verify_data_size; size_t offset, offset_start, offset_end; const char *filename; mess_image *image; FILE *f; UINT8 c; char filename_buf[512]; verify_data = (const UINT8 *) current_command->u.verify_args.verify_data; verify_data_size = current_command->u.verify_args.verify_data_size; image = image_from_devtype_and_index(current_command->u.verify_args.device_type, current_command->u.verify_args.device_slot); filename = image_filename(image); if (!filename) { state = STATE_ABORTED; report_message(MSG_FAILURE, "Failed verification: Device Not Loaded"); return; } /* very dirty hack - we unload the image because we cannot access it * because the file is locked */ strcpy(filename_buf, filename); image_unload(image); filename = filename_buf; f = fopen(filename, "r"); if (!f) { state = STATE_ABORTED; report_message(MSG_FAILURE, "Failed verification: Cannot open image to verify"); return; } offset_start = 0; offset_end = verify_data_size - 1; for (offset = offset_start; offset <= offset_end; offset++) { fseek(f, offset, SEEK_SET); c = (UINT8) fgetc(f); if (c != verify_data[offset]) { state = STATE_ABORTED; report_message(MSG_FAILURE, "Failed verification step (%s; 0x%x-0x%x)", filename, offset_start, offset_end); break; } } fclose(f); }
static int image_load_internal(mess_image *image, const char *path, int is_create, int create_format, option_resolution *create_args) { image_error_t err; const char *software_path; char *software_path_list = NULL; const void *buffer; const game_driver *gamedrv; UINT32 open_plan[4]; int i; /* sanity checks */ assert_always(image, "image_load(): image is NULL"); assert_always(path, "image_load(): path is NULL"); /* we are now loading */ image->is_loading = 1; /* first unload the image */ image_unload(image); /* record the filename */ image->err = set_image_filename(image, path, NULL); if (image->err) goto done; /* tell the OSD layer that this is changing */ osd_image_load_status_changed(image, 0); /* do we need to reset the CPU? */ if ((timer_get_time() > 0) && image->dev->reset_on_load) mame_schedule_soft_reset(Machine); /* determine open plan */ determine_open_plan(image, is_create, open_plan); /* attempt to open the file in various ways */ for (i = 0; !image->file && open_plan[i]; i++) { software_path = software_path_list; do { gamedrv = Machine->gamedrv; while(!is_loaded(image) && gamedrv) { /* open the file */ image->err = load_image_by_path(image, software_path, gamedrv, open_plan[i], path); if (image->err && (image->err != IMAGE_ERROR_FILENOTFOUND)) goto done; /* move on to the next driver */ gamedrv = mess_next_compatible_driver(gamedrv); } /* move on to the next entry in the software path; if we can */ if (software_path) software_path += strlen(software_path) + 1; } while(!is_loaded(image) && software_path && *software_path); } /* did we fail to find the file? */ if (!is_loaded(image)) { image->err = IMAGE_ERROR_FILENOTFOUND; goto done; } /* if applicable, call device verify */ if (image->dev->imgverify && !image_has_been_created(image)) { /* access the memory */ buffer = image_ptr(image); if (!buffer) { image->err = IMAGE_ERROR_OUTOFMEMORY; goto done; } /* verify the file */ err = image->dev->imgverify(buffer, (size_t) image->length); if (err) { image->err = IMAGE_ERROR_INVALIDIMAGE; goto done; } } /* call device load or create */ if (image_has_been_created(image) && image->dev->create) { err = image->dev->create(image, create_format, create_args); if (err) { if (!image->err) image->err = IMAGE_ERROR_UNSPECIFIED; goto done; } } else if (image->dev->load) { /* using device load */ err = image->dev->load(image); if (err) { if (!image->err) image->err = IMAGE_ERROR_UNSPECIFIED; goto done; } } /* success! */ done: if (software_path_list) free(software_path_list); if (image->err) image_clear(image); image->is_loading = 1; return image->err ? INIT_FAIL : INIT_PASS; }
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; }