escape.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 <ctype.h>
  26. #include <curl/curl.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "curl_memory.h"
  31. /* urldata.h and easyif.h are included for Curl_convert_... prototypes */
  32. #include "urldata.h"
  33. #include "easyif.h"
  34. #include "warnless.h"
  35. #define _MPRINTF_REPLACE /* use our functions only */
  36. #include <curl/mprintf.h>
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. /* Portable character check (remember EBCDIC). Do not use isalnum() because
  40. its behavior is altered by the current locale.
  41. See http://tools.ietf.org/html/rfc3986#section-2.3
  42. */
  43. static bool Curl_isunreserved(unsigned char in)
  44. {
  45. switch (in) {
  46. case '0': case '1': case '2': case '3': case '4':
  47. case '5': case '6': case '7': case '8': case '9':
  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 'A': case 'B': case 'C': case 'D': case 'E':
  54. case 'F': case 'G': case 'H': case 'I': case 'J':
  55. case 'K': case 'L': case 'M': case 'N': case 'O':
  56. case 'P': case 'Q': case 'R': case 'S': case 'T':
  57. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  58. case '-': case '.': case '_': case '~':
  59. return TRUE;
  60. default:
  61. break;
  62. }
  63. return FALSE;
  64. }
  65. /* for ABI-compatibility with previous versions */
  66. char *curl_escape(const char *string, int inlength)
  67. {
  68. return curl_easy_escape(NULL, string, inlength);
  69. }
  70. /* for ABI-compatibility with previous versions */
  71. char *curl_unescape(const char *string, int length)
  72. {
  73. return curl_easy_unescape(NULL, string, length, NULL);
  74. }
  75. char *curl_easy_escape(CURL *handle, const char *string, int inlength)
  76. {
  77. size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
  78. char *ns;
  79. char *testing_ptr = NULL;
  80. unsigned char in; /* we need to treat the characters unsigned */
  81. size_t newlen = alloc;
  82. int strindex=0;
  83. size_t length;
  84. #ifndef CURL_DOES_CONVERSIONS
  85. /* avoid compiler warnings */
  86. (void)handle;
  87. #endif
  88. ns = malloc(alloc);
  89. if(!ns)
  90. return NULL;
  91. length = alloc-1;
  92. while(length--) {
  93. in = *string;
  94. if (Curl_isunreserved(in)) {
  95. /* just copy this */
  96. ns[strindex++]=in;
  97. }
  98. else {
  99. /* encode it */
  100. newlen += 2; /* the size grows with two, since this'll become a %XX */
  101. if(newlen > alloc) {
  102. alloc *= 2;
  103. testing_ptr = realloc(ns, alloc);
  104. if(!testing_ptr) {
  105. free( ns );
  106. return NULL;
  107. }
  108. else {
  109. ns = testing_ptr;
  110. }
  111. }
  112. #ifdef CURL_DOES_CONVERSIONS
  113. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  114. if(!handle ||
  115. (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
  116. /* Curl_convert_to_network calls failf if unsuccessful */
  117. free(ns);
  118. return NULL;
  119. }
  120. #endif /* CURL_DOES_CONVERSIONS */
  121. snprintf(&ns[strindex], 4, "%%%02X", in);
  122. strindex+=3;
  123. }
  124. string++;
  125. }
  126. ns[strindex]=0; /* terminate it */
  127. return ns;
  128. }
  129. /*
  130. * Unescapes the given URL escaped string of given length. Returns a
  131. * pointer to a malloced string with length given in *olen.
  132. * If length == 0, the length is assumed to be strlen(string).
  133. * If olen == NULL, no output length is stored.
  134. */
  135. char *curl_easy_unescape(CURL *handle, const char *string, int length,
  136. int *olen)
  137. {
  138. int alloc = (length?length:(int)strlen(string))+1;
  139. char *ns = malloc(alloc);
  140. unsigned char in;
  141. int strindex=0;
  142. unsigned long hex;
  143. #ifndef CURL_DOES_CONVERSIONS
  144. /* avoid compiler warnings */
  145. (void)handle;
  146. #endif
  147. if( !ns )
  148. return NULL;
  149. while(--alloc > 0) {
  150. in = *string;
  151. if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  152. /* this is two hexadecimal digits following a '%' */
  153. char hexstr[3];
  154. char *ptr;
  155. hexstr[0] = string[1];
  156. hexstr[1] = string[2];
  157. hexstr[2] = 0;
  158. hex = strtoul(hexstr, &ptr, 16);
  159. in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
  160. #ifdef CURL_DOES_CONVERSIONS
  161. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  162. if(!handle ||
  163. (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
  164. /* Curl_convert_from_network calls failf if unsuccessful */
  165. free(ns);
  166. return NULL;
  167. }
  168. #endif /* CURL_DOES_CONVERSIONS */
  169. string+=2;
  170. alloc-=2;
  171. }
  172. ns[strindex++] = in;
  173. string++;
  174. }
  175. ns[strindex]=0; /* terminate it */
  176. if(olen)
  177. /* store output size */
  178. *olen = strindex;
  179. return ns;
  180. }
  181. /* For operating systems/environments that use different malloc/free
  182. systems for the app and for this library, we provide a free that uses
  183. the library's memory system */
  184. void curl_free(void *p)
  185. {
  186. if(p)
  187. free(p);
  188. }