|
| 1 | +// Copyright (c) 2015-2026 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 | + |
| 27 | +FormatStringResolutionProvider::FormatStringResolutionProvider(const string& name) : m_nameForRegister(name) {} |
| 28 | + |
| 29 | + |
| 30 | +FormatStringResolutionProvider::FormatStringResolutionProvider(BNFormatStringResolutionProvider* provider) |
| 31 | +{ |
| 32 | + m_object = provider; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +bool FormatStringResolutionProvider::IsValidCallback( |
| 37 | + void* ctxt, const char* format, BNTypeWithConfidence** types, size_t* count) |
| 38 | +{ |
| 39 | + FormatStringResolutionProvider* provider = (FormatStringResolutionProvider*)ctxt; |
| 40 | + auto result = provider->IsValid(format); |
| 41 | + if (!result.has_value()) |
| 42 | + { |
| 43 | + *types = nullptr; |
| 44 | + *count = 0; |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + *count = result->size(); |
| 49 | + if (result->empty()) |
| 50 | + { |
| 51 | + *types = nullptr; |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + *types = new BNTypeWithConfidence[result->size()]; |
| 56 | + for (size_t i = 0; i < result->size(); i++) |
| 57 | + { |
| 58 | + (*types)[i].type = BNNewTypeReference((*result)[i].GetValue()->GetObject()); |
| 59 | + (*types)[i].confidence = (*result)[i].GetConfidence(); |
| 60 | + } |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +void FormatStringResolutionProvider::FreeTypeListCallback( |
| 66 | + void*, BNTypeWithConfidence* types, size_t count) |
| 67 | +{ |
| 68 | + for (size_t i = 0; i < count; i++) |
| 69 | + BNFreeType(types[i].type); |
| 70 | + delete[] types; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +string FormatStringResolutionProvider::GetName() const |
| 75 | +{ |
| 76 | + char* name = BNGetFormatStringResolutionProviderName(m_object); |
| 77 | + string result = name; |
| 78 | + BNFreeString(name); |
| 79 | + return result; |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +vector<Ref<FormatStringResolutionProvider>> FormatStringResolutionProvider::GetList() |
| 84 | +{ |
| 85 | + size_t count; |
| 86 | + BNFormatStringResolutionProvider** list = BNGetFormatStringResolutionProviderList(&count); |
| 87 | + vector<Ref<FormatStringResolutionProvider>> result; |
| 88 | + result.reserve(count); |
| 89 | + for (size_t i = 0; i < count; i++) |
| 90 | + result.push_back(new CoreFormatStringResolutionProvider(list[i])); |
| 91 | + BNFreeFormatStringResolutionProviderList(list); |
| 92 | + return result; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +Ref<FormatStringResolutionProvider> FormatStringResolutionProvider::GetByName(const string& name) |
| 97 | +{ |
| 98 | + BNFormatStringResolutionProvider* result = BNGetFormatStringResolutionProviderByName(name.c_str()); |
| 99 | + if (!result) |
| 100 | + return nullptr; |
| 101 | + return new CoreFormatStringResolutionProvider(result); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +void FormatStringResolutionProvider::Register(FormatStringResolutionProvider* provider) |
| 106 | +{ |
| 107 | + BNFormatStringResolutionProviderCallbacks callbacks; |
| 108 | + callbacks.context = provider; |
| 109 | + callbacks.isValid = IsValidCallback; |
| 110 | + callbacks.freeTypeList = FreeTypeListCallback; |
| 111 | + provider->AddRefForRegistration(); |
| 112 | + provider->m_object = BNRegisterFormatStringResolutionProvider(provider->m_nameForRegister.c_str(), &callbacks); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +CoreFormatStringResolutionProvider::CoreFormatStringResolutionProvider(BNFormatStringResolutionProvider* provider) : |
| 117 | + FormatStringResolutionProvider(provider) |
| 118 | +{} |
| 119 | + |
| 120 | + |
| 121 | +optional<vector<Confidence<Ref<Type>>>> CoreFormatStringResolutionProvider::IsValid(const string& format) |
| 122 | +{ |
| 123 | + BNTypeWithConfidence* types = nullptr; |
| 124 | + size_t count = 0; |
| 125 | + if (!BNFormatStringResolutionProviderIsValid(m_object, format.c_str(), &types, &count)) |
| 126 | + return nullopt; |
| 127 | + |
| 128 | + vector<Confidence<Ref<Type>>> result; |
| 129 | + result.reserve(count); |
| 130 | + for (size_t i = 0; i < count; i++) |
| 131 | + { |
| 132 | + result.emplace_back( |
| 133 | + new Type(BNNewTypeReference(types[i].type)), types[i].confidence); |
| 134 | + } |
| 135 | + BNFreeTypeWithConfidenceList(types, count); |
| 136 | + return result; |
| 137 | +} |
0 commit comments