Created
October 10, 2021 20:06
-
-
Save flarn2006/ddf42113340102e500f42136fbc56432 to your computer and use it in GitHub Desktop.
Unrandomizer for LD_PRELOAD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by flarn2006. WTFPL | |
// Compile with gcc, -fPIC -shared | |
#include <stdlib.h> | |
static int n = 0; | |
static int m = 0; | |
static int count() | |
{ | |
static int chain; | |
static int chain_set = 0; | |
static int max; | |
static int max_set = 0; | |
if (!chain_set) { | |
const char* var = getenv("UNRAND_CHAIN"); | |
m = chain = var ? atoi(var) : 1; | |
chain_set = 1; | |
} | |
if (!max_set) { | |
const char* var = getenv("UNRAND_MAX"); | |
max = var ? atoi(var) : RAND_MAX; | |
} | |
if (m == 0) { | |
m = chain; | |
++n; | |
} | |
--m; | |
if (max == RAND_MAX) | |
return n; | |
else | |
return n % (max+1); | |
} | |
int rand_(void) __asm__("rand"); | |
int rand_() | |
{ | |
return count(); | |
} | |
int rand_r_(unsigned int*) __asm__("rand_r"); | |
int rand_r_(unsigned int* seedp) | |
{ | |
return count(); | |
} | |
int g_rand_int_range_(void*, int, int) __asm__("g_rand_int_range"); | |
int g_rand_int_range_(void *rand_, int begin, int end) | |
{ | |
int range = end - begin; | |
return begin + count() % range; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment