Skip to content

Commit e617d91

Browse files
author
Requiem
committed
feat: support for custom scores in techniques
1 parent 4be15a6 commit e617d91

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

src/vmaware.hpp

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11390,11 +11390,26 @@ struct VM {
1139011390

1139111391
// Temporary storage to capture which brand was detected by the currently running technique
1139211392
static const char* last_detected_brand;
11393+
static u8 last_detected_score;
1139311394

11394-
// directly return when adding a brand to the scoreboard for a more succint expression
11395-
static inline bool add(const char* p_brand, const char* extra_brand = "") noexcept {
11396-
// capture the brand being added so we can cache it later
11395+
// 1. one brand, custom score
11396+
static inline bool add(const char* p_brand, u8 score) noexcept {
11397+
return add_score(p_brand, "", score);
11398+
}
11399+
11400+
// 2. one brand, default score
11401+
static inline bool add(const char* p_brand) noexcept {
11402+
return add_score(p_brand, "", 0);
11403+
}
11404+
11405+
// 3. two brands, default score
11406+
static inline bool add(const char* p_brand, const char* extra_brand) noexcept {
11407+
return add_score(p_brand, extra_brand, 0);
11408+
}
11409+
11410+
static inline bool add_score(const char* p_brand, const char* extra_brand, u8 score) noexcept {
1139711411
last_detected_brand = p_brand;
11412+
last_detected_score = score; // Store for the engine to read
1139811413

1139911414
for (size_t i = 0; i < brand_count; ++i) {
1140011415
// pointer comparison is sufficient as we use the static constants from brands:: namespace
@@ -11404,7 +11419,7 @@ struct VM {
1140411419
}
1140511420
}
1140611421

11407-
if (extra_brand[0] != '\0') {
11422+
if (extra_brand && extra_brand[0] != '\0') {
1140811423
for (size_t i = 0; i < brand_count; ++i) {
1140911424
if (brand_scoreboard[i].name == extra_brand) {
1141011425
brand_scoreboard[i].score++;
@@ -11483,23 +11498,28 @@ struct VM {
1148311498

1148411499
// reset the last detected brand before running
1148511500
last_detected_brand = nullptr;
11501+
last_detected_score = 0;
1148611502

1148711503
// run the technique
1148811504
const bool result = technique_data.run();
1148911505

11490-
// retrieve the brand that was set during execution (if any)
11491-
const char* detected_brand = (result && last_detected_brand) ? last_detected_brand : brands::NULL_BRAND;
11492-
1149311506
if (result) {
11494-
points += technique_data.points;
11507+
// determine which points to use: Override or Default
11508+
const u8 points_to_add = (last_detected_score > 0) ? last_detected_score : technique_data.points;
1149511509

11510+
points += points_to_add;
1149611511
// this is specific to VM::detected_count() which
1149711512
// returns the number of techniques that found a VM.
1149811513
detected_count_num++;
11499-
}
1150011514

11501-
// store the current technique result to the cache
11502-
memo::cache_store(technique_macro, result, technique_data.points, detected_brand);
11515+
// retrieve the brand that was set during execution (if any)
11516+
const char* detected_brand = (last_detected_brand) ? last_detected_brand : brands::NULL_BRAND;
11517+
// store the current technique result to the cache
11518+
memo::cache_store(technique_macro, result, points_to_add, detected_brand);
11519+
}
11520+
else {
11521+
memo::cache_store(technique_macro, false, 0, brands::NULL_BRAND);
11522+
}
1150311523

1150411524
// for things like VM::detect() and VM::percentage(),
1150511525
// a score of 150+ is guaranteed to be a VM, so
@@ -11761,17 +11781,20 @@ struct VM {
1176111781

1176211782
if (auto run_fn = pair.run) {
1176311783
core::last_detected_brand = nullptr;
11784+
core::last_detected_score = 0;
1176411785

1176511786
bool result = run_fn();
11766-
if (result) detected_count_num++;
11767-
#ifdef __VMAWARE_DEBUG__
11768-
total_points += pair.points;
11769-
#endif
11770-
// determine the brand string associated with this result
11771-
const char* detected_brand = (result && core::last_detected_brand) ? core::last_detected_brand : brands::NULL_BRAND;
11787+
if (result) {
11788+
#ifdef __VMAWARE_DEBUG__
11789+
total_points += pair.points;
11790+
#endif
11791+
detected_count_num++;
11792+
u8 points_to_add = (core::last_detected_score > 0) ? core::last_detected_score : pair.points;
11793+
const char* detected_brand = (core::last_detected_brand) ? core::last_detected_brand : brands::NULL_BRAND;
1177211794

11773-
memo::cache_store(flag_bit, result, pair.points, detected_brand);
11774-
return result;
11795+
memo::cache_store(flag_bit, result, points_to_add, detected_brand);
11796+
return result;
11797+
}
1177511798
}
1177611799
else {
1177711800
throw_error("Flag is not known or not implemented");
@@ -12829,6 +12852,7 @@ std::array<VM::memo::leaf_entry, VM::memo::leaf_cache::CAPACITY> VM::memo::leaf_
1282912852
std::size_t VM::memo::leaf_cache::count = 0;
1283012853
std::size_t VM::memo::leaf_cache::next_index = 0;
1283112854
const char* VM::core::last_detected_brand = nullptr;
12855+
VM::u8 VM::core::last_detected_score = 0;
1283212856

1283312857
#ifdef __VMAWARE_DEBUG__
1283412858
VM::u16 VM::total_points = 0;

0 commit comments

Comments
 (0)