summaryrefslogtreecommitdiff
path: root/binaryninjaapi.cpp
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2020-09-14 17:15:37 -0400
committerGlenn Smith <glenn@vector35.com>2021-07-13 15:04:49 -0400
commitce08fff22e4bed3d977cfd662ed2a6d8caa82997 (patch)
tree9fed8c3070837f38099d13ff78a7577fbc4ac168 /binaryninjaapi.cpp
parent10e668db4d76658882bee7b75d47063b054a730d (diff)
Database api
Co-Authored-By: Josh Ferrell <josh@vector35.com>
Diffstat (limited to 'binaryninjaapi.cpp')
-rw-r--r--binaryninjaapi.cpp40
1 files changed, 39 insertions, 1 deletions
diff --git a/binaryninjaapi.cpp b/binaryninjaapi.cpp
index 6b2497a9..d2815e8f 100644
--- a/binaryninjaapi.cpp
+++ b/binaryninjaapi.cpp
@@ -19,6 +19,7 @@
// IN THE SOFTWARE.
#include "binaryninjaapi.h"
+#include <numeric>
using namespace BinaryNinja;
using namespace std;
@@ -385,4 +386,41 @@ map<string, uint64_t> BinaryNinja::GetMemoryUsageInfo()
result[info[i].name] = info[i].value;
BNFreeMemoryUsageInfo(info, count);
return result;
-} \ No newline at end of file
+}
+
+
+std::function<bool(size_t, size_t)>
+BinaryNinja::SplitProgress(std::function<bool(size_t, size_t)> originalFn, size_t subpart, size_t subpartCount)
+{
+ return SplitProgress(originalFn, subpart, std::vector<double>(subpartCount, 1.0 / (double)subpartCount));
+}
+
+
+std::function<bool(size_t, size_t)>
+BinaryNinja::SplitProgress(std::function<bool(size_t, size_t)> originalFn, size_t subpart, std::vector<double> subpartWeights)
+{
+ if (!originalFn)
+ return [](size_t, size_t){ return true; };
+
+ // Normalize weights
+ double weightSum = std::accumulate(subpartWeights.begin(), subpartWeights.end(), 0.0);
+ if (weightSum < 0.0001f)
+ return [](size_t, size_t){ return true; };
+ // Keep a running count of weights for the start
+ std::vector<double> subpartStarts;
+ double start = 0.0;
+ for (auto i = 0; i < subpartWeights.size(); ++i)
+ {
+ subpartStarts.push_back(start);
+ subpartWeights[i] /= weightSum;
+ start += subpartWeights[i];
+ }
+
+ return [=](size_t current, size_t max) {
+ // Just use a large number for easy divisibility
+ size_t steps = 1000000;
+ double subpartSize = steps * subpartWeights[subpart];
+ double subpartProgress = ((double)current / (double)max) * subpartSize;
+ return originalFn(subpartStarts[subpart] * steps + subpartProgress, steps);
+ };
+}