[FFmpeg-devel] [PATCH] Rename ffadler to ffhash and expand it using the generic hash API

James Almer jamrial at gmail.com
Tue May 14 02:54:33 CEST 2013


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavutil/Makefile            |  2 +-
 tools/{ffadler.c => ffhash.c} | 66 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 54 insertions(+), 14 deletions(-)
 rename tools/{ffadler.c => ffhash.c} (53%)

diff --git a/libavutil/Makefile b/libavutil/Makefile
index f7852ee..5dc61e1 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -152,6 +152,6 @@ TESTPROGS = adler32                                                     \
 
 TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
 
-TOOLS = ffadler ffeval ffescape
+TOOLS = ffhash ffeval ffescape
 
 $(SUBDIR)lzo-test$(EXESUF): ELIBS = -llzo2
diff --git a/tools/ffadler.c b/tools/ffhash.c
similarity index 53%
rename from tools/ffadler.c
rename to tools/ffhash.c
index 97e6257..b3fe022 100644
--- a/tools/ffadler.c
+++ b/tools/ffhash.c
@@ -1,6 +1,7 @@
 /*
-* Copyright (c) 2002 Fabrice Bellard
-* Copyright (c) 2013 Michael Niedermayer
+ * Copyright (c) 2002 Fabrice Bellard
+ * Copyright (c) 2013 Michael Niedermayer
+ * Copyright (c) 2013 James Almer
  *
  * This file is part of FFmpeg.
  *
@@ -20,7 +21,8 @@
  */
 
 #include "config.h"
-#include "libavutil/adler32.h"
+#include "libavutil/avassert.h"
+#include "libavutil/hash.h"
 
 #include <errno.h>
 #include <fcntl.h>
@@ -36,52 +38,90 @@
 
 #define SIZE 65536
 
+static struct AVHashContext *hash;
+
+static void finish(void)
+{
+    int i, len;
+    uint8_t res[32];
+
+    printf("%s=0x", av_hash_get_name(hash));
+    len = av_hash_get_size(hash);
+    av_assert0(len > 0 && len <= sizeof(res));
+    av_hash_final(hash, res);
+    for (i = 0; i < len; i++)
+        printf("%02"PRIx8, res[i]);
+    if(errno)
+        printf("+READ-FAILED-%d", errno);
+}
+
 static int check(char *file)
 {
     uint8_t buffer[SIZE];
-    uint32_t checksum = 1;
     int fd;
     int ret = 0;
 
     if (file) fd = open(file, O_RDONLY);
     else      fd = 0;
     if (fd == -1) {
-        printf("A32=OPEN-FAILED-%d", errno);
+        printf("%s=OPEN-FAILED-%d", av_hash_get_name(hash), errno);
         ret = 1;
         goto end;
     }
 
+    av_hash_init(hash);
     for (;;) {
         ssize_t size = read(fd, buffer, SIZE);
         if (size < 0) {
-            printf("A32=0x%08x+READ-FAILED-%d", checksum, errno);
+            finish();
             ret = 2;
             goto end;
         } else if(!size)
             break;
-        checksum = av_adler32_update(checksum, buffer, size);
+        av_hash_update(hash, buffer, size);
     }
     close(fd);
 
-    printf("A32=0x%08x", checksum);
+    finish();
 end:
     if (file)
         printf(" *%s", file);
     printf("\n");
+    av_hash_freep(&hash);
 
     return ret;
 }
 
 int main(int argc, char **argv)
 {
-    int i;
+    int i = 0;
     int ret = 0;
+    const char *name;
+
+    if (argc == 1) {
+        printf("usage: ffhash [algorithm] [input]...\n");
+        printf("Supported hash algorithms:");
+        do {
+            name = av_hash_names(i);
+            if (name)
+                printf(" %s", name);
+            i++;
+        } while(name);
+        printf("\n");
+        return 1;
+    }
+
+    ret = av_hash_alloc(&hash, argv[1]);
+    if (ret < 0) {
+        printf("Invalid hash type: %s\n", argv[1]);
+        return ret;
+    }
 
-    for (i = 1; i<argc; i++)
-        ret |= check(argv[i]);
+    for (i = 2; i<argc; i++)
+         ret |= check(argv[i]);
 
-    if (argc == 1)
-        ret |= check(NULL);
+    if (argc < 3)
+         ret |= check(NULL);
 
     return ret;
 }
-- 
1.8.1.5



More information about the ffmpeg-devel mailing list