#ifndef PPL_BYPASS_H
#define PPL_BYPASS_H

#include "../Common/ExploitCore.h"

#ifdef __cplusplus
extern "C" {
#endif

// ============================================================================
// PPL Bypass via momentarius (bundled in Dopamine)
// ============================================================================

// momentarius supports: A12/A13, iOS 15.0 - 26.0.1
// It disables PPL (Process Policy Layer) by patching ppl_control / ppl_ops

typedef enum {
    PPL_METHOD_MOMENTARIUS = 0,
    PPL_METHOD_TITAN = 1,      // A14-A17
    PPL_METHOD_DMAFAIL = 2,    // 15.0-16.5.1
} ppl_method_t;

typedef struct {
    ppl_method_t method;
    uint64_t kernel_base;
    uint64_t ppl_control_addr;
    uint64_t ppl_ops_addr;
    uint64_t cs_enforcement_addr;
    uint64_t amfi_addr;
    bool ppl_disabled;
    bool amfi_disabled;
    bool trustcache_injected;
} ppl_bypass_ctx_t;

// Initialize PPL bypass context
exploit_error_t ppl_bypass_init(ppl_bypass_ctx_t *ctx, uint64_t kernel_base, ppl_method_t method);

// Disable PPL via momentarius technique
exploit_error_t ppl_bypass_disable_ppl(ppl_bypass_ctx_t *ctx, kernel_rw_t *krw);

// Disable AMFI (Apple Mobile File Integrity)
exploit_error_t ppl_bypass_disable_amfi(ppl_bypass_ctx_t *ctx, kernel_rw_t *krw);

// Inject custom trustcache for unsigned code execution
exploit_error_t ppl_bypass_inject_trustcache(ppl_bypass_ctx_t *ctx, kernel_rw_t *krw, const void *trustcache, size_t size);

// Spawn root shell / execute payload
exploit_error_t ppl_bypass_spawn_payload(ppl_bypass_ctx_t *ctx, kernel_rw_t *krw, const char *payload_path);

// Cleanup
void ppl_bypass_cleanup(ppl_bypass_ctx_t *ctx);

// ============================================================================
// momentarius Offsets (iOS 18.7.8 A12)
// ============================================================================

// These are unslid offsets from kernel base
// momentarius patches: ppl_control, ppl_ops, cs_enforcement

#define MOMENTARIUS_PPL_CONTROL_OFFSET    0x0  // TODO: find in 18.7.8
#define MOMENTARIUS_PPL_OPS_OFFSET        0x0
#define MOMENTARIUS_CS_ENFORCEMENT_OFFSET 0x0
#define MOMENTARIUS_AMFI_OFFSET           0x0

// PPL structures (from xnu source)
typedef struct {
    uint32_t ppl_version;
    uint32_t ppl_flags;
    uint64_t ppl_ops;
    uint64_t ppl_arg;
} ppl_control_t;

typedef struct {
    int (*ppl_check_process)(void *proc, int action);
    int (*ppl_check_file)(void *vnode, int action);
    int (*ppl_check_memory)(void *map, int action);
    // ... more function pointers
} ppl_ops_t;

#ifdef __cplusplus
}
#endif

#endif // PPL_BYPASS_H