src/mess/devices/multcart.c
| r4955 | r4956 | |
| 13 | 13 | #include "xmlfile.h" |
| 14 | 14 | #include "driver.h" |
| 15 | 15 | #include "compcfg.h" |
| 16 | #include "hash.h" |
| 16 | 17 | |
| 17 | 18 | /*************************************************************************** |
| 18 | 19 | TYPE DEFINITIONS |
| r4955 | r4956 | |
| 79 | 80 | return NULL; |
| 80 | 81 | } |
| 81 | 82 | |
| 83 | static const zip_file_header *find_file_crc(zip_file *zip, const char *filename, UINT32 crc) |
| 84 | { |
| 85 | const zip_file_header *header; |
| 86 | for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip)) |
| 87 | { |
| 88 | // if the CRC and name both match, we're good |
| 89 | // if the CRC matches and the name doesn't, we're still good |
| 90 | if ((!core_stricmp(header->filename, filename)) && (header->crc == crc)) |
| 91 | { |
| 92 | return header; |
| 93 | } |
| 94 | else if (header->crc == crc) |
| 95 | { |
| 96 | return header; |
| 97 | } |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 82 | 101 | |
| 83 | 102 | /*------------------------------------------------- |
| 84 | 103 | find_pcb_and_resource_nodes |
| r4955 | r4956 | |
| 137 | 156 | static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node, |
| 138 | 157 | multicart_resource *resource) |
| 139 | 158 | { |
| 140 | | const char *file; |
| 159 | const char *file, *crcstr, *sha1; |
| 141 | 160 | const zip_file_header *header; |
| 142 | 161 | zip_error ziperr; |
| 162 | UINT32 crc; |
| 143 | 163 | |
| 144 | 164 | /* locate the 'file' attribute */ |
| 145 | 165 | file = xml_get_attribute_string(resource_node, "file", NULL); |
| 146 | 166 | if (file == NULL) |
| 147 | 167 | return MCERR_XML_ERROR; |
| 148 | 168 | |
| 149 | | /* locate the file in the ZIP file */ |
| 150 | | header = find_file(state->zip, file); |
| 169 | if (!(crcstr = xml_get_attribute_string(resource_node, "crc", NULL))) |
| 170 | { |
| 171 | /* locate the file in the ZIP file */ |
| 172 | header = find_file(state->zip, file); |
| 173 | } |
| 174 | else /* CRC tag is present, use it */ |
| 175 | { |
| 176 | crc = strtoul(crcstr, NULL, 16); |
| 177 | header = find_file_crc(state->zip, file, crc); |
| 178 | } |
| 151 | 179 | if (header == NULL) |
| 152 | 180 | return MCERR_INVALID_FILE_REF; |
| 153 | 181 | resource->length = header->uncompressed_length; |
| r4955 | r4956 | |
| 162 | 190 | if (ziperr != ZIPERR_NONE) |
| 163 | 191 | return MCERR_ZIP_ERROR; |
| 164 | 192 | |
| 193 | /* check SHA1 now */ |
| 194 | if ((sha1 = xml_get_attribute_string(resource_node, "sha1", NULL))) |
| 195 | { |
| 196 | char calc_sha[256]; |
| 197 | |
| 198 | memset(calc_sha, 0, sizeof(calc_sha)); |
| 199 | hash_compute(&calc_sha[0], resource->ptr, resource->length, HASH_SHA1); |
| 200 | |
| 201 | if ((strncmp(sha1, &calc_sha[2], 20))) |
| 202 | { |
| 203 | return MCERR_INVALID_FILE_REF; |
| 204 | } |
| 205 | } |
| 206 | |
| 165 | 207 | return MCERR_NONE; |
| 166 | 208 | } |
| 167 | 209 | |
| r4955 | r4956 | |
| 562 | 604 | save_ram_resources(cart); |
| 563 | 605 | pool_free(cart->data->pool); |
| 564 | 606 | } |
| 565 | | |