ffhash.c 3.35 KB
Newer Older
1
/*
2 3 4
 * Copyright (c) 2002 Fabrice Bellard
 * Copyright (c) 2013 Michael Niedermayer
 * Copyright (c) 2013 James Almer
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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"
24
#include "libavutil/avstring.h"
25 26 27
#include "libavutil/error.h"
#include "libavutil/hash.h"
#include "libavutil/mem.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>

#if HAVE_IO_H
#include <io.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#define SIZE 65536

43
static struct AVHashContext *hash;
44
static int out_b64;
45 46 47 48 49 50

static void usage(void)
{
    int i = 0;
    const char *name;

51
    printf("usage: ffhash [b64:]algorithm [input]...\n");
52 53 54 55 56 57 58 59 60 61 62 63
    printf("Supported hash algorithms:");
    do {
        name = av_hash_names(i);
        if (name)
            printf(" %s", name);
        i++;
    } while(name);
    printf("\n");
}

static void finish(void)
{
64 65 66 67 68 69 70 71 72 73
    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);
    }
74 75
}

76
static int check(char *file)
77 78
{
    uint8_t buffer[SIZE];
79
    int fd, flags = O_RDONLY;
80 81
    int ret = 0;

82 83 84 85
#ifdef O_BINARY
    flags |= O_BINARY;
#endif
    if (file) fd = open(file, flags);
86
    else      fd = 0;
87
    if (fd == -1) {
88
        printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
89 90 91 92
        ret = 1;
        goto end;
    }

93
    av_hash_init(hash);
94
    for (;;) {
95
        int size = read(fd, buffer, SIZE);
96
        if (size < 0) {
97
            int err = errno;
98
            close(fd);
99
            finish();
100
            printf("+READ-FAILED: %s", strerror(err));
101 102 103 104
            ret = 2;
            goto end;
        } else if(!size)
            break;
105
        av_hash_update(hash, buffer, size);
106 107 108
    }
    close(fd);

109
    finish();
110
end:
111 112
    if (file)
        printf(" *%s", file);
113 114 115 116
    printf("\n");

    return ret;
}
117 118 119 120 121

int main(int argc, char **argv)
{
    int i;
    int ret = 0;
122
    const char *hash_name;
123

124 125 126 127 128
    if (argc == 1) {
        usage();
        return 0;
    }

129 130 131
    hash_name = argv[1];
    out_b64 = av_strstart(hash_name, "b64:", &hash_name);
    if ((ret = av_hash_alloc(&hash, hash_name)) < 0) {
132 133
        switch(ret) {
        case AVERROR(EINVAL):
134
            printf("Invalid hash type: %s\n", hash_name);
135 136 137 138 139 140 141 142 143
            break;
        case AVERROR(ENOMEM):
            printf("%s\n", strerror(errno));
            break;
        }
        return 1;
    }

    for (i = 2; i < argc; i++)
144 145
        ret |= check(argv[i]);

146
    if (argc < 3)
147 148
        ret |= check(NULL);

149 150
    av_hash_freep(&hash);

151 152
    return ret;
}