diff options
author | Seppia <seppia@seppio.fish> | 2018-07-04 11:29:44 +0200 |
---|---|---|
committer | Seppia <seppia@seppio.fish> | 2018-07-04 11:29:44 +0200 |
commit | d4242f5690bb36f3fb05b04ad4b00cb61a91345d (patch) | |
tree | b5d277f03435b10b285ee15e6feecccfd366cdd5 /src | |
parent | e1da443c111a766720d5e1dd94b9c88be64eff69 (diff) | |
download | onetimebluh-d4242f5690bb36f3fb05b04ad4b00cb61a91345d.tar.gz onetimebluh-d4242f5690bb36f3fb05b04ad4b00cb61a91345d.tar.bz2 onetimebluh-d4242f5690bb36f3fb05b04ad4b00cb61a91345d.zip |
Default output changes
Default output has now changed to standard output, unless specified
a file name. This could be usefulf for scripting along with the use
of the 'quiet' option.
This commis also introduces a new 'input' option, in anticipation of
a future update which will defaults to standard input if no 'input'
option is specified as for the 'output' option.
Diffstat (limited to 'src')
-rw-r--r-- | src/onetimebluh.c | 196 |
1 files changed, 105 insertions, 91 deletions
diff --git a/src/onetimebluh.c b/src/onetimebluh.c index 0760036..3279291 100644 --- a/src/onetimebluh.c +++ b/src/onetimebluh.c @@ -45,30 +45,27 @@ int main (int argc, char* argv[]) { while (opt) { int option_index = 0; static struct option options[] = { - {"bluh", required_argument, 0, 'b'}, + {"bluh", no_argument, 0, 'b'}, {"char", required_argument, 0, 'c'}, - {"decrypt", required_argument, 0, 'd'}, - {"encrypt", required_argument, 0, 'e'}, + {"decrypt", no_argument, 0, 'd'}, + {"encrypt", no_argument, 0, 'e'}, {"key-file", required_argument, 0, 'k'}, + {"input", required_argument, 0, 'i'}, {"gen-key", no_argument, 0, 'g'}, {"help", no_argument, 0, 'h'}, {"nbytes", required_argument, 0, 'n'}, {"output", required_argument, 0, 'o'}, {"quiet", no_argument, 0, 'q'}, {"tear-page", no_argument, 0, 't'}, - {"uhbluh", required_argument, 0, 'u'}, + {"uhbluh", no_argument, 0, 'u'}, {0, 0, 0, 0}, }; - if ((opt = getopt_long (argc, argv, "b:c:d:e:ghk:n:o:qtu:", options, &option_index)) == -1) + if ((opt = getopt_long (argc, argv, "bc:deghk:i:n:o:qtu", options, &option_index)) == -1) break; switch (opt) { case 'b': - opzioni.input = argv[optind-1]; - if (access (opzioni.input, F_OK) == -1) { /* look at next comment */ - error (errno, errno, opzioni.input); - } command ++; opzioni.comm = 'b'; break; @@ -80,15 +77,10 @@ int main (int argc, char* argv[]) { opzioni.chars = argv[optind - 1]; break; case 'd': - opzioni.input = argv[optind-1]; command ++; opzioni.comm = 'd'; break; case 'e': - 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 ++; opzioni.comm = 'e'; break; @@ -102,10 +94,16 @@ int main (int argc, char* argv[]) { break; case 'k': opzioni.keyfile = argv[optind-1]; - if (access (opzioni.keyfile, F_OK) == -1) { /* look at the previous comment */ + if (access (opzioni.keyfile, F_OK) == -1) { error (errno, errno, opzioni.keyfile); } break; + case 'i': + opzioni.input = argv[optind-1]; + if (access (opzioni.input, F_OK) == -1) { + error (errno, errno, opzioni.input); + } + break; case 'n': opzioni.nbytes = strtol (argv[optind-1], &sip, 10); if (sip[0] == 'K') { @@ -123,6 +121,11 @@ int main (int argc, char* argv[]) { 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); + } + } break; case 'q': opzioni.quiet = 1; @@ -131,10 +134,6 @@ int main (int argc, char* argv[]) { opzioni.tear = 1; break; case 'u': - opzioni.input = argv[optind-1]; - if (access (opzioni.input, F_OK) == -1) { /* just guess */ - error (errno, errno, opzioni.input); - } command ++; opzioni.comm = 'u'; break; @@ -180,28 +179,13 @@ int main (int argc, char* argv[]) { void xor (Opts* opzioni) { - char* defenout = "critt"; - char* defdeout = "decritt"; - - /* In absence of input by users next block sets the default values */ - - if (opzioni->output == NULL) { - if (opzioni->comm == 'e') { - if (!opzioni->quiet) { - printf ("WARNING: no output name specified using default value 'critt'\n"); - } - opzioni->output = defenout; - } else if (opzioni->comm == 'd') { - if (!opzioni->quiet) { - printf ("WARNING: no output name specified usign default value 'decritt'\n"); - } - opzioni->output = defdeout; - } - } - + FILE* critt; FILE* mex = fopen (opzioni->input, "r"); FILE* keyx = fopen (opzioni->keyfile, "r+"); - FILE* critt = fopen (opzioni->output, "w"); + + if (opzioni->output != NULL) { + critt = fopen (opzioni->output, "w"); + } int64_t mess_size; int64_t pad_size; @@ -224,7 +208,20 @@ void xor (Opts* opzioni) { bytes[i] = a ^ b; } - fwrite (bytes, sizeof(char), mess_size, critt); + if (opzioni->output != NULL) { + fwrite (bytes, sizeof(char), mess_size, critt); + } else if (opzioni->output == NULL) { + if (!opzioni->quiet) { + printf ("WARNING: output not specified, printing to standard output\n"); + } + + fprintf (stdout, "%s", bytes); + + if (!opzioni->quiet) { + printf ("\n"); + } + } + if (opzioni->tear) { ftruncate (fileno (keyx), (pad_size - mess_size)); @@ -240,7 +237,10 @@ void xor (Opts* opzioni) { fclose (mex); fclose (keyx); - fclose (critt); + + if (opzioni->output != NULL) { + fclose (critt); + } if (opzioni->comm == 'e') { if (!opzioni->quiet) { @@ -257,21 +257,11 @@ void xor (Opts* opzioni) { void keyrand (Opts* opzioni) { - char* defout = "default.key"; - - /* Next block controls the inputs and eventually sets the default values */ - - if((opzioni->nbytes == -1) && (opzioni->output == NULL)) { - if (!opzioni->quiet) { - printf ("WARNING: no option specified using default values\n"); - } - opzioni->nbytes = 1048576; - opzioni->output = defout; - } if (opzioni->nbytes < -1) { // orribile printf ("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"); @@ -279,26 +269,35 @@ void keyrand (Opts* opzioni) { opzioni->nbytes = 1048576; } - if (opzioni->output == NULL) { - opzioni->output = defout; - if (!opzioni->quiet) { - printf ("WARNING: no output name specified, using default value: default.key\n"); - } - } - if (!opzioni->quiet) { printf ("INFO: generating pad...\n"); } + unsigned char* key = malloc (opzioni->nbytes); RAND_bytes (key, opzioni->nbytes); - FILE* file = fopen (opzioni->output, "w"); - fwrite (key, sizeof(char), opzioni->nbytes, file); - fclose (file); - free (key); - if (!opzioni->quiet) { - printf ("INFO: created key file %s of %ld bytes\n", opzioni->output, opzioni->nbytes); + FILE* file; + if (opzioni->output != NULL) { + file = fopen (opzioni->output, "w"); + fwrite (key, sizeof(char), opzioni->nbytes, file); + fclose (file); + free (key); + + if (!opzioni->quiet) { + printf ("INFO: created key file %s of %ld bytes\n", opzioni->output, opzioni->nbytes); + } + } else if (opzioni->output == NULL) { + if (!opzioni->quiet) { + printf ("WARNING: output not specified, printing to standard output\n"); + } + + fprintf (stdout, "%s", key); + + if (!opzioni->quiet) { + printf ("\n"); + } } + } else { printf ("WARNING: byte number specified is 0.\n"); printf ("WARNING: doing nothing!\n"); @@ -309,30 +308,16 @@ void keyrand (Opts* opzioni) { void bluh (Opts* opzioni) { - char* defbluh = "bluhed"; - char* defunbluh = "unbluhed"; char a = opzioni->chars[0]; char b = opzioni->chars[1]; - /* In absence of input by users next block sets the default values */ + FILE* bluh; + FILE* mex = fopen (opzioni->input, "r"); - if (opzioni->output == NULL) { - if (opzioni->comm == 'b') { - if (!opzioni->quiet) { - printf ("WARNING: no output name specified, using default value 'bluhed'\n"); - } - opzioni->output = defbluh; - } else if (opzioni->comm == 'u') { - if (!opzioni->quiet) { - printf ("WARNING: no output name specified, using default value 'unbluhed'\n"); - } - opzioni->output = defunbluh; - } + if (opzioni->output != NULL) { + bluh = fopen (opzioni->output, "w"); } - FILE* mex = fopen (opzioni->input, "r"); - FILE* bluh = fopen (opzioni->output, "w"); - int64_t len; fseek (mex, 0L, SEEK_END); @@ -353,7 +338,20 @@ void bluh (Opts* opzioni) { } } - fwrite (bits, sizeof(char), (8 * len), bluh); + if (opzioni->output != NULL) { + fwrite (bits, sizeof(char), (8 * len), bluh); + } else if (opzioni->output == NULL ) { + if (!opzioni->quiet) { + printf ("WARNING: output not specified, printing to standard output\n"); + } + + fprintf (stdout, "%s", bits); + + if (!opzioni->quiet) { + printf ("\n"); + } + } + free (bytes); free (bits); @@ -378,7 +376,20 @@ void bluh (Opts* opzioni) { } } - fwrite (comp, sizeof(char), (len / 8), bluh); + if (opzioni->output != NULL) { + fwrite (comp, sizeof(char), (len / 8), bluh); + } else if (opzioni->output == NULL ) { + if (!opzioni->quiet) { + printf ("WARNING: output not specified, printing to standard output\n"); + } + + fprintf (stdout, "%s", comp); + + if (!opzioni->quiet) { + printf ("\n"); + } + } + free (comp); free (bits); @@ -388,7 +399,9 @@ void bluh (Opts* opzioni) { } fclose (mex); - fclose (bluh); + if (opzioni->output != NULL) { + fclose (bluh); + } return; } @@ -397,17 +410,18 @@ void help (char* av[]) { fprintf (stdout,"Onetimebluh usage:\n" "%s [COMMAND] [OPTIONS]\n\n" "Commands:\n\n" - " -b, --bluh=FILE bluhes your message or in other words performs a binary dump\n" - " -d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling\n" - " -e, --encrypt=FILE encrypt message (input)\n" + " -b, --bluh bluhes your message or in other words performs a binary dump\n" + " -d, --decrypt decrypt message (input) same ad --encrypt, just for the feeling\n" + " -e, --encrypt encrypt message (input)\n" " -g, --gen-key create key file\n" " -h, --help print this help\n" - " -u, --unbluh=FILE unbluhes your message (gets the orginal message from the binary dump)\n\n" + " -u, --unbluh unbluhes your message (gets the orginal message from the binary dump)\n\n" "Options:\n\n" " -c, --char=CAHRS reads two cahracters as argument and uses them to bluh your message\n" " -k, --key-file=KEY_FILE use key (input)\n" + " -i, --input=FILE input file\n" " -n, --nbytes=NUM{K,M,G,T} number of bytes, you can specify K for KiB, M for MiB, etc... \n" - " -o, --output=FILE output name\n" + " -o, --output=FILE output file\n" " -q, --quiet suppresses all messages, except from error releated ones\n" " -t, --tear-page deletes from the pad file the bytes used to encrypt/decrypt\n\n" "Onetimebluh project repository at https://git.eigenlab.org/seppia/onetimebluh\n", av[0]); |