escape.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. *****************************************************************************/
  23. /* Escape and unescape URL encoding in strings. The functions return a new
  24. * allocated string or NULL if an error occurred. */
  25. #include "setup.h"
  26. #include <ctype.h>
  27. #include <curl/curl.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /* The last #include file should be: */
  32. #ifdef MALLOCDEBUG
  33. #include "memdebug.h"
  34. #endif
  35. char *curl_escape(const char *string, int length)
  36. {
  37. int alloc = (length?length:(int)strlen(string))+1;
  38. char *ns = malloc(alloc);
  39. unsigned char in;
  40. int newlen = alloc;
  41. int index=0;
  42. length = alloc-1;
  43. while(length--) {
  44. in = *string;
  45. if(' ' == in)
  46. ns[index++] = '+';
  47. else if(!(in >= 'a' && in <= 'z') &&
  48. !(in >= 'A' && in <= 'Z') &&
  49. !(in >= '0' && in <= '9')) {
  50. /* encode it */
  51. newlen += 2; /* the size grows with two, since this'll become a %XX */
  52. if(newlen > alloc) {
  53. alloc *= 2;
  54. ns = realloc(ns, alloc);
  55. if(!ns)
  56. return NULL;
  57. }
  58. sprintf(&ns[index], "%%%02X", in);
  59. index+=3;
  60. }
  61. else {
  62. /* just copy this */
  63. ns[index++]=in;
  64. }
  65. string++;
  66. }
  67. ns[index]=0; /* terminate it */
  68. return ns;
  69. }
  70. char *curl_unescape(const char *string, int length)
  71. {
  72. int alloc = (length?length:(int)strlen(string))+1;
  73. char *ns = malloc(alloc);
  74. unsigned char in;
  75. int index=0;
  76. unsigned int hex;
  77. char querypart=FALSE; /* everything to the right of a '?' letter is
  78. the "query part" where '+' should become ' '.
  79. RFC 2316, section 3.10 */
  80. while(--alloc > 0) {
  81. in = *string;
  82. if(querypart && ('+' == in))
  83. in = ' ';
  84. else if(!querypart && ('?' == in)) {
  85. /* we have "walked in" to the query part */
  86. querypart=TRUE;
  87. }
  88. else if('%' == in) {
  89. /* encoded part */
  90. if(sscanf(string+1, "%02X", &hex)) {
  91. in = hex;
  92. string+=2;
  93. alloc-=2;
  94. }
  95. }
  96. ns[index++] = in;
  97. string++;
  98. }
  99. ns[index]=0; /* terminate it */
  100. return ns;
  101. }
  102. /*
  103. * local variables:
  104. * eval: (load-file "../curl-mode.el")
  105. * end:
  106. * vim600: fdm=marker
  107. * vim: et sw=2 ts=2 sts=2 tw=78
  108. */