Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <unistd.h>
  9. #define MAX_SIZE 1000000
  10.  
  11.  
  12. /* void updatecheck(char * initialtime){
  13. struct stat attr;
  14. stat("/Users/meganholt/Dropbox/Test/Public_channel.txt", &attr);
  15. char newtime[100];
  16. memcpy(newtime,ctime(&attr.st_mtime),100);
  17. printf("initial time = %s\n", initialtime);
  18. printf("newtime = %s\n", newtime);
  19.  
  20. if (initialtime != newtime){
  21. int i = 0;
  22. while (i == 0)
  23. {
  24. stat("/Users/meganholt/Dropbox/Test/Public_channel.txt", &attr);
  25. memcpy(newtime,ctime(&attr.st_mtime),100);
  26. i = strcmp(initialtime, newtime);
  27. }
  28. }
  29. //WE CREATE NEWTIME AND CHECK IF IT DOESN'T EQUAL INITITAL TIME - AT END OF THIS FUNCTION WE SHOULD UPDATE INITIAL TIME????
  30. strcpy(initialtime, newtime);
  31. }*/
  32.  
  33. void updatecheck(){
  34. //function to continually open and read the first line of the file until the file line is expected flag
  35. FILE *Public;
  36. const char publicfile[]="/home/bob/Dropbox/Test/Public_channel.txt";
  37.  
  38. char line[1];
  39. char flag[]={'B'};
  40. while (line[0] != flag[0]){
  41. Public=fopen(publicfile, "r");
  42. fscanf(Public, "%c", &line[0]);
  43. printf("%c\n", line[0]);
  44. fclose(Public);
  45. }
  46.  
  47. //fgets(line, sizeof(line), Public);
  48. /* if(line[0] != flag[0]){
  49. printf("%c\n", line[0]);
  50. //memset(line, 0, sizeof(line));
  51. updatecheck();
  52. }*/
  53. printf("update check function finished");
  54.  
  55. }
  56.  
  57. int ** SplitArray(int arr[], int arrLength, int nSplits) {
  58. //function to split an array into given parts
  59. //Total size in bytes of array
  60. int arraySizeBytes = arrLength * sizeof(int);
  61. //Total size in bytes of each split block of memory
  62. int splitSizeBytes = arraySizeBytes / nSplits;
  63. //Work out number of ints in array dynamically (we don't like magic numbers)
  64. int nElements = arraySizeBytes / sizeof(int);
  65.  
  66. //Allocate memory for an array of pointers that point to int pointers
  67. int ** splits = (int**)malloc(nSplits * sizeof(int*));
  68.  
  69. //For each split...
  70. for (int i = 0; i < nSplits; i++) {
  71. //Allocate enough memory for split to hold splitSizeBytes
  72. splits[i] = (int*)malloc(splitSizeBytes);
  73. //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)
  74. memcpy(splits[i], &arr[(i * nElements) / nSplits], splitSizeBytes);
  75. }
  76. return splits;
  77. }
  78.  
  79. int ParityFinder(int *block, int blocksize){
  80. //function to calculate the parity of a given block
  81. int n = 0;
  82. int parity;
  83. for (int i = 0; i < blocksize; i++){
  84. if (block[i] == 1){
  85. n++;
  86. }
  87. }
  88. if (n % 2 ==0){
  89. parity = 0;}
  90. else{
  91. parity = 1;}
  92. return parity;
  93. }
  94.  
  95. void filechange(int writelength, int readlength, int *towrite, int *toread){
  96. //function to write a given array to a file line by line, wait for a change in the file and read response
  97. readlength=readlength+1;
  98. writelength=writelength+1;
  99. int skip=1;
  100. FILE *Public;
  101. const char publicfile[]="/home/bob/Dropbox/Test/Public_channel.txt";
  102. Public=fopen(publicfile, "w");
  103. for(int i=0; i<writelength-1; i++){
  104. printf("par= %d\n", towrite[i]);
  105. }
  106. for(int i=0; i<writelength; i++){
  107. if(i<skip){
  108. fprintf(Public, "%s\n", "A");
  109. continue;
  110. }
  111. fprintf(Public, "%d\n", towrite[i-1]);
  112. }
  113. fclose(Public);
  114.  
  115. updatecheck();
  116. Public=fopen(publicfile, "r");
  117. char line[MAX_SIZE];
  118. int i=0;
  119. while(fgets(line, sizeof(line), Public)!=NULL){
  120. i++;
  121. if(i<skip){
  122. continue;
  123. }
  124. fscanf(Public, "%d", &toread[i]);
  125. }
  126. fclose(Public);
  127. printf("Done\n");
  128.  
  129. }
  130.  
  131.  
  132. int Passes(int keylength, int k, int ** passblocks, int x){
  133. int Apars[x];
  134. int par;
  135. for(int i=0; i<x; i++){
  136. par=ParityFinder(passblocks[i], k); //call parity finder for each block
  137. Apars[i]=par;
  138. }
  139. //write parities to file, wait and read - FUNCTION??
  140. int Bpars[x];
  141. filechange(x, x, Apars, Bpars);
  142. printf("File changed\n");
  143.  
  144. int diff=0;
  145. for(int i=0; i<x; i++){
  146. if(Apars[i]!=Bpars[i]){
  147. diff++;
  148. }
  149. }
  150. int loc[diff];
  151. for(int i=0; i<x; i++){
  152. if(Apars[i]!=Bpars[i]){
  153. loc[i]=i*k; //loc will be a list of indices where each element is the first element in a block with an error
  154. }
  155. }
  156.  
  157. int blocksize=k;
  158. int A2pars[diff];
  159. int B2pars[diff];
  160. //iterate through the blocks, if odd parity then split those blocks and find parities. write to file etc
  161. for(int i=0; i<x; i++){
  162. if(Apars[i]!=Bpars[i]){
  163. //parities are not equal
  164. int **block=SplitArray(passblocks[i], k, 2);
  165. blocksize=blocksize/2;
  166. A2pars[i]=ParityFinder(block[0], blocksize);
  167. }
  168. }
  169.  
  170. //obtain Bob's parities from file
  171. filechange(diff, diff, A2pars, B2pars);
  172. printf("File changed\n");
  173.  
  174. while(blocksize > 1){
  175. //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
  176. for(int i=0; i<diff; i++){
  177. if(A2pars[i]==B2pars[i]){
  178. int **block=SplitArray(block[1], blocksize, 2);
  179. blocksize=blocksize/2;
  180. A2pars[i]=ParityFinder(block[0], blocksize);
  181. loc[i]=loc[i]+blocksize;
  182. }
  183. else{
  184. int **block=SplitArray(block[0], blocksize, 2);
  185. blocksize=blocksize/2;
  186. A2pars[i]=ParityFinder(block[0], blocksize);
  187. }
  188. }
  189. filechange(diff, diff, A2pars, B2pars);}
  190. printf("File changed\n");
  191. //Need a way of appending the index of an error to a list once it's found at block size 1
  192. //if(blocksize!=1){
  193. // binary(passblocks, A2pars, B2pars, x, k, loc);
  194. //}
  195. for(int i = 0; i < diff; i++){
  196. printf("%d", loc[i]);
  197. }
  198. return 0;
  199. }
  200.  
  201.  
  202. int main(){
  203. //char time[100];
  204. //char *timeptr;
  205. //timeptr=time;
  206.  
  207. int Akey[] = {0,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1};
  208. int keylength = 16;
  209. int k = 4;
  210. int x=keylength/k; //number of divisions for the key based on block size!!!
  211. int **passblocks=SplitArray(Akey, keylength, x);
  212. Passes(keylength, k, passblocks, x);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement