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
25 #include <libxml/parser.h>
26 #include <libxml/xmlerror.h>
27 #include <libxslt/pattern.h>
28 #include <libxslt/transform.h>
29 #include <libxslt/imports.h>
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/variables.h>
32 #include <libxslt/xsltInternals.h>
33 #include <libxslt/documents.h>
34 #include <libxslt/extensions.h>
35 #include <libxslt/extra.h>
46 #include "wine/debug.h"
48 #include "msxml_private.h"
50 HINSTANCE MSXML_hInstance
= NULL
;
52 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
54 void wineXmlCallbackLog(char const* caller
, xmlErrorLevel lvl
, char const* msg
, va_list ap
)
56 enum __wine_debug_class dbcl
;
58 const int max_size
= ARRAY_SIZE(buff
);
64 dbcl
= __WINE_DBCL_TRACE
;
67 dbcl
= __WINE_DBCL_WARN
;
70 dbcl
= __WINE_DBCL_ERR
;
74 len
= vsnprintf(buff
, max_size
, msg
, ap
);
75 if (len
== -1 || len
>= max_size
) buff
[max_size
-1] = 0;
77 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "%s", buff
);
80 void wineXmlCallbackError(char const* caller
, xmlErrorPtr err
)
82 enum __wine_debug_class dbcl
;
86 case XML_ERR_NONE
: dbcl
= __WINE_DBCL_TRACE
; break;
87 case XML_ERR_WARNING
: dbcl
= __WINE_DBCL_WARN
; break;
88 default: dbcl
= __WINE_DBCL_ERR
; break;
91 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "error code %d", err
->code
);
93 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, ": %s", err
->message
);
95 wine_dbg_log(dbcl
, &__wine_dbch_msxml
, caller
, "\n");
98 /* Support for loading xml files from a Wine Windows drive */
99 static int wineXmlMatchCallback (char const * filename
)
103 TRACE("%s\n", filename
);
106 * We will deal with loading XML files from the file system
107 * We only care about files that linux cannot find.
110 if(isalpha(filename
[0]) && filename
[1] == ':')
116 static void *wineXmlOpenCallback (char const * filename
)
118 BSTR sFilename
= bstr_from_xmlChar( (const xmlChar
*)filename
);
121 TRACE("%s\n", debugstr_w(sFilename
));
123 hFile
= CreateFileW(sFilename
, GENERIC_READ
,FILE_SHARE_READ
, NULL
,
124 OPEN_EXISTING
,FILE_ATTRIBUTE_NORMAL
, NULL
);
125 if(hFile
== INVALID_HANDLE_VALUE
) hFile
= 0;
126 SysFreeString(sFilename
);
130 static int wineXmlReadCallback(void * context
, char * buffer
, int len
)
134 TRACE("%p %s %d\n", context
, buffer
, len
);
136 if ((context
== NULL
) || (buffer
== NULL
))
139 if(!ReadFile( context
, buffer
,len
, &dwBytesRead
, NULL
))
141 ERR("Failed to read file\n");
145 TRACE("Read %ld bytes.\n", dwBytesRead
);
150 static int wineXmlFileCloseCallback (void * context
)
152 return CloseHandle(context
) ? 0 : -1;
155 static void init_libxslt(void)
158 xsltSetLoaderFunc(xslt_doc_default_loader
);
159 xsltRegisterExtModuleFunction(
160 (const xmlChar
*)"node-set",
161 (const xmlChar
*)"urn:schemas-microsoft-com:xslt",
162 xsltFunctionNodeSet
);
165 static int to_utf8(int cp
, unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
170 if (!in
|| !inlen
) goto done
;
172 len
= MultiByteToWideChar(cp
, 0, (const char *)in
, *inlen
, NULL
, 0);
173 tmp
= heap_alloc(len
* sizeof(WCHAR
));
175 MultiByteToWideChar(cp
, 0, (const char *)in
, *inlen
, tmp
, len
);
177 len
= WideCharToMultiByte(CP_UTF8
, 0, tmp
, len
, (char *)out
, *outlen
, NULL
, NULL
);
185 static int from_utf8(int cp
, unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
190 if (!in
|| !inlen
) goto done
;
192 len
= MultiByteToWideChar(CP_UTF8
, 0, (const char *)in
, *inlen
, NULL
, 0);
193 tmp
= heap_alloc(len
* sizeof(WCHAR
));
195 MultiByteToWideChar(CP_UTF8
, 0, (const char *)in
, *inlen
, tmp
, len
);
197 len
= WideCharToMultiByte(cp
, 0, tmp
, len
, (char *)out
, *outlen
, NULL
, NULL
);
205 static int gbk_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
207 return to_utf8(936, out
, outlen
, in
, inlen
);
210 static int utf8_to_gbk(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
212 return from_utf8(936, out
, outlen
, in
, inlen
);
215 static int win1250_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
217 return to_utf8(1250, out
, outlen
, in
, inlen
);
220 static int utf8_to_win1250(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
222 return from_utf8(1250, out
, outlen
, in
, inlen
);
225 static int win1251_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
227 return to_utf8(1251, out
, outlen
, in
, inlen
);
230 static int utf8_to_win1251(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
232 return from_utf8(1251, out
, outlen
, in
, inlen
);
235 static int win1252_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
237 return to_utf8(1252, out
, outlen
, in
, inlen
);
240 static int utf8_to_win1252(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
242 return from_utf8(1252, out
, outlen
, in
, inlen
);
245 static int win1253_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
247 return to_utf8(1253, out
, outlen
, in
, inlen
);
250 static int utf8_to_win1253(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
252 return from_utf8(1253, out
, outlen
, in
, inlen
);
254 static int win1254_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
256 return to_utf8(1254, out
, outlen
, in
, inlen
);
259 static int utf8_to_win1254(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
261 return from_utf8(1254, out
, outlen
, in
, inlen
);
264 static int win1255_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
266 return to_utf8(1255, out
, outlen
, in
, inlen
);
269 static int utf8_to_win1255(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
271 return from_utf8(1255, out
, outlen
, in
, inlen
);
274 static int win1256_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
276 return to_utf8(1256, out
, outlen
, in
, inlen
);
279 static int utf8_to_win1256(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
281 return from_utf8(1256, out
, outlen
, in
, inlen
);
284 static int win1257_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
286 return to_utf8(1257, out
, outlen
, in
, inlen
);
289 static int utf8_to_win1257(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
291 return from_utf8(1257, out
, outlen
, in
, inlen
);
294 static int win1258_to_utf8(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
296 return to_utf8(1258, out
, outlen
, in
, inlen
);
299 static int utf8_to_win1258(unsigned char *out
, int *outlen
, const unsigned char *in
, int *inlen
)
301 return from_utf8(1258, out
, outlen
, in
, inlen
);
304 static void init_char_encoders(void)
308 const char *encoding
;
309 xmlCharEncodingInputFunc input
;
310 xmlCharEncodingOutputFunc output
;
313 { "gbk", gbk_to_utf8
, utf8_to_gbk
},
314 { "windows-1250", win1250_to_utf8
, utf8_to_win1250
},
315 { "windows-1251", win1251_to_utf8
, utf8_to_win1251
},
316 { "windows-1252", win1252_to_utf8
, utf8_to_win1252
},
317 { "windows-1253", win1253_to_utf8
, utf8_to_win1253
},
318 { "windows-1254", win1254_to_utf8
, utf8_to_win1254
},
319 { "windows-1255", win1255_to_utf8
, utf8_to_win1255
},
320 { "windows-1256", win1256_to_utf8
, utf8_to_win1256
},
321 { "windows-1257", win1257_to_utf8
, utf8_to_win1257
},
322 { "windows-1258", win1258_to_utf8
, utf8_to_win1258
}
326 for (i
= 0; i
< ARRAY_SIZE(encoder
); i
++)
328 if (!xmlFindCharEncodingHandler(encoder
[i
].encoding
))
330 TRACE("Adding %s encoding handler\n", encoder
[i
].encoding
);
331 xmlNewCharEncodingHandler(encoder
[i
].encoding
, encoder
[i
].input
, encoder
[i
].output
);
336 const CLSID
* DOMDocument_version(MSXML_VERSION v
)
341 case MSXML_DEFAULT
: return &CLSID_DOMDocument
;
342 case MSXML3
: return &CLSID_DOMDocument30
;
343 case MSXML4
: return &CLSID_DOMDocument40
;
344 case MSXML6
: return &CLSID_DOMDocument60
;
348 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID reserved
)
350 MSXML_hInstance
= hInstDLL
;
354 case DLL_PROCESS_ATTACH
:
357 /* Set the default indent character to a single tab,
358 for this thread and as default for new threads */
359 xmlTreeIndentString
= "\t";
360 xmlThrDefTreeIndentString("\t");
362 /* Register callbacks for loading XML files */
363 if(xmlRegisterInputCallbacks(wineXmlMatchCallback
, wineXmlOpenCallback
,
364 wineXmlReadCallback
, wineXmlFileCloseCallback
) == -1)
365 WARN("Failed to register callbacks\n");
367 init_char_encoders();
371 DisableThreadLibraryCalls(hInstDLL
);
373 case DLL_PROCESS_DETACH
:
375 xsltCleanupGlobals();
376 /* Restore default Callbacks */
377 xmlCleanupInputCallbacks();
378 xmlRegisterDefaultInputCallbacks();