md5proto.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2010 Mans Rullgard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #define PRIV_SIZE 128
  29. static int md5_open(URLContext *h, const char *filename, int flags)
  30. {
  31. if (PRIV_SIZE < av_md5_size) {
  32. av_log(NULL, AV_LOG_ERROR, "Insuffient size for MD5 context\n");
  33. return -1;
  34. }
  35. if (!(flags & AVIO_FLAG_WRITE))
  36. return AVERROR(EINVAL);
  37. av_md5_init(h->priv_data);
  38. return 0;
  39. }
  40. static int md5_write(URLContext *h, const unsigned char *buf, int size)
  41. {
  42. av_md5_update(h->priv_data, buf, size);
  43. return size;
  44. }
  45. static int md5_close(URLContext *h)
  46. {
  47. const char *filename = h->filename;
  48. uint8_t md5[16], buf[64];
  49. URLContext *out;
  50. int i, err = 0;
  51. av_md5_final(h->priv_data, md5);
  52. for (i = 0; i < sizeof(md5); i++)
  53. snprintf(buf + i*2, 3, "%02x", md5[i]);
  54. buf[i*2] = '\n';
  55. av_strstart(filename, "md5:", &filename);
  56. if (*filename) {
  57. err = ffurl_open(&out, filename, AVIO_FLAG_WRITE,
  58. &h->interrupt_callback, NULL);
  59. if (err)
  60. return err;
  61. err = ffurl_write(out, buf, i*2+1);
  62. ffurl_close(out);
  63. } else {
  64. if (fwrite(buf, 1, i*2+1, stdout) < i*2+1)
  65. err = AVERROR(errno);
  66. }
  67. return err;
  68. }
  69. URLProtocol ff_md5_protocol = {
  70. .name = "md5",
  71. .url_open = md5_open,
  72. .url_write = md5_write,
  73. .url_close = md5_close,
  74. .priv_data_size = PRIV_SIZE,
  75. };