Browse code

tools/ffhash: implement base64 output.

Also fix usage string: the algorithm is not optional.

Nicolas George authored on 2014/04/22 00:23:38
Showing 1 changed files
... ...
@@ -21,6 +21,7 @@
21 21
  */
22 22
 
23 23
 #include "config.h"
24
+#include "libavutil/avstring.h"
24 25
 #include "libavutil/error.h"
25 26
 #include "libavutil/hash.h"
26 27
 #include "libavutil/mem.h"
... ...
@@ -40,13 +41,14 @@
40 40
 #define SIZE 65536
41 41
 
42 42
 static struct AVHashContext *hash;
43
+static int out_b64;
43 44
 
44 45
 static void usage(void)
45 46
 {
46 47
     int i = 0;
47 48
     const char *name;
48 49
 
49
-    printf("usage: ffhash [algorithm] [input]...\n");
50
+    printf("usage: ffhash [b64:]algorithm [input]...\n");
50 51
     printf("Supported hash algorithms:");
51 52
     do {
52 53
         name = av_hash_names(i);
... ...
@@ -59,10 +61,16 @@ static void usage(void)
59 59
 
60 60
 static void finish(void)
61 61
 {
62
-    char res[2 * AV_HASH_MAX_SIZE + 1];
63
-
64
-    av_hash_final_hex(hash, res, sizeof(res));
65
-    printf("%s=0x%s", av_hash_get_name(hash), res);
62
+    char res[2 * AV_HASH_MAX_SIZE + 4];
63
+
64
+    printf("%s=", av_hash_get_name(hash));
65
+    if (out_b64) {
66
+        av_hash_final_b64(hash, res, sizeof(res));
67
+        printf("b64:%s", res);
68
+    } else {
69
+        av_hash_final_hex(hash, res, sizeof(res));
70
+        printf("0x%s", res);
71
+    }
66 72
 }
67 73
 
68 74
 static int check(char *file)
... ...
@@ -110,16 +118,19 @@ int main(int argc, char **argv)
110 110
 {
111 111
     int i;
112 112
     int ret = 0;
113
+    const char *hash_name;
113 114
 
114 115
     if (argc == 1) {
115 116
         usage();
116 117
         return 0;
117 118
     }
118 119
 
119
-    if ((ret = av_hash_alloc(&hash, argv[1])) < 0) {
120
+    hash_name = argv[1];
121
+    out_b64 = av_strstart(hash_name, "b64:", &hash_name);
122
+    if ((ret = av_hash_alloc(&hash, hash_name)) < 0) {
120 123
         switch(ret) {
121 124
         case AVERROR(EINVAL):
122
-            printf("Invalid hash type: %s\n", argv[1]);
125
+            printf("Invalid hash type: %s\n", hash_name);
123 126
             break;
124 127
         case AVERROR(ENOMEM):
125 128
             printf("%s\n", strerror(errno));