129 lines
3.0 KiB
C++
129 lines
3.0 KiB
C++
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <malloc.h>
|
||
|
#include <time.h>
|
||
|
#include <string.h>
|
||
|
#include <errno.h>
|
||
|
#include <sysexits.h>
|
||
|
#include <sys/mman.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <unistd.h>
|
||
|
#include <openssl/sha.h>
|
||
|
#include <signal.h>
|
||
|
|
||
|
#pragma comment(lib, "openssl/sha.lib")
|
||
|
|
||
|
#define SHA_SUM_LENGTH (SHA_DIGEST_LENGTH + SHA_DIGEST_LENGTH + 1)
|
||
|
|
||
|
void picProto(void *picAddr, size_t picSize, void *clonePtr) {
|
||
|
void (*cloneFunc)(void *, size_t, char *) = clonePtr;
|
||
|
cloneFunc(picAddr, picSize);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void clone(void *picAddr, size_t picSize, char *checksum) {
|
||
|
auto retVal = EX_SOFTWARE;
|
||
|
|
||
|
struct drand48_data drand_data;
|
||
|
srand48_r(time(NULL), &drand_data);
|
||
|
long int randVal;
|
||
|
lrand48_r(&drand_data, &randVal);
|
||
|
|
||
|
unsigned int picOffset = (randVal % (picSize + 1));
|
||
|
unsigned char picFlip = ((char *)picAddr)[picOffset] & (randVal % 2);
|
||
|
|
||
|
printf("%x\t%x\n", picOffset, picFlip);
|
||
|
|
||
|
((char *)picAddr)[picOffset] = picFlip;
|
||
|
|
||
|
unsigned char digest[SHA_DIGEST_LENGTH];
|
||
|
SHA1(picAddr, picSize, digest);
|
||
|
|
||
|
for (int iter = 0; iter < SHA_DIGEST_LENGTH; iter++) {
|
||
|
sprintf(&checksum[iter * 2], "%02x", digest[iter]);
|
||
|
}
|
||
|
|
||
|
FILE *fileOutHandle = fopen(checksum, "w+");
|
||
|
if (NULL == fileOutHandle) {
|
||
|
retVal = errno;
|
||
|
goto CLONE_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = fwrite(picAddr, 1, picSize, fileOutHandle);
|
||
|
if (retVal != picSize) {
|
||
|
retVal = errno;
|
||
|
goto CLONE_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = EX_OK;
|
||
|
CLONE_CLEANUP:
|
||
|
if (fileOutHandle) {
|
||
|
fclose(fileOutHandle);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
int main(int argc, const char **argv) {
|
||
|
auto retVal = EX_SOFTWARE;
|
||
|
char fileInPath[SHA_SUM_LENGTH];
|
||
|
char checksum[SHA_SUM_LENGTH];
|
||
|
|
||
|
strncpy(fileInPath, argv[1], SHA_SUM_LENGTH);
|
||
|
|
||
|
while (1) {
|
||
|
|
||
|
FILE *fileInHandle = fopen(fileInPath, "rb");
|
||
|
if (NULL == fileInHandle) {
|
||
|
retVal = errno;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
struct stat picStat;
|
||
|
retVal = fstat(fileno(fileInHandle), &picStat);
|
||
|
if (-1 == retVal) {
|
||
|
retVal = errno;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
void *picBuffer = mmap(NULL, picStat.st_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
|
||
|
if (MAP_FAILED == picBuffer) {
|
||
|
retVal = errno;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = fread(picBuffer, 1, picStat.st_size, fileInHandle);
|
||
|
if (retVal != picStat.st_size) {
|
||
|
retVal = errno;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
if (NULL != fileInHandle) {
|
||
|
fclose(fileInHandle);
|
||
|
fileInHandle = NULL;
|
||
|
}
|
||
|
|
||
|
memset(checksum, 0, SHA_SUM_LENGTH);
|
||
|
void (*cloneFunc)(void *, size_t, char *) = clone;
|
||
|
void (*picFunc)(void *, size_t, void *, char *) = picBuffer;
|
||
|
|
||
|
signal(SIGSEGV, magic_handler);
|
||
|
|
||
|
picFunc(picBuffer, picStat.st_size, cloneFunc, checksum);
|
||
|
|
||
|
strncpy(fileInPath, checksum, SHA_SUM_LENGTH);
|
||
|
|
||
|
retVal = EX_OK;
|
||
|
MAIN_CLEANUP:
|
||
|
if (NULL != picBuffer) {
|
||
|
munmap(picBuffer, picStat.st_size);
|
||
|
}
|
||
|
if (NULL != fileInHandle) {
|
||
|
fclose(fileInHandle);
|
||
|
fileInHandle = NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return retVal;
|
||
|
}
|