SIGSEGV recovery? and drand48 for RNG

master
JoYo 2016-01-13 18:32:51 -05:00
parent 4a6c57b113
commit cd83b5b613
1 changed files with 28 additions and 4 deletions

View File

@ -10,11 +10,15 @@
#include <sys/stat.h>
#include <unistd.h>
#include <openssl/sha.h>
#include <signal.h>
#include <setjmp.h>
#pragma comment(lib, "openssl/sha.lib")
#define SHA_SUM_LENGTH (SHA_DIGEST_LENGTH + SHA_DIGEST_LENGTH + 1)
jmp_buf jmpBuf;
void picProto(void *picAddr, size_t picSize, void *clonePtr, char *checksum) {
void (*cloneFunc)(void *, size_t, char *) = clonePtr;
cloneFunc(picAddr, picSize, checksum);
@ -24,10 +28,13 @@ void picProto(void *picAddr, size_t picSize, void *clonePtr, char *checksum) {
void clone(void *picAddr, size_t picSize, char *checksum) {
auto retVal = EX_SOFTWARE;
srand(time(NULL));
struct drand48_data drand_data;
srand48_r(time(NULL), &drand_data);
long int randVal;
lrand48_r(&drand_data, &randVal);
unsigned int picOffset = (rand() % (picSize + 1));
unsigned char picFlip = ((char *)picAddr)[picOffset] & (rand() % 2);
unsigned int picOffset = (randVal % (picSize + 1));
unsigned char picFlip = ((char *)picAddr)[picOffset] & (randVal % 2);
printf("%x\t%x\n", picOffset, picFlip);
@ -60,6 +67,16 @@ CLONE_CLEANUP:
return;
}
void magic_handler(int sig) {
switch (sig) {
case SIGSEGV:
longjmp(jmpBuf, 1);
break;
}
}
int main(int argc, const char **argv) {
auto retVal = EX_SOFTWARE;
char fileInPath[SHA_SUM_LENGTH];
@ -105,7 +122,14 @@ int main(int argc, const char **argv) {
void (*cloneFunc)(void *, size_t, char *) = clone;
void (*picFunc)(void *, size_t, void *, char *) = picBuffer;
signal(SIGSEGV, magic_handler);
if (!setjmp(jmpBuf)) {
picFunc(picBuffer, picStat.st_size, cloneFunc, checksum);
} else {
retVal = errno;
goto MAIN_CLEANUP;
}
strncpy(fileInPath, checksum, SHA_SUM_LENGTH);