static file_error create_core_file_from_zip(zip_file *zip, const zip_file_header *header, core_file *&file) { file_error filerr; zip_error ziperr; void *ptr; ptr = malloc(header->uncompressed_length); if (ptr == NULL) { filerr = FILERR_OUT_OF_MEMORY; goto done; } ziperr = zip_file_decompress(zip, ptr, header->uncompressed_length); if (ziperr != ZIPERR_NONE) { filerr = file_error_from_zip_error(ziperr); goto done; } filerr = core_fopen_ram_copy(ptr, header->uncompressed_length, OPEN_FLAG_READ, &file); if (filerr != FILERR_NONE) goto done; done: if (ptr != NULL) free(ptr); return filerr; }
file_error emu_file::load_zipped_file() { assert(m_file == NULL); assert(m_zipdata.count() == 0); assert(m_zipfile != NULL); // allocate some memory m_zipdata.resize(m_ziplength); // read the data into our buffer and return zip_error ziperr = zip_file_decompress(m_zipfile, m_zipdata, m_zipdata.count()); if (ziperr != ZIPERR_NONE) { m_zipdata.reset(); return FILERR_FAILURE; } // convert to RAM file file_error filerr = core_fopen_ram(m_zipdata, m_zipdata.count(), m_openflags, &m_file); if (filerr != FILERR_NONE) { m_zipdata.reset(); return FILERR_FAILURE; } // close out the ZIP file zip_file_close(m_zipfile); m_zipfile = NULL; return FILERR_NONE; }
file_error emu_file::load_zipped_file() { assert(m_file == nullptr); assert(m_zipdata.empty()); assert(m_zipfile != nullptr); // allocate some memory m_zipdata.resize(m_ziplength); // read the data into our buffer and return zip_error ziperr = zip_file_decompress(m_zipfile, &m_zipdata[0], m_zipdata.size()); if (ziperr != ZIPERR_NONE) { m_zipdata.clear(); return FILERR_FAILURE; } // convert to RAM file file_error filerr = util::core_file::open_ram(&m_zipdata[0], m_zipdata.size(), m_openflags, m_file); if (filerr != FILERR_NONE) { m_zipdata.clear(); return FILERR_FAILURE; } // close out the ZIP file zip_file_close(m_zipfile); m_zipfile = nullptr; return FILERR_NONE; }
file_error emu_file::load_zipped_file() { assert(m_file == NULL); assert(m_zipdata == NULL); assert(m_zipfile != NULL); // allocate some memory m_zipdata = global_alloc_array(UINT8, m_ziplength); // read the data into our buffer and return zip_error ziperr = zip_file_decompress(m_zipfile, m_zipdata, m_ziplength); if (ziperr != ZIPERR_NONE) { global_free(m_zipdata); m_zipdata = NULL; return FILERR_FAILURE; } // convert to RAM file file_error filerr = core_fopen_ram(m_zipdata, m_ziplength, m_openflags, &m_file); if (filerr != FILERR_NONE) { global_free(m_zipdata); m_zipdata = NULL; return FILERR_FAILURE; } // close out the ZIP file zip_file_close(m_zipfile); m_zipfile = NULL; return FILERR_NONE; }
static imgtool_stream *stream_open_zip(const char *zipname, const char *subname, int read_or_write) { imgtool_stream *imgfile = NULL; // zip_error ziperr; zip_file *z = NULL; const zip_file_header *zipent; FILE *f; if (read_or_write) goto error; /* check to see if the file exists */ f = fopen(zipname, "r"); if (!f) goto error; fclose(f); imgfile = (imgtool_stream *)malloc(sizeof(imgtool_stream)); if (!imgfile) goto error; memset(imgfile, 0, sizeof(*imgfile)); imgfile->imgtype = IMG_MEM; imgfile->write_protect = 1; imgfile->position = 0; // ziperr = zip_file_open(zipname, &z); if (!z) goto error; zipent = zip_file_first_file(z); while(zipent && subname && strcmp(subname, zipent->filename)) zipent = zip_file_next_file(z); if (!zipent) goto error; imgfile->filesize = zipent->uncompressed_length; imgfile->u.buffer = (UINT8*)malloc(zipent->uncompressed_length); if (!imgfile->u.buffer) goto error; if (zip_file_decompress(z, imgfile->u.buffer, zipent->uncompressed_length)) goto error; zip_file_close(z); return imgfile; error: if (z) zip_file_close(z); if (imgfile) { if (imgfile->u.buffer) free(imgfile->u.buffer); free(imgfile); } return NULL; }
static void romident(const char *filename, romident_status *status) { osd_directory *directory; /* reset the status */ memset(status, 0, sizeof(*status)); /* first try to open as a directory */ directory = osd_opendir(filename); if (directory != NULL) { const osd_directory_entry *entry; /* iterate over all files in the directory */ while ((entry = osd_readdir(directory)) != NULL) if (entry->type == ENTTYPE_FILE) { astring *curfile = astring_assemble_3(astring_alloc(), filename, PATH_SEPARATOR, entry->name); identify_file(astring_c(curfile), status); astring_free(curfile); } osd_closedir(directory); } /* if that failed, and the filename ends with .zip, identify as a ZIP file */ else if (core_filename_ends_with(filename, ".zip")) { /* first attempt to examine it as a valid ZIP file */ zip_file *zip = NULL; zip_error ziperr = zip_file_open(filename, &zip); if (ziperr == ZIPERR_NONE && zip != NULL) { const zip_file_header *entry; /* loop over entries in the ZIP, skipping empty files and directories */ for (entry = zip_file_first_file(zip); entry; entry = zip_file_next_file(zip)) if (entry->uncompressed_length != 0) { UINT8 *data = (UINT8 *)malloc(entry->uncompressed_length); if (data != NULL) { /* decompress data into RAM and identify it */ ziperr = zip_file_decompress(zip, data, entry->uncompressed_length); if (ziperr == ZIPERR_NONE) identify_data(entry->filename, data, entry->uncompressed_length, status); free(data); } } /* close up */ zip_file_close(zip); } } /* otherwise, identify as a raw file */ else identify_file(filename, status); }
static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node, multicart_resource *resource) { const char *file, *crcstr, *sha1; const zip_file_header *header; zip_error ziperr; UINT32 crc; /* locate the 'file' attribute */ file = xml_get_attribute_string(resource_node, "file", NULL); if (file == NULL) return MCERR_XML_ERROR; if (!(crcstr = xml_get_attribute_string(resource_node, "crc", NULL))) { /* locate the file in the ZIP file */ header = find_file(state->zip, file); } else /* CRC tag is present, use it */ { crc = strtoul(crcstr, NULL, 16); header = find_file_crc(state->zip, file, crc); } if (header == NULL) return MCERR_INVALID_FILE_REF; resource->length = header->uncompressed_length; /* allocate bytes for this resource */ resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); if (resource->ptr == NULL) return MCERR_OUT_OF_MEMORY; /* and decompress it */ ziperr = zip_file_decompress(state->zip, resource->ptr, resource->length); if (ziperr != ZIPERR_NONE) return MCERR_ZIP_ERROR; /* check SHA1 now */ if ((sha1 = xml_get_attribute_string(resource_node, "sha1", NULL))) { hash_collection actual_hashes; actual_hashes.compute((const UINT8 *)resource->ptr, resource->length, hash_collection::HASH_TYPES_CRC_SHA1); hash_collection expected_hashes; expected_hashes.add_from_string(hash_collection::HASH_SHA1, sha1, strlen(sha1)); if (actual_hashes != expected_hashes) { return MCERR_INVALID_FILE_REF; } } return MCERR_NONE; }
static imgtool_stream *stream_open_zip(const char *zipname, const char *subname, int read_or_write) { if (read_or_write) return nullptr; /* check to see if the file exists */ FILE *f = fopen(zipname, "r"); if (!f) return nullptr; fclose(f); imgtool_stream::ptr imgfile(new imgtool_stream(true)); imgfile->imgtype = IMG_MEM; zip_file *z = nullptr; const zip_file_header *zipent = nullptr; zip_file_open(zipname, &z); if (!z) goto error; zipent = zip_file_first_file(z); while (zipent && subname && strcmp(subname, zipent->filename)) zipent = zip_file_next_file(z); if (!zipent) goto error; imgfile->filesize = zipent->uncompressed_length; imgfile->buffer = reinterpret_cast<std::uint8_t *>(malloc(zipent->uncompressed_length)); if (!imgfile->buffer) goto error; if (zip_file_decompress(z, imgfile->buffer, zipent->uncompressed_length)) goto error; zip_file_close(z); return imgfile.release(); error: if (z) zip_file_close(z); return nullptr; }
static file_error OpenDIBFile(const char *dir_name, const char *zip_name, const char *filename, core_file **file, void **buffer) { file_error filerr; zip_error ziperr; zip_file *zip; const zip_file_header *zip_header; astring *fname; // clear out result *file = NULL; // look for the raw file fname = astring_assemble_3(astring_alloc(), dir_name, PATH_SEPARATOR, filename); filerr = core_fopen(astring_c(fname), OPEN_FLAG_READ, file); astring_free(fname); // did the raw file not exist? if (filerr != FILERR_NONE) { // look into zip file fname = astring_assemble_4(astring_alloc(), dir_name, PATH_SEPARATOR, zip_name, ".zip"); ziperr = zip_file_open(astring_c(fname), &zip); astring_free(fname); if (ziperr == ZIPERR_NONE) { zip_header = zip_file_seek_file(zip, filename); if (zip_header != NULL) { *buffer = malloc(zip_header->uncompressed_length); ziperr = zip_file_decompress(zip, *buffer, zip_header->uncompressed_length); if (ziperr == ZIPERR_NONE) { filerr = core_fopen_ram(*buffer, zip_header->uncompressed_length, OPEN_FLAG_READ, file); } } zip_file_close(zip); } } return filerr; }
multicart_open_error multicart_open(emu_options &options, const char *filename, const char *gamedrv, multicart_load_flags load_flags, multicart_t **cart) { multicart_open_error err; zip_error ziperr; object_pool *pool; multicart_load_state state = {0, }; const zip_file_header *header; const char *pcb_type; char *layout_text = NULL; /* allocate an object pool */ pool = pool_alloc_lib(NULL); if (pool == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } /* allocate the multicart */ state.multicart = (multicart_t*)pool_malloc_lib(pool, sizeof(*state.multicart)); if (state.multicart == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } memset(state.multicart, 0, sizeof(*state.multicart)); /* allocate the multicart's private data */ state.multicart->data = (multicart_private*)pool_malloc_lib(pool, sizeof(*state.multicart->data)); if (state.multicart->data == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } memset(state.multicart->data, 0, sizeof(*state.multicart->data)); state.multicart->data->pool = pool; pool = NULL; /* open the ZIP file */ ziperr = zip_file_open(filename, &state.zip); if (ziperr != ZIPERR_NONE) { err = MCERR_NOT_MULTICART; goto done; } /* find the layout.xml file */ header = find_file(state.zip, "layout.xml"); if (header == NULL) { err = MCERR_MISSING_LAYOUT; goto done; } /* reserve space for the layout text */ layout_text = (char*)malloc(header->uncompressed_length + 1); if (layout_text == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } /* uncompress the layout text */ ziperr = zip_file_decompress(state.zip, layout_text, header->uncompressed_length); if (ziperr != ZIPERR_NONE) { err = MCERR_ZIP_ERROR; goto done; } layout_text[header->uncompressed_length] = '\0'; /* parse the layout text */ state.layout_xml = xml_string_read(layout_text, NULL); if (state.layout_xml == NULL) { err = MCERR_XML_ERROR; goto done; } /* locate the PCB node */ if (!find_pcb_and_resource_nodes(state.layout_xml, &state.pcb_node, &state.resources_node)) { err = MCERR_NO_PCB_OR_RESOURCES; goto done; } /* get the PCB resource_type */ pcb_type = xml_get_attribute_string(state.pcb_node, "type", ""); state.multicart->pcb_type = pool_strdup_lib(state.multicart->data->pool, pcb_type); if (state.multicart->pcb_type == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } state.multicart->gamedrv_name = pool_strdup_lib(state.multicart->data->pool, gamedrv); if (state.multicart->gamedrv_name == NULL) { err = MCERR_OUT_OF_MEMORY; goto done; } /* do we have to load resources? */ if (load_flags & MULTICART_FLAGS_LOAD_RESOURCES) { err = load_all_resources(options, &state); if (err != MCERR_NONE) goto done; err = load_all_sockets(&state); if (err != MCERR_NONE) goto done; } err = MCERR_NONE; done: if (pool != NULL) pool_free_lib(pool); if (state.zip != NULL) zip_file_close(state.zip); if (layout_text != NULL) free(layout_text); if (state.layout_xml != NULL) xml_file_free(state.layout_xml); if ((err != MCERR_NONE) && (state.multicart != NULL)) { multicart_close(options, state.multicart); state.multicart = NULL; } *cart = state.multicart; return err; }
static image_error_t load_zip_path(mess_image *image, const char *path) { image_error_t err = IMAGE_ERROR_FILENOTFOUND; zip_file *zip = NULL; zip_error ziperr; const zip_file_header *header; const char *zip_extension = ".zip"; char *path_copy; const char *zip_entry; void *ptr; int pos; /* create our own copy of the path */ path_copy = mame_strdup(path); if (!path_copy) { err = IMAGE_ERROR_OUTOFMEMORY; goto done; } /* loop through the path and try opening zip files */ ziperr = ZIPERR_FILE_ERROR; zip_entry = NULL; pos = strlen(path_copy); while(pos > strlen(zip_extension)) { /* is this a potential zip path? */ if ((path_copy[pos] == '\0') || !strncmp(&path_copy[pos], PATH_SEPARATOR, strlen(PATH_SEPARATOR))) { /* parse out the zip path */ if (path_copy[pos] == '\0') { /* no zip path */ zip_entry = NULL; } else { /* we are at a zip path */ path_copy[pos] = '\0'; zip_entry = &path_copy[pos + strlen(PATH_SEPARATOR)]; } /* try to open the zip file */ ziperr = zip_file_open(path_copy, &zip); if (ziperr != ZIPERR_FILE_ERROR) break; /* restore the path if we changed */ if (zip_entry) path_copy[pos] = PATH_SEPARATOR[0]; } pos--; } /* did we succeed in opening up a zip file? */ if (ziperr == ZIPERR_NONE) { /* iterate through the zip file */ header = zip_file_first_file(zip); /* if we specified a zip partial path, find it */ if (zip_entry) { while(header) { if (!mame_stricmp(header->filename, zip_entry)) break; header = zip_file_next_file(zip); } } /* were we successful? */ if (header) { /* if no zip path was specified, we have to change the name */ if (!zip_entry) { /* use the first entry; tough part is we have to change the name */ err = set_image_filename(image, image->name, header->filename); if (err) goto done; } /* allocate space for this zip file */ ptr = image_malloc(image, header->uncompressed_length); if (!ptr) { err = IMAGE_ERROR_OUTOFMEMORY; goto done; } ziperr = zip_file_decompress(zip, ptr, header->uncompressed_length); if (ziperr == ZIPERR_NONE) { /* success! */ err = IMAGE_ERROR_SUCCESS; image->ptr = ptr; image->length = header->uncompressed_length; } } } done: if (path_copy) free(path_copy); if (zip) zip_file_close(zip); return err; }