4 * Copyright 2002 Lionel Ulmer
5 * Copyright 2005 Mike McCormack
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.
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.
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
23 #include "wine/port.h"
29 # include <libxml/parser.h>
30 # include <libxml/xmlerror.h>
31 # ifdef SONAME_LIBXSLT
32 # ifdef HAVE_LIBXSLT_PATTERN_H
33 # include <libxslt/pattern.h>
35 # ifdef HAVE_LIBXSLT_TRANSFORM_H
36 # include <libxslt/transform.h>
38 # include <libxslt/imports.h>
39 # include <libxslt/xsltutils.h>
40 # include <libxslt/variables.h>
41 # include <libxslt/xsltInternals.h>
42 # include <libxslt/documents.h>
43 # include <libxslt/extensions.h>
44 # include <libxslt/extra.h>
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 #include "msxml_private.h"
61 HINSTANCE MSXML_hInstance
= NULL
;
65 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
67 void wineXmlCallbackLog(char const* caller
, xmlErrorLevel lvl
, char const* msg
, va_list ap
)
69 enum __wine_debug_class dbcl
;
71 const int max_size
= ARRAY_SIZE(buff
);
77 dbcl
= __WINE_DBCL_TRACE
;
80 dbcl
= __WINE_DBCL_WARN
;
83 dbcl
= __WINE_DBCL_ERR
;
87 len
= vsnprintf(buff
, max_size
, msg
, ap
);
88 if (len
== -1 || len
>= max_size
) buff
[max_size
-1] = 0;
90 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "%s", buff
);
93 void wineXmlCallbackError(char const* caller
, xmlErrorPtr err
)
95 enum __wine_debug_class dbcl
;
99 case XML_ERR_NONE
: dbcl
= __WINE_DBCL_TRACE
; break;
100 case XML_ERR_WARNING
: dbcl
= __WINE_DBCL_WARN
; break;
101 default: dbcl
= __WINE_DBCL_ERR
; break;
104 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "error code %d", err
->code
);
106 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, ": %s", err
->message
);
108 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "\n");
111 /* Support for loading xml files from a Wine Windows drive */
112 static int wineXmlMatchCallback (char const * filename
)
116 TRACE("%s\n", filename
);
119 * We will deal with loading XML files from the file system
120 * We only care about files that linux cannot find.
123 if(isalpha(filename
[0]) && filename
[1] == ':')
129 static void *wineXmlOpenCallback (char const * filename
)
131 BSTR sFilename
= bstr_from_xmlChar( (const xmlChar
*)filename
);
134 TRACE("%s\n", debugstr_w(sFilename
));
136 hFile
= CreateFileW(sFilename
, GENERIC_READ
,FILE_SHARE_READ
, NULL
,
137 OPEN_EXISTING
,FILE_ATTRIBUTE_NORMAL
, NULL
);
138 if(hFile
== INVALID_HANDLE_VALUE
) hFile
= 0;
139 SysFreeString(sFilename
);
143 static int wineXmlReadCallback(void * context
, char * buffer
, int len
)
147 TRACE("%p %s %d\n", context
, buffer
, len
);
149 if ((context
== NULL
) || (buffer
== NULL
))
152 if(!ReadFile( context
, buffer
,len
, &dwBytesRead
, NULL
))
154 ERR("Failed to read file\n");
158 TRACE("Read %d\n", dwBytesRead
);
163 static int wineXmlFileCloseCallback (void * context
)
165 return CloseHandle(context
) ? 0 : -1;
168 void* libxslt_handle
= NULL
;
169 #ifdef SONAME_LIBXSLT
170 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
171 DECL_FUNCPTR(xsltApplyStylesheet
);
172 DECL_FUNCPTR(xsltApplyStylesheetUser
);
173 DECL_FUNCPTR(xsltCleanupGlobals
);
174 DECL_FUNCPTR(xsltFreeStylesheet
);
175 DECL_FUNCPTR(xsltFreeTransformContext
);
176 DECL_FUNCPTR(xsltFunctionNodeSet
);
177 DECL_FUNCPTR(xsltNewTransformContext
);
178 DECL_FUNCPTR(xsltNextImport
);
179 DECL_FUNCPTR(xsltParseStylesheetDoc
);
180 DECL_FUNCPTR(xsltQuoteUserParams
);
181 DECL_FUNCPTR(xsltRegisterExtModuleFunction
);
182 DECL_FUNCPTR(xsltSaveResultTo
);
183 DECL_FUNCPTR(xsltSetLoaderFunc
);
187 static void init_libxslt(void)
189 #ifdef SONAME_LIBXSLT
190 void (*pxsltInit
)(void); /* Missing in libxslt <= 1.1.14 */
192 libxslt_handle
= dlopen(SONAME_LIBXSLT
, RTLD_NOW
);
196 #define LOAD_FUNCPTR(f, needed) \
197 if ((p##f = dlsym(libxslt_handle, #f)) == NULL) \
198 if (needed) { WARN("Can't find symbol %s\n", #f); goto sym_not_found; }
199 LOAD_FUNCPTR(xsltInit
, 0);
200 LOAD_FUNCPTR(xsltApplyStylesheet
, 1);
201 LOAD_FUNCPTR(xsltApplyStylesheetUser
, 1);
202 LOAD_FUNCPTR(xsltCleanupGlobals
, 1);
203 LOAD_FUNCPTR(xsltFreeStylesheet
, 1);
204 LOAD_FUNCPTR(xsltFreeTransformContext
, 1);
205 LOAD_FUNCPTR(xsltFunctionNodeSet
, 1);
206 LOAD_FUNCPTR(xsltNewTransformContext
, 1);
207 LOAD_FUNCPTR(xsltNextImport
, 1);
208 LOAD_FUNCPTR(xsltParseStylesheetDoc
, 1);
209 LOAD_FUNCPTR(xsltQuoteUserParams
, 1);
210 LOAD_FUNCPTR(xsltRegisterExtModuleFunction
, 1);
211 LOAD_FUNCPTR(xsltSaveResultTo
, 1);
212 LOAD_FUNCPTR(xsltSetLoaderFunc
, 1);
218 pxsltSetLoaderFunc(xslt_doc_default_loader
);
219 pxsltRegisterExtModuleFunction(
220 (const xmlChar
*)"node-set",
221 (const xmlChar
*)"urn:schemas-microsoft-com:xslt",
222 pxsltFunctionNodeSet
);
227 dlclose(libxslt_handle
);
228 libxslt_handle
= NULL
;
232 static int to_utf8(int cp
, unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
237 if (!in
|| !inlen
) return 0;
239 len
= MultiByteToWideChar(cp
, 0, (const char *)in
, *inlen
, NULL
, 0);
240 tmp
= heap_alloc(len
* sizeof(WCHAR
));
242 MultiByteToWideChar(cp
, 0, (const char *)in
, *inlen
, tmp
, len
);
244 len
= WideCharToMultiByte(CP_UTF8
, 0, tmp
, len
, (char *)out
, *outlen
, NULL
, NULL
);
252 static int from_utf8(int cp
, unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
257 if (!in
|| !inlen
) return 0;
259 len
= MultiByteToWideChar(CP_UTF8
, 0, (const char *)in
, *inlen
, NULL
, 0);
260 tmp
= heap_alloc(len
* sizeof(WCHAR
));
262 MultiByteToWideChar(CP_UTF8
, 0, (const char *)in
, *inlen
, tmp
, len
);
264 len
= WideCharToMultiByte(cp
, 0, tmp
, len
, (char *)out
, *outlen
, NULL
, NULL
);
272 static int win1250_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
274 return to_utf8(1250, out
, outlen
, in
, inlen
);
277 static int utf8_to_win1250(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
279 return from_utf8(1250, out
, outlen
, in
, inlen
);
282 static int win1251_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
284 return to_utf8(1251, out
, outlen
, in
, inlen
);
287 static int utf8_to_win1251(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
289 return from_utf8(1251, out
, outlen
, in
, inlen
);
292 static int win1252_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
294 return to_utf8(1252, out
, outlen
, in
, inlen
);
297 static int utf8_to_win1252(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
299 return from_utf8(1252, out
, outlen
, in
, inlen
);
302 static int win1253_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
304 return to_utf8(1253, out
, outlen
, in
, inlen
);
307 static int utf8_to_win1253(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
309 return from_utf8(1253, out
, outlen
, in
, inlen
);
311 static int win1254_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
313 return to_utf8(1254, out
, outlen
, in
, inlen
);
316 static int utf8_to_win1254(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
318 return from_utf8(1254, out
, outlen
, in
, inlen
);
321 static int win1255_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
323 return to_utf8(1255, out
, outlen
, in
, inlen
);
326 static int utf8_to_win1255(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
328 return from_utf8(1255, out
, outlen
, in
, inlen
);
331 static int win1256_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
333 return to_utf8(1256, out
, outlen
, in
, inlen
);
336 static int utf8_to_win1256(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
338 return from_utf8(1256, out
, outlen
, in
, inlen
);
341 static int win1257_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
343 return to_utf8(1257, out
, outlen
, in
, inlen
);
346 static int utf8_to_win1257(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
348 return from_utf8(1257, out
, outlen
, in
, inlen
);
351 static int win1258_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
353 return to_utf8(1258, out
, outlen
, in
, inlen
);
356 static int utf8_to_win1258(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
358 return from_utf8(1258, out
, outlen
, in
, inlen
);
361 static void init_char_encoders(void)
365 const char *encoding
;
366 xmlCharEncodingInputFunc input
;
367 xmlCharEncodingOutputFunc output
;
370 { "windows-1250", win1250_to_utf8
, utf8_to_win1250
},
371 { "windows-1251", win1251_to_utf8
, utf8_to_win1251
},
372 { "windows-1252", win1252_to_utf8
, utf8_to_win1252
},
373 { "windows-1253", win1253_to_utf8
, utf8_to_win1253
},
374 { "windows-1254", win1254_to_utf8
, utf8_to_win1254
},
375 { "windows-1255", win1255_to_utf8
, utf8_to_win1255
},
376 { "windows-1256", win1256_to_utf8
, utf8_to_win1256
},
377 { "windows-1257", win1257_to_utf8
, utf8_to_win1257
},
378 { "windows-1258", win1258_to_utf8
, utf8_to_win1258
}
382 xmlInitCharEncodingHandlers();
384 for (i
= 0; i
< ARRAY_SIZE(encoder
); i
++)
386 if (!xmlFindCharEncodingHandler(encoder
[i
].encoding
))
388 TRACE("Adding %s encoding handler\n", encoder
[i
].encoding
);
389 xmlNewCharEncodingHandler(encoder
[i
].encoding
, encoder
[i
].input
, encoder
[i
].output
);
394 #endif /* HAVE_LIBXML2 */
397 HRESULT WINAPI
DllCanUnloadNow(void)
403 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID reserved
)
405 MSXML_hInstance
= hInstDLL
;
409 case DLL_PROCESS_ATTACH
:
413 /* Set the default indent character to a single tab,
414 for this thread and as default for new threads */
415 xmlTreeIndentString
= "\t";
416 xmlThrDefTreeIndentString("\t");
418 /* Register callbacks for loading XML files */
419 if(xmlRegisterInputCallbacks(wineXmlMatchCallback
, wineXmlOpenCallback
,
420 wineXmlReadCallback
, wineXmlFileCloseCallback
) == -1)
421 WARN("Failed to register callbacks\n");
423 init_char_encoders();
428 DisableThreadLibraryCalls(hInstDLL
);
430 case DLL_PROCESS_DETACH
:
433 #ifdef SONAME_LIBXSLT
436 pxsltCleanupGlobals();
437 dlclose(libxslt_handle
);
440 /* Restore default Callbacks */
441 xmlCleanupInputCallbacks();
442 xmlRegisterDefaultInputCallbacks();
453 /***********************************************************************
454 * DllRegisterServer (MSXML3.@)
456 HRESULT WINAPI
DllRegisterServer(void)
458 return __wine_register_resources( MSXML_hInstance
);
461 /***********************************************************************
462 * DllUnregisterServer (MSXML3.@)
464 HRESULT WINAPI
DllUnregisterServer(void)
466 return __wine_unregister_resources( MSXML_hInstance
);