diff options
| author | louist103 <35883445+louist103@users.noreply.github.com> | 2023-04-29 22:02:24 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-29 22:02:24 -0400 |
| commit | cb6da2e5a70f22df309268d998b89869851f4a2d (patch) | |
| tree | f2dea6f44350763f55fcc1a8c88b12a74a3467c1 | |
| parent | 546f01cbfe2f1d7ed7f3e69f967ad84c10e7b10b (diff) | |
Better checks for hex digits in XMLs (#279)
* Better checking
* cleanup logic and explain with a better comment
* cleanups
* last cleanup
* the last one for real
| -rw-r--r-- | ZAPD/ZFile.cpp | 8 | ||||
| -rw-r--r-- | ZAPD/ZRoom/ZNames.h | 12 | ||||
| -rw-r--r-- | ZAPDUtils/Utils/StringHelper.h | 30 |
3 files changed, 39 insertions, 11 deletions
diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp index 0977650..ad56f98 100644 --- a/ZAPD/ZFile.cpp +++ b/ZAPD/ZFile.cpp @@ -213,14 +213,12 @@ void ZFile::ParseXML(tinyxml2::XMLElement* reader, const std::string& filename) // Check for repeated attributes. if (offsetXml != nullptr) { - std::string offsetStr = StringHelper::Split(offsetXml, "0x")[1]; - if (!StringHelper::HasOnlyHexDigits(offsetStr)) + if (!StringHelper::IsValidOffset(std::string_view(offsetXml))) { HANDLE_ERROR(WarningType::InvalidXML, - StringHelper::Sprintf("Invalid offset %s entered", offsetStr.c_str()), - ""); + StringHelper::Sprintf("Invalid offset %s entered", offsetXml), ""); } - rawDataIndex = strtol(offsetStr.c_str(), NULL, 16); + rawDataIndex = strtol(offsetXml, NULL, 16); if (offsetSet.find(offsetXml) != offsetSet.end()) { diff --git a/ZAPD/ZRoom/ZNames.h b/ZAPD/ZRoom/ZNames.h index 667407c..83c217e 100644 --- a/ZAPD/ZRoom/ZNames.h +++ b/ZAPD/ZRoom/ZNames.h @@ -48,11 +48,12 @@ public: static std::string GetEntranceName(uint16_t id) { if (ZNames::GetNumEntrances() == 0 || ZNames::GetNumSpecialEntrances() == 0) - return StringHelper::Sprintf("0x%04X", id); - + return StringHelper::Sprintf("0x%04X", id); + if (id < ZNames::GetNumEntrances()) return Globals::Instance->cfg.entranceList[id]; - else if ((id >= 0x7FF9 && id <= 0x7FFF) && !((id - 0x7FF9U) > GetNumSpecialEntrances())) // Special entrances + else if ((id >= 0x7FF9 && id <= 0x7FFF) && + !((id - 0x7FF9U) > GetNumSpecialEntrances())) // Special entrances return Globals::Instance->cfg.specialEntranceList[id - 0x7FF9]; else return StringHelper::Sprintf("0x%04X", id); @@ -60,5 +61,8 @@ public: static size_t GetNumActors() { return Globals::Instance->cfg.actorList.size(); } static size_t GetNumEntrances() { return Globals::Instance->cfg.entranceList.size(); } - static size_t GetNumSpecialEntrances() { return Globals::Instance->cfg.specialEntranceList.size(); } + static size_t GetNumSpecialEntrances() + { + return Globals::Instance->cfg.specialEntranceList.size(); + } }; diff --git a/ZAPDUtils/Utils/StringHelper.h b/ZAPDUtils/Utils/StringHelper.h index 8560d6b..3d32b03 100644 --- a/ZAPDUtils/Utils/StringHelper.h +++ b/ZAPDUtils/Utils/StringHelper.h @@ -106,9 +106,35 @@ public: return std::all_of(str.begin(), str.end(), ::isdigit); } - static bool HasOnlyHexDigits(const std::string& str) + static bool IsValidHex(std::string_view str) { - return std::all_of(str.begin(), str.end(), ::isxdigit); + if (str.length() < 3) + { + return false; + } + + if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) + { + return std::all_of(str.begin() + 2, str.end(), ::isxdigit); + } + + return false; + } + + static bool IsValidOffset(std::string_view str) + { + if (str.length() == 1) + { + // 0 is a valid offset + return isdigit(str[0]); + } + + return IsValidHex(str); + } + + static bool IsValidHex(const std::string& str) + { + return IsValidHex(std::string_view(str.c_str())); } static std::string ToUpper(const std::string& str) |
