diff options
author | Seppia <seppia@seppia.net> | 2018-07-04 09:20:08 +0200 |
---|---|---|
committer | Seppia <seppia@seppia.net> | 2018-07-04 09:20:08 +0200 |
commit | e394bf500ca25af51abfe6bf46360236c59d623a (patch) | |
tree | 7ef6eb89a92b712676e6f6706d6e6460df33a8a0 /src | |
parent | e33838a9a2746f12f68fd31c02f6af9c6e70dbb8 (diff) | |
download | onetimebluh-e394bf500ca25af51abfe6bf46360236c59d623a.tar.gz onetimebluh-e394bf500ca25af51abfe6bf46360236c59d623a.tar.bz2 onetimebluh-e394bf500ca25af51abfe6bf46360236c59d623a.zip |
Using struct for options
Defined an options struct in order to access with more ease the
options values inside functions and changed code accordingly.
Diffstat (limited to 'src')
-rw-r--r-- | src/onetimebluh.c | 220 |
1 files changed, 115 insertions, 105 deletions
diff --git a/src/onetimebluh.c b/src/onetimebluh.c index 50eaeae..f19137b 100644 --- a/src/onetimebluh.c +++ b/src/onetimebluh.c @@ -7,25 +7,38 @@ #include <errno.h> #include <error.h> -void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ -void keyrand (int64_t nb, char* outp, int64_t quiet); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ -void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet); /* performs the binary dump of the input file and prints that to outp */ +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); /* 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 */ void help (char* av[]); /* prints the help message */ int main (int argc, char* argv[]) { uint64_t opt = 1; int64_t command = 0; - int64_t comm = 0; - int64_t tear = 0; - int64_t quiet = 0; - char* chars = "01"; - char* message = NULL; - char* keyfile = NULL; - char* output = NULL; - int64_t nbytes = -1; // must be resolved temporary workaround (ho sonno) char* sip = NULL; + Opts opzioni; + opzioni.comm = 0; + opzioni.nbytes = -1; // must be resolved temporary workaround (ho sonno) + opzioni.quiet = 0; + opzioni.tear = 0; + opzioni.chars = "01"; + opzioni.keyfile = NULL; + opzioni.input = NULL; + opzioni.output = NULL; + /* The following while cycle parses the argv vector to find commands, options and relative arguments using the function getopt_long */ @@ -52,78 +65,78 @@ int main (int argc, char* argv[]) { switch (opt) { case 'b': - message = argv[optind-1]; - if (access (message, F_OK) == -1) { /* look at next comment */ - error (errno, errno, message); + opzioni.input = argv[optind-1]; + if (access (opzioni.input, F_OK) == -1) { /* look at next comment */ + error (errno, errno, opzioni.input); } command ++; - comm = 'b'; + opzioni.comm = 'b'; break; case 'c': if (strlen (argv[optind - 1]) != 2){ printf ("Too much or too many characters inserted!"); exit (EXIT_FAILURE); } - chars = argv[optind - 1]; + opzioni.chars = argv[optind - 1]; break; case 'd': - message = argv[optind-1]; + opzioni.input = argv[optind-1]; command ++; - comm = 'r'; + opzioni.comm = 'd'; break; case 'e': - message = argv[optind-1]; - if (access (message, F_OK) == -1) { /* checks the existence of the file and eventually exits */ - error (errno, errno, message); + opzioni.input = argv[optind-1]; + if (access (opzioni.input, F_OK) == -1) { /* checks the existence of the file and eventually exits */ + error (errno, errno, opzioni.input); } command ++; - comm = 'e'; + opzioni.comm = 'e'; break; case 'g': command++; - comm = 'g'; + opzioni.comm = 'g'; break; case 'h': help (argv); command ++; break; case 'k': - keyfile = argv[optind-1]; - if (access (keyfile, F_OK) == -1) { /* look at the previous comment */ - error (errno, errno, keyfile); + opzioni.keyfile = argv[optind-1]; + if (access (opzioni.keyfile, F_OK) == -1) { /* look at the previous comment */ + error (errno, errno, opzioni.keyfile); } break; case 'n': - nbytes = strtol (argv[optind-1], &sip, 10); + opzioni.nbytes = strtol (argv[optind-1], &sip, 10); if (sip[0] == 'K') { - nbytes = nbytes*1024; + opzioni.nbytes = opzioni.nbytes*1024; } else if (sip[0] == 'M') { - nbytes = nbytes*1024*1024; + opzioni.nbytes = opzioni.nbytes*1024*1024; } else if (sip[0] == 'G') { - nbytes = nbytes*1024*1024*1024; + opzioni.nbytes = opzioni.nbytes*1024*1024*1024; } else if (sip[0] == 'T') { - nbytes = nbytes*1024*1024*1024*1024; + opzioni.nbytes = opzioni.nbytes*1024*1024*1024*1024; } else if (sip[0] != '\0') { printf ("You must specify the size in KiB, MiB, GiB or TiB, respectively using K, M, G or T, or nothing for single bytes."); exit (EXIT_FAILURE); } break; case 'o': - output = argv[optind-1]; + opzioni.output = argv[optind-1]; break; case 'q': - quiet = 1; + opzioni.quiet = 1; break; case 't': - tear = 1; + opzioni.tear = 1; break; case 'u': - message = argv[optind-1]; - if (access (message, F_OK) == -1) { /* just guess */ - error (errno, errno, message); + opzioni.input = argv[optind-1]; + if (access (opzioni.input, F_OK) == -1) { /* just guess */ + error (errno, errno, opzioni.input); } command ++; - comm = 'u'; + opzioni.comm = 'u'; break; case '?': break; @@ -134,7 +147,7 @@ int main (int argc, char* argv[]) { /* Next section performs some input checks */ - if (command == 0) { + if (!command) { printf ("%s: No command called\n", argv[0]); exit (EXIT_FAILURE); } else if (command > 1) { @@ -150,48 +163,45 @@ int main (int argc, char* argv[]) { /* Next section detects the functions to call */ - if (comm == 'e' || comm == 'r') { - if (keyfile == NULL) { + if (opzioni.comm == 'e' || opzioni.comm == 'd') { + if (opzioni.keyfile == NULL) { printf ("%s: No key specified: exit!\n", argv[0]); exit (EXIT_FAILURE); } - if (tear == 1) { - comm++; - } - xor (comm, message, keyfile, output, quiet); - } else if (comm == 'g') { - keyrand (nbytes, output, quiet); - } else if (comm == 'b' || comm == 'u') { - bluh (comm, message, output, chars, quiet); + xor (&opzioni); + } else if (opzioni.comm == 'g') { + keyrand (&opzioni); + } else if (opzioni.comm == 'b' || opzioni.comm == 'u') { + bluh (&opzioni); } exit (EXIT_SUCCESS); } -void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet) { +void xor (Opts* opzioni) { - char* defenoutp = "critt"; - char* defdeoutp = "decritt"; + char* defenout = "critt"; + char* defdeout = "decritt"; /* In absence of input by users next block sets the default values */ - if (outp == NULL) { - if (ed == 'e' || ed == 'f') { - if (!quiet) { + if (opzioni->output == NULL) { + if (opzioni->comm == 'e') { + if (!opzioni->quiet) { printf ("WARNING no output name specified using default value 'critt' \n"); } - outp = defenoutp; - } else if (ed == 'r' || ed == 's') { - if (!quiet) { + opzioni->output = defenout; + } else if (opzioni->comm == 'd') { + if (!opzioni->quiet) { printf ("WARNING no output name specified usign default value 'decritt' \n"); } - outp = defdeoutp; + opzioni->output = defdeout; } } - FILE* mex = fopen (mess, "r"); - FILE* keyx = fopen (keyf, "r+"); - FILE* critt = fopen (outp, "w"); + FILE* mex = fopen (opzioni->input, "r"); + FILE* keyx = fopen (opzioni->keyfile, "r+"); + FILE* critt = fopen (opzioni->output, "w"); int64_t mess_size; int64_t pad_size; @@ -216,11 +226,11 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet) { fwrite (bytes, sizeof(char), mess_size, critt); - if (ed == 'f' || ed == 's') { + if (opzioni->tear) { ftruncate (fileno (keyx), (pad_size - mess_size)); fseek (keyx, 0L, SEEK_END); int64_t new_pad_size = ftell (keyx); - if (!quiet) { + if (!opzioni->quiet) { printf ("Your pad is now %li bytes shorter \n", mess_size); printf ("You now have %li bytes left \n", new_pad_size); } @@ -232,12 +242,12 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet) { fclose (keyx); fclose (critt); - if (ed == 'e' || ed == 'f') { - if (!quiet) { + if (opzioni->comm == 'e') { + if (!opzioni->quiet) { printf ("Message successfully encrypted \n"); } - } else if (ed == 'r' || ed == 's') { - if (!quiet) { + } else if (opzioni->comm == 'd') { + if (!opzioni->quiet) { printf ("Message successfully decrypted \n"); } } @@ -245,50 +255,50 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet) { return; } -void keyrand (int64_t nb, char* outp, int64_t quiet) { +void keyrand (Opts* opzioni) { - char* defoutp = "default.key"; + char* defout = "default.key"; /* Next block controls the inputs and eventually sets the default values */ - if((nb == -1) && (outp == NULL)) { - if (!quiet) { + if((opzioni->nbytes == -1) && (opzioni->output == NULL)) { + if (!opzioni->quiet) { printf ("WARNING no option specified usign default values... \n"); } - nb = 1048576; - outp = defoutp; + opzioni->nbytes = 1048576; + opzioni->output = defout; } - if (nb < -1) { // orribile + if (opzioni->nbytes < -1) { // orribile printf ("Negative byte value inserted! \n"); printf ("Exiting... \n"); exit (EXIT_FAILURE); - } else if (nb != 0) { - if(nb == -1) { - if (!quiet) { + } else if (opzioni->nbytes) { + if(opzioni->nbytes == -1) { + if (!opzioni->quiet) { printf ("No byte number specified... using default value: 1MB \n"); } - nb = 1048576; + opzioni->nbytes = 1048576; } - if (outp == NULL) { - outp = defoutp; - if (!quiet) { + if (opzioni->output == NULL) { + opzioni->output = defout; + if (!opzioni->quiet) { printf ("No output name specified... using default value: default.key \n"); } } - if (!quiet) { + if (!opzioni->quiet) { printf ("Generating pad...\n"); } - unsigned char* key = malloc (nb); - RAND_bytes (key, nb); + unsigned char* key = malloc (opzioni->nbytes); + RAND_bytes (key, opzioni->nbytes); - FILE* file = fopen (outp, "w"); - fwrite (key, sizeof(char), nb, file); + FILE* file = fopen (opzioni->output, "w"); + fwrite (key, sizeof(char), opzioni->nbytes, file); fclose (file); free (key); - if (!quiet) { - printf ("Created key file %s of %ld bytes \n", outp, nb); + if (!opzioni->quiet) { + printf ("Created key file %s of %ld bytes \n", opzioni->output, opzioni->nbytes); } } else { printf ("Byte number specified is 0. \n"); @@ -298,31 +308,31 @@ void keyrand (int64_t nb, char* outp, int64_t quiet) { return; } -void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet) { +void bluh (Opts* opzioni) { char* defbluh = "bluhed"; char* defunbluh = "unbluhed"; - char a = ch[0]; - char b = ch[1]; + char a = opzioni->chars[0]; + char b = opzioni->chars[1]; /* In absence of input by users next block sets the default values */ - if (outp == NULL) { - if (c == 'b') { - if (!quiet) { + if (opzioni->output == NULL) { + if (opzioni->comm == 'b') { + if (!opzioni->quiet) { printf ("WARNING no output name specified using default value 'bluhed' \n"); } - outp = defbluh; - } else if (c == 'u') { - if (!quiet) { + opzioni->output = defbluh; + } else if (opzioni->comm == 'u') { + if (!opzioni->quiet) { printf ("WARNING no output name specified usign default value 'unbluhed' \n"); } - outp = defunbluh; + opzioni->output = defunbluh; } } - FILE* mex = fopen (mess, "r"); - FILE* bluh = fopen (outp, "w"); + FILE* mex = fopen (opzioni->input, "r"); + FILE* bluh = fopen (opzioni->output, "w"); int64_t len; @@ -330,7 +340,7 @@ void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet) { len = ftell (mex); rewind (mex); - if (c == 'b') { + if (opzioni->comm == 'b') { char* bytes = malloc (len); char* bits = malloc (8 * len); @@ -348,11 +358,11 @@ void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet) { free (bytes); free (bits); - if (!quiet) { + if (!opzioni->quiet) { printf ("Message successfully bluhed!\n"); } - } else if (c == 'u') { + } else if (opzioni->comm == 'u') { char* comp = malloc ((len / 8) + 1); char* bits = malloc (len); @@ -373,7 +383,7 @@ void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet) { free (comp); free (bits); - if (!quiet) { + if (!opzioni->quiet) { printf ("Message successfully unbluhed!\n"); } } |