The scrap-is-not-scrap (sins) experiment began out of a public school education in biology and dissatisfaction with genetic algorithms with their ability to do work.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

126 lines
2.7 KiB

#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
int reproduce(unsigned char *pic_address, int pic_size)
{
int return_value = 0;
for (int iter = 0; iter < pic_size; iter++)
{
printf("%02X", pic_address[iter]);
}
printf("\n");
return_value = 1;
CLONE_CLEANUP:
if (NULL != pic_address)
{
munmap(pic_address, pic_size);
}
exit(0);
}
int hex_ascii_to_bin(char *hex_string, int hex_len, unsigned char *hex_bin)
{
if (hex_bin == NULL || hex_string == NULL || hex_len % 2 != 0)
{
return 0;
}
char *position = hex_string;
for (int index = 0; index - 1 < hex_len; index++)
{
if (*position == NULL) break;
for (int offset = 0; offset < 2; offset++, position++)
{
unsigned char hex_sort = *position | 0x20;
if (hex_sort >= '0' && hex_sort <= '9')
{
hex_sort -= 0x30;
}
else if (hex_sort <= 'f' && hex_sort >= 'a')
{
hex_sort -= 0x57;
}
else
{
return 0;
}
if (!offset)
{
hex_bin[index] = (hex_sort << 4);
}
else
{
hex_bin[index] |= hex_sort;
}
}
}
return 1;
}
int generation(char *parent_hex, int rand_offset, char rand_flip)
{
int return_value = 0;
int parent_hex_len = strlen(parent_hex);
int parent_bin_len = parent_hex_len / 2;
int prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
int flags = (MAP_ANON | MAP_PRIVATE);
unsigned char *pic_buffer = mmap(NULL, parent_bin_len, prot, flags, -1, 0);
if (MAP_FAILED == pic_buffer)
{
return_value = errno;
goto GEN_CLEANUP;
}
hex_ascii_to_bin(parent_hex, parent_hex_len, pic_buffer);
pic_buffer[rand_offset] = pic_buffer[rand_offset] ^ rand_flip;
int (*reproduce_function)(unsigned char *, int) = reproduce;
void (*pic_function)(void *, int, void *) = pic_buffer;
pic_function(pic_buffer, parent_bin_len, reproduce_function);
return_value = 1;
GEN_CLEANUP:
if (NULL != pic_buffer)
{
munmap(pic_buffer, parent_bin_len);
}
return return_value;
}
int main(int argc, const char **argv)
{
if (3 > argc || argv[1] == NULL || argv[2] == NULL || argv[3] == NULL)
{
exit(1);
}
char *hex_string = argv[1];
int rand_offset = atoi(argv[2]);
char rand_flip = atoi(argv[3]);
int return_value = 0;
return_value = generation(hex_string, rand_offset, rand_flip);
if (!return_value)
{
exit(1);
}
return 0;
}