Skip to content

Commit 4eec38b

Browse files
committed
clang-tidy fixes, added --simple arg, misc changes
1 parent 3c0605a commit 4eec38b

9 files changed

Lines changed: 137 additions & 129 deletions

File tree

src/cli/globals.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "globals.hpp"
22

3-
std::string dim = "\x1B[38;2;60;60;60m";
4-
std::string medium = "\x1B[38;2;120;120;120m";
3+
std::string dim = "\x1B[38;2;120;120;120m";
54
std::string bright = "\x1B[38;2;180;180;180m";
65

76
std::string bold = "\x1B[1;97m";
@@ -24,5 +23,6 @@ u8 disabled_count = 0;
2423

2524
std::string tag_detected = bold + "[" + green + " DETECTED " + bold + "]" + ansi_exit;
2625
std::string tag_not_detected = "[" + red + "NOT DETECTED" + ansi_exit + "]";
27-
std::string tag_skipped = ("\x1B[97m[\x1B[90m DISABLED \x1B[97m]\x1B[0m");
28-
std::string tag_notes = ("\x1B[97m[\x1B[90m NOTES \x1B[97m]\x1B[0m");
26+
std::string tag_skipped = "[" + grey + " DISABLED " + ansi_exit + "]";
27+
std::string tag_no_perms = "[" + grey + " NO PERMS " + ansi_exit + "]";
28+
std::string tag_notes = "[ NOTE ]";

src/cli/globals.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22

3+
#include <array>
34
#include <string>
45
#include <bitset>
56
#include "types.hpp"
67

78
extern std::string dim;
8-
extern std::string medium;
99
extern std::string bright;
1010

1111
extern std::string bold;
@@ -40,7 +40,8 @@ enum arg_enum : u8 {
4040
ENUMS,
4141
DETECTED_ONLY,
4242
JSON,
43-
NULL_ARG
43+
SIMPLE,
44+
NULL_ARG,
4445
};
4546

4647
constexpr u8 arg_bits = static_cast<u8>(NULL_ARG) + 1;
@@ -54,4 +55,10 @@ extern u8 disabled_count;
5455
extern std::string tag_detected;
5556
extern std::string tag_not_detected;
5657
extern std::string tag_skipped;
57-
extern std::string tag_notes;
58+
extern std::string tag_no_perms;
59+
extern std::string tag_notes;
60+
61+
// increment this each time a new argument is introduced
62+
constexpr std::size_t arg_count = 34;
63+
64+
using arg_table = std::array<std::pair<const char*, arg_enum>, arg_count>;

src/cli/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <vector>
2626
#include <fstream>
2727
#include <iostream>
28-
#include <array>
2928
#include <algorithm>
3029
#include <cstring>
3130
#include <string>
@@ -73,6 +72,7 @@ R"(Usage:
7372
--enums display the technique enum name used by the lib
7473
--detected-only only display the techniques that were detected
7574
--json output a json-formatted file of the results
75+
--simple output a simpler alternative of the output (Windows specific)
7676
7777
)";
7878

@@ -228,7 +228,7 @@ int main(int argc, char* argv[]) {
228228
return 0;
229229
}
230230

231-
static const std::array<std::pair<const char*, arg_enum>, 33> table{ {
231+
static const arg_table table{ {
232232
{ "-h", HELP },
233233
{ "-v", VERSION },
234234
{ "-a", ALL },
@@ -261,7 +261,8 @@ int main(int argc, char* argv[]) {
261261
{ "--no-ansi", NO_ANSI },
262262
{ "--detected-only", DETECTED_ONLY },
263263
{ "--json", JSON },
264-
{ "--output", OUTPUT }
264+
{ "--output", OUTPUT },
265+
{ "--simple", SIMPLE }
265266
} };
266267

267268
std::string potential_null_arg;
@@ -294,7 +295,7 @@ int main(int argc, char* argv[]) {
294295

295296
if (it == table.end()) {
296297
if (arg_bitset.test(OUTPUT)) {
297-
std::ofstream file(arg_string);
298+
const std::ofstream file(arg_string);
298299

299300
if (file.good()) {
300301
potential_output_arg = arg_string;

src/cli/output.cpp

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "../vmaware.hpp"
22
#include "output.hpp"
3-
#include "windows_cli.hpp"
3+
#include "windows_tui.hpp"
44
#include "globals.hpp"
55

66
#include <chrono>
@@ -121,6 +121,7 @@ static std::pair<bool, VM::enum_flags> string_to_technique(const std::string& na
121121
return { true, flag };
122122
}
123123
}
124+
124125
return { false, VM::NULL_ARG };
125126
}
126127

@@ -132,6 +133,7 @@ const char* get_vm_description(const std::string& vm_brand) {
132133
if (is_vm_brand_multiple(vm_brand)) {
133134
return "";
134135
}
136+
135137
struct brand_entry { const char* brand; const char* description; };
136138

137139
static const brand_entry table[] = {
@@ -244,7 +246,7 @@ static void checker(const VM::enum_flags flag, const char* message) {
244246
const bool result = VM::check(flag);
245247
auto end_time = std::chrono::high_resolution_clock::now();
246248

247-
double ms = std::chrono::duration<double, std::milli>(end_time - start_time).count();
249+
const double ms = std::chrono::duration<double, std::milli>(end_time - start_time).count();
248250

249251
if (arg_bitset.test(DETECTED_ONLY) && !result) {
250252
return;
@@ -254,12 +256,15 @@ static void checker(const VM::enum_flags flag, const char* message) {
254256
if (are_perms_required(flag)) {
255257
no_perms_count++;
256258
VM::check(flag);
259+
std::ostringstream perms_oss;
260+
perms_oss << tag_no_perms << " " << grey << "Skipped " << message << "." << ansi_exit;
261+
PRINT_LINE(perms_oss.str());
257262
return;
258263
}
259264
#endif
260265

261266
std::ostringstream cycle_oss;
262-
cycle_oss << dim << message << " " << medium << "| " << white << std::fixed << std::setprecision(4) << ms << " ms" << ansi_exit;
267+
cycle_oss << dim << message << " | " << white << std::fixed << std::setprecision(4) << ms << " ms" << ansi_exit;
263268
#if (CLI_WINDOWS)
264269
g_tui.addCycle(cycle_oss.str());
265270
#endif
@@ -276,11 +281,11 @@ static void checker(const VM::enum_flags flag, const char* message) {
276281
}
277282

278283
bool parse_disable_token(const char* token) {
279-
std::string tok(token);
284+
const std::string tok(token);
280285
std::vector<std::string> names;
281286
std::string current;
282287

283-
for (char c : tok) {
288+
for (const char c : tok) {
284289
if (c == ',') {
285290
if (!current.empty()) {
286291
names.push_back(current);
@@ -296,7 +301,7 @@ bool parse_disable_token(const char* token) {
296301
}
297302

298303
for (const auto& name : names) {
299-
std::pair<bool, VM::enum_flags> technique = string_to_technique(name);
304+
const std::pair<bool, VM::enum_flags> technique = string_to_technique(name);
300305

301306
const bool found = technique.first;
302307
const VM::enum_flags flag = technique.second;
@@ -357,7 +362,7 @@ void generate_json(const char* output) {
357362

358363
const auto detected_status = VM::detected_enums();
359364

360-
if (detected_status.size() == 0) {
365+
if (detected_status.empty()) {
361366
json.emplace_back("]\n}");
362367
} else {
363368
for (size_t i = 0; i < detected_status.size(); i++) {
@@ -448,7 +453,7 @@ void general(bool high_threshold, bool all, bool dynamic, const char* output_fil
448453
const VM::enum_flags dynamic_arg = dynamic ? VM::DYNAMIC : VM::NULL_ARG;
449454

450455
#if (CLI_LINUX)
451-
[[maybe_unused]] bool notes_enabled = !arg_bitset.test(NOTES);
456+
[[maybe_unused]] const bool notes_enabled = !arg_bitset.test(NOTES);
452457
#endif
453458

454459
std::ofstream output_fstream;
@@ -482,11 +487,11 @@ void general(bool high_threshold, bool all, bool dynamic, const char* output_fil
482487
}
483488

484489
#if (CLI_WINDOWS)
485-
DebugInterceptor* interceptor = nullptr;
486-
if (!arg_bitset.test(NO_ANSI)) {
490+
std::unique_ptr<DebugInterceptor> interceptor;
491+
if (!arg_bitset.test(NO_ANSI) && !arg_bitset.test(SIMPLE)) {
487492
g_tui.init();
488-
interceptor = new DebugInterceptor(std::cout.rdbuf());
489-
std::cout.rdbuf(interceptor);
493+
interceptor = std::make_unique<DebugInterceptor>(std::cout.rdbuf());
494+
std::cout.rdbuf(interceptor.get());
490495
}
491496
#endif
492497

@@ -598,15 +603,15 @@ void general(bool high_threshold, bool all, bool dynamic, const char* output_fil
598603
checker(VM::TIMER, "timing anomalies");
599604

600605
const auto t2 = std::chrono::high_resolution_clock::now();
601-
VM::vmaware vm(VM::MULTIPLE, high_thresh_arg, all_arg, dynamic_arg);
606+
const VM::vmaware vm(VM::MULTIPLE, high_thresh_arg, all_arg, dynamic_arg);
602607
std::vector<std::string> summary;
603608

604-
std::string brand = vm.brand;
609+
const std::string brand = vm.brand;
605610
const bool is_red = ((brand == VM::brands::NULL_BRAND) || (brand == VM::brands::HYPERV_ROOT));
606611
summary.push_back(bold + "\nVM brand: " + ansi_exit + (is_red ? red : green) + brand + ansi_exit);
607612

608613
if (!is_vm_brand_multiple(vm.brand)) {
609-
std::string current_color = (vm.type == "Unknown" || vm.type == "Host machine") ? red : green;
614+
const std::string current_color = (vm.type == "Unknown" || vm.type == "Host machine") ? red : green;
610615
summary.push_back(bold + "VM type: " + ansi_exit + current_color + vm.type + ansi_exit);
611616
}
612617

@@ -696,8 +701,8 @@ void general(bool high_threshold, bool all, bool dynamic, const char* output_fil
696701
}
697702

698703
const std::string is_bold = (vm.is_vm ? bold : "");
699-
700704
const char* conclusion_color = color(vm.percentage, vm.is_hardened);
705+
701706
summary.push_back(
702707
bold +
703708
"===== CONCLUSION: " +
@@ -712,68 +717,67 @@ void general(bool high_threshold, bool all, bool dynamic, const char* output_fil
712717
);
713718

714719
#if (CLI_WINDOWS)
715-
if (!arg_bitset.test(NO_ANSI)) {
720+
if (!arg_bitset.test(NO_ANSI) && !arg_bitset.test(SIMPLE)) {
716721
g_tui.drawSummaryBox(summary);
717-
} else {
718-
for (const auto& l : summary) {
719-
std::cout << l << "\n";
722+
723+
g_tui.finalize();
724+
725+
if (g_tui.raw_out) {
726+
*(g_tui.raw_out) << "\x1B[90mPress Enter, Q, or Ctrl+C to exit. Exceptions (Left/Right), Timings (Up/Down), Debug (PgUp/PgDn) to scroll.\x1B[0m\n";
727+
} else {
728+
std::cout << "\x1B[90mPress Enter, Q, or Ctrl+C to exit. Exceptions (Left/Right), Timings (Up/Down), Debug (PgUp/PgDn) to scroll.\x1B[0m\n";
720729
}
721-
}
722730

723-
g_tui.finalize();
731+
constexpr u8 KEY_ESCAPE_PREFIX = 0;
732+
constexpr u8 KEY_EXTENDED = 224;
733+
constexpr u8 KEY_UP = 72;
734+
constexpr u8 KEY_DOWN = 80;
735+
constexpr u8 KEY_PAGE_UP = 73;
736+
constexpr u8 KEY_PAGE_DOWN = 81;
737+
constexpr u8 KEY_LEFT = 75;
738+
constexpr u8 KEY_RIGHT = 77;
739+
constexpr u8 KEY_CTRL_C = 3;
740+
741+
while (true) {
742+
int ch = _getch();
743+
744+
if (ch == KEY_ESCAPE_PREFIX || ch == KEY_EXTENDED) {
745+
ch = _getch();
746+
747+
switch (ch) {
748+
case KEY_UP: g_tui.scrollCyclesUp(); continue;
749+
case KEY_DOWN: g_tui.scrollCyclesDown(); continue;
750+
case KEY_PAGE_UP: g_tui.scrollDebugUp(); continue;
751+
case KEY_PAGE_DOWN: g_tui.scrollDebugDown(); continue;
752+
case KEY_LEFT: g_tui.scrollExceptionsUp(); continue;
753+
case KEY_RIGHT: g_tui.scrollExceptionsDown(); continue;
754+
default: continue;
755+
}
756+
}
757+
758+
bool should_break = false;
724759

725-
if (g_tui.raw_out) {
726-
*(g_tui.raw_out) << "\x1B[90mPress Enter, Q, or Ctrl+C to exit. Exceptions (Left/Right), Timings (Up/Down), Debug (PgUp/PgDn) to scroll.\x1B[0m\n";
727-
} else {
728-
std::cout << "\x1B[90mPress Enter, Q, or Ctrl+C to exit. Exceptions (Left/Right), Timings (Up/Down), Debug (PgUp/PgDn) to scroll.\x1B[0m\n";
729-
}
730-
731-
constexpr u8 KEY_ESCAPE_PREFIX = 0;
732-
constexpr u8 KEY_EXTENDED = 224;
733-
constexpr u8 KEY_UP = 72;
734-
constexpr u8 KEY_DOWN = 80;
735-
constexpr u8 KEY_PAGE_UP = 73;
736-
constexpr u8 KEY_PAGE_DOWN = 81;
737-
constexpr u8 KEY_LEFT = 75;
738-
constexpr u8 KEY_RIGHT = 77;
739-
constexpr u8 KEY_CTRL_C = 3;
740-
741-
while (true) {
742-
int ch = _getch();
743-
744-
if (ch == KEY_ESCAPE_PREFIX || ch == KEY_EXTENDED) {
745-
ch = _getch();
746-
747760
switch (ch) {
748-
case KEY_UP: g_tui.scrollCyclesUp(); continue;
749-
case KEY_DOWN: g_tui.scrollCyclesDown(); continue;
750-
case KEY_PAGE_UP: g_tui.scrollDebugUp(); continue;
751-
case KEY_PAGE_DOWN: g_tui.scrollDebugDown(); continue;
752-
case KEY_LEFT: g_tui.scrollExceptionsUp(); continue;
753-
case KEY_RIGHT: g_tui.scrollExceptionsDown(); continue;
754-
default: continue;
761+
case '\r':
762+
case '\n':
763+
case 'q':
764+
case 'Q':
765+
case KEY_CTRL_C:
766+
should_break = true;
755767
}
756-
}
757768

758-
bool should_break = false;
759-
760-
switch (ch) {
761-
case '\r':
762-
case '\n':
763-
case 'q':
764-
case 'Q':
765-
case KEY_CTRL_C:
766-
should_break = true;
769+
if (should_break) {
770+
break;
771+
}
767772
}
768773

769-
if (should_break) {
770-
break;
774+
if (interceptor) {
775+
std::cout.rdbuf(interceptor->original);
776+
}
777+
} else {
778+
for (const auto& l : summary) {
779+
std::cout << l << "\n";
771780
}
772-
}
773-
774-
if (interceptor) {
775-
std::cout.rdbuf(interceptor->original);
776-
delete interceptor;
777781
}
778782
#else
779783
for (const auto& line : summary) {

0 commit comments

Comments
 (0)