escape.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, 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 "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "warnless.h"
  28. #include "non-ascii.h"
  29. #include "escape.h"
  30. #include "curl_printf.h"
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. /* Portable character check (remember EBCDIC). Do not use isalnum() because
  35. its behavior is altered by the current locale.
  36. See http://tools.ietf.org/html/rfc3986#section-2.3
  37. */
  38. static bool Curl_isunreserved(unsigned char in)
  39. {
  40. switch (in) {
  41. case '0': case '1': case '2': case '3': case '4':
  42. case '5': case '6': case '7': case '8': case '9':
  43. case 'a': case 'b': case 'c': case 'd': case 'e':
  44. case 'f': case 'g': case 'h': case 'i': case 'j':
  45. case 'k': case 'l': case 'm': case 'n': case 'o':
  46. case 'p': case 'q': case 'r': case 's': case 't':
  47. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  48. case 'A': case 'B': case 'C': case 'D': case 'E':
  49. case 'F': case 'G': case 'H': case 'I': case 'J':
  50. case 'K': case 'L': case 'M': case 'N': case 'O':
  51. case 'P': case 'Q': case 'R': case 'S': case 'T':
  52. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  53. case '-': case '.': case '_': case '~':
  54. return TRUE;
  55. default:
  56. break;
  57. }
  58. return FALSE;
  59. }
  60. /* for ABI-compatibility with previous versions */
  61. char *curl_escape(const char *string, int inlength)
  62. {
  63. return curl_easy_escape(NULL, string, inlength);
  64. }
  65. /* for ABI-compatibility with previous versions */
  66. char *curl_unescape(const char *string, int length)
  67. {
  68. return curl_easy_unescape(NULL, string, length, NULL);
  69. }
  70. char *curl_easy_escape(CURL *handle, const char *string, int inlength)
  71. {
  72. size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
  73. char *ns;
  74. char *testing_ptr = NULL;
  75. unsigned char in; /* we need to treat the characters unsigned */
  76. size_t newlen = alloc;
  77. size_t strindex=0;
  78. size_t length;
  79. CURLcode result;
  80. ns = malloc(alloc);
  81. if(!ns)
  82. return NULL;
  83. length = alloc-1;
  84. while(length--) {
  85. in = *string;
  86. if(Curl_isunreserved(in))
  87. /* just copy this */
  88. ns[strindex++]=in;
  89. else {
  90. /* encode it */
  91. newlen += 2; /* the size grows with two, since this'll become a %XX */
  92. if(newlen > alloc) {
  93. alloc *= 2;
  94. testing_ptr = realloc(ns, alloc);
  95. if(!testing_ptr) {
  96. free( ns );
  97. return NULL;
  98. }
  99. else {
  100. ns = testing_ptr;
  101. }
  102. }
  103. result = Curl_convert_to_network(handle, &in, 1);
  104. if(result) {
  105. /* Curl_convert_to_network calls failf if unsuccessful */
  106. free(ns);
  107. return NULL;
  108. }
  109. snprintf(&ns[strindex], 4, "%%%02X", in);
  110. strindex+=3;
  111. }
  112. string++;
  113. }
  114. ns[strindex]=0; /* terminate it */
  115. return ns;
  116. }
  117. /*
  118. * Curl_urldecode() URL decodes the given string.
  119. *
  120. * Optionally detects control characters (byte codes lower than 32) in the
  121. * data and rejects such data.
  122. *
  123. * Returns a pointer to a malloced string in *ostring with length given in
  124. * *olen. If length == 0, the length is assumed to be strlen(string).
  125. *
  126. */
  127. CURLcode Curl_urldecode(struct SessionHandle *data,
  128. const char *string, size_t length,
  129. char **ostring, size_t *olen,
  130. bool reject_ctrl)
  131. {
  132. size_t alloc = (length?length:strlen(string))+1;
  133. char *ns = malloc(alloc);
  134. unsigned char in;
  135. size_t strindex=0;
  136. unsigned long hex;
  137. CURLcode result;
  138. if(!ns)
  139. return CURLE_OUT_OF_MEMORY;
  140. while(--alloc > 0) {
  141. in = *string;
  142. if(('%' == in) && (alloc > 2) &&
  143. 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. result = Curl_convert_from_network(data, &in, 1);
  153. if(result) {
  154. /* Curl_convert_from_network calls failf if unsuccessful */
  155. free(ns);
  156. return result;
  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. /* store output string */
  173. *ostring = ns;
  174. return CURLE_OK;
  175. }
  176. /*
  177. * Unescapes the given URL escaped string of given length. Returns a
  178. * pointer to a malloced string with length given in *olen.
  179. * If length == 0, the length is assumed to be strlen(string).
  180. * If olen == NULL, no output length is stored.
  181. */
  182. char *curl_easy_unescape(CURL *handle, const char *string, int length,
  183. int *olen)
  184. {
  185. char *str = NULL;
  186. size_t inputlen = length;
  187. size_t outputlen;
  188. CURLcode res = Curl_urldecode(handle, string, inputlen, &str, &outputlen,
  189. FALSE);
  190. if(res)
  191. return NULL;
  192. if(olen)
  193. *olen = curlx_uztosi(outputlen);
  194. return str;
  195. }
  196. /* For operating systems/environments that use different malloc/free
  197. systems for the app and for this library, we provide a free that uses
  198. the library's memory system */
  199. void curl_free(void *p)
  200. {
  201. free(p);
  202. }