if () => if()
[platform/upstream/curl.git] / lib / escape.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, 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 "memory.h"
35 /* urldata.h and easyif.h are included for Curl_convert_... prototypes */
36 #include "urldata.h"
37 #include "easyif.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 /* for ABI-compatibility with previous versions */
46 char *curl_escape(const char *string, int inlength)
47 {
48   return curl_easy_escape(NULL, string, inlength);
49 }
50
51 /* for ABI-compatibility with previous versions */
52 char *curl_unescape(const char *string, int length)
53 {
54   return curl_easy_unescape(NULL, string, length, NULL);
55 }
56
57 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
58 {
59   size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
60   char *ns;
61   char *testing_ptr = NULL;
62   unsigned char in; /* we need to treat the characters unsigned */
63   size_t newlen = alloc;
64   int strindex=0;
65   size_t length;
66
67 #ifndef CURL_DOES_CONVERSIONS
68   /* avoid compiler warnings */
69   (void)handle;
70 #endif
71   ns = malloc(alloc);
72   if(!ns)
73     return NULL;
74
75   length = alloc-1;
76   while(length--) {
77     in = *string;
78
79     /* Portable character check (remember EBCDIC). Do not use isalnum() because
80        its behavior is altered by the current locale. */
81
82     switch (in) {
83     case '0': case '1': case '2': case '3': case '4':
84     case '5': case '6': case '7': case '8': case '9':
85     case 'a': case 'b': case 'c': case 'd': case 'e':
86     case 'f': case 'g': case 'h': case 'i': case 'j':
87     case 'k': case 'l': case 'm': case 'n': case 'o':
88     case 'p': case 'q': case 'r': case 's': case 't':
89     case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
90     case 'A': case 'B': case 'C': case 'D': case 'E':
91     case 'F': case 'G': case 'H': case 'I': case 'J':
92     case 'K': case 'L': case 'M': case 'N': case 'O':
93     case 'P': case 'Q': case 'R': case 'S': case 'T':
94     case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
95       /* just copy this */
96       ns[strindex++]=in;
97       break;
98     default:
99       /* encode it */
100       newlen += 2; /* the size grows with two, since this'll become a %XX */
101       if(newlen > alloc) {
102         alloc *= 2;
103         testing_ptr = realloc(ns, alloc);
104         if(!testing_ptr) {
105           free( ns );
106           return NULL;
107         }
108         else {
109           ns = testing_ptr;
110         }
111       }
112
113 #ifdef CURL_DOES_CONVERSIONS
114 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
115       if(!handle ||
116           (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
117         /* Curl_convert_to_network calls failf if unsuccessful */
118         free(ns);
119         return NULL;
120       }
121 #endif /* CURL_DOES_CONVERSIONS */
122
123       snprintf(&ns[strindex], 4, "%%%02X", in);
124
125       strindex+=3;
126       break;
127     }
128     string++;
129   }
130   ns[strindex]=0; /* terminate it */
131   return ns;
132 }
133
134 char *curl_easy_unescape(CURL *handle, const char *string, int length,
135                          int *olen)
136 {
137   int alloc = (length?length:(int)strlen(string))+1;
138   char *ns = malloc(alloc);
139   unsigned char in;
140   int strindex=0;
141   long hex;
142
143 #ifndef CURL_DOES_CONVERSIONS
144   /* avoid compiler warnings */
145   (void)handle;
146 #endif
147   if( !ns )
148     return NULL;
149
150   while(--alloc > 0) {
151     in = *string;
152     if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
153       /* this is two hexadecimal digits following a '%' */
154       char hexstr[3];
155       char *ptr;
156       hexstr[0] = string[1];
157       hexstr[1] = string[2];
158       hexstr[2] = 0;
159
160       hex = strtol(hexstr, &ptr, 16);
161
162       in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
163
164 #ifdef CURL_DOES_CONVERSIONS
165 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
166       if(!handle ||
167           (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
168         /* Curl_convert_from_network calls failf if unsuccessful */
169         free(ns);
170         return NULL;
171       }
172 #endif /* CURL_DOES_CONVERSIONS */
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    ssystems 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 }