static multicart_open_error load_socket(multicart_load_state *state, xml_data_node *socket_node) { const char *id; const char *uses; const multicart_resource *resource; multicart_socket *socket; multicart_socket **next_socket; /* get the 'id' and 'uses' attributes; error if not present */ id = xml_get_attribute_string(socket_node, "id", NULL); uses = xml_get_attribute_string(socket_node, "uses", NULL); if ((id == NULL) || (uses == NULL)) return MCERR_XML_ERROR; /* find the resource */ for (resource = state->multicart->resources; resource != NULL; resource = resource->next) { if (!strcmp(uses, resource->id)) break; } if (resource == NULL) return MCERR_INVALID_RESOURCE_REF; /* create the socket */ socket = (multicart_socket *)pool_malloc_lib(state->multicart->data->pool, sizeof(*socket)); if (socket == NULL) return MCERR_OUT_OF_MEMORY; memset(socket, 0, sizeof(*socket)); socket->resource = resource; socket->ptr = resource->ptr; /* copy id */ socket->id = pool_strdup_lib(state->multicart->data->pool, id); if (socket->id == NULL) return MCERR_OUT_OF_MEMORY; /* which pointer should I use? */ if (resource->ptr != NULL) { /* use the resource's ptr */ socket->ptr = resource->ptr; } else { /* allocate bytes for this socket */ socket->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); if (socket->ptr == NULL) return MCERR_OUT_OF_MEMORY; /* ...and clear it */ memset(socket->ptr, 0xCD, resource->length); } /* append the resource */ for (next_socket = &state->sockets; *next_socket; next_socket = &(*next_socket)->next) ; *next_socket = socket; return MCERR_NONE; }
imgtool_library *imgtool_library_create(void) { imgtool_library *library; object_pool *pool; /* create a memory pool */ pool = pool_alloc_lib(NULL); if (!pool) goto error; /* allocate the main structure */ library = (imgtool_library *)pool_malloc_lib(pool, sizeof(struct _imgtool_library)); if (!library) goto error; /* initialize the structure */ memset(library, 0, sizeof(*library)); library->pool = pool; return library; error: if (pool) pool_free_lib(pool); return NULL; }
static void node_imageverify(xml_data_node *node) { void *new_buffer; mess_pile pile; /* <imageverify> - verifies that an image contains specific data */ memset(&new_command, 0, sizeof(new_command)); new_command.command_type = MESSTEST_COMMAND_VERIFY_IMAGE; /* 'tag', 'type', 'slot' attributes */ if (!get_device_identity_tags(node, &new_command.u.verify_args.device_ident)) return; pile_init(&pile); messtest_get_data(node, &pile); new_buffer = pool_malloc_lib(command_pool, pile_size(&pile)); memcpy(new_buffer, pile_getptr(&pile), pile_size(&pile)); new_command.u.verify_args.verify_data = new_buffer; new_command.u.verify_args.verify_data_size = pile_size(&pile); pile_delete(&pile); if (!append_command()) { error_outofmemory(); return; } }
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 multicart_open_error load_ram_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, multicart_resource *resource) { const char *length_string; const char *ram_type; const char *ram_filename; astring *ram_pathname; /* locate the 'length' attribute */ length_string = xml_get_attribute_string(resource_node, "length", NULL); if (length_string == NULL) return MCERR_MISSING_RAM_LENGTH; /* ...and parse it */ resource->length = ram_parse_string(length_string); if (resource->length <= 0) return MCERR_INVALID_RAM_SPEC; /* 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; /* Is this a persistent RAM resource? Then try to load it. */ ram_type = xml_get_attribute_string(resource_node, "type", NULL); if (ram_type != NULL) { if (strcmp(ram_type, "persistent")==0) { astring tmp; /* Get the file name. */ ram_filename = xml_get_attribute_string(resource_node, "file", NULL); if (ram_filename==NULL) return MCERR_XML_ERROR; ram_pathname = astring_assemble_3(&tmp, state->multicart->gamedrv_name, PATH_SEPARATOR, ram_filename); /* Save the file name so that we can write the contents on unloading. If the RAM resource has no filename, we know that it was volatile only. */ resource->filename = pool_strdup_lib(state->multicart->data->pool, astring_c(ram_pathname)); if (resource->filename == NULL) return MCERR_OUT_OF_MEMORY; image_battery_load_by_name(options, resource->filename, resource->ptr, resource->length, 0x00); } /* else this type is volatile, in which case we just have a memory expansion */ } return MCERR_NONE; }
static multicart_open_error load_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, multicart_resource_type resource_type) { const char *id; multicart_open_error err; multicart_resource *resource; multicart_resource **next_resource; /* get the 'id' attribute; error if not present */ id = xml_get_attribute_string(resource_node, "id", NULL); if (id == NULL) return MCERR_XML_ERROR; /* allocate memory for the resource */ resource = (multicart_resource *)pool_malloc_lib(state->multicart->data->pool, sizeof(*resource)); if (resource == NULL) return MCERR_OUT_OF_MEMORY; memset(resource, 0, sizeof(*resource)); resource->type = resource_type; /* copy id */ resource->id = pool_strdup_lib(state->multicart->data->pool, id); if (resource->id == NULL) return MCERR_OUT_OF_MEMORY; switch(resource->type) { case MULTICART_RESOURCE_TYPE_ROM: err = load_rom_resource(state, resource_node, resource); if (err != MCERR_NONE) return err; break; case MULTICART_RESOURCE_TYPE_RAM: err = load_ram_resource(options, state, resource_node, resource); if (err != MCERR_NONE) return err; break; default: return MCERR_UNKNOWN_RESOURCE_TYPE; } /* append the resource */ for (next_resource = &state->resources; *next_resource; next_resource = &(*next_resource)->next) ; *next_resource = resource; return MCERR_NONE; }
static void node_memverify(xml_data_node *node) { xml_attribute_node *attr_node; const char *s1; const char *s2; void *new_buffer; mess_pile pile; /* <memverify> - verifies that a range of memory contains specific data */ attr_node = xml_get_attribute(node, "start"); s1 = attr_node ? attr_node->value : NULL; if (!s1) { error_missingattribute("start"); return; } attr_node = xml_get_attribute(node, "end"); s2 = attr_node ? attr_node->value : NULL; if (!s2) s2 = "0"; memset(&new_command, 0, sizeof(new_command)); new_command.command_type = MESSTEST_COMMAND_VERIFY_MEMORY; attr_node = xml_get_attribute(node, "region"); new_command.u.verify_args.mem_region = attr_node ? attr_node->value : NULL; attr_node = xml_get_attribute(node, "cpu"); new_command.u.verify_args.cpu_name = attr_node ? attr_node->value : NULL; new_command.u.verify_args.start = parse_offset(s1); new_command.u.verify_args.end = parse_offset(s2); pile_init(&pile); messtest_get_data(node, &pile); new_buffer = pool_malloc_lib(command_pool, pile_size(&pile)); memcpy(new_buffer, pile_getptr(&pile), pile_size(&pile)); new_command.u.verify_args.verify_data = new_buffer; new_command.u.verify_args.verify_data_size = pile_size(&pile); pile_delete(&pile); if (!append_command()) { error_outofmemory(); return; } }
static void verify_end_handler(const void *buffer, size_t size) { void *new_buffer; new_buffer = pool_malloc_lib(command_pool, size); memcpy(new_buffer, buffer, size); new_command.u.verify_args.verify_data = new_buffer; new_command.u.verify_args.verify_data_size = size; command_end_handler(NULL, 0); if (!append_command()) { error_outofmemory(); return; } }
BOOL CreateMessIcons(void) { int i = 0; HWND hwndSoftwareList; HIMAGELIST hSmall; HIMAGELIST hLarge; // create the icon index, if we haven't already if (!mess_icon_index) { mess_icon_index = (int*)pool_malloc_lib(GetMameUIMemoryPool(), driver_list::total() * IO_COUNT * sizeof(*mess_icon_index)); } for (i = 0; i < (driver_list::total() * IO_COUNT); i++) mess_icon_index[i] = 0; // Associate the image lists with the list view control. hwndSoftwareList = GetDlgItem(GetMainWindow(), IDC_SWLIST); hSmall = GetSmallImageList(); hLarge = GetLargeImageList(); (void)ListView_SetImageList(hwndSoftwareList, hSmall, LVSIL_SMALL); (void)ListView_SetImageList(hwndSoftwareList, hLarge, LVSIL_NORMAL); return TRUE; }
option_resolution *option_resolution_create(const option_guide *guide, const char *specification) { option_resolution *resolution = nullptr; const option_guide *guide_entry; int option_count; int opt = -1; object_pool *pool; assert(guide); /* first count the number of options specified in the guide */ option_count = option_resolution_countoptions(guide, specification); /* create a memory pool for this structure */ pool = pool_alloc_lib(nullptr); if (!pool) goto outofmemory; /* allocate the main structure */ resolution = (option_resolution *)pool_malloc_lib(pool, sizeof(option_resolution)); if (!resolution) goto outofmemory; memset(resolution, 0, sizeof(*resolution)); resolution->pool = pool; /* set up the entries list */ resolution->option_count = option_count; resolution->specification = specification; resolution->entries = (option_resolution_entry *)pool_malloc_lib(resolution->pool, sizeof(struct option_resolution_entry) * option_count); if (!resolution->entries) goto outofmemory; memset(resolution->entries, 0, sizeof(struct option_resolution_entry) * option_count); /* initialize each of the entries */ opt = 0; guide_entry = guide; while(guide_entry->option_type != OPTIONTYPE_END) { switch(guide_entry->option_type) { case OPTIONTYPE_INT: case OPTIONTYPE_ENUM_BEGIN: case OPTIONTYPE_STRING: if (lookup_in_specification(specification, guide_entry)) resolution->entries[opt++].guide_entry = guide_entry; break; case OPTIONTYPE_ENUM_VALUE: break; default: goto unexpected; } guide_entry++; } assert(opt == option_count); return resolution; unexpected: assert(FALSE); outofmemory: if (resolution) option_resolution_close(resolution); return nullptr; }
void *imgtool_library_malloc(imgtool_library *library, size_t mem) { return pool_malloc_lib(library->pool, mem); }
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; }
void *library::imgtool_library_malloc(size_t mem) { return pool_malloc_lib(m_pool, mem); }
int messdocs(const char *toc_filename, const char *dest_dir, const char *help_project_filename, const char *help_contents_filename, const char *help_filename) { char buf[4096]; struct messdocs_state state; int len; int done; FILE *in; FILE *chm_hhp; int i; char *s; XML_Memory_Handling_Suite memcallbacks; memset(&state, 0, sizeof(state)); state.m_pool = pool_alloc_lib(NULL); /* open the DOC */ in = fopen(toc_filename, "r"); if (!in) { fprintf(stderr, "Cannot open TOC file '%s'\n", toc_filename); goto error; } /* figure out the TOC directory */ state.m_toc_dir = pool_strdup_lib(state.m_pool, toc_filename); if (!state.m_toc_dir) goto outofmemory; for (i = strlen(state.m_toc_dir) - 1; (i > 0) && !osd_is_path_separator(state.m_toc_dir[i]); i--) state.m_toc_dir[i] = '\0'; /* clean the target directory */ rmdir_recursive(dest_dir); osd_mkdir(dest_dir); /* create the help contents file */ s = (char*)pool_malloc_lib(state.m_pool, strlen(dest_dir) + 1 + strlen(help_project_filename) + 1); if (!s) goto outofmemory; strcpy(s, dest_dir); strcat(s, PATH_SEPARATOR); strcat(s, help_contents_filename); state.m_chm_toc = fopen(s, "w"); state.m_dest_dir = dest_dir; if (!state.m_chm_toc) { fprintf(stderr, "Cannot open file %s\n", s); goto error; } fprintf(state.m_chm_toc, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\"\n"); fprintf(state.m_chm_toc, "<HTML>\n"); fprintf(state.m_chm_toc, "<HEAD>\n"); fprintf(state.m_chm_toc, "</HEAD>\n"); fprintf(state.m_chm_toc, "<BODY>\n"); fprintf(state.m_chm_toc, "<OBJECT type=\"text/site properties\">\n"); fprintf(state.m_chm_toc, "\t<param name=\"Window Styles\" value=\"0x800625\">\n"); fprintf(state.m_chm_toc, "\t<param name=\"ImageType\" value=\"Folder\">\n"); fprintf(state.m_chm_toc, "\t<param name=\"Font\" value=\"Arial,8,0\">\n"); fprintf(state.m_chm_toc, "</OBJECT>\n"); fprintf(state.m_chm_toc, "<UL>\n"); /* create the XML parser */ memcallbacks.malloc_fcn = expat_malloc; memcallbacks.realloc_fcn = expat_realloc; memcallbacks.free_fcn = expat_free; state.m_parser = XML_ParserCreate_MM(NULL, &memcallbacks, NULL); if (!state.m_parser) goto outofmemory; XML_SetUserData(state.m_parser, &state); XML_SetElementHandler(state.m_parser, start_handler, end_handler); XML_SetCharacterDataHandler(state.m_parser, data_handler); do { len = (int) fread(buf, 1, sizeof(buf), in); done = feof(in); if (XML_Parse(state.m_parser, buf, len, done) == XML_STATUS_ERROR) { process_error(&state, NULL, NULL); break; } } while(!done); fprintf(state.m_chm_toc, "</UL>\n"); fprintf(state.m_chm_toc, "</BODY></HTML>"); fclose(state.m_chm_toc); /* create the help project file */ s = (char*)pool_malloc_lib(state.m_pool, strlen(dest_dir) + 1 + strlen(help_project_filename) + 1); if (!s) goto outofmemory; strcpy(s, dest_dir); strcat(s, PATH_SEPARATOR); strcat(s, help_project_filename); chm_hhp = fopen(s, "w"); if (!chm_hhp) { fprintf(stderr, "Cannot open file %s\n", s); goto error; } fprintf(chm_hhp, "[OPTIONS]\n"); fprintf(chm_hhp, "Compiled file=%s\n", help_filename); fprintf(chm_hhp, "Contents file=%s\n", help_contents_filename); fprintf(chm_hhp, "Default topic=%s\n", state.m_default_topic); fprintf(chm_hhp, "Language=0x409 English (United States)\n"); fprintf(chm_hhp, "Title=%s\n", state.m_title); fprintf(chm_hhp, "\n"); fclose(chm_hhp); /* finish up */ XML_ParserFree(state.m_parser); fclose(in); pool_free_lib(state.m_pool); return state.m_error ? -1 : 0; outofmemory: fprintf(stderr, "Out of memory"); error: if (state.m_chm_toc) fclose(state.m_chm_toc); if (in) fclose(in); return -1; }