Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #include <math.h>
- #include <unistd.h>
- #define MAX_SIZE 1000000
- /* void updatecheck(char * initialtime){
- struct stat attr;
- stat("/Users/meganholt/Dropbox/Test/Public_channel.txt", &attr);
- char newtime[100];
- memcpy(newtime,ctime(&attr.st_mtime),100);
- printf("initial time = %s\n", initialtime);
- printf("newtime = %s\n", newtime);
- if (initialtime != newtime){
- int i = 0;
- while (i == 0)
- {
- stat("/Users/meganholt/Dropbox/Test/Public_channel.txt", &attr);
- memcpy(newtime,ctime(&attr.st_mtime),100);
- i = strcmp(initialtime, newtime);
- }
- }
- //WE CREATE NEWTIME AND CHECK IF IT DOESN'T EQUAL INITITAL TIME - AT END OF THIS FUNCTION WE SHOULD UPDATE INITIAL TIME????
- strcpy(initialtime, newtime);
- }*/
- void updatecheck(){
- //function to continually open and read the first line of the file until the file line is expected flag
- FILE *Public;
- const char publicfile[]="/home/bob/Dropbox/Test/Public_channel.txt";
- char line[1];
- char flag[]={'B'};
- while (line[0] != flag[0]){
- Public=fopen(publicfile, "r");
- fscanf(Public, "%c", &line[0]);
- printf("%c\n", line[0]);
- fclose(Public);
- }
- //fgets(line, sizeof(line), Public);
- /* if(line[0] != flag[0]){
- printf("%c\n", line[0]);
- //memset(line, 0, sizeof(line));
- updatecheck();
- }*/
- printf("update check function finished");
- }
- int ** SplitArray(int arr[], int arrLength, int nSplits) {
- //function to split an array into given parts
- //Total size in bytes of array
- int arraySizeBytes = arrLength * sizeof(int);
- //Total size in bytes of each split block of memory
- int splitSizeBytes = arraySizeBytes / nSplits;
- //Work out number of ints in array dynamically (we don't like magic numbers)
- int nElements = arraySizeBytes / sizeof(int);
- //Allocate memory for an array of pointers that point to int pointers
- int ** splits = (int**)malloc(nSplits * sizeof(int*));
- //For each split...
- for (int i = 0; i < nSplits; i++) {
- //Allocate enough memory for split to hold splitSizeBytes
- splits[i] = (int*)malloc(splitSizeBytes);
- //Copy required bytes of memory from src (address of bits, using i to offset location to each split point) to dest (the int pointer we allocated above)
- memcpy(splits[i], &arr[(i * nElements) / nSplits], splitSizeBytes);
- }
- return splits;
- }
- int ParityFinder(int *block, int blocksize){
- //function to calculate the parity of a given block
- int n = 0;
- int parity;
- for (int i = 0; i < blocksize; i++){
- if (block[i] == 1){
- n++;
- }
- }
- if (n % 2 ==0){
- parity = 0;}
- else{
- parity = 1;}
- return parity;
- }
- void filechange(int writelength, int readlength, int *towrite, int *toread){
- //function to write a given array to a file line by line, wait for a change in the file and read response
- readlength=readlength+1;
- writelength=writelength+1;
- int skip=1;
- FILE *Public;
- const char publicfile[]="/home/bob/Dropbox/Test/Public_channel.txt";
- Public=fopen(publicfile, "w");
- for(int i=0; i<writelength-1; i++){
- printf("par= %d\n", towrite[i]);
- }
- for(int i=0; i<writelength; i++){
- if(i<skip){
- fprintf(Public, "%s\n", "A");
- continue;
- }
- fprintf(Public, "%d\n", towrite[i-1]);
- }
- fclose(Public);
- updatecheck();
- Public=fopen(publicfile, "r");
- char line[MAX_SIZE];
- int i=0;
- while(fgets(line, sizeof(line), Public)!=NULL){
- i++;
- if(i<skip){
- continue;
- }
- fscanf(Public, "%d", &toread[i]);
- }
- fclose(Public);
- printf("Done\n");
- }
- int Passes(int keylength, int k, int ** passblocks, int x){
- int Apars[x];
- int par;
- for(int i=0; i<x; i++){
- par=ParityFinder(passblocks[i], k); //call parity finder for each block
- Apars[i]=par;
- }
- //write parities to file, wait and read - FUNCTION??
- int Bpars[x];
- filechange(x, x, Apars, Bpars);
- printf("File changed\n");
- int diff=0;
- for(int i=0; i<x; i++){
- if(Apars[i]!=Bpars[i]){
- diff++;
- }
- }
- int loc[diff];
- for(int i=0; i<x; i++){
- if(Apars[i]!=Bpars[i]){
- loc[i]=i*k; //loc will be a list of indices where each element is the first element in a block with an error
- }
- }
- int blocksize=k;
- int A2pars[diff];
- int B2pars[diff];
- //iterate through the blocks, if odd parity then split those blocks and find parities. write to file etc
- for(int i=0; i<x; i++){
- if(Apars[i]!=Bpars[i]){
- //parities are not equal
- int **block=SplitArray(passblocks[i], k, 2);
- blocksize=blocksize/2;
- A2pars[i]=ParityFinder(block[0], blocksize);
- }
- }
- //obtain Bob's parities from file
- filechange(diff, diff, A2pars, B2pars);
- printf("File changed\n");
- while(blocksize > 1){
- //if parity of first half is same, error occured in 2nd so we split that and send parity of first half of that and vice versa
- for(int i=0; i<diff; i++){
- if(A2pars[i]==B2pars[i]){
- int **block=SplitArray(block[1], blocksize, 2);
- blocksize=blocksize/2;
- A2pars[i]=ParityFinder(block[0], blocksize);
- loc[i]=loc[i]+blocksize;
- }
- else{
- int **block=SplitArray(block[0], blocksize, 2);
- blocksize=blocksize/2;
- A2pars[i]=ParityFinder(block[0], blocksize);
- }
- }
- filechange(diff, diff, A2pars, B2pars);}
- printf("File changed\n");
- //Need a way of appending the index of an error to a list once it's found at block size 1
- //if(blocksize!=1){
- // binary(passblocks, A2pars, B2pars, x, k, loc);
- //}
- for(int i = 0; i < diff; i++){
- printf("%d", loc[i]);
- }
- return 0;
- }
- int main(){
- //char time[100];
- //char *timeptr;
- //timeptr=time;
- int Akey[] = {0,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1};
- int keylength = 16;
- int k = 4;
- int x=keylength/k; //number of divisions for the key based on block size!!!
- int **passblocks=SplitArray(Akey, keylength, x);
- Passes(keylength, k, passblocks, x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement