From 4587e86e8692e0b51a4afae3b67e49329268cfb7 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Fri, 27 Oct 2023 06:35:17 +1100 Subject: Revert "chd.cpp: Refactoring, part 2" This reverts commit cc772072fa635146b1df39a5694d2a8f8aa5a34f. --- src/devices/imagedev/cdromimg.cpp | 34 ++---- src/lib/util/chd.cpp | 247 ++++++++++++++++++++------------------ src/lib/util/chd.h | 12 +- src/lib/util/dvdrom.cpp | 2 +- 4 files changed, 146 insertions(+), 149 deletions(-) diff --git a/src/devices/imagedev/cdromimg.cpp b/src/devices/imagedev/cdromimg.cpp index e5e0bc2b9e6..355314a8285 100644 --- a/src/devices/imagedev/cdromimg.cpp +++ b/src/devices/imagedev/cdromimg.cpp @@ -79,10 +79,9 @@ void cdrom_image_device::setup_current_preset_image() m_dvdrom_handle.reset(); chd_file *chd = current_preset_image_chd(); - - if (!chd->check_is_cd() || (m_gd_compat && !chd->check_is_gd())) + if (chd->is_cd() || (m_gd_compat && chd->is_gd())) m_cdrom_handle = std::make_unique(chd); - else if(m_dvd_compat && !chd->check_is_dvd()) + else if(m_dvd_compat && chd->is_dvd()) m_dvdrom_handle = std::make_unique(chd); else fatalerror("chd for region %s is not compatible with the cdrom image device\n", preset_images_list()[current_preset_image_id()]); @@ -127,31 +126,16 @@ std::pair cdrom_image_device::call_load() // open the CHD file if (chd) { - err = chd->check_is_cd(); - if (err == chd_file::error::METADATA_NOT_FOUND && m_gd_compat) - err = chd->check_is_gd(); - if (!err) - { + if (chd->is_cd() || (m_gd_compat && chd->is_gd())) m_cdrom_handle.reset(new cdrom_file(chd)); - return std::make_pair(std::error_condition(), std::string()); - } - if (err != chd_file::error::METADATA_NOT_FOUND) - goto error; - - if (m_dvd_compat) + else if(m_dvd_compat && chd->is_dvd()) + m_dvdrom_handle.reset(new dvdrom_file(chd)); + else { - err = chd->check_is_dvd(); - if (!err) - { - m_dvdrom_handle.reset(new dvdrom_file(chd)); - return std::make_pair(std::error_condition(), std::string()); - } - if (err != chd_file::error::METADATA_NOT_FOUND) - goto error; + err = image_error::UNSUPPORTED; + goto error; } - - err = image_error::UNSUPPORTED; - goto error; + m_cdrom_handle.reset(new cdrom_file(chd)); } else { diff --git a/src/lib/util/chd.cpp b/src/lib/util/chd.cpp index 52b61dbd289..ba96e3897a5 100644 --- a/src/lib/util/chd.cpp +++ b/src/lib/util/chd.cpp @@ -1292,19 +1292,19 @@ std::error_condition chd_file::write_bytes(uint64_t offset, const void *buffer, * @param searchindex The searchindex. * @param [in,out] output The output. * - * @return A std::error_condition. + * @return The metadata. */ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_t searchindex, std::string &output) { - // if we didn't find it, just return - metadata_entry metaentry; - if (std::error_condition err = metadata_find(searchtag, searchindex, metaentry)) - return err; - // wrap this for clean reporting try { + // if we didn't find it, just return + metadata_entry metaentry; + if (!metadata_find(searchtag, searchindex, metaentry)) + return std::error_condition(error::METADATA_NOT_FOUND); + // read the metadata output.assign(metaentry.length, '\0'); file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); @@ -1329,19 +1329,19 @@ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_ * @param searchindex The searchindex. * @param [in,out] output The output. * - * @return A std::error_condition. + * @return The metadata. */ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_t searchindex, std::vector &output) { - // if we didn't find it, just return - metadata_entry metaentry; - if (std::error_condition err = metadata_find(searchtag, searchindex, metaentry)) - return err; - // wrap this for clean reporting try { + // if we didn't find it, just return + metadata_entry metaentry; + if (!metadata_find(searchtag, searchindex, metaentry)) + throw std::error_condition(error::METADATA_NOT_FOUND); + // read the metadata output.resize(metaentry.length); file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); @@ -1368,19 +1368,19 @@ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_ * @param outputlen The outputlen. * @param [in,out] resultlen The resultlen. * - * @return A std::error_condition. + * @return The metadata. */ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_t searchindex, void *output, uint32_t outputlen, uint32_t &resultlen) { - // if we didn't find it, just return - metadata_entry metaentry; - if (std::error_condition err = metadata_find(searchtag, searchindex, metaentry)) - return err; - // wrap this for clean reporting try { + // if we didn't find it, just return + metadata_entry metaentry; + if (!metadata_find(searchtag, searchindex, metaentry)) + throw std::error_condition(error::METADATA_NOT_FOUND); + // read the metadata resultlen = metaentry.length; file_read(metaentry.offset + METADATA_HEADER_SIZE, output, std::min(outputlen, resultlen)); @@ -1407,19 +1407,19 @@ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_ * @param [in,out] resulttag The resulttag. * @param [in,out] resultflags The resultflags. * - * @return A std::error_condition. + * @return The metadata. */ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_t searchindex, std::vector &output, chd_metadata_tag &resulttag, uint8_t &resultflags) { - // if we didn't find it, just return - metadata_entry metaentry; - if (std::error_condition err = metadata_find(searchtag, searchindex, metaentry)) - return err; - // wrap this for clean reporting try { + // if we didn't find it, just return + metadata_entry metaentry; + if (!metadata_find(searchtag, searchindex, metaentry)) + throw std::error_condition(error::METADATA_NOT_FOUND); + // read the metadata output.resize(metaentry.length); file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); @@ -1462,8 +1462,7 @@ std::error_condition chd_file::write_metadata(chd_metadata_tag metatag, uint32_t // find the entry if it already exists metadata_entry metaentry; bool finished = false; - std::error_condition err = metadata_find(metatag, metaindex, metaentry); - if (!err) + if (metadata_find(metatag, metaindex, metaentry)) { // if the new data fits over the old data, just overwrite if (inputlen <= metaentry.length) @@ -1486,8 +1485,6 @@ std::error_condition chd_file::write_metadata(chd_metadata_tag metatag, uint32_t else metadata_set_previous_next(metaentry.prev, metaentry.next); } - else if (err != error::METADATA_NOT_FOUND) - throw err; // if not yet done, create a new entry and append if (!finished) @@ -1536,14 +1533,14 @@ std::error_condition chd_file::write_metadata(chd_metadata_tag metatag, uint32_t std::error_condition chd_file::delete_metadata(chd_metadata_tag metatag, uint32_t metaindex) { - // find the entry - metadata_entry metaentry; - if (std::error_condition err = metadata_find(metatag, metaindex, metaentry)) - return err; - // wrap this for clean reporting try { + // find the entry + metadata_entry metaentry; + if (!metadata_find(metatag, metaindex, metaentry)) + throw std::error_condition(error::METADATA_NOT_FOUND); + // point the previous to the next, unlinking us metadata_set_previous_next(metaentry.prev, metaentry.next); return std::error_condition(); @@ -1571,37 +1568,34 @@ std::error_condition chd_file::delete_metadata(chd_metadata_tag metatag, uint32_ std::error_condition chd_file::clone_all_metadata(chd_file &source) { - // iterate over metadata entries in the source - std::vector filedata; - metadata_entry metaentry; - metaentry.metatag = 0; - metaentry.length = 0; - metaentry.next = 0; - metaentry.flags = 0; - std::error_condition err; - for (err = source.metadata_find(CHDMETATAG_WILDCARD, 0, metaentry); !err; err = source.metadata_find(CHDMETATAG_WILDCARD, 0, metaentry, true)) + // wrap this for clean reporting + try { - // wrap this for clean reporting - try + // iterate over metadata entries in the source + std::vector filedata; + metadata_entry metaentry; + metaentry.metatag = 0; + metaentry.length = 0; + metaentry.next = 0; + metaentry.flags = 0; + for (bool has_data = source.metadata_find(CHDMETATAG_WILDCARD, 0, metaentry); has_data; has_data = source.metadata_find(CHDMETATAG_WILDCARD, 0, metaentry, true)) { // read the metadata item filedata.resize(metaentry.length); source.file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); + + // write it to the destination + std::error_condition err = write_metadata(metaentry.metatag, (uint32_t)-1, &filedata[0], metaentry.length, metaentry.flags); + if (err) + throw err; } - catch (std::error_condition const &filerr) - { - return filerr; - } - - // write it to the destination - err = write_metadata(metaentry.metatag, (uint32_t)-1, &filedata[0], metaentry.length, metaentry.flags); - if (err) - return err; - } - if (err == error::METADATA_NOT_FOUND) return std::error_condition(); - else + } + catch (std::error_condition const &err) + { + // return any errors return err; + } } /** @@ -1627,8 +1621,7 @@ util::sha1_t chd_file::compute_overall_sha1(util::sha1_t rawsha1) std::vector filedata; std::vector hasharray; metadata_entry metaentry; - std::error_condition err; - for (err = metadata_find(CHDMETATAG_WILDCARD, 0, metaentry); !err; err = metadata_find(CHDMETATAG_WILDCARD, 0, metaentry, true)) + for (bool has_data = metadata_find(CHDMETATAG_WILDCARD, 0, metaentry); has_data; has_data = metadata_find(CHDMETATAG_WILDCARD, 0, metaentry, true)) { // if not checksumming, continue if ((metaentry.flags & CHD_MDFLAGS_CHECKSUM) == 0) @@ -1644,8 +1637,6 @@ util::sha1_t chd_file::compute_overall_sha1(util::sha1_t rawsha1) hashentry.sha1 = util::sha1_creator::simple(&filedata[0], metaentry.length); hasharray.push_back(hashentry); } - if (err != error::METADATA_NOT_FOUND) - throw err; // sort the array if (!hasharray.empty()) @@ -2667,7 +2658,7 @@ void chd_file::hunk_copy_from_parent(uint32_t hunknum, uint64_t parentunit) } /** - * @fn std::error_condition chd_file::metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume) + * @fn bool chd_file::metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume) * * @brief ------------------------------------------------- * metadata_find - find a metadata entry @@ -2678,10 +2669,10 @@ void chd_file::hunk_copy_from_parent(uint32_t hunknum, uint64_t parentunit) * @param [in,out] metaentry The metaentry. * @param resume true to resume. * - * @return A std::error_condition (error::METADATA_NOT_FOUND if the search fails). + * @return true if it succeeds, false if it fails. */ -std::error_condition chd_file::metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume) const +bool chd_file::metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume) const { // start at the beginning unless we're resuming a previous search if (!resume) @@ -2695,40 +2686,31 @@ std::error_condition chd_file::metadata_find(chd_metadata_tag metatag, int32_t m metaentry.offset = metaentry.next; } - // wrap this for clean reporting - try + // loop until we run out of options + while (metaentry.offset != 0) { - // loop until we run out of options - while (metaentry.offset != 0) - { - // read the raw header - uint8_t raw_meta_header[METADATA_HEADER_SIZE]; - file_read(metaentry.offset, raw_meta_header, sizeof(raw_meta_header)); - - // extract the data - metaentry.metatag = get_u32be(&raw_meta_header[0]); - metaentry.flags = raw_meta_header[4]; - metaentry.length = get_u24be(&raw_meta_header[5]); - metaentry.next = get_u64be(&raw_meta_header[8]); - - // if we got a match, proceed - if (metatag == CHDMETATAG_WILDCARD || metaentry.metatag == metatag) - if (metaindex-- == 0) - return std::error_condition(); - - // no match, fetch the next link - metaentry.prev = metaentry.offset; - metaentry.offset = metaentry.next; - } + // read the raw header + uint8_t raw_meta_header[METADATA_HEADER_SIZE]; + file_read(metaentry.offset, raw_meta_header, sizeof(raw_meta_header)); - // if we get here, we didn't find it - return error::METADATA_NOT_FOUND; - } - catch (std::error_condition const &err) - { - // return any errors - return err; + // extract the data + metaentry.metatag = get_u32be(&raw_meta_header[0]); + metaentry.flags = raw_meta_header[4]; + metaentry.length = get_u24be(&raw_meta_header[5]); + metaentry.next = get_u64be(&raw_meta_header[8]); + + // if we got a match, proceed + if (metatag == CHDMETATAG_WILDCARD || metaentry.metatag == metatag) + if (metaindex-- == 0) + return true; + + // no match, fetch the next link + metaentry.prev = metaentry.offset; + metaentry.offset = metaentry.next; } + + // if we get here, we didn't find it + return false; } /** @@ -3362,40 +3344,71 @@ void chd_file_compressor::hashmap::add(uint64_t itemnum, util::crc16_t crc16, ut m_map[crc16] = entry; } -std::error_condition chd_file::check_is_hd() const +bool chd_file::is_hd() const { - metadata_entry metaentry; - return metadata_find(HARD_DISK_METADATA_TAG, 0, metaentry); + try + { + metadata_entry metaentry; + return metadata_find(HARD_DISK_METADATA_TAG, 0, metaentry); + } + catch (std::error_condition const &) + { + return false; + } } -std::error_condition chd_file::check_is_cd() const +bool chd_file::is_cd() const { - metadata_entry metaentry; - std::error_condition err = metadata_find(CDROM_OLD_METADATA_TAG, 0, metaentry); - if (err == error::METADATA_NOT_FOUND) - err = metadata_find(CDROM_TRACK_METADATA_TAG, 0, metaentry); - if (err == error::METADATA_NOT_FOUND) - err = metadata_find(CDROM_TRACK_METADATA2_TAG, 0, metaentry); - return err; + try + { + metadata_entry metaentry; + return metadata_find(CDROM_OLD_METADATA_TAG, 0, metaentry) + || metadata_find(CDROM_TRACK_METADATA_TAG, 0, metaentry) + || metadata_find(CDROM_TRACK_METADATA2_TAG, 0, metaentry); + } + catch (std::error_condition const &) + { + return false; + } } -std::error_condition chd_file::check_is_gd() const +bool chd_file::is_gd() const { - metadata_entry metaentry; - std::error_condition err = metadata_find(GDROM_OLD_METADATA_TAG, 0, metaentry); - if (err == error::METADATA_NOT_FOUND) - err = metadata_find(GDROM_TRACK_METADATA_TAG, 0, metaentry); - return err; + try + { + metadata_entry metaentry; + return metadata_find(GDROM_OLD_METADATA_TAG, 0, metaentry) + || metadata_find(GDROM_TRACK_METADATA_TAG, 0, metaentry); + } + catch (std::error_condition const &) + { + return false; + } } -std::error_condition chd_file::check_is_dvd() const +bool chd_file::is_dvd() const { - metadata_entry metaentry; - return metadata_find(DVD_METADATA_TAG, 0, metaentry); + try + { + metadata_entry metaentry; + return metadata_find(DVD_METADATA_TAG, 0, metaentry); + } + catch (std::error_condition const &) + { + return false; + } } -std::error_condition chd_file::check_is_av() const +bool chd_file::is_av() const { - metadata_entry metaentry; - return metadata_find(AV_METADATA_TAG, 0, metaentry); + try + { + metadata_entry metaentry; + return metadata_find(AV_METADATA_TAG, 0, metaentry); + } + catch (std::error_condition const &) + { + return false; + } } + diff --git a/src/lib/util/chd.h b/src/lib/util/chd.h index 7ec89fc68e4..be9f94aa341 100644 --- a/src/lib/util/chd.h +++ b/src/lib/util/chd.h @@ -361,11 +361,11 @@ public: std::error_condition codec_configure(chd_codec_type codec, int param, void *config); // typing - std::error_condition check_is_hd() const; - std::error_condition check_is_cd() const; - std::error_condition check_is_gd() const; - std::error_condition check_is_dvd() const; - std::error_condition check_is_av() const; + bool is_hd() const; + bool is_cd() const; + bool is_gd() const; + bool is_dvd() const; + bool is_av() const; private: struct metadata_entry; @@ -393,7 +393,7 @@ private: void hunk_write_compressed(uint32_t hunknum, int8_t compression, const uint8_t *compressed, uint32_t complength, util::crc16_t crc16); void hunk_copy_from_self(uint32_t hunknum, uint32_t otherhunk); void hunk_copy_from_parent(uint32_t hunknum, uint64_t parentunit); - std::error_condition metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume = false) const; + bool metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume = false) const; void metadata_set_previous_next(uint64_t prevoffset, uint64_t nextoffset); void metadata_update_hash(); static int CLIB_DECL metadata_hash_compare(const void *elem1, const void *elem2); diff --git a/src/lib/util/dvdrom.cpp b/src/lib/util/dvdrom.cpp index 3a19291b16f..36dc29e7215 100644 --- a/src/lib/util/dvdrom.cpp +++ b/src/lib/util/dvdrom.cpp @@ -58,7 +58,7 @@ dvdrom_file::dvdrom_file(chd_file *_chd) throw nullptr; /* check it's actually a DVD-ROM */ - if (chd->check_is_dvd()) + if (!chd->is_dvd()) throw nullptr; sector_count = chd->unit_count(); -- cgit v1.2.3