2 * Copyright (c) 2002 Fabrice Bellard
3 * Copyright (c) 2013 Michael Niedermayer
4 * Copyright (c) 2013 James Almer
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avstring.h"
25 #include "libavutil/error.h"
26 #include "libavutil/hash.h"
27 #include "libavutil/mem.h"
43 static struct AVHashContext *hash;
46 static void usage(void)
51 printf("usage: ffhash [b64:]algorithm [input]...\n");
52 printf("Supported hash algorithms:");
54 name = av_hash_names(i);
62 static void finish(void)
64 char res[2 * AV_HASH_MAX_SIZE + 4];
66 printf("%s=", av_hash_get_name(hash));
68 av_hash_final_b64(hash, res, sizeof(res));
69 printf("b64:%s", res);
71 av_hash_final_hex(hash, res, sizeof(res));
76 static int check(char *file)
79 int fd, flags = O_RDONLY;
85 if (file) fd = open(file, flags);
88 printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
95 int size = read(fd, buffer, SIZE);
100 printf("+READ-FAILED: %s", strerror(err));
105 av_hash_update(hash, buffer, size);
112 printf(" *%s", file);
118 int main(int argc, char **argv)
122 const char *hash_name;
130 out_b64 = av_strstart(hash_name, "b64:", &hash_name);
131 if ((ret = av_hash_alloc(&hash, hash_name)) < 0) {
133 case AVERROR(EINVAL):
134 printf("Invalid hash type: %s\n", hash_name);
136 case AVERROR(ENOMEM):
137 printf("%s\n", strerror(errno));
143 for (i = 2; i < argc; i++)
144 ret |= check(argv[i]);
149 av_hash_freep(&hash);