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