2016-01-08 19:47:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-01-13 01:05:45 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <time.h>
|
2016-01-08 19:47:59 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sysexits.h>
|
2016-01-13 01:05:45 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2016-01-08 19:47:59 +00:00
|
|
|
#include <openssl/sha.h>
|
2016-01-13 01:05:45 +00:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/buffer.h>
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-08 23:31:31 +00:00
|
|
|
#pragma comment(lib, "openssl/sha.lib")
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
void picProto(void *picAddr, size_t picSize, void *clonePtr, char *checksum) {
|
|
|
|
void (*cloneFunc)(void *, size_t, char *) = clonePtr;
|
|
|
|
cloneFunc(picAddr, picSize, checksum);
|
|
|
|
return;
|
2016-01-08 19:47:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
void clone(void *picAddr, size_t picSize, char *checksum) {
|
2016-01-08 19:47:59 +00:00
|
|
|
auto retVal = EX_SOFTWARE;
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
srand(time(NULL));
|
2016-01-08 19:47:59 +00:00
|
|
|
|
|
|
|
unsigned int picOffset = (rand() % (picSize + 1));
|
2016-01-13 01:05:45 +00:00
|
|
|
unsigned char picFlip = ((char *)picAddr)[picOffset] & (rand() % 1);
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
((char *)picAddr)[picOffset] = picFlip;
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
unsigned char digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1(picAddr, picSize, digest);
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
for (int iter = 0; iter < SHA_DIGEST_LENGTH; iter++) {
|
|
|
|
sprintf(&checksum[iter * 2], "%02x", digest[iter]);
|
|
|
|
}
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
FILE *fileOutHandle = fopen(checksum, "w+");
|
2016-01-08 19:47:59 +00:00
|
|
|
if (NULL == fileOutHandle) {
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto CLONE_CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = fwrite(picAddr, 1, picSize, fileOutHandle);
|
|
|
|
if (retVal != picSize) {
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto CLONE_CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = EX_OK;
|
|
|
|
CLONE_CLEANUP:
|
|
|
|
if (fileOutHandle) {
|
|
|
|
fclose(fileOutHandle);
|
|
|
|
}
|
2016-01-13 01:05:45 +00:00
|
|
|
return;
|
2016-01-08 19:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
auto retVal = EX_SOFTWARE;
|
|
|
|
char *fileInPath = argv[1];
|
|
|
|
|
|
|
|
FILE *fileInHandle = fopen(fileInPath, "rb");
|
|
|
|
if (NULL == fileInHandle) {
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto MAIN_CLEANUP;
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
struct stat picStat;
|
|
|
|
fstat(fileno(fileInHandle), &picStat);
|
|
|
|
if (-1 == picStat.st_size) {
|
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto MAIN_CLEANUP;
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
void *picBuffer = memalign(getpagesize(), picStat.st_size);
|
2016-01-08 19:47:59 +00:00
|
|
|
if (NULL == picBuffer) {
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto MAIN_CLEANUP;
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal =
|
|
|
|
mprotect(picBuffer, picStat.st_size, PROT_READ | PROT_WRITE | PROT_EXEC);
|
|
|
|
if (0 != retVal) {
|
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto MAIN_CLEANUP;
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
retVal = fread(picBuffer, 1, picStat.st_size, fileInHandle);
|
|
|
|
if (retVal != picStat.st_size) {
|
|
|
|
retVal = errno;
|
2016-01-08 19:47:59 +00:00
|
|
|
goto MAIN_CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileInHandle) {
|
|
|
|
fclose(fileInHandle);
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
char checksum[(SHA_DIGEST_LENGTH * 2) + 1];
|
|
|
|
void (*cloneFunc)(void *, size_t, char *) = clone;
|
|
|
|
void (*picFunc)(void *, size_t, void *, char *) = picBuffer;
|
2016-01-08 19:47:59 +00:00
|
|
|
|
2016-01-13 01:05:45 +00:00
|
|
|
picFunc(picBuffer, picStat.st_size, cloneFunc, &checksum);
|
2016-01-08 19:47:59 +00:00
|
|
|
|
|
|
|
retVal = EX_OK;
|
|
|
|
MAIN_CLEANUP:
|
|
|
|
if (fileInHandle) {
|
|
|
|
fclose(fileInHandle);
|
|
|
|
}
|
|
|
|
if (picBuffer) {
|
|
|
|
free(picBuffer);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|