Skip to content

Commit

Permalink
Update C++ example code to use BinaryNinja::Load to open a file. Fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed May 17, 2024
1 parent e07d7c1 commit df3c57d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 95 deletions.
24 changes: 5 additions & 19 deletions examples/bin-info/src/bin-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,13 @@ int main(int argc, char* argv[])
SetBundledPluginDirectory(GetBundledPluginDirectory());
InitPlugins();

Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
if (!bd)
{
fprintf(stderr, "Could not open input file.\n");
return -1;
}
Ref<BinaryView> bv;
for (auto type : BinaryViewType::GetViewTypes())
{
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
{
bv = type->Create(bd);
break;
}
}

Ref<BinaryView> bv = BinaryNinja::Load(fname);
if (!bv || bv->GetTypeName() == "Raw")
{
fprintf(stderr, "Input file does not appear to be an exectuable\n");
fprintf(stderr, "Input file does not appear to be an executable\n");
return -1;
}

bv->UpdateAnalysisAndWait();

cout << "Target: " << fname << endl << endl;
cout << "TYPE: " << bv->GetTypeName() << endl;
cout << "START: 0x" << hex << bv->GetStart() << endl;
Expand Down Expand Up @@ -100,6 +83,9 @@ int main(int argc, char* argv[])
break;
}

// Close the file so that the resources can be freed
bv->GetFile()->Close();

// Shutting down is required to allow for clean exit of the core
BNShutdown();

Expand Down
24 changes: 5 additions & 19 deletions examples/enterprise_test/src/enterprise_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,13 @@ int main(int argc, char* argv[])
SetBundledPluginDirectory(GetBundledPluginDirectory());
InitPlugins();

Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
if (!bd)
{
fprintf(stderr, "Could not open input file.\n");
return -1;
}
Ref<BinaryView> bv;
for (auto type : BinaryViewType::GetViewTypes())
{
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
{
bv = type->Create(bd);
break;
}
}

Ref<BinaryView> bv = BinaryNinja::Load(fname);
if (!bv || bv->GetTypeName() == "Raw")
{
fprintf(stderr, "Input file does not appear to be an exectuable\n");
fprintf(stderr, "Input file does not appear to be an executable\n");
return -1;
}

bv->UpdateAnalysisAndWait();

cout << "Target: " << fname << endl << endl;
cout << "TYPE: " << bv->GetTypeName() << endl;
cout << "START: 0x" << hex << bv->GetStart() << endl;
Expand Down Expand Up @@ -99,6 +82,9 @@ int main(int argc, char* argv[])
break;
}

// Close the file so that the resources can be freed
bv->GetFile()->Close();

// Shutting down is required to allow for clean exit of the core
BNShutdown();

Expand Down
24 changes: 5 additions & 19 deletions examples/llil_parser/src/llil_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,13 @@ int main(int argc, char* argv[])
SetBundledPluginDirectory(GetBundledPluginDirectory());
InitPlugins();

Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
if (!bd)
{
fprintf(stderr, "Could not open input file.\n");
return -1;
}
Ref<BinaryView> bv;
for (auto type : BinaryViewType::GetViewTypes())
{
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
{
bv = type->Create(bd);
break;
}
}

Ref<BinaryView> bv = BinaryNinja::Load(argv[1]);
if (!bv || bv->GetTypeName() == "Raw")
{
fprintf(stderr, "Input file does not appear to be an exectuable\n");
fprintf(stderr, "Input file does not appear to be an executable\n");
return -1;
}

bv->UpdateAnalysisAndWait();

// Go through all functions in the binary
for (auto& func : bv->GetAnalysisFunctionList())
{
Expand Down Expand Up @@ -398,6 +381,9 @@ int main(int argc, char* argv[])
printf("\n");
}

// Close the file so that the resources can be freed
bv->GetFile()->Close();

// Shutting down is required to allow for clean exit of the core
BNShutdown();

Expand Down
24 changes: 5 additions & 19 deletions examples/mlil_parser/src/mlil_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,13 @@ int main(int argc, char* argv[])
SetBundledPluginDirectory(GetBundledPluginDirectory());
InitPlugins();

Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
if (!bd)
{
fprintf(stderr, "Could not open input file.\n");
return -1;
}
Ref<BinaryView> bv;
for (auto type : BinaryViewType::GetViewTypes())
{
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
{
bv = type->Create(bd);
break;
}
}

Ref<BinaryView> bv = BinaryNinja::Load(argv[1]);
if (!bv || bv->GetTypeName() == "Raw")
{
fprintf(stderr, "Input file does not appear to be an exectuable\n");
fprintf(stderr, "Input file does not appear to be an executable\n");
return -1;
}

bv->UpdateAnalysisAndWait();

// Go through all functions in the binary
for (auto& func : bv->GetAnalysisFunctionList())
{
Expand Down Expand Up @@ -342,6 +325,9 @@ int main(int argc, char* argv[])
printf("\n");
}

// Close the file so that the resources can be freed
bv->GetFile()->Close();

// Shutting down is required to allow for clean exit of the core
BNShutdown();

Expand Down
24 changes: 5 additions & 19 deletions examples/print_syscalls/src/arm-syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,13 @@ int main(int argc, char* argv[])
SetBundledPluginDirectory(GetBundledPluginDirectory());
InitPlugins();

Ref<BinaryData> bd = BinaryData::CreateFromFilename(new FileMetadata(), argv[1]);
if (!bd)
{
fprintf(stderr, "Could not open input file.\n");
return -1;
}
Ref<BinaryView> bv;
for (auto type : BinaryViewType::GetViewTypes())
{
if (type->IsTypeValidForData(bd) && type->GetName() != "Raw")
{
bv = type->Create(bd);
break;
}
}

Ref<BinaryView> bv = BinaryNinja::Load(fname);
if (!bv || bv->GetTypeName() == "Raw")
{
fprintf(stderr, "Input file does not appear to be an exectuable\n");
fprintf(stderr, "Input file does not appear to be an executable\n");
return -1;
}

bv->UpdateAnalysisAndWait();

auto arch = bv->GetDefaultArchitecture();
auto platform = bv->GetDefaultPlatform();

Expand Down Expand Up @@ -97,6 +80,9 @@ int main(int argc, char* argv[])
}
}

// Close the file so that the resources can be freed
bv->GetFile()->Close();

// Shutting down is required to allow for clean exit of the core
BNShutdown();

Expand Down

0 comments on commit df3c57d

Please sign in to comment.