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.

97 lines
2.5KB

  1. /*
  2. * Copyright (c) 2010 Mans Rullgard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/md5.h"
  23. #include "libavutil/mem.h"
  24. #include "libavutil/error.h"
  25. #include "avformat.h"
  26. #include "avio.h"
  27. #include "url.h"
  28. struct MD5Context {
  29. struct AVMD5 *md5;
  30. };
  31. static int md5_open(URLContext *h, const char *filename, int flags)
  32. {
  33. struct MD5Context *c = h->priv_data;
  34. if (!(flags & AVIO_FLAG_WRITE))
  35. return AVERROR(EINVAL);
  36. c->md5 = av_md5_alloc();
  37. if (!c->md5)
  38. return AVERROR(ENOMEM);
  39. av_md5_init(c->md5);
  40. return 0;
  41. }
  42. static int md5_write(URLContext *h, const unsigned char *buf, int size)
  43. {
  44. struct MD5Context *c = h->priv_data;
  45. av_md5_update(c->md5, buf, size);
  46. return size;
  47. }
  48. static int md5_close(URLContext *h)
  49. {
  50. struct MD5Context *c = h->priv_data;
  51. const char *filename = h->filename;
  52. uint8_t md5[16], buf[64];
  53. URLContext *out;
  54. int i, err = 0;
  55. av_md5_final(c->md5, md5);
  56. for (i = 0; i < sizeof(md5); i++)
  57. snprintf(buf + i*2, 3, "%02x", md5[i]);
  58. buf[i*2] = '\n';
  59. av_strstart(filename, "md5:", &filename);
  60. if (*filename) {
  61. err = ffurl_open(&out, filename, AVIO_FLAG_WRITE,
  62. &h->interrupt_callback, NULL,
  63. h->protocols, h);
  64. if (err)
  65. return err;
  66. err = ffurl_write(out, buf, i*2+1);
  67. ffurl_close(out);
  68. } else {
  69. if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
  70. err = AVERROR(errno);
  71. }
  72. av_freep(&c->md5);
  73. return err;
  74. }
  75. const URLProtocol ff_md5_protocol = {
  76. .name = "md5",
  77. .url_open = md5_open,
  78. .url_write = md5_write,
  79. .url_close = md5_close,
  80. .priv_data_size = sizeof(struct MD5Context),
  81. };