]> wagner.pp.ru Git - openssl-gost/engine.git/blobdiff - gost12sum.c
Added CAVEATS section
[openssl-gost/engine.git] / gost12sum.c
index 71d73ebf5d3d5f966eadebfe537a3a071bd1175d..887eb1c0984d422a9c48efa5ef6e84863ad544f0 100644 (file)
@@ -14,7 +14,7 @@
 #include <limits.h>
 #include <fcntl.h>
 #ifdef _WIN32
-#include <io.h>
+# include <io.h>
 #endif
 #include <string.h>
 #include "gosthash2012.h"
@@ -34,17 +34,18 @@ int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
 int get_line(FILE *f, char *hash, char *filename, int verbose);
 
-void help()
+void help(const char *progname)
 {
     fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
-    fprintf(stderr, "gostsum12 [-bvl] [-c [file]]| [files]|-x\n"
+    fprintf(stderr, "%s [-bvl] [-c [file]]| [files]|-x\n"
             "\t-c check message digests (default is generate)\n"
             "\t-v verbose, print file names when checking\n"
             "\t-b read files in binary mode\n"
             "\t-l use 512 bit hash (default 256 bit)\n"
             "\t-x read filenames from stdin rather than from arguments \n"
             "The input for -c should be the list of message digests and file names\n"
-            "that is printed on stdout by this program when it generates digests.\n");
+                  "that is printed on stdout by this program when it generates"
+                  "digests.\n", progname);
     exit(3);
 }
 
@@ -82,9 +83,9 @@ int main(int argc, char **argv)
 
     while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
         switch (c) {
-               case 'b':
-                       open_mode = open_mode | O_BINARY;
-                       break;
+        case 'b':
+            open_mode = open_mode | O_BINARY;
+            break;
         case 'v':
             verbose = 1;
             break;
@@ -107,11 +108,12 @@ int main(int argc, char **argv)
             break;
         default:
             fprintf(stderr, "invalid option %c", optopt);
-            help();
+            help(argv[0]);
         }
     }
     if (check_file) {
-        char inhash[MAX_HASH_SIZE+1], calcsum[MAX_HASH_SIZE+1], filename[PATH_MAX];
+        char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
+            filename[PATH_MAX];
         int failcount = 0, count = 0;;
         if (check_file == stdin && optind < argc) {
             check_file = fopen(argv[optind], "r");
@@ -124,9 +126,9 @@ int main(int argc, char **argv)
             count++;
             if (!hash_file(&ctx, filename, calcsum, open_mode)) {
                 errors++;
-                               continue;
+                continue;
             }
-            if (!strncmp(calcsum, inhash, hashsize/4+1)) {
+            if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
                 if (verbose) {
                     fprintf(stderr, "%s\tOK\n", filename);
                 }
@@ -172,9 +174,9 @@ int main(int argc, char **argv)
     } else if (optind == argc) {
         char sum[65];
 #ifdef _WIN32
-               if (open_mode & O_BINARY) {
-                       _setmode(fileno(stdin), O_BINARY);
-               }
+        if (open_mode & O_BINARY) {
+            _setmode(fileno(stdin), O_BINARY);
+        }
 #endif
         if (!hash_stream(&ctx, fileno(stdin), sum)) {
             perror("stdin");
@@ -213,19 +215,19 @@ int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
 {
     unsigned char buffer[BUF_SIZE];
-       unsigned char reverted_buffer[BUF_SIZE];
     ssize_t bytes;
-    int i,j,k;
+               size_t i;
+
     start_hash12(ctx);
     while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
-        hash12_block(ctx, reverted_buffer, bytes);
+        hash12_block(ctx, buffer, bytes);
     }
     if (bytes < 0) {
         return 0;
     }
     finish_hash12(ctx, buffer);
     for (i = 0; i < (hashsize / 8); i++) {
-        sprintf(sum + 2 * i, "%02x", buffer[i]);
+        sprintf(sum + 2 * i, "%02x", buffer[hashsize/8-1-i]);
     }
     return 1;
 }
@@ -233,12 +235,12 @@ int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
 int get_line(FILE *f, char *hash, char *filename, int verbose)
 {
     int i, len;
-       int hashstrlen = hashsize/4;
+    int hashstrlen = hashsize / 4;
     while (!feof(f)) {
         if (!fgets(filename, PATH_MAX, f))
             return 0;
         len = strlen(filename);
-        if (len < hashstrlen+2) {
+        if (len < hashstrlen + 2) {
             goto nextline;
         }
         if (filename[hashstrlen] != ' ') {
@@ -255,11 +257,11 @@ int get_line(FILE *f, char *hash, char *filename, int verbose)
         hash[hashstrlen] = 0;
         while (filename[--len] == '\n' || filename[len] == '\r')
             filename[len] = 0;
-        memmove(filename, filename + hashstrlen+1, len - hashstrlen +1);
+        memmove(filename, filename + hashstrlen + 1, len - hashstrlen + 1);
         return 1;
   nextline:
+ nextline:
         if (verbose)
-            printf(filename);
+           printf("%s\n", filename);
     }
     return 0;
 }