int chd_create_ref(void *ref, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent) { char filename[ENCODED_IMAGE_REF_LEN]; encode_ptr(ref, filename); return chd_create(filename, logicalbytes, hunkbytes, compression, parent); }
static void process_disk_entries(rom_load_data *romdata, const rom_entry *romp) { /* loop until we hit the end of this region */ while (!ROMENTRY_ISREGIONEND(romp)) { /* handle files */ if (ROMENTRY_ISFILE(romp)) { chd_file *source, *diff = NULL; chd_header header; char filename[1024]; char acthash[HASH_BUF_SIZE]; chd_error err; /* make the filename of the source */ sprintf(filename, "%s.chd", ROM_GETNAME(romp)); /* first open the source drive */ debugload("Opening disk image: %s\n", filename); err = open_disk_image(Machine->gamedrv, romp, &source); if (err != CHDERR_NONE) { if (err == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s NOT FOUND\n", filename); /* if this is NO_DUMP, keep going, though the system may not be able to handle it */ if (hash_data_has_info(ROM_GETHASHDATA(romp), HASH_INFO_NO_DUMP)) romdata->warnings++; else romdata->errors++; romp++; continue; } /* get the header and extract the MD5/SHA1 */ header = *chd_get_header(source); hash_data_clear(acthash); hash_data_insert_binary_checksum(acthash, HASH_MD5, header.md5); hash_data_insert_binary_checksum(acthash, HASH_SHA1, header.sha1); /* verify the MD5 */ if (!hash_data_is_equal(ROM_GETHASHDATA(romp), acthash, 0)) { sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s WRONG CHECKSUMS:\n", filename); dump_wrong_and_correct_checksums(romdata, ROM_GETHASHDATA(romp), acthash); romdata->warnings++; } else if (hash_data_has_info(ROM_GETHASHDATA(romp), HASH_INFO_BAD_DUMP)) { sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s CHD NEEDS REDUMP\n", filename); romdata->warnings++; } /* if not read-only, make the diff file */ if (!DISK_ISREADONLY(romp)) { /* make the filename of the diff */ sprintf(filename, "%s.dif", ROM_GETNAME(romp)); /* try to open the diff */ debugload("Opening differencing image: %s\n", filename); err = chd_open(filename, CHD_OPEN_READWRITE, source, &diff); if (err != CHDERR_NONE) { /* didn't work; try creating it instead */ debugload("Creating differencing image: %s\n", filename); err = chd_create(filename, 0, 0, CHDCOMPRESSION_NONE, source); if (err != CHDERR_NONE) { if (err == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s: CAN'T CREATE DIFF FILE\n", filename); romdata->errors++; romp++; continue; } /* open the newly-created diff file */ debugload("Opening differencing image: %s\n", filename); err = chd_open(filename, CHD_OPEN_READWRITE, source, &diff); if (err != CHDERR_NONE) { if (err == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%s: CAN'T OPEN DIFF FILE\n", filename); romdata->errors++; romp++; continue; } } } /* we're okay, set the handle */ debugload("Assigning to handle %d\n", DISK_GETINDEX(romp)); disk_handle[DISK_GETINDEX(romp)] = DISK_ISREADONLY(romp) ? source : diff; romp++; } } }
static int process_disk_entries(struct rom_load_data *romdata, const struct RomModule *romp) { /* loop until we hit the end of this region */ while (!ROMENTRY_ISREGIONEND(romp)) { /* handle files */ if (ROMENTRY_ISFILE(romp)) { struct chd_file *source, *diff = NULL; struct chd_header header; char filename[1024], *c; char acthash[HASH_BUF_SIZE]; int err; /* make the filename of the source */ strcpy(filename, ROM_GETNAME(romp)); c = strrchr(filename, '.'); if (c) strcpy(c, ".chd"); else strcat(filename, ".chd"); /* first open the source drive */ debugload("Opening disk image: %s\n", filename); source = chd_open(filename, 0, NULL); if (!source) { if (chd_get_last_error() == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s NOT FOUND\n", filename); romdata->errors++; romp++; continue; } /* get the header and extract the MD5/SHA1 */ header = *chd_get_header(source); hash_data_clear(acthash); hash_data_insert_binary_checksum(acthash, HASH_MD5, header.md5); hash_data_insert_binary_checksum(acthash, HASH_SHA1, header.sha1); /* verify the MD5 */ if (!hash_data_is_equal(ROM_GETHASHDATA(romp), acthash, 0)) { sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s WRONG CHECKSUMS:\n", filename); dump_wrong_and_correct_checksums(romdata, ROM_GETHASHDATA(romp), acthash); romdata->warnings++; } /* if not read-only, make the diff file */ if (!DISK_ISREADONLY(romp)) { /* make the filename of the diff */ strcpy(filename, ROM_GETNAME(romp)); c = strrchr(filename, '.'); if (c) strcpy(c, ".dif"); else strcat(filename, ".dif"); /* try to open the diff */ debugload("Opening differencing image: %s\n", filename); diff = chd_open(filename, 1, source); if (!diff) { /* didn't work; try creating it instead */ debugload("Creating differencing image: %s\n", filename); err = chd_create(filename, 0, 0, CHDCOMPRESSION_NONE, source); if (err != CHDERR_NONE) { if (chd_get_last_error() == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s: CAN'T CREATE DIFF FILE\n", filename); romdata->errors++; romp++; continue; } /* open the newly-created diff file */ debugload("Opening differencing image: %s\n", filename); diff = chd_open(filename, 1, source); if (!diff) { if (chd_get_last_error() == CHDERR_UNSUPPORTED_VERSION) sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s UNSUPPORTED CHD VERSION\n", filename); else sprintf(&romdata->errorbuf[strlen(romdata->errorbuf)], "%-12s: CAN'T OPEN DIFF FILE\n", filename); romdata->errors++; romp++; continue; } } } /* we're okay, set the handle */ debugload("Assigning to handle %d\n", DISK_GETINDEX(romp)); disk_handle[DISK_GETINDEX(romp)] = DISK_ISREADONLY(romp) ? source : diff; romp++; } } return 1; }
/* imghd_create() Create a MAME HD image */ imgtoolerr_t imghd_create(imgtool_stream *stream, UINT32 hunksize, UINT32 cylinders, UINT32 heads, UINT32 sectors, UINT32 seclen) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; char encoded_image_ref[encoded_image_ref_max_len]; chd_interface interface_save; UINT8 *cache = NULL; chd_file *chd = NULL; int rc; UINT64 logicalbytes; int hunknum, totalhunks; char metadata[256]; /* jump through the hoops as required by the CHD system */ encode_image_ref(stream, encoded_image_ref); chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); /* sanity check args */ if (hunksize >= 2048) { err = IMGTOOLERR_PARAMCORRUPT; goto done; } if (hunksize <= 0) hunksize = 1024; /* default value */ /* bail if we are read only */ if (stream_isreadonly(stream)) { err = IMGTOOLERR_READONLY; goto done; } /* calculations */ logicalbytes = (UINT64)cylinders * heads * sectors * seclen; /* create the new hard drive */ rc = chd_create(encoded_image_ref, logicalbytes, hunksize, CHDCOMPRESSION_NONE, NULL); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* open the new hard drive */ rc = chd_open(encoded_image_ref, CHD_OPEN_READWRITE, NULL, &chd); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* write the metadata */ sprintf(metadata, HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, seclen); err = chd_set_metadata(chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* alloc and zero buffer */ cache = malloc(hunksize); if (!cache) { err = IMGTOOLERR_OUTOFMEMORY; goto done; } memset(cache, '\0', hunksize); /* zero out every hunk */ totalhunks = (logicalbytes + hunksize - 1) / hunksize; for (hunknum = 0; hunknum < totalhunks; hunknum++) { rc = chd_write(chd, hunknum, cache); if (rc) { err = IMGTOOLERR_WRITEERROR; goto done; } } done: if (cache) free(cache); if (chd) chd_close(chd); chd_set_interface(&interface_save); return err; }