...
 
Commits (2)
  • Seppia's avatar
    Print functions · ceb8b342
    Seppia authored
    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.
    ceb8b342
  • Seppia's avatar
    Main header · 43e1fd07
    Seppia authored
    Created main source header file and moved appropriate declarations
    and function prototipes there.
    43e1fd07
......@@ -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;
}
#ifndef LIBLUH
#define LIBLUH
typedef struct opts {
int64_t comm;
int64_t nbytes;
int64_t quiet;
int64_t tear;
char* chars;
char* keyfile;
char* input;
char* output;
} Opts;
char* readInput (FILE* inputFile, uint64_t allocSize);
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
......@@ -6,13 +6,9 @@
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include "onetimebluh.h"
#include "libluh.h"
void xor (Opts* opzioni);
void keyrand (Opts* opzioni);
void bluh (Opts* opzioni);
void help (char* av[]);
int main (int argc, char* argv[]) {
uint64_t opt = 1;
......@@ -61,7 +57,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 +101,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 +131,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 +148,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 +171,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 +200,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 +216,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 +235,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 +246,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 +268,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 +281,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 +297,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 +330,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 +345,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 +362,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 +377,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) {
......
#ifndef ONETIMEBLUH
#define ONETIMEBLUH
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
typedef struct opts {
int64_t comm;
int64_t nbytes;
int64_t quiet;
int64_t tear;
char* chars;
char* keyfile;
char* input;
char* output;
} Opts;
void xor (Opts* opzioni);
void keyrand (Opts* opzioni);
void bluh (Opts* opzioni);
void help (char* av[]);
#endif