ffhash.c 3.3 KB

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