include: Make sure __int64 is correctly defined on PPC64.
[wine.git] / dlls / jsproxy / main.c
blob1fbaa5cccce19e26212bd51f9ec93277a2b3e302
1 /*
2 * Copyright 2014 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>
20 #include <sys/types.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winsock2.h"
25 #include "ws2ipdef.h"
26 #include "ws2tcpip.h"
27 #include "winnls.h"
28 #include "wininet.h"
29 #define COBJMACROS
30 #include "ole2.h"
31 #include "dispex.h"
32 #include "activscp.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 static HINSTANCE instance;
38 WINE_DEFAULT_DEBUG_CHANNEL(jsproxy);
40 static CRITICAL_SECTION cs_jsproxy;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
43 0, 0, &cs_jsproxy,
44 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": cs_jsproxy") }
47 static CRITICAL_SECTION cs_jsproxy = { &critsect_debug, -1, 0, 0, 0, 0 };
49 /******************************************************************
50 * DllMain (jsproxy.@)
52 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
54 switch (reason)
56 case DLL_PROCESS_ATTACH:
57 instance = hinst;
58 DisableThreadLibraryCalls( hinst );
59 break;
61 case DLL_PROCESS_DETACH:
62 break;
64 return TRUE;
67 static inline WCHAR *strdupAW( const char *src, int len )
69 WCHAR *dst = NULL;
70 if (src)
72 int dst_len = MultiByteToWideChar( CP_ACP, 0, src, len, NULL, 0 );
73 if ((dst = heap_alloc( (dst_len + 1) * sizeof(WCHAR) )))
75 len = MultiByteToWideChar( CP_ACP, 0, src, len, dst, dst_len );
76 dst[dst_len] = 0;
79 return dst;
82 static inline char *strdupWA( const WCHAR *src )
84 char *dst = NULL;
85 if (src)
87 int len = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
88 if ((dst = heap_alloc( len ))) WideCharToMultiByte( CP_ACP, 0, src, -1, dst, len, NULL, NULL );
90 return dst;
93 static struct pac_script
95 WCHAR *text;
96 } pac_script;
97 static struct pac_script *global_script = &pac_script;
99 /******************************************************************
100 * InternetDeInitializeAutoProxyDll (jsproxy.@)
102 BOOL WINAPI InternetDeInitializeAutoProxyDll( LPSTR mime, DWORD reserved )
104 TRACE( "%s, %u\n", debugstr_a(mime), reserved );
106 EnterCriticalSection( &cs_jsproxy );
108 heap_free( global_script->text );
109 global_script->text = NULL;
111 LeaveCriticalSection( &cs_jsproxy );
112 return TRUE;
115 static WCHAR *load_script( const char *filename )
117 HANDLE handle;
118 DWORD size, bytes_read;
119 char *buffer;
120 int len;
121 WCHAR *script = NULL;
123 handle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
124 if (handle == INVALID_HANDLE_VALUE) return NULL;
126 size = GetFileSize( handle, NULL );
127 if (!(buffer = heap_alloc( size ))) goto done;
128 if (!ReadFile( handle, buffer, size, &bytes_read, NULL ) || bytes_read != size) goto done;
130 len = MultiByteToWideChar( CP_ACP, 0, buffer, size, NULL, 0 );
131 if (!(script = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
132 MultiByteToWideChar( CP_ACP, 0, buffer, size, script, len );
133 script[len] = 0;
135 done:
136 CloseHandle( handle );
137 heap_free( buffer );
138 return script;
141 /******************************************************************
142 * InternetInitializeAutoProxyDll (jsproxy.@)
144 BOOL WINAPI JSPROXY_InternetInitializeAutoProxyDll( DWORD version, LPSTR tmpfile, LPSTR mime,
145 AutoProxyHelperFunctions *callbacks,
146 LPAUTO_PROXY_SCRIPT_BUFFER buffer )
148 BOOL ret = FALSE;
150 TRACE( "%u, %s, %s, %p, %p\n", version, debugstr_a(tmpfile), debugstr_a(mime), callbacks, buffer );
152 if (callbacks) FIXME( "callbacks not supported\n" );
154 EnterCriticalSection( &cs_jsproxy );
156 if (buffer && buffer->dwStructSize == sizeof(*buffer) && buffer->lpszScriptBuffer)
158 if (!buffer->dwScriptBufferSize)
160 SetLastError( ERROR_INVALID_PARAMETER );
161 LeaveCriticalSection( &cs_jsproxy );
162 return FALSE;
164 heap_free( global_script->text );
165 if ((global_script->text = strdupAW( buffer->lpszScriptBuffer,
166 buffer->dwScriptBufferSize ))) ret = TRUE;
168 else
170 heap_free( global_script->text );
171 if ((global_script->text = load_script( tmpfile ))) ret = TRUE;
174 LeaveCriticalSection( &cs_jsproxy );
175 return ret;
178 static HRESULT WINAPI dispex_QueryInterface(
179 IDispatchEx *iface, REFIID riid, void **ppv )
181 *ppv = NULL;
183 if (IsEqualGUID( riid, &IID_IUnknown ) ||
184 IsEqualGUID( riid, &IID_IDispatch ) ||
185 IsEqualGUID( riid, &IID_IDispatchEx ))
186 *ppv = iface;
187 else
188 return E_NOINTERFACE;
190 return S_OK;
193 static ULONG WINAPI dispex_AddRef(
194 IDispatchEx *iface )
196 return 2;
199 static ULONG WINAPI dispex_Release(
200 IDispatchEx *iface )
202 return 1;
205 static HRESULT WINAPI dispex_GetTypeInfoCount(
206 IDispatchEx *iface, UINT *info )
208 return E_NOTIMPL;
211 static HRESULT WINAPI dispex_GetTypeInfo(
212 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
214 return E_NOTIMPL;
217 static HRESULT WINAPI dispex_GetIDsOfNames(
218 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
220 return E_NOTIMPL;
223 static HRESULT WINAPI dispex_Invoke(
224 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
225 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
227 return E_NOTIMPL;
230 static HRESULT WINAPI dispex_DeleteMemberByName(
231 IDispatchEx *iface, BSTR name, DWORD flags )
233 return E_NOTIMPL;
236 static HRESULT WINAPI dispex_DeleteMemberByDispID(
237 IDispatchEx *iface, DISPID id )
239 return E_NOTIMPL;
242 static HRESULT WINAPI dispex_GetMemberProperties(
243 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
245 return E_NOTIMPL;
248 static HRESULT WINAPI dispex_GetMemberName(
249 IDispatchEx *iface, DISPID id, BSTR *name )
251 return E_NOTIMPL;
254 static HRESULT WINAPI dispex_GetNextDispID(
255 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
257 return E_NOTIMPL;
260 static HRESULT WINAPI dispex_GetNameSpaceParent(
261 IDispatchEx *iface, IUnknown **unk )
263 return E_NOTIMPL;
266 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
268 static HRESULT WINAPI dispex_GetDispID(
269 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
271 if (!lstrcmpW( name, L"dns_resolve" ))
273 *id = DISPID_GLOBAL_DNSRESOLVE;
274 return S_OK;
276 return DISP_E_UNKNOWNNAME;
279 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
281 char *ret;
282 DWORD size = 0;
284 GetComputerNameExA( format, NULL, &size );
285 if (GetLastError() != ERROR_MORE_DATA) return NULL;
286 if (!(ret = heap_alloc( size ))) return NULL;
287 if (!GetComputerNameExA( format, ret, &size ))
289 heap_free( ret );
290 return NULL;
292 return ret;
295 static void printf_addr( const WCHAR *fmt, WCHAR *buf, SIZE_T size, struct sockaddr_in *addr )
297 swprintf( buf, size, fmt,
298 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
299 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
300 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
301 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
304 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
306 WCHAR addr[16];
307 struct addrinfo *ai, *elem;
308 char *hostnameA;
309 int res;
311 if (hostname[0])
312 hostnameA = strdupWA( hostname );
313 else
314 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
316 if (!hostnameA) return E_OUTOFMEMORY;
317 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
318 heap_free( hostnameA );
319 if (res) return S_FALSE;
321 elem = ai;
322 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
323 if (!elem)
325 freeaddrinfo( ai );
326 return S_FALSE;
328 printf_addr( L"%u.%u.%u.%u", addr, ARRAY_SIZE(addr), (struct sockaddr_in *)elem->ai_addr );
329 freeaddrinfo( ai );
330 V_VT( result ) = VT_BSTR;
331 V_BSTR( result ) = SysAllocString( addr );
332 return S_OK;
335 static HRESULT WINAPI dispex_InvokeEx(
336 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
337 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
339 if (id == DISPID_GLOBAL_DNSRESOLVE)
341 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
342 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
343 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
345 return DISP_E_MEMBERNOTFOUND;
348 static const IDispatchExVtbl dispex_vtbl =
350 dispex_QueryInterface,
351 dispex_AddRef,
352 dispex_Release,
353 dispex_GetTypeInfoCount,
354 dispex_GetTypeInfo,
355 dispex_GetIDsOfNames,
356 dispex_Invoke,
357 dispex_GetDispID,
358 dispex_InvokeEx,
359 dispex_DeleteMemberByName,
360 dispex_DeleteMemberByDispID,
361 dispex_GetMemberProperties,
362 dispex_GetMemberName,
363 dispex_GetNextDispID,
364 dispex_GetNameSpaceParent
367 static IDispatchEx global_dispex = { &dispex_vtbl };
369 static HRESULT WINAPI site_QueryInterface(
370 IActiveScriptSite *iface, REFIID riid, void **ppv )
372 *ppv = NULL;
374 if (IsEqualGUID( &IID_IUnknown, riid ))
375 *ppv = iface;
376 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
377 *ppv = iface;
378 else
379 return E_NOINTERFACE;
381 IUnknown_AddRef( (IUnknown *)*ppv );
382 return S_OK;
385 static ULONG WINAPI site_AddRef(
386 IActiveScriptSite *iface )
388 return 2;
391 static ULONG WINAPI site_Release(
392 IActiveScriptSite *iface )
394 return 1;
397 static HRESULT WINAPI site_GetLCID(
398 IActiveScriptSite *iface, LCID *lcid )
400 return E_NOTIMPL;
403 static HRESULT WINAPI site_GetItemInfo(
404 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
405 IUnknown **item, ITypeInfo **type_info )
407 if (!lstrcmpW( name, L"global_funcs" ) && mask == SCRIPTINFO_IUNKNOWN)
409 *item = (IUnknown *)&global_dispex;
410 return S_OK;
412 return E_NOTIMPL;
415 static HRESULT WINAPI site_GetDocVersionString(
416 IActiveScriptSite *iface, BSTR *version )
418 return E_NOTIMPL;
421 static HRESULT WINAPI site_OnScriptTerminate(
422 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
424 return E_NOTIMPL;
427 static HRESULT WINAPI site_OnStateChange(
428 IActiveScriptSite *iface, SCRIPTSTATE state )
430 return E_NOTIMPL;
433 static HRESULT WINAPI site_OnScriptError(
434 IActiveScriptSite *iface, IActiveScriptError *error )
436 return E_NOTIMPL;
439 static HRESULT WINAPI site_OnEnterScript(
440 IActiveScriptSite *iface )
442 return E_NOTIMPL;
445 static HRESULT WINAPI site_OnLeaveScript(
446 IActiveScriptSite *iface )
448 return E_NOTIMPL;
451 static const IActiveScriptSiteVtbl site_vtbl =
453 site_QueryInterface,
454 site_AddRef,
455 site_Release,
456 site_GetLCID,
457 site_GetItemInfo,
458 site_GetDocVersionString,
459 site_OnScriptTerminate,
460 site_OnStateChange,
461 site_OnScriptError,
462 site_OnEnterScript,
463 site_OnLeaveScript
466 static IActiveScriptSite script_site = { &site_vtbl };
468 static BSTR include_pac_utils( const WCHAR *script )
470 HMODULE hmod = GetModuleHandleA( "jsproxy.dll" );
471 HRSRC rsrc;
472 DWORD size;
473 const char *data;
474 BSTR ret;
475 int len;
477 if (!(rsrc = FindResourceW( hmod, L"pac.js", (LPCWSTR)40 ))) return NULL;
478 size = SizeofResource( hmod, rsrc );
479 data = LoadResource( hmod, rsrc );
481 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
482 if (!(ret = SysAllocStringLen( NULL, len + lstrlenW( script ) + 1 ))) return NULL;
483 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
484 lstrcpyW( ret + len, script );
485 return ret;
488 #ifdef _WIN64
489 #define IActiveScriptParse_Release IActiveScriptParse64_Release
490 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
491 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
492 #else
493 #define IActiveScriptParse_Release IActiveScriptParse32_Release
494 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
495 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
496 #endif
498 static BOOL run_script( const WCHAR *script, const WCHAR *url, const WCHAR *hostname, char **result_str, DWORD *result_len )
500 IActiveScriptParse *parser = NULL;
501 IActiveScript *engine = NULL;
502 IDispatch *dispatch = NULL;
503 BOOL ret = FALSE;
504 CLSID clsid;
505 DISPID dispid;
506 BSTR func = NULL, full_script = NULL;
507 VARIANT args[2], retval;
508 DISPPARAMS params;
509 HRESULT hr, init;
511 init = CoInitialize( NULL );
512 hr = CLSIDFromProgID( L"JScript", &clsid );
513 if (hr != S_OK) goto done;
515 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
516 &IID_IActiveScript, (void **)&engine );
517 if (hr != S_OK) goto done;
519 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
520 if (hr != S_OK) goto done;
522 hr = IActiveScriptParse_InitNew( parser );
523 if (hr != S_OK) goto done;
525 hr = IActiveScript_SetScriptSite( engine, &script_site );
526 if (hr != S_OK) goto done;
528 hr = IActiveScript_AddNamedItem( engine, L"global_funcs", SCRIPTITEM_GLOBALMEMBERS );
529 if (hr != S_OK) goto done;
531 if (!(full_script = include_pac_utils( script ))) goto done;
533 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
534 if (hr != S_OK) goto done;
536 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
537 if (hr != S_OK) goto done;
539 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
540 if (hr != S_OK) goto done;
542 if (!(func = SysAllocString( L"FindProxyForURL" ))) goto done;
543 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
544 if (hr != S_OK) goto done;
546 V_VT( &args[0] ) = VT_BSTR;
547 V_BSTR( &args[0] ) = SysAllocString( hostname );
548 V_VT( &args[1] ) = VT_BSTR;
549 V_BSTR( &args[1] ) = SysAllocString( url );
551 params.rgvarg = args;
552 params.rgdispidNamedArgs = NULL;
553 params.cArgs = 2;
554 params.cNamedArgs = 0;
555 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
556 &params, &retval, NULL, NULL );
557 VariantClear( &args[0] );
558 VariantClear( &args[1] );
559 if (hr != S_OK)
561 WARN("script failed 0x%08x\n", hr);
562 goto done;
564 if ((*result_str = strdupWA( V_BSTR( &retval ) )))
566 TRACE( "result: %s\n", debugstr_a(*result_str) );
567 *result_len = strlen( *result_str ) + 1;
568 ret = TRUE;
570 VariantClear( &retval );
572 done:
573 SysFreeString( full_script );
574 SysFreeString( func );
575 if (dispatch) IDispatch_Release( dispatch );
576 if (parser) IActiveScriptParse_Release( parser );
577 if (engine) IActiveScript_Release( engine );
578 if (SUCCEEDED( init )) CoUninitialize();
579 return ret;
582 /******************************************************************
583 * InternetGetProxyInfo (jsproxy.@)
585 BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
586 LPDWORD len_proxy )
588 WCHAR *urlW = NULL, *hostnameW = NULL;
589 BOOL ret = FALSE;
591 TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url), len_url, hostname, len_hostname, proxy, len_proxy );
593 EnterCriticalSection( &cs_jsproxy );
595 if (!global_script->text)
597 SetLastError( ERROR_CAN_NOT_COMPLETE );
598 goto done;
600 if (hostname && len_hostname < strlen( hostname ))
602 SetLastError( ERROR_INSUFFICIENT_BUFFER );
603 goto done;
605 if (!(urlW = strdupAW( url, -1 ))) goto done;
606 if (hostname && !(hostnameW = strdupAW( hostname, -1 ))) goto done;
608 TRACE( "%s\n", debugstr_w(global_script->text) );
609 ret = run_script( global_script->text, urlW, hostnameW, proxy, len_proxy );
611 done:
612 heap_free( hostnameW );
613 heap_free( urlW );
614 LeaveCriticalSection( &cs_jsproxy );
615 return ret;