escape.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Escape and unescape URL encoding in strings. The functions return a new
  23. * allocated string or NULL if an error occurred. */
  24. #include "setup.h"
  25. #include <curl/curl.h>
  26. #include "curl_memory.h"
  27. #include "urldata.h"
  28. #include "warnless.h"
  29. #include "non-ascii.h"
  30. #include "escape.h"
  31. #define _MPRINTF_REPLACE /* use our functions only */
  32. #include <curl/mprintf.h>
  33. /* The last #include file should be: */
  34. #include "memdebug.h"
  35. /* Portable character check (remember EBCDIC). Do not use isalnum() because
  36. its behavior is altered by the current locale.
  37. See http://tools.ietf.org/html/rfc3986#section-2.3
  38. */
  39. static bool Curl_isunreserved(unsigned char in)
  40. {
  41. switch (in) {
  42. case '0': case '1': case '2': case '3': case '4':
  43. case '5': case '6': case '7': case '8': case '9':
  44. case 'a': case 'b': case 'c': case 'd': case 'e':
  45. case 'f': case 'g': case 'h': case 'i': case 'j':
  46. case 'k': case 'l': case 'm': case 'n': case 'o':
  47. case 'p': case 'q': case 'r': case 's': case 't':
  48. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  49. case 'A': case 'B': case 'C': case 'D': case 'E':
  50. case 'F': case 'G': case 'H': case 'I': case 'J':
  51. case 'K': case 'L': case 'M': case 'N': case 'O':
  52. case 'P': case 'Q': case 'R': case 'S': case 'T':
  53. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  54. case '-': case '.': case '_': case '~':
  55. return TRUE;
  56. default:
  57. break;
  58. }
  59. return FALSE;
  60. }
  61. /* for ABI-compatibility with previous versions */
  62. char *curl_escape(const char *string, int inlength)
  63. {
  64. return curl_easy_escape(NULL, string, inlength);
  65. }
  66. /* for ABI-compatibility with previous versions */
  67. char *curl_unescape(const char *string, int length)
  68. {
  69. return curl_easy_unescape(NULL, string, length, NULL);
  70. }
  71. char *curl_easy_escape(CURL *handle, const char *string, int inlength)
  72. {
  73. size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
  74. char *ns;
  75. char *testing_ptr = NULL;
  76. unsigned char in; /* we need to treat the characters unsigned */
  77. size_t newlen = alloc;
  78. size_t strindex=0;
  79. size_t length;
  80. CURLcode res;
  81. ns = malloc(alloc);
  82. if(!ns)
  83. return NULL;
  84. length = alloc-1;
  85. while(length--) {
  86. in = *string;
  87. if(Curl_isunreserved(in))
  88. /* just copy this */
  89. ns[strindex++]=in;
  90. else {
  91. /* encode it */
  92. newlen += 2; /* the size grows with two, since this'll become a %XX */
  93. if(newlen > alloc) {
  94. alloc *= 2;
  95. testing_ptr = realloc(ns, alloc);
  96. if(!testing_ptr) {
  97. free( ns );
  98. return NULL;
  99. }
  100. else {
  101. ns = testing_ptr;
  102. }
  103. }
  104. res = Curl_convert_to_network(handle, &in, 1);
  105. if(res) {
  106. /* Curl_convert_to_network calls failf if unsuccessful */
  107. free(ns);
  108. return NULL;
  109. }
  110. snprintf(&ns[strindex], 4, "%%%02X", in);
  111. strindex+=3;
  112. }
  113. string++;
  114. }
  115. ns[strindex]=0; /* terminate it */
  116. return ns;
  117. }
  118. /*
  119. * Curl_urldecode() URL decodes the given string.
  120. *
  121. * Optionally detects control characters (byte codes lower than 32) in the
  122. * data and rejects such data.
  123. *
  124. * Returns a pointer to a malloced string in *ostring with length given in
  125. * *olen. If length == 0, the length is assumed to be strlen(string).
  126. *
  127. */
  128. CURLcode Curl_urldecode(struct SessionHandle *data,
  129. const char *string, size_t length,
  130. char **ostring, size_t *olen,
  131. bool reject_ctrl)
  132. {
  133. size_t alloc = (length?length:strlen(string))+1;
  134. char *ns = malloc(alloc);
  135. unsigned char in;
  136. size_t strindex=0;
  137. unsigned long hex;
  138. CURLcode res;
  139. if(!ns)
  140. return CURLE_OUT_OF_MEMORY;
  141. while(--alloc > 0) {
  142. in = *string;
  143. if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  144. /* this is two hexadecimal digits following a '%' */
  145. char hexstr[3];
  146. char *ptr;
  147. hexstr[0] = string[1];
  148. hexstr[1] = string[2];
  149. hexstr[2] = 0;
  150. hex = strtoul(hexstr, &ptr, 16);
  151. in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
  152. res = Curl_convert_from_network(data, &in, 1);
  153. if(res) {
  154. /* Curl_convert_from_network calls failf if unsuccessful */
  155. free(ns);
  156. return res;
  157. }
  158. string+=2;
  159. alloc-=2;
  160. }
  161. if(reject_ctrl && (in < 0x20)) {
  162. free(ns);
  163. return CURLE_URL_MALFORMAT;
  164. }
  165. ns[strindex++] = in;
  166. string++;
  167. }
  168. ns[strindex]=0; /* terminate it */
  169. if(olen)
  170. /* store output size */
  171. *olen = strindex;
  172. if(ostring)
  173. /* store output string */
  174. *ostring = ns;
  175. return CURLE_OK;
  176. }
  177. /*
  178. * Unescapes the given URL escaped string of given length. Returns a
  179. * pointer to a malloced string with length given in *olen.
  180. * If length == 0, the length is assumed to be strlen(string).
  181. * If olen == NULL, no output length is stored.
  182. */
  183. char *curl_easy_unescape(CURL *handle, const char *string, int length,
  184. int *olen)
  185. {
  186. char *str = NULL;
  187. size_t inputlen = length;
  188. size_t outputlen;
  189. CURLcode res = Curl_urldecode(handle, string, inputlen, &str, &outputlen,
  190. FALSE);
  191. if(res)
  192. return NULL;
  193. if(olen)
  194. *olen = curlx_uztosi(outputlen);
  195. return str;
  196. }
  197. /* For operating systems/environments that use different malloc/free
  198. systems for the app and for this library, we provide a free that uses
  199. the library's memory system */
  200. void curl_free(void *p)
  201. {
  202. if(p)
  203. free(p);
  204. }