md5proto.c 2.4 KB

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