fix to parse html resource

Signed-off-by: Koh M. Nakagawa <tsunekou1019@gmail.com>
This commit is contained in:
Koh M. Nakagawa 2020-07-06 00:42:24 +09:00
parent 4bbe410333
commit eb87ca6ef6
4 changed files with 65 additions and 1 deletions

View File

@ -113,6 +113,14 @@ void create<ResourcesManager>(py::module& m) {
&ResourcesManager::string_table,
"Return list of " RST_CLASS_REF(lief.PE.ResourceStringTable) " present in the resource")
.def_property_readonly("has_html",
&ResourcesManager::has_html,
"``True`` if resources contain HTML resource")
.def_property_readonly("html",
&ResourcesManager::html,
"HTML resource as the list of ``string``")
.def("get_node_type",
static_cast<no_const_func<ResourceNode&, RESOURCE_TYPES>>(&ResourcesManager::get_node_type),
"Return " RST_CLASS_REF(lief.PE.ResourceNode) " with "

View File

@ -127,6 +127,15 @@ class LIEF_API ResourcesManager : public Object {
//! @brief Return the list of the string table in the resource
std::vector<ResourceStringTable> string_table(void) const;
// HTML
// ====
//! @brief ``true`` if resources contain html
bool has_html(void) const;
//! @brief Return the list of the html resource
std::vector<std::string> html(void) const;
// Print
// =====

View File

@ -353,7 +353,6 @@ std::string ResourcesManager::manifest(void) const {
}
it_childs childs_l2 = childs_l1[0].childs();
if (childs_l2.size() < 1) {
throw not_found("Manifest corrupted");
}
@ -1248,6 +1247,46 @@ bool ResourcesManager::has_string_table(void) const {
return this->has_type(RESOURCE_TYPES::STRING);
}
std::vector<std::string> ResourcesManager::html(void) const {
it_childs nodes = this->resources_->childs();
auto&& it_html = std::find_if(
std::begin(nodes),
std::end(nodes),
[] (const ResourceNode& node) {
return static_cast<RESOURCE_TYPES>(node.id()) == RESOURCE_TYPES::HTML;
}
);
if (it_html == std::end(nodes)) {
throw not_found(std::string("Missing '") + to_string(RESOURCE_TYPES::HTML) + "' entry");
}
std::vector<std::string> html;
for (const ResourceNode& child_l1 : it_html->childs()) {
for (const ResourceNode& child_l2 : child_l1.childs()) {
const ResourceData* html_node = dynamic_cast<const ResourceData*>(&child_l2);
if (!html_node) {
LOG(ERROR) << "html node is null";
continue;
}
const std::vector<uint8_t>& content = html_node->content();
if (content.empty()) {
LOG(ERROR) << "html content is empty";
continue;
}
html.push_back(std::string{std::begin(content), std::end(content)});
}
}
return html;
}
bool ResourcesManager::has_html(void) const {
return this->has_type(RESOURCE_TYPES::HTML);
}
// Prints
// ======

View File

@ -507,6 +507,14 @@ void JsonVisitor::visit(const ResourcesManager& resources_manager) {
}
}
if (resources_manager.has_html()) {
try {
this->node_["html"] = resources_manager.html();
} catch (const LIEF::exception& e) {
LOG(WARNING) << e.what();
}
}
if (resources_manager.has_version()) {
JsonVisitor version_visitor;
try {