X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;ds=sidebyside;f=skein_cli.c;h=0ea815bbda0f965110f3c8cfee10e4d38779a67b;hb=a8a06bb53b71c2608d3e1e22181b0e9a6786b029;hp=bbe820c4082c4316bbf36f9084cc95c8fd5f4803;hpb=f92102e1a10a938eaad61481658409dd9b686d54;p=skeinsum.git diff --git a/skein_cli.c b/skein_cli.c index bbe820c..0ea815b 100644 --- a/skein_cli.c +++ b/skein_cli.c @@ -31,6 +31,8 @@ along with skeinsum. If not, see . #include #include "SHA3api_ref.h" +#define TRYHELP_GOODBYE() do { printf("Try 'skein%dsum --help' for more information.\n", hashbitlen); exit(1); } while(0) + extern const int hashbitlen; #define MaxNmberFiles 10 @@ -52,14 +54,14 @@ enum static struct option const long_options[] = { - { "binary", no_argument, NULL, 'b' }, - { "check", no_argument, NULL, 'c' }, - { "quiet", no_argument, NULL, QUIET_OPTION }, - { "status", no_argument, NULL, STATUS_OPTION }, - { "text", no_argument, NULL, 't' }, - { "warn", no_argument, NULL, 'w' }, - { "tag", no_argument, NULL, TAG_OPTION }, - { "help", no_argument, NULL, HELP_OPTION }, + { "binary", no_argument, NULL, 'b' }, + { "check", no_argument, NULL, 'c' }, + { "quiet", no_argument, NULL, QUIET_OPTION }, + { "status", no_argument, NULL, STATUS_OPTION }, + { "text", no_argument, NULL, 't' }, + { "warn", no_argument, NULL, 'w' }, + { "tag", no_argument, NULL, TAG_OPTION }, + { "help", no_argument, NULL, HELP_OPTION }, { "version", no_argument, NULL, VERSION_OPTION }, { NULL, 0, NULL, 0 } }; @@ -183,12 +185,26 @@ int HashBinaryMode(char file_name[], char MsgDigest[]) return HashWithMode(file_name, MsgDigest, 'b'); } -int HashStringMode(char Msg[],char MsgDigest[]) +void HashStringMode(char Msg[],char MsgDigest[]) { unsigned char output[hashbitlen/4]; Hash(hashbitlen,(unsigned char*) Msg,sizeof(Msg),output); printHexMsg(output,hashbitlen/4,MsgDigest); - return 1; +} + +void HashStdin(char MsgDigest[], int tag) +{ + char stri[100]; + char longstring[1000]; + longstring[0] = 0; + while(fgets(stri,100,stdin)) + strcat(longstring, stri); + HashStringMode(longstring, MsgDigest); + if (tag) { + printf("skein%d_v%s (-) = %s\n", hashbitlen, skeinVersion, MsgDigest); + } else { + printf("%s *-\n", MsgDigest); + } } int isProper(char MsgDigest[]) @@ -366,9 +382,7 @@ int main(int argc, char** argv) case HELP_OPTION : print_usage(); /* ..never returns */ case VERSION_OPTION : print_version(); /* ..never returns */ - default: - printf("Try `skein%dsum --help' for more information.\n",hashbitlen); - exit(1); + default: TRYHELP_GOODBYE(); } } @@ -410,8 +424,7 @@ int main(int argc, char** argv) if (check == 1 || quiet == 1 || warn == 1 || status == 1) { printf("skein%dsum: the --binary and --text options are meaningless when verifying checksums\n",hashbitlen); - printf("Try 'skein%dsum --help' for more information.\n",hashbitlen); - exit(0); + TRYHELP_GOODBYE(); } if(number_files > 0) { @@ -440,23 +453,8 @@ int main(int argc, char** argv) } } else if(errorFound != 1) - { // read stdin for strings - char stri[100]; - char longstring[1000]; - longstring[0] = 0; - while((fgets(stri,100,stdin))!=NULL) - { - strcat(longstring,stri); - } - HashStringMode(longstring,MsgDigest); - if(tag == 1) - { - printf("skein%d_v%s (-) = %s\n",hashbitlen,skeinVersion,MsgDigest); - } - else - { - printf("%s *-\n",MsgDigest); - } + { + HashStdin(MsgDigest, tag); } } @@ -465,8 +463,7 @@ int main(int argc, char** argv) if (check == 1 || quiet == 1 || warn == 1 || status == 1) { printf("skein%dsum: the --binary and --text options are meaningless when verifying checksums\n",hashbitlen); - printf("Try 'skein%dsum --help' for more information.\n",hashbitlen); - exit(0); + TRYHELP_GOODBYE(); } if(number_files > 0) { @@ -489,23 +486,8 @@ int main(int argc, char** argv) } } else if(errorFound != 1) - { // read stdin for strings - char stri[100]; - char longstring[1000]; - longstring[0] = 0; - while((fgets(stri,100,stdin))!=NULL) - { - strcat(longstring,stri); - } - HashStringMode(longstring,MsgDigest); - if(tag == 1) - { - printf("skein%d_v%s (-) = %s\n",hashbitlen,skeinVersion,MsgDigest); - } - else - { - printf("%s -\n",MsgDigest); - } + { + HashStdin(MsgDigest, tag); } } @@ -521,9 +503,7 @@ int main(int argc, char** argv) printf("skein%dsum: the --status option is meaningful only when verifying checksums\n",hashbitlen); if(warn == 1) printf("skein%dsum: the --warn option is meaningful only when verifying checksums\n",hashbitlen); - - printf("Try 'skein%dsum --help' for more information.\n",hashbitlen); - exit(1); + TRYHELP_GOODBYE(); } if(number_files > 0) {// hashing files @@ -546,23 +526,8 @@ int main(int argc, char** argv) } } else if(errorFound != 1) - { // hasing strings read from stdin - char stri[100]; - char longstring[1000]; /* TODO!!! Fix buffer overflow!!! */ - longstring[0] = 0; - while((fgets(stri,100,stdin))!=NULL) - { - strcat(longstring,stri); - } - HashStringMode(longstring,MsgDigest); - if(tag == 1) - { - printf("skein%d_v%s (-) = %s\n",hashbitlen,skeinVersion,MsgDigest); - } - else - { - printf("%s -\n",MsgDigest); - } + { + HashStdin(MsgDigest, tag); } } else if(check == 1) @@ -570,8 +535,7 @@ int main(int argc, char** argv) if(tag == 1) { printf("skein%dsum: the --tag option is meaningless when verifying checksums\n",hashbitlen); - printf("Try 'skein%dsum --help' for more information\n",hashbitlen); - exit(1); + TRYHELP_GOODBYE(); } int index_files = 0; while(index_files < number_files) @@ -726,18 +690,8 @@ int main(int argc, char** argv) } } - if ((errorFound != 1) && (argc == 1 || (hashString == 1 && number_files == 0))) - { - char stri[100]; - char longstring[1000]; - longstring[0] = 0; - while((fgets(stri,100,stdin))!=NULL) - { - strcat(longstring,stri); - } - HashStringMode(longstring,MsgDigest); - printf("%s -\n",MsgDigest); - } + if (!errorFound && (argc == 1 || (hashString == 1 && !number_files))) + HashStdin(MsgDigest, 0); return 1; }