2
0

escape.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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. * $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. #include "memory.h"
  32. /* urldata.h and easyif.h are included for Curl_convert_... prototypes */
  33. #include "urldata.h"
  34. #include "easyif.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. static bool Curl_isalnum(unsigned char in)
  42. {
  43. switch (in) {
  44. case '0': case '1': case '2': case '3': case '4':
  45. case '5': case '6': case '7': case '8': case '9':
  46. case 'a': case 'b': case 'c': case 'd': case 'e':
  47. case 'f': case 'g': case 'h': case 'i': case 'j':
  48. case 'k': case 'l': case 'm': case 'n': case 'o':
  49. case 'p': case 'q': case 'r': case 's': case 't':
  50. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  51. case 'A': case 'B': case 'C': case 'D': case 'E':
  52. case 'F': case 'G': case 'H': case 'I': case 'J':
  53. case 'K': case 'L': case 'M': case 'N': case 'O':
  54. case 'P': case 'Q': case 'R': case 'S': case 'T':
  55. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  56. return TRUE;
  57. default:
  58. break;
  59. }
  60. return FALSE;
  61. }
  62. /* for ABI-compatibility with previous versions */
  63. char *curl_escape(const char *string, int inlength)
  64. {
  65. return curl_easy_escape(NULL, string, inlength);
  66. }
  67. /* for ABI-compatibility with previous versions */
  68. char *curl_unescape(const char *string, int length)
  69. {
  70. return curl_easy_unescape(NULL, string, length, NULL);
  71. }
  72. char *curl_easy_escape(CURL *handle, const char *string, int inlength)
  73. {
  74. size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
  75. char *ns;
  76. char *testing_ptr = NULL;
  77. unsigned char in; /* we need to treat the characters unsigned */
  78. size_t newlen = alloc;
  79. int strindex=0;
  80. size_t length;
  81. #ifndef CURL_DOES_CONVERSIONS
  82. /* avoid compiler warnings */
  83. (void)handle;
  84. #endif
  85. ns = malloc(alloc);
  86. if(!ns)
  87. return NULL;
  88. length = alloc-1;
  89. while(length--) {
  90. in = *string;
  91. if (Curl_isalnum(in)) {
  92. /* just copy this */
  93. ns[strindex++]=in;
  94. } else {
  95. /* encode it */
  96. newlen += 2; /* the size grows with two, since this'll become a %XX */
  97. if(newlen > alloc) {
  98. alloc *= 2;
  99. testing_ptr = realloc(ns, alloc);
  100. if(!testing_ptr) {
  101. free( ns );
  102. return NULL;
  103. }
  104. else {
  105. ns = testing_ptr;
  106. }
  107. }
  108. #ifdef CURL_DOES_CONVERSIONS
  109. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  110. if(!handle ||
  111. (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
  112. /* Curl_convert_to_network calls failf if unsuccessful */
  113. free(ns);
  114. return NULL;
  115. }
  116. #endif /* CURL_DOES_CONVERSIONS */
  117. snprintf(&ns[strindex], 4, "%%%02X", in);
  118. strindex+=3;
  119. }
  120. string++;
  121. }
  122. ns[strindex]=0; /* terminate it */
  123. return ns;
  124. }
  125. /*
  126. * Unescapes the given URL escaped string of given length. Returns a
  127. * pointer to a malloced string with length given in *olen.
  128. * If length == 0, the length is assumed to be strlen(string).
  129. * If olen == NULL, no output length is stored.
  130. */
  131. char *curl_easy_unescape(CURL *handle, const char *string, int length,
  132. int *olen)
  133. {
  134. int alloc = (length?length:(int)strlen(string))+1;
  135. char *ns = malloc(alloc);
  136. unsigned char in;
  137. int strindex=0;
  138. long hex;
  139. #ifndef CURL_DOES_CONVERSIONS
  140. /* avoid compiler warnings */
  141. (void)handle;
  142. #endif
  143. if( !ns )
  144. return NULL;
  145. while(--alloc > 0) {
  146. in = *string;
  147. if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  148. /* this is two hexadecimal digits following a '%' */
  149. char hexstr[3];
  150. char *ptr;
  151. hexstr[0] = string[1];
  152. hexstr[1] = string[2];
  153. hexstr[2] = 0;
  154. hex = strtol(hexstr, &ptr, 16);
  155. in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
  156. #ifdef CURL_DOES_CONVERSIONS
  157. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  158. if(!handle ||
  159. (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
  160. /* Curl_convert_from_network calls failf if unsuccessful */
  161. free(ns);
  162. return NULL;
  163. }
  164. #endif /* CURL_DOES_CONVERSIONS */
  165. string+=2;
  166. alloc-=2;
  167. }
  168. ns[strindex++] = in;
  169. string++;
  170. }
  171. ns[strindex]=0; /* terminate it */
  172. if(olen)
  173. /* store output size */
  174. *olen = strindex;
  175. return ns;
  176. }
  177. /* For operating systems/environments that use different malloc/free
  178. systems for the app and for this library, we provide a free that uses
  179. the library's memory system */
  180. void curl_free(void *p)
  181. {
  182. if(p)
  183. free(p);
  184. }