urldecode.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Simple URL decoding function
  3. * Copyright (c) 2012 Antti Seppälä
  4. *
  5. * References:
  6. * RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
  7. * T. Berners-Lee et al. The Internet Society, 2005
  8. *
  9. * based on http://www.icosaedro.it/apache/urldecode.c
  10. * from Umberto Salsi (salsi@icosaedro.it)
  11. *
  12. * This file is part of FFmpeg.
  13. *
  14. * FFmpeg is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * FFmpeg is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with FFmpeg; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include "libavutil/mem.h"
  31. #include "libavutil/avstring.h"
  32. #include "urldecode.h"
  33. char *ff_urldecode(const char *url)
  34. {
  35. int s = 0, d = 0, url_len = 0;
  36. char c;
  37. char *dest = NULL;
  38. if (!url)
  39. return NULL;
  40. url_len = strlen(url) + 1;
  41. dest = av_malloc(url_len);
  42. if (!dest)
  43. return NULL;
  44. while (s < url_len) {
  45. c = url[s++];
  46. if (c == '%' && s + 2 < url_len) {
  47. char c2 = url[s++];
  48. char c3 = url[s++];
  49. if (isxdigit(c2) && isxdigit(c3)) {
  50. c2 = av_tolower(c2);
  51. c3 = av_tolower(c3);
  52. if (c2 <= '9')
  53. c2 = c2 - '0';
  54. else
  55. c2 = c2 - 'a' + 10;
  56. if (c3 <= '9')
  57. c3 = c3 - '0';
  58. else
  59. c3 = c3 - 'a' + 10;
  60. dest[d++] = 16 * c2 + c3;
  61. } else { /* %zz or something other invalid */
  62. dest[d++] = c;
  63. dest[d++] = c2;
  64. dest[d++] = c3;
  65. }
  66. } else if (c == '+') {
  67. dest[d++] = ' ';
  68. } else {
  69. dest[d++] = c;
  70. }
  71. }
  72. return dest;
  73. }