You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.1KB

  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;
  65. int ret = 0;
  66. if (file) fd = open(file, O_RDONLY);
  67. else fd = 0;
  68. if (fd == -1) {
  69. printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
  70. ret = 1;
  71. goto end;
  72. }
  73. av_hash_init(hash);
  74. for (;;) {
  75. ssize_t size = read(fd, buffer, SIZE);
  76. if (size < 0) {
  77. finish();
  78. printf("+READ-FAILED: %s", strerror(errno));
  79. ret = 2;
  80. goto end;
  81. } else if(!size)
  82. break;
  83. av_hash_update(hash, buffer, size);
  84. }
  85. close(fd);
  86. finish();
  87. end:
  88. if (file)
  89. printf(" *%s", file);
  90. printf("\n");
  91. return ret;
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. int i;
  96. int ret = 0;
  97. if (argc == 1) {
  98. usage();
  99. return 0;
  100. }
  101. if ((ret = av_hash_alloc(&hash, argv[1])) < 0) {
  102. switch(ret) {
  103. case AVERROR(EINVAL):
  104. printf("Invalid hash type: %s\n", argv[1]);
  105. break;
  106. case AVERROR(ENOMEM):
  107. printf("%s\n", strerror(errno));
  108. break;
  109. }
  110. return 1;
  111. }
  112. res = av_malloc(av_hash_get_size(hash));
  113. if (!res) {
  114. printf("%s\n", strerror(errno));
  115. return 1;
  116. }
  117. for (i = 2; i < argc; i++)
  118. ret |= check(argv[i]);
  119. if (argc < 3)
  120. ret |= check(NULL);
  121. av_hash_freep(&hash);
  122. av_freep(&res);
  123. return ret;
  124. }