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