#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct frame_f1
{
unsigned char data[24];
unsigned char used[24];
};
struct frame_f2
{
unsigned char data[32];
unsigned char used[32];
};
void do_circ_decode()
{
int inputs_used = 0;
int outputs_used = 0;
printf("CIRC DECODE!\n");
FILE *in;
FILE *out;
struct frame_f2 *input;
struct frame_f1 *output;
in = fopen("circout.txt", "rb");
out = fopen("circtest.txt", "wb");
if ((in == NULL) || (out == NULL))
{
printf("Failed to open files for decoding\n");
return;
}
unsigned int size;
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
unsigned int num_ins = size / 32;
printf("Number input frames is %d\n", num_ins);
input = malloc(num_ins * sizeof(struct frame_f2));
output = malloc((num_ins-108) * sizeof(struct frame_f1));
int i;
for (i = 0; i < num_ins; i++)
{
fread(&input[i].data, 32, 1, in);
memset(&input[i].used, 0, 32);
}
for (i = 0; i < (num_ins-108); i++)
{
memset(&output[i].used, 0, 24);
memset(&output[i].data, 0xFE, 24);
}
for (i = 0; i < (num_ins-108); i++)
{
int source_byte[24];
source_byte[ 0] = 32*111 + 32*i + 0 - 32*108;
source_byte[ 1] = 32*111 + 32*i + 0 - 32*105 + 1;
source_byte[ 2] = 32*111 + 32*i + 6 - 32*84;
source_byte[ 3] = 32*111 + 32*i + 6 - 32*81 + 1;
source_byte[ 4] = 32*111 + 32*i + 16 - 32*46;
source_byte[ 5] = 32*111 + 32*i + 16 - 32*43 + 1;
source_byte[ 6] = 32*111 + 32*i + 22 - 32*22;
source_byte[ 7] = 32*111 + 32*i + 22 - 32*19 + 1;
source_byte[ 8] = 32*111 + 32*i + 2 - 32*100;
source_byte[ 9] = 32*111 + 32*i + 2 - 32*97 + 1;
source_byte[10] = 32*111 + 32*i + 8 - 32*76;
source_byte[11] = 32*111 + 32*i + 8 - 32*73 + 1;
source_byte[12] = 32*111 + 32*i + 18 - 32*38;
source_byte[13] = 32*111 + 32*i + 18 - 32*35 + 1;
source_byte[14] = 32*111 + 32*i + 24 - 32*14;
source_byte[15] = 32*111 + 32*i + 24 - 32*11 + 1;
source_byte[16] = 32*111 + 32*i + 4 - 32*92;
source_byte[17] = 32*111 + 32*i + 4 - 32*89 + 1;
source_byte[18] = 32*111 + 32*i + 10 - 32*68;
source_byte[19] = 32*111 + 32*i + 10 - 32*65 + 1;
source_byte[20] = 32*111 + 32*i + 20 - 32*30;
source_byte[21] = 32*111 + 32*i + 20 - 32*27 + 1;
source_byte[22] = 32*111 + 32*i + 26 - 32*6;
source_byte[23] = 32*111 + 32*i + 26 - 32*3 + 1;
int j;
for (j = 0; j < 24; j++)
{
if ( (source_byte[j] >= 0) &&
(input[source_byte[j] / 32].used[source_byte[j] % 32] == 0) &&
(source_byte[j] < size) )
{
output[i].data[j] = input[source_byte[j] / 32].data[source_byte[j] % 32];
input[source_byte[j] / 32].used[source_byte[j] % 32] = 1;
output[i].used[j]++;
inputs_used++;
outputs_used++;
}
}
fwrite(&output[i].data, 24, 1, out);
}
printf("INPUT DATA (decoder)\n");
for (i = 0; i < num_ins; i++)
{
int j;
for (j = 0; j < 32; j++)
{
if (input[i].used[j] == 1)
{
printf("1");
}
else if (input[i].used[j] == 0)
{
printf("0");
}
else
{
printf("X");
}
}
printf("\n");
}
printf("OUTPUT DATA (decoder)\n");
for (i = 0; i < (num_ins - 108); i++)
{
int j;
for (j = 0; j < 24; j++)
{
if (output[i].used[j] == 1)
{
printf("1");
}
else if (output[i].used[j] == 0)
{
printf("0");
}
else
{
printf("X");
}
}
printf("\n");
}
printf("\nUsed %d inputs of %d (%d intentionally not used)\n", inputs_used, size - (size%32), size%32);
fclose(in);
fclose(out);
}
void do_circ_encode()
{
int inputs_used = 0;
int outputs_used = 0;
FILE *in;
FILE *out;
struct frame_f1 *input;
struct frame_f2 *output;
in = fopen("circin.txt", "rb");
out = fopen("circout.txt", "wb");
if ((in == NULL) || (out == NULL))
{
printf("Failed to open files for encoding\n");
return;
}
unsigned int size;
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
unsigned int num_ins = size / 24;
printf("Number input frames is %d\n", num_ins);
input = malloc(num_ins * sizeof(struct frame_f1));
output = malloc((num_ins+108) * sizeof(struct frame_f2));
int i;
for (i = 0; i < num_ins; i++)
{
fread(&input[i].data, 24, 1, in);
memset(&input[i].used, 0, 24);
}
for (i = 0; i < (num_ins+108); i++)
{
memset(&output[i].used, 0, 32);
memset(&output[i].data, 0xFF, 32);
}
for (i = 0; i < (num_ins+108); i++)
{
int source_byte[32];
source_byte[ 0] = 24*i + 0 - 24*3;
source_byte[ 1] = 24*i + 0 - 24*6 + 1;
source_byte[ 2] = 24*i + 8 - 24*11;
source_byte[ 3] = 24*i + 8 - 24*14 + 1;
source_byte[ 4] = 24*i + 16 - 24*19;
source_byte[ 5] = 24*i + 16 - 24*22 + 1;
source_byte[ 6] = 24*i + 2 - 24*27;
source_byte[ 7] = 24*i + 2 - 24*30 + 1;
source_byte[ 8] = 24*i + 10 - 24*35;
source_byte[ 9] = 24*i + 10 - 24*38 + 1;
source_byte[10] = 24*i + 18 - 24*43;
source_byte[11] = 24*i + 18 - 24*46 + 1;
source_byte[12] = -1;//24*i + 0 - 24*49;//////////////////
source_byte[13] = -1;//24*i + 1 - 24*52;//////////////////
source_byte[14] = -1;//24*i + 2 - 24*57;//////////////////
source_byte[15] = -1;//24*i + 3 - 24*60;//////////////////
source_byte[16] = 24*i + 4 - 24*65;
source_byte[17] = 24*i + 4 - 24*68 + 1;
source_byte[18] = 24*i + 12 - 24*73;
source_byte[19] = 24*i + 12 - 24*76 + 1;
source_byte[20] = 24*i + 20 - 24*81;
source_byte[21] = 24*i + 20 - 24*84 + 1;
source_byte[22] = 24*i + 6 - 24*89;
source_byte[23] = 24*i + 6 - 24*92 + 1;
source_byte[24] = 24*i + 14 - 24*97;
source_byte[25] = 24*i + 14 - 24*100 + 1;
source_byte[26] = 24*i + 22 - 24*105;
source_byte[27] = 24*i + 22 - 24*108 + 1;
source_byte[28] = -1;//24*i + 0 - 24*1;///////////////////
source_byte[29] = -1;//24*i + 1;//////////////////////////
source_byte[30] = -1;//24*i + 2 - 24*1;///////////////////
source_byte[31] = -1;//24*i + 3;//////////////////////////
int j;
for (j = 0; j < 32; j++)
{
if ( (source_byte[j] >= 0) &&
(input[source_byte[j] / 24].used[source_byte[j] % 24] == 0) &&
(source_byte[j] < size) )
{
output[i].data[j] = input[source_byte[j] / 24].data[source_byte[j] % 24];
input[source_byte[j] / 24].used[source_byte[j] % 24] = 1;
output[i].used[j]++;
inputs_used++;
outputs_used++;
}
}
fwrite(&output[i].data, 32, 1, out);
}
printf("INPUT DATA (encoder)\n");
for (i = 0; i < num_ins; i++)
{
int j;
for (j = 0; j < 24; j++)
{
if (input[i].used[j] == 1)
{
printf("1");
}
else if (input[i].used[j] == 0)
{
printf("0");
}
else
{
printf("X");
}
}
printf("\n");
}
printf("OUTPUT DATA (encoder)\n");
for (i = 0; i < (num_ins + 108); i++)
{
int j;
for (j = 0; j < 32; j++)
{
if (output[i].used[j] == 1)
{
printf("1");
}
else if (output[i].used[j] == 0)
{
printf("0");
}
else
{
printf("X");
}
}
printf("\n");
}
printf("\nUsed %d inputs of %d (%d intentionally not used)\n", inputs_used, size - (size%24), size%24);
fclose(in);
fclose(out);
}
void make_input()
{
FILE *out;
out = fopen("circin.txt", "wb");
if (out == NULL)
{
printf("Failed create input file\n");
return;
}
int i;
for (i = 1; i <= 240; i++)
{
unsigned char temp;
temp = i;
fwrite(&temp, 1, 1, out);
}
fclose(out);
}
int main(int argc, char**argv)
{
make_input();
do_circ_encode();
do_circ_decode();
printf("Done\n");
return 0;
}