Previous 15994 Revisions Next

r4956 Sunday 7th June, 2009 at 00:49:12 UTC by R. Belmont
Add support for CRC32 and SHA1 checksums in XML multicarts.
[src/mess/devices]multcart.c

src/mess/devices/multcart.c
r4955r4956
1313#include "xmlfile.h"
1414#include "driver.h"
1515#include "compcfg.h"
16#include "hash.h"
1617
1718/***************************************************************************
1819    TYPE DEFINITIONS
r4955r4956
7980   return NULL;
8081}
8182
83static 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}
82101
83102/*-------------------------------------------------
84103    find_pcb_and_resource_nodes
r4955r4956
137156static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node,
138157   multicart_resource *resource)
139158{
140   const char *file;
159   const char *file, *crcstr, *sha1;
141160   const zip_file_header *header;
142161   zip_error ziperr;
162   UINT32 crc;
143163
144164   /* locate the 'file' attribute */
145165   file = xml_get_attribute_string(resource_node, "file", NULL);
146166   if (file == NULL)
147167      return MCERR_XML_ERROR;
148168
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   }
151179   if (header == NULL)
152180      return MCERR_INVALID_FILE_REF;
153181   resource->length = header->uncompressed_length;
r4955r4956
162190   if (ziperr != ZIPERR_NONE)
163191      return MCERR_ZIP_ERROR;
164192
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
165207   return MCERR_NONE;
166208}
167209
r4955r4956
562604   save_ram_resources(cart);
563605   pool_free(cart->data->pool);
564606}
565

Previous 15994 Revisions Next


© 1998-2018 The MESS Team