md5proto.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (err)
  64. return err;
  65. err = ffurl_write(out, buf, i*2+1);
  66. ffurl_close(out);
  67. } else {
  68. if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
  69. err = AVERROR(errno);
  70. }
  71. av_freep(&c->md5);
  72. return err;
  73. }
  74. URLProtocol ff_md5_protocol = {
  75. .name = "md5",
  76. .url_open = md5_open,
  77. .url_write = md5_write,
  78. .url_close = md5_close,
  79. .priv_data_size = sizeof(struct MD5Context),
  80. };