Skip to content

Commit

Permalink
Allow whitespace at start/end of hash values.
Browse files Browse the repository at this point in the history
  • Loading branch information
0-wiz-0 committed Dec 12, 2023
1 parent 0e825d3 commit 6dc3c76
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
9 changes: 9 additions & 0 deletions regress/mamedb-whitespace.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
>>> table dat (dat_idx, name, description, author, version)
0|M.A.M.E.|<null>|<null>|1
>>> table file (game_id, file_type, file_idx, name, merge, status, location, size, crc, md5, sha1)
1|0|0|parent-1|<null>|0|0|8|305419896|<null>|<1234567890123456789012345678901234567890>
1|0|1|parent-2|<null>|0|0|8|2427178479|<null>|<2345678901234567890123456789012345678901>
>>> table game (game_id, name, parent, description, dat_idx)
1|parent|<null>|Parent|0
>>> table rule (rule_idx, start_offset, end_offset, operation)
>>> table test (rule_idx, test_idx, type, offset, size, mask, value, result)
20 changes: 20 additions & 0 deletions regress/mamedb-whitespace.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<mame build="1" debug="no" mameconfig="10">
<game name="parent" sourcefile="parent.c">
<description>Parent</description>
<year>2014</year>
<manufacturer>Made Up Games</manufacturer>
<rom name="parent-1" size="
8
" crc="
12345678
" sha1="
1234567890123456789012345678901234567890
" region="
maincpu
" offset="
8000
"/>
<rom name="parent-2" size="8" crc="90abcdef" sha1="2345678901234567890123456789012345678901" region="maincpu" offset="a000"/>
</game>
</mame>
2 changes: 1 addition & 1 deletion regress/mkmamedb-broken-sha1.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions regress/mkmamedb-whitespace.test
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion src/Hashes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6dc3c76

Please sign in to comment.