summaryrefslogtreecommitdiff
path: root/ZAPD/ZFile.cpp
diff options
context:
space:
mode:
authorAnghelo Carvajal <angheloalf95@gmail.com>2021-04-22 14:54:23 -0400
committerGitHub <noreply@github.com>2021-04-22 14:54:23 -0400
commitb0d98ff1bbda9fa25515a28bcf0c36314156c66c (patch)
treefb72d1baab60c2e16dae5399be023debeba4a3a4 /ZAPD/ZFile.cpp
parente06757c87e317e6bb102bd39fbe4cdcfed277fa9 (diff)
Add a few errors checks (#119)
* Check for repeated names, outnames and offsets Signed-off-by: angie <angheloalf95@gmail.com> * Check for File inside File Signed-off-by: angie <angheloalf95@gmail.com> * Error if a ZResource node has a child Signed-off-by: angie <angheloalf95@gmail.com> * Error if an invalid ZResource name is included in the XML Signed-off-by: angie <angheloalf95@gmail.com> * error when a ZResource is not in a ZFile Signed-off-by: angie <angheloalf95@gmail.com> * ups Signed-off-by: angie <angheloalf95@gmail.com> * Add error if unknown element is found and other fixes * Run format
Diffstat (limited to 'ZAPD/ZFile.cpp')
-rw-r--r--ZAPD/ZFile.cpp56
1 files changed, 52 insertions, 4 deletions
diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp
index ca7f6bd..79a0a4d 100644
--- a/ZAPD/ZFile.cpp
+++ b/ZAPD/ZFile.cpp
@@ -1,6 +1,7 @@
#include "ZFile.h"
#include <algorithm>
#include <cassert>
+#include <unordered_set>
#include "Directory.h"
#include "File.h"
#include "Globals.h"
@@ -125,18 +126,55 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
rawData = File::ReadAllBytes(basePath + "/" + name);
}
+ std::unordered_set<std::string> nameSet;
+ std::unordered_set<std::string> outNameSet;
+ std::unordered_set<std::string> offsetSet;
+
auto nodeMap = *GetNodeMap();
int rawDataIndex = 0;
for (XMLElement* child = reader->FirstChildElement(); child != NULL;
child = child->NextSiblingElement())
{
- if (child->Attribute("Offset") != NULL)
- rawDataIndex =
- strtol(StringHelper::Split(child->Attribute("Offset"), "0x")[1].c_str(), NULL, 16);
+ const char* nameXml = child->Attribute("Name");
+ const char* outNameXml = child->Attribute("OutName");
+ const char* offsetXml = child->Attribute("Offset");
if (Globals::Instance->verbosity >= VERBOSITY_INFO)
- printf("%s: 0x%06X\n", child->Attribute("Name"), rawDataIndex);
+ printf("%s: 0x%06X\n", nameXml, rawDataIndex);
+
+ if (offsetXml != NULL)
+ {
+ rawDataIndex = strtol(StringHelper::Split(offsetXml, "0x")[1].c_str(), NULL, 16);
+
+ if (offsetSet.find(offsetXml) != offsetSet.end())
+ {
+ throw std::runtime_error(StringHelper::Sprintf(
+ "ZFile::ParseXML: Error in '%s'.\n\t Repeated 'Offset' attribute: %s \n",
+ name.c_str(), offsetXml));
+ }
+ offsetSet.insert(offsetXml);
+ }
+ if (outNameXml != NULL)
+ {
+ if (outNameSet.find(outNameXml) != outNameSet.end())
+ {
+ throw std::runtime_error(StringHelper::Sprintf(
+ "ZFile::ParseXML: Error in '%s'.\n\t Repeated 'OutName' attribute: %s \n",
+ name.c_str(), outNameXml));
+ }
+ outNameSet.insert(outNameXml);
+ }
+ if (nameXml != NULL)
+ {
+ if (nameSet.find(nameXml) != nameSet.end())
+ {
+ throw std::runtime_error(StringHelper::Sprintf(
+ "ZFile::ParseXML: Error in '%s'.\n\t Repeated 'Name' attribute: %s \n",
+ name.c_str(), nameXml));
+ }
+ nameSet.insert(nameXml);
+ }
string nodeName = string(child->Name());
@@ -173,8 +211,18 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
resources.push_back(nRes);
rawDataIndex += nRes->GetRawDataSize();
}
+ else if (string(child->Name()) == "File")
+ {
+ throw std::runtime_error(StringHelper::Sprintf(
+ "ZFile::ParseXML: Error in '%s'.\n\t Can't declare a File inside a File.\n",
+ name.c_str()));
+ }
else
{
+ throw std::runtime_error(
+ StringHelper::Sprintf("ZFile::ParseXML: Error in '%s'.\n\t Unknown element found "
+ "inside a File element: '%s'.\n",
+ name.c_str(), nodeName.c_str()));
}
}
}