ENH: typo
[cmake.git] / Utilities / cmcurl / escape.c
blobcce8b156ac8e85195e2aa7de514313f1743fe516
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: escape.c,v 1.2 2007-03-15 19:22:13 andy Exp $
22 ***************************************************************************/
24 /* Escape and unescape URL encoding in strings. The functions return a new
25 * allocated string or NULL if an error occurred. */
27 #include "setup.h"
28 #include <ctype.h>
29 #include <curl/curl.h>
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"
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
42 /* The last #include file should be: */
43 #include "memdebug.h"
45 /* for ABI-compatibility with previous versions */
46 char *curl_escape(const char *string, int inlength)
48 return curl_easy_escape(NULL, string, inlength);
51 /* for ABI-compatibility with previous versions */
52 char *curl_unescape(const char *string, int length)
54 return curl_easy_unescape(NULL, string, length, NULL);
57 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
59 size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
60 char *ns;
61 char *testing_ptr = NULL;
62 unsigned char in;
63 size_t newlen = alloc;
64 int strindex=0;
65 size_t length;
67 #ifndef CURL_DOES_CONVERSIONS
68 /* avoid compiler warnings */
69 (void)handle;
70 #endif
71 ns = malloc(alloc);
72 if(!ns)
73 return NULL;
75 length = alloc-1;
76 while(length--) {
77 in = *string;
78 if(!(in >= 'a' && in <= 'z') &&
79 !(in >= 'A' && in <= 'Z') &&
80 !(in >= '0' && in <= '9')) {
81 /* encode it */
82 newlen += 2; /* the size grows with two, since this'll become a %XX */
83 if(newlen > alloc) {
84 alloc *= 2;
85 testing_ptr = realloc(ns, alloc);
86 if(!testing_ptr) {
87 free( ns );
88 return NULL;
90 else {
91 ns = testing_ptr;
95 #ifdef CURL_DOES_CONVERSIONS
96 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
97 if (!handle ||
98 (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
99 /* Curl_convert_to_network calls failf if unsuccessful */
100 free(ns);
101 return NULL;
103 #endif /* CURL_DOES_CONVERSIONS */
105 snprintf(&ns[strindex], 4, "%%%02X", in);
107 strindex+=3;
109 else {
110 /* just copy this */
111 ns[strindex++]=in;
113 string++;
115 ns[strindex]=0; /* terminate it */
116 return ns;
119 char *curl_easy_unescape(CURL *handle, const char *string, int length,
120 int *olen)
122 int alloc = (length?length:(int)strlen(string))+1;
123 char *ns = malloc(alloc);
124 unsigned char in;
125 int strindex=0;
126 long hex;
128 #ifndef CURL_DOES_CONVERSIONS
129 /* avoid compiler warnings */
130 (void)handle;
131 #endif
132 if( !ns )
133 return NULL;
135 while(--alloc > 0) {
136 in = *string;
137 if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
138 /* this is two hexadecimal digits following a '%' */
139 char hexstr[3];
140 char *ptr;
141 hexstr[0] = string[1];
142 hexstr[1] = string[2];
143 hexstr[2] = 0;
145 hex = strtol(hexstr, &ptr, 16);
147 in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
149 #ifdef CURL_DOES_CONVERSIONS
150 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
151 if (!handle ||
152 (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
153 /* Curl_convert_from_network calls failf if unsuccessful */
154 free(ns);
155 return NULL;
157 #endif /* CURL_DOES_CONVERSIONS */
159 string+=2;
160 alloc-=2;
163 ns[strindex++] = in;
164 string++;
166 ns[strindex]=0; /* terminate it */
168 if(olen)
169 /* store output size */
170 *olen = strindex;
171 return ns;
174 /* For operating systems/environments that use different malloc/free
175 ssystems for the app and for this library, we provide a free that uses
176 the library's memory system */
177 void curl_free(void *p)
179 if(p)
180 free(p);