Git init
[external/curl.git] / lib / escape.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, 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  ***************************************************************************/
22
23 /* Escape and unescape URL encoding in strings. The functions return a new
24  * allocated string or NULL if an error occurred.  */
25
26 #include "setup.h"
27 #include <ctype.h>
28 #include <curl/curl.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "curl_memory.h"
34 /* urldata.h and easyif.h are included for Curl_convert_... prototypes */
35 #include "urldata.h"
36 #include "easyif.h"
37 #include "warnless.h"
38
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
41
42 /* The last #include file should be: */
43 #include "memdebug.h"
44
45 /* Portable character check (remember EBCDIC). Do not use isalnum() because
46    its behavior is altered by the current locale.
47    See http://tools.ietf.org/html/rfc3986#section-2.3
48 */
49 static bool Curl_isunreserved(unsigned char in)
50 {
51   switch (in) {
52     case '0': case '1': case '2': case '3': case '4':
53     case '5': case '6': case '7': case '8': case '9':
54     case 'a': case 'b': case 'c': case 'd': case 'e':
55     case 'f': case 'g': case 'h': case 'i': case 'j':
56     case 'k': case 'l': case 'm': case 'n': case 'o':
57     case 'p': case 'q': case 'r': case 's': case 't':
58     case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
59     case 'A': case 'B': case 'C': case 'D': case 'E':
60     case 'F': case 'G': case 'H': case 'I': case 'J':
61     case 'K': case 'L': case 'M': case 'N': case 'O':
62     case 'P': case 'Q': case 'R': case 'S': case 'T':
63     case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
64     case '-': case '.': case '_': case '~':
65       return TRUE;
66     default:
67       break;
68   }
69   return FALSE;
70 }
71
72 /* for ABI-compatibility with previous versions */
73 char *curl_escape(const char *string, int inlength)
74 {
75   return curl_easy_escape(NULL, string, inlength);
76 }
77
78 /* for ABI-compatibility with previous versions */
79 char *curl_unescape(const char *string, int length)
80 {
81   return curl_easy_unescape(NULL, string, length, NULL);
82 }
83
84 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
85 {
86   size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
87   char *ns;
88   char *testing_ptr = NULL;
89   unsigned char in; /* we need to treat the characters unsigned */
90   size_t newlen = alloc;
91   int strindex=0;
92   size_t length;
93
94 #ifndef CURL_DOES_CONVERSIONS
95   /* avoid compiler warnings */
96   (void)handle;
97 #endif
98   ns = malloc(alloc);
99   if(!ns)
100     return NULL;
101
102   length = alloc-1;
103   while(length--) {
104     in = *string;
105
106     if (Curl_isunreserved(in)) {
107       /* just copy this */
108       ns[strindex++]=in;
109     }
110     else {
111       /* encode it */
112       newlen += 2; /* the size grows with two, since this'll become a %XX */
113       if(newlen > alloc) {
114         alloc *= 2;
115         testing_ptr = realloc(ns, alloc);
116         if(!testing_ptr) {
117           free( ns );
118           return NULL;
119         }
120         else {
121           ns = testing_ptr;
122         }
123       }
124
125 #ifdef CURL_DOES_CONVERSIONS
126 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
127       if(!handle ||
128           (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
129         /* Curl_convert_to_network calls failf if unsuccessful */
130         free(ns);
131         return NULL;
132       }
133 #endif /* CURL_DOES_CONVERSIONS */
134
135       snprintf(&ns[strindex], 4, "%%%02X", in);
136
137       strindex+=3;
138     }
139     string++;
140   }
141   ns[strindex]=0; /* terminate it */
142   return ns;
143 }
144
145 /*
146  * Unescapes the given URL escaped string of given length. Returns a
147  * pointer to a malloced string with length given in *olen.
148  * If length == 0, the length is assumed to be strlen(string).
149  * If olen == NULL, no output length is stored.
150  */
151 char *curl_easy_unescape(CURL *handle, const char *string, int length,
152                          int *olen)
153 {
154   int alloc = (length?length:(int)strlen(string))+1;
155   char *ns = malloc(alloc);
156   unsigned char in;
157   int strindex=0;
158   unsigned long hex;
159
160 #ifndef CURL_DOES_CONVERSIONS
161   /* avoid compiler warnings */
162   (void)handle;
163 #endif
164   if( !ns )
165     return NULL;
166
167   while(--alloc > 0) {
168     in = *string;
169     if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
170       /* this is two hexadecimal digits following a '%' */
171       char hexstr[3];
172       char *ptr;
173       hexstr[0] = string[1];
174       hexstr[1] = string[2];
175       hexstr[2] = 0;
176
177       hex = strtoul(hexstr, &ptr, 16);
178
179       in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
180
181 #ifdef CURL_DOES_CONVERSIONS
182 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
183       if(!handle ||
184           (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
185         /* Curl_convert_from_network calls failf if unsuccessful */
186         free(ns);
187         return NULL;
188       }
189 #endif /* CURL_DOES_CONVERSIONS */
190
191       string+=2;
192       alloc-=2;
193     }
194
195     ns[strindex++] = in;
196     string++;
197   }
198   ns[strindex]=0; /* terminate it */
199
200   if(olen)
201     /* store output size */
202     *olen = strindex;
203   return ns;
204 }
205
206 /* For operating systems/environments that use different malloc/free
207    systems for the app and for this library, we provide a free that uses
208    the library's memory system */
209 void curl_free(void *p)
210 {
211   if(p)
212     free(p);
213 }