diff options
| author | Peter LaFosse <peter@vector35.com> | 2018-09-12 17:03:38 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2018-09-12 17:03:38 -0400 |
| commit | 74a8547853dd66fe62d2e75b93c1b723eb6f69cf (patch) | |
| tree | 067119496c2beaf0636c955d27291d4620e17c0b /flowgraph.cpp | |
| parent | 097a2e4c8849d4938d8a027985ce3816fd9785ed (diff) | |
| parent | 59a2824b1ad36ea5d0520919e86ad779cd97cc8f (diff) | |
Merging with dev
Diffstat (limited to 'flowgraph.cpp')
| -rw-r--r-- | flowgraph.cpp | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/flowgraph.cpp b/flowgraph.cpp new file mode 100644 index 00000000..dc7e628e --- /dev/null +++ b/flowgraph.cpp @@ -0,0 +1,358 @@ +// Copyright (c) 2015-2017 Vector 35 LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "binaryninjaapi.h" + +using namespace BinaryNinja; +using namespace std; + + +FlowGraphLayoutRequest::FlowGraphLayoutRequest(FlowGraph* graph, const std::function<void()>& completeFunc): + m_completeFunc(completeFunc) +{ + m_object = BNStartFlowGraphLayout(graph->GetObject(), this, CompleteCallback); +} + + +FlowGraphLayoutRequest::~FlowGraphLayoutRequest() +{ + // This object is going away, so ensure that any pending completion routines are + // no longer called + Abort(); + + BNFreeFlowGraphLayoutRequest(m_object); +} + + +void FlowGraphLayoutRequest::CompleteCallback(void* ctxt) +{ + FlowGraphLayoutRequest* layout = (FlowGraphLayoutRequest*)ctxt; + layout->m_completeFunc(); +} + + +Ref<FlowGraph> FlowGraphLayoutRequest::GetGraph() const +{ + return new CoreFlowGraph(BNGetGraphForFlowGraphLayoutRequest(m_object)); +} + + +bool FlowGraphLayoutRequest::IsComplete() const +{ + return BNIsFlowGraphLayoutRequestComplete(m_object); +} + + +void FlowGraphLayoutRequest::Abort() +{ + // Must clear the callback with the core before clearing our own function object, as until it + // is cleared in the core it can be called at any time from a different thread. + BNAbortFlowGraphLayoutRequest(m_object); + m_completeFunc = []() {}; +} + + +FlowGraph::FlowGraph() +{ + BNCustomFlowGraph callbacks; + callbacks.context = this; + callbacks.prepareForLayout = PrepareForLayoutCallback; + callbacks.populateNodes = PopulateNodesCallback; + callbacks.completeLayout = CompleteLayoutCallback; + m_object = BNCreateCustomFlowGraph(&callbacks); +} + + +FlowGraph::FlowGraph(BNFlowGraph* graph) +{ + m_object = graph; +} + + +void FlowGraph::PrepareForLayoutCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->PrepareForLayout(); +} + + +void FlowGraph::PopulateNodesCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->PopulateNodes(); +} + + +void FlowGraph::CompleteLayoutCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->CompleteLayout(); +} + + +BNFlowGraph* FlowGraph::UpdateCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + Ref<FlowGraph> result = graph->Update(); + if (!result) + return nullptr; + return BNNewFlowGraphReference(result->GetObject()); +} + + +void FlowGraph::FinishPrepareForLayout() +{ + BNFinishPrepareForLayout(m_object); +} + + +void FlowGraph::PrepareForLayout() +{ + FinishPrepareForLayout(); +} + + +void FlowGraph::PopulateNodes() +{ +} + + +void FlowGraph::CompleteLayout() +{ +} + + +Ref<Function> FlowGraph::GetFunction() const +{ + BNFunction* func = BNGetFunctionForFlowGraph(m_object); + if (!func) + return nullptr; + return new Function(BNNewFunctionReference(func)); +} + + +void FlowGraph::SetFunction(Function* func) +{ + BNSetFunctionForFlowGraph(m_object, func ? func->GetObject() : nullptr); +} + + +int FlowGraph::GetHorizontalNodeMargin() const +{ + return BNGetHorizontalFlowGraphNodeMargin(m_object); +} + + +int FlowGraph::GetVerticalNodeMargin() const +{ + return BNGetVerticalFlowGraphNodeMargin(m_object); +} + + +void FlowGraph::SetNodeMargins(int horiz, int vert) +{ + BNSetFlowGraphNodeMargins(m_object, horiz, vert); +} + + +Ref<FlowGraphLayoutRequest> FlowGraph::StartLayout(const std::function<void()>& func) +{ + return new FlowGraphLayoutRequest(this, func); +} + + +bool FlowGraph::IsLayoutComplete() +{ + return BNIsFlowGraphLayoutComplete(m_object); +} + + +vector<Ref<FlowGraphNode>> FlowGraph::GetNodes() +{ + size_t count; + BNFlowGraphNode** nodes = BNGetFlowGraphNodes(m_object, &count); + + vector<Ref<FlowGraphNode>> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + auto node = m_cachedNodes.find(nodes[i]); + if (node == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(BNNewFlowGraphNodeReference(nodes[i])); + m_cachedNodes[nodes[i]] = newNode; + result.push_back(newNode); + } + else + { + result.push_back(node->second); + } + } + + BNFreeFlowGraphNodeList(nodes, count); + return result; +} + + +Ref<FlowGraphNode> FlowGraph::GetNode(size_t i) +{ + BNFlowGraphNode* node = BNGetFlowGraphNode(m_object, i); + if (!node) + return nullptr; + + auto nodeIter = m_cachedNodes.find(node); + if (nodeIter == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(node); + m_cachedNodes[node] = newNode; + return newNode; + } + else + { + BNFreeFlowGraphNode(node); + return nodeIter->second; + } +} + + +bool FlowGraph::HasNodes() const +{ + return BNFlowGraphHasNodes(m_object); +} + + +size_t FlowGraph::AddNode(FlowGraphNode* node) +{ + m_cachedNodes[node->GetObject()] = node; + return BNAddFlowGraphNode(m_object, node->GetObject()); +} + + +int FlowGraph::GetWidth() const +{ + return BNGetFlowGraphWidth(m_object); +} + + +int FlowGraph::GetHeight() const +{ + return BNGetFlowGraphHeight(m_object); +} + + +vector<Ref<FlowGraphNode>> FlowGraph::GetNodesInRegion(int left, int top, int right, int bottom) +{ + size_t count; + BNFlowGraphNode** nodes = BNGetFlowGraphNodesInRegion(m_object, left, top, right, bottom, &count); + + vector<Ref<FlowGraphNode>> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + auto node = m_cachedNodes.find(nodes[i]); + if (node == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(BNNewFlowGraphNodeReference(nodes[i])); + m_cachedNodes[nodes[i]] = newNode; + result.push_back(newNode); + } + else + { + result.push_back(node->second); + } + } + + BNFreeFlowGraphNodeList(nodes, count); + return result; +} + + +bool FlowGraph::IsILGraph() const +{ + return BNIsILFlowGraph(m_object); +} + + +bool FlowGraph::IsLowLevelILGraph() const +{ + return BNIsLowLevelILFlowGraph(m_object); +} + + +bool FlowGraph::IsMediumLevelILGraph() const +{ + return BNIsMediumLevelILFlowGraph(m_object); +} + + +Ref<LowLevelILFunction> FlowGraph::GetLowLevelILFunction() const +{ + BNLowLevelILFunction* func = BNGetFlowGraphLowLevelILFunction(m_object); + if (!func) + return nullptr; + return new LowLevelILFunction(func); +} + + +Ref<MediumLevelILFunction> FlowGraph::GetMediumLevelILFunction() const +{ + BNMediumLevelILFunction* func = BNGetFlowGraphMediumLevelILFunction(m_object); + if (!func) + return nullptr; + return new MediumLevelILFunction(func); +} + + +void FlowGraph::SetLowLevelILFunction(LowLevelILFunction* func) +{ + BNSetFlowGraphLowLevelILFunction(m_object, func ? func->GetObject() : nullptr); +} + + +void FlowGraph::SetMediumLevelILFunction(MediumLevelILFunction* func) +{ + BNSetFlowGraphMediumLevelILFunction(m_object, func ? func->GetObject() : nullptr); +} + + +void FlowGraph::Show(const string& title) +{ + ShowGraphReport(title, this); +} + + +Ref<FlowGraph> FlowGraph::Update() +{ + return nullptr; +} + + +CoreFlowGraph::CoreFlowGraph(BNFlowGraph* graph): FlowGraph(graph) +{ +} + + +Ref<FlowGraph> CoreFlowGraph::Update() +{ + BNFlowGraph* graph = BNUpdateFlowGraph(GetObject()); + if (!graph) + return nullptr; + return new CoreFlowGraph(graph); +} |
