|
| 1 | +// Copyright (c) 2015-2025 Vector 35 Inc |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to |
| 5 | +// deal in the Software without restriction, including without limitation the |
| 6 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +// IN THE SOFTWARE. |
| 20 | + |
| 21 | +#include "binaryninjaapi.h" |
| 22 | + |
| 23 | +using namespace BinaryNinja; |
| 24 | +using namespace std; |
| 25 | + |
| 26 | +DatabaseObject::DatabaseObject(BNDatabaseObject* object) |
| 27 | +{ |
| 28 | + m_object = object; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +DatabaseObject::~DatabaseObject() = default; |
| 33 | + |
| 34 | + |
| 35 | +Ref<DatabaseObject> DatabaseObject::GetParent() const |
| 36 | +{ |
| 37 | + BNDatabaseObject* parent = BNGetDatabaseObjectParent(m_object); |
| 38 | + if (!parent) |
| 39 | + { |
| 40 | + return nullptr; |
| 41 | + } |
| 42 | + return new DatabaseObject(parent); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +std::string DatabaseObject::GetId() const |
| 47 | +{ |
| 48 | + char* id = BNGetDatabaseObjectId(m_object); |
| 49 | + std::string result = id; |
| 50 | + BNFreeString(id); |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +std::string DatabaseObject::GetDescription() const |
| 56 | +{ |
| 57 | + char* desc = BNGetDatabaseObjectDescription(m_object); |
| 58 | + std::string result = desc; |
| 59 | + BNFreeString(desc); |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +int DatabaseObject::GetType() const |
| 65 | +{ |
| 66 | + return BNGetDatabaseObjectType(m_object); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +std::unordered_map<std::string, Ref<DatabaseObject>> DatabaseObject::GetChildren() |
| 71 | +{ |
| 72 | + char** names; |
| 73 | + BNDatabaseObject** objects; |
| 74 | + size_t count = BNGetDatabaseObjectChildren(m_object, &names, &objects); |
| 75 | + |
| 76 | + std::unordered_map<std::string, Ref<DatabaseObject>> result; |
| 77 | + for (size_t i = 0; i < count; i++) |
| 78 | + { |
| 79 | + result[names[i]] = new DatabaseObject(BNNewDatabaseObjectReference(objects[i])); |
| 80 | + } |
| 81 | + |
| 82 | + BNFreeStringList(names, count); |
| 83 | + BNFreeDatabaseObjectList(objects, count); |
| 84 | + return result; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +std::vector<std::string> DatabaseObject::GetDependencies() const |
| 89 | +{ |
| 90 | + size_t count; |
| 91 | + char** deps = BNGetDatabaseObjectDependencies(m_object, &count); |
| 92 | + |
| 93 | + std::vector<std::string> result; |
| 94 | + for (size_t i = 0; i < count; i++) |
| 95 | + { |
| 96 | + result.push_back(deps[i]); |
| 97 | + } |
| 98 | + |
| 99 | + BNFreeStringList(deps, count); |
| 100 | + return result; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +DiffState::DiffState(BNDiffState* state) |
| 105 | +{ |
| 106 | + m_object = state; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +DiffState::DiffState(Ref<Logger> logger) |
| 111 | +{ |
| 112 | + m_object = BNCreateDiffState(logger->GetObject()); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +DiffState::~DiffState() = default; |
| 117 | + |
| 118 | + |
| 119 | +std::vector<std::string> DiffState::GetErrors() const |
| 120 | +{ |
| 121 | + size_t count; |
| 122 | + char** errors = BNGetDiffStateErrors(m_object, &count); |
| 123 | + |
| 124 | + std::vector<std::string> result; |
| 125 | + for (size_t i = 0; i < count; i++) |
| 126 | + { |
| 127 | + result.push_back(errors[i]); |
| 128 | + } |
| 129 | + |
| 130 | + BNFreeStringList(errors, count); |
| 131 | + return result; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +void DiffState::ClearErrors() |
| 136 | +{ |
| 137 | + BNClearDiffStateErrors(m_object); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +Ref<DiffObject> DiffState::GenerateDiff( |
| 142 | + Ref<DatabaseObject> base, |
| 143 | + Ref<DatabaseObject> left, |
| 144 | + Ref<DatabaseObject> right |
| 145 | +) |
| 146 | +{ |
| 147 | + BNDiffObject* diff = BNDiffStateGenerateDiff( |
| 148 | + m_object, |
| 149 | + base->GetObject(), |
| 150 | + left->GetObject(), |
| 151 | + right->GetObject() |
| 152 | + ); |
| 153 | + |
| 154 | + if (!diff) |
| 155 | + { |
| 156 | + return nullptr; |
| 157 | + } |
| 158 | + return new DiffObject(diff); |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +bool DiffState::ApplyDiff( |
| 163 | + Ref<DiffObject> diff, |
| 164 | + Ref<DatabaseObject> base, |
| 165 | + Ref<DatabaseObject> left, |
| 166 | + Ref<DatabaseObject> right, |
| 167 | + Ref<DatabaseObject> result |
| 168 | +) |
| 169 | +{ |
| 170 | + return BNDiffStateApplyDiff( |
| 171 | + m_object, diff->GetObject(), base->GetObject(), |
| 172 | + left->GetObject(), right->GetObject(), result->GetObject()); |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +bool DiffState::IsDiffed(Ref<DatabaseObject> object) const |
| 177 | +{ |
| 178 | + return BNDiffStateIsDiffed(m_object, object->GetObject()); |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +bool DiffState::IsApplied(Ref<DiffObject> object) const |
| 183 | +{ |
| 184 | + return BNDiffStateIsApplied(m_object, object->GetObject()); |
| 185 | +} |
| 186 | + |
| 187 | + |
| 188 | +DiffObject::DiffObject(BNDiffObject* object) |
| 189 | +{ |
| 190 | + m_object = object; |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | +DiffObject::~DiffObject() = default; |
| 195 | + |
| 196 | + |
| 197 | +std::string DiffObject::GetBase() const |
| 198 | +{ |
| 199 | + char* base = BNGetDiffObjectBase(m_object); |
| 200 | + std::string result = base; |
| 201 | + BNFreeString(base); |
| 202 | + return result; |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +std::string DiffObject::GetLeft() const |
| 207 | +{ |
| 208 | + char* left = BNGetDiffObjectLeft(m_object); |
| 209 | + std::string result = left; |
| 210 | + BNFreeString(left); |
| 211 | + return result; |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | +std::string DiffObject::GetRight() const |
| 216 | +{ |
| 217 | + char* right = BNGetDiffObjectRight(m_object); |
| 218 | + std::string result = right; |
| 219 | + BNFreeString(right); |
| 220 | + return result; |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +std::unordered_map<std::string, Ref<DiffObject>> DiffObject::GetChildren() const |
| 225 | +{ |
| 226 | + char** names; |
| 227 | + BNDiffObject** objects; |
| 228 | + size_t count = BNGetDiffObjectChildren(m_object, &names, &objects); |
| 229 | + |
| 230 | + std::unordered_map<std::string, Ref<DiffObject>> result; |
| 231 | + for (size_t i = 0; i < count; i++) |
| 232 | + { |
| 233 | + result[names[i]] = new DiffObject(BNNewDiffObjectReference(objects[i])); |
| 234 | + } |
| 235 | + |
| 236 | + BNFreeStringList(names, count); |
| 237 | + BNFreeDiffObjectList(objects, count); |
| 238 | + return result; |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +BNMergeStrategy DiffObject::GetMergeStrategy() const |
| 243 | +{ |
| 244 | + return BNGetDiffObjectMergeStrategy(m_object); |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +void DiffObject::SetMergeStrategy(BNMergeStrategy strategy) |
| 249 | +{ |
| 250 | + BNSetDiffObjectMergeStrategy(m_object, strategy); |
| 251 | +} |
0 commit comments