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