From acc1d309d20ac4414db230f52cd75d877084b969 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Tue, 15 Nov 2022 16:23:50 -0500 Subject: Add TypePrinter::PrintAllTypes to export C headers --- typeprinter.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'typeprinter.cpp') diff --git a/typeprinter.cpp b/typeprinter.cpp index aed010b2..3242f323 100644 --- a/typeprinter.cpp +++ b/typeprinter.cpp @@ -116,6 +116,22 @@ bool TypePrinter::GetTypeLinesCallback(void* ctxt, BNType* type, BNBinaryView* d } +bool TypePrinter::PrintAllTypesCallback(void* ctxt, BNQualifiedName* names, BNType** types, size_t typeCount, + BNBinaryView* data, int lineWidth, BNTokenEscapingType escaping, char** result) +{ + TypePrinter* printer = (TypePrinter*)ctxt; + vector>> apiTypes; + for (size_t i = 0; i < typeCount; ++i) + { + apiTypes.push_back({QualifiedName::FromAPIObject(&names[i]), new Type(types[i])}); + } + + string resultStr = printer->PrintAllTypes(apiTypes, new BinaryView(data), lineWidth, escaping); + *result = BNAllocString(resultStr.c_str()); + return true; +} + + void TypePrinter::FreeTokensCallback(void* ctxt, BNInstructionTextToken* tokens, size_t count) { InstructionTextToken::FreeInstructionTextTokenList(tokens, count); @@ -246,6 +262,50 @@ std::string TypePrinter::GetTypeStringAfterName( } +std::string TypePrinter::PrintAllTypes( + const std::vector>>& types, + Ref data, + int lineWidth, + BNTokenEscapingType escaping +) +{ + return DefaultPrintAllTypes(types, data, lineWidth, escaping); +} + + +std::string TypePrinter::DefaultPrintAllTypes( + const std::vector>>& types, + Ref data, + int lineWidth, + BNTokenEscapingType escaping +) +{ + BNQualifiedName* apiNames = new BNQualifiedName[types.size()]; + BNType** apiTypes = new BNType*[types.size()]; + + for (size_t i = 0; i < types.size(); i ++) + { + apiNames[i] = types[i].first.GetAPIObject(); + apiTypes[i] = types[i].second->GetObject(); + } + + char* resultStr; + BNTypePrinterDefaultPrintAllTypes(m_object, apiNames, apiTypes, types.size(), data->GetObject(), + lineWidth, escaping, &resultStr); + + for (size_t i = 0; i < types.size(); i ++) + { + QualifiedName::FreeAPIObject(&apiNames[i]); + } + delete[] apiTypes; + delete[] apiNames; + + std::string result = resultStr; + BNFreeString(resultStr); + return result; +} + + CoreTypePrinter::CoreTypePrinter(BNTypePrinter* printer): TypePrinter(printer) { @@ -398,3 +458,36 @@ std::vector CoreTypePrinter::GetTypeLines(Ref type, return cppLines; } + + +std::string CoreTypePrinter::PrintAllTypes( + const std::vector>>& types, + Ref data, + int lineWidth, + BNTokenEscapingType escaping +) +{ + BNQualifiedName* apiNames = new BNQualifiedName[types.size()]; + BNType** apiTypes = new BNType*[types.size()]; + + for (size_t i = 0; i < types.size(); i ++) + { + apiNames[i] = types[i].first.GetAPIObject(); + apiTypes[i] = types[i].second->GetObject(); + } + + char* resultStr; + BNTypePrinterPrintAllTypes(m_object, apiNames, apiTypes, types.size(), data->GetObject(), + lineWidth, escaping, &resultStr); + + for (size_t i = 0; i < types.size(); i ++) + { + QualifiedName::FreeAPIObject(&apiNames[i]); + } + delete[] apiTypes; + delete[] apiNames; + + std::string result = resultStr; + BNFreeString(resultStr); + return result; +} -- cgit v1.3.1