]> Creatis software - bbtk.git/blob - kernel/src/xmlParser.cpp
#3203 BBTK Feature New Normal vtk7itk4wx3-mingw64
[bbtk.git] / kernel / src / xmlParser.cpp
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /**
29  ****************************************************************************
30  * <P> XML.c - implementation file for basic XML parser written in ANSI C++
31  * for portability. It works by using recursion and a node tree for breaking
32  * down the elements of an XML document.  </P>
33  *
34  * @version     V2.23
35  * @author      Frank Vanden Berghen
36  *
37  * NOTE:
38  *
39  *   If you add "#define STRICT_PARSING", on the first line of this file
40  *   the parser will see the following XML-stream:
41  *      <a><b>some text</b><b>other text    </a>
42  *   as an error. Otherwise, this tring will be equivalent to:
43  *      <a><b>some text</b><b>other text</b></a>
44  *
45  * NOTE:
46  *
47  *   If you add "#define APPROXIMATE_PARSING" on the first line of this file
48  *   the parser will see the following XML-stream:
49  *     <data name="n1">
50  *     <data name="n2">
51  *     <data name="n3" />
52  *   as equivalent to the following XML-stream:
53  *     <data name="n1" />
54  *     <data name="n2" />
55  *     <data name="n3" />
56  *   This can be useful for badly-formed XML-streams but prevent the use
57  *   of the following XML-stream (problem is: tags at contiguous levels
58  *   have the same names):
59  *     <data name="n1">
60  *        <data name="n2">
61  *            <data name="n3" />
62  *        </data>
63  *     </data>
64  *
65  * NOTE:
66  *
67  *   If you add "#define _XMLPARSER_NO_MESSAGEBOX_" on the first line of this file
68  *   the "openFileHelper" function will always display error messages inside the
69  *   console instead of inside a message-box-window. Message-box-windows are
70  *   available on windows 9x/NT/2000/XP/Vista only.
71  *
72  * BSD license:
73  * Copyright (c) 2002, Frank Vanden Berghen
74  * All rights reserved.
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions are met:
77  *
78  *     * Redistributions of source code must retain the above copyright
79  *       notice, this list of conditions and the following disclaimer.
80  *     * Redistributions in binary form must reproduce the above copyright
81  *       notice, this list of conditions and the following disclaimer in the
82  *       documentation and/or other materials provided with the distribution.
83  *     * Neither the name of the Frank Vanden Berghen nor the
84  *       names of its contributors may be used to endorse or promote products
85  *       derived from this software without specific prior written permission.
86  *
87  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
88  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
89  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
91  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
92  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
93  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
94  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
95  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  *
98  ****************************************************************************
99  */
100 #ifndef _CRT_SECURE_NO_DEPRECATE
101 #define _CRT_SECURE_NO_DEPRECATE
102 #endif
103 #include "xmlParser.h"
104 #ifdef _XMLWINDOWS
105 //#ifdef _DEBUG
106 //#define _CRTDBG_MAP_ALLOC
107 //#include <crtdbg.h>
108 //#endif
109 #define WIN32_LEAN_AND_MEAN
110 #include <Windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files
111                      // to have "MessageBoxA" to display error messages for openFilHelper
112 #endif
113
114 #include <memory.h>
115 #include <assert.h>
116 #include <stdio.h>
117 #include <string.h>
118 #include <stdlib.h>
119
120 XMLCSTR XMLNode::getVersion() { return _T("v2.23"); }
121 void free_XMLDLL(void *t){free(t);}
122
123 static char strictUTF8Parsing=1, guessUnicodeChars=1, dropWhiteSpace=1;
124
125 inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; }
126
127 // You can modify the initialization of the variable "XMLClearTags" below
128 // to change the clearTags that are currently recognized by the library.
129 // The number on the second columns is the length of the string inside the
130 // first column. The "<!DOCTYPE" declaration must be the second in the list.
131 static ALLXMLClearTag XMLClearTags[] =
132 {
133     {    _T("<![CDATA["),9,  _T("]]>")      },
134     {    _T("<!DOCTYPE"),9,  _T(">")        },
135     {    _T("<PRE>")    ,5,  _T("</PRE>")   },
136     {    _T("<Script>") ,8,  _T("</Script>")},
137     {    _T("<!--")     ,4,  _T("-->")      },
138     {    NULL           ,0,  NULL           }
139 };
140 ALLXMLClearTag* XMLNode::getClearTagTable() { return XMLClearTags; }
141
142 // You can modify the initialization of the variable "XMLEntities" below
143 // to change the character entities that are currently recognized by the library.
144 // The number on the second columns is the length of the string inside the
145 // first column. Additionally, the syntaxes "&#xA0;" and "&#160;" are recognized.
146 typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
147 static XMLCharacterEntity XMLEntities[] =
148 {
149     { _T("&amp;" ), 5, _T('&' )},
150     { _T("&lt;"  ), 4, _T('<' )},
151     { _T("&gt;"  ), 4, _T('>' )},
152     { _T("&quot;"), 6, _T('\"')},
153     { _T("&apos;"), 6, _T('\'')},
154     { NULL        , 0, '\0'    }
155 };
156
157 // When rendering the XMLNode to a string (using the "createXMLString" function),
158 // you can ask for a beautiful formatting. This formatting is using the
159 // following indentation character:
160 #define INDENTCHAR _T('\t')
161
162 // The following function parses the XML errors into a user friendly string.
163 // You can edit this to change the output language of the library to something else.
164 XMLCSTR XMLNode::getError(XMLError xerror)
165 {
166     switch (xerror)
167     {
168     case eXMLErrorNone:                  return _T("No error");
169     case eXMLErrorMissingEndTag:         return _T("Warning: Unmatched end tag");
170     case eXMLErrorEmpty:                 return _T("Error: No XML data");
171     case eXMLErrorFirstNotStartTag:      return _T("Error: First token not start tag");
172     case eXMLErrorMissingTagName:        return _T("Error: Missing start tag name");
173     case eXMLErrorMissingEndTagName:     return _T("Error: Missing end tag name");
174     case eXMLErrorNoMatchingQuote:       return _T("Error: Unmatched quote");
175     case eXMLErrorUnmatchedEndTag:       return _T("Error: Unmatched end tag");
176     case eXMLErrorUnmatchedEndClearTag:  return _T("Error: Unmatched clear tag end");
177     case eXMLErrorUnexpectedToken:       return _T("Error: Unexpected token found");
178     case eXMLErrorInvalidTag:            return _T("Error: Invalid tag found");
179     case eXMLErrorNoElements:            return _T("Error: No elements found");
180     case eXMLErrorFileNotFound:          return _T("Error: File not found");
181     case eXMLErrorFirstTagNotFound:      return _T("Error: First Tag not found");
182     case eXMLErrorUnknownCharacterEntity:return _T("Error: Unknown character entity");
183     case eXMLErrorCharConversionError:   return _T("Error: unable to convert between UNICODE and MultiByte chars");
184     case eXMLErrorCannotOpenWriteFile:   return _T("Error: unable to open file for writing");
185     case eXMLErrorCannotWriteFile:       return _T("Error: cannot write into file");
186
187     case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _T("Warning: Base64-string length is not a multiple of 4");
188     case eXMLErrorBase64DecodeTruncatedData:      return _T("Warning: Base64-string is truncated");
189     case eXMLErrorBase64DecodeIllegalCharacter:   return _T("Error: Base64-string contains an illegal character");
190     case eXMLErrorBase64DecodeBufferTooSmall:     return _T("Error: Base64 decode output buffer is too small");
191     };
192     return _T("Unknown");
193 }
194
195 // Here is an abstraction layer to access some common string manipulation functions.
196 // The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
197 // Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
198 // If you plan to "port" the library to a new system/compiler, all you have to do is
199 // to edit the following lines.
200 #ifdef XML_NO_WIDE_CHAR
201 char myIsTextUnicode(const void *b, int len) { return FALSE; }
202 #else
203     #if defined (UNDER_CE) || !defined(WIN32)
204     char myIsTextUnicode(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
205     {
206 #ifdef sun
207         // for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
208         if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE;
209 #endif
210         const wchar_t *s=(const wchar_t*)b;
211
212         // buffer too small:
213         if (len<(int)sizeof(wchar_t)) return FALSE;
214
215         // odd length test
216         if (len&1) return FALSE;
217
218         /* only checks the first 256 characters */
219         len=mmin(256,len/sizeof(wchar_t));
220
221         // Check for the special byte order:
222         if (*s == 0xFFFE) return FALSE;     // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
223         if (*s == 0xFEFF) return TRUE;      // IS_TEXT_UNICODE_SIGNATURE
224
225         // checks for ASCII characters in the UNICODE stream
226         int i,stats=0;
227         for (i=0; i<len; i++) if (s[i]<=(unsigned short)255) stats++;
228         if (stats>len/2) return TRUE;
229
230         // Check for UNICODE NULL chars
231         for (i=0; i<len; i++) if (!s[i]) return TRUE;
232
233         return FALSE;
234     }
235     #else
236     char myIsTextUnicode(const void *b,int l) { return (char)IsTextUnicode((CONST LPVOID)b,l,NULL); };
237     #endif
238 #endif
239
240 #ifdef _XMLWINDOWS
241 // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET,
242     #ifdef _XMLUNICODE
243         wchar_t *myMultiByteToWideChar(const char *s,int l)
244         {
245             int i;
246             if (strictUTF8Parsing)  i=(int)MultiByteToWideChar(CP_UTF8,0             ,s,l,NULL,0);
247             else                    i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,l,NULL,0);
248             if (i<0) return NULL;
249             wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
250             if (strictUTF8Parsing)  i=(int)MultiByteToWideChar(CP_UTF8,0             ,s,l,d,i);
251             else                    i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,l,d,i);
252             d[i]=0;
253             return d;
254         }
255     #else
256         char *myWideCharToMultiByte(const wchar_t *s,int l)
257         {
258             UINT codePage=CP_ACP; if (strictUTF8Parsing) codePage=CP_UTF8;
259             int i=(int)WideCharToMultiByte(codePage,  // code page
260                 0,                       // performance and mapping flags
261                 s,                       // wide-character string
262                 l,                       // number of chars in string
263                 NULL,                    // buffer for new string
264                 0,                       // size of buffer
265                 NULL,                    // default for unmappable chars
266                 NULL                     // set when default char used
267                 );
268             if (i<0) return NULL;
269             char *d=(char*)malloc(i+1);
270             WideCharToMultiByte(codePage,// code page
271                 0,                       // performance and mapping flags
272                 s,                       // wide-character string
273                 l,                       // number of chars in string
274                 d,                       // buffer for new string
275                 i,                       // size of buffer
276                 NULL,                    // default for unmappable chars
277                 NULL                     // set when default char used
278                 );
279             d[i]=0;
280             return d;
281         }
282     #endif
283     #ifdef __BORLANDC__
284     int _strnicmp(char *c1, char *c2, int l){ return strnicmp(c1,c2,l);}
285     #endif
286 #else
287 // for gcc and CC
288     #ifdef XML_NO_WIDE_CHAR
289         char *myWideCharToMultiByte(const wchar_t *s, int l) { return NULL; }
290     #else
291         char *myWideCharToMultiByte(const wchar_t *s, int l)
292         {
293             const wchar_t *ss=s;
294             int i=(int)wcsrtombs(NULL,&ss,0,NULL);
295             if (i<0) return NULL;
296             char *d=(char *)malloc(i+1);
297             wcsrtombs(d,&s,i,NULL);
298             d[i]=0;
299             return d;
300         }
301     #endif
302     #ifdef _XMLUNICODE
303         wchar_t *myMultiByteToWideChar(const char *s, int l)
304         {
305             const char *ss=s;
306             int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
307             if (i<0) return NULL;
308             wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(wchar_t));
309             mbsrtowcs(d,&s,l,NULL);
310             d[i]=0;
311             return d;
312         }
313         int _tcslen(XMLCSTR c)   { return wcslen(c); }
314         #ifdef sun
315         // for CC
316            #include <widec.h>
317            int _tcsnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);}
318            int _tcsicmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); }
319         #else
320         // for gcc
321            int _tcsnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);}
322            int _tcsicmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); }
323         #endif
324         XMLSTR _tcsstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
325         XMLSTR _tcscpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
326         FILE *_tfopen(XMLCSTR filename,XMLCSTR mode)
327         {
328             char *filenameAscii=myWideCharToMultiByte(filename,0);
329             FILE *f;
330             if (mode[0]==_T('r')) f=fopen(filenameAscii,"rb");
331             else                  f=fopen(filenameAscii,"wb");
332             free(filenameAscii);
333             return f;
334         }
335     #else
336         FILE *_tfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
337         int _tcslen(XMLCSTR c)   { return strlen(c); }
338         int _tcsnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);}
339         int _tcsicmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); }
340         XMLSTR _tcsstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
341         XMLSTR _tcscpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
342     #endif
343     int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);}
344 #endif
345
346 /////////////////////////////////////////////////////////////////////////
347 //      Here start the core implementation of the XMLParser library    //
348 /////////////////////////////////////////////////////////////////////////
349
350 // You should normally not change anything below this point.
351 // For your own information, I suggest that you read the openFileHelper below:
352 XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
353 {
354     // guess the value of the global parameter "strictUTF8Parsing"
355     // (the guess is based on the first 200 bytes of the file).
356     FILE *f=_tfopen(filename,_T("rb"));
357     if (f)
358     {
359         char bb[205];
360         int l=(int)fread(bb,1,200,f);
361         setGlobalOptions(guessUnicodeChars,guessUTF8ParsingParameterValue(bb,l),dropWhiteSpace);
362         fclose(f);
363     }
364
365     // parse the file
366     XMLResults pResults;
367     XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
368
369     // display error message (if any)
370     if (pResults.error != eXMLErrorNone)
371     {
372         // create message
373         char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_T("");
374         if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; }
375         sprintf(message,
376 #ifdef _XMLUNICODE
377             "XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
378 #else
379             "XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
380 #endif
381             ,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3);
382
383         // display message
384 #if defined(WIN32) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
385 //                      MessageBoxA(NULL,message,"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST);
386         printf("%s",message);
387 #else
388         printf("%s",message);
389 #endif
390         exit(255);
391     }
392     return xnode;
393 }
394
395 #ifndef _XMLUNICODE
396 // If "strictUTF8Parsing=0" then we assume that all characters have the same length of 1 byte.
397 // If "strictUTF8Parsing=1" then the characters have different lengths (from 1 byte to 4 bytes).
398 // This table is used as lookup-table to know the length of a character (in byte) based on the
399 // content of the first byte of the character.
400 // (note: if you modify this, you must always have XML_utf8ByteTable[0]=0 ).
401 static const char XML_utf8ByteTable[256] =
402 {
403     //  0 1 2 3 4 5 6 7 8 9 a b c d e f
404     0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
405     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
406     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
407     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
408     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
409     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
410     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
411     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70End of ASCII range
412     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 0x80 to 0xc1 invalid
413     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
414     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
415     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
416     1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 0xc2 to 0xdf 2 byte
417     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
418     3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,// 0xe0 0xe0 to 0xef 3 byte
419     4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid
420 };
421 static const char XML_asciiByteTable[256] =
422 {
423     0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
424     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
425     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
426     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
427     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
428     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
429 };
430 static const char *XML_ByteTable=(const char *)XML_utf8ByteTable; // the default is "strictUTF8Parsing=1"
431 #endif
432
433 XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const
434 {
435   //printf("EED XMLNode::writeToFile 01\n");
436     int i;
437     XMLSTR t=createXMLString(nFormat,&i);
438     FILE *f=_tfopen(filename,_T("wb"));
439     if (!f) return eXMLErrorCannotOpenWriteFile;
440 #ifdef _XMLUNICODE
441     unsigned char h[2]={ 0xFF, 0xFE };
442     if (!fwrite(h,2,1,f)) return eXMLErrorCannotWriteFile;
443     if (!isDeclaration())
444     {
445         if (!fwrite(_T("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n"),sizeof(wchar_t)*40,1,f))
446             return eXMLErrorCannotWriteFile;
447     }
448 #else
449     if (!isDeclaration())
450     {
451         if ((!encoding)||(XML_ByteTable==XML_utf8ByteTable))
452         {
453             // header so that windows recognize the file as UTF-8:
454             unsigned char h[3]={0xEF,0xBB,0xBF};
455             if (!fwrite(h,3,1,f)) return eXMLErrorCannotWriteFile;
456             if (!fwrite("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",39,1,f)) return eXMLErrorCannotWriteFile;
457         }
458         else
459             if (fprintf(f,"<?xml version=\"1.0\" encoding=\"%s\"?>\n",encoding)<0) return eXMLErrorCannotWriteFile;
460     } else
461     {
462         if (XML_ByteTable==XML_utf8ByteTable) // test if strictUTF8Parsing==1"
463         {
464             unsigned char h[3]={0xEF,0xBB,0xBF}; if (!fwrite(h,3,1,f)) return eXMLErrorCannotWriteFile;
465         }
466     }
467 #endif
468     if (!fwrite(t,sizeof(XMLCHAR)*i,1,f)) return eXMLErrorCannotWriteFile;
469     //printf("EED XMLNode::writeToFile 02\n");
470     if (fclose(f)!=0) return eXMLErrorCannotWriteFile;
471     free(t);
472     return eXMLErrorNone;
473 }
474
475 // Duplicate a given string.
476 XMLSTR stringDup(XMLCSTR lpszData, int cbData)
477 {
478     if (lpszData==NULL) return NULL;
479
480     XMLSTR lpszNew;
481     if (cbData==0) cbData=(int)_tcslen(lpszData);
482     lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
483     if (lpszNew)
484     {
485         memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
486         lpszNew[cbData] = (XMLCHAR)NULL;
487     }
488     return lpszNew;
489 }
490
491 XMLNode XMLNode::emptyXMLNode;
492 XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL};
493 XMLAttribute XMLNode::emptyXMLAttribute={ NULL, NULL};
494
495 // Enumeration used to decipher what type a token is
496 typedef enum XMLTokenTypeTag
497 {
498     eTokenText = 0,
499     eTokenQuotedText,
500     eTokenTagStart,         /* "<"            */
501     eTokenTagEnd,           /* "</"           */
502     eTokenCloseTag,         /* ">"            */
503     eTokenEquals,           /* "="            */
504     eTokenDeclaration,      /* "<?"           */
505     eTokenShortHandClose,   /* "/>"           */
506     eTokenClear,
507     eTokenError
508 } XMLTokenType;
509
510 // Main structure used for parsing XML
511 typedef struct XML
512 {
513     XMLCSTR                lpXML;
514     XMLCSTR                lpszText;
515     int                    nIndex,nIndexMissigEndTag;
516     enum XMLError          error;
517     XMLCSTR                lpEndTag;
518     int                    cbEndTag;
519     XMLCSTR                lpNewElement;
520     int                    cbNewElement;
521     int                    nFirst;
522 } XML;
523
524 typedef struct
525 {
526     ALLXMLClearTag *pClr;
527     XMLCSTR     pStr;
528 } NextToken;
529
530 // Enumeration used when parsing attributes
531 typedef enum Attrib
532 {
533     eAttribName = 0,
534     eAttribEquals,
535     eAttribValue
536 } Attrib;
537
538 // Enumeration used when parsing elements to dictate whether we are currently
539 // inside a tag
540 typedef enum Status
541 {
542     eInsideTag = 0,
543     eOutsideTag
544 } Status;
545
546 // private (used while rendering):
547 XMLSTR toXMLString(XMLSTR dest,XMLCSTR source)
548 {
549     XMLSTR dd=dest;
550     XMLCHAR ch;
551     XMLCharacterEntity *entity;
552     while ((ch=*source))
553     {
554         entity=XMLEntities;
555         do
556         {
557             if (ch==entity->c) {_tcscpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; }
558             entity++;
559         } while(entity->s);
560 #ifdef _XMLUNICODE
561         *(dest++)=*(source++);
562 #else
563         switch(XML_ByteTable[(unsigned char)ch])
564         {
565         case 4: *(dest++)=*(source++);
566         case 3: *(dest++)=*(source++);
567         case 2: *(dest++)=*(source++);
568         case 1: *(dest++)=*(source++);
569         }
570 #endif
571 out_of_loop1:
572         ;
573     }
574     *dest=0;
575     return dd;
576 }
577
578 // private (used while rendering):
579 int lengthXMLString(XMLCSTR source)
580 {
581     int r=0;
582     XMLCharacterEntity *entity;
583     XMLCHAR ch;
584     while ((ch=*source))
585     {
586         entity=XMLEntities;
587         do
588         {
589             if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; }
590             entity++;
591         } while(entity->s);
592 #ifdef _XMLUNICODE
593         r++; source++;
594 #else
595         ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
596 #endif
597 out_of_loop1:
598         ;
599     }
600     return r;
601 }
602
603 XMLSTR toXMLString(XMLCSTR source)
604 {
605     XMLSTR dest=(XMLSTR)malloc((lengthXMLString(source)+1)*sizeof(XMLCHAR));
606     return toXMLString(dest,source);
607 }
608
609 XMLSTR toXMLStringFast(XMLSTR *dest,int *destSz, XMLCSTR source)
610 {
611     int l=lengthXMLString(source)+1;
612     if (l>*destSz) { *destSz=l; *dest=(XMLSTR)realloc(*dest,l*sizeof(XMLCHAR)); }
613     return toXMLString(*dest,source);
614 }
615
616 // private:
617 XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
618 {
619     // This function is the opposite of the function "toXMLString". It decodes the escape
620     // sequences &amp;, &quot;, &apos;, &lt;, &gt; and replace them by the characters
621     // &,",',<,>. This function is used internally by the XML Parser. All the calls to
622     // the XML library will always gives you back "decoded" strings.
623     //
624     // in: string (s) and length (lo) of string
625     // out:  new allocated string converted from xml
626     if (!s) return NULL;
627
628     int ll=0,j;
629     XMLSTR d;
630     XMLCSTR ss=s;
631     XMLCharacterEntity *entity;
632     while ((lo>0)&&(*s))
633     {
634         if (*s==_T('&'))
635         {
636             if ((lo>2)&&(s[1]==_T('#')))
637             {
638                 s+=2; lo-=2;
639                 if ((*s==_T('X'))||(*s==_T('x'))) { s++; lo--; }
640                 while ((*s)&&(*s!=_T(';'))&&((lo--)>0)) s++;
641                 if (*s!=_T(';'))
642                 {
643                     pXML->error=eXMLErrorUnknownCharacterEntity;
644                     return NULL;
645                 }
646                 s++; lo--;
647             } else
648             {
649                 entity=XMLEntities;
650                 do
651                 {
652                     if ((lo>=entity->l)&&(_tcsnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; }
653                     entity++;
654                 } while(entity->s);
655                 if (!entity->s)
656                 {
657                     pXML->error=eXMLErrorUnknownCharacterEntity;
658                     return NULL;
659                 }
660             }
661         } else
662         {
663 #ifdef _XMLUNICODE
664             s++; lo--;
665 #else
666             j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
667 #endif
668         }
669         ll++;
670     }
671
672     d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
673     s=d;
674     while (ll-->0)
675     {
676         if (*ss==_T('&'))
677         {
678             if (ss[1]==_T('#'))
679             {
680                 ss+=2; j=0;
681                 if ((*ss==_T('X'))||(*ss==_T('x')))
682                 {
683                     ss++;
684                     while (*ss!=_T(';'))
685                     {
686                         if ((*ss>=_T('0'))&&(*ss<=_T('9'))) j=(j<<4)+*ss-_T('0');
687                         else if ((*ss>=_T('A'))&&(*ss<=_T('F'))) j=(j<<4)+*ss-_T('A')+10;
688                         else if ((*ss>=_T('a'))&&(*ss<=_T('f'))) j=(j<<4)+*ss-_T('a')+10;
689                         else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
690                         ss++;
691                     }
692                 } else
693                 {
694                     while (*ss!=_T(';'))
695                     {
696                         if ((*ss>=_T('0'))&&(*ss<=_T('9'))) j=(j*10)+*ss-_T('0');
697                         else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
698                         ss++;
699                     }
700                 }
701                 (*d++)=(XMLCHAR)j; ss++;
702             } else
703             {
704                 entity=XMLEntities;
705                 do
706                 {
707                     if (_tcsnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; }
708                     entity++;
709                 } while(entity->s);
710             }
711         } else
712         {
713 #ifdef _XMLUNICODE
714             *(d++)=*(ss++);
715 #else
716             switch(XML_ByteTable[(unsigned char)*ss])
717             {
718             case 4: *(d++)=*(ss++); ll--;
719             case 3: *(d++)=*(ss++); ll--;
720             case 2: *(d++)=*(ss++); ll--;
721             case 1: *(d++)=*(ss++);
722             }
723 #endif
724         }
725     }
726     *d=0;
727     return (XMLSTR)s;
728 }
729
730 #define XML_isSPACECHAR(ch) ((ch==_T('\n'))||(ch==_T(' '))||(ch== _T('\t'))||(ch==_T('\r')))
731
732 // private:
733 char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
734 // !!!! WARNING strange convention&:
735 // return 0 if equals
736 // return 1 if different
737 {
738     if (!cclose) return 1;
739     int l=(int)_tcslen(cclose);
740     if (_tcsnicmp(cclose, copen, l)!=0) return 1;
741     const XMLCHAR c=copen[l];
742     if (XML_isSPACECHAR(c)||
743         (c==_T('/' ))||
744         (c==_T('<' ))||
745         (c==_T('>' ))||
746         (c==_T('=' ))) return 0;
747     return 1;
748 }
749
750 // Obtain the next character from the string.
751 static inline XMLCHAR getNextChar(XML *pXML)
752 {
753     XMLCHAR ch = pXML->lpXML[pXML->nIndex];
754 #ifdef _XMLUNICODE
755     if (ch!=0) pXML->nIndex++;
756 #else
757     pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
758 #endif
759     return ch;
760 }
761
762 // Find the next token in a string.
763 // pcbToken contains the number of characters that have been read.
764 static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
765 {
766     NextToken        result;
767     XMLCHAR            ch;
768     XMLCHAR            chTemp;
769     int              indexStart,nFoundMatch,nIsText=FALSE;
770     result.pClr=NULL; // prevent warning
771
772     // Find next non-white space character
773     do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch);
774
775     if (ch)
776     {
777         // Cache the current string pointer
778         result.pStr = &pXML->lpXML[indexStart];
779
780         // First check whether the token is in the clear tag list (meaning it
781         // does not need formatting).
782         ALLXMLClearTag *ctag=XMLClearTags;
783         do
784         {
785             if (_tcsnicmp(ctag->lpszOpen, result.pStr, ctag->openTagLen)==0)
786             {
787                 result.pClr=ctag;
788                 pXML->nIndex+=ctag->openTagLen-1;
789                 *pType=eTokenClear;
790                 return result;
791             }
792             ctag++;
793         } while(ctag->lpszOpen);
794
795         // If we didn't find a clear tag then check for standard tokens
796         switch(ch)
797         {
798         // Check for quotes
799         case _T('\''):
800         case _T('\"'):
801             // Type of token
802             *pType = eTokenQuotedText;
803             chTemp = ch;
804
805             // Set the size
806             nFoundMatch = FALSE;
807
808             // Search through the string to find a matching quote
809             while((ch = getNextChar(pXML)))
810             {
811                 if (ch==chTemp) { nFoundMatch = TRUE; break; }
812                 if (ch==_T('<')) break;
813             }
814
815             // If we failed to find a matching quote
816             if (nFoundMatch == FALSE)
817             {
818                 pXML->nIndex=indexStart+1;
819                 nIsText=TRUE;
820                 break;
821             }
822
823 //  4.02.2002
824 //            if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
825
826             break;
827
828         // Equals (used with attribute values)
829         case _T('='):
830             *pType = eTokenEquals;
831             break;
832
833         // Close tag
834         case _T('>'):
835             *pType = eTokenCloseTag;
836             break;
837
838         // Check for tag start and tag end
839         case _T('<'):
840
841             // Peek at the next character to see if we have an end tag '</',
842             // or an xml declaration '<?'
843             chTemp = pXML->lpXML[pXML->nIndex];
844
845             // If we have a tag end...
846             if (chTemp == _T('/'))
847             {
848                 // Set the type and ensure we point at the next character
849                 getNextChar(pXML);
850                 *pType = eTokenTagEnd;
851             }
852
853             // If we have an XML declaration tag
854             else if (chTemp == _T('?'))
855             {
856
857                 // Set the type and ensure we point at the next character
858                 getNextChar(pXML);
859                 *pType = eTokenDeclaration;
860             }
861
862             // Otherwise we must have a start tag
863             else
864             {
865                 *pType = eTokenTagStart;
866             }
867             break;
868
869         // Check to see if we have a short hand type end tag ('/>').
870         case _T('/'):
871
872             // Peek at the next character to see if we have a short end tag '/>'
873             chTemp = pXML->lpXML[pXML->nIndex];
874
875             // If we have a short hand end tag...
876             if (chTemp == _T('>'))
877             {
878                 // Set the type and ensure we point at the next character
879                 getNextChar(pXML);
880                 *pType = eTokenShortHandClose;
881                 break;
882             }
883
884             // If we haven't found a short hand closing tag then drop into the
885             // text process
886
887         // Other characters
888         default:
889             nIsText = TRUE;
890         }
891
892         // If this is a TEXT node
893         if (nIsText)
894         {
895             // Indicate we are dealing with text
896             *pType = eTokenText;
897             while((ch = getNextChar(pXML)))
898             {
899                 if XML_isSPACECHAR(ch)
900                 {
901                     indexStart++; break;
902
903                 } else if (ch==_T('/'))
904                 {
905                     // If we find a slash then this maybe text or a short hand end tag
906                     // Peek at the next character to see it we have short hand end tag
907                     ch=pXML->lpXML[pXML->nIndex];
908                     // If we found a short hand end tag then we need to exit the loop
909                     if (ch==_T('>')) { pXML->nIndex--; break; }
910
911                 } else if ((ch==_T('<'))||(ch==_T('>'))||(ch==_T('=')))
912                 {
913                     pXML->nIndex--; break;
914                 }
915             }
916         }
917         *pcbToken = pXML->nIndex-indexStart;
918     } else
919     {
920         // If we failed to obtain a valid character
921         *pcbToken = 0;
922         *pType = eTokenError;
923         result.pStr=NULL;
924     }
925
926     return result;
927 }
928
929 XMLCSTR XMLNode::updateName_WOSD(XMLCSTR lpszName)
930 {
931     if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName);
932     d->lpszName=lpszName;
933     return lpszName;
934 }
935
936 // private:
937 XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
938 XMLNode::XMLNode(XMLNodeData *pParent, XMLCSTR lpszName, char isDeclaration)
939 {
940     d=(XMLNodeData*)malloc(sizeof(XMLNodeData));
941     d->ref_count=1;
942
943     d->lpszName=NULL;
944     d->nChild= 0;
945     d->nText = 0;
946     d->nClear = 0;
947     d->nAttribute = 0;
948
949     d->isDeclaration = isDeclaration;
950
951     d->pParent = pParent;
952     d->pChild= NULL;
953     d->pText= NULL;
954     d->pClear= NULL;
955     d->pAttribute= NULL;
956     d->pOrder= NULL;
957
958     updateName_WOSD(lpszName);
959 }
960
961 XMLNode XMLNode::createXMLTopNode_WOSD(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); }
962 XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); }
963
964 #define MEMORYINCREASE 50
965
966 static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
967 {
968     if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
969     if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
970 //    if (!p)
971 //    {
972 //        printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
973 //    }
974     return p;
975 }
976
977 // private:
978 int XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xtype)
979 {
980     if (index<0) return -1;
981     int i=0,j=(int)((index<<2)+xtype),*o=d->pOrder; while (o[i]!=j) i++; return i;
982 }
983
984 // private:
985 // update "order" information when deleting a content of a XMLNode
986 int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
987 {
988     int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
989     memmove(o+i, o+i+1, (n-i)*sizeof(int));
990     for (;i<n;i++)
991         if ((o[i]&3)==(int)t) o[i]-=4;
992     // We should normally do:
993     // d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
994     // but we skip reallocation because it's too time consuming.
995     // Anyway, at the end, it will be free'd completely at once.
996     return i;
997 }
998
999 void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype)
1000 {
1001     //  in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
1002     // out: *_pos is the index inside p
1003     p=myRealloc(p,(nc+1),memoryIncrease,size);
1004     int n=d->nChild+d->nText+d->nClear;
1005     d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int));
1006     int pos=*_pos,*o=d->pOrder;
1007
1008     if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1009
1010     int i=pos;
1011     memmove(o+i+1, o+i, (n-i)*sizeof(int));
1012
1013     while ((pos<n)&&((o[pos]&3)!=(int)xtype)) pos++;
1014     if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1015
1016     o[i]=o[pos];
1017     for (i=pos+1;i<=n;i++) if ((o[i]&3)==(int)xtype) o[i]+=4;
1018
1019     *_pos=pos=o[pos]>>2;
1020     memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size);
1021
1022     return p;
1023 }
1024
1025 // Add a child node to the given element.
1026 XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLCSTR lpszName, char isDeclaration, int pos)
1027 {
1028     if (!lpszName) return emptyXMLNode;
1029     d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
1030     d->pChild[pos].d=NULL;
1031     d->pChild[pos]=XMLNode(d,lpszName,isDeclaration);
1032     d->nChild++;
1033     return d->pChild[pos];
1034 }
1035
1036 // Add an attribute to an element.
1037 XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLCSTR lpszName, XMLCSTR lpszValuev)
1038 {
1039     if (!lpszName) return &emptyXMLAttribute;
1040     int nc=d->nAttribute;
1041     d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute));
1042     XMLAttribute *pAttr=d->pAttribute+nc;
1043     pAttr->lpszName = lpszName;
1044     pAttr->lpszValue = lpszValuev;
1045     d->nAttribute++;
1046     return pAttr;
1047 }
1048
1049 // Add text to the element.
1050 XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLCSTR lpszValue, int pos)
1051 {
1052     if (!lpszValue) return NULL;
1053     d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText);
1054     d->pText[pos]=lpszValue;
1055     d->nText++;
1056     return lpszValue;
1057 }
1058
1059 // Add clear (unformatted) text to the element.
1060 XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
1061 {
1062     if (!lpszValue) return &emptyXMLClear;
1063     d->pClear=(XMLClear *)addToOrder(memoryIncrease,&pos,d->nClear,d->pClear,sizeof(XMLClear),eNodeClear);
1064     XMLClear *pNewClear=d->pClear+pos;
1065     pNewClear->lpszValue = lpszValue;
1066     if (!lpszOpen) lpszOpen=getClearTagTable()->lpszOpen;
1067     if (!lpszClose) lpszOpen=getClearTagTable()->lpszClose;
1068     pNewClear->lpszOpenTag = lpszOpen;
1069     pNewClear->lpszCloseTag = lpszClose;
1070     d->nClear++;
1071     return pNewClear;
1072 }
1073
1074 // private:
1075 // Parse a clear (unformatted) type node.
1076 char XMLNode::parseClearTag(void *px, ALLXMLClearTag *pClear)
1077 {
1078     XML *pXML=(XML *)px;
1079     int cbTemp=0;
1080     XMLCSTR lpszTemp=NULL;
1081     XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
1082     static XMLCSTR docTypeEnd=_T("]>");
1083
1084     // Find the closing tag
1085     // Seems the <!DOCTYPE need a better treatment so lets handle it
1086     if (pClear->lpszOpen==XMLClearTags[1].lpszOpen)
1087     {
1088         XMLCSTR pCh=lpXML;
1089         while (*pCh)
1090         {
1091             if (*pCh==_T('<')) { pClear->lpszClose=docTypeEnd; lpszTemp=_tcsstr(lpXML,docTypeEnd); break; }
1092             else if (*pCh==_T('>')) { lpszTemp=pCh; break; }
1093 #ifdef _XMLUNICODE
1094             pCh++;
1095 #else
1096             pCh+=XML_ByteTable[(unsigned char)(*pCh)];
1097 #endif
1098         }
1099     } else lpszTemp=_tcsstr(lpXML, pClear->lpszClose);
1100
1101     if (lpszTemp)
1102     {
1103         // Cache the size and increment the index
1104         cbTemp = (int)(lpszTemp - lpXML);
1105
1106         pXML->nIndex += cbTemp+(int)_tcslen(pClear->lpszClose);
1107
1108         // Add the clear node to the current element
1109         addClear_priv(MEMORYINCREASE,stringDup(lpXML,cbTemp), pClear->lpszOpen, pClear->lpszClose,-1);
1110         return 0;
1111     }
1112
1113     // If we failed to find the end tag
1114     pXML->error = eXMLErrorUnmatchedEndClearTag;
1115     return 1;
1116 }
1117
1118 void XMLNode::exactMemory(XMLNodeData *d)
1119 {
1120     if (d->pOrder)     d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int));
1121     if (d->pChild)     d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode));
1122     if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute));
1123     if (d->pText)      d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR));
1124     if (d->pClear)     d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear));
1125 }
1126
1127 char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
1128 {
1129     XML *pXML=(XML *)pa;
1130     XMLCSTR lpszText=pXML->lpszText;
1131     if (!lpszText) return 0;
1132     if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
1133     int cbText = (int)(tokenPStr - lpszText);
1134     if (!cbText) { pXML->lpszText=NULL; return 0; }
1135     if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
1136     if (!cbText) { pXML->lpszText=NULL; return 0; }
1137     lpszText=fromXMLString(lpszText,cbText,pXML);
1138     if (!lpszText) return 1;
1139     addText_priv(MEMORYINCREASE,lpszText,-1);
1140     pXML->lpszText=NULL;
1141     return 0;
1142 }
1143 // private:
1144 // Recursively parse an XML element.
1145 int XMLNode::ParseXMLElement(void *pa)
1146 {
1147     XML *pXML=(XML *)pa;
1148     int cbToken;
1149     enum XMLTokenTypeTag type;
1150     NextToken token;
1151     XMLCSTR lpszTemp=NULL;
1152     int cbTemp=0;
1153     char nDeclaration;
1154     XMLNode pNew;
1155     enum Status status; // inside or outside a tag
1156     enum Attrib attrib = eAttribName;
1157
1158     assert(pXML);
1159
1160     // If this is the first call to the function
1161     if (pXML->nFirst)
1162     {
1163         // Assume we are outside of a tag definition
1164         pXML->nFirst = FALSE;
1165         status = eOutsideTag;
1166     } else
1167     {
1168         // If this is not the first call then we should only be called when inside a tag.
1169         status = eInsideTag;
1170     }
1171
1172     // Iterate through the tokens in the document
1173     for(;;)
1174     {
1175         // Obtain the next token
1176         token = GetNextToken(pXML, &cbToken, &type);
1177
1178         if (type != eTokenError)
1179         {
1180             // Check the current status
1181             switch(status)
1182             {
1183
1184             // If we are outside of a tag definition
1185             case eOutsideTag:
1186
1187                 // Check what type of token we obtained
1188                 switch(type)
1189                 {
1190                 // If we have found text or quoted text
1191                 case eTokenText:
1192                 case eTokenCloseTag:          /* '>'         */
1193                 case eTokenShortHandClose:    /* '/>'        */
1194                 case eTokenQuotedText:
1195                 case eTokenEquals:
1196                     break;
1197
1198                 // If we found a start tag '<' and declarations '<?'
1199                 case eTokenTagStart:
1200                 case eTokenDeclaration:
1201
1202                     // Cache whether this new element is a declaration or not
1203                     nDeclaration = (type == eTokenDeclaration);
1204
1205                     // If we have node text then add this to the element
1206                     if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1207
1208                     // Find the name of the tag
1209                     token = GetNextToken(pXML, &cbToken, &type);
1210
1211                     // Return an error if we couldn't obtain the next token or
1212                     // it wasnt text
1213                     if (type != eTokenText)
1214                     {
1215                         pXML->error = eXMLErrorMissingTagName;
1216                         return FALSE;
1217                     }
1218
1219                     // If we found a new element which is the same as this
1220                     // element then we need to pass this back to the caller..
1221
1222 #ifdef APPROXIMATE_PARSING
1223                     if (d->lpszName &&
1224                         myTagCompare(d->lpszName, token.pStr) == 0)
1225                     {
1226                         // Indicate to the caller that it needs to create a
1227                         // new element.
1228                         pXML->lpNewElement = token.pStr;
1229                         pXML->cbNewElement = cbToken;
1230                         return TRUE;
1231                     } else
1232 #endif
1233                     {
1234                         // If the name of the new element differs from the name of
1235                         // the current element we need to add the new element to
1236                         // the current one and recurse
1237                         pNew = addChild_priv(MEMORYINCREASE,stringDup(token.pStr,cbToken), nDeclaration,-1);
1238
1239                         while (!pNew.isEmpty())
1240                         {
1241                             // Callself to process the new node.  If we return
1242                             // FALSE this means we dont have any more
1243                             // processing to do...
1244
1245                             if (!pNew.ParseXMLElement(pXML)) return FALSE;
1246                             else
1247                             {
1248                                 // If the call to recurse this function
1249                                 // evented in a end tag specified in XML then
1250                                 // we need to unwind the calls to this
1251                                 // function until we find the appropriate node
1252                                 // (the element name and end tag name must
1253                                 // match)
1254                                 if (pXML->cbEndTag)
1255                                 {
1256                                     // If we are back at the root node then we
1257                                     // have an unmatched end tag
1258                                     if (!d->lpszName)
1259                                     {
1260                                         pXML->error=eXMLErrorUnmatchedEndTag;
1261                                         return FALSE;
1262                                     }
1263
1264                                     // If the end tag matches the name of this
1265                                     // element then we only need to unwind
1266                                     // once more...
1267
1268                                     if (myTagCompare(d->lpszName, pXML->lpEndTag)==0)
1269                                     {
1270                                         pXML->cbEndTag = 0;
1271                                     }
1272
1273                                     return TRUE;
1274                                 } else
1275                                     if (pXML->cbNewElement)
1276                                     {
1277                                         // If the call indicated a new element is to
1278                                         // be created on THIS element.
1279
1280                                         // If the name of this element matches the
1281                                         // name of the element we need to create
1282                                         // then we need to return to the caller
1283                                         // and let it process the element.
1284
1285                                         if (myTagCompare(d->lpszName, pXML->lpNewElement)==0)
1286                                         {
1287                                             return TRUE;
1288                                         }
1289
1290                                         // Add the new element and recurse
1291                                         pNew = addChild_priv(MEMORYINCREASE,stringDup(pXML->lpNewElement,pXML->cbNewElement),0,-1);
1292                                         pXML->cbNewElement = 0;
1293                                     }
1294                                     else
1295                                     {
1296                                         // If we didn't have a new element to create
1297                                         pNew = emptyXMLNode;
1298
1299                                     }
1300                             }
1301                         }
1302                     }
1303                     break;
1304
1305                 // If we found an end tag
1306                 case eTokenTagEnd:
1307
1308                     // If we have node text then add this to the element
1309                     if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1310
1311                     // Find the name of the end tag
1312                     token = GetNextToken(pXML, &cbTemp, &type);
1313
1314                     // The end tag should be text
1315                     if (type != eTokenText)
1316                     {
1317                         pXML->error = eXMLErrorMissingEndTagName;
1318                         return FALSE;
1319                     }
1320                     lpszTemp = token.pStr;
1321
1322                     // After the end tag we should find a closing tag
1323                     token = GetNextToken(pXML, &cbToken, &type);
1324                     if (type != eTokenCloseTag)
1325                     {
1326                         pXML->error = eXMLErrorMissingEndTagName;
1327                         return FALSE;
1328                     }
1329                     pXML->lpszText=pXML->lpXML+pXML->nIndex;
1330
1331                     // We need to return to the previous caller.  If the name
1332                     // of the tag cannot be found we need to keep returning to
1333                     // caller until we find a match
1334                     if (myTagCompare(d->lpszName, lpszTemp) != 0)
1335 #ifdef STRICT_PARSING
1336                     {
1337                         pXML->error=eXMLErrorUnmatchedEndTag;
1338                         pXML->nIndexMissigEndTag=pXML->nIndex;
1339                         return FALSE;
1340                     }
1341 #else
1342                     {
1343                         pXML->error=eXMLErrorMissingEndTag;
1344                         pXML->nIndexMissigEndTag=pXML->nIndex;
1345                         pXML->lpEndTag = lpszTemp;
1346                         pXML->cbEndTag = cbTemp;
1347                     }
1348 #endif
1349
1350                     // Return to the caller
1351                     exactMemory(d);
1352                     return TRUE;
1353
1354                 // If we found a clear (unformatted) token
1355                 case eTokenClear:
1356                     // If we have node text then add this to the element
1357                     if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1358                     if (parseClearTag(pXML, token.pClr)) return FALSE;
1359                     pXML->lpszText=pXML->lpXML+pXML->nIndex;
1360                     break;
1361
1362                 default:
1363                     break;
1364                 }
1365                 break;
1366
1367             // If we are inside a tag definition we need to search for attributes
1368             case eInsideTag:
1369
1370                 // Check what part of the attribute (name, equals, value) we
1371                 // are looking for.
1372                 switch(attrib)
1373                 {
1374                 // If we are looking for a new attribute
1375                 case eAttribName:
1376
1377                     // Check what the current token type is
1378                     switch(type)
1379                     {
1380                     // If the current type is text...
1381                     // Eg.  'attribute'
1382                     case eTokenText:
1383                         // Cache the token then indicate that we are next to
1384                         // look for the equals
1385                         lpszTemp = token.pStr;
1386                         cbTemp = cbToken;
1387                         attrib = eAttribEquals;
1388                         break;
1389
1390                     // If we found a closing tag...
1391                     // Eg.  '>'
1392                     case eTokenCloseTag:
1393                         // We are now outside the tag
1394                         status = eOutsideTag;
1395                         pXML->lpszText=pXML->lpXML+pXML->nIndex;
1396                         break;
1397
1398                     // If we found a short hand '/>' closing tag then we can
1399                     // return to the caller
1400                     case eTokenShortHandClose:
1401                         exactMemory(d);
1402                         pXML->lpszText=pXML->lpXML+pXML->nIndex;
1403                         return TRUE;
1404
1405                     // Errors...
1406                     case eTokenQuotedText:    /* '"SomeText"'   */
1407                     case eTokenTagStart:      /* '<'            */
1408                     case eTokenTagEnd:        /* '</'           */
1409                     case eTokenEquals:        /* '='            */
1410                     case eTokenDeclaration:   /* '<?'           */
1411                     case eTokenClear:
1412                         pXML->error = eXMLErrorUnexpectedToken;
1413                         return FALSE;
1414                     default: break;
1415                     }
1416                     break;
1417
1418                 // If we are looking for an equals
1419                 case eAttribEquals:
1420                     // Check what the current token type is
1421                     switch(type)
1422                     {
1423                     // If the current type is text...
1424                     // Eg.  'Attribute AnotherAttribute'
1425                     case eTokenText:
1426                         // Add the unvalued attribute to the list
1427                         addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1428                         // Cache the token then indicate.  We are next to
1429                         // look for the equals attribute
1430                         lpszTemp = token.pStr;
1431                         cbTemp = cbToken;
1432                         break;
1433
1434                     // If we found a closing tag 'Attribute >' or a short hand
1435                     // closing tag 'Attribute />'
1436                     case eTokenShortHandClose:
1437                     case eTokenCloseTag:
1438                         // If we are a declaration element '<?' then we need
1439                         // to remove extra closing '?' if it exists
1440                         pXML->lpszText=pXML->lpXML+pXML->nIndex;
1441
1442                         if (d->isDeclaration &&
1443                             (lpszTemp[cbTemp-1]) == _T('?'))
1444                         {
1445                             cbTemp--;
1446                         }
1447
1448                         if (cbTemp)
1449                         {
1450                             // Add the unvalued attribute to the list
1451                             addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1452                         }
1453
1454                         // If this is the end of the tag then return to the caller
1455                         if (type == eTokenShortHandClose)
1456                         {
1457                             exactMemory(d);
1458                             return TRUE;
1459                         }
1460
1461                         // We are now outside the tag
1462                         status = eOutsideTag;
1463                         break;
1464
1465                     // If we found the equals token...
1466                     // Eg.  'Attribute ='
1467                     case eTokenEquals:
1468                         // Indicate that we next need to search for the value
1469                         // for the attribute
1470                         attrib = eAttribValue;
1471                         break;
1472
1473                     // Errors...
1474                     case eTokenQuotedText:    /* 'Attribute "InvalidAttr"'*/
1475                     case eTokenTagStart:      /* 'Attribute <'            */
1476                     case eTokenTagEnd:        /* 'Attribute </'           */
1477                     case eTokenDeclaration:   /* 'Attribute <?'           */
1478                     case eTokenClear:
1479                         pXML->error = eXMLErrorUnexpectedToken;
1480                         return FALSE;
1481                     default: break;
1482                     }
1483                     break;
1484
1485                 // If we are looking for an attribute value
1486                 case eAttribValue:
1487                     // Check what the current token type is
1488                     switch(type)
1489                     {
1490                     // If the current type is text or quoted text...
1491                     // Eg.  'Attribute = "Value"' or 'Attribute = Value' or
1492                     // 'Attribute = 'Value''.
1493                     case eTokenText:
1494                     case eTokenQuotedText:
1495                         // If we are a declaration element '<?' then we need
1496                         // to remove extra closing '?' if it exists
1497                         if (d->isDeclaration &&
1498                             (token.pStr[cbToken-1]) == _T('?'))
1499                         {
1500                             cbToken--;
1501                         }
1502
1503                         if (cbTemp)
1504                         {
1505                             // Add the valued attribute to the list
1506                             if (type==eTokenQuotedText) { token.pStr++; cbToken-=2; }
1507                             XMLCSTR attrVal=token.pStr;
1508                             if (attrVal)
1509                             {
1510                                 attrVal=fromXMLString(attrVal,cbToken,pXML);
1511                                 if (!attrVal) return FALSE;
1512                             }
1513                             addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp),attrVal);
1514                         }
1515
1516                         // Indicate we are searching for a new attribute
1517                         attrib = eAttribName;
1518                         break;
1519
1520                     // Errors...
1521                     case eTokenTagStart:        /* 'Attr = <'          */
1522                     case eTokenTagEnd:          /* 'Attr = </'         */
1523                     case eTokenCloseTag:        /* 'Attr = >'          */
1524                     case eTokenShortHandClose:  /* "Attr = />"         */
1525                     case eTokenEquals:          /* 'Attr = ='          */
1526                     case eTokenDeclaration:     /* 'Attr = <?'         */
1527                     case eTokenClear:
1528                         pXML->error = eXMLErrorUnexpectedToken;
1529                         return FALSE;
1530                         break;
1531                     default: break;
1532                     }
1533                 }
1534             }
1535         }
1536         // If we failed to obtain the next token
1537         else
1538         {
1539             if ((!d->isDeclaration)&&(d->pParent))
1540             {
1541 #ifdef STRICT_PARSING
1542                 pXML->error=eXMLErrorUnmatchedEndTag;
1543 #else
1544                 pXML->error=eXMLErrorMissingEndTag;
1545 #endif
1546                 pXML->nIndexMissigEndTag=pXML->nIndex;
1547             }
1548             return FALSE;
1549         }
1550     }
1551 }
1552
1553 // Count the number of lines and columns in an XML string.
1554 static void CountLinesAndColumns(XMLCSTR lpXML, int nUpto, XMLResults *pResults)
1555 {
1556     XMLCHAR ch;
1557     assert(lpXML);
1558     assert(pResults);
1559
1560     struct XML xml={ lpXML,lpXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1561
1562     pResults->nLine = 1;
1563     pResults->nColumn = 1;
1564     while (xml.nIndex<nUpto)
1565     {
1566         ch = getNextChar(&xml);
1567         if (ch != _T('\n')) pResults->nColumn++;
1568         else
1569         {
1570             pResults->nLine++;
1571             pResults->nColumn=1;
1572         }
1573     }
1574 }
1575
1576 // Parse XML and return the root element.
1577 XMLNode XMLNode::parseString(XMLCSTR lpszXML, XMLCSTR tag, XMLResults *pResults)
1578 {
1579     if (!lpszXML)
1580     {
1581         if (pResults)
1582         {
1583             pResults->error=eXMLErrorNoElements;
1584             pResults->nLine=0;
1585             pResults->nColumn=0;
1586         }
1587         return emptyXMLNode;
1588     }
1589
1590     XMLNode xnode(NULL,NULL,FALSE);
1591     struct XML xml={ lpszXML, lpszXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1592
1593     // Create header element
1594     xnode.ParseXMLElement(&xml);
1595     enum XMLError error = xml.error;
1596     if ((xnode.nChildNode()==1)&&(xnode.nElement()==1)) xnode=xnode.getChildNode(); // skip the empty node
1597
1598     // If no error occurred
1599     if ((error==eXMLErrorNone)||(error==eXMLErrorMissingEndTag))
1600     {
1601         XMLCSTR name=xnode.getName();
1602         if (tag&&_tcslen(tag)&&((!name)||(_tcsicmp(xnode.getName(),tag))))
1603         {
1604             XMLNode nodeTmp;
1605             int i=0;
1606             while (i<xnode.nChildNode())
1607             {
1608                 nodeTmp=xnode.getChildNode(i);
1609                 if (_tcsicmp(nodeTmp.getName(),tag)==0) break;
1610                 if (nodeTmp.isDeclaration()) { xnode=nodeTmp; i=0; } else i++;
1611             }
1612             if (i>=xnode.nChildNode())
1613             {
1614                 if (pResults)
1615                 {
1616                     pResults->error=eXMLErrorFirstTagNotFound;
1617                     pResults->nLine=0;
1618                     pResults->nColumn=0;
1619                 }
1620                 return emptyXMLNode;
1621             }
1622             xnode=nodeTmp;
1623         }
1624     } else
1625     {
1626         // Cleanup: this will destroy all the nodes
1627         xnode = emptyXMLNode;
1628     }
1629
1630
1631     // If we have been given somewhere to place results
1632     if (pResults)
1633     {
1634         pResults->error = error;
1635
1636         // If we have an error
1637         if (error!=eXMLErrorNone)
1638         {
1639             if (error==eXMLErrorMissingEndTag) xml.nIndex=xml.nIndexMissigEndTag;
1640             // Find which line and column it starts on.
1641             CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
1642         }
1643     }
1644     return xnode;
1645 }
1646
1647 XMLNode XMLNode::parseFile(XMLCSTR filename, XMLCSTR tag, XMLResults *pResults)
1648 {
1649     if (pResults) { pResults->nLine=0; pResults->nColumn=0; }
1650     FILE *f=_tfopen(filename,_T("rb"));
1651     if (f==NULL) { if (pResults) pResults->error=eXMLErrorFileNotFound; return emptyXMLNode; }
1652     fseek(f,0,SEEK_END);
1653     int l=ftell(f),headerSz=0;
1654     if (!l) { if (pResults) pResults->error=eXMLErrorEmpty; return emptyXMLNode; }
1655     fseek(f,0,SEEK_SET);
1656     unsigned char *buf=(unsigned char*)malloc(l+1);
1657     fread(buf,l,1,f);
1658     fclose(f);
1659     buf[l]=0;
1660 #ifdef _XMLUNICODE
1661     if (guessUnicodeChars)
1662     {
1663         if (!myIsTextUnicode(buf,l))
1664         {
1665             if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1666             XMLSTR b2=myMultiByteToWideChar((const char*)(buf+headerSz),l-headerSz);
1667             free(buf); buf=(unsigned char*)b2; headerSz=0;
1668         } else
1669         {
1670             if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1671             if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1672         }
1673     }
1674 #else
1675     if (guessUnicodeChars)
1676     {
1677         if (myIsTextUnicode(buf,l))
1678         {
1679             l/=sizeof(wchar_t);
1680             if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1681             if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1682             char *b2=myWideCharToMultiByte((const wchar_t*)(buf+headerSz),l-headerSz);
1683             free(buf); buf=(unsigned char*)b2; headerSz=0;
1684         } else
1685         {
1686             if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1687         }
1688     }
1689 #endif
1690
1691     if (!buf) { if (pResults) pResults->error=eXMLErrorCharConversionError; return emptyXMLNode; }
1692     XMLNode x=parseString((XMLSTR)(buf+headerSz),tag,pResults);
1693     free(buf);
1694     return x;
1695 }
1696
1697 static inline void charmemset(XMLSTR dest,XMLCHAR c,int l) { while (l--) *(dest++)=c; }
1698 // private:
1699 // Creates an user friendly XML string from a given element with
1700 // appropriate white space and carriage returns.
1701 //
1702 // This recurses through all subnodes then adds contents of the nodes to the
1703 // string.
1704 int XMLNode::CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat)
1705 {
1706     int nResult = 0;
1707     int cb;
1708     int cbElement;
1709     int nChildFormat=-1;
1710     int nElementI=pEntry->nChild+pEntry->nText+pEntry->nClear;
1711     int i,j;
1712
1713     assert(pEntry);
1714
1715 #define LENSTR(lpsz) (lpsz ? _tcslen(lpsz) : 0)
1716
1717     // If the element has no name then assume this is the head node.
1718     cbElement = (int)LENSTR(pEntry->lpszName);
1719
1720     if (cbElement)
1721     {
1722         // "<elementname "
1723         cb = nFormat == -1 ? 0 : nFormat;
1724
1725         if (lpszMarker)
1726         {
1727             if (cb) charmemset(lpszMarker, INDENTCHAR, sizeof(XMLCHAR)*cb);
1728             nResult = cb;
1729             lpszMarker[nResult++]=_T('<');
1730             if (pEntry->isDeclaration) lpszMarker[nResult++]=_T('?');
1731             _tcscpy(&lpszMarker[nResult], pEntry->lpszName);
1732             nResult+=cbElement;
1733             lpszMarker[nResult++]=_T(' ');
1734
1735         } else
1736         {
1737             nResult+=cbElement+2+cb;
1738             if (pEntry->isDeclaration) nResult++;
1739         }
1740
1741         // Enumerate attributes and add them to the string
1742         XMLAttribute *pAttr=pEntry->pAttribute;
1743         for (i=0; i<pEntry->nAttribute; i++)
1744         {
1745             // "Attrib
1746             cb = (int)LENSTR(pAttr->lpszName);
1747             if (cb)
1748             {
1749                 if (lpszMarker) _tcscpy(&lpszMarker[nResult], pAttr->lpszName);
1750                 nResult += cb;
1751                 // "Attrib=Value "
1752                 if (pAttr->lpszValue)
1753                 {
1754                     cb=(int)lengthXMLString(pAttr->lpszValue);
1755                     if (lpszMarker)
1756                     {
1757                         lpszMarker[nResult]=_T('=');
1758                         lpszMarker[nResult+1]=_T('"');
1759                         if (cb) toXMLString(&lpszMarker[nResult+2],pAttr->lpszValue);
1760                         lpszMarker[nResult+cb+2]=_T('"');
1761                     }
1762                     nResult+=cb+3;
1763                 }
1764                 if (lpszMarker) lpszMarker[nResult] = _T(' ');
1765                 nResult++;
1766             }
1767             pAttr++;
1768         }
1769
1770         if (pEntry->isDeclaration)
1771         {
1772             if (lpszMarker)
1773             {
1774                 lpszMarker[nResult-1]=_T('?');
1775                 lpszMarker[nResult]=_T('>');
1776             }
1777             nResult++;
1778             if (nFormat!=-1)
1779             {
1780                 if (lpszMarker) lpszMarker[nResult]=_T('\n');
1781                 nResult++;
1782             }
1783         } else
1784             // If there are child nodes we need to terminate the start tag
1785             if (nElementI)
1786             {
1787                 if (lpszMarker) lpszMarker[nResult-1]=_T('>');
1788                 if (nFormat!=-1)
1789                 {
1790                     if (lpszMarker) lpszMarker[nResult]=_T('\n');
1791                     nResult++;
1792                 }
1793             } else nResult--;
1794     }
1795
1796     // Calculate the child format for when we recurse.  This is used to
1797     // determine the number of spaces used for prefixes.
1798     if (nFormat!=-1)
1799     {
1800         if (cbElement&&(!pEntry->isDeclaration)) nChildFormat=nFormat+1;
1801         else nChildFormat=nFormat;
1802     }
1803
1804     // Enumerate through remaining children
1805     for (i=0; i<nElementI; i++)
1806     {
1807         j=pEntry->pOrder[i];
1808         switch((XMLElementType)(j&3))
1809         {
1810         // Text nodes
1811         case eNodeText:
1812             {
1813                 // "Text"
1814                 XMLCSTR pChild=pEntry->pText[j>>2];
1815                 cb = (int)lengthXMLString(pChild);
1816                 if (cb)
1817                 {
1818                     if (nFormat!=-1)
1819                     {
1820                         if (lpszMarker)
1821                         {
1822                             charmemset(&lpszMarker[nResult],INDENTCHAR,sizeof(XMLCHAR)*(nFormat + 1));
1823                             toXMLString(&lpszMarker[nResult+nFormat+1],pChild);
1824                             lpszMarker[nResult+nFormat+1+cb]=_T('\n');
1825                         }
1826                         nResult+=cb+nFormat+2;
1827                     } else
1828                     {
1829                         if (lpszMarker) toXMLString(&lpszMarker[nResult], pChild);
1830                         nResult += cb;
1831                     }
1832                 }
1833                 break;
1834             }
1835
1836         // Clear type nodes
1837         case eNodeClear:
1838             {
1839                 XMLClear *pChild=pEntry->pClear+(j>>2);
1840                 // "OpenTag"
1841                 cb = (int)LENSTR(pChild->lpszOpenTag);
1842                 if (cb)
1843                 {
1844                     if (nFormat!=-1)
1845                     {
1846                         if (lpszMarker)
1847                         {
1848                             charmemset(&lpszMarker[nResult], INDENTCHAR, sizeof(XMLCHAR)*(nFormat + 1));
1849                             _tcscpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag);
1850                         }
1851                         nResult+=cb+nFormat+1;
1852                     }
1853                     else
1854                     {
1855                         if (lpszMarker)_tcscpy(&lpszMarker[nResult], pChild->lpszOpenTag);
1856                         nResult += cb;
1857                     }
1858                 }
1859
1860                 // "OpenTag Value"
1861                 cb = (int)LENSTR(pChild->lpszValue);
1862                 if (cb)
1863                 {
1864                     if (lpszMarker) _tcscpy(&lpszMarker[nResult], pChild->lpszValue);
1865                     nResult += cb;
1866                 }
1867
1868                 // "OpenTag Value CloseTag"
1869                 cb = (int)LENSTR(pChild->lpszCloseTag);
1870                 if (cb)
1871                 {
1872                     if (lpszMarker) _tcscpy(&lpszMarker[nResult], pChild->lpszCloseTag);
1873                     nResult += cb;
1874                 }
1875
1876                 if (nFormat!=-1)
1877                 {
1878                     if (lpszMarker) lpszMarker[nResult] = _T('\n');
1879                     nResult++;
1880                 }
1881                 break;
1882             }
1883
1884         // Element nodes
1885         case eNodeChild:
1886             {
1887                 // Recursively add child nodes
1888                 nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
1889                 break;
1890             }
1891         default: break;
1892         }
1893     }
1894
1895     if ((cbElement)&&(!pEntry->isDeclaration))
1896     {
1897         // If we have child entries we need to use long XML notation for
1898         // closing the element - "<elementname>blah blah blah</elementname>"
1899         if (nElementI)
1900         {
1901             // "</elementname>\0"
1902             if (lpszMarker)
1903             {
1904                 if (nFormat != -1)
1905                 {
1906                     if (nFormat)
1907                     {
1908                         charmemset(&lpszMarker[nResult], INDENTCHAR,sizeof(XMLCHAR)*nFormat);
1909                         nResult+=nFormat;
1910                     }
1911                 }
1912
1913                 _tcscpy(&lpszMarker[nResult], _T("</"));
1914                 nResult += 2;
1915                 _tcscpy(&lpszMarker[nResult], pEntry->lpszName);
1916                 nResult += cbElement;
1917
1918                 if (nFormat == -1)
1919                 {
1920                     _tcscpy(&lpszMarker[nResult], _T(">"));
1921                     nResult++;
1922                 } else
1923                 {
1924                     _tcscpy(&lpszMarker[nResult], _T(">\n"));
1925                     nResult+=2;
1926                 }
1927             } else
1928             {
1929                 if (nFormat != -1) nResult+=cbElement+4+nFormat;
1930                 else nResult+=cbElement+3;
1931             }
1932         } else
1933         {
1934             // If there are no children we can use shorthand XML notation -
1935             // "<elementname/>"
1936             // "/>\0"
1937             if (lpszMarker)
1938             {
1939                 if (nFormat == -1)
1940                 {
1941                     _tcscpy(&lpszMarker[nResult], _T("/>"));
1942                     nResult += 2;
1943                 }
1944                 else
1945                 {
1946                     _tcscpy(&lpszMarker[nResult], _T("/>\n"));
1947                     nResult += 3;
1948                 }
1949             }
1950             else
1951             {
1952                 nResult += nFormat == -1 ? 2 : 3;
1953             }
1954         }
1955     }
1956
1957     return nResult;
1958 }
1959
1960 #undef LENSTR
1961
1962 // Create an XML string
1963 // @param       int nFormat             - 0 if no formatting is required
1964 //                                        otherwise nonzero for formatted text
1965 //                                        with carriage returns and indentation.
1966 // @param       int *pnSize             - [out] pointer to the size of the
1967 //                                        returned string not including the
1968 //                                        NULL terminator.
1969 // @return      XMLSTR                  - Allocated XML string, you must free
1970 //                                        this with free().
1971 XMLSTR XMLNode::createXMLString(int nFormat, int *pnSize) const
1972 {
1973     if (!d) { if (pnSize) *pnSize=0; return NULL; }
1974
1975     XMLSTR lpszResult = NULL;
1976     int cbStr;
1977
1978     // Recursively Calculate the size of the XML string
1979     if (!dropWhiteSpace) nFormat=0;
1980     nFormat = nFormat ? 0 : -1;
1981     cbStr = CreateXMLStringR(d, 0, nFormat);
1982     assert(cbStr);
1983     // Alllocate memory for the XML string + the NULL terminator and
1984     // create the recursively XML string.
1985     lpszResult=(XMLSTR)malloc((cbStr+1)*sizeof(XMLCHAR));
1986     CreateXMLStringR(d, lpszResult, nFormat);
1987     if (pnSize) *pnSize = cbStr;
1988     return lpszResult;
1989 }
1990
1991 XMLNode::~XMLNode() { deleteNodeContent(); }
1992
1993 int XMLNode::detachFromParent(XMLNodeData *d)
1994 {
1995     XMLNode *pa=d->pParent->pChild;
1996     int i=0;
1997     while (((void*)(pa[i].d))!=((void*)d)) i++;
1998     d->pParent->nChild--;
1999     if (d->pParent->nChild) memmove(pa+i,pa+i+1,(d->pParent->nChild-i)*sizeof(XMLNode));
2000     else { free(pa); d->pParent->pChild=NULL; }
2001     return removeOrderElement(d->pParent,eNodeChild,i);
2002 }
2003
2004 void XMLNode::deleteNodeContent(char force)
2005 {
2006     if (!d) return;
2007     (d->ref_count) --;
2008     if ((d->ref_count==0)||force)
2009     {
2010         int i;
2011         if (d->pParent) detachFromParent(d);
2012         for(i=0; i<d->nChild; i++) { d->pChild[i].d->pParent=NULL; d->pChild[i].deleteNodeContent(force); }
2013         free(d->pChild);
2014         for(i=0; i<d->nText; i++) free((void*)d->pText[i]);
2015         free(d->pText);
2016         for(i=0; i<d->nClear; i++) free((void*)d->pClear[i].lpszValue);
2017         free(d->pClear);
2018         for(i=0; i<d->nAttribute; i++)
2019         {
2020             free((void*)d->pAttribute[i].lpszName);
2021             if (d->pAttribute[i].lpszValue) free((void*)d->pAttribute[i].lpszValue);
2022         }
2023         free(d->pAttribute);
2024         free(d->pOrder);
2025         free((void*)d->lpszName);
2026         free(d);
2027         d=NULL;
2028     }
2029 }
2030
2031 XMLNode XMLNode::addChild(XMLNode childNode, int pos)
2032 {
2033     XMLNodeData *dc=childNode.d;
2034     if ((!dc)||(!d)) return childNode;
2035     if (dc->pParent) { if ((detachFromParent(dc)<=pos)&&(dc->pParent==d)) pos--; } else dc->ref_count++;
2036     dc->pParent=d;
2037 //     int nc=d->nChild;
2038 //     d->pChild=(XMLNode*)myRealloc(d->pChild,(nc+1),memoryIncrease,sizeof(XMLNode));
2039     d->pChild=(XMLNode*)addToOrder(0,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
2040     d->pChild[pos].d=dc;
2041     d->nChild++;
2042     return childNode;
2043 }
2044
2045 void XMLNode::deleteAttribute(int i)
2046 {
2047     if ((!d)||(i<0)||(i>=d->nAttribute)) return;
2048     d->nAttribute--;
2049     XMLAttribute *p=d->pAttribute+i;
2050     free((void*)p->lpszName);
2051     if (p->lpszValue) free((void*)p->lpszValue);
2052     if (d->nAttribute) memmove(p,p+1,(d->nAttribute-i)*sizeof(XMLAttribute)); else { free(p); d->pAttribute=NULL; }
2053 }
2054
2055 void XMLNode::deleteAttribute(XMLAttribute *a){ if (a) deleteAttribute(a->lpszName); }
2056 void XMLNode::deleteAttribute(XMLCSTR lpszName)
2057 {
2058     int j=0;
2059     getAttribute(lpszName,&j);
2060     if (j) deleteAttribute(j-1);
2061 }
2062
2063 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,int i)
2064 {
2065     if (!d) return NULL;
2066     if (i>=d->nAttribute)
2067     {
2068         if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2069         return NULL;
2070     }
2071     XMLAttribute *p=d->pAttribute+i;
2072     if (p->lpszValue&&p->lpszValue!=lpszNewValue) free((void*)p->lpszValue);
2073     p->lpszValue=lpszNewValue;
2074     if (lpszNewName&&p->lpszName!=lpszNewName) { free((void*)p->lpszName); p->lpszName=lpszNewName; };
2075     return p;
2076 }
2077
2078 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2079 {
2080     if (oldAttribute) return updateAttribute_WOSD(newAttribute->lpszValue,newAttribute->lpszName,oldAttribute->lpszName);
2081     return addAttribute_WOSD(newAttribute->lpszName,newAttribute->lpszValue);
2082 }
2083
2084 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName)
2085 {
2086     int j=0;
2087     getAttribute(lpszOldName,&j);
2088     if (j) return updateAttribute_WOSD(lpszNewValue,lpszNewName,j-1);
2089     else
2090     {
2091         if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2092         else             return addAttribute_WOSD(stringDup(lpszOldName),lpszNewValue);
2093     }
2094 }
2095
2096 int XMLNode::indexText(XMLCSTR lpszValue) const
2097 {
2098     if (!d) return -1;
2099     int i,l=d->nText;
2100     if (!lpszValue) { if (l) return 0; return -1; }
2101     XMLCSTR *p=d->pText;
2102     for (i=0; i<l; i++) if (lpszValue==p[i]) return i;
2103     return -1;
2104 }
2105
2106 void XMLNode::deleteText(int i)
2107 {
2108     if ((!d)||(i<0)||(i>=d->nText)) return;
2109     d->nText--;
2110     XMLCSTR *p=d->pText+i;
2111     free((void*)*p);
2112     if (d->nText) memmove(p,p+1,(d->nText-i)*sizeof(XMLCSTR)); else { free(p); d->pText=NULL; }
2113     removeOrderElement(d,eNodeText,i);
2114 }
2115
2116 void XMLNode::deleteText(XMLCSTR lpszValue) { deleteText(indexText(lpszValue)); }
2117
2118 XMLCSTR XMLNode::updateText_WOSD(XMLCSTR lpszNewValue, int i)
2119 {
2120     if (!d) return NULL;
2121     if (i>=d->nText) return addText_WOSD(lpszNewValue);
2122     XMLCSTR *p=d->pText+i;
2123     if (*p!=lpszNewValue) { free((void*)*p); *p=lpszNewValue; }
2124     return lpszNewValue;
2125 }
2126
2127 XMLCSTR XMLNode::updateText_WOSD(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2128 {
2129     if (!d) return NULL;
2130     int i=indexText(lpszOldValue);
2131     if (i>=0) return updateText_WOSD(lpszNewValue,i);
2132     return addText_WOSD(lpszNewValue);
2133 }
2134
2135 void XMLNode::deleteClear(int i)
2136 {
2137     if ((!d)||(i<0)||(i>=d->nClear)) return;
2138     d->nClear--;
2139     XMLClear *p=d->pClear+i;
2140     free((void*)p->lpszValue);
2141     if (d->nClear) memmove(p,p+1,(d->nText-i)*sizeof(XMLClear)); else { free(p); d->pClear=NULL; }
2142     removeOrderElement(d,eNodeClear,i);
2143 }
2144
2145 int XMLNode::indexClear(XMLCSTR lpszValue) const
2146 {
2147     if (!d) return -1;
2148     int i,l=d->nClear;
2149     if (!lpszValue) { if (l) return 0; return -1; }
2150     XMLClear *p=d->pClear;
2151     for (i=0; i<l; i++) if (lpszValue==p[i].lpszValue) return i;
2152     return -1;
2153 }
2154
2155 void XMLNode::deleteClear(XMLCSTR lpszValue) { deleteClear(indexClear(lpszValue)); }
2156 void XMLNode::deleteClear(XMLClear *a) { if (a) deleteClear(a->lpszValue); }
2157
2158 XMLClear *XMLNode::updateClear_WOSD(XMLCSTR lpszNewContent, int i)
2159 {
2160     if (!d) return NULL;
2161     if (i>=d->nClear)
2162     {
2163         return addClear_WOSD(XMLClearTags[0].lpszOpen,lpszNewContent,XMLClearTags[0].lpszClose);
2164     }
2165     XMLClear *p=d->pClear+i;
2166     if (lpszNewContent!=p->lpszValue) { free((void*)p->lpszValue); p->lpszValue=lpszNewContent; }
2167     return p;
2168 }
2169
2170 XMLClear *XMLNode::updateClear_WOSD(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2171 {
2172     if (!d) return NULL;
2173     int i=indexClear(lpszOldValue);
2174     if (i>=0) return updateClear_WOSD(lpszNewValue,i);
2175     return addClear_WOSD(lpszNewValue,XMLClearTags[0].lpszOpen,XMLClearTags[0].lpszClose);
2176 }
2177
2178 XMLClear *XMLNode::updateClear_WOSD(XMLClear *newP,XMLClear *oldP)
2179 {
2180     if (oldP) return updateClear_WOSD(newP->lpszValue,oldP->lpszValue);
2181     return NULL;
2182 }
2183
2184 XMLNode& XMLNode::operator=( const XMLNode& A )
2185 {
2186     // shallow copy
2187     if (this != &A)
2188     {
2189         deleteNodeContent();
2190         d=A.d;
2191         if (d) (d->ref_count) ++ ;
2192     }
2193     return *this;
2194 }
2195
2196 XMLNode::XMLNode(const XMLNode &A)
2197 {
2198     // shallow copy
2199     d=A.d;
2200     if (d) (d->ref_count)++ ;
2201 }
2202
2203 int XMLNode::nChildNode(XMLCSTR name) const
2204 {
2205     if (!d) return 0;
2206     int i,j=0,n=d->nChild;
2207     XMLNode *pc=d->pChild;
2208     for (i=0; i<n; i++)
2209     {
2210         if (_tcsicmp(pc->d->lpszName, name)==0) j++;
2211         pc++;
2212     }
2213     return j;
2214 }
2215
2216 XMLNode XMLNode::getChildNode(XMLCSTR name, int *j) const
2217 {
2218     if (!d) return emptyXMLNode;
2219     int i=0,n=d->nChild;
2220     if (j) i=*j;
2221     XMLNode *pc=d->pChild+i;
2222     for (; i<n; i++)
2223     {
2224         if (_tcsicmp(pc->d->lpszName, name)==0)
2225         {
2226             if (j) *j=i+1;
2227             return *pc;
2228         }
2229         pc++;
2230     }
2231     return emptyXMLNode;
2232 }
2233
2234 XMLNode XMLNode::getChildNode(XMLCSTR name, int j) const
2235 {
2236     if (!d) return emptyXMLNode;
2237     int i=0;
2238     while (j-->0) getChildNode(name,&i);
2239     return getChildNode(name,&i);
2240 }
2241
2242 int XMLNode::positionOfText     (int i) const { if (i>=d->nText ) i=d->nText-1;  return findPosition(d,i,eNodeText ); }
2243 int XMLNode::positionOfClear    (int i) const { if (i>=d->nClear) i=d->nClear-1; return findPosition(d,i,eNodeClear); }
2244 int XMLNode::positionOfChildNode(int i) const { if (i>=d->nChild) i=d->nChild-1; return findPosition(d,i,eNodeChild); }
2245 int XMLNode::positionOfText (XMLCSTR lpszValue) const { return positionOfText (indexText (lpszValue)); }
2246 int XMLNode::positionOfClear(XMLCSTR lpszValue) const { return positionOfClear(indexClear(lpszValue)); }
2247 int XMLNode::positionOfClear(XMLClear *a) const { if (a) return positionOfClear(a->lpszValue); return positionOfClear(); }
2248 int XMLNode::positionOfChildNode(XMLNode x)  const
2249 {
2250     if ((!d)||(!x.d)) return -1;
2251     XMLNodeData *dd=x.d;
2252     XMLNode *pc=d->pChild;
2253     int i=d->nChild;
2254     while (i--) if (pc[i].d==dd) return findPosition(d,i,eNodeChild);
2255     return -1;
2256 }
2257 int XMLNode::positionOfChildNode(XMLCSTR name, int count) const
2258 {
2259     if (!name) return positionOfChildNode(count);
2260     int j=0;
2261     do { getChildNode(name,&j); if (j<0) return -1; } while (count--);
2262     return findPosition(d,j-1,eNodeChild);
2263 }
2264
2265 XMLNode XMLNode::getChildNodeWithAttribute(XMLCSTR name,XMLCSTR attributeName,XMLCSTR attributeValue, int *k) const
2266 {
2267      int i=0,j;
2268      if (k) i=*k;
2269      XMLNode x;
2270      XMLCSTR t;
2271      do
2272      {
2273          x=getChildNode(name,&i);
2274          if (!x.isEmpty())
2275          {
2276              if (attributeValue)
2277              {
2278                  j=0;
2279                  do
2280                  {
2281                      t=x.getAttribute(attributeName,&j);
2282                      if (t&&(_tcsicmp(attributeValue,t)==0)) { if (k) *k=i+1; return x; }
2283                  } while (t);
2284              } else
2285              {
2286                  if (x.isAttributeSet(attributeName)) { if (k) *k=i+1; return x; }
2287              }
2288          }
2289      } while (!x.isEmpty());
2290      return emptyXMLNode;
2291 }
2292
2293 // Find an attribute on an node.
2294 XMLCSTR XMLNode::getAttribute(XMLCSTR lpszAttrib, int *j) const
2295 {
2296     if (!d) return NULL;
2297     int i=0,n=d->nAttribute;
2298     if (j) i=*j;
2299     XMLAttribute *pAttr=d->pAttribute+i;
2300     for (; i<n; i++)
2301     {
2302         if (_tcsicmp(pAttr->lpszName, lpszAttrib)==0)
2303         {
2304             if (j) *j=i+1;
2305             return pAttr->lpszValue;
2306         }
2307         pAttr++;
2308     }
2309     return NULL;
2310 }
2311
2312 char XMLNode::isAttributeSet(XMLCSTR lpszAttrib) const
2313 {
2314     if (!d) return FALSE;
2315     int i,n=d->nAttribute;
2316     XMLAttribute *pAttr=d->pAttribute;
2317     for (i=0; i<n; i++)
2318     {
2319         if (_tcsicmp(pAttr->lpszName, lpszAttrib)==0)
2320         {
2321             return TRUE;
2322         }
2323         pAttr++;
2324     }
2325     return FALSE;
2326 }
2327
2328 XMLCSTR XMLNode::getAttribute(XMLCSTR name, int j) const
2329 {
2330     if (!d) return NULL;
2331     int i=0;
2332     while (j-->0) getAttribute(name,&i);
2333     return getAttribute(name,&i);
2334 }
2335
2336 XMLNodeContents XMLNode::enumContents(int i) const
2337 {
2338     XMLNodeContents c;
2339     if (!d) { c.type=eNodeNULL; return c; }
2340     if (i<d->nAttribute)
2341     {
2342         c.type=eNodeAttribute;
2343         c.attrib=d->pAttribute[i];
2344         return c;
2345     }
2346     i-=d->nAttribute;
2347     c.type=(XMLElementType)(d->pOrder[i]&3);
2348     i=(d->pOrder[i])>>2;
2349     switch (c.type)
2350     {
2351     case eNodeChild:     c.child = d->pChild[i];      break;
2352     case eNodeText:      c.text  = d->pText[i];       break;
2353     case eNodeClear:     c.clear = d->pClear[i];      break;
2354     default: break;
2355     }
2356     return c;
2357 }
2358
2359 XMLCSTR XMLNode::getName() const { if (!d) return NULL; return d->lpszName;   }
2360 int XMLNode::nText()       const { if (!d) return 0;    return d->nText;      }
2361 int XMLNode::nChildNode()  const { if (!d) return 0;    return d->nChild;     }
2362 int XMLNode::nAttribute()  const { if (!d) return 0;    return d->nAttribute; }
2363 int XMLNode::nClear()      const { if (!d) return 0;    return d->nClear;     }
2364 int XMLNode::nElement()    const { if (!d) return 0;    return d->nAttribute+d->nChild+d->nText+d->nClear; }
2365 XMLClear     XMLNode::getClear         (int i) const { if ((!d)||(i>=d->nClear    )) return emptyXMLClear;     return d->pClear[i];     }
2366 XMLAttribute XMLNode::getAttribute     (int i) const { if ((!d)||(i>=d->nAttribute)) return emptyXMLAttribute; return d->pAttribute[i]; }
2367 XMLCSTR      XMLNode::getAttributeName (int i) const { if ((!d)||(i>=d->nAttribute)) return NULL;              return d->pAttribute[i].lpszName;  }
2368 XMLCSTR      XMLNode::getAttributeValue(int i) const { if ((!d)||(i>=d->nAttribute)) return NULL;              return d->pAttribute[i].lpszValue; }
2369 XMLCSTR      XMLNode::getText          (int i) const { if ((!d)||(i>=d->nText     )) return NULL;              return d->pText[i];      }
2370 XMLNode      XMLNode::getChildNode     (int i) const { if ((!d)||(i>=d->nChild    )) return emptyXMLNode;      return d->pChild[i];     }
2371 XMLNode      XMLNode::getParentNode    (     ) const { if ((!d)||(!d->pParent     )) return emptyXMLNode;      return XMLNode(d->pParent); }
2372 char         XMLNode::isDeclaration    (     ) const { if (!d) return 0;             return d->isDeclaration; }
2373 char         XMLNode::isEmpty          (     ) const { return (d==NULL); }
2374
2375 XMLNode       XMLNode::addChild(XMLCSTR lpszName, char isDeclaration, int pos)
2376               { return addChild_priv(0,stringDup(lpszName),isDeclaration,pos); }
2377 XMLNode       XMLNode::addChild_WOSD(XMLCSTR lpszName, char isDeclaration, int pos)
2378               { return addChild_priv(0,lpszName,isDeclaration,pos); }
2379 XMLAttribute *XMLNode::addAttribute(XMLCSTR lpszName, XMLCSTR lpszValue)
2380               { return addAttribute_priv(0,stringDup(lpszName),stringDup(lpszValue)); }
2381 XMLAttribute *XMLNode::addAttribute_WOSD(XMLCSTR lpszName, XMLCSTR lpszValuev)
2382               { return addAttribute_priv(0,lpszName,lpszValuev); }
2383 XMLCSTR       XMLNode::addText(XMLCSTR lpszValue, int pos)
2384               { return addText_priv(0,stringDup(lpszValue),pos); }
2385 XMLCSTR       XMLNode::addText_WOSD(XMLCSTR lpszValue, int pos)
2386               { return addText_priv(0,lpszValue,pos); }
2387 XMLClear     *XMLNode::addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
2388               { return addClear_priv(0,stringDup(lpszValue),lpszOpen,lpszClose,pos); }
2389 XMLClear     *XMLNode::addClear_WOSD(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
2390               { return addClear_priv(0,lpszValue,lpszOpen,lpszClose,pos); }
2391 XMLCSTR       XMLNode::updateName(XMLCSTR lpszName)
2392               { return updateName_WOSD(stringDup(lpszName)); }
2393 XMLAttribute *XMLNode::updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2394               { return updateAttribute_WOSD(stringDup(newAttribute->lpszValue),stringDup(newAttribute->lpszName),oldAttribute->lpszName); }
2395 XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,int i)
2396               { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),i); }
2397 XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName)
2398               { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),lpszOldName); }
2399 XMLCSTR       XMLNode::updateText(XMLCSTR lpszNewValue, int i)
2400               { return updateText_WOSD(stringDup(lpszNewValue),i); }
2401 XMLCSTR       XMLNode::updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2402               { return updateText_WOSD(stringDup(lpszNewValue),lpszOldValue); }
2403 XMLClear     *XMLNode::updateClear(XMLCSTR lpszNewContent, int i)
2404               { return updateClear_WOSD(stringDup(lpszNewContent),i); }
2405 XMLClear     *XMLNode::updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2406               { return updateClear_WOSD(stringDup(lpszNewValue),lpszOldValue); }
2407 XMLClear     *XMLNode::updateClear(XMLClear *newP,XMLClear *oldP)
2408               { return updateClear_WOSD(stringDup(newP->lpszValue),oldP->lpszValue); }
2409
2410 void XMLNode::setGlobalOptions(char _guessUnicodeChars, char _strictUTF8Parsing, char _dropWhiteSpace)
2411 {
2412     guessUnicodeChars=_guessUnicodeChars; dropWhiteSpace=_dropWhiteSpace; strictUTF8Parsing=_strictUTF8Parsing;
2413 #ifndef _XMLUNICODE
2414     if (_strictUTF8Parsing) XML_ByteTable=XML_utf8ByteTable; else XML_ByteTable=XML_asciiByteTable;
2415 #endif
2416 }
2417
2418 char XMLNode::guessUTF8ParsingParameterValue(void *buf,int l, char useXMLEncodingAttribute)
2419 {
2420 #ifdef _XMLUNICODE
2421     return 0;
2422 #else
2423     if (l<25) return 0;
2424     if (myIsTextUnicode(buf,l)) return 0;
2425     unsigned char *b=(unsigned char*)buf;
2426     if ((b[0]==0xef)&&(b[1]==0xbb)&&(b[2]==0xbf)) return 1;
2427
2428     // Match utf-8 model ?
2429     int i=0;
2430     while (i<l)
2431         switch (XML_utf8ByteTable[b[i]])
2432         {
2433         case 4: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) return 0; // 10bbbbbb ?
2434         case 3: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) return 0; // 10bbbbbb ?
2435         case 2: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) return 0; // 10bbbbbb ?
2436         case 1: i++; break;
2437         case 0: i=l;
2438         }
2439     if (!useXMLEncodingAttribute) return 1;
2440     // if encoding is specified and different from utf-8 than it's non-utf8
2441     // otherwise it's utf-8
2442     char bb[201];
2443     l=mmin(l,200);
2444     memcpy(bb,buf,l); // copy buf into bb to be able to do "bb[l]=0"
2445     bb[l]=0;
2446     b=(unsigned char*)strstr(bb,"encoding");
2447     if (!b) return 1;
2448     b+=8; while XML_isSPACECHAR(*b) b++; if (*b!='=') return 1;
2449     b++;  while XML_isSPACECHAR(*b) b++; if ((*b!='\'')&&(*b!='"')) return 1;
2450     b++;  while XML_isSPACECHAR(*b) b++; if ((_strnicmp((char*)b,"utf-8",5)==0)||
2451                                              (_strnicmp((char*)b,"utf8",4)==0)) return 1;
2452     return 0;
2453 #endif
2454 }
2455 #undef XML_isSPACECHAR
2456
2457 //////////////////////////////////////////////////////////
2458 //      Here starts the base64 conversion functions.    //
2459 //////////////////////////////////////////////////////////
2460
2461 static const char base64Fillchar = _T('='); // used to mark partial words at the end
2462
2463 // this lookup table defines the base64 encoding
2464 XMLCSTR base64EncodeTable=_T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2465
2466 // Decode Table gives the index of any valid base64 character in the Base64 table]
2467 // 96: '='  -   97: space char   -   98: illegal char   -   99: end of string
2468 const unsigned char base64DecodeTable[] = {
2469     99,98,98,98,98,98,98,98,98,97,  97,98,98,97,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  //00 -29
2470     98,98,97,98,98,98,98,98,98,98,  98,98,98,62,98,98,98,63,52,53,  54,55,56,57,58,59,60,61,98,98,  //30 -59
2471     98,96,98,98,98, 0, 1, 2, 3, 4,   5, 6, 7, 8, 9,10,11,12,13,14,  15,16,17,18,19,20,21,22,23,24,  //60 -89
2472     25,98,98,98,98,98,98,26,27,28,  29,30,31,32,33,34,35,36,37,38,  39,40,41,42,43,44,45,46,47,48,  //90 -119
2473     49,50,51,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  //120 -149
2474     98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  //150 -179
2475     98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  //180 -209
2476     98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98,98,98,98,98,  //210 -239
2477     98,98,98,98,98,98,98,98,98,98,  98,98,98,98,98,98                                               //240 -255
2478 };
2479
2480 XMLParserBase64Tool::~XMLParserBase64Tool(){ freeBuffer(); }
2481
2482 void XMLParserBase64Tool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
2483
2484 int XMLParserBase64Tool::encodeLength(int inlen, char formatted)
2485 {
2486     unsigned int i=((inlen-1)/3*4+4+1);
2487     if (formatted) i+=inlen/54;
2488     return i;
2489 }
2490
2491 XMLSTR XMLParserBase64Tool::encode(unsigned char *inbuf, unsigned int inlen, char formatted)
2492 {
2493     int i=encodeLength(inlen,formatted),k=17,eLen=inlen/3,j;
2494     alloc(i*sizeof(XMLCHAR));
2495     XMLSTR curr=(XMLSTR)buf;
2496     for(i=0;i<eLen;i++)
2497     {
2498         // Copy next three bytes into lower 24 bits of int, paying attention to sign.
2499         j=(inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
2500         // Encode the int into four chars
2501         *(curr++)=base64EncodeTable[ j>>18      ];
2502         *(curr++)=base64EncodeTable[(j>>12)&0x3f];
2503         *(curr++)=base64EncodeTable[(j>> 6)&0x3f];
2504         *(curr++)=base64EncodeTable[(j    )&0x3f];
2505         if (formatted) { if (!k) { *(curr++)=_T('\n'); k=18; } k--; }
2506     }
2507     eLen=inlen-eLen*3; // 0 - 2.
2508     if (eLen==1)
2509     {
2510         *(curr++)=base64EncodeTable[ inbuf[0]>>2      ];
2511         *(curr++)=base64EncodeTable[(inbuf[0]<<4)&0x3F];
2512         *(curr++)=base64Fillchar;
2513         *(curr++)=base64Fillchar;
2514     } else if (eLen==2)
2515     {
2516         j=(inbuf[0]<<8)|inbuf[1];
2517         *(curr++)=base64EncodeTable[ j>>10      ];
2518         *(curr++)=base64EncodeTable[(j>> 4)&0x3f];
2519         *(curr++)=base64EncodeTable[(j<< 2)&0x3f];
2520         *(curr++)=base64Fillchar;
2521     }
2522     *(curr++)=0;
2523     return (XMLSTR)buf;
2524 }
2525
2526 unsigned int XMLParserBase64Tool::decodeSize(XMLCSTR data,XMLError *xe)
2527 {
2528      if (xe) *xe=eXMLErrorNone;
2529     int size=0;
2530     unsigned char c;
2531     //skip any extra characters (e.g. newlines or spaces)
2532     while (*data)
2533     {
2534 #ifdef _XMLUNICODE
2535         if (*data>255) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2536 #endif
2537         c=base64DecodeTable[(unsigned char)(*data)];
2538         if (c<97) size++;
2539         else if (c==98) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2540         data++;
2541     }
2542     if (xe&&(size%4!=0)) *xe=eXMLErrorBase64DataSizeIsNotMultipleOf4;
2543     if (size==0) return 0;
2544     do { data--; size--; } while(*data==base64Fillchar); size++;
2545     return (unsigned int)((size*3)/4);
2546 }
2547
2548 unsigned char XMLParserBase64Tool::decode(XMLCSTR data, unsigned char *buf, int len, XMLError *xe)
2549 {
2550     if (xe) *xe=eXMLErrorNone;
2551     int i=0,p=0;
2552     unsigned char d,c;
2553     for(;;)
2554     {
2555
2556 #ifdef _XMLUNICODE
2557 #define BASE64DECODE_READ_NEXT_CHAR(c)                                              \
2558         do {                                                                        \
2559             if (data[i]>255){ c=98; break; }                                        \
2560             c=base64DecodeTable[(unsigned char)data[i++]];                       \
2561         }while (c==97);                                                             \
2562         if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2563 #else
2564 #define BASE64DECODE_READ_NEXT_CHAR(c)                                           \
2565         do { c=base64DecodeTable[(unsigned char)data[i++]]; }while (c==97);   \
2566         if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2567 #endif
2568
2569         BASE64DECODE_READ_NEXT_CHAR(c)
2570         if (c==99) { return 2; }
2571         if (c==96)
2572         {
2573             if (p==(int)len) return 2;
2574             if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;
2575             return 1;
2576         }
2577
2578         BASE64DECODE_READ_NEXT_CHAR(d)
2579         if ((d==99)||(d==96)) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;  return 1; }
2580         if (p==(int)len) {      if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; return 0; }
2581         buf[p++]=(c<<2)|((d>>4)&0x3);
2582
2583         BASE64DECODE_READ_NEXT_CHAR(c)
2584         if (c==99) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;  return 1; }
2585         if (p==(int)len)
2586         {
2587             if (c==96) return 2;
2588             if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2589             return 0;
2590         }
2591         if (c==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;  return 1; }
2592         buf[p++]=((d<<4)&0xf0)|((c>>2)&0xf);
2593
2594         BASE64DECODE_READ_NEXT_CHAR(d)
2595         if (d==99 ) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;  return 1; }
2596         if (p==(int)len)
2597         {
2598             if (d==96) return 2;
2599             if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2600             return 0;
2601         }
2602         if (d==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;  return 1; }
2603         buf[p++]=((c<<6)&0xc0)|d;
2604     }
2605 }
2606 #undef BASE64DECODE_READ_NEXT_CHAR
2607
2608 void XMLParserBase64Tool::alloc(int newsize)
2609 {
2610     if ((!buf)&&(newsize)) { buf=malloc(newsize); buflen=newsize; return; }
2611     if (newsize>buflen) { buf=realloc(buf,newsize); buflen=newsize; }
2612 }
2613
2614 unsigned char *XMLParserBase64Tool::decode(XMLCSTR data, int *outlen, XMLError *xe)
2615 {
2616     if (xe) *xe=eXMLErrorNone;
2617     unsigned int len=decodeSize(data,xe);
2618     if (outlen) *outlen=len;
2619     if (!len) return NULL;
2620     alloc(len+1);
2621     if(!decode(data,(unsigned char*)buf,len,xe)){ return NULL; }
2622     return (unsigned char*)buf;
2623 }
2624