]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/curl/lib/escape.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / curl / lib / escape.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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  ***************************************************************************/
22
23 /* Escape and unescape URL encoding in strings. The functions return a new
24  * allocated string or NULL if an error occurred.  */
25
26 #include "curl_setup.h"
27
28 #include <curl/curl.h>
29
30 #include "curl_memory.h"
31 #include "urldata.h"
32 #include "warnless.h"
33 #include "non-ascii.h"
34 #include "escape.h"
35
36 #define _MPRINTF_REPLACE /* use our functions only */
37 #include <curl/mprintf.h>
38
39 /* The last #include file should be: */
40 #include "memdebug.h"
41
42 /* Portable character check (remember EBCDIC). Do not use isalnum() because
43    its behavior is altered by the current locale.
44    See http://tools.ietf.org/html/rfc3986#section-2.3
45 */
46 static bool Curl_isunreserved(unsigned char in)
47 {
48   switch (in) {
49     case '0': case '1': case '2': case '3': case '4':
50     case '5': case '6': case '7': case '8': case '9':
51     case 'a': case 'b': case 'c': case 'd': case 'e':
52     case 'f': case 'g': case 'h': case 'i': case 'j':
53     case 'k': case 'l': case 'm': case 'n': case 'o':
54     case 'p': case 'q': case 'r': case 's': case 't':
55     case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
56     case 'A': case 'B': case 'C': case 'D': case 'E':
57     case 'F': case 'G': case 'H': case 'I': case 'J':
58     case 'K': case 'L': case 'M': case 'N': case 'O':
59     case 'P': case 'Q': case 'R': case 'S': case 'T':
60     case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
61     case '-': case '.': case '_': case '~':
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   size_t strindex=0;
89   size_t length;
90   CURLcode res;
91
92   ns = malloc(alloc);
93   if(!ns)
94     return NULL;
95
96   length = alloc-1;
97   while(length--) {
98     in = *string;
99
100     if(Curl_isunreserved(in))
101       /* just copy this */
102       ns[strindex++]=in;
103     else {
104       /* encode it */
105       newlen += 2; /* the size grows with two, since this'll become a %XX */
106       if(newlen > alloc) {
107         alloc *= 2;
108         testing_ptr = realloc(ns, alloc);
109         if(!testing_ptr) {
110           free( ns );
111           return NULL;
112         }
113         else {
114           ns = testing_ptr;
115         }
116       }
117
118       res = Curl_convert_to_network(handle, &in, 1);
119       if(res) {
120         /* Curl_convert_to_network calls failf if unsuccessful */
121         free(ns);
122         return NULL;
123       }
124
125       snprintf(&ns[strindex], 4, "%%%02X", in);
126
127       strindex+=3;
128     }
129     string++;
130   }
131   ns[strindex]=0; /* terminate it */
132   return ns;
133 }
134
135 /*
136  * Curl_urldecode() URL decodes the given string.
137  *
138  * Optionally detects control characters (byte codes lower than 32) in the
139  * data and rejects such data.
140  *
141  * Returns a pointer to a malloced string in *ostring with length given in
142  * *olen. If length == 0, the length is assumed to be strlen(string).
143  *
144  */
145 CURLcode Curl_urldecode(struct SessionHandle *data,
146                         const char *string, size_t length,
147                         char **ostring, size_t *olen,
148                         bool reject_ctrl)
149 {
150   size_t alloc = (length?length:strlen(string))+1;
151   char *ns = malloc(alloc);
152   unsigned char in;
153   size_t strindex=0;
154   unsigned long hex;
155   CURLcode res;
156
157   if(!ns)
158     return CURLE_OUT_OF_MEMORY;
159
160   while(--alloc > 0) {
161     in = *string;
162     if(('%' == in) && (alloc > 2) &&
163        ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
164       /* this is two hexadecimal digits following a '%' */
165       char hexstr[3];
166       char *ptr;
167       hexstr[0] = string[1];
168       hexstr[1] = string[2];
169       hexstr[2] = 0;
170
171       hex = strtoul(hexstr, &ptr, 16);
172
173       in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
174
175       res = Curl_convert_from_network(data, &in, 1);
176       if(res) {
177         /* Curl_convert_from_network calls failf if unsuccessful */
178         free(ns);
179         return res;
180       }
181
182       string+=2;
183       alloc-=2;
184     }
185     if(reject_ctrl && (in < 0x20)) {
186       free(ns);
187       return CURLE_URL_MALFORMAT;
188     }
189
190     ns[strindex++] = in;
191     string++;
192   }
193   ns[strindex]=0; /* terminate it */
194
195   if(olen)
196     /* store output size */
197     *olen = strindex;
198
199   if(ostring)
200     /* store output string */
201     *ostring = ns;
202
203   return CURLE_OK;
204 }
205
206 /*
207  * Unescapes the given URL escaped string of given length. Returns a
208  * pointer to a malloced string with length given in *olen.
209  * If length == 0, the length is assumed to be strlen(string).
210  * If olen == NULL, no output length is stored.
211  */
212 char *curl_easy_unescape(CURL *handle, const char *string, int length,
213                          int *olen)
214 {
215   char *str = NULL;
216   size_t inputlen = length;
217   size_t outputlen;
218   CURLcode res = Curl_urldecode(handle, string, inputlen, &str, &outputlen,
219                                 FALSE);
220   if(res)
221     return NULL;
222   if(olen)
223     *olen = curlx_uztosi(outputlen);
224   return str;
225 }
226
227 /* For operating systems/environments that use different malloc/free
228    systems for the app and for this library, we provide a free that uses
229    the library's memory system */
230 void curl_free(void *p)
231 {
232   if(p)
233     free(p);
234 }