Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rewrite illustration check to not use regex #358

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ const bool OPTIONAL = false;
const std::string LANGS_REGEXP = "\\w{3}(,\\w{3})*";
const std::string DATE_REGEXP = R"(\d\d\d\d-\d\d-\d\d)";

// PNG regexp has to be defined in such a tricky way because it includes
// a NUL character
const char PNG_REGEXP_DATA[] = "^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a(.|\\s|\0)+";
const std::string PNG_REGEXP(PNG_REGEXP_DATA, sizeof(PNG_REGEXP_DATA)-1);

bool matchRegex(const std::string& regexStr, const std::string& text)
{
const std::regex regex(regexStr);
Expand Down
26 changes: 25 additions & 1 deletion src/metadata_constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Metadata::ReservedMetadataTable reservedMetadataInfoTable = {
MANDATORY,
0, // There are no constraints on the illustration metadata size
0, // in order to avoid decoding it as UTF-8 encoded text
PNG_REGEXP
""
},
};

Expand All @@ -30,3 +30,27 @@ METADATA_ASSERT("LongDescription shouldn't be shorter than Description")
return !data.has("LongDescription")
|| data["LongDescription"].size() >= data["Description"].size();
}

METADATA_ASSERT("Illustration must exist and be a 48x48 PNG file")
{
if (!data.has("Illustration_48x48@1")) {
return false;
}

const std::string& illustration = data["Illustration_48x48@1"];

if (illustration.length() >= 24) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this test needed? What's wrong with always (unconditionally) testing the first 8 bytes?

if (!(illustration.substr(0, 8) == "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a")) {
return false;
}
}

// In PNG file, 16-20 is width and 20-24 is height in big endian order.
const std::string png48x48 = {0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30};

if (illustration.substr(16, 8) != png48x48) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

illustration.substr(16, 8) will throw if illustration.length() < 16

return false;
};

return true;
}