diff options
| -rw-r--r-- | architecture.cpp | 101 | ||||
| -rw-r--r-- | basicblock.cpp | 25 | ||||
| -rw-r--r-- | binaryninjaapi.h | 209 | ||||
| -rw-r--r-- | binaryreader.cpp | 2 | ||||
| -rw-r--r-- | binaryview.cpp | 207 | ||||
| -rw-r--r-- | binaryviewtype.cpp | 33 | ||||
| -rw-r--r-- | binarywriter.cpp | 2 | ||||
| -rw-r--r-- | callingconvention.cpp | 32 | ||||
| -rw-r--r-- | filemetadata.cpp | 64 | ||||
| -rw-r--r-- | function.cpp | 57 | ||||
| -rw-r--r-- | functiongraphblock.cpp | 27 | ||||
| -rw-r--r-- | functionrecognizer.cpp | 2 | ||||
| -rw-r--r-- | lowlevelil.cpp | 41 | ||||
| -rw-r--r-- | platform.cpp | 45 | ||||
| -rw-r--r-- | plugin.cpp | 17 | ||||
| -rw-r--r-- | tempfile.cpp | 24 | ||||
| -rw-r--r-- | transform.cpp | 27 | ||||
| -rw-r--r-- | type.cpp | 111 |
18 files changed, 519 insertions, 507 deletions
diff --git a/architecture.cpp b/architecture.cpp index 2420d69b..af0ec4c8 100644 --- a/architecture.cpp +++ b/architecture.cpp @@ -22,7 +22,7 @@ void InstructionInfo::AddBranch(BNBranchType type, uint64_t target, Architecture branchDelay = hasDelaySlot; branchType[branchCount] = type; branchTarget[branchCount] = target; - branchArch[branchCount++] = arch ? arch->GetArchitectureObject() : nullptr; + branchArch[branchCount++] = arch ? arch->GetObject() : nullptr; } @@ -37,20 +37,22 @@ InstructionTextToken::InstructionTextToken(BNInstructionTextTokenType t, const s } -Architecture::Architecture(BNArchitecture* arch): m_arch(arch) +Architecture::Architecture(BNArchitecture* arch) { + m_object = arch; } Architecture::Architecture(const string& name): m_nameForRegister(name) { + m_object = nullptr; } void Architecture::InitCallback(void* ctxt, BNArchitecture* obj) { Architecture* arch = (Architecture*)ctxt; - arch->m_arch = obj; + arch->m_object = obj; } @@ -342,6 +344,7 @@ void Architecture::Register(Architecture* arch) callbacks.alwaysBranch = AlwaysBranchCallback; callbacks.invertBranch = InvertBranchCallback; callbacks.skipAndReturnValue = SkipAndReturnValueCallback; + arch->AddRefForRegistration(); BNRegisterArchitecture(arch->m_nameForRegister.c_str(), &callbacks); } @@ -372,7 +375,7 @@ vector<Ref<Architecture>> Architecture::GetList() string Architecture::GetName() const { - char* name = BNGetArchitectureName(m_arch); + char* name = BNGetArchitectureName(m_object); string result = name; BNFreeString(name); return result; @@ -468,7 +471,7 @@ uint32_t Architecture::GetLinkRegister() vector<uint32_t> Architecture::GetModifiedRegistersOnWrite(uint32_t reg) { size_t count; - uint32_t* regs = BNGetModifiedArchitectureRegistersOnWrite(m_arch, reg, &count); + uint32_t* regs = BNGetModifiedArchitectureRegistersOnWrite(m_object, reg, &count); vector<uint32_t> result; for (size_t i = 0; i < count; i++) @@ -481,7 +484,7 @@ vector<uint32_t> Architecture::GetModifiedRegistersOnWrite(uint32_t reg) uint32_t Architecture::GetRegisterByName(const string& name) { - return BNGetArchitectureRegisterByName(m_arch, name.c_str()); + return BNGetArchitectureRegisterByName(m_object, name.c_str()); } @@ -554,19 +557,19 @@ void Architecture::RegisterFunctionRecognizer(FunctionRecognizer* recog) bool Architecture::IsBinaryViewTypeConstantDefined(const string& type, const string& name) { - return BNIsBinaryViewTypeArchitectureConstantDefined(m_arch, type.c_str(), name.c_str()); + return BNIsBinaryViewTypeArchitectureConstantDefined(m_object, type.c_str(), name.c_str()); } uint64_t Architecture::GetBinaryViewTypeConstant(const string& type, const string& name, uint64_t defaultValue) { - return BNGetBinaryViewTypeArchitectureConstant(m_arch, type.c_str(), name.c_str(), defaultValue); + return BNGetBinaryViewTypeArchitectureConstant(m_object, type.c_str(), name.c_str(), defaultValue); } void Architecture::SetBinaryViewTypeConstant(const string& type, const string& name, uint64_t value) { - BNSetBinaryViewTypeArchitectureConstant(m_arch, type.c_str(), name.c_str(), value); + BNSetBinaryViewTypeArchitectureConstant(m_object, type.c_str(), name.c_str(), value); } @@ -586,7 +589,7 @@ bool Architecture::ParseTypesFromSource(const string& source, const string& file variables.clear(); functions.clear(); - bool ok = BNParseTypesFromSource(m_arch, source.c_str(), fileName.c_str(), &result, + bool ok = BNParseTypesFromSource(m_object, source.c_str(), fileName.c_str(), &result, &errorStr, includeDirList, includeDirs.size()); errors = errorStr; BNFreeString(errorStr); @@ -619,7 +622,7 @@ bool Architecture::ParseTypesFromSourceFile(const string& fileName, map<string, variables.clear(); functions.clear(); - bool ok = BNParseTypesFromSourceFile(m_arch, fileName.c_str(), &result, &errorStr, + bool ok = BNParseTypesFromSourceFile(m_object, fileName.c_str(), &result, &errorStr, includeDirList, includeDirs.size()); errors = errorStr; BNFreeString(errorStr); @@ -639,14 +642,14 @@ bool Architecture::ParseTypesFromSourceFile(const string& fileName, map<string, void Architecture::RegisterCallingConvention(CallingConvention* cc) { - BNRegisterCallingConvention(m_arch, cc->GetCallingConventionObject()); + BNRegisterCallingConvention(m_object, cc->GetObject()); } vector<Ref<CallingConvention>> Architecture::GetCallingConventions() { size_t count; - BNCallingConvention** list = BNGetArchitectureCallingConventions(m_arch, &count); + BNCallingConvention** list = BNGetArchitectureCallingConventions(m_object, &count); vector<Ref<CallingConvention>> result; for (size_t i = 0; i < count; i++) @@ -659,7 +662,7 @@ vector<Ref<CallingConvention>> Architecture::GetCallingConventions() Ref<CallingConvention> Architecture::GetCallingConventionByName(const string& name) { - BNCallingConvention* cc = BNGetArchitectureCallingConventionByName(m_arch, name.c_str()); + BNCallingConvention* cc = BNGetArchitectureCallingConventionByName(m_object, name.c_str()); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -668,31 +671,31 @@ Ref<CallingConvention> Architecture::GetCallingConventionByName(const string& na void Architecture::SetDefaultCallingConvention(CallingConvention* cc) { - BNSetArchitectureDefaultCallingConvention(m_arch, cc->GetCallingConventionObject()); + BNSetArchitectureDefaultCallingConvention(m_object, cc->GetObject()); } void Architecture::SetCdeclCallingConvention(CallingConvention* cc) { - BNSetArchitectureCdeclCallingConvention(m_arch, cc->GetCallingConventionObject()); + BNSetArchitectureCdeclCallingConvention(m_object, cc->GetObject()); } void Architecture::SetStdcallCallingConvention(CallingConvention* cc) { - BNSetArchitectureStdcallCallingConvention(m_arch, cc->GetCallingConventionObject()); + BNSetArchitectureStdcallCallingConvention(m_object, cc->GetObject()); } void Architecture::SetFastcallCallingConvention(CallingConvention* cc) { - BNSetArchitectureFastcallCallingConvention(m_arch, cc->GetCallingConventionObject()); + BNSetArchitectureFastcallCallingConvention(m_object, cc->GetObject()); } Ref<CallingConvention> Architecture::GetDefaultCallingConvention() { - BNCallingConvention* cc = BNGetArchitectureDefaultCallingConvention(m_arch); + BNCallingConvention* cc = BNGetArchitectureDefaultCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -701,7 +704,7 @@ Ref<CallingConvention> Architecture::GetDefaultCallingConvention() Ref<CallingConvention> Architecture::GetCdeclCallingConvention() { - BNCallingConvention* cc = BNGetArchitectureCdeclCallingConvention(m_arch); + BNCallingConvention* cc = BNGetArchitectureCdeclCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -710,7 +713,7 @@ Ref<CallingConvention> Architecture::GetCdeclCallingConvention() Ref<CallingConvention> Architecture::GetStdcallCallingConvention() { - BNCallingConvention* cc = BNGetArchitectureStdcallCallingConvention(m_arch); + BNCallingConvention* cc = BNGetArchitectureStdcallCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -719,7 +722,7 @@ Ref<CallingConvention> Architecture::GetStdcallCallingConvention() Ref<CallingConvention> Architecture::GetFastcallCallingConvention() { - BNCallingConvention* cc = BNGetArchitectureFastcallCallingConvention(m_arch); + BNCallingConvention* cc = BNGetArchitectureFastcallCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -728,7 +731,7 @@ Ref<CallingConvention> Architecture::GetFastcallCallingConvention() Ref<Platform> Architecture::GetStandalonePlatform() { - return new Platform(BNGetArchitectureStandalonePlatform(m_arch)); + return new Platform(BNGetArchitectureStandalonePlatform(m_object)); } @@ -739,25 +742,25 @@ CoreArchitecture::CoreArchitecture(BNArchitecture* arch): Architecture(arch) BNEndianness CoreArchitecture::GetEndianness() const { - return BNGetArchitectureEndianness(m_arch); + return BNGetArchitectureEndianness(m_object); } size_t CoreArchitecture::GetAddressSize() const { - return BNGetArchitectureAddressSize(m_arch); + return BNGetArchitectureAddressSize(m_object); } size_t CoreArchitecture::GetDefaultIntegerSize() const { - return BNGetArchitectureDefaultIntegerSize(m_arch); + return BNGetArchitectureDefaultIntegerSize(m_object); } bool CoreArchitecture::GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) { - return BNGetInstructionInfo(m_arch, data, addr, maxLen, &result); + return BNGetInstructionInfo(m_object, data, addr, maxLen, &result); } @@ -765,7 +768,7 @@ bool CoreArchitecture::GetInstructionText(const uint8_t* data, uint64_t addr, si { BNInstructionTextToken* tokens = nullptr; size_t count = 0; - if (!BNGetInstructionText(m_arch, data, addr, &len, &tokens, &count)) + if (!BNGetInstructionText(m_object, data, addr, &len, &tokens, &count)) return false; for (size_t i = 0; i < count; i++) @@ -778,13 +781,13 @@ bool CoreArchitecture::GetInstructionText(const uint8_t* data, uint64_t addr, si bool CoreArchitecture::GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il) { - return BNGetInstructionLowLevelIL(m_arch, data, addr, &len, il.GetFunctionObject()); + return BNGetInstructionLowLevelIL(m_object, data, addr, &len, il.GetObject()); } string CoreArchitecture::GetRegisterName(uint32_t reg) { - char* name = BNGetArchitectureRegisterName(m_arch, reg); + char* name = BNGetArchitectureRegisterName(m_object, reg); string result = name; BNFreeString(name); return result; @@ -793,7 +796,7 @@ string CoreArchitecture::GetRegisterName(uint32_t reg) string CoreArchitecture::GetFlagName(uint32_t flag) { - char* name = BNGetArchitectureFlagName(m_arch, flag); + char* name = BNGetArchitectureFlagName(m_object, flag); string result = name; BNFreeString(name); return result; @@ -802,7 +805,7 @@ string CoreArchitecture::GetFlagName(uint32_t flag) string CoreArchitecture::GetFlagWriteTypeName(uint32_t flags) { - char* name = BNGetArchitectureFlagWriteTypeName(m_arch, flags); + char* name = BNGetArchitectureFlagWriteTypeName(m_object, flags); string result = name; BNFreeString(name); return result; @@ -812,7 +815,7 @@ string CoreArchitecture::GetFlagWriteTypeName(uint32_t flags) vector<uint32_t> CoreArchitecture::GetFullWidthRegisters() { size_t count; - uint32_t* regs = BNGetFullWidthArchitectureRegisters(m_arch, &count); + uint32_t* regs = BNGetFullWidthArchitectureRegisters(m_object, &count); vector<uint32_t> result; for (size_t i = 0; i < count; i++) @@ -826,7 +829,7 @@ vector<uint32_t> CoreArchitecture::GetFullWidthRegisters() vector<uint32_t> CoreArchitecture::GetAllRegisters() { size_t count; - uint32_t* regs = BNGetAllArchitectureRegisters(m_arch, &count); + uint32_t* regs = BNGetAllArchitectureRegisters(m_object, &count); vector<uint32_t> result; for (size_t i = 0; i < count; i++) @@ -840,7 +843,7 @@ vector<uint32_t> CoreArchitecture::GetAllRegisters() vector<uint32_t> CoreArchitecture::GetAllFlags() { size_t count; - uint32_t* regs = BNGetAllArchitectureFlags(m_arch, &count); + uint32_t* regs = BNGetAllArchitectureFlags(m_object, &count); vector<uint32_t> result; for (size_t i = 0; i < count; i++) @@ -854,7 +857,7 @@ vector<uint32_t> CoreArchitecture::GetAllFlags() vector<uint32_t> CoreArchitecture::GetAllFlagWriteTypes() { size_t count; - uint32_t* regs = BNGetAllArchitectureFlagWriteTypes(m_arch, &count); + uint32_t* regs = BNGetAllArchitectureFlagWriteTypes(m_object, &count); vector<uint32_t> result; for (size_t i = 0; i < count; i++) @@ -867,26 +870,26 @@ vector<uint32_t> CoreArchitecture::GetAllFlagWriteTypes() BNRegisterInfo CoreArchitecture::GetRegisterInfo(uint32_t reg) { - return BNGetArchitectureRegisterInfo(m_arch, reg); + return BNGetArchitectureRegisterInfo(m_object, reg); } uint32_t CoreArchitecture::GetStackPointerRegister() { - return BNGetArchitectureStackPointerRegister(m_arch); + return BNGetArchitectureStackPointerRegister(m_object); } uint32_t CoreArchitecture::GetLinkRegister() { - return BNGetArchitectureLinkRegister(m_arch); + return BNGetArchitectureLinkRegister(m_object); } bool CoreArchitecture::Assemble(const string& code, uint64_t addr, DataBuffer& result, string& errors) { char* errorStr = nullptr; - bool ok = BNAssemble(m_arch, code.c_str(), addr, result.GetBufferObject(), &errorStr); + bool ok = BNAssemble(m_object, code.c_str(), addr, result.GetBufferObject(), &errorStr); if (errorStr) { errors = errorStr; @@ -898,53 +901,53 @@ bool CoreArchitecture::Assemble(const string& code, uint64_t addr, DataBuffer& r bool CoreArchitecture::IsNeverBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) { - return BNIsArchitectureNeverBranchPatchAvailable(m_arch, data, addr, len); + return BNIsArchitectureNeverBranchPatchAvailable(m_object, data, addr, len); } bool CoreArchitecture::IsAlwaysBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) { - return BNIsArchitectureAlwaysBranchPatchAvailable(m_arch, data, addr, len); + return BNIsArchitectureAlwaysBranchPatchAvailable(m_object, data, addr, len); } bool CoreArchitecture::IsInvertBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) { - return BNIsArchitectureInvertBranchPatchAvailable(m_arch, data, addr, len); + return BNIsArchitectureInvertBranchPatchAvailable(m_object, data, addr, len); } bool CoreArchitecture::IsSkipAndReturnZeroPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) { - return BNIsArchitectureSkipAndReturnZeroPatchAvailable(m_arch, data, addr, len); + return BNIsArchitectureSkipAndReturnZeroPatchAvailable(m_object, data, addr, len); } bool CoreArchitecture::IsSkipAndReturnValuePatchAvailable(const uint8_t* data, uint64_t addr, size_t len) { - return BNIsArchitectureSkipAndReturnValuePatchAvailable(m_arch, data, addr, len); + return BNIsArchitectureSkipAndReturnValuePatchAvailable(m_object, data, addr, len); } bool CoreArchitecture::ConvertToNop(uint8_t* data, uint64_t addr, size_t len) { - return BNArchitectureConvertToNop(m_arch, data, addr, len); + return BNArchitectureConvertToNop(m_object, data, addr, len); } bool CoreArchitecture::AlwaysBranch(uint8_t* data, uint64_t addr, size_t len) { - return BNArchitectureAlwaysBranch(m_arch, data, addr, len); + return BNArchitectureAlwaysBranch(m_object, data, addr, len); } bool CoreArchitecture::InvertBranch(uint8_t* data, uint64_t addr, size_t len) { - return BNArchitectureInvertBranch(m_arch, data, addr, len); + return BNArchitectureInvertBranch(m_object, data, addr, len); } bool CoreArchitecture::SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value) { - return BNArchitectureSkipAndReturnValue(m_arch, data, addr, len, value); + return BNArchitectureSkipAndReturnValue(m_object, data, addr, len, value); } diff --git a/basicblock.cpp b/basicblock.cpp index e27cdfa2..1c86a93b 100644 --- a/basicblock.cpp +++ b/basicblock.cpp @@ -4,51 +4,46 @@ using namespace BinaryNinja; using namespace std; -BasicBlock::BasicBlock(BNBasicBlock* block): m_block(block) +BasicBlock::BasicBlock(BNBasicBlock* block) { -} - - -BasicBlock::~BasicBlock() -{ - BNFreeBasicBlock(m_block); + m_object = block; } Ref<Function> BasicBlock::GetFunction() const { - return new Function(BNGetBasicBlockFunction(m_block)); + return new Function(BNGetBasicBlockFunction(m_object)); } Ref<Architecture> BasicBlock::GetArchitecture() const { - return new CoreArchitecture(BNGetBasicBlockArchitecture(m_block)); + return new CoreArchitecture(BNGetBasicBlockArchitecture(m_object)); } uint64_t BasicBlock::GetStart() const { - return BNGetBasicBlockStart(m_block); + return BNGetBasicBlockStart(m_object); } uint64_t BasicBlock::GetEnd() const { - return BNGetBasicBlockEnd(m_block); + return BNGetBasicBlockEnd(m_object); } uint64_t BasicBlock::GetLength() const { - return BNGetBasicBlockLength(m_block); + return BNGetBasicBlockLength(m_object); } vector<BasicBlockEdge> BasicBlock::GetOutgoingEdges() const { size_t count; - BNBasicBlockEdge* array = BNGetBasicBlockOutgoingEdges(m_block, &count); + BNBasicBlockEdge* array = BNGetBasicBlockOutgoingEdges(m_object, &count); vector<BasicBlockEdge> result; for (size_t i = 0; i < count; i++) @@ -67,11 +62,11 @@ vector<BasicBlockEdge> BasicBlock::GetOutgoingEdges() const bool BasicBlock::HasUndeterminedOutgoingEdges() const { - return BNBasicBlockHasUndeterminedOutgoingEdges(m_block); + return BNBasicBlockHasUndeterminedOutgoingEdges(m_object); } void BasicBlock::MarkRecentUse() { - BNMarkBasicBlockAsRecentlyUsed(m_block); + BNMarkBasicBlockAsRecentlyUsed(m_object); } diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 43dd3b3d..c57bdf3a 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -49,6 +49,110 @@ namespace BinaryNinja } }; + template <class T, T* (*AddObjectReference)(T*), void (*FreeObjectReference)(T*)> + class CoreRefCountObject + { + void AddRefInternal() + { +#ifdef WIN32 + InterlockedIncrement((LONG*)&m_refs); +#else + __sync_fetch_and_add(&m_refs, 1); +#endif + } + + void ReleaseInternal() + { +#ifdef WIN32 + if (InterlockedDecrement((LONG*)&m_refs) == 0) + delete this; +#else + if (__sync_fetch_and_add(&m_refs, -1) == 1) + delete this; +#endif + } + + public: + int m_refs; + T* m_object; + CoreRefCountObject(): m_refs(0), m_object(nullptr) {} + virtual ~CoreRefCountObject() {} + + T* GetObject() { return m_object; } + + void AddRef() + { + if (m_object && (m_refs != 0)) + AddObjectReference(m_object); + AddRefInternal(); + } + + void Release() + { + if (m_object) + FreeObjectReference(m_object); + ReleaseInternal(); + } + + void AddRefForRegistration() + { + AddRefInternal(); + } + + void ReleaseForRegistration() + { + m_object = nullptr; + ReleaseInternal(); + } + }; + + template <class T> + class StaticCoreRefCountObject + { + void AddRefInternal() + { +#ifdef WIN32 + InterlockedIncrement((LONG*)&m_refs); +#else + __sync_fetch_and_add(&m_refs, 1); +#endif + } + + void ReleaseInternal() + { +#ifdef WIN32 + if (InterlockedDecrement((LONG*)&m_refs) == 0) + delete this; +#else + if (__sync_fetch_and_add(&m_refs, -1) == 1) + delete this; +#endif + } + + public: + int m_refs; + T* m_object; + StaticCoreRefCountObject(): m_refs(0), m_object(nullptr) {} + virtual ~StaticCoreRefCountObject() {} + + T* GetObject() { return m_object; } + + void AddRef() + { + AddRefInternal(); + } + + void Release() + { + ReleaseInternal(); + } + + void AddRefForRegistration() + { + AddRefInternal(); + } + }; + template <class T> class Ref { @@ -236,18 +340,15 @@ namespace BinaryNinja bool ZlibDecompress(DataBuffer& output) const; }; - class TemporaryFile: public RefCountObject + class TemporaryFile: public CoreRefCountObject<BNTemporaryFile, BNNewTemporaryFileReference, BNFreeTemporaryFile> { - BNTemporaryFile* m_file; - public: TemporaryFile(); TemporaryFile(const DataBuffer& contents); TemporaryFile(const std::string& contents); TemporaryFile(BNTemporaryFile* file); - ~TemporaryFile(); - bool IsValid() const { return m_file != nullptr; } + bool IsValid() const { return m_object != nullptr; } std::string GetPath() const; DataBuffer GetContents(); }; @@ -280,6 +381,7 @@ namespace BinaryNinja std::string m_typeName; BNActionType m_actionType; + static void FreeCallback(void* ctxt); static void UndoCallback(void* ctxt, BNBinaryView* data); static void RedoCallback(void* ctxt, BNBinaryView* data); static char* SerializeCallback(void* ctxt); @@ -315,17 +417,12 @@ namespace BinaryNinja virtual UndoAction* Deserialize(const Json::Value& data) = 0; }; - class FileMetadata: public RefCountObject + class FileMetadata: public CoreRefCountObject<BNFileMetadata, BNNewFileReference, BNFreeFileMetadata> { - BNFileMetadata* m_file; - public: FileMetadata(); FileMetadata(const std::string& filename); FileMetadata(BNFileMetadata* file); - ~FileMetadata(); - - BNFileMetadata* GetFileObject() const { return m_file; } void Close(); @@ -427,18 +524,13 @@ namespace BinaryNinja class Function; class BasicBlock; - class Symbol: public RefCountObject + class Symbol: public CoreRefCountObject<BNSymbol, BNNewSymbolReference, BNFreeSymbol> { - BNSymbol* m_sym; - public: Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName, const std::string& rawName, uint64_t addr); Symbol(BNSymbolType type, const std::string& name, uint64_t addr); Symbol(BNSymbol* sym); - virtual ~Symbol(); - - BNSymbol* GetSymbolObject() const { return m_sym; } BNSymbolType GetType() const; std::string GetShortName() const; @@ -458,10 +550,9 @@ namespace BinaryNinja uint64_t addr; }; - class BinaryView: public RefCountObject + class BinaryView: public CoreRefCountObject<BNBinaryView, BNNewViewReference, BNFreeBinaryView> { protected: - BNBinaryView* m_view; Ref<FileMetadata> m_file; BinaryView(const std::string& typeName, FileMetadata* file); @@ -492,6 +583,7 @@ namespace BinaryNinja private: static bool InitCallback(void* ctxt); + static void FreeCallback(void* ctxt); static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len); static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len); static size_t InsertCallback(void* ctxt, uint64_t offset, const void* src, size_t len); @@ -512,12 +604,10 @@ namespace BinaryNinja public: BinaryView(BNBinaryView* view); - virtual ~BinaryView(); virtual bool Init() { return true; } FileMetadata* GetFile() const { return m_file; } - BNBinaryView* GetViewObject() const { return m_view; } std::string GetTypeName() const; bool IsModified() const; @@ -641,10 +731,9 @@ namespace BinaryNinja class Platform; - class BinaryViewType: public RefCountObject + class BinaryViewType: public StaticCoreRefCountObject<BNBinaryViewType> { protected: - BNBinaryViewType* m_type; std::string m_nameForRegister, m_longNameForRegister; static BNBinaryView* CreateCallback(void* ctxt, BNBinaryView* data); @@ -798,10 +887,9 @@ namespace BinaryNinja size_t fixedLength; // Variable length if zero }; - class Transform: public RefCountObject + class Transform: public StaticCoreRefCountObject<BNTransform> { protected: - BNTransform* m_xform; BNTransformType m_typeForRegister; std::string m_nameForRegister, m_longNameForRegister, m_groupForRegister; @@ -869,10 +957,9 @@ namespace BinaryNinja typedef size_t ExprId; - class Architecture: public RefCountObject + class Architecture: public StaticCoreRefCountObject<BNArchitecture> { protected: - BNArchitecture* m_arch; std::string m_nameForRegister; Architecture(BNArchitecture* arch); @@ -916,8 +1003,6 @@ namespace BinaryNinja public: Architecture(const std::string& name); - BNArchitecture* GetArchitectureObject() const { return m_arch; } - static void Register(Architecture* arch); static Ref<Architecture> GetByName(const std::string& name); static std::vector<Ref<Architecture>> GetList(); @@ -1035,15 +1120,10 @@ namespace BinaryNinja Ref<Type> type; }; - class Type: public RefCountObject + class Type: public CoreRefCountObject<BNType, BNNewTypeReference, BNFreeType> { - BNType* m_type; - public: Type(BNType* type); - ~Type(); - - BNType* GetTypeObject() const { return m_type; } BNTypeClass GetClass() const; uint64_t GetWidth() const; @@ -1085,15 +1165,10 @@ namespace BinaryNinja uint64_t offset; }; - class Structure: public RefCountObject + class Structure: public CoreRefCountObject<BNStructure, BNNewStructureReference, BNFreeStructure> { - BNStructure* m_struct; - public: Structure(BNStructure* s); - ~Structure(); - - BNStructure* GetStructureObject() const { return m_struct; } std::string GetName() const; void SetName(const std::string& name); @@ -1117,15 +1192,10 @@ namespace BinaryNinja bool isDefault; }; - class Enumeration: public RefCountObject + class Enumeration: public CoreRefCountObject<BNEnumeration, BNNewEnumerationReference, BNFreeEnumeration> { - BNEnumeration* m_enum; - public: Enumeration(BNEnumeration* e); - ~Enumeration(); - - BNEnumeration* GetEnumerationObject() const { return m_enum; } std::string GetName() const; void SetName(const std::string& name); @@ -1145,13 +1215,10 @@ namespace BinaryNinja Ref<Architecture> arch; }; - class BasicBlock: public RefCountObject + class BasicBlock: public CoreRefCountObject<BNBasicBlock, BNNewBasicBlockReference, BNFreeBasicBlock> { - BNBasicBlock* m_block; - public: BasicBlock(BNBasicBlock* block); - ~BasicBlock(); Ref<Function> GetFunction() const; Ref<Architecture> GetArchitecture() const; @@ -1168,15 +1235,10 @@ namespace BinaryNinja class FunctionGraph; - class Function: public RefCountObject + class Function: public CoreRefCountObject<BNFunction, BNNewFunctionReference, BNFreeFunction> { - BNFunction* m_func; - public: Function(BNFunction* func); - ~Function(); - - BNFunction* GetFunctionObject() const { return m_func; } Ref<Architecture> GetArchitecture() const; Ref<Platform> GetPlatform() const; @@ -1223,15 +1285,11 @@ namespace BinaryNinja std::vector<BNPoint> points; }; - class FunctionGraphBlock: public RefCountObject + class FunctionGraphBlock: public CoreRefCountObject<BNFunctionGraphBlock, + BNNewFunctionGraphBlockReference, BNFreeFunctionGraphBlock> { - BNFunctionGraphBlock* m_block; - public: FunctionGraphBlock(BNFunctionGraphBlock* block); - ~FunctionGraphBlock(); - - BNFunctionGraphBlock* GetBlockObject() const { return m_block; } Ref<Architecture> GetArchitecture() const; uint64_t GetStart() const; @@ -1287,16 +1345,12 @@ namespace BinaryNinja LowLevelILLabel(); }; - class LowLevelILFunction: public RefCountObject + class LowLevelILFunction: public CoreRefCountObject<BNLowLevelILFunction, + BNNewLowLevelILFunctionReference, BNFreeLowLevelILFunction> { - BNLowLevelILFunction* m_func; - public: LowLevelILFunction(); LowLevelILFunction(BNLowLevelILFunction* func); - ~LowLevelILFunction(); - - BNLowLevelILFunction* GetFunctionObject() const { return m_func; } uint64_t GetCurrentAddress() const; void SetCurrentAddress(uint64_t addr); @@ -1516,14 +1570,15 @@ namespace BinaryNinja void Execute(const PluginCommandContext& ctxt) const; }; - class CallingConvention: public RefCountObject + class CallingConvention: public CoreRefCountObject<BNCallingConvention, + BNNewCallingConventionReference, BNFreeCallingConvention> { protected: - BNCallingConvention* m_callingConvention; - CallingConvention(BNCallingConvention* cc); CallingConvention(Architecture* arch, const std::string& name); + static void FreeCallback(void* ctxt); + static uint32_t* GetCallerSavedRegistersCallback(void* ctxt, size_t* count); static uint32_t* GetIntegerArgumentRegistersCallback(void* ctxt, size_t* count); static uint32_t* GetFloatArgumentRegistersCallback(void* ctxt, size_t* count); @@ -1537,9 +1592,6 @@ namespace BinaryNinja static uint32_t GetFloatReturnValueRegisterCallback(void* ctxt); public: - virtual ~CallingConvention(); - - BNCallingConvention* GetCallingConventionObject() const { return m_callingConvention; } Ref<Architecture> GetArchitecture() const; std::string GetName() const; @@ -1572,18 +1624,13 @@ namespace BinaryNinja virtual uint32_t GetFloatReturnValueRegister() override; }; - class Platform: public RefCountObject + class Platform: public CoreRefCountObject<BNPlatform, BNNewPlatformReference, BNFreePlatform> { protected: - BNPlatform* m_platform; - Platform(Architecture* arch, const std::string& name); public: Platform(BNPlatform* platform); - virtual ~Platform(); - - BNPlatform* GetPlatformObject() const { return m_platform; } Ref<Architecture> GetArchitecture() const; std::string GetName() const; diff --git a/binaryreader.cpp b/binaryreader.cpp index b30a64b0..54e03977 100644 --- a/binaryreader.cpp +++ b/binaryreader.cpp @@ -6,7 +6,7 @@ using namespace std; BinaryReader::BinaryReader(BinaryView* data, BNEndianness endian): m_view(data) { - m_stream = BNCreateBinaryReader(data->GetViewObject()); + m_stream = BNCreateBinaryReader(data->GetObject()); BNSetBinaryReaderEndianness(m_stream, endian); } diff --git a/binaryview.cpp b/binaryview.cpp index b62d0290..cbb70166 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -87,37 +87,31 @@ BinaryDataNotification::BinaryDataNotification() Symbol::Symbol(BNSymbolType type, const string& shortName, const string& fullName, const string& rawName, uint64_t addr) { - m_sym = BNCreateSymbol(type, shortName.c_str(), fullName.c_str(), rawName.c_str(), addr); + m_object = BNCreateSymbol(type, shortName.c_str(), fullName.c_str(), rawName.c_str(), addr); } Symbol::Symbol(BNSymbolType type, const std::string& name, uint64_t addr) { - m_sym = BNCreateSymbol(type, name.c_str(), name.c_str(), name.c_str(), addr); + m_object = BNCreateSymbol(type, name.c_str(), name.c_str(), name.c_str(), addr); } Symbol::Symbol(BNSymbol* sym) { - m_sym = sym; -} - - -Symbol::~Symbol() -{ - BNFreeSymbol(m_sym); + m_object = sym; } BNSymbolType Symbol::GetType() const { - return BNGetSymbolType(m_sym); + return BNGetSymbolType(m_object); } string Symbol::GetShortName() const { - char* name = BNGetSymbolShortName(m_sym); + char* name = BNGetSymbolShortName(m_object); string result = name; BNFreeString(name); return result; @@ -126,7 +120,7 @@ string Symbol::GetShortName() const string Symbol::GetFullName() const { - char* name = BNGetSymbolFullName(m_sym); + char* name = BNGetSymbolFullName(m_object); string result = name; BNFreeString(name); return result; @@ -135,7 +129,7 @@ string Symbol::GetFullName() const string Symbol::GetRawName() const { - char* name = BNGetSymbolRawName(m_sym); + char* name = BNGetSymbolRawName(m_object); string result = name; BNFreeString(name); return result; @@ -144,25 +138,25 @@ string Symbol::GetRawName() const uint64_t Symbol::GetAddress() const { - return BNGetSymbolAddress(m_sym); + return BNGetSymbolAddress(m_object); } bool Symbol::IsAutoDefined() const { - return BNIsSymbolAutoDefined(m_sym); + return BNIsSymbolAutoDefined(m_object); } void Symbol::SetAutoDefined(bool val) { - BNSetSymbolAutoDefined(m_sym, val); + BNSetSymbolAutoDefined(m_object, val); } Ref<Symbol> Symbol::ImportedFunctionFromImportAddressSymbol(Symbol* sym, uint64_t addr) { - return new Symbol(BNImportedFunctionFromImportAddressSymbol(sym->GetSymbolObject(), addr)); + return new Symbol(BNImportedFunctionFromImportAddressSymbol(sym->GetObject(), addr)); } @@ -171,6 +165,7 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) BNCustomBinaryView view; view.context = this; view.init = InitCallback; + view.freeObject = FreeCallback; view.read = ReadCallback; view.write = WriteCallback; view.insert = InsertCallback; @@ -190,27 +185,29 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file) view.save = SaveCallback; m_file = file; - m_view = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetFileObject(), &view); + AddRefForRegistration(); + m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), &view); } BinaryView::BinaryView(BNBinaryView* view) { - m_view = view; - m_file = new FileMetadata(BNNewFileReference(BNGetFileForView(m_view))); + m_object = view; + m_file = new FileMetadata(BNNewFileReference(BNGetFileForView(m_object))); } -BinaryView::~BinaryView() +bool BinaryView::InitCallback(void* ctxt) { - BNFreeBinaryView(m_view); + BinaryView* view = (BinaryView*)ctxt; + return view->Init(); } -bool BinaryView::InitCallback(void* ctxt) +void BinaryView::FreeCallback(void* ctxt) { BinaryView* view = (BinaryView*)ctxt; - return view->Init(); + view->ReleaseForRegistration(); } @@ -389,25 +386,25 @@ size_t BinaryView::PerformGetAddressSize() const void BinaryView::NotifyDataWritten(uint64_t offset, size_t len) { - BNNotifyDataWritten(m_view, offset, len); + BNNotifyDataWritten(m_object, offset, len); } void BinaryView::NotifyDataInserted(uint64_t offset, size_t len) { - BNNotifyDataInserted(m_view, offset, len); + BNNotifyDataInserted(m_object, offset, len); } void BinaryView::NotifyDataRemoved(uint64_t offset, uint64_t len) { - BNNotifyDataRemoved(m_view, offset, len); + BNNotifyDataRemoved(m_object, offset, len); } string BinaryView::GetTypeName() const { - char* str = BNGetViewType(m_view); + char* str = BNGetViewType(m_object); string result = str; BNFreeString(str); return result; @@ -416,7 +413,7 @@ string BinaryView::GetTypeName() const bool BinaryView::IsModified() const { - return BNIsViewModified(m_view); + return BNIsViewModified(m_object); } @@ -452,7 +449,7 @@ void BinaryView::BeginUndoActions() void BinaryView::AddUndoAction(UndoAction* action) { - action->Add(m_view); + action->Add(m_object); } @@ -494,27 +491,27 @@ bool BinaryView::Navigate(const string& view, uint64_t offset) DataBuffer BinaryView::ReadBuffer(uint64_t offset, size_t len) { - BNDataBuffer* result = BNReadViewBuffer(m_view, offset, len); + BNDataBuffer* result = BNReadViewBuffer(m_object, offset, len); return DataBuffer(result); } size_t BinaryView::WriteBuffer(uint64_t offset, const DataBuffer& data) { - return BNWriteViewBuffer(m_view, offset, data.GetBufferObject()); + return BNWriteViewBuffer(m_object, offset, data.GetBufferObject()); } size_t BinaryView::InsertBuffer(uint64_t offset, const DataBuffer& data) { - return BNInsertViewBuffer(m_view, offset, data.GetBufferObject()); + return BNInsertViewBuffer(m_object, offset, data.GetBufferObject()); } vector<BNModificationStatus> BinaryView::GetModification(uint64_t offset, size_t len) { BNModificationStatus* mod = new BNModificationStatus[len]; - len = BNGetModificationArray(m_view, offset, mod, len); + len = BNGetModificationArray(m_object, offset, mod, len); vector<BNModificationStatus> result; for (size_t i = 0; i < len; i++) @@ -527,109 +524,109 @@ vector<BNModificationStatus> BinaryView::GetModification(uint64_t offset, size_t uint64_t BinaryView::GetEnd() const { - return BNGetEndOffset(m_view); + return BNGetEndOffset(m_object); } bool BinaryView::Save(const string& path) { - return BNSaveToFilename(m_view, path.c_str()); + return BNSaveToFilename(m_object, path.c_str()); } void BinaryView::RegisterNotification(BinaryDataNotification* notify) { - BNRegisterDataNotification(m_view, notify->GetCallbacks()); + BNRegisterDataNotification(m_object, notify->GetCallbacks()); } void BinaryView::UnregisterNotification(BinaryDataNotification* notify) { - BNUnregisterDataNotification(m_view, notify->GetCallbacks()); + BNUnregisterDataNotification(m_object, notify->GetCallbacks()); } size_t BinaryView::Read(void* dest, uint64_t offset, size_t len) { - return BNReadViewData(m_view, dest, offset, len); + return BNReadViewData(m_object, dest, offset, len); } size_t BinaryView::Write(uint64_t offset, const void* data, size_t len) { - return BNWriteViewData(m_view, offset, data, len); + return BNWriteViewData(m_object, offset, data, len); } size_t BinaryView::Insert(uint64_t offset, const void* data, size_t len) { - return BNInsertViewData(m_view, offset, data, len); + return BNInsertViewData(m_object, offset, data, len); } size_t BinaryView::Remove(uint64_t offset, uint64_t len) { - return BNRemoveViewData(m_view, offset, len); + return BNRemoveViewData(m_object, offset, len); } BNModificationStatus BinaryView::GetModification(uint64_t offset) { - return BNGetModification(m_view, offset); + return BNGetModification(m_object, offset); } bool BinaryView::IsValidOffset(uint64_t offset) const { - return BNIsValidOffset(m_view, offset); + return BNIsValidOffset(m_object, offset); } bool BinaryView::IsOffsetReadable(uint64_t offset) const { - return BNIsOffsetReadable(m_view, offset); + return BNIsOffsetReadable(m_object, offset); } bool BinaryView::IsOffsetWritable(uint64_t offset) const { - return BNIsOffsetWritable(m_view, offset); + return BNIsOffsetWritable(m_object, offset); } bool BinaryView::IsOffsetExecutable(uint64_t offset) const { - return BNIsOffsetExecutable(m_view, offset); + return BNIsOffsetExecutable(m_object, offset); } uint64_t BinaryView::GetNextValidOffset(uint64_t offset) const { - return BNGetNextValidOffset(m_view, offset); + return BNGetNextValidOffset(m_object, offset); } uint64_t BinaryView::GetStart() const { - return BNGetStartOffset(m_view); + return BNGetStartOffset(m_object); } uint64_t BinaryView::GetLength() const { - return BNGetViewLength(m_view); + return BNGetViewLength(m_object); } uint64_t BinaryView::GetEntryPoint() const { - return BNGetEntryPoint(m_view); + return BNGetEntryPoint(m_object); } Ref<Architecture> BinaryView::GetDefaultArchitecture() const { - BNArchitecture* arch = BNGetDefaultArchitecture(m_view); + BNArchitecture* arch = BNGetDefaultArchitecture(m_object); if (!arch) return nullptr; return new CoreArchitecture(arch); @@ -639,15 +636,15 @@ Ref<Architecture> BinaryView::GetDefaultArchitecture() const void BinaryView::SetDefaultArchitecture(Architecture* arch) { if (arch) - BNSetDefaultArchitecture(m_view, arch->GetArchitectureObject()); + BNSetDefaultArchitecture(m_object, arch->GetObject()); else - BNSetDefaultArchitecture(m_view, nullptr); + BNSetDefaultArchitecture(m_object, nullptr); } Ref<Platform> BinaryView::GetDefaultPlatform() const { - BNPlatform* platform = BNGetDefaultPlatform(m_view); + BNPlatform* platform = BNGetDefaultPlatform(m_object); if (!platform) return nullptr; return new Platform(platform); @@ -657,76 +654,76 @@ Ref<Platform> BinaryView::GetDefaultPlatform() const void BinaryView::SetDefaultPlatform(Platform* platform) { if (platform) - BNSetDefaultPlatform(m_view, platform->GetPlatformObject()); + BNSetDefaultPlatform(m_object, platform->GetObject()); else - BNSetDefaultPlatform(m_view, nullptr); + BNSetDefaultPlatform(m_object, nullptr); } BNEndianness BinaryView::GetDefaultEndianness() const { - return BNGetDefaultEndianness(m_view); + return BNGetDefaultEndianness(m_object); } size_t BinaryView::GetAddressSize() const { - return BNGetViewAddressSize(m_view); + return BNGetViewAddressSize(m_object); } bool BinaryView::IsExecutable() const { - return BNIsExecutableView(m_view); + return BNIsExecutableView(m_object); } bool BinaryView::Save(FileAccessor* file) { - return BNSaveToFile(m_view, file->GetCallbacks()); + return BNSaveToFile(m_object, file->GetCallbacks()); } void BinaryView::AddFunctionForAnalysis(Platform* platform, uint64_t addr) { - BNAddFunctionForAnalysis(m_view, platform->GetPlatformObject(), addr); + BNAddFunctionForAnalysis(m_object, platform->GetObject(), addr); } void BinaryView::AddEntryPointForAnalysis(Platform* platform, uint64_t addr) { - BNAddEntryPointForAnalysis(m_view, platform->GetPlatformObject(), addr); + BNAddEntryPointForAnalysis(m_object, platform->GetObject(), addr); } void BinaryView::RemoveAnalysisFunction(Function* func) { - BNRemoveAnalysisFunction(m_view, func->GetFunctionObject()); + BNRemoveAnalysisFunction(m_object, func->GetObject()); } void BinaryView::CreateUserFunction(Platform* platform, uint64_t start) { - BNCreateUserFunction(m_view, platform->GetPlatformObject(), start); + BNCreateUserFunction(m_object, platform->GetObject(), start); } void BinaryView::UpdateAnalysis() { - BNUpdateAnalysis(m_view); + BNUpdateAnalysis(m_object); } void BinaryView::AbortAnalysis() { - BNAbortAnalysis(m_view); + BNAbortAnalysis(m_object); } vector<Ref<Function>> BinaryView::GetAnalysisFunctionList() { size_t count; - BNFunction** list = BNGetAnalysisFunctionList(m_view, &count); + BNFunction** list = BNGetAnalysisFunctionList(m_object, &count); vector<Ref<Function>> result; for (size_t i = 0; i < count; i++) @@ -739,13 +736,13 @@ vector<Ref<Function>> BinaryView::GetAnalysisFunctionList() bool BinaryView::HasFunctions() const { - return BNHasFunctions(m_view); + return BNHasFunctions(m_object); } Ref<Function> BinaryView::GetAnalysisFunction(Platform* platform, uint64_t addr) { - BNFunction* func = BNGetAnalysisFunction(m_view, platform->GetPlatformObject(), addr); + BNFunction* func = BNGetAnalysisFunction(m_object, platform->GetObject(), addr); if (!func) return nullptr; return new Function(func); @@ -754,7 +751,7 @@ Ref<Function> BinaryView::GetAnalysisFunction(Platform* platform, uint64_t addr) Ref<Function> BinaryView::GetRecentAnalysisFunctionForAddress(uint64_t addr) { - BNFunction* func = BNGetRecentAnalysisFunctionForAddress(m_view, addr); + BNFunction* func = BNGetRecentAnalysisFunctionForAddress(m_object, addr); if (!func) return nullptr; return new Function(func); @@ -764,7 +761,7 @@ Ref<Function> BinaryView::GetRecentAnalysisFunctionForAddress(uint64_t addr) vector<Ref<Function>> BinaryView::GetAnalysisFunctionsForAddress(uint64_t addr) { size_t count; - BNFunction** list = BNGetAnalysisFunctionsForAddress(m_view, addr, &count); + BNFunction** list = BNGetAnalysisFunctionsForAddress(m_object, addr, &count); vector<Ref<Function>> result; for (size_t i = 0; i < count; i++) @@ -777,7 +774,7 @@ vector<Ref<Function>> BinaryView::GetAnalysisFunctionsForAddress(uint64_t addr) Ref<Function> BinaryView::GetAnalysisEntryPoint() { - BNFunction* func = BNGetAnalysisEntryPoint(m_view); + BNFunction* func = BNGetAnalysisEntryPoint(m_object); if (!func) return nullptr; return new Function(func); @@ -786,7 +783,7 @@ Ref<Function> BinaryView::GetAnalysisEntryPoint() Ref<BasicBlock> BinaryView::GetRecentBasicBlockForAddress(uint64_t addr) { - BNBasicBlock* block = BNGetRecentBasicBlockForAddress(m_view, addr); + BNBasicBlock* block = BNGetRecentBasicBlockForAddress(m_object, addr); if (!block) return nullptr; return new BasicBlock(block); @@ -796,7 +793,7 @@ Ref<BasicBlock> BinaryView::GetRecentBasicBlockForAddress(uint64_t addr) vector<Ref<BasicBlock>> BinaryView::GetBasicBlocksForAddress(uint64_t addr) { size_t count; - BNBasicBlock** blocks = BNGetBasicBlocksForAddress(m_view, addr, &count); + BNBasicBlock** blocks = BNGetBasicBlocksForAddress(m_object, addr, &count); vector<Ref<BasicBlock>> result; for (size_t i = 0; i < count; i++) @@ -810,7 +807,7 @@ vector<Ref<BasicBlock>> BinaryView::GetBasicBlocksForAddress(uint64_t addr) vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr) { size_t count; - BNReferenceSource* refs = BNGetCodeReferences(m_view, addr, &count); + BNReferenceSource* refs = BNGetCodeReferences(m_object, addr, &count); vector<ReferenceSource> result; for (size_t i = 0; i < count; i++) @@ -830,7 +827,7 @@ vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr) vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr, uint64_t len) { size_t count; - BNReferenceSource* refs = BNGetCodeReferencesInRange(m_view, addr, len, &count); + BNReferenceSource* refs = BNGetCodeReferencesInRange(m_object, addr, len, &count); vector<ReferenceSource> result; for (size_t i = 0; i < count; i++) @@ -849,7 +846,7 @@ vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr, uint64_t le Ref<Symbol> BinaryView::GetSymbolByAddress(uint64_t addr) { - BNSymbol* sym = BNGetSymbolByAddress(m_view, addr); + BNSymbol* sym = BNGetSymbolByAddress(m_object, addr); if (!sym) return nullptr; return new Symbol(sym); @@ -858,7 +855,7 @@ Ref<Symbol> BinaryView::GetSymbolByAddress(uint64_t addr) Ref<Symbol> BinaryView::GetSymbolByRawName(const string& name) { - BNSymbol* sym = BNGetSymbolByRawName(m_view, name.c_str()); + BNSymbol* sym = BNGetSymbolByRawName(m_object, name.c_str()); if (!sym) return nullptr; return new Symbol(sym); @@ -868,7 +865,7 @@ Ref<Symbol> BinaryView::GetSymbolByRawName(const string& name) vector<Ref<Symbol>> BinaryView::GetSymbolsByName(const string& name) { size_t count; - BNSymbol** syms = BNGetSymbolsByName(m_view, name.c_str(), &count); + BNSymbol** syms = BNGetSymbolsByName(m_object, name.c_str(), &count); vector<Ref<Symbol>> result; for (size_t i = 0; i < count; i++) @@ -882,7 +879,7 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsByName(const string& name) vector<Ref<Symbol>> BinaryView::GetSymbols() { size_t count; - BNSymbol** syms = BNGetSymbols(m_view, &count); + BNSymbol** syms = BNGetSymbols(m_object, &count); vector<Ref<Symbol>> result; for (size_t i = 0; i < count; i++) @@ -896,7 +893,7 @@ vector<Ref<Symbol>> BinaryView::GetSymbols() vector<Ref<Symbol>> BinaryView::GetSymbols(uint64_t start, uint64_t len) { size_t count; - BNSymbol** syms = BNGetSymbolsInRange(m_view, start, len, &count); + BNSymbol** syms = BNGetSymbolsInRange(m_object, start, len, &count); vector<Ref<Symbol>> result; for (size_t i = 0; i < count; i++) @@ -910,7 +907,7 @@ vector<Ref<Symbol>> BinaryView::GetSymbols(uint64_t start, uint64_t len) vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type) { size_t count; - BNSymbol** syms = BNGetSymbolsOfType(m_view, type, &count); + BNSymbol** syms = BNGetSymbolsOfType(m_object, type, &count); vector<Ref<Symbol>> result; for (size_t i = 0; i < count; i++) @@ -924,7 +921,7 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type) vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len) { size_t count; - BNSymbol** syms = BNGetSymbolsOfTypeInRange(m_view, type, start, len, &count); + BNSymbol** syms = BNGetSymbolsOfTypeInRange(m_object, type, start, len, &count); vector<Ref<Symbol>> result; for (size_t i = 0; i < count; i++) @@ -937,92 +934,92 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t sta void BinaryView::DefineAutoSymbol(Symbol* sym) { - BNDefineAutoSymbol(m_view, sym->GetSymbolObject()); + BNDefineAutoSymbol(m_object, sym->GetObject()); } void BinaryView::UndefineAutoSymbol(Symbol* sym) { - BNUndefineAutoSymbol(m_view, sym->GetSymbolObject()); + BNUndefineAutoSymbol(m_object, sym->GetObject()); } void BinaryView::DefineUserSymbol(Symbol* sym) { - BNDefineUserSymbol(m_view, sym->GetSymbolObject()); + BNDefineUserSymbol(m_object, sym->GetObject()); } void BinaryView::UndefineUserSymbol(Symbol* sym) { - BNUndefineUserSymbol(m_view, sym->GetSymbolObject()); + BNUndefineUserSymbol(m_object, sym->GetObject()); } bool BinaryView::IsNeverBranchPatchAvailable(Architecture* arch, uint64_t addr) { - return BNIsNeverBranchPatchAvailable(m_view, arch->GetArchitectureObject(), addr); + return BNIsNeverBranchPatchAvailable(m_object, arch->GetObject(), addr); } bool BinaryView::IsAlwaysBranchPatchAvailable(Architecture* arch, uint64_t addr) { - return BNIsAlwaysBranchPatchAvailable(m_view, arch->GetArchitectureObject(), addr); + return BNIsAlwaysBranchPatchAvailable(m_object, arch->GetObject(), addr); } bool BinaryView::IsInvertBranchPatchAvailable(Architecture* arch, uint64_t addr) { - return BNIsInvertBranchPatchAvailable(m_view, arch->GetArchitectureObject(), addr); + return BNIsInvertBranchPatchAvailable(m_object, arch->GetObject(), addr); } bool BinaryView::IsSkipAndReturnZeroPatchAvailable(Architecture* arch, uint64_t addr) { - return BNIsSkipAndReturnZeroPatchAvailable(m_view, arch->GetArchitectureObject(), addr); + return BNIsSkipAndReturnZeroPatchAvailable(m_object, arch->GetObject(), addr); } bool BinaryView::IsSkipAndReturnValuePatchAvailable(Architecture* arch, uint64_t addr) { - return BNIsSkipAndReturnValuePatchAvailable(m_view, arch->GetArchitectureObject(), addr); + return BNIsSkipAndReturnValuePatchAvailable(m_object, arch->GetObject(), addr); } bool BinaryView::ConvertToNop(Architecture* arch, uint64_t addr) { - return BNConvertToNop(m_view, arch->GetArchitectureObject(), addr); + return BNConvertToNop(m_object, arch->GetObject(), addr); } bool BinaryView::AlwaysBranch(Architecture* arch, uint64_t addr) { - return BNAlwaysBranch(m_view, arch->GetArchitectureObject(), addr); + return BNAlwaysBranch(m_object, arch->GetObject(), addr); } bool BinaryView::InvertBranch(Architecture* arch, uint64_t addr) { - return BNInvertBranch(m_view, arch->GetArchitectureObject(), addr); + return BNInvertBranch(m_object, arch->GetObject(), addr); } bool BinaryView::SkipAndReturnValue(Architecture* arch, uint64_t addr, uint64_t value) { - return BNSkipAndReturnValue(m_view, arch->GetArchitectureObject(), addr, value); + return BNSkipAndReturnValue(m_object, arch->GetObject(), addr, value); } size_t BinaryView::GetInstructionLength(Architecture* arch, uint64_t addr) { - return BNGetInstructionLength(m_view, arch->GetArchitectureObject(), addr); + return BNGetInstructionLength(m_object, arch->GetObject(), addr); } vector<BNStringReference> BinaryView::GetStrings() { size_t count; - BNStringReference* strings = BNGetStrings(m_view, &count); + BNStringReference* strings = BNGetStrings(m_object, &count); vector<BNStringReference> result; result.insert(result.end(), strings, strings + count); BNFreeStringList(strings); @@ -1033,7 +1030,7 @@ vector<BNStringReference> BinaryView::GetStrings() vector<BNStringReference> BinaryView::GetStrings(uint64_t start, uint64_t len) { size_t count; - BNStringReference* strings = BNGetStringsInRange(m_view, start, len, &count); + BNStringReference* strings = BNGetStringsInRange(m_object, start, len, &count); vector<BNStringReference> result; result.insert(result.end(), strings, strings + count); BNFreeStringList(strings); @@ -1041,30 +1038,30 @@ vector<BNStringReference> BinaryView::GetStrings(uint64_t start, uint64_t len) } -BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetFileObject())) +BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetObject())) { } BinaryData::BinaryData(FileMetadata* file, const DataBuffer& data): - BinaryView(BNCreateBinaryDataViewFromBuffer(file->GetFileObject(), data.GetBufferObject())) + BinaryView(BNCreateBinaryDataViewFromBuffer(file->GetObject(), data.GetBufferObject())) { } BinaryData::BinaryData(FileMetadata* file, const void* data, size_t len): - BinaryView(BNCreateBinaryDataViewFromData(file->GetFileObject(), data, len)) + BinaryView(BNCreateBinaryDataViewFromData(file->GetObject(), data, len)) { } BinaryData::BinaryData(FileMetadata* file, const string& path): - BinaryView(BNCreateBinaryDataViewFromFilename(file->GetFileObject(), path.c_str())) + BinaryView(BNCreateBinaryDataViewFromFilename(file->GetObject(), path.c_str())) { } BinaryData::BinaryData(FileMetadata* file, FileAccessor* accessor): - BinaryView(BNCreateBinaryDataViewFromFile(file->GetFileObject(), accessor->GetCallbacks())) + BinaryView(BNCreateBinaryDataViewFromFile(file->GetObject(), accessor->GetCallbacks())) { } diff --git a/binaryviewtype.cpp b/binaryviewtype.cpp index e231586b..f7e1fbc0 100644 --- a/binaryviewtype.cpp +++ b/binaryviewtype.cpp @@ -9,7 +9,7 @@ BNBinaryView* BinaryViewType::CreateCallback(void* ctxt, BNBinaryView* data) BinaryViewType* type = (BinaryViewType*)ctxt; Ref<BinaryView> view = new BinaryView(BNNewViewReference(data)); Ref<BinaryView> result = type->Create(view); - return BNNewViewReference(result->GetViewObject()); + return BNNewViewReference(result->GetObject()); } @@ -21,14 +21,16 @@ bool BinaryViewType::IsValidCallback(void* ctxt, BNBinaryView* data) } -BinaryViewType::BinaryViewType(BNBinaryViewType* type): m_type(type) +BinaryViewType::BinaryViewType(BNBinaryViewType* type) { + m_object = type; } BinaryViewType::BinaryViewType(const string& name, const string& longName): - m_type(nullptr), m_nameForRegister(name), m_longNameForRegister(longName) + m_nameForRegister(name), m_longNameForRegister(longName) { + m_object = nullptr; } @@ -39,8 +41,9 @@ void BinaryViewType::Register(BinaryViewType* type) callbacks.create = CreateCallback; callbacks.isValidForData = IsValidCallback; - type->m_type = BNRegisterBinaryViewType(type->m_nameForRegister.c_str(), - type->m_longNameForRegister.c_str(), &callbacks); + type->AddRefForRegistration(); + type->m_object = BNRegisterBinaryViewType(type->m_nameForRegister.c_str(), + type->m_longNameForRegister.c_str(), &callbacks); } @@ -72,7 +75,7 @@ vector<Ref<BinaryViewType>> BinaryViewType::GetViewTypesForData(BinaryView* data { BNBinaryViewType** types; size_t count; - types = BNGetBinaryViewTypesForData(data->GetViewObject(), &count); + types = BNGetBinaryViewTypesForData(data->GetObject(), &count); vector<Ref<BinaryViewType>> result; for (size_t i = 0; i < count; i++) @@ -94,13 +97,13 @@ void BinaryViewType::RegisterArchitecture(const string& name, uint32_t id, Archi void BinaryViewType::RegisterArchitecture(uint32_t id, Architecture* arch) { - BNRegisterArchitectureForViewType(m_type, id, arch->GetArchitectureObject()); + BNRegisterArchitectureForViewType(m_object, id, arch->GetObject()); } Ref<Architecture> BinaryViewType::GetArchitecture(uint32_t id) { - BNArchitecture* arch = BNGetArchitectureForViewType(m_type, id); + BNArchitecture* arch = BNGetArchitectureForViewType(m_object, id); if (!arch) return nullptr; return new CoreArchitecture(arch); @@ -127,19 +130,19 @@ void BinaryViewType::RegisterDefaultPlatform(const string& name, Architecture* a void BinaryViewType::RegisterPlatform(uint32_t id, Architecture* arch, Platform* platform) { - BNRegisterPlatformForViewType(m_type, id, arch->GetArchitectureObject(), platform->GetPlatformObject()); + BNRegisterPlatformForViewType(m_object, id, arch->GetObject(), platform->GetObject()); } void BinaryViewType::RegisterDefaultPlatform(Architecture* arch, Platform* platform) { - BNRegisterDefaultPlatformForViewType(m_type, arch->GetArchitectureObject(), platform->GetPlatformObject()); + BNRegisterDefaultPlatformForViewType(m_object, arch->GetObject(), platform->GetObject()); } Ref<Platform> BinaryViewType::GetPlatform(uint32_t id, Architecture* arch) { - BNPlatform* platform = BNGetPlatformForViewType(m_type, id, arch->GetArchitectureObject()); + BNPlatform* platform = BNGetPlatformForViewType(m_object, id, arch->GetObject()); if (!platform) return nullptr; return new Platform(platform); @@ -148,7 +151,7 @@ Ref<Platform> BinaryViewType::GetPlatform(uint32_t id, Architecture* arch) string BinaryViewType::GetName() { - char* contents = BNGetBinaryViewTypeName(m_type); + char* contents = BNGetBinaryViewTypeName(m_object); string result = contents; BNFreeString(contents); return result; @@ -157,7 +160,7 @@ string BinaryViewType::GetName() string BinaryViewType::GetLongName() { - char* contents = BNGetBinaryViewTypeLongName(m_type); + char* contents = BNGetBinaryViewTypeLongName(m_object); string result = contents; BNFreeString(contents); return result; @@ -171,7 +174,7 @@ CoreBinaryViewType::CoreBinaryViewType(BNBinaryViewType* type): BinaryViewType(t BinaryView* CoreBinaryViewType::Create(BinaryView* data) { - BNBinaryView* view = BNCreateBinaryViewOfType(m_type, data->GetViewObject()); + BNBinaryView* view = BNCreateBinaryViewOfType(m_object, data->GetObject()); if (!view) return nullptr; return new BinaryView(view); @@ -180,5 +183,5 @@ BinaryView* CoreBinaryViewType::Create(BinaryView* data) bool CoreBinaryViewType::IsTypeValidForData(BinaryView* data) { - return BNIsBinaryViewTypeValidForData(m_type, data->GetViewObject()); + return BNIsBinaryViewTypeValidForData(m_object, data->GetObject()); } diff --git a/binarywriter.cpp b/binarywriter.cpp index 808d0d5f..e13d0035 100644 --- a/binarywriter.cpp +++ b/binarywriter.cpp @@ -6,7 +6,7 @@ using namespace std; BinaryWriter::BinaryWriter(BinaryView* data, BNEndianness endian): m_view(data) { - m_stream = BNCreateBinaryWriter(data->GetViewObject()); + m_stream = BNCreateBinaryWriter(data->GetObject()); BNSetBinaryWriterEndianness(m_stream, endian); } diff --git a/callingconvention.cpp b/callingconvention.cpp index 076536c9..c87b3aeb 100644 --- a/callingconvention.cpp +++ b/callingconvention.cpp @@ -4,8 +4,9 @@ using namespace std; using namespace BinaryNinja; -CallingConvention::CallingConvention(BNCallingConvention* cc): m_callingConvention(cc) +CallingConvention::CallingConvention(BNCallingConvention* cc) { + m_object = cc; } @@ -13,6 +14,7 @@ CallingConvention::CallingConvention(Architecture* arch, const string& name) { BNCustomCallingConvention cc; cc.context = this; + cc.freeObject = FreeCallback; cc.getCallerSavedRegisters = GetCallerSavedRegistersCallback; cc.getIntegerArgumentRegisters = GetIntegerArgumentRegistersCallback; cc.getFloatArgumentRegisters = GetFloatArgumentRegistersCallback; @@ -23,13 +25,15 @@ CallingConvention::CallingConvention(Architecture* arch, const string& name) cc.getHighIntegerReturnValueRegister = GetHighIntegerReturnValueRegisterCallback; cc.getFloatReturnValueRegister = GetFloatReturnValueRegisterCallback; - m_callingConvention = BNCreateCallingConvention(arch->GetArchitectureObject(), name.c_str(), &cc); + AddRefForRegistration(); + m_object = BNCreateCallingConvention(arch->GetObject(), name.c_str(), &cc); } -CallingConvention::~CallingConvention() +void CallingConvention::FreeCallback(void* ctxt) { - BNFreeCallingConvention(m_callingConvention); + CallingConvention* cc = (CallingConvention*)ctxt; + cc->ReleaseForRegistration(); } @@ -115,13 +119,13 @@ uint32_t CallingConvention::GetFloatReturnValueRegisterCallback(void* ctxt) Ref<Architecture> CallingConvention::GetArchitecture() const { - return new CoreArchitecture(BNGetCallingConventionArchitecture(m_callingConvention)); + return new CoreArchitecture(BNGetCallingConventionArchitecture(m_object)); } string CallingConvention::GetName() const { - char* str = BNGetCallingConventionName(m_callingConvention); + char* str = BNGetCallingConventionName(m_object); string result = str; BNFreeString(str); return result; @@ -178,7 +182,7 @@ CoreCallingConvention::CoreCallingConvention(BNCallingConvention* cc): CallingCo vector<uint32_t> CoreCallingConvention::GetCallerSavedRegisters() { size_t count; - uint32_t* regs = BNGetCallerSavedRegisters(m_callingConvention, &count); + uint32_t* regs = BNGetCallerSavedRegisters(m_object, &count); vector<uint32_t> result; result.insert(result.end(), regs, ®s[count]); BNFreeRegisterList(regs); @@ -189,7 +193,7 @@ vector<uint32_t> CoreCallingConvention::GetCallerSavedRegisters() vector<uint32_t> CoreCallingConvention::GetIntegerArgumentRegisters() { size_t count; - uint32_t* regs = BNGetIntegerArgumentRegisters(m_callingConvention, &count); + uint32_t* regs = BNGetIntegerArgumentRegisters(m_object, &count); vector<uint32_t> result; result.insert(result.end(), regs, ®s[count]); BNFreeRegisterList(regs); @@ -200,7 +204,7 @@ vector<uint32_t> CoreCallingConvention::GetIntegerArgumentRegisters() vector<uint32_t> CoreCallingConvention::GetFloatArgumentRegisters() { size_t count; - uint32_t* regs = BNGetFloatArgumentRegisters(m_callingConvention, &count); + uint32_t* regs = BNGetFloatArgumentRegisters(m_object, &count); vector<uint32_t> result; result.insert(result.end(), regs, ®s[count]); BNFreeRegisterList(regs); @@ -210,29 +214,29 @@ vector<uint32_t> CoreCallingConvention::GetFloatArgumentRegisters() bool CoreCallingConvention::AreArgumentRegistersSharedIndex() { - return BNAreArgumentRegistersSharedIndex(m_callingConvention); + return BNAreArgumentRegistersSharedIndex(m_object); } bool CoreCallingConvention::IsStackReservedForArgumentRegisters() { - return BNIsStackReservedForArgumentRegisters(m_callingConvention); + return BNIsStackReservedForArgumentRegisters(m_object); } uint32_t CoreCallingConvention::GetIntegerReturnValueRegister() { - return BNGetIntegerReturnValueRegister(m_callingConvention); + return BNGetIntegerReturnValueRegister(m_object); } uint32_t CoreCallingConvention::GetHighIntegerReturnValueRegister() { - return BNGetHighIntegerReturnValueRegister(m_callingConvention); + return BNGetHighIntegerReturnValueRegister(m_object); } uint32_t CoreCallingConvention::GetFloatReturnValueRegister() { - return BNGetFloatReturnValueRegister(m_callingConvention); + return BNGetFloatReturnValueRegister(m_object); } diff --git a/filemetadata.cpp b/filemetadata.cpp index fa420301..4a2c656a 100644 --- a/filemetadata.cpp +++ b/filemetadata.cpp @@ -36,6 +36,13 @@ NavigationHandler::NavigationHandler() } +void UndoAction::FreeCallback(void* ctxt) +{ + UndoAction* action = (UndoAction*)ctxt; + delete action; +} + + void UndoAction::UndoCallback(void* ctxt, BNBinaryView* data) { UndoAction* action = (UndoAction*)ctxt; @@ -80,6 +87,7 @@ BNUndoAction UndoAction::GetCallbacks() BNUndoAction action; action.type = m_actionType; action.context = this; + action.freeObject = FreeCallback; action.undo = UndoCallback; action.redo = RedoCallback; action.serialize = SerializeCallback; @@ -135,47 +143,41 @@ void UndoActionType::Register(UndoActionType* type) FileMetadata::FileMetadata() { - m_file = BNCreateFileMetadata(); + m_object = BNCreateFileMetadata(); } FileMetadata::FileMetadata(const string& filename) { - m_file = BNCreateFileMetadata(); - BNSetFilename(m_file, filename.c_str()); + m_object = BNCreateFileMetadata(); + BNSetFilename(m_object, filename.c_str()); } FileMetadata::FileMetadata(BNFileMetadata* file) { - m_file = file; -} - - -FileMetadata::~FileMetadata() -{ - BNFreeFileMetadata(m_file); + m_object = file; } void FileMetadata::Close() { - BNCloseFile(m_file); + BNCloseFile(m_object); } void FileMetadata::SetNavigationHandler(NavigationHandler* handler) { if (handler) - BNSetFileMetadataNavigationHandler(m_file, handler->GetCallbacks()); + BNSetFileMetadataNavigationHandler(m_object, handler->GetCallbacks()); else - BNSetFileMetadataNavigationHandler(m_file, nullptr); + BNSetFileMetadataNavigationHandler(m_object, nullptr); } string FileMetadata::GetFilename() const { - char* str = BNGetFilename(m_file); + char* str = BNGetFilename(m_object); string result = str; BNFreeString(str); return result; @@ -184,49 +186,49 @@ string FileMetadata::GetFilename() const void FileMetadata::SetFilename(const string& name) { - BNSetFilename(m_file, name.c_str()); + BNSetFilename(m_object, name.c_str()); } bool FileMetadata::IsModified() const { - return BNIsFileModified(m_file); + return BNIsFileModified(m_object); } bool FileMetadata::IsAnalysisChanged() const { - return BNIsAnalysisChanged(m_file); + return BNIsAnalysisChanged(m_object); } void FileMetadata::MarkFileModified() { - BNMarkFileModified(m_file); + BNMarkFileModified(m_object); } void FileMetadata::MarkFileSaved() { - BNMarkFileSaved(m_file); + BNMarkFileSaved(m_object); } bool FileMetadata::IsBackedByDatabase() const { - return BNIsBackedByDatabase(m_file); + return BNIsBackedByDatabase(m_object); } bool FileMetadata::CreateDatabase(const string& name, BinaryView* data) { - return BNCreateDatabase(data->GetViewObject(), name.c_str()); + return BNCreateDatabase(data->GetObject(), name.c_str()); } Ref<BinaryView> FileMetadata::OpenExistingDatabase(const string& path) { - BNBinaryView* data = BNOpenExistingDatabase(m_file, path.c_str()); + BNBinaryView* data = BNOpenExistingDatabase(m_object, path.c_str()); if (!data) return nullptr; return new BinaryView(data); @@ -235,37 +237,37 @@ Ref<BinaryView> FileMetadata::OpenExistingDatabase(const string& path) bool FileMetadata::SaveAutoSnapshot(BinaryView* data) { - return BNSaveAutoSnapshot(data->GetViewObject()); + return BNSaveAutoSnapshot(data->GetObject()); } void FileMetadata::BeginUndoActions() { - BNBeginUndoActions(m_file); + BNBeginUndoActions(m_object); } void FileMetadata::CommitUndoActions() { - BNCommitUndoActions(m_file); + BNCommitUndoActions(m_object); } bool FileMetadata::Undo() { - return BNUndo(m_file); + return BNUndo(m_object); } bool FileMetadata::Redo() { - return BNRedo(m_file); + return BNRedo(m_object); } string FileMetadata::GetCurrentView() { - char* view = BNGetCurrentView(m_file); + char* view = BNGetCurrentView(m_object); string result = view; BNFreeString(view); return result; @@ -274,19 +276,19 @@ string FileMetadata::GetCurrentView() uint64_t FileMetadata::GetCurrentOffset() { - return BNGetCurrentOffset(m_file); + return BNGetCurrentOffset(m_object); } bool FileMetadata::Navigate(const string& view, uint64_t offset) { - return BNNavigate(m_file, view.c_str(), offset); + return BNNavigate(m_object, view.c_str(), offset); } Ref<BinaryView> FileMetadata::GetViewOfType(const string& name) { - BNBinaryView* view = BNGetFileViewOfType(m_file, name.c_str()); + BNBinaryView* view = BNGetFileViewOfType(m_object, name.c_str()); if (!view) return nullptr; return new BinaryView(view); diff --git a/function.cpp b/function.cpp index 27d0795b..20b7ee89 100644 --- a/function.cpp +++ b/function.cpp @@ -4,57 +4,52 @@ using namespace BinaryNinja; using namespace std; -Function::Function(BNFunction* func): m_func(func) +Function::Function(BNFunction* func) { -} - - -Function::~Function() -{ - BNFreeFunction(m_func); + m_object = func; } Ref<Platform> Function::GetPlatform() const { - return new Platform(BNGetFunctionPlatform(m_func)); + return new Platform(BNGetFunctionPlatform(m_object)); } Ref<Architecture> Function::GetArchitecture() const { - return new CoreArchitecture(BNGetFunctionArchitecture(m_func)); + return new CoreArchitecture(BNGetFunctionArchitecture(m_object)); } uint64_t Function::GetStart() const { - return BNGetFunctionStart(m_func); + return BNGetFunctionStart(m_object); } Ref<Symbol> Function::GetSymbol() const { - return new Symbol(BNGetFunctionSymbol(m_func)); + return new Symbol(BNGetFunctionSymbol(m_object)); } bool Function::WasAutomaticallyDiscovered() const { - return BNWasFunctionAutomaticallyDiscovered(m_func); + return BNWasFunctionAutomaticallyDiscovered(m_object); } bool Function::CanReturn() const { - return BNCanFunctionReturn(m_func); + return BNCanFunctionReturn(m_object); } vector<Ref<BasicBlock>> Function::GetBasicBlocks() const { size_t count; - BNBasicBlock** blocks = BNGetFunctionBasicBlockList(m_func, &count); + BNBasicBlock** blocks = BNGetFunctionBasicBlockList(m_object, &count); vector<Ref<BasicBlock>> result; for (size_t i = 0; i < count; i++) @@ -67,13 +62,13 @@ vector<Ref<BasicBlock>> Function::GetBasicBlocks() const void Function::MarkRecentUse() { - BNMarkFunctionAsRecentlyUsed(m_func); + BNMarkFunctionAsRecentlyUsed(m_object); } string Function::GetCommentForAddress(uint64_t addr) const { - char* comment = BNGetCommentForAddress(m_func, addr); + char* comment = BNGetCommentForAddress(m_object, addr); string result = comment; BNFreeString(comment); return result; @@ -83,7 +78,7 @@ string Function::GetCommentForAddress(uint64_t addr) const vector<uint64_t> Function::GetCommentedAddresses() const { size_t count; - uint64_t* addrs = BNGetCommentedAddresses(m_func, &count); + uint64_t* addrs = BNGetCommentedAddresses(m_object, &count); vector<uint64_t> result; result.insert(result.end(), addrs, &addrs[count]); BNFreeAddressList(addrs); @@ -93,20 +88,20 @@ vector<uint64_t> Function::GetCommentedAddresses() const void Function::SetCommentForAddress(uint64_t addr, const string& comment) { - BNSetCommentForAddress(m_func, addr, comment.c_str()); + BNSetCommentForAddress(m_object, addr, comment.c_str()); } Ref<LowLevelILFunction> Function::GetLowLevelIL() const { - return new LowLevelILFunction(BNGetFunctionLowLevelIL(m_func)); + return new LowLevelILFunction(BNGetFunctionLowLevelIL(m_object)); } vector<Ref<BasicBlock>> Function::GetLowLevelILBasicBlocks() const { size_t count; - BNBasicBlock** blocks = BNGetFunctionLowLevelILBasicBlockList(m_func, &count); + BNBasicBlock** blocks = BNGetFunctionLowLevelILBasicBlockList(m_object, &count); vector<Ref<BasicBlock>> result; for (size_t i = 0; i < count; i++) @@ -119,14 +114,14 @@ vector<Ref<BasicBlock>> Function::GetLowLevelILBasicBlocks() const size_t Function::GetLowLevelILForInstruction(Architecture* arch, uint64_t addr) { - return BNGetLowLevelILForInstruction(m_func, arch->GetArchitectureObject(), addr); + return BNGetLowLevelILForInstruction(m_object, arch->GetObject(), addr); } vector<size_t> Function::GetLowLevelILExitsForInstruction(Architecture* arch, uint64_t addr) { size_t count; - size_t* exits = BNGetLowLevelILExitsForInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + size_t* exits = BNGetLowLevelILExitsForInstruction(m_object, arch->GetObject(), addr, &count); vector<size_t> result; result.insert(result.end(), exits, &exits[count]); @@ -138,32 +133,32 @@ vector<size_t> Function::GetLowLevelILExitsForInstruction(Architecture* arch, ui BNRegisterValue Function::GetRegisterValueAtInstruction(Architecture* arch, uint64_t addr, uint32_t reg) { - return BNGetRegisterValueAtInstruction(m_func, arch->GetArchitectureObject(), addr, reg); + return BNGetRegisterValueAtInstruction(m_object, arch->GetObject(), addr, reg); } BNRegisterValue Function::GetRegisterValueAfterInstruction(Architecture* arch, uint64_t addr, uint32_t reg) { - return BNGetRegisterValueAfterInstruction(m_func, arch->GetArchitectureObject(), addr, reg); + return BNGetRegisterValueAfterInstruction(m_object, arch->GetObject(), addr, reg); } BNRegisterValue Function::GetRegisterValueAtLowLevelILInstruction(size_t i, uint32_t reg) { - return BNGetRegisterValueAtLowLevelILInstruction(m_func, i, reg); + return BNGetRegisterValueAtLowLevelILInstruction(m_object, i, reg); } BNRegisterValue Function::GetRegisterValueAfterLowLevelILInstruction(size_t i, uint32_t reg) { - return BNGetRegisterValueAfterLowLevelILInstruction(m_func, i, reg); + return BNGetRegisterValueAfterLowLevelILInstruction(m_object, i, reg); } vector<uint32_t> Function::GetRegistersReadByInstruction(Architecture* arch, uint64_t addr) { size_t count; - uint32_t* regs = BNGetRegistersReadByInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + uint32_t* regs = BNGetRegistersReadByInstruction(m_object, arch->GetObject(), addr, &count); vector<uint32_t> result; result.insert(result.end(), regs, ®s[count]); @@ -176,7 +171,7 @@ vector<uint32_t> Function::GetRegistersReadByInstruction(Architecture* arch, uin vector<uint32_t> Function::GetRegistersWrittenByInstruction(Architecture* arch, uint64_t addr) { size_t count; - uint32_t* regs = BNGetRegistersWrittenByInstruction(m_func, arch->GetArchitectureObject(), addr, &count); + uint32_t* regs = BNGetRegistersWrittenByInstruction(m_object, arch->GetObject(), addr, &count); vector<uint32_t> result; result.insert(result.end(), regs, ®s[count]); @@ -188,18 +183,18 @@ vector<uint32_t> Function::GetRegistersWrittenByInstruction(Architecture* arch, Ref<Type> Function::GetType() const { - return new Type(BNGetFunctionType(m_func)); + return new Type(BNGetFunctionType(m_object)); } void Function::ApplyImportedTypes(Symbol* sym) { - BNApplyImportedTypes(m_func, sym->GetSymbolObject()); + BNApplyImportedTypes(m_object, sym->GetObject()); } Ref<FunctionGraph> Function::CreateFunctionGraph() { - BNFunctionGraph* graph = BNCreateFunctionGraph(m_func); + BNFunctionGraph* graph = BNCreateFunctionGraph(m_object); return new FunctionGraph(graph); } diff --git a/functiongraphblock.cpp b/functiongraphblock.cpp index d598c4a1..58a384ab 100644 --- a/functiongraphblock.cpp +++ b/functiongraphblock.cpp @@ -4,63 +4,58 @@ using namespace BinaryNinja; using namespace std; -FunctionGraphBlock::FunctionGraphBlock(BNFunctionGraphBlock* block): m_block(block) +FunctionGraphBlock::FunctionGraphBlock(BNFunctionGraphBlock* block) { -} - - -FunctionGraphBlock::~FunctionGraphBlock() -{ - BNFreeFunctionGraphBlock(m_block); + m_object = block; } Ref<Architecture> FunctionGraphBlock::GetArchitecture() const { - return new CoreArchitecture(BNGetFunctionGraphBlockArchitecture(m_block)); + return new CoreArchitecture(BNGetFunctionGraphBlockArchitecture(m_object)); } uint64_t FunctionGraphBlock::GetStart() const { - return BNGetFunctionGraphBlockStart(m_block); + return BNGetFunctionGraphBlockStart(m_object); } uint64_t FunctionGraphBlock::GetEnd() const { - return BNGetFunctionGraphBlockEnd(m_block); + return BNGetFunctionGraphBlockEnd(m_object); } int FunctionGraphBlock::GetX() const { - return BNGetFunctionGraphBlockX(m_block); + return BNGetFunctionGraphBlockX(m_object); } int FunctionGraphBlock::GetY() const { - return BNGetFunctionGraphBlockY(m_block); + return BNGetFunctionGraphBlockY(m_object); } int FunctionGraphBlock::GetWidth() const { - return BNGetFunctionGraphBlockWidth(m_block); + return BNGetFunctionGraphBlockWidth(m_object); } int FunctionGraphBlock::GetHeight() const { - return BNGetFunctionGraphBlockHeight(m_block); + return BNGetFunctionGraphBlockHeight(m_object); } vector<FunctionGraphTextLine> FunctionGraphBlock::GetLines() const { size_t count; - BNFunctionGraphTextLine* lines = BNGetFunctionGraphBlockLines(m_block, &count); + BNFunctionGraphTextLine* lines = BNGetFunctionGraphBlockLines(m_object, &count); vector<FunctionGraphTextLine> result; for (size_t i = 0; i < count; i++) @@ -86,7 +81,7 @@ vector<FunctionGraphTextLine> FunctionGraphBlock::GetLines() const vector<FunctionGraphEdge> FunctionGraphBlock::GetOutgoingEdges() const { size_t count; - BNFunctionGraphEdge* edges = BNGetFunctionGraphBlockOutgoingEdges(m_block, &count); + BNFunctionGraphEdge* edges = BNGetFunctionGraphBlockOutgoingEdges(m_object, &count); vector<FunctionGraphEdge> result; for (size_t i = 0; i < count; i++) diff --git a/functionrecognizer.cpp b/functionrecognizer.cpp index 6943bf03..24f70939 100644 --- a/functionrecognizer.cpp +++ b/functionrecognizer.cpp @@ -32,7 +32,7 @@ void FunctionRecognizer::RegisterArchitectureFunctionRecognizer(Architecture* ar BNFunctionRecognizer reg; reg.context = recog; reg.recognizeLowLevelIL = RecognizeLowLevelILCallback; - BNRegisterArchitectureFunctionRecognizer(arch->GetArchitectureObject(), ®); + BNRegisterArchitectureFunctionRecognizer(arch->GetObject(), ®); } diff --git a/lowlevelil.cpp b/lowlevelil.cpp index 9d515836..9cd5b0a1 100644 --- a/lowlevelil.cpp +++ b/lowlevelil.cpp @@ -12,43 +12,38 @@ LowLevelILLabel::LowLevelILLabel() LowLevelILFunction::LowLevelILFunction() { - m_func = BNCreateLowLevelILFunction(); + m_object = BNCreateLowLevelILFunction(); } -LowLevelILFunction::LowLevelILFunction(BNLowLevelILFunction* func): m_func(func) +LowLevelILFunction::LowLevelILFunction(BNLowLevelILFunction* func) { -} - - -LowLevelILFunction::~LowLevelILFunction() -{ - BNFreeLowLevelILFunction(m_func); + m_object = func; } uint64_t LowLevelILFunction::GetCurrentAddress() const { - return BNLowLevelILGetCurrentAddress(m_func); + return BNLowLevelILGetCurrentAddress(m_object); } void LowLevelILFunction::SetCurrentAddress(uint64_t addr) { - BNLowLevelILSetCurrentAddress(m_func, addr); + BNLowLevelILSetCurrentAddress(m_object, addr); } ExprId LowLevelILFunction::AddExpr(BNLowLevelILOperation operation, size_t size, uint32_t flags, ExprId a, ExprId b, ExprId c, ExprId d) { - return BNLowLevelILAddExpr(m_func, operation, size, flags, a, b, c, d); + return BNLowLevelILAddExpr(m_object, operation, size, flags, a, b, c, d); } ExprId LowLevelILFunction::AddInstruction(size_t expr) { - return BNLowLevelILAddInstruction(m_func, expr); + return BNLowLevelILAddInstruction(m_object, expr); } @@ -432,55 +427,55 @@ ExprId LowLevelILFunction::UnimplementedMemoryRef(size_t size, ExprId addr) ExprId LowLevelILFunction::Goto(BNLowLevelILLabel& label) { - return BNLowLevelILGoto(m_func, &label); + return BNLowLevelILGoto(m_object, &label); } ExprId LowLevelILFunction::If(ExprId operand, BNLowLevelILLabel& t, BNLowLevelILLabel& f) { - return BNLowLevelILIf(m_func, operand, &t, &f); + return BNLowLevelILIf(m_object, operand, &t, &f); } void LowLevelILFunction::MarkLabel(BNLowLevelILLabel& label) { - BNLowLevelILMarkLabel(m_func, &label); + BNLowLevelILMarkLabel(m_object, &label); } BNLowLevelILInstruction LowLevelILFunction::operator[](size_t i) const { - return BNGetLowLevelILByIndex(m_func, i); + return BNGetLowLevelILByIndex(m_object, i); } size_t LowLevelILFunction::GetIndexForInstruction(size_t i) const { - return BNGetLowLevelILIndexForInstruction(m_func, i); + return BNGetLowLevelILIndexForInstruction(m_object, i); } size_t LowLevelILFunction::GetInstructionCount() const { - return BNGetLowLevelILInstructionCount(m_func); + return BNGetLowLevelILInstructionCount(m_object); } void LowLevelILFunction::AddLabelForAddress(Architecture* arch, ExprId addr) { - BNAddLowLevelILLabelForAddress(m_func, arch->GetArchitectureObject(), addr); + BNAddLowLevelILLabelForAddress(m_object, arch->GetObject(), addr); } BNLowLevelILLabel* LowLevelILFunction::GetLabelForAddress(Architecture* arch, ExprId addr) { - return BNGetLowLevelILLabelForAddress(m_func, arch->GetArchitectureObject(), addr); + return BNGetLowLevelILLabelForAddress(m_object, arch->GetObject(), addr); } void LowLevelILFunction::Finalize() { - BNFinalizeLowLevelILFunction(m_func); + BNFinalizeLowLevelILFunction(m_object); } @@ -488,7 +483,7 @@ bool LowLevelILFunction::GetExprText(Architecture* arch, ExprId expr, vector<Ins { size_t count; BNInstructionTextToken* list; - if (!BNGetLowLevelILExprText(m_func, arch->GetArchitectureObject(), expr, &list, &count)) + if (!BNGetLowLevelILExprText(m_object, arch->GetObject(), expr, &list, &count)) return false; tokens.clear(); @@ -510,7 +505,7 @@ bool LowLevelILFunction::GetInstructionText(Architecture* arch, size_t instr, ve { size_t count; BNInstructionTextToken* list; - if (!BNGetLowLevelILInstructionText(m_func, arch->GetArchitectureObject(), instr, &list, &count)) + if (!BNGetLowLevelILInstructionText(m_object, arch->GetObject(), instr, &list, &count)) return false; tokens.clear(); diff --git a/platform.cpp b/platform.cpp index 26d3b945..da072dd8 100644 --- a/platform.cpp +++ b/platform.cpp @@ -4,32 +4,27 @@ using namespace std; using namespace BinaryNinja; -Platform::Platform(BNPlatform* platform): m_platform(platform) +Platform::Platform(BNPlatform* platform) { + m_object = platform; } Platform::Platform(Architecture* arch, const string& name) { - m_platform = BNCreatePlatform(arch->GetArchitectureObject(), name.c_str()); -} - - -Platform::~Platform() -{ - BNFreePlatform(m_platform); + m_object = BNCreatePlatform(arch->GetObject(), name.c_str()); } Ref<Architecture> Platform::GetArchitecture() const { - return new CoreArchitecture(BNGetPlatformArchitecture(m_platform)); + return new CoreArchitecture(BNGetPlatformArchitecture(m_object)); } string Platform::GetName() const { - char* str = BNGetPlatformName(m_platform); + char* str = BNGetPlatformName(m_object); string result = str; BNFreeString(str); return result; @@ -38,7 +33,7 @@ string Platform::GetName() const void Platform::Register(const string& os, Platform* platform) { - BNRegisterPlatform(os.c_str(), platform->GetPlatformObject()); + BNRegisterPlatform(os.c_str(), platform->GetObject()); } @@ -68,7 +63,7 @@ vector<Ref<Platform>> Platform::GetList() vector<Ref<Platform>> Platform::GetList(Architecture* arch) { size_t count; - BNPlatform** list = BNGetPlatformListByArchitecture(arch->GetArchitectureObject(), &count); + BNPlatform** list = BNGetPlatformListByArchitecture(arch->GetObject(), &count); vector<Ref<Platform>> result; for (size_t i = 0; i < count; i++) @@ -96,7 +91,7 @@ vector<Ref<Platform>> Platform::GetList(const string& os) vector<Ref<Platform>> Platform::GetList(const string& os, Architecture* arch) { size_t count; - BNPlatform** list = BNGetPlatformListByOSAndArchitecture(os.c_str(), arch->GetArchitectureObject(), &count); + BNPlatform** list = BNGetPlatformListByOSAndArchitecture(os.c_str(), arch->GetObject(), &count); vector<Ref<Platform>> result; for (size_t i = 0; i < count; i++) @@ -123,7 +118,7 @@ vector<std::string> Platform::GetOSList() Ref<CallingConvention> Platform::GetDefaultCallingConvention() const { - BNCallingConvention* cc = BNGetPlatformDefaultCallingConvention(m_platform); + BNCallingConvention* cc = BNGetPlatformDefaultCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -132,7 +127,7 @@ Ref<CallingConvention> Platform::GetDefaultCallingConvention() const Ref<CallingConvention> Platform::GetCdeclCallingConvention() const { - BNCallingConvention* cc = BNGetPlatformCdeclCallingConvention(m_platform); + BNCallingConvention* cc = BNGetPlatformCdeclCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -141,7 +136,7 @@ Ref<CallingConvention> Platform::GetCdeclCallingConvention() const Ref<CallingConvention> Platform::GetStdcallCallingConvention() const { - BNCallingConvention* cc = BNGetPlatformStdcallCallingConvention(m_platform); + BNCallingConvention* cc = BNGetPlatformStdcallCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -150,7 +145,7 @@ Ref<CallingConvention> Platform::GetStdcallCallingConvention() const Ref<CallingConvention> Platform::GetFastcallCallingConvention() const { - BNCallingConvention* cc = BNGetPlatformFastcallCallingConvention(m_platform); + BNCallingConvention* cc = BNGetPlatformFastcallCallingConvention(m_object); if (!cc) return nullptr; return new CoreCallingConvention(cc); @@ -160,7 +155,7 @@ Ref<CallingConvention> Platform::GetFastcallCallingConvention() const vector<Ref<CallingConvention>> Platform::GetCallingConventions() const { size_t count; - BNCallingConvention** list = BNGetPlatformCallingConventions(m_platform, &count); + BNCallingConvention** list = BNGetPlatformCallingConventions(m_object, &count); vector<Ref<CallingConvention>> result; for (size_t i = 0; i < count; i++) @@ -173,37 +168,37 @@ vector<Ref<CallingConvention>> Platform::GetCallingConventions() const void Platform::RegisterCallingConvention(CallingConvention* cc) { - BNRegisterPlatformCallingConvention(m_platform, cc->GetCallingConventionObject()); + BNRegisterPlatformCallingConvention(m_object, cc->GetObject()); } void Platform::RegisterDefaultCallingConvention(CallingConvention* cc) { - BNRegisterPlatformDefaultCallingConvention(m_platform, cc->GetCallingConventionObject()); + BNRegisterPlatformDefaultCallingConvention(m_object, cc->GetObject()); } void Platform::RegisterCdeclCallingConvention(CallingConvention* cc) { - BNRegisterPlatformCdeclCallingConvention(m_platform, cc->GetCallingConventionObject()); + BNRegisterPlatformCdeclCallingConvention(m_object, cc->GetObject()); } void Platform::RegisterStdcallCallingConvention(CallingConvention* cc) { - BNRegisterPlatformStdcallCallingConvention(m_platform, cc->GetCallingConventionObject()); + BNRegisterPlatformStdcallCallingConvention(m_object, cc->GetObject()); } void Platform::RegisterFastcallCallingConvention(CallingConvention* cc) { - BNRegisterPlatformFastcallCallingConvention(m_platform, cc->GetCallingConventionObject()); + BNRegisterPlatformFastcallCallingConvention(m_object, cc->GetObject()); } Ref<Platform> Platform::GetRelatedPlatform(Architecture* arch) { - BNPlatform* platform = BNGetRelatedPlatform(m_platform, arch->GetArchitectureObject()); + BNPlatform* platform = BNGetRelatedPlatform(m_object, arch->GetObject()); if (!platform) return nullptr; return new Platform(platform); @@ -212,5 +207,5 @@ Ref<Platform> Platform::GetRelatedPlatform(Architecture* arch) void Platform::AddRelatedPlatform(Architecture* arch, Platform* platform) { - BNAddRelatedPlatform(m_platform, arch->GetArchitectureObject(), platform->GetPlatformObject()); + BNAddRelatedPlatform(m_object, arch->GetObject(), platform->GetObject()); } @@ -210,24 +210,23 @@ bool PluginCommand::IsValid(const PluginCommandContext& ctxt) const case DefaultPluginCommand: if (!m_command.defaultIsValid) return true; - return m_command.defaultIsValid(m_command.context, ctxt.view->GetViewObject()); + return m_command.defaultIsValid(m_command.context, ctxt.view->GetObject()); case AddressPluginCommand: if (!m_command.addressIsValid) return true; - return m_command.addressIsValid(m_command.context, ctxt.view->GetViewObject(), ctxt.address); + return m_command.addressIsValid(m_command.context, ctxt.view->GetObject(), ctxt.address); case RangePluginCommand: if (ctxt.length == 0) return false; if (!m_command.rangeIsValid) return true; - return m_command.rangeIsValid(m_command.context, ctxt.view->GetViewObject(), ctxt.address, ctxt.length); + return m_command.rangeIsValid(m_command.context, ctxt.view->GetObject(), ctxt.address, ctxt.length); case FunctionPluginCommand: if (!ctxt.function) return false; if (!m_command.functionIsValid) return true; - return m_command.functionIsValid(m_command.context, ctxt.view->GetViewObject(), - ctxt.function->GetFunctionObject()); + return m_command.functionIsValid(m_command.context, ctxt.view->GetObject(), ctxt.function->GetObject()); default: return false; } @@ -242,16 +241,16 @@ void PluginCommand::Execute(const PluginCommandContext& ctxt) const switch (m_command.type) { case DefaultPluginCommand: - m_command.defaultCommand(m_command.context, ctxt.view->GetViewObject()); + m_command.defaultCommand(m_command.context, ctxt.view->GetObject()); break; case AddressPluginCommand: - m_command.addressCommand(m_command.context, ctxt.view->GetViewObject(), ctxt.address); + m_command.addressCommand(m_command.context, ctxt.view->GetObject(), ctxt.address); break; case RangePluginCommand: - m_command.rangeCommand(m_command.context, ctxt.view->GetViewObject(), ctxt.address, ctxt.length); + m_command.rangeCommand(m_command.context, ctxt.view->GetObject(), ctxt.address, ctxt.length); break; case FunctionPluginCommand: - m_command.functionCommand(m_command.context, ctxt.view->GetViewObject(), ctxt.function->GetFunctionObject()); + m_command.functionCommand(m_command.context, ctxt.view->GetObject(), ctxt.function->GetObject()); break; default: break; diff --git a/tempfile.cpp b/tempfile.cpp index 33256c76..b4578f8d 100644 --- a/tempfile.cpp +++ b/tempfile.cpp @@ -6,41 +6,35 @@ using namespace std; TemporaryFile::TemporaryFile() { - m_file = BNCreateTemporaryFile(); + m_object = BNCreateTemporaryFile(); } TemporaryFile::TemporaryFile(const DataBuffer& contents) { - m_file = BNCreateTemporaryFileWithContents(contents.GetBufferObject()); + m_object = BNCreateTemporaryFileWithContents(contents.GetBufferObject()); } TemporaryFile::TemporaryFile(const string& contents) { DataBuffer buf(contents.c_str(), contents.size()); - m_file = BNCreateTemporaryFileWithContents(buf.GetBufferObject()); + m_object = BNCreateTemporaryFileWithContents(buf.GetBufferObject()); } -TemporaryFile::TemporaryFile(BNTemporaryFile* file): m_file(file) +TemporaryFile::TemporaryFile(BNTemporaryFile* file) { -} - - -TemporaryFile::~TemporaryFile() -{ - if (m_file) - BNFreeTemporaryFile(m_file); + m_object = file; } string TemporaryFile::GetPath() const { - if (!m_file) + if (!m_object) return string(); - char* str = BNGetTemporaryFilePath(m_file); + char* str = BNGetTemporaryFilePath(m_object); string result = str; BNFreeString(str); return result; @@ -49,7 +43,7 @@ string TemporaryFile::GetPath() const DataBuffer TemporaryFile::GetContents() { - if (!m_file) + if (!m_object) return DataBuffer(); - return DataBuffer(BNGetTemporaryFileContents(m_file)); + return DataBuffer(BNGetTemporaryFileContents(m_object)); } diff --git a/transform.cpp b/transform.cpp index 181f099c..2201ba54 100644 --- a/transform.cpp +++ b/transform.cpp @@ -4,15 +4,17 @@ using namespace BinaryNinja; using namespace std; -Transform::Transform(BNTransform* xform): m_xform(xform) +Transform::Transform(BNTransform* xform) { + m_object = xform; } Transform::Transform(BNTransformType type, const string& name, const string& longName, const string& group): - m_xform(nullptr), m_typeForRegister(type), m_nameForRegister(name), m_longNameForRegister(longName), + m_typeForRegister(type), m_nameForRegister(name), m_longNameForRegister(longName), m_groupForRegister(group) { + m_object = nullptr; } @@ -113,9 +115,10 @@ void Transform::Register(Transform* xform) callbacks.freeParameters = FreeParametersCallback; callbacks.decode = DecodeCallback; callbacks.encode = EncodeCallback; - xform->m_xform = BNRegisterTransformType(xform->m_typeForRegister, xform->m_nameForRegister.c_str(), - xform->m_longNameForRegister.c_str(), xform->m_groupForRegister.c_str(), - &callbacks); + xform->AddRefForRegistration(); + xform->m_object = BNRegisterTransformType(xform->m_typeForRegister, xform->m_nameForRegister.c_str(), + xform->m_longNameForRegister.c_str(), xform->m_groupForRegister.c_str(), + &callbacks); } @@ -144,13 +147,13 @@ vector<Ref<Transform>> Transform::GetTransformTypes() BNTransformType Transform::GetType() const { - return BNGetTransformType(m_xform); + return BNGetTransformType(m_object); } string Transform::GetName() const { - char* name = BNGetTransformName(m_xform); + char* name = BNGetTransformName(m_object); string result = name; BNFreeString(name); return result; @@ -159,7 +162,7 @@ string Transform::GetName() const string Transform::GetLongName() const { - char* name = BNGetTransformLongName(m_xform); + char* name = BNGetTransformLongName(m_object); string result = name; BNFreeString(name); return result; @@ -168,7 +171,7 @@ string Transform::GetLongName() const string Transform::GetGroup() const { - char* name = BNGetTransformGroup(m_xform); + char* name = BNGetTransformGroup(m_object); string result = name; BNFreeString(name); return result; @@ -203,7 +206,7 @@ CoreTransform::CoreTransform(BNTransform* xform): Transform(xform) vector<TransformParameter> CoreTransform::GetParameters() const { size_t count; - BNTransformParameterInfo* list = BNGetTransformParameterList(m_xform, &count); + BNTransformParameterInfo* list = BNGetTransformParameterList(m_object, &count); vector<TransformParameter> result; for (size_t i = 0; i < count; i++) @@ -230,7 +233,7 @@ bool CoreTransform::Decode(const DataBuffer& input, DataBuffer& output, const ma list[idx++].value = i.second.GetBufferObject(); } - bool result = BNDecode(m_xform, input.GetBufferObject(), output.GetBufferObject(), list, idx); + bool result = BNDecode(m_object, input.GetBufferObject(), output.GetBufferObject(), list, idx); delete[] list; return result; @@ -247,7 +250,7 @@ bool CoreTransform::Encode(const DataBuffer& input, DataBuffer& output, const ma list[idx++].value = i.second.GetBufferObject(); } - bool result = BNEncode(m_xform, input.GetBufferObject(), output.GetBufferObject(), list, idx); + bool result = BNEncode(m_object, input.GetBufferObject(), output.GetBufferObject(), list, idx); delete[] list; return result; @@ -4,56 +4,51 @@ using namespace BinaryNinja; using namespace std; -Type::Type(BNType* type): m_type(type) +Type::Type(BNType* type) { -} - - -Type::~Type() -{ - BNFreeType(m_type); + m_object = type; } BNTypeClass Type::GetClass() const { - return BNGetTypeClass(m_type); + return BNGetTypeClass(m_object); } uint64_t Type::GetWidth() const { - return BNGetTypeWidth(m_type); + return BNGetTypeWidth(m_object); } size_t Type::GetAlignment() const { - return BNGetTypeAlignment(m_type); + return BNGetTypeAlignment(m_object); } bool Type::IsSigned() const { - return BNIsTypeSigned(m_type); + return BNIsTypeSigned(m_object); } bool Type::IsConst() const { - return BNIsTypeConst(m_type); + return BNIsTypeConst(m_object); } bool Type::IsFloat() const { - return BNIsTypeFloatingPoint(m_type); + return BNIsTypeFloatingPoint(m_object); } Ref<Type> Type::GetChildType() const { - BNType* type = BNGetChildType(m_type); + BNType* type = BNGetChildType(m_object); if (type) return new Type(type); return nullptr; @@ -62,7 +57,7 @@ Ref<Type> Type::GetChildType() const Ref<CallingConvention> Type::GetCallingConvention() const { - BNCallingConvention* cc = BNGetTypeCallingConvention(m_type); + BNCallingConvention* cc = BNGetTypeCallingConvention(m_object); if (cc) return new CoreCallingConvention(cc); return nullptr; @@ -72,7 +67,7 @@ Ref<CallingConvention> Type::GetCallingConvention() const vector<NameAndType> Type::GetParameters() const { size_t count; - BNNameAndType* types = BNGetTypeParameters(m_type, &count); + BNNameAndType* types = BNGetTypeParameters(m_object, &count); vector<NameAndType> result; for (size_t i = 0; i < count; i++) @@ -90,19 +85,19 @@ vector<NameAndType> Type::GetParameters() const bool Type::HasVariableArguments() const { - return BNTypeHasVariableArguments(m_type); + return BNTypeHasVariableArguments(m_object); } bool Type::CanReturn() const { - return BNFunctionTypeCanReturn(m_type); + return BNFunctionTypeCanReturn(m_object); } Ref<Structure> Type::GetStructure() const { - BNStructure* s = BNGetTypeStructure(m_type); + BNStructure* s = BNGetTypeStructure(m_object); if (s) return new Structure(s); return nullptr; @@ -111,7 +106,7 @@ Ref<Structure> Type::GetStructure() const Ref<Enumeration> Type::GetEnumeration() const { - BNEnumeration* e = BNGetTypeEnumeration(m_type); + BNEnumeration* e = BNGetTypeEnumeration(m_object); if (e) return new Enumeration(e); return nullptr; @@ -120,13 +115,13 @@ Ref<Enumeration> Type::GetEnumeration() const uint64_t Type::GetElementCount() const { - return BNGetTypeElementCount(m_type); + return BNGetTypeElementCount(m_object); } string Type::GetString() const { - char* str = BNGetTypeString(m_type); + char* str = BNGetTypeString(m_object); string result = str; BNFreeString(str); return result; @@ -135,7 +130,7 @@ string Type::GetString() const string Type::GetStringBeforeName() const { - char* str = BNGetTypeStringBeforeName(m_type); + char* str = BNGetTypeStringBeforeName(m_object); string result = str; BNFreeString(str); return result; @@ -144,7 +139,7 @@ string Type::GetStringBeforeName() const string Type::GetStringAfterName() const { - char* str = BNGetTypeStringAfterName(m_type); + char* str = BNGetTypeStringAfterName(m_object); string result = str; BNFreeString(str); return result; @@ -153,7 +148,7 @@ string Type::GetStringAfterName() const Ref<Type> Type::Duplicate() const { - return new Type(BNDuplicateType(m_type)); + return new Type(BNDuplicateType(m_object)); } @@ -183,25 +178,25 @@ Ref<Type> Type::FloatType(size_t width) Ref<Type> Type::StructureType(Structure* strct) { - return new Type(BNCreateStructureType(strct->GetStructureObject())); + return new Type(BNCreateStructureType(strct->GetObject())); } Ref<Type> Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width) { - return new Type(BNCreateEnumerationType(arch->GetArchitectureObject(), enm->GetEnumerationObject(), width)); + return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width)); } Ref<Type> Type::PointerType(Architecture* arch, Type* type, bool cnst) { - return new Type(BNCreatePointerType(arch->GetArchitectureObject(), type->GetTypeObject(), cnst)); + return new Type(BNCreatePointerType(arch->GetObject(), type->GetObject(), cnst)); } Ref<Type> Type::ArrayType(Type* type, uint64_t elem) { - return new Type(BNCreateArrayType(type->GetTypeObject(), elem)); + return new Type(BNCreateArrayType(type->GetObject(), elem)); } @@ -212,31 +207,26 @@ Ref<Type> Type::FunctionType(Type* returnValue, CallingConvention* callingConven for (size_t i = 0; i < params.size(); i++) { paramArray[i].name = (char*)params[i].name.c_str(); - paramArray[i].type = params[i].type->GetTypeObject(); + paramArray[i].type = params[i].type->GetObject(); } - Type* type = new Type(BNCreateFunctionType(returnValue->GetTypeObject(), - callingConvention ? callingConvention->GetCallingConventionObject() : nullptr, + Type* type = new Type(BNCreateFunctionType(returnValue->GetObject(), + callingConvention ? callingConvention->GetObject() : nullptr, paramArray, params.size(), varArg)); delete[] paramArray; return type; } -Structure::Structure(BNStructure* s): m_struct(s) -{ -} - - -Structure::~Structure() +Structure::Structure(BNStructure* s) { - BNFreeStructure(m_struct); + m_object = s; } string Structure::GetName() const { - char* name = BNGetStructureName(m_struct); + char* name = BNGetStructureName(m_object); string result = name; BNFreeString(name); return result; @@ -245,14 +235,14 @@ string Structure::GetName() const void Structure::SetName(const string& name) { - BNSetStructureName(m_struct, name.c_str()); + BNSetStructureName(m_object, name.c_str()); } vector<StructureMember> Structure::GetMembers() const { size_t count; - BNStructureMember* members = BNGetStructureMembers(m_struct, &count); + BNStructureMember* members = BNGetStructureMembers(m_object, &count); vector<StructureMember> result; for (size_t i = 0; i < count; i++) @@ -271,72 +261,67 @@ vector<StructureMember> Structure::GetMembers() const uint64_t Structure::GetWidth() const { - return BNGetStructureWidth(m_struct); + return BNGetStructureWidth(m_object); } size_t Structure::GetAlignment() const { - return BNGetStructureAlignment(m_struct); + return BNGetStructureAlignment(m_object); } bool Structure::IsPacked() const { - return BNIsStructurePacked(m_struct); + return BNIsStructurePacked(m_object); } void Structure::SetPacked(bool packed) { - BNSetStructurePacked(m_struct, packed); + BNSetStructurePacked(m_object, packed); } bool Structure::IsUnion() const { - return BNIsStructureUnion(m_struct); + return BNIsStructureUnion(m_object); } void Structure::SetUnion(bool u) { - BNSetStructureUnion(m_struct, u); + BNSetStructureUnion(m_object, u); } void Structure::AddMember(Type* type, const string& name) { - BNAddStructureMember(m_struct, type->GetTypeObject(), name.c_str()); + BNAddStructureMember(m_object, type->GetObject(), name.c_str()); } void Structure::AddMemberAtOffset(Type* type, const string& name, uint64_t offset) { - BNAddStructureMemberAtOffset(m_struct, type->GetTypeObject(), name.c_str(), offset); + BNAddStructureMemberAtOffset(m_object, type->GetObject(), name.c_str(), offset); } void Structure::RemoveMember(size_t idx) { - BNRemoveStructureMember(m_struct, idx); -} - - -Enumeration::Enumeration(BNEnumeration* e): m_enum(e) -{ + BNRemoveStructureMember(m_object, idx); } -Enumeration::~Enumeration() +Enumeration::Enumeration(BNEnumeration* e) { - BNFreeEnumeration(m_enum); + m_object = e; } string Enumeration::GetName() const { - char* name = BNGetEnumerationName(m_enum); + char* name = BNGetEnumerationName(m_object); string result = name; BNFreeString(name); return result; @@ -345,14 +330,14 @@ string Enumeration::GetName() const void Enumeration::SetName(const string& name) { - BNSetEnumerationName(m_enum, name.c_str()); + BNSetEnumerationName(m_object, name.c_str()); } vector<EnumerationMember> Enumeration::GetMembers() const { size_t count; - BNEnumerationMember* members = BNGetEnumerationMembers(m_enum, &count); + BNEnumerationMember* members = BNGetEnumerationMembers(m_object, &count); vector<EnumerationMember> result; for (size_t i = 0; i < count; i++) @@ -371,13 +356,13 @@ vector<EnumerationMember> Enumeration::GetMembers() const void Enumeration::AddMember(const string& name) { - BNAddEnumerationMember(m_enum, name.c_str()); + BNAddEnumerationMember(m_object, name.c_str()); } void Enumeration::AddMemberWithValue(const string& name, uint64_t value) { - BNAddEnumerationMemberWithValue(m_enum, name.c_str(), value); + BNAddEnumerationMemberWithValue(m_object, name.c_str(), value); } |
