escape.c 5.5 KB

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