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