Skip to content

Commit

Permalink
Merge branch 'Vector35:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
unknowntrojan authored Jan 2, 2024
2 parents 19ac3eb + 7e566f1 commit 7f9ec65
Show file tree
Hide file tree
Showing 130 changed files with 9,055 additions and 629 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "suite/binaries"]
path = suite/binaries
url = https://github.com/Vector35/BinaryTestCases.git
[submodule "vendor/fmt"]
path = vendor/fmt
url = https://github.com/fmtlib/fmt.git
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ target_link_libraries(binaryninjaapi PUBLIC ${BinaryNinjaCore_LIBRARIES})
target_link_directories(binaryninjaapi PUBLIC ${BinaryNinjaCore_LIBRARY_DIRS})
target_compile_definitions(binaryninjaapi PUBLIC ${BinaryNinjaCore_DEFINITIONS})

add_subdirectory(vendor/fmt)
target_link_libraries(binaryninjaapi PUBLIC fmt::fmt)

set_target_properties(binaryninjaapi PROPERTIES
CXX_STANDARD 17
CXX_VISIBILITY_PRESET hidden
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cmake --build build -j8
In addition to the default build setup, you may want to:

- **Build examples.** To build the [API examples](#examples), pass `-DBN_API_BUILD_EXAMPLES=ON` to CMake when configuring the build. After the build succeeds, you can install the built plugins by running the `install` target. When using the "Unix Makefiles" build generator, this looks like: `make install`.
- **Build UI plugins.** You will need Qt 6.5.3 (as of writing) installed to build UI plugins.
- **Build UI plugins.** You will need Qt 6.6.1 (as of writing) installed to build UI plugins.
- **Build headlessly.** If you are using a headless Binary Ninja distribution or you do not wish to build UI plugins, pass `-DHEADLESS=ON` to CMake when configuring the build.

### Troubleshooting
Expand Down
36 changes: 14 additions & 22 deletions architecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,43 +899,33 @@ bool Architecture::GetInstructionLowLevelIL(const uint8_t*, uint64_t, size_t&, L

string Architecture::GetRegisterName(uint32_t reg)
{
char regStr[32];
snprintf(regStr, sizeof(regStr), "r%" PRIu32, reg);
return regStr;
return fmt::format("r{}", reg);
}


string Architecture::GetFlagName(uint32_t flag)
{
char flagStr[32];
snprintf(flagStr, sizeof(flagStr), "flag%" PRIu32, flag);
return flagStr;
return fmt::format("flag{}", flag);
}


string Architecture::GetFlagWriteTypeName(uint32_t flags)
{
char flagStr[32];
snprintf(flagStr, sizeof(flagStr), "update%" PRIu32, flags);
return flagStr;
return fmt::format("update{}", flags);
}


string Architecture::GetSemanticFlagClassName(uint32_t semClass)
{
if (semClass == 0)
return "";
char flagStr[32];
snprintf(flagStr, sizeof(flagStr), "semantic%" PRIu32, semClass);
return flagStr;
return fmt::format("semantic{}", semClass);
}


string Architecture::GetSemanticFlagGroupName(uint32_t semGroup)
{
char flagStr[32];
snprintf(flagStr, sizeof(flagStr), "group%" PRIu32, semGroup);
return flagStr;
return fmt::format("group{}", semGroup);
}


Expand Down Expand Up @@ -1097,9 +1087,7 @@ bool Architecture::IsSystemRegister(uint32_t reg)

string Architecture::GetRegisterStackName(uint32_t regStack)
{
char regStr[32];
snprintf(regStr, sizeof(regStr), "reg_stack_%" PRIu32, regStack);
return regStr;
return fmt::format("reg_stack_{}", regStack);
}


Expand Down Expand Up @@ -1129,9 +1117,7 @@ uint32_t Architecture::GetRegisterStackForRegister(uint32_t reg)

string Architecture::GetIntrinsicName(uint32_t intrinsic)
{
char intrinsicStr[32];
snprintf(intrinsicStr, sizeof(intrinsicStr), "intrinsic_%" PRIu32, intrinsic);
return intrinsicStr;
return fmt::format("intrinsic_{}", intrinsic);
}


Expand Down Expand Up @@ -2443,7 +2429,13 @@ vector<DisassemblyTextLine> DisassemblyTextRenderer::PostProcessInstructionTextL
size_t count = 0;
result = BNPostProcessDisassemblyTextRendererLines(
m_object, addr, len, inLines, lines.size(), &count, indentSpaces.c_str());
BNFreeDisassemblyTextLines(inLines, lines.size());

for (size_t i = 0; i < lines.size(); i++)
{
InstructionTextToken::FreeInstructionTextTokenList(inLines[i].tokens, inLines[i].count);
Tag::FreeTagList(inLines[i].tags, inLines[i].tagCount);
}
delete[] inLines;

vector<DisassemblyTextLine> outLines;
for (size_t i = 0; i < count; i++)
Expand Down
154 changes: 154 additions & 0 deletions binaryninjaapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,157 @@ bool BinaryNinja::ProgressCallback(void* ctxt, size_t current, size_t total)
return true;
return pctxt->callback(current, total);
}


fmt::format_context::iterator fmtByteString(const std::vector<uint8_t>& string, fmt::format_context& ctx)
{
*ctx.out()++ = 'b';
*ctx.out()++ = '\"';
for (uint8_t ch: string)
{
if (ch == '\n')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 'n';
}
else if (ch == '\r')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 'r';
}
else if (ch == '\t')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 't';
}
else if (ch == '\"')
{
*ctx.out()++ = '\\';
*ctx.out()++ = '\"';
}
else if (ch == '\\')
{
*ctx.out()++ = '\\';
*ctx.out()++ = '\\';
}
else if (ch < 0x20 || ch >= 0x7f)
{
fmt::format_to(ctx.out(), "\\x{:02x}", ch);
}
else
{
*ctx.out()++ = ch;
}
}
*ctx.out()++ = '\"';
return ctx.out();
}


fmt::format_context::iterator fmtQuotedString(const std::string& string, fmt::format_context& ctx)
{
*ctx.out()++ = '\"';
for (char ch: string)
{
if (ch == '\n')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 'n';
}
else if (ch == '\r')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 'r';
}
else if (ch == '\t')
{
*ctx.out()++ = '\\';
*ctx.out()++ = 't';
}
else if (ch == '\"')
{
*ctx.out()++ = '\\';
*ctx.out()++ = '\"';
}
else if (ch == '\\')
{
*ctx.out()++ = '\\';
*ctx.out()++ = '\\';
}
else if (ch < 0x20 || ch >= 0x7f)
{
fmt::format_to(ctx.out(), "\\x{:02x}", ch);
}
else
{
*ctx.out()++ = ch;
}
}
*ctx.out()++ = '\"';
return ctx.out();
}


fmt::format_context::iterator fmt::formatter<BinaryNinja::Metadata>::format(const BinaryNinja::Metadata& obj, format_context& ctx) const
{
switch (obj.GetType())
{
default:
case InvalidDataType:
return fmt::format_to(ctx.out(), "(invalid)");
case BooleanDataType:
return fmt::format_to(ctx.out(), "{}", obj.GetBoolean());
case StringDataType:
return fmt::format_to(ctx.out(), "{}", obj.GetString());
case UnsignedIntegerDataType:
return fmt::format_to(ctx.out(), "{}", obj.GetUnsignedInteger());
case SignedIntegerDataType:
return fmt::format_to(ctx.out(), "{}", obj.GetSignedInteger());
case DoubleDataType:
return fmt::format_to(ctx.out(), "{}", obj.GetDouble());
case RawDataType:
return fmtByteString(obj.GetRaw(), ctx);
case KeyValueDataType:
{
*ctx.out()++ = '{';
bool first = true;
for (auto& [name, value]: obj.GetKeyValueStore())
{
if (!first)
{
*ctx.out()++ = ',';
*ctx.out()++ = ' ';
}
first = false;

fmtQuotedString(name, ctx);
*ctx.out()++ = ':';
*ctx.out()++ = ' ';
fmt::format_to(ctx.out(), "{}", value);
}
*ctx.out()++ = '}';
return ctx.out();
}
case ArrayDataType:
*ctx.out()++ = '[';
bool first = true;
for (auto& value: obj.GetArray())
{
if (!first)
{
*ctx.out()++ = ',';
*ctx.out()++ = ' ';
}
first = false;
fmt::format_to(ctx.out(), "{}", value);
}
*ctx.out()++ = ']';
return ctx.out();
}
}


fmt::format_context::iterator fmt::formatter<BinaryNinja::NameList>::format(const BinaryNinja::NameList& obj, format_context& ctx) const
{
return fmt::format_to(ctx.out(), "{}", obj.GetString());
}
Loading

0 comments on commit 7f9ec65

Please sign in to comment.