ffhash.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "config.h"
  23. #include "libavutil/error.h"
  24. #include "libavutil/hash.h"
  25. #include "libavutil/mem.h"
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <sys/stat.h>
  30. #if HAVE_IO_H
  31. #include <io.h>
  32. #endif
  33. #if HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #define SIZE 65536
  37. static struct AVHashContext *hash;
  38. static uint8_t *res;
  39. static void usage(void)
  40. {
  41. int i = 0;
  42. const char *name;
  43. printf("usage: ffhash [algorithm] [input]...\n");
  44. printf("Supported hash algorithms:");
  45. do {
  46. name = av_hash_names(i);
  47. if (name)
  48. printf(" %s", name);
  49. i++;
  50. } while(name);
  51. printf("\n");
  52. }
  53. static void finish(void)
  54. {
  55. int i, len = av_hash_get_size(hash);
  56. printf("%s=0x", av_hash_get_name(hash));
  57. av_hash_final(hash, res);
  58. for (i = 0; i < len; i++)
  59. printf("%02x", res[i]);
  60. }
  61. static int check(char *file)
  62. {
  63. uint8_t buffer[SIZE];
  64. int fd, flags = O_RDONLY;
  65. int ret = 0;
  66. #ifdef O_BINARY
  67. flags |= O_BINARY;
  68. #endif
  69. if (file) fd = open(file, flags);
  70. else fd = 0;
  71. if (fd == -1) {
  72. printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
  73. ret = 1;
  74. goto end;
  75. }
  76. av_hash_init(hash);
  77. for (;;) {
  78. ssize_t size = read(fd, buffer, SIZE);
  79. if (size < 0) {
  80. close(fd);
  81. finish();
  82. printf("+READ-FAILED: %s", strerror(errno));
  83. ret = 2;
  84. goto end;
  85. } else if(!size)
  86. break;
  87. av_hash_update(hash, buffer, size);
  88. }
  89. close(fd);
  90. finish();
  91. end:
  92. if (file)
  93. printf(" *%s", file);
  94. printf("\n");
  95. return ret;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. int i;
  100. int ret = 0;
  101. if (argc == 1) {
  102. usage();
  103. return 0;
  104. }
  105. if ((ret = av_hash_alloc(&hash, argv[1])) < 0) {
  106. switch(ret) {
  107. case AVERROR(EINVAL):
  108. printf("Invalid hash type: %s\n", argv[1]);
  109. break;
  110. case AVERROR(ENOMEM):
  111. printf("%s\n", strerror(errno));
  112. break;
  113. }
  114. return 1;
  115. }
  116. res = av_malloc(av_hash_get_size(hash));
  117. if (!res) {
  118. printf("%s\n", strerror(errno));
  119. return 1;
  120. }
  121. for (i = 2; i < argc; i++)
  122. ret |= check(argv[i]);
  123. if (argc < 3)
  124. ret |= check(NULL);
  125. av_hash_freep(&hash);
  126. av_freep(&res);
  127. return ret;
  128. }