FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffhash.c
Go to the documentation of this file.
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 {
74  int fd, flags = O_RDONLY;
75  int ret = 0;
76 
77 #ifdef O_BINARY
78  flags |= O_BINARY;
79 #endif
80  if (file) fd = open(file, flags);
81  else fd = 0;
82  if (fd == -1) {
83  printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
84  ret = 1;
85  goto end;
86  }
87 
88  av_hash_init(hash);
89  for (;;) {
90  ssize_t size = read(fd, buffer, SIZE);
91  if (size < 0) {
92  close(fd);
93  finish();
94  printf("+READ-FAILED: %s", strerror(errno));
95  ret = 2;
96  goto end;
97  } else if(!size)
98  break;
99  av_hash_update(hash, buffer, size);
100  }
101  close(fd);
102 
103  finish();
104 end:
105  if (file)
106  printf(" *%s", file);
107  printf("\n");
108 
109  return ret;
110 }
111 
112 int main(int argc, char **argv)
113 {
114  int i;
115  int ret = 0;
116 
117  if (argc == 1) {
118  usage();
119  return 0;
120  }
121 
122  if ((ret = av_hash_alloc(&hash, argv[1])) < 0) {
123  switch(ret) {
124  case AVERROR(EINVAL):
125  printf("Invalid hash type: %s\n", argv[1]);
126  break;
127  case AVERROR(ENOMEM):
128  printf("%s\n", strerror(errno));
129  break;
130  }
131  return 1;
132  }
133  res = av_malloc(av_hash_get_size(hash));
134  if (!res) {
135  printf("%s\n", strerror(errno));
136  return 1;
137  }
138 
139  for (i = 2; i < argc; i++)
140  ret |= check(argv[i]);
141 
142  if (argc < 3)
143  ret |= check(NULL);
144 
145  av_hash_freep(&hash);
146  av_freep(&res);
147 
148  return ret;
149 }