diff options
author | Ryan Mitchell <rtmitchell@google.com> | 2020-12-07 16:06:04 -0800 |
---|---|---|
committer | Ryan Mitchell <rtmitchell@google.com> | 2021-01-08 13:36:35 -0800 |
commit | fb4d09cadd27a3fb1a2e268417f0f511aa92e344 (patch) | |
tree | a8fea8ddc1c63ff6f7492ecb12a7118da1449e5c /cmds/idmap2/libidmap2/XmlParser.cpp | |
parent | 30dc2e01714a50e3816efa3bd347d1bf783fa0a5 (diff) |
Read manifest values using resource id in idmap2
Idmap2 currently reads android:targetPackage and android:targetName
from overlay manifests by looking for an attribute with the name of
the attribute resource.
This fixes the divergence from package parsing by finding the
attributes using their resource ids.
Bug: 175060836
Test: libidmap2_tests
Change-Id: I09965e5880a1e6c48c3f8077db0c595484804ce7
Diffstat (limited to 'cmds/idmap2/libidmap2/XmlParser.cpp')
-rw-r--r-- | cmds/idmap2/libidmap2/XmlParser.cpp | 62 |
1 files changed, 40 insertions, 22 deletions
diff --git a/cmds/idmap2/libidmap2/XmlParser.cpp b/cmds/idmap2/libidmap2/XmlParser.cpp index 4030b83b3a41..00baea46f909 100644 --- a/cmds/idmap2/libidmap2/XmlParser.cpp +++ b/cmds/idmap2/libidmap2/XmlParser.cpp @@ -90,15 +90,27 @@ std::string XmlParser::Node::name() const { return String8(key16).c_str(); } -Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const { - auto value = GetAttributeValue(name); - if (!value) { - return value.GetError(); +template <typename Func> +Result<Res_value> FindAttribute(const ResXMLParser& parser, const std::string& label, + Func&& predicate) { + for (size_t i = 0; i < parser.getAttributeCount(); i++) { + if (!predicate(i)) { + continue; + } + Res_value res_value{}; + if (parser.getAttributeValue(i, &res_value) == BAD_TYPE) { + return Error(R"(Bad value for attribute "%s")", label.c_str()); + } + return res_value; } + return Error(R"(Failed to find attribute "%s")", label.c_str()); +} - switch ((*value).dataType) { +Result<std::string> GetStringValue(const ResXMLParser& parser, const Res_value& value, + const std::string& label) { + switch (value.dataType) { case Res_value::TYPE_STRING: { - if (auto str = parser_.getStrings().string8ObjectAt((*value).data); str.ok()) { + if (auto str = parser.getStrings().string8ObjectAt(value.data); str.ok()) { return std::string(str->string()); } break; @@ -106,31 +118,37 @@ Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& case Res_value::TYPE_INT_DEC: case Res_value::TYPE_INT_HEX: case Res_value::TYPE_INT_BOOLEAN: { - return std::to_string((*value).data); + return std::to_string(value.data); } } + return Error(R"(Failed to convert attribute "%s" value to a string)", label.c_str()); +} - return Error(R"(Failed to convert attribute "%s" value to a string)", name.c_str()); +Result<Res_value> XmlParser::Node::GetAttributeValue(ResourceId attr, + const std::string& label) const { + return FindAttribute(parser_, label, [&](size_t index) -> bool { + return parser_.getAttributeNameResID(index) == attr; + }); } Result<Res_value> XmlParser::Node::GetAttributeValue(const std::string& name) const { - size_t len; - for (size_t i = 0; i < parser_.getAttributeCount(); i++) { - const String16 key16(parser_.getAttributeName(i, &len)); + return FindAttribute(parser_, name, [&](size_t index) -> bool { + size_t len; + const String16 key16(parser_.getAttributeName(index, &len)); std::string key = String8(key16).c_str(); - if (key != name) { - continue; - } - - Res_value res_value{}; - if (parser_.getAttributeValue(i, &res_value) == BAD_TYPE) { - return Error(R"(Bad value for attribute "%s")", name.c_str()); - } + return key == name; + }); +} - return res_value; - } +Result<std::string> XmlParser::Node::GetAttributeStringValue(ResourceId attr, + const std::string& label) const { + auto value = GetAttributeValue(attr, label); + return value ? GetStringValue(parser_, *value, label) : value.GetError(); +} - return Error(R"(Failed to find attribute "%s")", name.c_str()); +Result<std::string> XmlParser::Node::GetAttributeStringValue(const std::string& name) const { + auto value = GetAttributeValue(name); + return value ? GetStringValue(parser_, *value, name) : value.GetError(); } Result<std::unique_ptr<const XmlParser>> XmlParser::Create(const void* data, size_t size, |