summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSeppia <seppia@seppio.fish>2018-07-04 15:24:29 +0200
committerSeppia <seppia@seppio.fish>2018-07-04 15:24:29 +0200
commit7be518d6781b8b4ecf72c4d20213a4780c1aec30 (patch)
tree36b8abf3528dcb51cb90a48603923b479c1eacfe /src
parentd4242f5690bb36f3fb05b04ad4b00cb61a91345d (diff)
downloadonetimebluh-7be518d6781b8b4ecf72c4d20213a4780c1aec30.tar.gz
onetimebluh-7be518d6781b8b4ecf72c4d20213a4780c1aec30.tar.bz2
onetimebluh-7be518d6781b8b4ecf72c4d20213a4780c1aec30.zip
Input from stdin
With this commit the program can now read directly from standard input, as long as from a file, and defaults to this beheaviour. If the user now wants to set an input file must use the 'input' option previously introduced.
Diffstat (limited to 'src')
-rw-r--r--src/onetimebluh.c123
1 files changed, 101 insertions, 22 deletions
diff --git a/src/onetimebluh.c b/src/onetimebluh.c
index 3279291..6dfc2ef 100644
--- a/src/onetimebluh.c
+++ b/src/onetimebluh.c
@@ -18,6 +18,7 @@ typedef struct opts {
char* output;
} Opts;
+char* readInput(FILE* input, uint64_t size);
void xor (Opts* opzioni); /* operates the bitwise XOR between mess and keyf and puts the output to outp */
void keyrand (Opts* opzioni); /* generates random numbers using RAND_bytes from openssl and puts them into outp */
void bluh (Opts* opzioni); /* performs the binary dump of the input file and prints that to outp */
@@ -177,12 +178,40 @@ int main (int argc, char* argv[]) {
exit (EXIT_SUCCESS);
}
+char* readInput (FILE* stdinput, uint64_t size) {
+
+ int64_t character;
+ uint64_t length;
+ char* input;
+
+ input = malloc (sizeof (char) * size);
+
+ while (EOF != (character = fgetc (stdinput))) {
+ input[length++] = character;
+ if (length == size) {
+ input = realloc (input, sizeof (char) * (size += 8));
+ }
+ }
+
+ input[length++] = '\0';
+
+ return realloc (input, sizeof (char) * length);
+}
+
void xor (Opts* opzioni) {
+ char* input;
FILE* critt;
- FILE* mex = fopen (opzioni->input, "r");
+ FILE* mex;
FILE* keyx = fopen (opzioni->keyfile, "r+");
+ if (opzioni->input != NULL) {
+ mex = fopen (opzioni->input, "r");
+ } else if (opzioni->input == NULL) {
+ printf("INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
+ input = readInput (stdin, 8);
+ }
+
if (opzioni->output != NULL) {
critt = fopen (opzioni->output, "w");
}
@@ -190,9 +219,14 @@ void xor (Opts* opzioni) {
int64_t mess_size;
int64_t pad_size;
- fseek (mex, 0L, SEEK_END);
- mess_size = ftell (mex);
- rewind (mex);
+ if (opzioni->input != NULL) {
+ fseek (mex, 0L, SEEK_END);
+ mess_size = ftell (mex);
+ rewind (mex);
+ } else if (opzioni->input == NULL) {
+ mess_size = strlen (input);
+ }
+
fseek (keyx, 0L, SEEK_END);
pad_size = ftell (keyx);
fseek (keyx, (pad_size - mess_size), SEEK_SET);
@@ -202,10 +236,18 @@ void xor (Opts* opzioni) {
int64_t i;
char a, b;
- for (i = 0; i < mess_size; i ++) {
- fscanf (mex, "%c", &a);
- fscanf (keyx, "%c", &b);
- bytes[i] = a ^ b;
+ if (opzioni->input != NULL) {
+ for (i = 0; i < mess_size; i ++) {
+ fscanf (mex, "%c", &a);
+ fscanf (keyx, "%c", &b);
+ bytes[i] = a ^ b;
+ }
+ } else if (opzioni->input == NULL) {
+ for (i = 0; i < mess_size; i ++) {
+ a = input[i];
+ fscanf (keyx, "%c", &b);
+ bytes[i] = a ^ b;
+ }
}
if (opzioni->output != NULL) {
@@ -235,9 +277,12 @@ void xor (Opts* opzioni) {
free (bytes);
- fclose (mex);
fclose (keyx);
+ if (opzioni->input != NULL) {
+ fclose (mex);
+ }
+
if (opzioni->output != NULL) {
fclose (critt);
}
@@ -311,8 +356,16 @@ void bluh (Opts* opzioni) {
char a = opzioni->chars[0];
char b = opzioni->chars[1];
+ char* input;
FILE* bluh;
- FILE* mex = fopen (opzioni->input, "r");
+ FILE* mex;
+
+ if (opzioni->input != NULL) {
+ mex = fopen (opzioni->input, "r");
+ } else if (opzioni->input == NULL) {
+ printf("INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
+ input = readInput (stdin, 8);
+ }
if (opzioni->output != NULL) {
bluh = fopen (opzioni->output, "w");
@@ -320,15 +373,25 @@ void bluh (Opts* opzioni) {
int64_t len;
- fseek (mex, 0L, SEEK_END);
- len = ftell (mex);
- rewind (mex);
+ if (opzioni->input != NULL) {
+ fseek (mex, 0L, SEEK_END);
+ len = ftell (mex);
+ rewind (mex);
+ } else if (opzioni->input == NULL) {
+ len = strlen (input);
+ }
if (opzioni->comm == 'b') {
- char* bytes = malloc (len);
- char* bits = malloc (8 * len);
+ char* bytes;
+
+ if (opzioni->input != NULL) {
+ bytes = malloc (len);
+ fread (bytes, sizeof(char), len, mex);
+ } else if (opzioni->input == NULL) {
+ bytes = input;
+ }
- fread (bytes, sizeof(char), len, mex);
+ char* bits = malloc (8 * len);
int64_t i,j;
@@ -340,7 +403,7 @@ void bluh (Opts* opzioni) {
if (opzioni->output != NULL) {
fwrite (bits, sizeof(char), (8 * len), bluh);
- } else if (opzioni->output == NULL ) {
+ } else if (opzioni->output == NULL) {
if (!opzioni->quiet) {
printf ("WARNING: output not specified, printing to standard output\n");
}
@@ -352,7 +415,10 @@ void bluh (Opts* opzioni) {
}
}
- free (bytes);
+ if (opzioni->input != NULL) {
+ free (bytes);
+ }
+
free (bits);
if (!opzioni->quiet) {
@@ -360,10 +426,17 @@ void bluh (Opts* opzioni) {
}
} else if (opzioni->comm == 'u') {
+ char* bits;
+
+ if (opzioni->input != NULL) {
+ bits = malloc (len);
+ fread (bits, sizeof(char), len, mex);
+ } else if (opzioni->input == NULL) {
+ bits = input;
+ }
+
char* comp = malloc ((len / 8) + 1);
- char* bits = malloc (len);
- fread (bits, sizeof(char), len, mex);
memset (comp, 0, ((len / 8) + 1));
int64_t i, j;
@@ -390,15 +463,21 @@ void bluh (Opts* opzioni) {
}
}
+ if (opzioni->input != NULL) {
+ free (bits);
+ }
+
free (comp);
- free (bits);
if (!opzioni->quiet) {
printf ("INFO: message successfully unbluhed!\n");
}
}
- fclose (mex);
+ if (opzioni->input != NULL) {
+ fclose (mex);
+ }
+
if (opzioni->output != NULL) {
fclose (bluh);
}