2013-05-08 13:35:01 +00:00
|
|
|
/*
|
2013-05-15 22:18:25 +00:00
|
|
|
* Copyright (c) 2002 Fabrice Bellard
|
|
|
|
* Copyright (c) 2013 Michael Niedermayer
|
|
|
|
* Copyright (c) 2013 James Almer
|
2013-05-08 13:35:01 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2014-04-21 15:23:38 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2013-05-15 22:18:25 +00:00
|
|
|
#include "libavutil/error.h"
|
|
|
|
#include "libavutil/hash.h"
|
2013-05-08 13:35:01 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2021-07-22 12:47:57 +00:00
|
|
|
#include <string.h>
|
2013-05-08 13:35:01 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#if HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SIZE 65536
|
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
static struct AVHashContext *hash;
|
2014-04-21 15:23:38 +00:00
|
|
|
static int out_b64;
|
2013-05-15 22:18:25 +00:00
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const char *name;
|
|
|
|
|
2014-04-21 15:23:38 +00:00
|
|
|
printf("usage: ffhash [b64:]algorithm [input]...\n");
|
2013-05-15 22:18:25 +00:00
|
|
|
printf("Supported hash algorithms:");
|
|
|
|
do {
|
|
|
|
name = av_hash_names(i);
|
|
|
|
if (name)
|
|
|
|
printf(" %s", name);
|
|
|
|
i++;
|
|
|
|
} while(name);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finish(void)
|
|
|
|
{
|
2014-04-21 15:23:38 +00:00
|
|
|
char res[2 * AV_HASH_MAX_SIZE + 4];
|
|
|
|
|
|
|
|
printf("%s=", av_hash_get_name(hash));
|
|
|
|
if (out_b64) {
|
|
|
|
av_hash_final_b64(hash, res, sizeof(res));
|
|
|
|
printf("b64:%s", res);
|
|
|
|
} else {
|
|
|
|
av_hash_final_hex(hash, res, sizeof(res));
|
|
|
|
printf("0x%s", res);
|
|
|
|
}
|
2013-05-15 22:18:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 16:11:50 +00:00
|
|
|
static int check(char *file)
|
2013-05-08 13:35:01 +00:00
|
|
|
{
|
|
|
|
uint8_t buffer[SIZE];
|
2013-05-16 22:10:28 +00:00
|
|
|
int fd, flags = O_RDONLY;
|
2013-05-08 13:35:01 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-05-16 22:10:28 +00:00
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
if (file) fd = open(file, flags);
|
2013-05-08 16:11:50 +00:00
|
|
|
else fd = 0;
|
2013-05-08 13:35:01 +00:00
|
|
|
if (fd == -1) {
|
2013-05-15 22:18:25 +00:00
|
|
|
printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
|
2013-05-08 13:35:01 +00:00
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
av_hash_init(hash);
|
2013-05-08 13:35:01 +00:00
|
|
|
for (;;) {
|
2013-09-24 13:16:32 +00:00
|
|
|
int size = read(fd, buffer, SIZE);
|
2013-05-08 13:35:01 +00:00
|
|
|
if (size < 0) {
|
2014-10-25 14:12:16 +00:00
|
|
|
int err = errno;
|
2013-06-02 20:52:23 +00:00
|
|
|
close(fd);
|
2013-05-15 22:18:25 +00:00
|
|
|
finish();
|
2014-10-25 14:12:16 +00:00
|
|
|
printf("+READ-FAILED: %s", strerror(err));
|
2013-05-08 13:35:01 +00:00
|
|
|
ret = 2;
|
|
|
|
goto end;
|
|
|
|
} else if(!size)
|
|
|
|
break;
|
2013-05-15 22:18:25 +00:00
|
|
|
av_hash_update(hash, buffer, size);
|
2013-05-08 13:35:01 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
finish();
|
2013-05-08 13:35:01 +00:00
|
|
|
end:
|
2013-05-08 16:11:50 +00:00
|
|
|
if (file)
|
|
|
|
printf(" *%s", file);
|
2013-05-08 13:35:01 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-08 16:11:50 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2014-04-21 15:23:38 +00:00
|
|
|
const char *hash_name;
|
2013-05-08 16:11:50 +00:00
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-21 15:23:38 +00:00
|
|
|
hash_name = argv[1];
|
|
|
|
out_b64 = av_strstart(hash_name, "b64:", &hash_name);
|
|
|
|
if ((ret = av_hash_alloc(&hash, hash_name)) < 0) {
|
2013-05-15 22:18:25 +00:00
|
|
|
switch(ret) {
|
|
|
|
case AVERROR(EINVAL):
|
2014-04-21 15:23:38 +00:00
|
|
|
printf("Invalid hash type: %s\n", hash_name);
|
2013-05-15 22:18:25 +00:00
|
|
|
break;
|
|
|
|
case AVERROR(ENOMEM):
|
|
|
|
printf("%s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 2; i < argc; i++)
|
2013-05-08 16:11:50 +00:00
|
|
|
ret |= check(argv[i]);
|
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
if (argc < 3)
|
2013-05-08 16:11:50 +00:00
|
|
|
ret |= check(NULL);
|
|
|
|
|
2013-05-15 22:18:25 +00:00
|
|
|
av_hash_freep(&hash);
|
|
|
|
|
2013-05-08 16:11:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|