Add .gitattributes
[skeinsum.git] / skein_cli.c
index 706091e4c7359c60046db0c46f9f5ab0f4daae03..bb8235483aa91b11309102a1b26055d34436ed3e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 2015 Jason Self <j@jxself.org>
+/* Copyright (C) 2014 2015 2019 Jason Self <j@jxself.org>
 
 This file is part of skeinsum.
 
@@ -29,7 +29,7 @@ along with skeinsum. If not, see <http://www.gnu.org/licenses/>.
 #include <glob.h>
 #include <sys/stat.h>
 #include <errno.h>
-#include "SHA3api_ref.h"
+#include "skeinapi_ref.h"
 
 #define WARN(msg, ...) fprintf(stderr, "skein%dsum: " msg, hashbitlen, ##__VA_ARGS__)
 
@@ -166,7 +166,7 @@ int HashFile(const char file_name[], char MsgDigest[], char mode)
        }
 
        unsigned char output[hashbitlen/4];
-       Hash(hashbitlen, (unsigned char*) readbuf, total_readed, output);
+       Hash(hashbitlen, (unsigned char*) readbuf, total_readed * 8, output);
 
        free(readbuf);
 
@@ -366,9 +366,18 @@ int VerifyHashesFromFile(FILE *fp, int status, int warn, int quiet)
        return (NotProper || NoMatch) ? -1 : 1;
 }
 
+void PrintStringHash(const char *s)
+{
+       unsigned char output[hashbitlen/4];
+       char digest[hashbitlen/4 + 1];
+       Hash(hashbitlen, s, strlen(s) * 8, output);
+       hash2hexstr(output, digest);
+       printf("%s -%s\n", digest, s);
+}
+
 void print_version(void)
 {
-       printf("skein%dsum 1.0\n", hashbitlen);
+       printf("skein%dsum %s\n", hashbitlen, VERSION);
        printf("License GPLv3+: GNU GPL version 3 or later\n");
        printf("<http://gnu.org/licenses/gpl.html>\n");
        printf("This is free software: you are free to change and redistribute it.\n");
@@ -378,29 +387,33 @@ void print_version(void)
 
 void print_usage(void)
 {
+
        printf("Usage: skein%dsum [OPTION]... [FILE]...\n",hashbitlen);
        printf("Print or check skein (%d-bit) checksums.\n",hashbitlen);
-       printf("With no FILE, or when FILE is -, read standard input.\n");
-       printf("\n");
-       printf("-b, --binary         read in binary mode\n");
-       printf("-c, --check          read skein sums from the FILEs and check them\n");
-       printf("--tag            create a BSD-style checksum\n");
-       printf("-t, --text           read in text mode (default)\n");
-       printf("\n");
-       printf("The following three options are useful only when verifying checksums:\n");
-       printf("--quiet          don't print OK for each successfully verified file\n");
-       printf("--status         don't output anything, status code shows success\n");
-       printf("-w, --warn           warn about improperly formatted checksum lines\n");
-       printf("\n");
-       printf("--strict         with --check, exit non-zero for any invalid input\n");
-       printf("--help     display this help and exit\n");
-       printf("--version  output version information and exit\n");
-       printf("\n");
-       printf("The sums are computed as described in version 1.3 of the Skein\n");
-       printf("specification. When checking, the input should be a former output of\n");
-       printf("this program. The default mode is to print a line with checksum, a\n");
-       printf("character indicating input mode ('*' for binary, space for text), and\n");
-       printf("name for each FILE.\n");
+       printf("With no FILE, or when FILE is -, read standard input.\n"
+              "\n"
+              "-b, --binary     read in binary mode\n"
+              "-c, --check      read skein sums from the FILEs and check them\n"
+              "--tag            create a BSD-style checksum\n"
+              "-t, --text       read in text mode (default)\n"
+              "-0               hash strings from command line\n"
+              "\n"
+              "The following three options are useful only when verifying checksums:\n"
+              "--quiet          don't print OK for each successfully verified file\n"
+              "--status         don't output anything, status code shows success\n"
+              "-w, --warn       warn about improperly formatted checksum lines\n"
+              "\n"
+              "--strict         with --check, exit non-zero for any invalid input\n"
+              "-h, --help       display this help and exit\n"
+              "-V, --version    output version information and exit\n"
+              "\n"
+              "The sums are computed as described in version %s of the Skein\n"
+              "specification. When checking, the input should be a former output of\n"
+              "this program. The default mode is to print a line with checksum, a\n"
+              "character indicating input mode ('*' for binary, space for text), and\n"
+              "name for each FILE.\n", skeinVersion);
+       printf("%s home page: %s\n", PACKAGE_NAME, PACKAGE_URL);
+        printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
        exit(1);
 }
 
@@ -431,6 +444,27 @@ int is_goodfile(const char filename[])
        return 1;  /* try it as good */
 }
 
+int warn_check_opts(int quiet, int warn, int status)
+{
+       int n = 0;
+       if(quiet == 1)
+               n = WARN("the --quiet option is meaningful only when verifying checksums\n");
+       if(status ==1)
+               n = WARN("the --status option is meaningful only when verifying checksums\n");
+       if(warn == 1)
+               n = WARN("the --warn option is meaningful only when verifying checksums\n");
+       return n;
+}
+
+int warn_gen_opts(int tag, int binary)
+{
+       int n = 0;
+       if (tag == 1)
+               n = WARN("the --tag option is meaningful only when calculating file checksums\n");
+       if (binary >= 0)
+               n = WARN("the --text and --binary options are meaningful only when calculating file checksums\n");
+       return n;
+}
 
 int main(int argc, char** argv)
 {
@@ -448,10 +482,10 @@ int main(int argc, char** argv)
 /*****************************************************************************************
  ************************************* GETTING DATA ***********************************
  *****************************************************************************************/
-       while ((opt = getopt_long (argc, argv, "hVbctw", long_options, NULL)) != -1)
+       while ((opt = getopt_long (argc, argv, "hVbctw0", long_options, NULL)) != -1)
        {
                switch (opt) {
-                       case 0              : hashString = 1;  break;
+                       case '0'            : hashString = 1;  break;
                        case 'b'            : binary     = 1;  break;
                        case 't'            : binary     = 0;  break;
                        case 'c'            : check      = 1;  break;
@@ -473,18 +507,25 @@ int main(int argc, char** argv)
  ************************************* PROCESSING DATA ***********************************
  *****************************************************************************************/
 
-       if (check < 0)   /* READ FILES, GENERATE CHECKSUMS AND PRINT TO STDOUT.. */
+       if (hashString > 0)
        {
-               if (quiet == 1 || warn == 1 || status == 1)
-               {
-                       if(quiet == 1)
-                               WARN("the --quiet option is meaningful only when verifying checksums\n");
-                       if(status ==1)
-                               WARN("the --status option is meaningful only when verifying checksums\n");
-                       if(warn == 1)
-                               WARN("the --warn option is meaningful only when verifying checksums\n");
+               if (warn_check_opts(quiet, warn, status) + warn_gen_opts(tag, binary))
+                       TRYHELP_GOODBYE();
+
+               int n = first_file;
+               if (n >= argc) {
+                       WARN("command line should contain strings for hashing\n");
                        TRYHELP_GOODBYE();
                }
+               while (n < argc)
+                       PrintStringHash(argv[n++]);
+               return 0;
+       }
+
+       if (check < 0)   /* READ FILES, GENERATE CHECKSUMS AND PRINT TO STDOUT.. */
+       {
+               if (warn_check_opts(quiet, warn, status))
+                       TRYHELP_GOODBYE();
 
                char mode = binary > 0 ? 'b' : 't';
 
@@ -515,14 +556,8 @@ int main(int argc, char** argv)
 
        if (check == 1)   /* READ LISTFILES, GENERATE HASHES, COMPARE WITH STORED, PRINT RESULT */
        {
-               if (tag == 1 || binary >= 0)
-               {
-                       if (tag == 1)
-                               WARN("the --tag option is meaningless when verifying checksums\n");
-                       if (binary >= 0)
-                               WARN("the --text and --binary options are meaningless when verifying checksums\n");
+               if (warn_gen_opts(tag, binary))
                        TRYHELP_GOODBYE();
-               }
 
                int file_index;
                for (file_index = first_file; file_index < argc; file_index++)