00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "setup.h"
00028 #include <ctype.h>
00029 #include <curl/curl.h>
00030
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include "memory.h"
00035
00036 #include "urldata.h"
00037 #include "easyif.h"
00038
00039 #define _MPRINTF_REPLACE
00040 #include <curl/mprintf.h>
00041
00042
00043 #include "memdebug.h"
00044
00045
00046 char *curl_escape(const char *string, int inlength)
00047 {
00048 return curl_easy_escape(NULL, string, inlength);
00049 }
00050
00051
00052 char *curl_unescape(const char *string, int length)
00053 {
00054 return curl_easy_unescape(NULL, string, length, NULL);
00055 }
00056
00057 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
00058 {
00059 size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
00060 char *ns;
00061 char *testing_ptr = NULL;
00062 unsigned char in;
00063 size_t newlen = alloc;
00064 int strindex=0;
00065 size_t length;
00066
00067 #ifndef CURL_DOES_CONVERSIONS
00068
00069 (void)handle;
00070 #endif
00071 ns = malloc(alloc);
00072 if(!ns)
00073 return NULL;
00074
00075 length = alloc-1;
00076 while(length--) {
00077 in = *string;
00078 if(!(in >= 'a' && in <= 'z') &&
00079 !(in >= 'A' && in <= 'Z') &&
00080 !(in >= '0' && in <= '9')) {
00081
00082 newlen += 2;
00083 if(newlen > alloc) {
00084 alloc *= 2;
00085 testing_ptr = realloc(ns, alloc);
00086 if(!testing_ptr) {
00087 free( ns );
00088 return NULL;
00089 }
00090 else {
00091 ns = testing_ptr;
00092 }
00093 }
00094
00095 #ifdef CURL_DOES_CONVERSIONS
00096
00097 if (!handle ||
00098 (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
00099
00100 free(ns);
00101 return NULL;
00102 }
00103 #endif
00104
00105 snprintf(&ns[strindex], 4, "%%%02X", in);
00106
00107 strindex+=3;
00108 }
00109 else {
00110
00111 ns[strindex++]=in;
00112 }
00113 string++;
00114 }
00115 ns[strindex]=0;
00116 return ns;
00117 }
00118
00119 char *curl_easy_unescape(CURL *handle, const char *string, int length,
00120 int *olen)
00121 {
00122 int alloc = (length?length:(int)strlen(string))+1;
00123 char *ns = malloc(alloc);
00124 unsigned char in;
00125 int strindex=0;
00126 long hex;
00127
00128 #ifndef CURL_DOES_CONVERSIONS
00129
00130 (void)handle;
00131 #endif
00132 if( !ns )
00133 return NULL;
00134
00135 while(--alloc > 0) {
00136 in = *string;
00137 if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
00138
00139 char hexstr[3];
00140 char *ptr;
00141 hexstr[0] = string[1];
00142 hexstr[1] = string[2];
00143 hexstr[2] = 0;
00144
00145 hex = strtol(hexstr, &ptr, 16);
00146
00147 in = (unsigned char)hex;
00148
00149 #ifdef CURL_DOES_CONVERSIONS
00150
00151 if (!handle ||
00152 (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
00153
00154 free(ns);
00155 return NULL;
00156 }
00157 #endif
00158
00159 string+=2;
00160 alloc-=2;
00161 }
00162
00163 ns[strindex++] = in;
00164 string++;
00165 }
00166 ns[strindex]=0;
00167
00168 if(olen)
00169
00170 *olen = strindex;
00171 return ns;
00172 }
00173
00174
00175
00176
00177 void curl_free(void *p)
00178 {
00179 if(p)
00180 free(p);
00181 }