summaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-03-21 21:32:46 +0100
committerBenedict <benedict@0xb8000.de>2017-02-21 13:00:25 +0100
commit27a1b8050d2170b40024fe82cb27088f3c676f26 (patch)
treec27966b7aeb1e4eb4fe889e6b7fc9a6cd68a5b17 /lib/lib.c
parent95b1e82c4fca864600108d4e36ceadbb290a76f3 (diff)
completed set2, challenge 10
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index c2ee753..0eb5be8 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -471,3 +471,54 @@ char brute_force_single_byte_xor(char *string, int length, struct key_and_freq *
return key;
}
+
+
+/**
+ * reads a base64 an decode it
+ * @param file path of the file
+ * @param out writes file in this buffer, allocated by this method
+ */
+int read_base64_file(const char *file, char **out)
+{
+ char ch;
+ FILE *fp;
+ int file_size = 60;
+ char *new_file_content;
+ int file_pos = 0;
+
+ fp = fopen(file, "r");
+
+ if (fp == NULL) {
+ printf("Error open file\n");
+ exit(1);
+ }
+
+ *out = malloc(file_size);
+
+ if (*out == NULL) {
+ perror("out of memory");
+ exit(1);
+ }
+
+ // read data and decode it from base64
+ 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(*out, (file_size+60));
+ if (new_file_content != NULL) {
+ *out = new_file_content;
+ file_size += 60;
+ }
+ else {
+ printf("error allocating memory\n");
+ exit(1);
+ }
+ }
+ (*out)[file_pos++] = ch;
+ }
+ return file_pos;
+
+}