gdi32: Try to parse font names without FreeType.
[wine/zf.git] / dlls / webservices / url.c
blobf2fbda83c1f40ac735b3c0b2506205e4995b7c1c
1 /*
2 * Copyright 2016 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "winuser.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/heap.h"
29 #include "wine/list.h"
30 #include "webservices_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
34 static WS_URL_SCHEME_TYPE scheme_type( const WCHAR *str, ULONG len )
36 if (len == ARRAY_SIZE( L"http" ) - 1 && !wcsnicmp( str, L"http", ARRAY_SIZE( L"http" ) - 1 ))
37 return WS_URL_HTTP_SCHEME_TYPE;
39 if (len == ARRAY_SIZE( L"https" ) - 1 && !wcsnicmp( str, L"https", ARRAY_SIZE( L"https" ) - 1 ))
40 return WS_URL_HTTPS_SCHEME_TYPE;
42 if (len == ARRAY_SIZE( L"net.tcp" ) - 1 && !wcsnicmp( str, L"net.tcp", ARRAY_SIZE( L"net.tcp" ) - 1 ))
43 return WS_URL_NETTCP_SCHEME_TYPE;
45 if (len == ARRAY_SIZE( L"soap.udp" ) - 1 && !wcsnicmp( str, L"soap.udp", ARRAY_SIZE( L"soap.udp" ) - 1 ))
46 return WS_URL_SOAPUDP_SCHEME_TYPE;
48 if (len == ARRAY_SIZE( L"net.pipe" ) - 1 && !wcsnicmp( str, L"net.pipe", ARRAY_SIZE( L"net.pipe" ) - 1 ))
49 return WS_URL_NETPIPE_SCHEME_TYPE;
51 return ~0u;
54 static USHORT default_port( WS_URL_SCHEME_TYPE scheme )
56 switch (scheme)
58 case WS_URL_HTTP_SCHEME_TYPE: return 80;
59 case WS_URL_HTTPS_SCHEME_TYPE: return 443;
60 case WS_URL_NETTCP_SCHEME_TYPE: return 808;
61 case WS_URL_SOAPUDP_SCHEME_TYPE:
62 case WS_URL_NETPIPE_SCHEME_TYPE: return 65535;
63 default:
64 ERR( "unhandled scheme %u\n", scheme );
65 return 0;
69 static unsigned char *strdup_utf8( const WCHAR *str, ULONG len, ULONG *ret_len )
71 unsigned char *ret;
72 *ret_len = WideCharToMultiByte( CP_UTF8, 0, str, len, NULL, 0, NULL, NULL );
73 if ((ret = heap_alloc( *ret_len )))
74 WideCharToMultiByte( CP_UTF8, 0, str, len, (char *)ret, *ret_len, NULL, NULL );
75 return ret;
78 static inline int url_decode_byte( char c1, char c2 )
80 int ret;
82 if (c1 >= '0' && c1 <= '9') ret = (c1 - '0') * 16;
83 else if (c1 >= 'a' && c1 <= 'f') ret = (c1 - 'a' + 10) * 16;
84 else if (c1 >= 'A' && c1 <= 'F') ret = (c1 - 'A' + 10) * 16;
85 else return -1;
87 if (c2 >= '0' && c2 <= '9') ret += c2 - '0';
88 else if (c2 >= 'a' && c2 <= 'f') ret += c2 - 'a' + 10;
89 else if (c2 >= 'A' && c2 <= 'F') ret += c2 - 'A' + 10;
90 else return -1;
92 return ret;
95 static WCHAR *url_decode( WCHAR *str, ULONG len, WS_HEAP *heap, ULONG *ret_len )
97 WCHAR *p = str, *q, *ret;
98 BOOL decode = FALSE, convert = FALSE;
99 ULONG i, len_utf8, len_left;
100 unsigned char *utf8, *r;
101 int b;
103 *ret_len = len;
104 for (i = 0; i < len; i++, p++)
106 if ((len - i) < 3) break;
107 if (p[0] == '%' && (b = url_decode_byte( p[1], p[2] )) != -1)
109 decode = TRUE;
110 if (b > 159)
112 convert = TRUE;
113 break;
115 *ret_len -= 2;
118 if (!decode) return str;
119 if (!convert)
121 if (!(q = ret = ws_alloc( heap, *ret_len * sizeof(WCHAR) ))) return NULL;
122 p = str;
123 while (len)
125 if (len >= 3 && p[0] == '%' && (b = url_decode_byte( p[1], p[2] )) != -1)
127 *q++ = b;
128 p += 3;
129 len -= 3;
131 else
133 *q++ = *p++;
134 len -= 1;
137 return ret;
140 if (!(r = utf8 = strdup_utf8( str, len, &len_utf8 ))) return NULL;
141 len_left = len_utf8;
142 while (len_left)
144 if (len_left >= 3 && r[0] == '%' && (b = url_decode_byte( r[1], r[2] )) != -1)
146 r[0] = b;
147 len_left -= 3;
148 memmove( r + 1, r + 3, len_left );
149 len_utf8 -= 2;
151 else len_left -= 1;
152 r++;
155 if (!(*ret_len = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, (char *)utf8,
156 len_utf8, NULL, 0 )))
158 WARN( "invalid UTF-8 sequence\n" );
159 heap_free( utf8 );
160 return NULL;
162 if ((ret = ws_alloc( heap, *ret_len * sizeof(WCHAR) )))
163 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8, len_utf8, ret, *ret_len );
165 heap_free( utf8 );
166 return ret;
169 /**************************************************************************
170 * WsDecodeUrl [webservices.@]
172 HRESULT WINAPI WsDecodeUrl( const WS_STRING *str, ULONG flags, WS_HEAP *heap, WS_URL **ret,
173 WS_ERROR *error )
175 HRESULT hr = WS_E_QUOTA_EXCEEDED;
176 WCHAR *p, *q, *decoded = NULL;
177 WS_HTTP_URL *url = NULL;
178 ULONG len, len_decoded, port = 0;
180 TRACE( "%s %08x %p %p %p\n", str ? debugstr_wn(str->chars, str->length) : "null", flags,
181 heap, ret, error );
182 if (error) FIXME( "ignoring error parameter\n" );
184 if (!str || !heap) return E_INVALIDARG;
185 if (!str->length) return WS_E_INVALID_FORMAT;
186 if (flags)
188 FIXME( "unimplemented flags %08x\n", flags );
189 return E_NOTIMPL;
191 if (!(decoded = url_decode( str->chars, str->length, heap, &len_decoded )) ||
192 !(url = ws_alloc( heap, sizeof(*url) ))) goto done;
194 hr = WS_E_INVALID_FORMAT;
196 p = q = decoded;
197 len = len_decoded;
198 while (len && *q != ':') { q++; len--; };
199 if (*q != ':') goto done;
200 if ((url->url.scheme = scheme_type( p, q - p )) == ~0u) goto done;
202 if (!--len || *++q != '/') goto done;
203 if (!--len || *++q != '/') goto done;
205 p = ++q; len--;
206 while (len && *q != '/' && *q != ':' && *q != '?' && *q != '#') { q++; len--; };
207 if (q == p) goto done;
208 url->host.length = q - p;
209 url->host.chars = p;
211 if (len && *q == ':')
213 p = ++q; len--;
214 while (len && '0' <= *q && *q <= '9')
216 if ((port = port * 10 + *q - '0') > 65535) goto done;
217 q++; len--;
219 url->port = port;
220 url->portAsString.length = q - p;
221 url->portAsString.chars = p;
223 if (!port)
225 url->port = default_port( url->url.scheme );
226 url->portAsString.length = 0;
227 url->portAsString.chars = NULL;
230 if (len && *q == '/')
232 p = q;
233 while (len && *q != '?') { q++; len--; };
234 url->path.length = q - p;
235 url->path.chars = p;
237 else url->path.length = 0;
239 if (len && *q == '?')
241 p = ++q; len--;
242 while (len && *q != '#') { q++; len--; };
243 url->query.length = q - p;
244 url->query.chars = p;
246 else url->query.length = 0;
248 if (len && *q == '#')
250 p = ++q; len--;
251 while (len && *q != '#') { q++; len--; };
252 url->fragment.length = q - p;
253 url->fragment.chars = p;
255 else url->fragment.length = 0;
257 *ret = (WS_URL *)url;
258 hr = S_OK;
260 done:
261 if (hr != S_OK)
263 if (decoded != str->chars) ws_free( heap, decoded, len_decoded );
264 ws_free( heap, url, sizeof(*url) );
266 TRACE( "returning %08x\n", hr );
267 return hr;
270 static const WCHAR *scheme_str( WS_URL_SCHEME_TYPE scheme, ULONG *len )
272 switch (scheme)
274 case WS_URL_HTTP_SCHEME_TYPE:
275 *len = ARRAY_SIZE( L"http" ) - 1;
276 return L"http";
278 case WS_URL_HTTPS_SCHEME_TYPE:
279 *len = ARRAY_SIZE( L"https" ) - 1;
280 return L"https";
282 case WS_URL_NETTCP_SCHEME_TYPE:
283 *len = ARRAY_SIZE( L"net.tcp" ) - 1;
284 return L"net.tcp";
286 case WS_URL_SOAPUDP_SCHEME_TYPE:
287 *len = ARRAY_SIZE( L"soap.udp" ) - 1;
288 return L"soap.udp";
290 case WS_URL_NETPIPE_SCHEME_TYPE:
291 *len = ARRAY_SIZE( L"net.pipe" ) - 1;
292 return L"net.pipe";
294 default:
295 ERR( "unhandled scheme %u\n", scheme );
296 return NULL;
300 static inline ULONG escape_size( unsigned char ch, const char *except )
302 const char *p = except;
303 while (*p)
305 if (*p == ch) return 1;
306 p++;
308 if ((ch >= 'a' && ch <= 'z' ) || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) return 1;
309 if (ch < 33 || ch > 126) return 3;
310 switch (ch)
312 case '/':
313 case '?':
314 case '"':
315 case '#':
316 case '%':
317 case '<':
318 case '>':
319 case '\\':
320 case '[':
321 case ']':
322 case '^':
323 case '`':
324 case '{':
325 case '|':
326 case '}':
327 return 3;
328 default:
329 return 1;
333 static HRESULT url_encode_size( const WCHAR *str, ULONG len, const char *except, ULONG *ret_len )
335 ULONG i, len_utf8;
336 BOOL convert = FALSE;
337 unsigned char *utf8;
339 *ret_len = 0;
340 for (i = 0; i < len; i++)
342 if (str[i] > 159)
344 convert = TRUE;
345 break;
347 *ret_len += escape_size( str[i], except );
349 if (!convert) return S_OK;
351 *ret_len = 0;
352 if (!(utf8 = strdup_utf8( str, len, &len_utf8 ))) return E_OUTOFMEMORY;
353 for (i = 0; i < len_utf8; i++) *ret_len += escape_size( utf8[i], except );
354 heap_free( utf8 );
356 return S_OK;
359 static ULONG url_encode_byte( unsigned char byte, const char *except, WCHAR *buf )
361 static const WCHAR hex[] = L"0123456789ABCDEF";
362 switch (escape_size( byte, except ))
364 case 3:
365 buf[0] = '%';
366 buf[1] = hex[(byte >> 4) & 0xf];
367 buf[2] = hex[byte & 0xf];
368 return 3;
370 case 1:
371 buf[0] = byte;
372 return 1;
374 default:
375 ERR( "unhandled escape size\n" );
376 return 0;
381 static HRESULT url_encode( const WCHAR *str, ULONG len, WCHAR *buf, const char *except, ULONG *ret_len )
383 HRESULT hr = S_OK;
384 ULONG i, len_utf8, len_enc;
385 BOOL convert = FALSE;
386 WCHAR *p = buf;
387 unsigned char *utf8;
389 *ret_len = 0;
390 for (i = 0; i < len; i++)
392 if (str[i] > 159)
394 convert = TRUE;
395 break;
397 len_enc = url_encode_byte( str[i], except, p );
398 *ret_len += len_enc;
399 p += len_enc;
401 if (!convert) return S_OK;
403 p = buf;
404 *ret_len = 0;
405 if (!(utf8 = strdup_utf8( str, len, &len_utf8 ))) return E_OUTOFMEMORY;
406 for (i = 0; i < len_utf8; i++)
408 len_enc = url_encode_byte( utf8[i], except, p );
409 *ret_len += len_enc;
410 p += len_enc;
413 heap_free( utf8 );
414 return hr;
417 /**************************************************************************
418 * WsEncodeUrl [webservices.@]
420 HRESULT WINAPI WsEncodeUrl( const WS_URL *base, ULONG flags, WS_HEAP *heap, WS_STRING *ret,
421 WS_ERROR *error )
423 ULONG len = 0, len_scheme, len_enc, ret_size = 0;
424 const WS_HTTP_URL *url = (const WS_HTTP_URL *)base;
425 const WCHAR *scheme;
426 WCHAR *str = NULL, *p, *q;
427 ULONG port = 0;
428 HRESULT hr = WS_E_INVALID_FORMAT;
430 TRACE( "%p %08x %p %p %p\n", base, flags, heap, ret, error );
431 if (error) FIXME( "ignoring error parameter\n" );
433 if (!url || !heap || !ret) return E_INVALIDARG;
434 if (flags)
436 FIXME( "unimplemented flags %08x\n", flags );
437 return E_NOTIMPL;
439 if (!(scheme = scheme_str( url->url.scheme, &len_scheme ))) goto done;
440 len = len_scheme + 3; /* '://' */
441 len += 6; /* ':65535' */
443 if ((hr = url_encode_size( url->host.chars, url->host.length, "", &len_enc )) != S_OK)
444 goto done;
445 len += len_enc;
447 if ((hr = url_encode_size( url->path.chars, url->path.length, "/", &len_enc )) != S_OK)
448 goto done;
449 len += len_enc;
451 if ((hr = url_encode_size( url->query.chars, url->query.length, "/?", &len_enc )) != S_OK)
452 goto done;
453 len += len_enc + 1; /* '?' */
455 if ((hr = url_encode_size( url->fragment.chars, url->fragment.length, "/?", &len_enc )) != S_OK)
456 goto done;
457 len += len_enc + 1; /* '#' */
459 ret_size = len * sizeof(WCHAR);
460 if (!(str = ws_alloc( heap, ret_size )))
462 hr = WS_E_QUOTA_EXCEEDED;
463 goto done;
466 memcpy( str, scheme, len_scheme * sizeof(WCHAR) );
467 p = str + len_scheme;
468 p[0] = ':';
469 p[1] = p[2] = '/';
470 p += 3;
472 if ((hr = url_encode( url->host.chars, url->host.length, p, "", &len_enc )) != S_OK)
473 goto done;
474 p += len_enc;
476 if (url->portAsString.length)
478 q = url->portAsString.chars;
479 len = url->portAsString.length;
480 while (len && '0' <= *q && *q <= '9')
482 if ((port = port * 10 + *q - '0') > 65535)
484 hr = WS_E_INVALID_FORMAT;
485 goto done;
487 q++; len--;
489 if (url->port && port != url->port)
491 hr = E_INVALIDARG;
492 goto done;
494 } else port = url->port;
496 if (port == default_port( url->url.scheme )) port = 0;
497 if (port)
499 WCHAR buf[7];
500 len = swprintf( buf, ARRAY_SIZE(buf), L":%u", port );
501 memcpy( p, buf, len * sizeof(WCHAR) );
502 p += len;
505 if ((hr = url_encode( url->path.chars, url->path.length, p, "/", &len_enc )) != S_OK)
506 goto done;
507 p += len_enc;
509 if (url->query.length)
511 *p++ = '?';
512 if ((hr = url_encode( url->query.chars, url->query.length, p, "/?", &len_enc )) != S_OK)
513 goto done;
514 p += len_enc;
517 if (url->fragment.length)
519 *p++ = '#';
520 if ((hr = url_encode( url->fragment.chars, url->fragment.length, p, "/?", &len_enc )) != S_OK)
521 goto done;
522 p += len_enc;
525 ret->length = p - str;
526 ret->chars = str;
527 hr = S_OK;
529 done:
530 if (hr != S_OK) ws_free( heap, str, ret_size );
531 TRACE( "returning %08x\n", hr );
532 return hr;