Advertisement
Guest User

Stupid Sexy Pthreads

a guest
Feb 17th, 2016
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/time.h>
  6. #define NUM_THREADS         8
  7. #define THREAD_PERMUTATIONS 2197
  8.  
  9. typedef enum { false, true } bool;
  10.  
  11. int encode_cipher[1201];
  12. bool found_correct = false;
  13.  
  14. void gen_char_start_list(char *arr, int id) {
  15.     // set first starting char ascii value
  16.     if (id >= 5) {
  17.         arr[0] = (97 + id * 3) + 1;
  18.     } else {
  19.         int s = 97 + (id * 3);
  20.         arr[0] = s;
  21.     }
  22.    
  23.     // set the second starting char ascii value
  24.     if (id >= 5 && id < 7) {
  25.         arr[1] = (97 + (id - 4) * 6);
  26.     } else if (id == 3 || id == 4 || id == 7) {
  27.         if (id == 7) { id = 3; }
  28.         arr[1] = (97 + id * 6) + 1;
  29.     } else {
  30.         arr[1] = 97 + (id * 6);
  31.     }
  32.    
  33.     // finally set the third starting char
  34.     if (id == 0) {
  35.         arr[2] = 97;
  36.     } else if (id % 2 == 0) {
  37.         arr[2] = 122;
  38.     } else {
  39.         arr[2] = 109;
  40.     }
  41. }
  42.  
  43. bool is_correct_pass(char *decr_cipher) {
  44.     float num_e, num_o;
  45.     num_e = num_o = 0;
  46.     for (int i = 0; i < 1202; i++) {
  47.         if (decr_cipher[i] == 101) { num_e++; }
  48.         else if (decr_cipher[i] == 111) { num_o++; }
  49.     }
  50.    
  51.     float e_per = (num_e / 969) * 100.0;
  52.     float o_per = (num_o / 969) * 100.0;
  53.    
  54.     if (e_per > 11 && e_per < 13) {
  55.         if (o_per > 6 && o_per < 8) {
  56.             return true;
  57.         } else { return false; }
  58.     } else { return false; }
  59. }
  60.  
  61. void *decode(void *thread_id) {
  62.     char chars[3];
  63.     int p = 0, id = (int) thread_id;
  64.     char *decode_cipher = malloc(sizeof(char) * 1202);
  65.    
  66.     gen_char_start_list(chars, id);
  67.    
  68.     while(p < THREAD_PERMUTATIONS) {
  69.         if (found_correct) {
  70.             pthread_exit(NULL);
  71.             free(decode_cipher);
  72.         }
  73.  
  74.         for (int i = 0; i < 1202; i++) {
  75.             char decoded_char = encode_cipher[i] ^ chars[i % 3];
  76.             decode_cipher[i] = decoded_char;
  77.         }
  78.        
  79.         bool correct = is_correct_pass(decode_cipher);        
  80.         if (correct == true) {
  81.             found_correct = true;
  82.             pthread_exit(decode_cipher);
  83.         }
  84.          
  85.         // if both positions 1 and 2 are at the max reset them to 97 and increment position 0 by 1
  86.         if (chars[1] == 122 && chars[2] == 122) {
  87.             chars[0]++;
  88.             chars[1] = chars[2] = 97;
  89.         } else if (chars[2] == 122) {  // if just the last position is at the max, reset and increment pos 1
  90.             chars[1]++;
  91.             chars[2] = 97;
  92.         } else { // just increment the last pos
  93.             chars[2]++;
  94.         }
  95.         p++;
  96.     }
  97.  
  98.     free(decode_cipher);
  99.     pthread_exit(NULL);
  100. }
  101.  
  102. int main(int argc, char *agrv[]) {
  103.     FILE *fp;
  104.     int rc, k = 0, sum = 0;
  105.     char *token, *final;
  106.     char line[3202];
  107.     void *ptr_return;
  108.     const char *s = ",";
  109.     struct timeval stop, start;
  110.     fp = fopen("cipher.txt", "r");
  111.     pthread_t threads[NUM_THREADS];
  112.  
  113.     gettimeofday(&start, NULL);
  114.    
  115.     if(fgets(line, 3203, fp) != NULL) {
  116.         token = strtok(line, s);
  117.         while(token != NULL) {
  118.             encode_cipher[k] = atoi(token);
  119.             k++;
  120.             token = strtok(NULL, s);
  121.         }
  122.     }
  123.    
  124.     fclose(fp);
  125.  
  126.     for (long i = 0; i < NUM_THREADS; i++) {
  127.         rc = pthread_create(&threads[i], NULL, decode, (void *)i);
  128.         if (rc) {
  129.             printf("%d", rc);
  130.             exit(-1);
  131.         }
  132.     }
  133.    
  134.     for (int j = 0; j < NUM_THREADS; j++) {
  135.         pthread_join(threads[j], &ptr_return);
  136.         if (ptr_return != NULL) {
  137.             final = ptr_return;
  138.             for (int x = 0; x < 1201; x++) {
  139.                 sum += (int)final[x];
  140.             }
  141.         }
  142.     }
  143.  
  144.     printf("%d\n", sum);
  145.     free(final);
  146.     gettimeofday(&stop, NULL);
  147.     printf("took %ld seconds\n", stop.tv_sec - start.tv_sec);
  148.     printf("took %d microseconds\n", stop.tv_usec - start.tv_usec);
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement