2
0

escape.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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. /* 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;
  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. if(!(in >= 'a' && in <= 'z') &&
  69. !(in >= 'A' && in <= 'Z') &&
  70. !(in >= '0' && in <= '9')) {
  71. /* encode it */
  72. newlen += 2; /* the size grows with two, since this'll become a %XX */
  73. if(newlen > alloc) {
  74. alloc *= 2;
  75. testing_ptr = realloc(ns, alloc);
  76. if(!testing_ptr) {
  77. free( ns );
  78. return NULL;
  79. }
  80. else {
  81. ns = testing_ptr;
  82. }
  83. }
  84. #ifdef CURL_DOES_CONVERSIONS
  85. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  86. if (!handle ||
  87. (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
  88. /* Curl_convert_to_network calls failf if unsuccessful */
  89. free(ns);
  90. return NULL;
  91. }
  92. #endif /* CURL_DOES_CONVERSIONS */
  93. snprintf(&ns[strindex], 4, "%%%02X", in);
  94. strindex+=3;
  95. }
  96. else {
  97. /* just copy this */
  98. ns[strindex++]=in;
  99. }
  100. string++;
  101. }
  102. ns[strindex]=0; /* terminate it */
  103. return ns;
  104. }
  105. char *curl_easy_unescape(CURL *handle, const char *string, int length,
  106. int *olen)
  107. {
  108. int alloc = (length?length:(int)strlen(string))+1;
  109. char *ns = malloc(alloc);
  110. unsigned char in;
  111. int strindex=0;
  112. long hex;
  113. #ifndef CURL_DOES_CONVERSIONS
  114. /* avoid compiler warnings */
  115. (void)handle;
  116. #endif
  117. if( !ns )
  118. return NULL;
  119. while(--alloc > 0) {
  120. in = *string;
  121. if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  122. /* this is two hexadecimal digits following a '%' */
  123. char hexstr[3];
  124. char *ptr;
  125. hexstr[0] = string[1];
  126. hexstr[1] = string[2];
  127. hexstr[2] = 0;
  128. hex = strtol(hexstr, &ptr, 16);
  129. in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
  130. #ifdef CURL_DOES_CONVERSIONS
  131. /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
  132. if (!handle ||
  133. (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
  134. /* Curl_convert_from_network calls failf if unsuccessful */
  135. free(ns);
  136. return NULL;
  137. }
  138. #endif /* CURL_DOES_CONVERSIONS */
  139. string+=2;
  140. alloc-=2;
  141. }
  142. ns[strindex++] = in;
  143. string++;
  144. }
  145. ns[strindex]=0; /* terminate it */
  146. if(olen)
  147. /* store output size */
  148. *olen = strindex;
  149. return ns;
  150. }
  151. /* For operating systems/environments that use different malloc/free
  152. ssystems for the app and for this library, we provide a free that uses
  153. the library's memory system */
  154. void curl_free(void *p)
  155. {
  156. if(p)
  157. free(p);
  158. }