From 27a1b8050d2170b40024fe82cb27088f3c676f26 Mon Sep 17 00:00:00 2001 From: Benedict Date: Mon, 21 Mar 2016 21:32:46 +0100 Subject: completed set2, challenge 10 --- set1/task6.c | 53 ++++++++--------------------------------------------- 1 file changed, 8 insertions(+), 45 deletions(-) (limited to 'set1') diff --git a/set1/task6.c b/set1/task6.c index 7055044..10e19b2 100644 --- a/set1/task6.c +++ b/set1/task6.c @@ -27,56 +27,19 @@ char **transpose_blocks(char *ciphertext, int blocksize, int length) return blocks; } + int main() { - FILE *fp; int keysize; - char ch; - char *file_content, *new_file_content, *chiphertext, *block1, *block2; - int file_size = 60, file_pos = 0; - int i; + char *chiphertext, *block1, *block2; + char *file_content; + int i, file_length; double min_hamming_distance, tmp; + int ciphertext_len; - fp = fopen("6.txt", "r"); - - if (fp == NULL) { - printf("Error open file\n"); - exit(1); - } - - file_content = malloc(file_size); - - if (file_content == NULL) { - perror("out of memory"); - exit(1); - } - - // read data and decode it from base64 - // result is not a hex strin or? - - while ( (ch = fgetc(fp)) != EOF) { - // ignore new lines as this is part of base64 - if (ch == '\n' || ch == '\r') - continue; - - if (file_pos+1 >= file_size) { - new_file_content = realloc(file_content, (file_size+60)); - if (new_file_content != NULL) { - file_content = new_file_content; - file_size += 60; - } - else { - printf("error allocating memory\n"); - exit(1); - } - } - file_content[file_pos++] = ch; - } - file_content[file_pos] = '\0'; - - int ciphertext_len = 0; - - chiphertext = malloc(file_pos+1); + file_length = read_base64_file("6.txt", &file_content); + printf("read: %i, address: %p\n", file_length, file_content); + chiphertext = malloc(file_length+1); ciphertext_len = decode_base64(file_content, chiphertext); block1 = malloc(41); -- cgit v1.2.3-70-g09d2