Revert "chd.cpp, chdcodec.cpp: Minor refactoring"

This reverts commit 901a68e2e0.
This commit is contained in:
Vas Crabb 2023-10-27 06:35:20 +11:00
parent 4587e86e86
commit de955af966
5 changed files with 68 additions and 139 deletions

View File

@ -134,7 +134,7 @@ struct chd_file::metadata_hash
// stream in bigendian order // stream in bigendian order
//------------------------------------------------- //-------------------------------------------------
inline util::sha1_t chd_file::be_read_sha1(const uint8_t *base) const noexcept inline util::sha1_t chd_file::be_read_sha1(const uint8_t *base)const
{ {
util::sha1_t result; util::sha1_t result;
memcpy(&result.m_raw[0], base, sizeof(result.m_raw)); memcpy(&result.m_raw[0], base, sizeof(result.m_raw));
@ -147,7 +147,7 @@ inline util::sha1_t chd_file::be_read_sha1(const uint8_t *base) const noexcept
// stream in bigendian order // stream in bigendian order
//------------------------------------------------- //-------------------------------------------------
inline void chd_file::be_write_sha1(uint8_t *base, util::sha1_t value) noexcept inline void chd_file::be_write_sha1(uint8_t *base, util::sha1_t value)
{ {
memcpy(base, &value.m_raw[0], sizeof(value.m_raw)); memcpy(base, &value.m_raw[0], sizeof(value.m_raw));
} }
@ -338,7 +338,7 @@ bool chd_file::parent_missing() const noexcept
* @return A sha1_t. * @return A sha1_t.
*/ */
util::sha1_t chd_file::sha1() const util::sha1_t chd_file::sha1()
{ {
try try
{ {
@ -349,7 +349,7 @@ util::sha1_t chd_file::sha1() const
} }
catch (std::error_condition const &) catch (std::error_condition const &)
{ {
// on failure, return null // on failure, return nullptr
return util::sha1_t::null; return util::sha1_t::null;
} }
} }
@ -367,7 +367,7 @@ util::sha1_t chd_file::sha1() const
* @return A sha1_t. * @return A sha1_t.
*/ */
util::sha1_t chd_file::raw_sha1() const util::sha1_t chd_file::raw_sha1()
{ {
try try
{ {
@ -382,7 +382,7 @@ util::sha1_t chd_file::raw_sha1() const
} }
catch (std::error_condition const &) catch (std::error_condition const &)
{ {
// on failure, return null // on failure, return nullptr
return util::sha1_t::null; return util::sha1_t::null;
} }
} }
@ -400,7 +400,7 @@ util::sha1_t chd_file::raw_sha1() const
* @return A sha1_t. * @return A sha1_t.
*/ */
util::sha1_t chd_file::parent_sha1() const util::sha1_t chd_file::parent_sha1()
{ {
try try
{ {
@ -415,7 +415,7 @@ util::sha1_t chd_file::parent_sha1() const
} }
catch (std::error_condition const &) catch (std::error_condition const &)
{ {
// on failure, return null // on failure, return nullptr
return util::sha1_t::null; return util::sha1_t::null;
} }
} }
@ -532,45 +532,33 @@ std::error_condition chd_file::hunk_info(uint32_t hunknum, chd_codec_type &compr
} }
/** /**
* @fn std::error_condition chd_file::set_raw_sha1(sha1_t rawdata) * @fn void chd_file::set_raw_sha1(sha1_t rawdata)
* *
* @brief ------------------------------------------------- * @brief -------------------------------------------------
* set_raw_sha1 - set our SHA1 values * set_raw_sha1 - set our SHA1 values
* -------------------------------------------------. * -------------------------------------------------.
* *
* @param rawdata The rawdata. * @param rawdata The rawdata.
*
* @return A std::error_condition.
*/ */
std::error_condition chd_file::set_raw_sha1(util::sha1_t rawdata) void chd_file::set_raw_sha1(util::sha1_t rawdata)
{ {
// wrap this for clean reporting // create a big-endian version
try uint8_t rawbuf[sizeof(util::sha1_t)];
{ be_write_sha1(rawbuf, rawdata);
// create a big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
be_write_sha1(rawbuf, rawdata);
// write to the header // write to the header
uint64_t offset = (m_rawsha1_offset != 0) ? m_rawsha1_offset : m_sha1_offset; uint64_t offset = (m_rawsha1_offset != 0) ? m_rawsha1_offset : m_sha1_offset;
assert(offset != 0); assert(offset != 0);
file_write(offset, rawbuf, sizeof(rawbuf)); file_write(offset, rawbuf, sizeof(rawbuf));
// if we have a separate rawsha1_offset, update the full sha1 as well // if we have a separate rawsha1_offset, update the full sha1 as well
if (m_rawsha1_offset != 0) if (m_rawsha1_offset != 0)
metadata_update_hash(); metadata_update_hash();
return std::error_condition();
}
catch (std::error_condition const &err)
{
// return any errors
return err;
}
} }
/** /**
* @fn std::error_condition chd_file::set_parent_sha1(sha1_t parent) * @fn void chd_file::set_parent_sha1(sha1_t parent)
* *
* @brief ------------------------------------------------- * @brief -------------------------------------------------
* set_parent_sha1 - set the parent SHA1 value * set_parent_sha1 - set the parent SHA1 value
@ -579,33 +567,21 @@ std::error_condition chd_file::set_raw_sha1(util::sha1_t rawdata)
* @exception CHDERR_INVALID_FILE Thrown when a chderr invalid file error condition occurs. * @exception CHDERR_INVALID_FILE Thrown when a chderr invalid file error condition occurs.
* *
* @param parent The parent. * @param parent The parent.
*
* @return A std::error_condition.
*/ */
std::error_condition chd_file::set_parent_sha1(util::sha1_t parent) void chd_file::set_parent_sha1(util::sha1_t parent)
{ {
// if no file, fail // if no file, fail
if (!m_file) if (!m_file)
return error::INVALID_FILE; throw std::error_condition(error::INVALID_FILE);
// wrap this for clean reporting // create a big-endian version
try uint8_t rawbuf[sizeof(util::sha1_t)];
{ be_write_sha1(rawbuf, parent);
// create a big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
be_write_sha1(rawbuf, parent);
// write to the header // write to the header
assert(m_parentsha1_offset != 0); assert(m_parentsha1_offset != 0);
file_write(m_parentsha1_offset, rawbuf, sizeof(rawbuf)); file_write(m_parentsha1_offset, rawbuf, sizeof(rawbuf));
return std::error_condition();
}
catch (std::error_condition const &err)
{
// return any errors
return err;
}
} }
/** /**
@ -917,7 +893,7 @@ void chd_file::close()
* @param hunknum The hunknum. * @param hunknum The hunknum.
* @param [in,out] buffer If non-null, the buffer. * @param [in,out] buffer If non-null, the buffer.
* *
* @return A std::error_condition. * @return The hunk.
*/ */
std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer) std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer)
@ -994,12 +970,10 @@ std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer)
else if (m_parent_missing) else if (m_parent_missing)
throw std::error_condition(error::REQUIRES_PARENT); throw std::error_condition(error::REQUIRES_PARENT);
else if (m_parent) else if (m_parent)
return m_parent->read_hunk(hunknum, dest); m_parent->read_hunk(hunknum, dest);
else else
{
memset(dest, 0, m_hunkbytes); memset(dest, 0, m_hunkbytes);
return std::error_condition(); return std::error_condition();
}
} }
// compressed case // compressed case
@ -2623,7 +2597,7 @@ void chd_file::hunk_copy_from_self(uint32_t hunknum, uint32_t otherhunk)
// only permitted to reference prior hunks // only permitted to reference prior hunks
if (otherhunk >= hunknum) if (otherhunk >= hunknum)
throw std::error_condition(error::HUNK_OUT_OF_RANGE); throw std::error_condition(std::errc::invalid_argument);
// update the map entry // update the map entry
uint8_t *rawmap = &m_rawmap[hunknum * 12]; uint8_t *rawmap = &m_rawmap[hunknum * 12];
@ -2969,8 +2943,8 @@ std::error_condition chd_file_compressor::compress_continue(double &progress, do
// writes of all-0 data don't actually take space, so see if we count this // writes of all-0 data don't actually take space, so see if we count this
chd_codec_type codec = CHD_CODEC_NONE; chd_codec_type codec = CHD_CODEC_NONE;
uint32_t complen; uint32_t complen;
err = hunk_info(item.m_hunknum, codec, complen); hunk_info(item.m_hunknum, codec, complen);
if (!err && codec == CHD_CODEC_NONE) if (codec == CHD_CODEC_NONE)
m_total_out += m_hunkbytes; m_total_out += m_hunkbytes;
} }
@ -3023,14 +2997,10 @@ std::error_condition chd_file_compressor::compress_continue(double &progress, do
else else
{ {
osd_work_queue_wait(m_read_queue, 30 * osd_ticks_per_second()); osd_work_queue_wait(m_read_queue, 30 * osd_ticks_per_second());
std::error_condition err; if (!compressed())
if (compressed()) return std::error_condition();
{ set_raw_sha1(m_compsha1.finish());
err = set_raw_sha1(m_compsha1.finish()); return compress_v5_map();
if (!err)
err = compress_v5_map();
}
return err;
} }
} }
} }
@ -3195,9 +3165,7 @@ void chd_file_compressor::async_read()
uint8_t *curdest = dest; uint8_t *curdest = dest;
for (uint64_t curoffs = m_read_done_offset; curoffs < end_offset + 1; curoffs += hunk_bytes()) for (uint64_t curoffs = m_read_done_offset; curoffs < end_offset + 1; curoffs += hunk_bytes())
{ {
std::error_condition err = m_parent->read_hunk(curoffs / hunk_bytes(), curdest); m_parent->read_hunk(curoffs / hunk_bytes(), curdest);
if (err)
throw err;
curdest += hunk_bytes(); curdest += hunk_bytes();
} }
} }
@ -3346,69 +3314,34 @@ void chd_file_compressor::hashmap::add(uint64_t itemnum, util::crc16_t crc16, ut
bool chd_file::is_hd() const bool chd_file::is_hd() const
{ {
try metadata_entry metaentry;
{ return metadata_find(HARD_DISK_METADATA_TAG, 0, metaentry);
metadata_entry metaentry;
return metadata_find(HARD_DISK_METADATA_TAG, 0, metaentry);
}
catch (std::error_condition const &)
{
return false;
}
} }
bool chd_file::is_cd() const bool chd_file::is_cd() const
{ {
try metadata_entry metaentry;
{ return metadata_find(CDROM_OLD_METADATA_TAG, 0, metaentry)
metadata_entry metaentry; || metadata_find(CDROM_TRACK_METADATA_TAG, 0, metaentry)
return metadata_find(CDROM_OLD_METADATA_TAG, 0, metaentry) || metadata_find(CDROM_TRACK_METADATA2_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;
}
} }
bool chd_file::is_gd() const bool chd_file::is_gd() const
{ {
try metadata_entry metaentry;
{ return metadata_find(GDROM_OLD_METADATA_TAG, 0, metaentry)
metadata_entry metaentry; || metadata_find(GDROM_TRACK_METADATA_TAG, 0, 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;
}
} }
bool chd_file::is_dvd() const bool chd_file::is_dvd() const
{ {
try metadata_entry metaentry;
{ return metadata_find(DVD_METADATA_TAG, 0, metaentry);
metadata_entry metaentry;
return metadata_find(DVD_METADATA_TAG, 0, metaentry);
}
catch (std::error_condition const &)
{
return false;
}
} }
bool chd_file::is_av() const bool chd_file::is_av() const
{ {
try metadata_entry metaentry;
{ return metadata_find(AV_METADATA_TAG, 0, metaentry);
metadata_entry metaentry;
return metadata_find(AV_METADATA_TAG, 0, metaentry);
}
catch (std::error_condition const &)
{
return false;
}
} }

View File

@ -309,18 +309,18 @@ public:
uint32_t hunk_count() const noexcept { return m_hunkcount; } uint32_t hunk_count() const noexcept { return m_hunkcount; }
uint32_t unit_bytes() const noexcept { return m_unitbytes; } uint32_t unit_bytes() const noexcept { return m_unitbytes; }
uint64_t unit_count() const noexcept { return m_unitcount; } uint64_t unit_count() const noexcept { return m_unitcount; }
bool compressed() const noexcept { return (m_compression[0] != CHD_CODEC_NONE); } bool compressed() const { return (m_compression[0] != CHD_CODEC_NONE); }
chd_codec_type compression(int index) const noexcept { return m_compression[index]; } chd_codec_type compression(int index) const noexcept { return m_compression[index]; }
chd_file *parent() const noexcept { return m_parent.get(); } chd_file *parent() const noexcept { return m_parent.get(); }
bool parent_missing() const noexcept; bool parent_missing() const noexcept;
util::sha1_t sha1() const; util::sha1_t sha1();
util::sha1_t raw_sha1() const; util::sha1_t raw_sha1();
util::sha1_t parent_sha1() const; util::sha1_t parent_sha1();
std::error_condition hunk_info(uint32_t hunknum, chd_codec_type &compressor, uint32_t &compbytes); std::error_condition hunk_info(uint32_t hunknum, chd_codec_type &compressor, uint32_t &compbytes);
// setters // setters
std::error_condition set_raw_sha1(util::sha1_t rawdata); void set_raw_sha1(util::sha1_t rawdata);
std::error_condition set_parent_sha1(util::sha1_t parent); void set_parent_sha1(util::sha1_t parent);
// file create // file create
std::error_condition create(std::string_view filename, uint64_t logicalbytes, uint32_t hunkbytes, uint32_t unitbytes, chd_codec_type compression[4]); std::error_condition create(std::string_view filename, uint64_t logicalbytes, uint32_t hunkbytes, uint32_t unitbytes, chd_codec_type compression[4]);
@ -372,8 +372,8 @@ private:
struct metadata_hash; struct metadata_hash;
// inline helpers // inline helpers
util::sha1_t be_read_sha1(const uint8_t *base) const noexcept; util::sha1_t be_read_sha1(const uint8_t *base) const;
void be_write_sha1(uint8_t *base, util::sha1_t value) noexcept; void be_write_sha1(uint8_t *base, util::sha1_t value);
void file_read(uint64_t offset, void *dest, uint32_t length) const; void file_read(uint64_t offset, void *dest, uint32_t length) const;
void file_write(uint64_t offset, const void *source, uint32_t length); void file_write(uint64_t offset, const void *source, uint32_t length);
uint64_t file_append(const void *source, uint32_t length, uint32_t alignment = 0); uint64_t file_append(const void *source, uint32_t length, uint32_t alignment = 0);

View File

@ -512,7 +512,7 @@ const codec_entry f_codec_list[] =
// instance of the given type // instance of the given type
//------------------------------------------------- //-------------------------------------------------
const codec_entry *find_in_list(chd_codec_type type) noexcept const codec_entry *find_in_list(chd_codec_type type)
{ {
// find in the list and construct the class // find in the list and construct the class
for (auto & elem : f_codec_list) for (auto & elem : f_codec_list)
@ -627,7 +627,7 @@ chd_decompressor::ptr chd_codec_list::new_decompressor(chd_codec_type type, chd_
// corresponds to a supported codec // corresponds to a supported codec
//------------------------------------------------- //-------------------------------------------------
bool chd_codec_list::codec_exists(chd_codec_type type) noexcept bool chd_codec_list::codec_exists(chd_codec_type type)
{ {
// find in the list and construct the class // find in the list and construct the class
return bool(find_in_list(type)); return bool(find_in_list(type));
@ -639,7 +639,7 @@ bool chd_codec_list::codec_exists(chd_codec_type type) noexcept
// codec // codec
//------------------------------------------------- //-------------------------------------------------
const char *chd_codec_list::codec_name(chd_codec_type type) noexcept const char *chd_codec_list::codec_name(chd_codec_type type)
{ {
// find in the list and construct the class // find in the list and construct the class
const codec_entry *entry = find_in_list(type); const codec_entry *entry = find_in_list(type);

View File

@ -104,8 +104,8 @@ public:
static chd_decompressor::ptr new_decompressor(chd_codec_type type, chd_file &file); static chd_decompressor::ptr new_decompressor(chd_codec_type type, chd_file &file);
// utilities // utilities
static bool codec_exists(chd_codec_type type) noexcept; static bool codec_exists(chd_codec_type type);
static const char *codec_name(chd_codec_type type) noexcept; static const char *codec_name(chd_codec_type type);
}; };

View File

@ -1660,9 +1660,7 @@ static void do_verify(parameters_map &params)
// fix it if requested; this also fixes the overall one so we don't need to do any more // fix it if requested; this also fixes the overall one so we don't need to do any more
if (params.find(OPTION_FIX) != params.end()) if (params.find(OPTION_FIX) != params.end())
{ {
std::error_condition err = input_chd.set_raw_sha1(computed_sha1); input_chd.set_raw_sha1(computed_sha1);
if (err)
report_error(1, "Error updating SHA-1: %s", err.message());
printf("SHA-1 updated to correct value in input CHD\n"); printf("SHA-1 updated to correct value in input CHD\n");
} }
} }
@ -1684,9 +1682,7 @@ static void do_verify(parameters_map &params)
// fix it if requested // fix it if requested
if (params.find(OPTION_FIX) != params.end()) if (params.find(OPTION_FIX) != params.end())
{ {
std::error_condition err = input_chd.set_raw_sha1(computed_sha1); input_chd.set_raw_sha1(computed_sha1);
if (err)
report_error(1, "Error updating SHA-1: %s", err.message());
printf("SHA-1 updated to correct value in input CHD\n"); printf("SHA-1 updated to correct value in input CHD\n");
} }
} }