From 6dc3c76d37ee30f3be8561a7a4b08f42f300b92e Mon Sep 17 00:00:00 2001 From: Thomas Klausner Date: Tue, 12 Dec 2023 08:34:14 +0100 Subject: [PATCH] Allow whitespace at start/end of hash values. --- regress/mamedb-whitespace.dump | 9 +++++++++ regress/mamedb-whitespace.xml | 20 ++++++++++++++++++++ regress/mkmamedb-broken-sha1.test | 2 +- regress/mkmamedb-whitespace.test | 6 ++++++ src/Hashes.cc | 12 +++++++++++- src/Parser.cc | 2 +- 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 regress/mamedb-whitespace.dump create mode 100644 regress/mamedb-whitespace.xml create mode 100644 regress/mkmamedb-whitespace.test diff --git a/regress/mamedb-whitespace.dump b/regress/mamedb-whitespace.dump new file mode 100644 index 00000000..0d117691 --- /dev/null +++ b/regress/mamedb-whitespace.dump @@ -0,0 +1,9 @@ +>>> table dat (dat_idx, name, description, author, version) +0|M.A.M.E.|||1 +>>> table file (game_id, file_type, file_idx, name, merge, status, location, size, crc, md5, sha1) +1|0|0|parent-1||0|0|8|305419896||<1234567890123456789012345678901234567890> +1|0|1|parent-2||0|0|8|2427178479||<2345678901234567890123456789012345678901> +>>> table game (game_id, name, parent, description, dat_idx) +1|parent||Parent|0 +>>> table rule (rule_idx, start_offset, end_offset, operation) +>>> table test (rule_idx, test_idx, type, offset, size, mask, value, result) diff --git a/regress/mamedb-whitespace.xml b/regress/mamedb-whitespace.xml new file mode 100644 index 00000000..03f9e33e --- /dev/null +++ b/regress/mamedb-whitespace.xml @@ -0,0 +1,20 @@ + + + + Parent + 2014 + Made Up Games + + + + diff --git a/regress/mkmamedb-broken-sha1.test b/regress/mkmamedb-broken-sha1.test index 1d71b712..e44ccef9 100644 --- a/regress/mkmamedb-broken-sha1.test +++ b/regress/mkmamedb-broken-sha1.test @@ -4,5 +4,5 @@ program mkmamedb args -o mamedb-test.db mamedb.dat file mamedb.dat mamedb-broken-sha1.dat mamedb-broken-sha1.dat stderr-data -mamedb.dat:11: invalid argument for sha1 +mamedb.dat:11: invalid argument for sha1: '1234567890' end-of-data diff --git a/regress/mkmamedb-whitespace.test b/regress/mkmamedb-whitespace.test new file mode 100644 index 00000000..9857d56e --- /dev/null +++ b/regress/mkmamedb-whitespace.test @@ -0,0 +1,6 @@ +description game has data with leading/trailing whitespace inside the quotes +return 0 +program mkmamedb +args -o mamedb-test.db mamedb.xml +file mamedb.xml mamedb-whitespace.xml mamedb-whitespace.xml +file-new mamedb-test.db mamedb-whitespace.dump diff --git a/src/Hashes.cc b/src/Hashes.cc index 3ee36af3..f27b9994 100644 --- a/src/Hashes.cc +++ b/src/Hashes.cc @@ -361,9 +361,19 @@ std::string Hashes::to_string(int type) const { } -int Hashes::set_from_string(const std::string &s) { +int Hashes::set_from_string(const std::string& s) { auto str = s; + /* remove leading & trailing whitespace */ + auto data_start = str.find_first_not_of(" \t\n\r"); + if (data_start != std::string::npos) { + str = str.substr(data_start); + } + auto data_end = str.find_last_not_of(" \t\n\r"); + if (data_end != std::string::npos) { + str = str.substr(0, data_end + 1); + } + if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { str = str.substr(2); } diff --git a/src/Parser.cc b/src/Parser.cc index 9d4739ac..6b57a1be 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -233,7 +233,7 @@ bool Parser::file_hash(filetype_t ft, int ht, const std::string& attr) { h = &r[ft]->hashes; if (h->set_from_string(attr) != ht) { - output.line_error(lineno, "invalid argument for %s", Hashes::type_name(ht).c_str()); + output.line_error(lineno, "invalid argument for %s: '%s'", Hashes::type_name(ht).c_str(), attr.c_str()); error = true; return false; }