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