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