src/mess/hashfile.c
| r4160 | r4161 | |
| 71 | 71 | CORE IMPLEMENTATION |
| 72 | 72 | ***************************************************************************/ |
| 73 | 73 | |
| 74 | /*------------------------------------------------- |
| 75 | parse_error |
| 76 | -------------------------------------------------*/ |
| 77 | |
| 74 | 78 | static void ATTR_PRINTF(2,3) parse_error(struct hash_parse_state *state, const char *fmt, ...) |
| 75 | 79 | { |
| 76 | 80 | char buf[256]; |
| r4160 | r4161 | |
| 79 | 83 | if (state->error_proc) |
| 80 | 84 | { |
| 81 | 85 | va_start(va, fmt); |
| 82 | | vsnprintf(buf, sizeof(buf) / sizeof(buf[0]), fmt, va); |
| 86 | vsnprintf(buf, ARRAY_LENGTH(buf), fmt, va); |
| 83 | 87 | va_end(va); |
| 84 | | state->error_proc(buf); |
| 88 | (*state->error_proc)(buf); |
| 85 | 89 | } |
| 86 | 90 | } |
| 87 | 91 | |
| 88 | 92 | |
| 89 | 93 | |
| 94 | /*------------------------------------------------- |
| 95 | unknown_tag |
| 96 | -------------------------------------------------*/ |
| 97 | |
| 90 | 98 | static void unknown_tag(struct hash_parse_state *state, const char *tagname) |
| 91 | 99 | { |
| 92 | 100 | parse_error(state, "[%lu:%lu]: Unknown tag: %s\n", |
| r4160 | r4161 | |
| 97 | 105 | |
| 98 | 106 | |
| 99 | 107 | |
| 108 | /*------------------------------------------------- |
| 109 | unknown_attribute |
| 110 | -------------------------------------------------*/ |
| 111 | |
| 100 | 112 | static void unknown_attribute(struct hash_parse_state *state, const char *attrname) |
| 101 | 113 | { |
| 102 | 114 | parse_error(state, "[%lu:%lu]: Unknown attribute: %s\n", |
| r4160 | r4161 | |
| 107 | 119 | |
| 108 | 120 | |
| 109 | 121 | |
| 122 | /*------------------------------------------------- |
| 123 | unknown_attribute_value |
| 124 | -------------------------------------------------*/ |
| 125 | |
| 110 | 126 | static void unknown_attribute_value(struct hash_parse_state *state, |
| 111 | 127 | const char *attrname, const char *attrvalue) |
| 112 | 128 | { |
| r4160 | r4161 | |
| 118 | 134 | |
| 119 | 135 | |
| 120 | 136 | |
| 137 | /*------------------------------------------------- |
| 138 | start_handler |
| 139 | -------------------------------------------------*/ |
| 140 | |
| 121 | 141 | static void start_handler(void *data, const char *tagname, const char **attributes) |
| 122 | 142 | { |
| 123 | 143 | struct hash_parse_state *state = (struct hash_parse_state *) data; |
| r4160 | r4161 | |
| 129 | 149 | iodevice_t device; |
| 130 | 150 | int i; |
| 131 | 151 | |
| 132 | | switch(state->pos++) { |
| 133 | | case POS_ROOT: |
| 134 | | if (!strcmp(tagname, "hashfile")) |
| 135 | | { |
| 136 | | } |
| 137 | | else |
| 138 | | { |
| 139 | | unknown_tag(state, tagname); |
| 140 | | } |
| 141 | | break; |
| 152 | switch(state->pos++) |
| 153 | { |
| 154 | case POS_ROOT: |
| 155 | if (!strcmp(tagname, "hashfile")) |
| 156 | { |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | unknown_tag(state, tagname); |
| 161 | } |
| 162 | break; |
| 142 | 163 | |
| 143 | | case POS_MAIN: |
| 144 | | if (!strcmp(tagname, "hash")) |
| 145 | | { |
| 146 | | // we are now examining a hash tag |
| 147 | | name = NULL; |
| 148 | | memset(hash_string, 0, sizeof(hash_string)); |
| 149 | | all_functions = 0; |
| 150 | | device = IO_COUNT; |
| 164 | case POS_MAIN: |
| 165 | if (!strcmp(tagname, "hash")) |
| 166 | { |
| 167 | // we are now examining a hash tag |
| 168 | name = NULL; |
| 169 | memset(hash_string, 0, sizeof(hash_string)); |
| 170 | all_functions = 0; |
| 171 | device = IO_COUNT; |
| 151 | 172 | |
| 152 | | while(attributes[0]) |
| 153 | | { |
| 154 | | functions = 0; |
| 155 | | if (!strcmp(attributes[0], "name")) |
| 173 | while(attributes[0]) |
| 156 | 174 | { |
| 157 | | /* name attribute */ |
| 158 | | name = attributes[1]; |
| 175 | functions = 0; |
| 176 | if (!strcmp(attributes[0], "name")) |
| 177 | { |
| 178 | /* name attribute */ |
| 179 | name = attributes[1]; |
| 180 | } |
| 181 | else if (!strcmp(attributes[0], "crc32")) |
| 182 | { |
| 183 | /* crc32 attribute */ |
| 184 | functions = HASH_CRC; |
| 185 | } |
| 186 | else if (!strcmp(attributes[0], "md5")) |
| 187 | { |
| 188 | /* md5 attribute */ |
| 189 | functions = HASH_MD5; |
| 190 | } |
| 191 | else if (!strcmp(attributes[0], "sha1")) |
| 192 | { |
| 193 | /* sha1 attribute */ |
| 194 | functions = HASH_SHA1; |
| 195 | } |
| 196 | else if (!strcmp(attributes[0], "type")) |
| 197 | { |
| 198 | /* type attribute */ |
| 199 | i = device_typeid(attributes[1]); |
| 200 | if (i < 0) |
| 201 | unknown_attribute_value(state, attributes[0], attributes[1]); |
| 202 | else |
| 203 | device = (iodevice_t) i; |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | /* unknown attribute */ |
| 208 | unknown_attribute(state, attributes[0]); |
| 209 | } |
| 210 | |
| 211 | if (functions) |
| 212 | { |
| 213 | hash_data_insert_printable_checksum(hash_string, functions, attributes[1]); |
| 214 | all_functions |= functions; |
| 215 | } |
| 216 | |
| 217 | attributes += 2; |
| 159 | 218 | } |
| 160 | | else if (!strcmp(attributes[0], "crc32")) |
| 219 | |
| 220 | if (device == IO_COUNT) |
| 161 | 221 | { |
| 162 | | /* crc32 attribute */ |
| 163 | | functions = HASH_CRC; |
| 222 | for (i = 0; i < IO_COUNT; i++) |
| 223 | state->hashfile->functions[i] |= all_functions; |
| 164 | 224 | } |
| 165 | | else if (!strcmp(attributes[0], "md5")) |
| 166 | | { |
| 167 | | /* md5 attribute */ |
| 168 | | functions = HASH_MD5; |
| 169 | | } |
| 170 | | else if (!strcmp(attributes[0], "sha1")) |
| 171 | | { |
| 172 | | /* sha1 attribute */ |
| 173 | | functions = HASH_SHA1; |
| 174 | | } |
| 175 | | else if (!strcmp(attributes[0], "type")) |
| 176 | | { |
| 177 | | /* type attribute */ |
| 178 | | i = device_typeid(attributes[1]); |
| 179 | | if (i < 0) |
| 180 | | unknown_attribute_value(state, attributes[0], attributes[1]); |
| 181 | | else |
| 182 | | device = (iodevice_t) i; |
| 183 | | } |
| 184 | 225 | else |
| 185 | | { |
| 186 | | /* unknown attribute */ |
| 187 | | unknown_attribute(state, attributes[0]); |
| 188 | | } |
| 226 | state->hashfile->functions[device] |= all_functions; |
| 189 | 227 | |
| 190 | | if (functions) |
| 228 | /* do we use this hash? */ |
| 229 | if (!state->selector_proc || state->selector_proc(state->hashfile, state->param, name, hash_string)) |
| 191 | 230 | { |
| 192 | | hash_data_insert_printable_checksum(hash_string, functions, attributes[1]); |
| 193 | | all_functions |= functions; |
| 194 | | } |
| 231 | hi = pool_malloc(state->hashfile->pool, sizeof(hash_info)); |
| 232 | if (!hi) |
| 233 | return; |
| 234 | memset(hi, 0, sizeof(*hi)); |
| 195 | 235 | |
| 196 | | attributes += 2; |
| 197 | | } |
| 236 | hi->longname = pool_strdup(state->hashfile->pool, name); |
| 237 | if (!hi->longname) |
| 238 | return; |
| 198 | 239 | |
| 199 | | if (device == IO_COUNT) |
| 200 | | { |
| 201 | | for (i = 0; i < IO_COUNT; i++) |
| 202 | | state->hashfile->functions[i] |= all_functions; |
| 240 | strcpy(hi->hash, hash_string); |
| 241 | state->hi = hi; |
| 242 | } |
| 203 | 243 | } |
| 204 | 244 | else |
| 205 | | state->hashfile->functions[device] |= all_functions; |
| 206 | | |
| 207 | | /* do we use this hash? */ |
| 208 | | if (!state->selector_proc || state->selector_proc(state->hashfile, state->param, name, hash_string)) |
| 209 | 245 | { |
| 210 | | hi = pool_malloc(state->hashfile->pool, sizeof(hash_info)); |
| 211 | | if (!hi) |
| 212 | | return; |
| 213 | | memset(hi, 0, sizeof(*hi)); |
| 214 | | |
| 215 | | hi->longname = pool_strdup(state->hashfile->pool, name); |
| 216 | | if (!hi->longname) |
| 217 | | return; |
| 218 | | |
| 219 | | strcpy(hi->hash, hash_string); |
| 220 | | state->hi = hi; |
| 246 | unknown_tag(state, tagname); |
| 221 | 247 | } |
| 222 | | } |
| 223 | | else |
| 224 | | { |
| 225 | | unknown_tag(state, tagname); |
| 226 | | } |
| 227 | | break; |
| 248 | break; |
| 228 | 249 | |
| 229 | | case POS_HASH: |
| 230 | | text_dest = NULL; |
| 250 | case POS_HASH: |
| 251 | text_dest = NULL; |
| 231 | 252 | |
| 232 | | if (!strcmp(tagname, "year")) |
| 233 | | text_dest = (char **) &state->hi->year; |
| 234 | | else if (!strcmp(tagname, "manufacturer")) |
| 235 | | text_dest = (char **) &state->hi->manufacturer; |
| 236 | | else if (!strcmp(tagname, "status")) |
| 237 | | text_dest = (char **) &state->hi->playable; |
| 238 | | else if (!strcmp(tagname, "extrainfo")) |
| 239 | | text_dest = (char **) &state->hi->extrainfo; |
| 240 | | else |
| 241 | | unknown_tag(state, tagname); |
| 253 | if (!strcmp(tagname, "year")) |
| 254 | text_dest = (char **) &state->hi->year; |
| 255 | else if (!strcmp(tagname, "manufacturer")) |
| 256 | text_dest = (char **) &state->hi->manufacturer; |
| 257 | else if (!strcmp(tagname, "status")) |
| 258 | text_dest = (char **) &state->hi->playable; |
| 259 | else if (!strcmp(tagname, "extrainfo")) |
| 260 | text_dest = (char **) &state->hi->extrainfo; |
| 261 | else |
| 262 | unknown_tag(state, tagname); |
| 242 | 263 | |
| 243 | | if (text_dest && state->hi) |
| 244 | | state->text_dest = text_dest; |
| 245 | | break; |
| 264 | if (text_dest && state->hi) |
| 265 | state->text_dest = text_dest; |
| 266 | break; |
| 246 | 267 | } |
| 247 | 268 | } |
| 248 | 269 | |
| 249 | 270 | |
| 250 | 271 | |
| 272 | /*------------------------------------------------- |
| 273 | end_handler |
| 274 | -------------------------------------------------*/ |
| 275 | |
| 251 | 276 | static void end_handler(void *data, const char *name) |
| 252 | 277 | { |
| 253 | 278 | struct hash_parse_state *state = (struct hash_parse_state *) data; |
| 254 | 279 | state->text_dest = NULL; |
| 255 | 280 | |
| 256 | | switch(--state->pos) { |
| 257 | | case POS_ROOT: |
| 258 | | case POS_HASH: |
| 259 | | break; |
| 281 | switch(--state->pos) |
| 282 | { |
| 283 | case POS_ROOT: |
| 284 | case POS_HASH: |
| 285 | break; |
| 260 | 286 | |
| 261 | | case POS_MAIN: |
| 262 | | if (state->hi) |
| 263 | | { |
| 264 | | if (state->use_proc) |
| 265 | | state->use_proc(state->hashfile, state->param, state->hi); |
| 266 | | state->hi = NULL; |
| 267 | | } |
| 268 | | break; |
| 287 | case POS_MAIN: |
| 288 | if (state->hi) |
| 289 | { |
| 290 | if (state->use_proc) |
| 291 | (*state->use_proc)(state->hashfile, state->param, state->hi); |
| 292 | state->hi = NULL; |
| 293 | } |
| 294 | break; |
| 269 | 295 | } |
| 270 | 296 | } |
| 271 | 297 | |
| 272 | 298 | |
| 273 | 299 | |
| 300 | /*------------------------------------------------- |
| 301 | data_handler |
| 302 | -------------------------------------------------*/ |
| 303 | |
| 274 | 304 | static void data_handler(void *data, const XML_Char *s, int len) |
| 275 | 305 | { |
| 276 | 306 | struct hash_parse_state *state = (struct hash_parse_state *) data; |
| r4160 | r4161 | |
| 294 | 324 | |
| 295 | 325 | |
| 296 | 326 | |
| 327 | /*------------------------------------------------- |
| 328 | hashfile_parse |
| 329 | -------------------------------------------------*/ |
| 330 | |
| 297 | 331 | static void hashfile_parse(hash_file *hashfile, |
| 298 | 332 | int (*selector_proc)(hash_file *hashfile, void *param, const char *name, const char *hash), |
| 299 | 333 | void (*use_proc)(hash_file *hashfile, void *param, hash_info *hi), |
| r4160 | r4161 | |
| 347 | 381 | |
| 348 | 382 | |
| 349 | 383 | |
| 350 | | /* ----------------------------------------------------------------------- */ |
| 384 | /*------------------------------------------------- |
| 385 | preload_use_proc |
| 386 | -------------------------------------------------*/ |
| 351 | 387 | |
| 352 | 388 | static void preload_use_proc(hash_file *hashfile, void *param, hash_info *hi) |
| 353 | 389 | { |
| r4160 | r4161 | |
| 364 | 400 | |
| 365 | 401 | |
| 366 | 402 | |
| 403 | /*------------------------------------------------- |
| 404 | hashfile_open_options |
| 405 | -------------------------------------------------*/ |
| 406 | |
| 367 | 407 | hash_file *hashfile_open_options(core_options *opts, const char *sysname, int is_preload, |
| 368 | 408 | void (*error_proc)(const char *message)) |
| 369 | 409 | { |
| r4160 | r4161 | |
| 408 | 448 | |
| 409 | 449 | |
| 410 | 450 | |
| 451 | /*------------------------------------------------- |
| 452 | hashfile_open |
| 453 | -------------------------------------------------*/ |
| 454 | |
| 411 | 455 | hash_file *hashfile_open(const char *sysname, int is_preload, |
| 412 | 456 | void (*error_proc)(const char *message)) |
| 413 | 457 | { |
| r4160 | r4161 | |
| 416 | 460 | |
| 417 | 461 | |
| 418 | 462 | |
| 463 | /*------------------------------------------------- |
| 464 | hashfile_close |
| 465 | -------------------------------------------------*/ |
| 466 | |
| 419 | 467 | void hashfile_close(hash_file *hashfile) |
| 420 | 468 | { |
| 421 | 469 | if (hashfile->file) |
| r4160 | r4161 | |
| 425 | 473 | |
| 426 | 474 | |
| 427 | 475 | |
| 476 | /*------------------------------------------------- |
| 477 | singular_selector_proc |
| 478 | -------------------------------------------------*/ |
| 479 | |
| 428 | 480 | struct hashlookup_params |
| 429 | 481 | { |
| 430 | 482 | const char *hash; |
| 431 | 483 | hash_info *hi; |
| 432 | 484 | }; |
| 433 | 485 | |
| 434 | | |
| 435 | | |
| 436 | 486 | static int singular_selector_proc(hash_file *hashfile, void *param, const char *name, const char *hash) |
| 437 | 487 | { |
| 438 | 488 | struct hashlookup_params *hlparams = (struct hashlookup_params *) param; |
| r4160 | r4161 | |
| 442 | 492 | |
| 443 | 493 | |
| 444 | 494 | |
| 495 | /*------------------------------------------------- |
| 496 | singular_use_proc |
| 497 | -------------------------------------------------*/ |
| 498 | |
| 445 | 499 | static void singular_use_proc(hash_file *hashfile, void *param, hash_info *hi) |
| 446 | 500 | { |
| 447 | 501 | struct hashlookup_params *hlparams = (struct hashlookup_params *) param; |
| r4160 | r4161 | |
| 450 | 504 | |
| 451 | 505 | |
| 452 | 506 | |
| 507 | /*------------------------------------------------- |
| 508 | hashfile_lookup |
| 509 | -------------------------------------------------*/ |
| 510 | |
| 453 | 511 | const hash_info *hashfile_lookup(hash_file *hashfile, const char *hash) |
| 454 | 512 | { |
| 455 | 513 | struct hashlookup_params param; |
| r4160 | r4161 | |
| 471 | 529 | |
| 472 | 530 | |
| 473 | 531 | |
| 532 | /*------------------------------------------------- |
| 533 | hashfile_functions_used |
| 534 | -------------------------------------------------*/ |
| 535 | |
| 474 | 536 | unsigned int hashfile_functions_used(hash_file *hashfile, iodevice_t devtype) |
| 475 | 537 | { |
| 476 | 538 | assert(devtype >= 0); |
| r4160 | r4161 | |
| 480 | 542 | |
| 481 | 543 | |
| 482 | 544 | |
| 545 | /*------------------------------------------------- |
| 546 | hashfile_verify |
| 547 | -------------------------------------------------*/ |
| 548 | |
| 483 | 549 | int hashfile_verify(const char *sysname, void (*my_error_proc)(const char *message)) |
| 484 | 550 | { |
| 485 | 551 | hash_file *hashfile; |