diff options
Diffstat (limited to 'binaryview.cpp')
| -rw-r--r-- | binaryview.cpp | 744 |
1 files changed, 735 insertions, 9 deletions
diff --git a/binaryview.cpp b/binaryview.cpp index a3abc02a..8edbc2bf 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -18,6 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. +#include <algorithm> +#include <iterator> #include "binaryninjaapi.h" using namespace BinaryNinja; @@ -75,6 +77,42 @@ void BinaryDataNotification::FunctionUpdatedCallback(void* ctxt, BNBinaryView* o } +void BinaryDataNotification::DataVariableAddedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref<BinaryView> view = new BinaryView(BNNewViewReference(object)); + DataVariable varObj; + varObj.address = var->address; + varObj.type = new Type(BNNewTypeReference(var->type)); + varObj.autoDiscovered = var->autoDiscovered; + notify->OnDataVariableAdded(view, varObj); +} + + +void BinaryDataNotification::DataVariableRemovedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref<BinaryView> view = new BinaryView(BNNewViewReference(object)); + DataVariable varObj; + varObj.address = var->address; + varObj.type = new Type(BNNewTypeReference(var->type)); + varObj.autoDiscovered = var->autoDiscovered; + notify->OnDataVariableRemoved(view, varObj); +} + + +void BinaryDataNotification::DataVariableUpdatedCallback(void* ctxt, BNBinaryView* object, BNDataVariable* var) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref<BinaryView> view = new BinaryView(BNNewViewReference(object)); + DataVariable varObj; + varObj.address = var->address; + varObj.type = new Type(BNNewTypeReference(var->type)); + varObj.autoDiscovered = var->autoDiscovered; + notify->OnDataVariableUpdated(view, varObj); +} + + void BinaryDataNotification::StringFoundCallback(void* ctxt, BNBinaryView* object, BNStringType type, uint64_t offset, size_t len) { BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; @@ -91,6 +129,24 @@ void BinaryDataNotification::StringRemovedCallback(void* ctxt, BNBinaryView* obj } +void BinaryDataNotification::TypeDefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref<BinaryView> view = new BinaryView(BNNewViewReference(data)); + Ref<Type> typeObj = new Type(BNNewTypeReference(type)); + notify->OnTypeDefined(view, QualifiedName::FromAPIObject(name), typeObj); +} + + +void BinaryDataNotification::TypeUndefinedCallback(void* ctxt, BNBinaryView* data, BNQualifiedName* name, BNType* type) +{ + BinaryDataNotification* notify = (BinaryDataNotification*)ctxt; + Ref<BinaryView> view = new BinaryView(BNNewViewReference(data)); + Ref<Type> typeObj = new Type(BNNewTypeReference(type)); + notify->OnTypeUndefined(view, QualifiedName::FromAPIObject(name), typeObj); +} + + BinaryDataNotification::BinaryDataNotification() { m_callbacks.context = this; @@ -100,8 +156,13 @@ BinaryDataNotification::BinaryDataNotification() m_callbacks.functionAdded = FunctionAddedCallback; m_callbacks.functionRemoved = FunctionRemovedCallback; m_callbacks.functionUpdated = FunctionUpdatedCallback; + m_callbacks.dataVariableAdded = DataVariableAddedCallback; + m_callbacks.dataVariableRemoved = DataVariableRemovedCallback; + m_callbacks.dataVariableUpdated = DataVariableUpdatedCallback; m_callbacks.stringFound = StringFoundCallback; m_callbacks.stringRemoved = StringRemovedCallback; + m_callbacks.typeDefined = TypeDefinedCallback; + m_callbacks.typeUndefined = TypeUndefinedCallback; } @@ -204,7 +265,7 @@ void AnalysisCompletionEvent::Cancel() } -BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) +BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView) { BNCustomBinaryView view; view.context = this; @@ -219,6 +280,7 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) view.isOffsetReadable = IsOffsetReadableCallback; view.isOffsetWritable = IsOffsetWritableCallback; view.isOffsetExecutable = IsOffsetExecutableCallback; + view.isOffsetBackedByFile = IsOffsetBackedByFileCallback; view.getNextValidOffset = GetNextValidOffsetCallback; view.getStart = GetStartCallback; view.getLength = GetLengthCallback; @@ -230,7 +292,8 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) m_file = file; AddRefForRegistration(); - m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), &view); + m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), + parentView ? parentView->GetObject() : nullptr, &view); } @@ -318,6 +381,13 @@ bool BinaryView::IsOffsetExecutableCallback(void* ctxt, uint64_t offset) } +bool BinaryView::IsOffsetBackedByFileCallback(void* ctxt, uint64_t offset) +{ + BinaryView* view = (BinaryView*)ctxt; + return view->PerformIsOffsetBackedByFile(offset); +} + + uint64_t BinaryView::GetNextValidOffsetCallback(void* ctxt, uint64_t offset) { BinaryView* view = (BinaryView*)ctxt; @@ -400,6 +470,12 @@ bool BinaryView::PerformIsOffsetExecutable(uint64_t offset) } +bool BinaryView::PerformIsOffsetBackedByFile(uint64_t offset) +{ + return PerformIsValidOffset(offset); +} + + uint64_t BinaryView::PerformGetNextValidOffset(uint64_t offset) { if (offset < PerformGetStart()) @@ -428,6 +504,15 @@ size_t BinaryView::PerformGetAddressSize() const } +bool BinaryView::PerformSave(FileAccessor* file) +{ + Ref<BinaryView> parent = GetParentView(); + if (parent) + return parent->Save(file); + return false; +} + + void BinaryView::NotifyDataWritten(uint64_t offset, size_t len) { BNNotifyDataWritten(m_object, offset, len); @@ -446,6 +531,15 @@ void BinaryView::NotifyDataRemoved(uint64_t offset, uint64_t len) } +Ref<BinaryView> BinaryView::GetParentView() const +{ + BNBinaryView* view = BNGetParentView(m_object); + if (!view) + return nullptr; + return new BinaryView(view); +} + + string BinaryView::GetTypeName() const { char* str = BNGetViewType(m_object); @@ -479,12 +573,25 @@ bool BinaryView::CreateDatabase(const string& path) } +bool BinaryView::CreateDatabase(const string& path, + const function<void(size_t progress, size_t total)>& progressCallback) +{ + return m_file->CreateDatabase(path, this, progressCallback); +} + + bool BinaryView::SaveAutoSnapshot() { return m_file->SaveAutoSnapshot(this); } +bool BinaryView::SaveAutoSnapshot(const function<void(size_t progress, size_t total)>& progressCallback) +{ + return m_file->SaveAutoSnapshot(this, progressCallback); +} + + void BinaryView::BeginUndoActions() { m_file->BeginUndoActions(); @@ -644,6 +751,12 @@ bool BinaryView::IsOffsetExecutable(uint64_t offset) const } +bool BinaryView::IsOffsetBackedByFile(uint64_t offset) const +{ + return BNIsOffsetBackedByFile(m_object, offset); +} + + uint64_t BinaryView::GetNextValidOffset(uint64_t offset) const { return BNGetNextValidOffset(m_object, offset); @@ -752,6 +865,12 @@ void BinaryView::CreateUserFunction(Platform* platform, uint64_t start) } +void BinaryView::RemoveUserFunction(Function* func) +{ + BNRemoveUserFunction(m_object, func->GetObject()); +} + + void BinaryView::UpdateAnalysis() { BNUpdateAnalysis(m_object); @@ -764,6 +883,67 @@ void BinaryView::AbortAnalysis() } +void BinaryView::DefineDataVariable(uint64_t addr, Type* type) +{ + BNDefineDataVariable(m_object, addr, type->GetObject()); +} + + +void BinaryView::DefineUserDataVariable(uint64_t addr, Type* type) +{ + BNDefineUserDataVariable(m_object, addr, type->GetObject()); +} + + +void BinaryView::UndefineDataVariable(uint64_t addr) +{ + BNUndefineDataVariable(m_object, addr); +} + + +void BinaryView::UndefineUserDataVariable(uint64_t addr) +{ + BNUndefineUserDataVariable(m_object, addr); +} + + +map<uint64_t, DataVariable> BinaryView::GetDataVariables() +{ + size_t count; + BNDataVariable* vars = BNGetDataVariables(m_object, &count); + + map<uint64_t, DataVariable> result; + for (size_t i = 0; i < count; i++) + { + DataVariable var; + var.address = vars[i].address; + var.type = new Type(BNNewTypeReference(vars[i].type)); + var.autoDiscovered = vars[i].autoDiscovered; + result[var.address] = var; + } + + BNFreeDataVariables(vars, count); + return result; +} + + +bool BinaryView::GetDataVariableAtAddress(uint64_t addr, DataVariable& var) +{ + var.address = 0; + var.type = nullptr; + var.autoDiscovered = false; + + BNDataVariable result; + if (!BNGetDataVariableAtAddress(m_object, addr, &result)) + return false; + + var.address = result.address; + var.type = new Type(result.type); + var.autoDiscovered = result.autoDiscovered; + return true; +} + + vector<Ref<Function>> BinaryView::GetAnalysisFunctionList() { size_t count; @@ -848,6 +1028,20 @@ vector<Ref<BasicBlock>> BinaryView::GetBasicBlocksForAddress(uint64_t addr) } +vector<Ref<BasicBlock>> BinaryView::GetBasicBlocksStartingAtAddress(uint64_t addr) +{ + size_t count; + BNBasicBlock** blocks = BNGetBasicBlocksStartingAtAddress(m_object, addr, &count); + + vector<Ref<BasicBlock>> result; + for (size_t i = 0; i < count; i++) + result.push_back(new BasicBlock(BNNewBasicBlockReference(blocks[i]))); + + BNFreeBasicBlockList(blocks, count); + return result; +} + + vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr) { size_t count; @@ -976,31 +1170,38 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t sta } -void BinaryView::DefineAutoSymbol(Symbol* sym) +void BinaryView::DefineAutoSymbol(Ref<Symbol> sym) { BNDefineAutoSymbol(m_object, sym->GetObject()); } -void BinaryView::UndefineAutoSymbol(Symbol* sym) +void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type) +{ + BNDefineAutoSymbolAndVariableOrFunction(m_object, platform ? platform->GetObject() : nullptr, sym->GetObject(), + type ? type->GetObject() : nullptr); +} + + +void BinaryView::UndefineAutoSymbol(Ref<Symbol> sym) { BNUndefineAutoSymbol(m_object, sym->GetObject()); } -void BinaryView::DefineUserSymbol(Symbol* sym) +void BinaryView::DefineUserSymbol(Ref<Symbol> sym) { BNDefineUserSymbol(m_object, sym->GetObject()); } -void BinaryView::UndefineUserSymbol(Symbol* sym) +void BinaryView::UndefineUserSymbol(Ref<Symbol> sym) { BNUndefineUserSymbol(m_object, sym->GetObject()); } -void BinaryView::DefineImportedFunction(Symbol* importAddressSym, Function* func) +void BinaryView::DefineImportedFunction(Ref<Symbol> importAddressSym, Ref<Function> func) { BNDefineImportedFunction(m_object, importAddressSym->GetObject(), func->GetObject()); } @@ -1072,7 +1273,7 @@ vector<BNStringReference> BinaryView::GetStrings() BNStringReference* strings = BNGetStrings(m_object, &count); vector<BNStringReference> result; result.insert(result.end(), strings, strings + count); - BNFreeStringList(strings); + BNFreeStringReferenceList(strings); return result; } @@ -1083,7 +1284,7 @@ vector<BNStringReference> BinaryView::GetStrings(uint64_t start, uint64_t len) BNStringReference* strings = BNGetStringsInRange(m_object, start, len, &count); vector<BNStringReference> result; result.insert(result.end(), strings, strings + count); - BNFreeStringList(strings); + BNFreeStringReferenceList(strings); return result; } @@ -1100,6 +1301,531 @@ BNAnalysisProgress BinaryView::GetAnalysisProgress() } +uint64_t BinaryView::GetNextFunctionStartAfterAddress(uint64_t addr) +{ + return BNGetNextFunctionStartAfterAddress(m_object, addr); +} + + +uint64_t BinaryView::GetNextBasicBlockStartAfterAddress(uint64_t addr) +{ + return BNGetNextBasicBlockStartAfterAddress(m_object, addr); +} + + +uint64_t BinaryView::GetNextDataAfterAddress(uint64_t addr) +{ + return BNGetNextDataAfterAddress(m_object, addr); +} + + +uint64_t BinaryView::GetPreviousFunctionStartBeforeAddress(uint64_t addr) +{ + return BNGetPreviousFunctionStartBeforeAddress(m_object, addr); +} + + +uint64_t BinaryView::GetPreviousBasicBlockStartBeforeAddress(uint64_t addr) +{ + return BNGetPreviousBasicBlockStartBeforeAddress(m_object, addr); +} + + +uint64_t BinaryView::GetPreviousBasicBlockEndBeforeAddress(uint64_t addr) +{ + return BNGetPreviousBasicBlockEndBeforeAddress(m_object, addr); +} + + +uint64_t BinaryView::GetPreviousDataBeforeAddress(uint64_t addr) +{ + return BNGetPreviousDataBeforeAddress(m_object, addr); +} + + +LinearDisassemblyPosition BinaryView::GetLinearDisassemblyPositionForAddress(uint64_t addr, + DisassemblySettings* settings) +{ + BNLinearDisassemblyPosition pos = BNGetLinearDisassemblyPositionForAddress(m_object, addr, + settings ? settings->GetObject() : nullptr); + + LinearDisassemblyPosition result; + result.function = pos.function ? new Function(pos.function) : nullptr; + result.block = pos.block ? new BasicBlock(pos.block) : nullptr; + result.address = pos.address; + return result; +} + + +vector<LinearDisassemblyLine> BinaryView::GetPreviousLinearDisassemblyLines(LinearDisassemblyPosition& pos, + DisassemblySettings* settings) +{ + BNLinearDisassemblyPosition linearPos; + linearPos.function = pos.function ? BNNewFunctionReference(pos.function->GetObject()) : nullptr; + linearPos.block = pos.block ? BNNewBasicBlockReference(pos.block->GetObject()) : nullptr; + linearPos.address = pos.address; + + size_t count; + BNLinearDisassemblyLine* lines = BNGetPreviousLinearDisassemblyLines(m_object, &linearPos, + settings ? settings->GetObject() : nullptr, &count); + + vector<LinearDisassemblyLine> result; + for (size_t i = 0; i < count; i++) + { + LinearDisassemblyLine line; + line.type = lines[i].type; + line.function = lines[i].function ? new Function(BNNewFunctionReference(lines[i].function)) : nullptr; + line.block = lines[i].block ? new BasicBlock(BNNewBasicBlockReference(lines[i].block)) : nullptr; + line.lineOffset = lines[i].lineOffset; + line.contents.addr = lines[i].contents.addr; + for (size_t j = 0; j < lines[i].contents.count; j++) + { + InstructionTextToken token; + token.type = lines[i].contents.tokens[j].type; + token.text = lines[i].contents.tokens[j].text; + token.value = lines[i].contents.tokens[j].value; + token.size = lines[i].contents.tokens[j].size; + token.operand = lines[i].contents.tokens[j].operand; + token.context = lines[i].contents.tokens[j].context; + token.address = lines[i].contents.tokens[j].address; + line.contents.tokens.push_back(token); + } + result.push_back(line); + } + + pos.function = linearPos.function ? new Function(linearPos.function) : nullptr; + pos.block = linearPos.block ? new BasicBlock(linearPos.block) : nullptr; + pos.address = linearPos.address; + + BNFreeLinearDisassemblyLines(lines, count); + return result; +} + + +vector<LinearDisassemblyLine> BinaryView::GetNextLinearDisassemblyLines(LinearDisassemblyPosition& pos, + DisassemblySettings* settings) +{ + BNLinearDisassemblyPosition linearPos; + linearPos.function = pos.function ? BNNewFunctionReference(pos.function->GetObject()) : nullptr; + linearPos.block = pos.block ? BNNewBasicBlockReference(pos.block->GetObject()) : nullptr; + linearPos.address = pos.address; + + size_t count; + BNLinearDisassemblyLine* lines = BNGetNextLinearDisassemblyLines(m_object, &linearPos, + settings ? settings->GetObject() : nullptr, &count); + + vector<LinearDisassemblyLine> result; + for (size_t i = 0; i < count; i++) + { + LinearDisassemblyLine line; + line.type = lines[i].type; + line.function = lines[i].function ? new Function(BNNewFunctionReference(lines[i].function)) : nullptr; + line.block = lines[i].block ? new BasicBlock(BNNewBasicBlockReference(lines[i].block)) : nullptr; + line.lineOffset = lines[i].lineOffset; + line.contents.addr = lines[i].contents.addr; + for (size_t j = 0; j < lines[i].contents.count; j++) + { + InstructionTextToken token; + token.type = lines[i].contents.tokens[j].type; + token.text = lines[i].contents.tokens[j].text; + token.value = lines[i].contents.tokens[j].value; + token.size = lines[i].contents.tokens[j].size; + token.operand = lines[i].contents.tokens[j].operand; + token.context = lines[i].contents.tokens[j].context; + token.address = lines[i].contents.tokens[j].address; + line.contents.tokens.push_back(token); + } + result.push_back(line); + } + + pos.function = linearPos.function ? new Function(linearPos.function) : nullptr; + pos.block = linearPos.block ? new BasicBlock(linearPos.block) : nullptr; + pos.address = linearPos.address; + + BNFreeLinearDisassemblyLines(lines, count); + return result; +} + + +bool BinaryView::ParseTypeString(const string& text, QualifiedNameAndType& result, string& errors) +{ + BNQualifiedNameAndType nt; + char* errorStr; + + if (!BNParseTypeString(m_object, text.c_str(), &nt, &errorStr)) + { + errors = errorStr; + BNFreeString(errorStr); + return false; + } + + result.name = QualifiedName::FromAPIObject(&nt.name); + result.type = new Type(BNNewTypeReference(nt.type)); + errors = ""; + BNFreeQualifiedNameAndType(&nt); + return true; +} + + +map<QualifiedName, Ref<Type>> BinaryView::GetTypes() +{ + size_t count; + BNQualifiedNameAndType* types = BNGetAnalysisTypeList(m_object, &count); + + map<QualifiedName, Ref<Type>> result; + for (size_t i = 0; i < count; i++) + { + QualifiedName name = QualifiedName::FromAPIObject(&types[i].name); + result[name] = new Type(BNNewTypeReference(types[i].type)); + } + + BNFreeTypeList(types, count); + return result; +} + + +Ref<Type> BinaryView::GetTypeByName(const QualifiedName& name) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + BNType* type = BNGetAnalysisTypeByName(m_object, &nameObj); + QualifiedName::FreeAPIObject(&nameObj); + + if (!type) + return nullptr; + return new Type(type); +} + + +Ref<Type> BinaryView::GetTypeById(const string& id) +{ + BNType* type = BNGetAnalysisTypeById(m_object, id.c_str()); + if (!type) + return nullptr; + return new Type(type); +} + + +QualifiedName BinaryView::GetTypeNameById(const string& id) +{ + BNQualifiedName name = BNGetAnalysisTypeNameById(m_object, id.c_str()); + QualifiedName result = QualifiedName::FromAPIObject(&name); + BNFreeQualifiedName(&name); + return result; +} + + +string BinaryView::GetTypeId(const QualifiedName& name) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + char* id = BNGetAnalysisTypeId(m_object, &nameObj); + QualifiedName::FreeAPIObject(&nameObj); + string result = id; + BNFreeString(id); + return result; +} + + +bool BinaryView::IsTypeAutoDefined(const QualifiedName& name) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + bool result = BNIsAnalysisTypeAutoDefined(m_object, &nameObj); + QualifiedName::FreeAPIObject(&nameObj); + return result; +} + + +QualifiedName BinaryView::DefineType(const string& id, const QualifiedName& defaultName, Ref<Type> type) +{ + BNQualifiedName nameObj = defaultName.GetAPIObject(); + BNQualifiedName regName = BNDefineAnalysisType(m_object, id.c_str(), &nameObj, type->GetObject()); + QualifiedName::FreeAPIObject(&nameObj); + QualifiedName result = QualifiedName::FromAPIObject(®Name); + BNFreeQualifiedName(®Name); + return result; +} + + +void BinaryView::DefineUserType(const QualifiedName& name, Ref<Type> type) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + BNDefineUserAnalysisType(m_object, &nameObj, type->GetObject()); + QualifiedName::FreeAPIObject(&nameObj); +} + + +void BinaryView::UndefineType(const string& id) +{ + BNUndefineAnalysisType(m_object, id.c_str()); +} + + +void BinaryView::UndefineUserType(const QualifiedName& name) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + BNUndefineUserAnalysisType(m_object, &nameObj); + QualifiedName::FreeAPIObject(&nameObj); +} + + +void BinaryView::RenameType(const QualifiedName& oldName, const QualifiedName& newName) +{ + BNQualifiedName oldNameObj = oldName.GetAPIObject(); + BNQualifiedName newNameObj = newName.GetAPIObject(); + BNRenameAnalysisType(m_object, &oldNameObj, &newNameObj); + QualifiedName::FreeAPIObject(&oldNameObj); + QualifiedName::FreeAPIObject(&newNameObj); +} + + +void BinaryView::RegisterPlatformTypes(Platform* platform) +{ + BNRegisterPlatformTypes(m_object, platform->GetObject()); +} + + +bool BinaryView::FindNextData(uint64_t start, const DataBuffer& data, uint64_t& result, BNFindFlag flags) +{ + return BNFindNextData(m_object, start, data.GetBufferObject(), &result, flags); +} + + +void BinaryView::Reanalyze() +{ + BNReanalyzeAllFunctions(m_object); +} + + +void BinaryView::ShowPlainTextReport(const string& title, const string& contents) +{ + BNShowPlainTextReport(m_object, title.c_str(), contents.c_str()); +} + + +void BinaryView::ShowMarkdownReport(const string& title, const string& contents, const string& plainText) +{ + BNShowMarkdownReport(m_object, title.c_str(), contents.c_str(), plainText.c_str()); +} + + +void BinaryView::ShowHTMLReport(const string& title, const string& contents, const string& plainText) +{ + BNShowHTMLReport(m_object, title.c_str(), contents.c_str(), plainText.c_str()); +} + + +bool BinaryView::GetAddressInput(uint64_t& result, const string& prompt, const string& title) +{ + uint64_t currentAddress = 0; + if (m_file) + currentAddress = m_file->GetCurrentOffset(); + return BNGetAddressInput(&result, prompt.c_str(), title.c_str(), m_object, currentAddress); +} + + +bool BinaryView::GetAddressInput(uint64_t& result, const string& prompt, const string& title, uint64_t currentAddress) +{ + return BNGetAddressInput(&result, prompt.c_str(), title.c_str(), m_object, currentAddress); +} + + +void BinaryView::AddAutoSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, + uint32_t flags) +{ + BNAddAutoSegment(m_object, start, length, dataOffset, dataLength, flags); +} + + +void BinaryView::RemoveAutoSegment(uint64_t start, uint64_t length) +{ + BNRemoveAutoSegment(m_object, start, length); +} + + +void BinaryView::AddUserSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, + uint32_t flags) +{ + BNAddUserSegment(m_object, start, length, dataOffset, dataLength, flags); +} + + +void BinaryView::RemoveUserSegment(uint64_t start, uint64_t length) +{ + BNRemoveUserSegment(m_object, start, length); +} + + +vector<Segment> BinaryView::GetSegments() +{ + size_t count; + BNSegment* segments = BNGetSegments(m_object, &count); + + vector<Segment> result; + for (size_t i = 0; i < count; i++) + { + Segment segment; + segment.start = segments[i].start; + segment.length = segments[i].length; + segment.dataOffset = segments[i].dataOffset; + segment.dataLength = segments[i].dataLength; + segment.flags = segments[i].flags; + result.push_back(segment); + } + + BNFreeSegmentList(segments); + return result; +} + + +bool BinaryView::GetSegmentAt(uint64_t addr, Segment& result) +{ + BNSegment segment; + if (!BNGetSegmentAt(m_object, addr, &segment)) + return false; + + result.start = segment.start; + result.length = segment.length; + result.dataOffset = segment.dataOffset; + result.dataLength = segment.dataLength; + result.flags = segment.flags; + return true; +} + + +bool BinaryView::GetAddressForDataOffset(uint64_t offset, uint64_t& addr) +{ + return BNGetAddressForDataOffset(m_object, offset, &addr); +} + + +void BinaryView::AddAutoSection(const string& name, uint64_t start, uint64_t length, const string& type, + uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData) +{ + BNAddAutoSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(), + infoSection.c_str(), infoData); +} + + +void BinaryView::RemoveAutoSection(const string& name) +{ + BNRemoveAutoSection(m_object, name.c_str()); +} + + +void BinaryView::AddUserSection(const string& name, uint64_t start, uint64_t length, const string& type, + uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData) +{ + BNAddUserSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(), + infoSection.c_str(), infoData); +} + + +void BinaryView::RemoveUserSection(const string& name) +{ + BNRemoveUserSection(m_object, name.c_str()); +} + + +vector<Section> BinaryView::GetSections() +{ + size_t count; + BNSection* sections = BNGetSections(m_object, &count); + + vector<Section> result; + for (size_t i = 0; i < count; i++) + { + Section section; + section.name = sections[i].name; + section.type = sections[i].type; + section.start = sections[i].start; + section.length = sections[i].length; + section.linkedSection = sections[i].linkedSection; + section.infoSection = sections[i].infoSection; + section.infoData = sections[i].infoData; + section.align = sections[i].align; + section.entrySize = sections[i].entrySize; + result.push_back(section); + } + + BNFreeSectionList(sections, count); + return result; +} + + +vector<Section> BinaryView::GetSectionsAt(uint64_t addr) +{ + size_t count; + BNSection* sections = BNGetSectionsAt(m_object, addr, &count); + + vector<Section> result; + for (size_t i = 0; i < count; i++) + { + Section section; + section.name = sections[i].name; + section.type = sections[i].type; + section.start = sections[i].start; + section.length = sections[i].length; + section.linkedSection = sections[i].linkedSection; + section.infoSection = sections[i].infoSection; + section.infoData = sections[i].infoData; + section.align = sections[i].align; + section.entrySize = sections[i].entrySize; + result.push_back(section); + } + + BNFreeSectionList(sections, count); + return result; +} + + +bool BinaryView::GetSectionByName(const string& name, Section& result) +{ + BNSection section; + if (!BNGetSectionByName(m_object, name.c_str(), §ion)) + return false; + + result.name = section.name; + result.type = section.type; + result.start = section.start; + result.length = section.length; + result.linkedSection = section.linkedSection; + result.infoSection = section.infoSection; + result.infoData = section.infoData; + result.align = section.align; + result.entrySize = section.entrySize; + + BNFreeSection(§ion); + return true; +} + + +vector<string> BinaryView::GetUniqueSectionNames(const vector<string>& names) +{ + const char** incomingNames = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + incomingNames[i] = names[i].c_str(); + + char** outgoingNames = BNGetUniqueSectionNames(m_object, incomingNames, names.size()); + vector<string> result; + for (size_t i = 0; i < names.size(); i++) + result.push_back(outgoingNames[i]); + + BNFreeStringList(outgoingNames, names.size()); + return result; +} + + +vector<BNAddressRange> BinaryView::GetAllocatedRanges() +{ + size_t count; + BNAddressRange* ranges = BNGetAllocatedRanges(m_object, &count); + + vector<BNAddressRange> result; + copy(&ranges[0], &ranges[count], back_inserter(result)); + BNFreeAddressRanges(ranges); + return result; +} + + BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetObject())) { } |
