#include #include #include #include #include #define MAXLEN 4096 // This is what we had before: //void print_decryption(char* text, int current_key, size_t textlen){ struct pd_param{ char* text; int current_key; size_t textlen; }; void* print_decryption(void* params){ struct pd_param* p = (struct pd_param*)params; char valid = 0; // sizeof(char) is 1, but if this was an int or something it would matter // Ready for upgrade to unicode... // Except not because of character code issues... char* new_text = (char*)malloc(sizeof(char) * p->textlen); for(size_t i = 0; i < p->textlen; i++){ if(islower(p->text[i])){ new_text[i] = p->text[i] - p->current_key; if(new_text[i] < 97) new_text[i] += 26; } else if(isupper(p->text[i])){ new_text[i] = p->text[i] - p->current_key; if(new_text[i] < 65) new_text[i] += 26; } else { new_text[i] = p->text[i]; } if(i > 2) if(!strncmp(new_text+i-2, "the", 3)) valid = 1; } if(valid) printf("Key %d: %s\n", p->current_key, new_text); } int main(int argc, char ** argv){ if(argc < 2){ printf("Usage: %s ", argv[0]); return 1; } FILE *infile = fopen(argv[1], "r"); char data[MAXLEN]; size_t read_size = fread(data, 1, MAXLEN, infile); fclose(infile); pthread_t threads[25]; struct pd_param params[25]; for(char i = 0; i < 25; i++){ params[i].text = data; params[i].current_key = i+1; params[i].textlen = read_size; // pd_param + i is the same as &(pd_param[i]) pthread_create(threads + i, 0, print_decryption, params + i); } for(char i = 0; i < 25; i++) pthread_join(threads[i], 0); return 0; }