> For the complete documentation index, see [llms.txt](https://easyauth.papelship.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easyauth.papelship.com/documentation/core-functions/memory-protection.md).

# Memory Protection

EasyAuth includes an innovative **Memory Remapping and In-Memory Code Erasure Engine**. Critical routines registered with EasyAuth are moved to dynamic encrypted heaps, and their original opcodes in the `.text` section are completely wiped to defeat disassemblers and static memory dumpers.

***

## Registering Protected Functions

### `easyauth::register_protected_function`

Registers a critical C++ function to be protected by the memory remapping and wiping engine.

```cpp
void register_protected_function(void* fn_ptr, size_t size = 0);
```

#### Parameters

* `fn_ptr`: Pointer to the function to protect.
* `size` *(Optional)*: Function size in bytes. If set to `0`, EasyAuth automatically calculates the length using its embedded instruction disassembler (`ldasm`).

#### How It Works

{% stepper %}
{% step %}

## Register the function

When `register_protected_function(&MyFunction)` is called, EasyAuth measures the function's bytecode.
{% endstep %}

{% step %}

## Initialize EasyAuth

During `easyauth::initialize()`, the bytecode is cloned to an encrypted dynamic memory page.
{% endstep %}

{% step %}

## Wipe the original body

The original function body in the `.text` segment is wiped (`0xCC` / `0x00` or redirected to a stealth trap).
{% endstep %}

{% step %}

## Execute through the remapped stub

Calls to `MyFunction` automatically transition through the remapped execution stub.
{% endstep %}
{% endstepper %}

***

## Code Example

```cpp
#include "qPapelEasyAuth.h"
#include <iostream>

// Use __declspec(noinline) so the compiler doesn't optimize out the function body
__declspec(noinline) static int SensitiveCalculation(int x, int y) {
    return ((x * y) ^ (x + y)) + 0x1337;
}

int main() {
    // 1. Register function BEFORE initialize()
    void* pFn = reinterpret_cast<void*>(&SensitiveCalculation);
    easyauth::register_protected_function(pFn);

    // 2. Initialize EasyAuth engine
    easyauth::initialize();

    // 3. Verify protection status
    if (easyauth::is_wiped(pFn)) {
        std::cout << "[+] Function opcodes successfully erased from .text section!\n";
    }

    // 4. Execute the function normally
    int result = SensitiveCalculation(10, 20);
    std::cout << "[+] Calculation Result: " << result << "\n";

    return 0;
}
```

***

## Introspection Functions

### `easyauth::is_remapped`

Checks if the global remapping engine is active and operational.

```cpp
bool is_remapped();
```

### `easyauth::is_wiped`

Checks if a specific function's original memory segment has been wiped.

```cpp
bool is_wiped(void* original_fn);
```

### `easyauth::get_remapped`

Returns the dynamic execution address where the remapped function currently resides.

```cpp
void* get_remapped(void* original_fn);
```
