some progress in c before I move to cpp
							parent
							
								
									50533d793e
								
							
						
					
					
						commit
						02a75dcd79
					
				| 
						 | 
					@ -61,5 +61,3 @@ CommentPragmas:  '^ IWYU pragma:'
 | 
				
			||||||
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
 | 
					ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
 | 
				
			||||||
SpaceBeforeParens: ControlStatements
 | 
					SpaceBeforeParens: ControlStatements
 | 
				
			||||||
DisableFormat:   false
 | 
					DisableFormat:   false
 | 
				
			||||||
...
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,147 @@
 | 
				
			||||||
 | 
					#include <errno.h>
 | 
				
			||||||
 | 
					#include <malloc.h>
 | 
				
			||||||
 | 
					#include <openssl/sha.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <sys/mman.h>
 | 
				
			||||||
 | 
					#include <sys/stat.h>
 | 
				
			||||||
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma comment(lib, "openssl/sha.lib")
 | 
				
			||||||
 | 
					#define SHA_SUM_LENGTH (SHA_DIGEST_LENGTH + SHA_DIGEST_LENGTH + 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int generation(char* seed_path);
 | 
				
			||||||
 | 
					int reproduce(void *pic_address, size_t pic_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int generation(char* seed_path)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int return_value = 0;
 | 
				
			||||||
 | 
					    FILE *seed_handle = NULL;
 | 
				
			||||||
 | 
					    struct stat pic_statistics;
 | 
				
			||||||
 | 
					    void *pic_buffer = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    seed_handle = fopen(seed_path, "rb");
 | 
				
			||||||
 | 
					    if (NULL == seed_handle)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto GEN_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return_value = fstat(fileno(seed_handle), &pic_statistics);
 | 
				
			||||||
 | 
					    if (-1 == return_value)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto GEN_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
 | 
				
			||||||
 | 
					    int flags = (MAP_ANON | MAP_PRIVATE);
 | 
				
			||||||
 | 
					    pic_buffer = mmap(NULL, pic_statistics.st_size, prot, flags, -1, 0);
 | 
				
			||||||
 | 
					    if (MAP_FAILED == pic_buffer)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto GEN_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return_value = fread(pic_buffer, 1, pic_statistics.st_size, seed_handle);
 | 
				
			||||||
 | 
					    if (return_value != pic_statistics.st_size)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto GEN_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (NULL != seed_handle)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        fclose(seed_handle);
 | 
				
			||||||
 | 
					        seed_handle = NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int (*reproduce_function)(void *, size_t) = reproduce;
 | 
				
			||||||
 | 
					    void (*pic_function)(void *, size_t, void *) = pic_buffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pic_function(pic_buffer, pic_statistics.st_size, reproduce_function);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return_value = 1;
 | 
				
			||||||
 | 
					GEN_CLEANUP:
 | 
				
			||||||
 | 
					    if (NULL != pic_buffer)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        munmap(pic_buffer, pic_statistics.st_size);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (NULL != seed_handle)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        fclose(seed_handle);
 | 
				
			||||||
 | 
					        seed_handle = NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return return_value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int reproduce(void *pic_address, size_t pic_size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int return_value = 0;
 | 
				
			||||||
 | 
					    char checksum[SHA_SUM_LENGTH];
 | 
				
			||||||
 | 
					    unsigned char digest[SHA_DIGEST_LENGTH];
 | 
				
			||||||
 | 
					    struct drand48_data drand_data;
 | 
				
			||||||
 | 
					    long int mutation_value;
 | 
				
			||||||
 | 
					    unsigned int mutation_offset = 0;
 | 
				
			||||||
 | 
					    unsigned char pic_mutated = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    srand48_r(time(NULL), &drand_data);
 | 
				
			||||||
 | 
					    lrand48_r(&drand_data, &mutation_value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    mutation_offset = (mutation_value % (pic_size + 1));
 | 
				
			||||||
 | 
					    pic_mutated = ((unsigned char *)pic_address)[mutation_offset] & (mutation_value % 2);
 | 
				
			||||||
 | 
					    ((char *)pic_address)[mutation_offset] = pic_mutated;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("%x\t%x\n", mutation_offset, pic_mutated);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    memset(checksum, 0, SHA_SUM_LENGTH);
 | 
				
			||||||
 | 
					    SHA1((const unsigned char *)pic_address, pic_size, digest);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int iter = 0; iter < SHA_DIGEST_LENGTH; iter++)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        sprintf(&checksum[iter * 2], "%02x", digest[iter]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    FILE *survived_store = fopen(checksum, "w+");
 | 
				
			||||||
 | 
					    if (NULL == survived_store)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto CLONE_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return_value = fwrite(pic_address, 1, pic_size, survived_store);
 | 
				
			||||||
 | 
					    if (return_value != pic_size)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = errno;
 | 
				
			||||||
 | 
					        goto CLONE_CLEANUP;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return_value = 1;
 | 
				
			||||||
 | 
					CLONE_CLEANUP:
 | 
				
			||||||
 | 
					    if (survived_store)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        fclose(survived_store);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return generation(checksum);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, const char **argv)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int return_value = 1;
 | 
				
			||||||
 | 
					    char seed_path[SHA_SUM_LENGTH];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    strncpy(seed_path, argv[1], SHA_SUM_LENGTH);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while (return_value)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return_value = generation(seed_path);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 1;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										144
									
								
								sins.cpp
								
								
								
								
							
							
						
						
									
										144
									
								
								sins.cpp
								
								
								
								
							| 
						 | 
					@ -1,144 +0,0 @@
 | 
				
			||||||
#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) = clonePtr;
 | 
					 | 
				
			||||||
    cloneFunc(picAddr, picSize);
 | 
					 | 
				
			||||||
    return;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void clone(void *picAddr, size_t picSize)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    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];
 | 
					 | 
				
			||||||
    FILE *fileInHandle = NULL;
 | 
					 | 
				
			||||||
    struct stat picStat;
 | 
					 | 
				
			||||||
    void *picBuffer = NULL;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    strncpy(fileInPath, argv[1], SHA_SUM_LENGTH);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    while (1)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        fileInHandle = fopen(fileInPath, "rb");
 | 
					 | 
				
			||||||
        if (NULL == fileInHandle)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            retVal = errno;
 | 
					 | 
				
			||||||
            goto MAIN_CLEANUP;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        retVal = fstat(fileno(fileInHandle), &picStat);
 | 
					 | 
				
			||||||
        if (-1 == retVal)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            retVal = errno;
 | 
					 | 
				
			||||||
            goto MAIN_CLEANUP;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        auto mmapFlags = (PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE);
 | 
					 | 
				
			||||||
        picBuffer = mmap(NULL, picStat.st_size, mmapFlags, -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) = clone;
 | 
					 | 
				
			||||||
        void (*picFunc)(void *, size_t, void *) = picBuffer;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        picFunc(picBuffer, picStat.st_size, cloneFunc);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										9
									
								
								wscript
								
								
								
								
							
							
						
						
									
										9
									
								
								wscript
								
								
								
								
							| 
						 | 
					@ -1,14 +1,17 @@
 | 
				
			||||||
#! /usr/bin/env python
 | 
					#! /usr/bin/env python
 | 
				
			||||||
# encoding: utf-8
 | 
					# encoding: utf-8
 | 
				
			||||||
 | 
					CC='clang'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def options(opt):
 | 
					def options(opt):
 | 
				
			||||||
    opt.load('nasm')
 | 
					    opt.load('nasm')
 | 
				
			||||||
    opt.load('compiler_cxx')
 | 
					    opt.load('compiler_c')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def configure(conf):
 | 
					def configure(conf):
 | 
				
			||||||
    conf.load('nasm')
 | 
					    conf.load('nasm')
 | 
				
			||||||
    conf.load('compiler_cxx')
 | 
					    conf.load('compiler_c')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def build(bld):
 | 
					def build(bld):
 | 
				
			||||||
    bld.program(source='sins.cpp', target='sins', cflags='-g')
 | 
					    bld.program(source='sins.c', target='sins',
 | 
				
			||||||
 | 
					                cflags=['-g', '-std=gnu11'],
 | 
				
			||||||
 | 
					                linkflags=['-lcrypto', '-lssl'])
 | 
				
			||||||
    bld(features='asm', source='scrap.asm', target='scrap')
 | 
					    bld(features='asm', source='scrap.asm', target='scrap')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue