25ff33e5cbbaca81f359786fad56afa427f1196b
[reactos.git] / reactos / dll / win32 / msxml3 / main.c
1 /*
2 * MSXML Class Factory
3 *
4 * Copyright 2002 Lionel Ulmer
5 * Copyright 2005 Mike McCormack
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define WIN32_NO_STATUS
23 #define _INC_WINDOWS
24
25 #include <config.h>
26 #include <wine/port.h>
27
28 #define COBJMACROS
29
30 //#include <stdarg.h>
31 #ifdef HAVE_LIBXML2
32 //# include <libxml/parser.h>
33 //# include <libxml/xmlerror.h>
34 # ifdef SONAME_LIBXSLT
35 # ifdef HAVE_LIBXSLT_PATTERN_H
36 # include <libxslt/pattern.h>
37 # endif
38 # ifdef HAVE_LIBXSLT_TRANSFORM_H
39 # include <libxslt/transform.h>
40 # endif
41 # include <libxslt/xsltutils.h>
42 # include <libxslt/xsltInternals.h>
43 # endif
44 #endif
45
46 #include <windef.h>
47 #include <winbase.h>
48 //#include "winuser.h"
49 #include <ole2.h>
50 #include <rpcproxy.h>
51 //#include "msxml.h"
52 #include <msxml6.h>
53
54 //#include "wine/unicode.h"
55 #include <wine/debug.h>
56 #include <wine/library.h>
57
58 #include "msxml_private.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
61
62 HINSTANCE MSXML_hInstance = NULL;
63
64 #ifdef HAVE_LIBXML2
65
66 void wineXmlCallbackLog(char const* caller, xmlErrorLevel lvl, char const* msg, va_list ap)
67 {
68 enum __wine_debug_class dbcl;
69 char buff[200];
70 static const int max_size = sizeof(buff) / sizeof(buff[0]);
71 int len;
72
73 switch (lvl)
74 {
75 case XML_ERR_NONE:
76 dbcl = __WINE_DBCL_TRACE;
77 break;
78 case XML_ERR_WARNING:
79 dbcl = __WINE_DBCL_WARN;
80 break;
81 default:
82 dbcl = __WINE_DBCL_ERR;
83 break;
84 }
85
86 len = vsnprintf(buff, max_size, msg, ap);
87 if (len == -1 || len >= max_size) buff[max_size-1] = 0;
88
89 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "%s", buff);
90 }
91
92 void wineXmlCallbackError(char const* caller, xmlErrorPtr err)
93 {
94 enum __wine_debug_class dbcl;
95
96 switch (err->level)
97 {
98 case XML_ERR_NONE: dbcl = __WINE_DBCL_TRACE; break;
99 case XML_ERR_WARNING: dbcl = __WINE_DBCL_WARN; break;
100 default: dbcl = __WINE_DBCL_ERR; break;
101 }
102
103 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "error code %d", err->code);
104 if (err->message)
105 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, ": %s", err->message);
106 else
107 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "\n");
108 }
109
110 /* Support for loading xml files from a Wine Windows drive */
111 static int wineXmlMatchCallback (char const * filename)
112 {
113 int nRet = 0;
114
115 TRACE("%s\n", filename);
116
117 /*
118 * We will deal with loading XML files from the file system
119 * We only care about files that linux cannot find.
120 * e.g. C:,D: etc
121 */
122 if(isalpha(filename[0]) && filename[1] == ':')
123 nRet = 1;
124
125 return nRet;
126 }
127
128 static void *wineXmlOpenCallback (char const * filename)
129 {
130 BSTR sFilename = bstr_from_xmlChar( (const xmlChar*)filename);
131 HANDLE hFile;
132
133 TRACE("%s\n", debugstr_w(sFilename));
134
135 hFile = CreateFileW(sFilename, GENERIC_READ,FILE_SHARE_READ, NULL,
136 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
137 if(hFile == INVALID_HANDLE_VALUE) hFile = 0;
138 SysFreeString(sFilename);
139 return hFile;
140 }
141
142 static int wineXmlReadCallback(void * context, char * buffer, int len)
143 {
144 DWORD dwBytesRead;
145
146 TRACE("%p %s %d\n", context, buffer, len);
147
148 if ((context == NULL) || (buffer == NULL))
149 return(-1);
150
151 if(!ReadFile( context, buffer,len, &dwBytesRead, NULL))
152 {
153 ERR("Failed to read file\n");
154 return -1;
155 }
156
157 TRACE("Read %d\n", dwBytesRead);
158
159 return dwBytesRead;
160 }
161
162 static int wineXmlFileCloseCallback (void * context)
163 {
164 return CloseHandle(context) ? 0 : -1;
165 }
166
167 #endif
168
169
170 HRESULT WINAPI DllCanUnloadNow(void)
171 {
172 return S_FALSE;
173 }
174
175
176 void* libxslt_handle = NULL;
177 #ifdef SONAME_LIBXSLT
178 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
179 DECL_FUNCPTR(xsltApplyStylesheet);
180 DECL_FUNCPTR(xsltCleanupGlobals);
181 DECL_FUNCPTR(xsltFreeStylesheet);
182 DECL_FUNCPTR(xsltParseStylesheetDoc);
183 # undef DECL_FUNCPTR
184 #endif
185
186 static void init_libxslt(void)
187 {
188 #ifdef SONAME_LIBXSLT
189 void (*pxsltInit)(void); /* Missing in libxslt <= 1.1.14 */
190
191 libxslt_handle = wine_dlopen(SONAME_LIBXSLT, RTLD_NOW, NULL, 0);
192 if (!libxslt_handle)
193 return;
194
195 #define LOAD_FUNCPTR(f, needed) \
196 if ((p##f = wine_dlsym(libxslt_handle, #f, NULL, 0)) == NULL) \
197 if (needed) { WARN("Can't find symbol %s\n", #f); goto sym_not_found; }
198 LOAD_FUNCPTR(xsltInit, 0);
199 LOAD_FUNCPTR(xsltApplyStylesheet, 1);
200 LOAD_FUNCPTR(xsltCleanupGlobals, 1);
201 LOAD_FUNCPTR(xsltFreeStylesheet, 1);
202 LOAD_FUNCPTR(xsltParseStylesheetDoc, 1);
203 #undef LOAD_FUNCPTR
204
205 if (pxsltInit)
206 pxsltInit();
207 return;
208
209 sym_not_found:
210 wine_dlclose(libxslt_handle, NULL, 0);
211 libxslt_handle = NULL;
212 #endif
213 }
214
215 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID reserved)
216 {
217 MSXML_hInstance = hInstDLL;
218
219 switch(fdwReason)
220 {
221 case DLL_PROCESS_ATTACH:
222 #ifdef HAVE_LIBXML2
223 xmlInitParser();
224
225 /* Set the default indent character to a single tab,
226 for this thread and as default for new threads */
227 xmlTreeIndentString = "\t";
228 xmlThrDefTreeIndentString("\t");
229
230 /* Register callbacks for loading XML files */
231 if(xmlRegisterInputCallbacks(wineXmlMatchCallback, wineXmlOpenCallback,
232 wineXmlReadCallback, wineXmlFileCloseCallback) == -1)
233 WARN("Failed to register callbacks\n");
234
235 schemasInit();
236 #endif
237 init_libxslt();
238 DisableThreadLibraryCalls(hInstDLL);
239 break;
240 case DLL_PROCESS_DETACH:
241 #ifdef SONAME_LIBXSLT
242 if (libxslt_handle)
243 {
244 pxsltCleanupGlobals();
245 wine_dlclose(libxslt_handle, NULL, 0);
246 }
247 #endif
248 #ifdef HAVE_LIBXML2
249 /* Restore default Callbacks */
250 xmlCleanupInputCallbacks();
251 xmlRegisterDefaultInputCallbacks();
252
253 xmlCleanupParser();
254 schemasCleanup();
255 #endif
256 release_typelib();
257 break;
258 }
259 return TRUE;
260 }
261
262 const char *debugstr_variant(const VARIANT *v)
263 {
264 if(!v)
265 return "(null)";
266
267 switch(V_VT(v)) {
268 case VT_EMPTY:
269 return "{VT_EMPTY}";
270 case VT_NULL:
271 return "{VT_NULL}";
272 case VT_I1:
273 return wine_dbg_sprintf("{VT_I1: %d}", V_I1(v));
274 case VT_I2:
275 return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
276 case VT_I4:
277 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
278 case VT_INT:
279 return wine_dbg_sprintf("{VT_INT: %d}", V_INT(v));
280 case VT_R8:
281 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
282 case VT_BSTR:
283 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
284 case VT_DISPATCH:
285 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
286 case VT_BOOL:
287 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
288 case VT_UNKNOWN:
289 return wine_dbg_sprintf("{VT_UNKNOWN: %p}", V_UNKNOWN(v));
290 case VT_UINT:
291 return wine_dbg_sprintf("{VT_UINT: %u}", V_UINT(v));
292 case VT_BSTR|VT_BYREF:
293 return wine_dbg_sprintf("{VT_BSTR|VT_BYREF: ptr %p, data %s}",
294 V_BSTRREF(v), debugstr_w(V_BSTRREF(v) ? *V_BSTRREF(v) : NULL));
295 case VT_ERROR:
296 return wine_dbg_sprintf("{VT_ERROR: 0x%08x}", V_ERROR(v));
297 case VT_VARIANT|VT_BYREF:
298 return wine_dbg_sprintf("{VT_VARIANT|VT_BYREF: %s}", debugstr_variant(V_VARIANTREF(v)));
299 case VT_UI1|VT_ARRAY:
300 return "{VT_UI1|VT_ARRAY}";
301 default:
302 return wine_dbg_sprintf("{vt %d}", V_VT(v));
303 }
304 }
305
306 /***********************************************************************
307 * DllRegisterServer (MSXML3.@)
308 */
309 HRESULT WINAPI DllRegisterServer(void)
310 {
311 return __wine_register_resources( MSXML_hInstance );
312 }
313
314 /***********************************************************************
315 * DllUnregisterServer (MSXML3.@)
316 */
317 HRESULT WINAPI DllUnregisterServer(void)
318 {
319 return __wine_unregister_resources( MSXML_hInstance );
320 }