summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSeppia <seppia@seppio.fish>2018-07-06 11:31:03 +0200
committerSeppia <seppia@seppio.fish>2018-07-06 11:31:03 +0200
commitceb8b34275423e4e3ec12a8b9f43d4df90ab7833 (patch)
tree0ed695fa523d2d2fbf156dc6e0fdce1873b03ff0 /src
parentcd5d8fcfa37dd2321a54168c5784cada8da6035c (diff)
downloadonetimebluh-ceb8b34275423e4e3ec12a8b9f43d4df90ab7833.tar.gz
onetimebluh-ceb8b34275423e4e3ec12a8b9f43d4df90ab7833.tar.bz2
onetimebluh-ceb8b34275423e4e3ec12a8b9f43d4df90ab7833.zip
Print functions
Defined macro for an error print function to be used to output errors. Defined also a quiet print function which gets executed only if numeric value passed as first argument is true. Changed also code in main source accordingly.
Diffstat (limited to 'src')
-rw-r--r--src/libluh.c15
-rw-r--r--src/libluh.h3
-rw-r--r--src/onetimebluh.c82
3 files changed, 44 insertions, 56 deletions
diff --git a/src/libluh.c b/src/libluh.c
index 2ef11ca..4f25e53 100644
--- a/src/libluh.c
+++ b/src/libluh.c
@@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
#include "libluh.h"
char* readInput (FILE* inputFile, uint64_t allocSize) {
@@ -103,3 +104,17 @@ char* ubinDump (char* inputStr, char* binChars) {
return comp;
}
+
+int qprintf (int isQuiet, const char* printStr, ...) {
+
+ int ret;
+
+ if (!isQuiet) {
+ va_list args;
+ va_start(args, printStr);
+ ret = vprintf (printStr, args);
+ va_end(args);
+ }
+
+ return ret;
+}
diff --git a/src/libluh.h b/src/libluh.h
index 77a8d52..ba3f450 100644
--- a/src/libluh.h
+++ b/src/libluh.h
@@ -1,6 +1,8 @@
#ifndef LIBLUH
#define LIBLUH
+#define eprintf(...) fprintf(stderr, __VA_ARGS__)
+
typedef struct opts {
int64_t comm;
int64_t nbytes;
@@ -17,5 +19,6 @@ char* cryptXor (char* inputStr, FILE* keyFile);
char* fcryptXor (FILE* inputFile, FILE* keyFile);
char* binDump (char* inputStr, char* binChars);
char* ubinDump (char* inputStr, char* binChars);
+int qprintf (int isQuiet, const char* printStr, ...);
#endif
diff --git a/src/onetimebluh.c b/src/onetimebluh.c
index fed8476..67cefef 100644
--- a/src/onetimebluh.c
+++ b/src/onetimebluh.c
@@ -61,7 +61,7 @@ int main (int argc, char* argv[]) {
break;
case 'c':
if (strlen (argv[optind - 1]) != 2){
- printf ("ERROR: too much or too many characters inserted! You must insert exactly two characters.");
+ eprintf ("ERROR: too much or too many characters inserted! You must insert exactly two characters.");
exit (EXIT_FAILURE);
}
opzioni.chars = argv[optind - 1];
@@ -105,16 +105,14 @@ int main (int argc, char* argv[]) {
} else if (sip[0] == 'T') {
opzioni.nbytes = opzioni.nbytes*1024*1024*1024*1024;
} else if (sip[0] != '\0') {
- printf ("ERROR: you must specify the size in KiB, MiB, GiB or TiB, respectively using K, M, G or T, or nothing for just bytes.");
+ eprintf ("ERROR: you must specify the size in KiB, MiB, GiB or TiB, respectively using K, M, G or T, or nothing for just bytes.");
exit (EXIT_FAILURE);
}
break;
case 'o':
opzioni.output = argv[optind-1];
if (!access (opzioni.output, F_OK)) {
- if (!opzioni.quiet) {
- printf ("WARNING: %s file exists in filesystem and will be overwritten!", opzioni.output);
- }
+ qprintf (opzioni.quiet, "WARNING: %s file exists in filesystem and will be overwritten!", opzioni.output);
}
break;
case 'q':
@@ -137,16 +135,16 @@ int main (int argc, char* argv[]) {
/* Next section performs some input checks */
if (!command) {
- printf ("ERROR: no command called\n");
+ eprintf ("ERROR: no command called\n");
exit (EXIT_FAILURE);
} else if (command > 1) {
- printf ("ERROR: multiple commands called\n");
- printf ("%s [COMMAND] [OPTIONS] ...\n", argv[0]);
+ eprintf ("ERROR: multiple commands called\n");
+ eprintf ("%s [COMMAND] [OPTIONS] ...\n", argv[0]);
exit (EXIT_FAILURE);
}
if (optind < argc) {
- printf ("ERROR: too many arguments\n");
+ eprintf ("ERROR: too many arguments\n");
exit (EXIT_FAILURE);
}
@@ -154,7 +152,7 @@ int main (int argc, char* argv[]) {
if (opzioni.comm == 'e' || opzioni.comm == 'd') {
if (!opzioni.keyfile) {
- printf ("ERROR: no key specified\n");
+ eprintf ("ERROR: no key specified\n");
exit (EXIT_FAILURE);
}
xor (&opzioni);
@@ -177,9 +175,7 @@ void xor (Opts* opzioni) {
if (opzioni->input) {
mex = fopen (opzioni->input, "r");
} else if (!opzioni->input) {
- if (!opzioni->quiet) {
- printf("INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
- }
+ qprintf(opzioni->quiet, "INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
input = readInput (stdin, 8);
}
@@ -208,9 +204,7 @@ void xor (Opts* opzioni) {
if (opzioni->output) {
fwrite (bytes, sizeof(char), mess_size, critt);
} else if (!opzioni->output) {
- if (!opzioni->quiet) {
- printf ("WARNING: output not specified, printing to standard output\n");
- }
+ qprintf (opzioni->quiet, "WARNING: output not specified, printing to standard output\n");
fprintf (stdout, "%s", bytes);
@@ -226,10 +220,8 @@ void xor (Opts* opzioni) {
ftruncate (fileno (keyx), (pad_size - mess_size));
fseek (keyx, 0L, SEEK_END);
int64_t new_pad_size = ftell (keyx);
- if (!opzioni->quiet) {
- printf ("INFO: your pad is now %li bytes shorter\n", mess_size);
- printf ("INFO: you now have %li bytes left\n", new_pad_size);
- }
+ qprintf (opzioni->quiet, "INFO: your pad is now %li bytes shorter\n", mess_size);
+ qprintf (opzioni->quiet, "INFO: you now have %li bytes left\n", new_pad_size);
}
free (bytes);
@@ -247,13 +239,9 @@ void xor (Opts* opzioni) {
}
if (opzioni->comm == 'e') {
- if (!opzioni->quiet) {
- printf ("INFO: message successfully encrypted\n");
- }
+ qprintf (opzioni->quiet, "INFO: message successfully encrypted\n");
} else if (opzioni->comm == 'd') {
- if (!opzioni->quiet) {
- printf ("INFO: message successfully decrypted\n");
- }
+ qprintf (opzioni->quiet, "INFO: message successfully decrypted\n");
}
return;
@@ -262,20 +250,16 @@ void xor (Opts* opzioni) {
void keyrand (Opts* opzioni) {
if (opzioni->nbytes < -1) { // orribile
- printf ("ERROR: negative byte value inserted!\n");
+ eprintf ("ERROR: negative byte value inserted!\n");
exit (EXIT_FAILURE);
} else if (opzioni->nbytes) {
if(opzioni->nbytes == -1) {
- if (!opzioni->quiet) {
- printf ("WARNING: no byte number specified, using default value: 1MB\n");
- }
+ qprintf (opzioni->quiet, "WARNING: no byte number specified, using default value: 1MB\n");
opzioni->nbytes = 1048576;
}
- if (!opzioni->quiet) {
- printf ("INFO: generating pad...\n");
- }
+ qprintf (opzioni->quiet, "INFO: generating pad...\n");
unsigned char* key = malloc (opzioni->nbytes);
RAND_bytes (key, opzioni->nbytes);
@@ -288,13 +272,9 @@ void keyrand (Opts* opzioni) {
fclose (file);
free (key);
- if (!opzioni->quiet) {
- printf ("INFO: created key file %s of %ld bytes\n", opzioni->output, opzioni->nbytes);
- }
+ qprintf (opzioni->quiet, "INFO: created key file %s of %ld bytes\n", opzioni->output, opzioni->nbytes);
} else if (!opzioni->output) {
- if (!opzioni->quiet) {
- printf ("WARNING: output not specified, printing to standard output\n");
- }
+ qprintf (opzioni->quiet, "WARNING: output not specified, printing to standard output\n");
fprintf (stdout, "%s", key);
free (key);
@@ -305,8 +285,8 @@ void keyrand (Opts* opzioni) {
}
} else {
- printf ("WARNING: byte number specified is 0.\n");
- printf ("WARNING: doing nothing!\n");
+ eprintf ("WARNING: byte number specified is 0.\n");
+ eprintf ("WARNING: doing nothing!\n");
}
return;
@@ -321,9 +301,7 @@ void bluh (Opts* opzioni) {
if (opzioni->input) {
mex = fopen (opzioni->input, "r");
} else if (!opzioni->input) {
- if (!opzioni->quiet) {
- printf("INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
- }
+ qprintf(opzioni->quiet, "INFO: Reading from standard input (press ^D to insert the EOF character when you are finished):\n");
input = readInput (stdin, 8);
}
@@ -356,9 +334,7 @@ void bluh (Opts* opzioni) {
if (opzioni->output) {
fwrite (bits, sizeof(char), (8 * len), bluh);
} else if (!opzioni->output) {
- if (!opzioni->quiet) {
- printf ("WARNING: output not specified, printing to standard output\n");
- }
+ qprintf (opzioni->quiet, "WARNING: output not specified, printing to standard output\n");
fprintf (stdout, "%s", bits);
@@ -373,9 +349,7 @@ void bluh (Opts* opzioni) {
free (bits);
- if (!opzioni->quiet) {
- printf ("INFO: message successfully bluhed!\n");
- }
+ qprintf (opzioni->quiet, "INFO: message successfully bluhed!\n");
} else if (opzioni->comm == 'u') {
char* bits;
@@ -392,9 +366,7 @@ void bluh (Opts* opzioni) {
if (opzioni->output) {
fwrite (comp, sizeof(char), (len / 8), bluh);
} else if (!opzioni->output) {
- if (!opzioni->quiet) {
- printf ("WARNING: output not specified, printing to standard output\n");
- }
+ qprintf (opzioni->quiet, "WARNING: output not specified, printing to standard output\n");
fprintf (stdout, "%s", comp);
@@ -409,9 +381,7 @@ void bluh (Opts* opzioni) {
free (comp);
- if (!opzioni->quiet) {
- printf ("INFO: message successfully unbluhed!\n");
- }
+ qprintf (opzioni->quiet, "INFO: message successfully unbluhed!\n");
}
if (opzioni->input) {