#include "setup.h"
#include <ctype.h>
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "curl_memory.h"
#include "urldata.h"
#include "easyif.h"
#define _MPRINTF_REPLACE
#include <curl/mprintf.h>
#include "memdebug.h"
static bool Curl_isalnum(unsigned char in)
{
switch (in) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
return TRUE;
default:
break;
}
return FALSE;
}
char *curl_escape(const char *string, int inlength)
{
return curl_easy_escape(NULL, string, inlength);
}
char *curl_unescape(const char *string, int length)
{
return curl_easy_unescape(NULL, string, length, NULL);
}
char *curl_easy_escape(CURL *handle, const char *string, int inlength)
{
size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
char *ns;
char *testing_ptr = NULL;
unsigned char in;
size_t newlen = alloc;
int strindex=0;
size_t length;
#ifndef CURL_DOES_CONVERSIONS
(void)handle;
#endif
ns = malloc(alloc);
if(!ns)
return NULL;
length = alloc-1;
while(length--) {
in = *string;
if (Curl_isalnum(in)) {
ns[strindex++]=in;
} else {
newlen += 2;
if(newlen > alloc) {
alloc *= 2;
testing_ptr = realloc(ns, alloc);
if(!testing_ptr) {
free( ns );
return NULL;
}
else {
ns = testing_ptr;
}
}
#ifdef CURL_DOES_CONVERSIONS
if(!handle ||
(Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
free(ns);
return NULL;
}
#endif
snprintf(&ns[strindex], 4, "%%%02X", in);
strindex+=3;
}
string++;
}
ns[strindex]=0;
return ns;
}
char *curl_easy_unescape(CURL *handle, const char *string, int length,
int *olen)
{
int alloc = (length?length:(int)strlen(string))+1;
char *ns = malloc(alloc);
unsigned char in;
int strindex=0;
long hex;
#ifndef CURL_DOES_CONVERSIONS
(void)handle;
#endif
if( !ns )
return NULL;
while(--alloc > 0) {
in = *string;
if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
char hexstr[3];
char *ptr;
hexstr[0] = string[1];
hexstr[1] = string[2];
hexstr[2] = 0;
hex = strtol(hexstr, &ptr, 16);
in = (unsigned char)hex;
#ifdef CURL_DOES_CONVERSIONS
if(!handle ||
(Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
free(ns);
return NULL;
}
#endif
string+=2;
alloc-=2;
}
ns[strindex++] = in;
string++;
}
ns[strindex]=0;
if(olen)
*olen = strindex;
return ns;
}
void curl_free(void *p)
{
if(p)
free(p);
}