Skip to content

Instantly share code, notes, and snippets.

@synap5e
Created February 10, 2014 12:10
Show Gist options
  • Save synap5e/8914789 to your computer and use it in GitHub Desktop.
Save synap5e/8914789 to your computer and use it in GitHub Desktop.
A quick hack that allows using LD_PRELOAD for a speedhack
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
typedef int (*go)(struct timeval *tv, struct timezone *tz);
static int t = 0;
static int M = 100;
static int started = 0;
void* input_thread(void*){
while (getchar()){
if (t){
printf("Speedhack disabled");
t=0;
} else {
printf("Speedhack enabled");
t=1;
}
}
return NULL;
}
static long last_usec;
static long skipped_usec=0;
extern "C" int gettimeofday(struct timeval *tv, struct timezone *tz)
{
// Testing purposes:
go gettimeofday_orig;
int val;
gettimeofday_orig=(go)dlsym(RTLD_NEXT,"gettimeofday");
if (!started)
{
pthread_t t;
pthread_create(&t, NULL, input_thread, NULL);
started=1;
}
val = gettimeofday_orig(tv,tz);
long usec = tv->tv_sec*1000000 + tv->tv_usec;
long delta_usec = usec - last_usec;
last_usec = usec;
if (t){
skipped_usec+= delta_usec*M;
}
tv->tv_usec += skipped_usec;
tv->tv_sec += skipped_usec / 1000000;
tv->tv_usec += skipped_usec % 1000000;
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment