]> git.sesse.net Git - ffmpeg/blob - tools/ffhash.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / tools / ffhash.c
1 /*
2  * Copyright (c) 2002 Fabrice Bellard
3  * Copyright (c) 2013 Michael Niedermayer
4  * Copyright (c) 2013 James Almer
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "config.h"
24 #include "libavutil/error.h"
25 #include "libavutil/hash.h"
26 #include "libavutil/mem.h"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32
33 #if HAVE_IO_H
34 #include <io.h>
35 #endif
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #define SIZE 65536
41
42 static struct AVHashContext *hash;
43 static uint8_t *res;
44
45 static void usage(void)
46 {
47     int i = 0;
48     const char *name;
49
50     printf("usage: ffhash [algorithm] [input]...\n");
51     printf("Supported hash algorithms:");
52     do {
53         name = av_hash_names(i);
54         if (name)
55             printf(" %s", name);
56         i++;
57     } while(name);
58     printf("\n");
59 }
60
61 static void finish(void)
62 {
63     int i, len = av_hash_get_size(hash);
64
65     printf("%s=0x", av_hash_get_name(hash));
66     av_hash_final(hash, res);
67     for (i = 0; i < len; i++)
68         printf("%02x", res[i]);
69 }
70
71 static int check(char *file)
72 {
73     uint8_t buffer[SIZE];
74     int fd;
75     int ret = 0;
76
77     if (file) fd = open(file, O_RDONLY);
78     else      fd = 0;
79     if (fd == -1) {
80         printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
81         ret = 1;
82         goto end;
83     }
84
85     av_hash_init(hash);
86     for (;;) {
87         ssize_t size = read(fd, buffer, SIZE);
88         if (size < 0) {
89             finish();
90             printf("+READ-FAILED: %s", strerror(errno));
91             ret = 2;
92             goto end;
93         } else if(!size)
94             break;
95         av_hash_update(hash, buffer, size);
96     }
97     close(fd);
98
99     finish();
100 end:
101     if (file)
102         printf(" *%s", file);
103     printf("\n");
104
105     return ret;
106 }
107
108 int main(int argc, char **argv)
109 {
110     int i;
111     int ret = 0;
112
113     if (argc == 1) {
114         usage();
115         return 0;
116     }
117
118     if ((ret = av_hash_alloc(&hash, argv[1])) < 0) {
119         switch(ret) {
120         case AVERROR(EINVAL):
121             printf("Invalid hash type: %s\n", argv[1]);
122             break;
123         case AVERROR(ENOMEM):
124             printf("%s\n", strerror(errno));
125             break;
126         }
127         return 1;
128     }
129     res = av_malloc(av_hash_get_size(hash));
130     if (!res) {
131         printf("%s\n", strerror(errno));
132         return 1;
133     }
134
135     for (i = 2; i < argc; i++)
136         ret |= check(argv[i]);
137
138     if (argc < 3)
139         ret |= check(NULL);
140
141     av_hash_freep(&hash);
142     av_freep(&res);
143
144     return ret;
145 }