Skip to content

Commit

Permalink
includes/std: Drastically improved std::mem::MagicSearch, added fin…
Browse files Browse the repository at this point in the history
…d_string and find_string_in_range
  • Loading branch information
WerWolv committed Jul 4, 2024
1 parent f797c71 commit 18f968d
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions includes/std/mem.pat
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ namespace auto std::mem {
namespace impl {

struct MagicSearchImpl<auto Magic, T> {
if ($ < (std::mem::base_address() + std::mem::size() - builtin::std::string::length(Magic) - 1)) {
char __potentialMagic__[builtin::std::string::length(Magic)] [[hidden, no_unique_address]];

if (__potentialMagic__ == Magic) {
T data [[inline]];
} else {
padding[1];
continue;
}
} else {
padding[1];
continue;
s128 address = builtin::std::mem::find_string_in_range(0, $, builtin::std::mem::size(), Magic);
if (address < 0)
break;

$ = address;
try {
T data [[inline]];
} catch {
T data;
}
};

Expand Down Expand Up @@ -63,33 +60,34 @@ namespace auto std::mem {


/**
Gets the base address of the memory
Gets the base address of the data
@return The base address of the memory
*/
fn base_address() {
return builtin::std::mem::base_address();
};

/**
Gets the size of the memory
Gets the size of the data
@return The size of the memory
*/
fn size() {
return builtin::std::mem::size();
};

/**
Finds a sequence of bytes in the memory
Finds a sequence of bytes in the data
@param occurrence_index The index of the occurrence to find
@param bytes The bytes to find
@return The address of the sequence
*/
fn find_sequence(u128 occurrence_index, auto ... bytes) {
return builtin::std::mem::find_sequence_in_range(occurrence_index, builtin::std::mem::base_address(), builtin::std::mem::size(), bytes);
const u128 address = builtin::std::mem::base_address();
return builtin::std::mem::find_sequence_in_range(occurrence_index, address, address + builtin::std::mem::size(), bytes);
};

/**
Finds a sequence of bytes in a specific region of the memory
Finds a sequence of bytes in a specific region of the data
@param occurrence_index The index of the occurrence to find
@param offsetFrom The offset from which to start searching
@param offsetTo The offset to which to search
Expand All @@ -100,6 +98,30 @@ namespace auto std::mem {
return builtin::std::mem::find_sequence_in_range(occurrence_index, offsetFrom, offsetTo, bytes);
};


/**
Finds a string in the data
@param occurrence_index The index of the occurrence to find
@param string The string to find
@return The address of the sequence
*/
fn find_string(u128 occurrence_index, str string) {
const u128 address = builtin::std::mem::base_address();
return builtin::std::mem::find_string_in_range(occurrence_index, address, address + builtin::std::mem::size(), string);
};

/**
Finds a string in a specific region of the data
@param occurrence_index The index of the occurrence to find
@param offsetFrom The offset from which to start searching
@param offsetTo The offset to which to search
@param string The string to find
@return The address of the sequence
*/
fn find_string_in_range(u128 occurrence_index, u128 offsetFrom, u128 offsetTo, str string) {
return builtin::std::mem::find_string_in_range(occurrence_index, offsetFrom, offsetTo, string);
};

/**
Reads a unsigned value from the memory
@param address The address to read from
Expand Down

0 comments on commit 18f968d

Please sign in to comment.