Skip to content

Commit

Permalink
Basic XML Output Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r committed Aug 26, 2019
1 parent ef176d4 commit 2d511d4
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -1,3 +1,6 @@
[submodule "ghidra/ghidra"]
path = ghidra/ghidra
url = https://github.com/thestr4ng3r/ghidra.git
[submodule "third-party/pugixml"]
path = third-party/pugixml
url = https://github.com/zeux/pugixml.git
8 changes: 6 additions & 2 deletions CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_CXX_STANDARD 11)

add_subdirectory(ghidra)
add_subdirectory(third-party)

set(SOURCE
src/core_ghidra.cpp
Expand All @@ -27,7 +28,9 @@ set(SOURCE
src/R2Emit.cpp
src/R2Emit.h
src/AnnotatedCode.h
src/AnnotatedCode.c)
src/AnnotatedCode.c
src/CodeXMLParse.h
src/CodeXMLParse.cpp)

find_package(Radare2 REQUIRED)

Expand All @@ -37,7 +40,8 @@ endif()

add_library(core_ghidra SHARED ${SOURCE})
target_link_libraries(core_ghidra ghidra_decompiler_base ghidra_libdecomp ghidra_decompiler_sleigh)
target_link_libraries(core_ghidra pugixml)
target_link_libraries(core_ghidra Radare2::libr)
set_target_properties(core_ghidra PROPERTIES
OUTPUT_NAME core_ghidra
PREFIX "")
target_link_libraries(core_ghidra Radare2::libr)
50 changes: 50 additions & 0 deletions src/CodeXMLParse.cpp
@@ -0,0 +1,50 @@

#include "CodeXMLParse.h"
#include <funcdata.hh>
#include <r_util.h>
#include <pugixml.hpp>
#include <sstream>
#include <string>

static void ParseNode(pugi::xml_node node, Funcdata *func, std::ostream &stream, RAnnotatedCode *code)
{
if(node.type() == pugi::xml_node_type::node_pcdata)
{
stream << node.value();
return;
}

if(strcmp(node.name(), "break") == 0)
{
stream << "\n";
stream << std::string(node.attribute("indent").as_uint(0), ' ');
}

for(pugi::xml_node child : node)
ParseNode(child, func, stream, code);
}

R_API RAnnotatedCode *ParseCodeXML(Funcdata *func, const char *xml)
{
pugi::xml_document doc;
if(!doc.load_string(xml, pugi::parse_default | pugi::parse_ws_pcdata))
return nullptr;

std::stringstream ss;
RAnnotatedCode *code = r_annotated_code_new(nullptr);
if(!code)
return nullptr;

ParseNode(doc.child("function"), func, ss, code);

std::string str = ss.str();
code->code = reinterpret_cast<char *>(r_malloc(str.length() + 1));
if(!code->code)
{
r_annotated_code_free(code);
return nullptr;
}
memcpy(code->code, str.c_str(), str.length());
code->code[str.length()] = '\0';
return code;
}
11 changes: 11 additions & 0 deletions src/CodeXMLParse.h
@@ -0,0 +1,11 @@

#ifndef R2GHIDRA_CODEXMLPARSE_H
#define R2GHIDRA_CODEXMLPARSE_H

#include "AnnotatedCode.h"

class Funcdata;

R_API RAnnotatedCode *ParseCodeXML(Funcdata *func, const char *xml);

#endif //R2GHIDRA_CODEXMLPARSE_H
17 changes: 16 additions & 1 deletion src/core_ghidra.cpp
@@ -1,6 +1,7 @@
/* radare - LGPL - Copyright 2019 - thestr4ng3r */

#include "R2Architecture.h"
#include "CodeXMLParse.h"

#include <libdecomp.hh>
#include <printc.hh>
Expand Down Expand Up @@ -150,15 +151,19 @@ static void Decompile(RCore *core, DecompileMode mode)
func->saveXml(out_stream, true);
out_stream << "</function><code>";
}
else if(mode == DecompileMode::OFFSET)
{
arch.print->setXML(true);
}

switch(mode)
{
case DecompileMode::XML:
case DecompileMode::DEFAULT:
case DecompileMode::OFFSET:
arch.print->docFunction(func);
break;
case DecompileMode::STATEMENTS:
case DecompileMode::OFFSET:
arch.print_with_offsets->docFunction(func);
break;
case DecompileMode::DEBUG_XML:
Expand All @@ -168,6 +173,16 @@ static void Decompile(RCore *core, DecompileMode mode)

if(mode == DecompileMode::OFFSET)
{
RAnnotatedCode *code = ParseCodeXML(func, out_stream.str().c_str());
if(!code)
{
eprintf("Failed to parse XML code\n");
return;
}
// TODO: print code with offsets from RAnnotatedCode here
r_cons_print(code->code);
r_annotated_code_free(code);
return;
ut64 offset;
string line;
std::stringstream line_stream;
Expand Down
8 changes: 8 additions & 0 deletions third-party/CMakeLists.txt
@@ -0,0 +1,8 @@

add_library(pugixml STATIC
pugixml/src/pugiconfig.hpp
pugixml/src/pugixml.cpp
pugixml/src/pugixml.hpp)
target_include_directories(pugixml PUBLIC pugixml/src)
set_target_properties(pugixml PROPERTIES POSITION_INDEPENDENT_CODE ON)

1 change: 1 addition & 0 deletions third-party/pugixml
Submodule pugixml added at fdf029

0 comments on commit 2d511d4

Please sign in to comment.