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>
8
* @author Frank Vanden Berghen
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>
20
* If you add "#define APPROXIMATE_PARSING" on the first line of this file
21
* the parser will see the following XML-stream:
25
* as equivalent to the following XML-stream:
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):
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.
45
* Copyright (c) 2002, Business-Insight
46
* <a href="http://www.Business-Insight.com">Business-Insight</a>
47
* All rights reserved.
48
* See the file "AFPL-license.txt" about the licensing terms
50
****************************************************************************
52
#ifndef _CRT_SECURE_NO_DEPRECATE
53
#define _CRT_SECURE_NO_DEPRECATE
55
#include "musicbrainz4/xmlParser.h"
58
//#define _CRTDBG_MAP_ALLOC
61
#define WIN32_LEAN_AND_MEAN
62
#include <Windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files
63
// to have "MessageBoxA" to display error messages for openFilHelper
72
XMLCSTR XMLNode::getVersion() { return _CXML("v2.43"); }
73
void freeXMLString(XMLSTR t){if(t)free(t);}
75
static XMLNode::XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8;
76
static char guessWideCharChars=1, dropWhiteSpace=1, removeCommentsInMiddleOfText=1;
78
inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; }
80
// You can modify the initialization of the variable "XMLClearTags" below
81
// to change the clearTags that are currently recognized by the library.
82
// The number on the second columns is the length of the string inside the
84
// The "<!DOCTYPE" declaration must be the second in the list.
85
// The "<!--" declaration must be the third in the list.
86
// All ClearTag Strings must start with the '<' character.
87
typedef struct { XMLCSTR lpszOpen; int openTagLen; XMLCSTR lpszClose;} ALLXMLClearTag;
88
static ALLXMLClearTag XMLClearTags[] =
90
{ _CXML("<![CDATA["),9, _CXML("]]>") },
91
{ _CXML("<!DOCTYPE"),9, _CXML(">") },
92
{ _CXML("<!--") ,4, _CXML("-->") },
93
{ _CXML("<PRE>") ,5, _CXML("</PRE>") },
94
// { _CXML("<Script>") ,8, _CXML("</Script>")},
98
// You can modify the initialization of the variable "XMLEntities" below
99
// to change the character entities that are currently recognized by the library.
100
// The number on the second columns is the length of the string inside the
101
// first column. Additionally, the syntaxes " " and " " are recognized.
102
typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
103
static XMLCharacterEntity XMLEntities[] =
105
{ _CXML("&" ), 5, _CXML('&' )},
106
{ _CXML("<" ), 4, _CXML('<' )},
107
{ _CXML(">" ), 4, _CXML('>' )},
108
{ _CXML("""), 6, _CXML('\"')},
109
{ _CXML("'"), 6, _CXML('\'')},
113
// When rendering the XMLNode to a string (using the "createXMLString" function),
114
// you can ask for a beautiful formatting. This formatting is using the
115
// following indentation character:
116
#define INDENTCHAR _CXML('\t')
118
// The following function parses the XML errors into a user friendly string.
119
// You can edit this to change the output language of the library to something else.
120
XMLCSTR XMLNode::getError(XMLError xerror)
124
case eXMLErrorNone: return _CXML("No error");
125
case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag");
126
case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found");
127
case eXMLErrorEmpty: return _CXML("Error: No XML data");
128
case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name");
129
case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name");
130
case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag");
131
case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end");
132
case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found");
133
case eXMLErrorNoElements: return _CXML("Error: No elements found");
134
case eXMLErrorFileNotFound: return _CXML("Error: File not found");
135
case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found");
136
case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity");
137
case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode.");
138
case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars");
139
case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing");
140
case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file");
142
case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4");
143
case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated");
144
case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character");
145
case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small");
147
return _CXML("Unknown");
150
/////////////////////////////////////////////////////////////////////////
151
// Here start the abstraction layer to be OS-independent //
152
/////////////////////////////////////////////////////////////////////////
154
// Here is an abstraction layer to access some common string manipulation functions.
155
// The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
156
// Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
157
// If you plan to "port" the library to a new system/compiler, all you have to do is
158
// to edit the following lines.
159
#ifdef XML_NO_WIDE_CHAR
160
char myIsTextWideChar(const void *b, int len) { return FALSE; }
162
#if defined (UNDER_CE) || !defined(_XMLWINDOWS)
163
char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
166
// for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
167
if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE;
169
const wchar_t *s=(const wchar_t*)b;
172
if (len<(int)sizeof(wchar_t)) return FALSE;
175
if (len&1) return FALSE;
177
/* only checks the first 256 characters */
178
len=mmin(256,len/sizeof(wchar_t));
180
// Check for the special byte order:
181
if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
182
if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE
184
// checks for ASCII characters in the UNICODE stream
186
for (i=0; i<len; i++) if (s[i]<=(unsigned short)255) stats++;
187
if (stats>len/2) return TRUE;
189
// Check for UNICODE NULL chars
190
for (i=0; i<len; i++) if (!s[i]) return TRUE;
195
char myIsTextWideChar(const void *b,int l) { return (char)IsTextUnicode((CONST LPVOID)b,l,NULL); }
200
// for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
202
wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
205
if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,NULL,0);
206
else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,NULL,0);
207
if (i<0) return NULL;
208
wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
209
if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,d,i);
210
else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,d,i);
214
static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return _wfopen(filename,mode); }
215
static inline int xstrlen(XMLCSTR c) { return (int)wcslen(c); }
216
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _wcsnicmp(c1,c2,l);}
217
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
218
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _wcsicmp(c1,c2); }
219
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
220
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
222
char *myWideCharToMultiByte(const wchar_t *s)
224
UINT codePage=CP_ACP; if (characterEncoding==XMLNode::char_encoding_UTF8) codePage=CP_UTF8;
225
int i=(int)WideCharToMultiByte(codePage, // code page
226
0, // performance and mapping flags
227
s, // wide-character string
228
-1, // number of chars in string
229
NULL, // buffer for new string
231
NULL, // default for unmappable chars
232
NULL // set when default char used
234
if (i<0) return NULL;
235
char *d=(char*)malloc(i+1);
236
WideCharToMultiByte(codePage, // code page
237
0, // performance and mapping flags
238
s, // wide-character string
239
-1, // number of chars in string
240
d, // buffer for new string
242
NULL, // default for unmappable chars
243
NULL // set when default char used
248
static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
249
static inline int xstrlen(XMLCSTR c) { return (int)strlen(c); }
251
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strnicmp(c1,c2,l);}
252
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return stricmp(c1,c2); }
254
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _strnicmp(c1,c2,l);}
255
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _stricmp(c1,c2); }
257
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
258
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
259
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
263
#ifdef XML_NO_WIDE_CHAR
264
char *myWideCharToMultiByte(const wchar_t *s) { return NULL; }
266
char *myWideCharToMultiByte(const wchar_t *s)
269
int i=(int)wcsrtombs(NULL,&ss,0,NULL);
270
if (i<0) return NULL;
271
char *d=(char *)malloc(i+1);
272
wcsrtombs(d,&s,i,NULL);
278
wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
281
int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
282
if (i<0) return NULL;
283
wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(wchar_t));
284
mbsrtowcs(d,&s,i,NULL);
288
int xstrlen(XMLCSTR c) { return wcslen(c); }
292
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);}
293
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1,c2,l);}
294
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); }
296
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
299
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);}
300
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); }
303
// for gcc/non-linux (MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw)
304
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2)
309
left=towlower(*c1++); right=towlower(*c2++);
310
} while (left&&(left==right));
311
return (int)left-(int)right;
313
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l)
318
left=towlower(*c1++); right=towlower(*c2++);
319
if ((!left)||(left!=right)) return (int)left-(int)right;
325
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
326
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
327
static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode)
329
char *filenameAscii=myWideCharToMultiByte(filename);
331
if (mode[0]==_CXML('r')) f=fopen(filenameAscii,"rb");
332
else f=fopen(filenameAscii,"wb");
337
static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
338
static inline int xstrlen(XMLCSTR c) { return strlen(c); }
339
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);}
340
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
341
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); }
342
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
343
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
345
static inline int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);}
349
///////////////////////////////////////////////////////////////////////////////
350
// the "xmltoc,xmltob,xmltoi,xmltol,xmltof,xmltoa" functions //
351
///////////////////////////////////////////////////////////////////////////////
352
// These 6 functions are not used inside the XMLparser.
353
// There are only here as "convenience" functions for the user.
354
// If you don't need them, you can delete them without any trouble.
357
// for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
358
char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)_wtoi(t); return v; }
359
int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return _wtoi(t); return v; }
360
long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return _wtol(t); return v; }
361
double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
366
char xmltob(XMLCSTR t,char v){ if (t) return (char)wstol(t,NULL,10); return v; }
367
int xmltoi(XMLCSTR t,int v){ if (t) return (int)wstol(t,NULL,10); return v; }
368
long xmltol(XMLCSTR t,long v){ if (t) return wstol(t,NULL,10); return v; }
371
char xmltob(XMLCSTR t,char v){ if (t) return (char)wcstol(t,NULL,10); return v; }
372
int xmltoi(XMLCSTR t,int v){ if (t) return (int)wcstol(t,NULL,10); return v; }
373
long xmltol(XMLCSTR t,long v){ if (t) return wcstol(t,NULL,10); return v; }
375
double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
378
char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)atoi(t); return v; }
379
int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return atoi(t); return v; }
380
long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return atol(t); return v; }
381
double xmltof(XMLCSTR t,double v){ if (t&&(*t)) return atof(t); return v; }
383
XMLCSTR xmltoa(XMLCSTR t, XMLCSTR v){ if (t) return t; return v; }
384
XMLCHAR xmltoc(XMLCSTR t,const XMLCHAR v){ if (t&&(*t)) return *t; return v; }
386
/////////////////////////////////////////////////////////////////////////
387
// the "openFileHelper" function //
388
/////////////////////////////////////////////////////////////////////////
390
// Since each application has its own way to report and deal with errors, you should modify & rewrite
391
// the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs.
392
XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
394
// guess the value of the global parameter "characterEncoding"
395
// (the guess is based on the first 200 bytes of the file).
396
FILE *f=xfopen(filename,_CXML("rb"));
400
int l=(int)fread(bb,1,200,f);
401
setGlobalOptions(guessCharEncoding(bb,l),guessWideCharChars,dropWhiteSpace,removeCommentsInMiddleOfText);
407
XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
409
// display error message (if any)
410
if (pResults.error != eXMLErrorNone)
413
char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_CXML("");
414
if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; }
417
"XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
419
"XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
421
,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3);
424
#if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
425
MessageBoxA(NULL,message,"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST);
427
printf("%s",message);
433
/////////////////////////////////////////////////////////////////////////
434
// Here start the core implementation of the XMLParser library //
435
/////////////////////////////////////////////////////////////////////////
437
// You should normally not change anything below this point.
440
// If "characterEncoding=ascii" then we assume that all characters have the same length of 1 byte.
441
// If "characterEncoding=UTF8" then the characters have different lengths (from 1 byte to 4 bytes).
442
// If "characterEncoding=ShiftJIS" then the characters have different lengths (from 1 byte to 2 bytes).
443
// This table is used as lookup-table to know the length of a character (in byte) based on the
444
// content of the first byte of the character.
445
// (note: if you modify this, you must always have XML_utf8ByteTable[0]=0 ).
446
static const char XML_utf8ByteTable[256] =
448
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
449
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
450
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
451
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
452
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
453
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
454
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
455
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
456
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 End of ASCII range
457
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 0x80 to 0xc1 invalid
458
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
459
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
460
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
461
1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 0xc2 to 0xdf 2 byte
462
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
463
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,// 0xe0 0xe0 to 0xef 3 byte
464
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
466
static const char XML_legacyByteTable[256] =
468
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,
469
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
470
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
471
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
472
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
473
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
475
static const char XML_sjisByteTable[256] =
477
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
478
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
479
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
480
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
481
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
482
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
483
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
484
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
485
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
486
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0x9F 2 bytes
487
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
488
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
489
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
490
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xc0
491
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xd0
492
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 0xe0 to 0xef 2 bytes
493
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 // 0xf0
495
static const char XML_gb2312ByteTable[256] =
497
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
498
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
499
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
500
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
501
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
502
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
503
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
504
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
505
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
506
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80
507
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
508
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0 0xa1 to 0xf7 2 bytes
509
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
510
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
511
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
512
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
513
2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1 // 0xf0
515
static const char XML_gbk_big5_ByteTable[256] =
517
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
518
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
519
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
520
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
521
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
522
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
523
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
524
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
525
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
526
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0xfe 2 bytes
527
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
528
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0
529
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
530
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
531
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
532
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
533
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 // 0xf0
535
static const char *XML_ByteTable=(const char *)XML_utf8ByteTable; // the default is "characterEncoding=XMLNode::encoding_UTF8"
539
XMLNode XMLNode::emptyXMLNode;
540
XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL};
541
XMLAttribute XMLNode::emptyXMLAttribute={ NULL, NULL};
543
// Enumeration used to decipher what type a token is
544
typedef enum XMLTokenTypeTag
548
eTokenTagStart, /* "<" */
549
eTokenTagEnd, /* "</" */
550
eTokenCloseTag, /* ">" */
551
eTokenEquals, /* "=" */
552
eTokenDeclaration, /* "<?" */
553
eTokenShortHandClose, /* "/>" */
558
// Main structure used for parsing XML
563
int nIndex,nIndexMissigEndTag;
567
XMLCSTR lpNewElement;
574
ALLXMLClearTag *pClr;
578
// Enumeration used when parsing attributes
586
// Enumeration used when parsing elements to dictate whether we are currently
588
typedef enum XMLStatus
594
XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const
596
if (!d) return eXMLErrorNone;
597
FILE *f=xfopen(filename,_CXML("wb"));
598
if (!f) return eXMLErrorCannotOpenWriteFile;
600
unsigned char h[2]={ 0xFF, 0xFE };
601
if (!fwrite(h,2,1,f))
604
return eXMLErrorCannotWriteFile;
606
if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
608
if (!fwrite(L"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n",sizeof(wchar_t)*40,1,f))
611
return eXMLErrorCannotWriteFile;
615
if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
617
if (characterEncoding==char_encoding_UTF8)
619
// header so that windows recognize the file as UTF-8:
620
unsigned char h[3]={0xEF,0xBB,0xBF};
621
if (!fwrite(h,3,1,f))
624
return eXMLErrorCannotWriteFile;
627
} else if (characterEncoding==char_encoding_ShiftJIS) encoding="SHIFT-JIS";
629
if (!encoding) encoding="ISO-8859-1";
630
if (fprintf(f,"<?xml version=\"1.0\" encoding=\"%s\"?>\n",encoding)<0)
633
return eXMLErrorCannotWriteFile;
637
if (characterEncoding==char_encoding_UTF8)
639
unsigned char h[3]={0xEF,0xBB,0xBF};
640
if (!fwrite(h,3,1,f))
643
return eXMLErrorCannotWriteFile;
649
XMLSTR t=createXMLString(nFormat,&i);
650
if (!fwrite(t,sizeof(XMLCHAR)*i,1,f))
654
return eXMLErrorCannotWriteFile;
659
return eXMLErrorCannotWriteFile;
662
return eXMLErrorNone;
665
// Duplicate a given string.
666
XMLSTR stringDup(XMLCSTR lpszData, int cbData)
668
if (lpszData==NULL) return NULL;
671
if (cbData==-1) cbData=(int)xstrlen(lpszData);
672
lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
675
memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
676
lpszNew[cbData] = (XMLCHAR)NULL;
681
XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest,XMLCSTR source)
685
XMLCharacterEntity *entity;
691
if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; }
695
*(dest++)=*(source++);
697
switch(XML_ByteTable[(unsigned char)ch])
699
case 4: *(dest++)=*(source++);
700
case 3: *(dest++)=*(source++);
701
case 2: *(dest++)=*(source++);
702
case 1: *(dest++)=*(source++);
712
// private (used while rendering):
713
int ToXMLStringTool::lengthXMLString(XMLCSTR source)
716
XMLCharacterEntity *entity;
723
if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; }
729
ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
737
ToXMLStringTool::~ToXMLStringTool(){ freeBuffer(); }
738
void ToXMLStringTool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
739
XMLSTR ToXMLStringTool::toXML(XMLCSTR source)
743
if (buflen<1) { buflen=1; buf=(XMLSTR)malloc(sizeof(XMLCHAR)); }
747
int l=lengthXMLString(source)+1;
748
if (l>buflen) { freeBuffer(); buflen=l; buf=(XMLSTR)malloc(l*sizeof(XMLCHAR)); }
749
return toXMLUnSafe(buf,source);
753
XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
755
// This function is the opposite of the function "toXMLString". It decodes the escape
756
// sequences &, ", ', <, > and replace them by the characters
757
// &,",',<,>. This function is used internally by the XML Parser. All the calls to
758
// the XML library will always gives you back "decoded" strings.
760
// in: string (s) and length (lo) of string
761
// out: new allocated string converted from xml
767
XMLCharacterEntity *entity;
772
if ((lo>2)&&(s[1]==_CXML('#')))
775
if ((*s==_CXML('X'))||(*s==_CXML('x'))) { s++; lo--; }
776
while ((*s)&&(*s!=_CXML(';'))&&((lo--)>0)) s++;
779
pXML->error=eXMLErrorUnknownCharacterEntity;
788
if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; }
793
pXML->error=eXMLErrorUnknownCharacterEntity;
802
j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
808
d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
814
if (ss[1]==_CXML('#'))
817
if ((*ss==_CXML('X'))||(*ss==_CXML('x')))
820
while (*ss!=_CXML(';'))
822
if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j<<4)+*ss-_CXML('0');
823
else if ((*ss>=_CXML('A'))&&(*ss<=_CXML('F'))) j=(j<<4)+*ss-_CXML('A')+10;
824
else if ((*ss>=_CXML('a'))&&(*ss<=_CXML('f'))) j=(j<<4)+*ss-_CXML('a')+10;
825
else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
830
while (*ss!=_CXML(';'))
832
if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j*10)+*ss-_CXML('0');
833
else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
838
if (j>255) { free((void*)s); pXML->error=eXMLErrorCharacterCodeAbove255;return NULL;}
840
(*d++)=(XMLCHAR)j; ss++;
846
if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; }
855
switch(XML_ByteTable[(unsigned char)*ss])
857
case 4: *(d++)=*(ss++); ll--;
858
case 3: *(d++)=*(ss++); ll--;
859
case 2: *(d++)=*(ss++); ll--;
860
case 1: *(d++)=*(ss++);
869
#define XML_isSPACECHAR(ch) ((ch==_CXML('\n'))||(ch==_CXML(' '))||(ch== _CXML('\t'))||(ch==_CXML('\r')))
872
char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
873
// !!!! WARNING strange convention&:
874
// return 0 if equals
875
// return 1 if different
877
if (!cclose) return 1;
878
int l=(int)xstrlen(cclose);
879
if (xstrnicmp(cclose, copen, l)!=0) return 1;
880
const XMLCHAR c=copen[l];
881
if (XML_isSPACECHAR(c)||
885
(c==_CXML('=' ))) return 0;
889
// Obtain the next character from the string.
890
static inline XMLCHAR getNextChar(XML *pXML)
892
XMLCHAR ch = pXML->lpXML[pXML->nIndex];
894
if (ch!=0) pXML->nIndex++;
896
pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
901
// Find the next token in a string.
902
// pcbToken contains the number of characters that have been read.
903
static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
908
int indexStart,nFoundMatch,nIsText=FALSE;
909
result.pClr=NULL; // prevent warning
911
// Find next non-white space character
912
do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch);
916
// Cache the current string pointer
917
result.pStr = &pXML->lpXML[indexStart];
919
// check for standard tokens
926
*pType = eTokenQuotedText;
932
// Search through the string to find a matching quote
933
while((ch = getNextChar(pXML)))
935
if (ch==chTemp) { nFoundMatch = TRUE; break; }
936
if (ch==_CXML('<')) break;
939
// If we failed to find a matching quote
940
if (nFoundMatch == FALSE)
942
pXML->nIndex=indexStart+1;
948
// if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
952
// Equals (used with attribute values)
954
*pType = eTokenEquals;
959
*pType = eTokenCloseTag;
962
// Check for tag start and tag end
966
// First check whether the token is in the clear tag list (meaning it
967
// does not need formatting).
968
ALLXMLClearTag *ctag=XMLClearTags;
971
if (!xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen))
974
pXML->nIndex+=ctag->openTagLen-1;
979
} while(ctag->lpszOpen);
981
// Peek at the next character to see if we have an end tag '</',
982
// or an xml declaration '<?'
983
chTemp = pXML->lpXML[pXML->nIndex];
985
// If we have a tag end...
986
if (chTemp == _CXML('/'))
988
// Set the type and ensure we point at the next character
990
*pType = eTokenTagEnd;
993
// If we have an XML declaration tag
994
else if (chTemp == _CXML('?'))
997
// Set the type and ensure we point at the next character
999
*pType = eTokenDeclaration;
1002
// Otherwise we must have a start tag
1005
*pType = eTokenTagStart;
1010
// Check to see if we have a short hand type end tag ('/>').
1013
// Peek at the next character to see if we have a short end tag '/>'
1014
chTemp = pXML->lpXML[pXML->nIndex];
1016
// If we have a short hand end tag...
1017
if (chTemp == _CXML('>'))
1019
// Set the type and ensure we point at the next character
1021
*pType = eTokenShortHandClose;
1025
// If we haven't found a short hand closing tag then drop into the
1033
// If this is a TEXT node
1036
// Indicate we are dealing with text
1037
*pType = eTokenText;
1038
while((ch = getNextChar(pXML)))
1040
if XML_isSPACECHAR(ch)
1042
indexStart++; break;
1044
} else if (ch==_CXML('/'))
1046
// If we find a slash then this maybe text or a short hand end tag
1047
// Peek at the next character to see it we have short hand end tag
1048
ch=pXML->lpXML[pXML->nIndex];
1049
// If we found a short hand end tag then we need to exit the loop
1050
if (ch==_CXML('>')) { pXML->nIndex--; break; }
1052
} else if ((ch==_CXML('<'))||(ch==_CXML('>'))||(ch==_CXML('=')))
1054
pXML->nIndex--; break;
1058
*pcbToken = pXML->nIndex-indexStart;
1061
// If we failed to obtain a valid character
1063
*pType = eTokenError;
1070
XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName)
1072
if (!d) { free(lpszName); return NULL; }
1073
if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName);
1074
d->lpszName=lpszName;
1079
XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
1080
XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration)
1082
d=(XMLNodeData*)malloc(sizeof(XMLNodeData));
1091
d->isDeclaration = isDeclaration;
1093
d->pParent = pParent;
1097
d->pAttribute= NULL;
1100
updateName_WOSD(lpszName);
1103
XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); }
1104
XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); }
1106
#define MEMORYINCREASE 50
1108
static inline void myFree(void *p) { if (p) free(p); }
1109
static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
1111
if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
1112
if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
1115
// printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
1121
XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype)
1123
if (index<0) return -1;
1124
int i=0,j=(int)((index<<2)+xxtype),*o=d->pOrder; while (o[i]!=j) i++; return i;
1128
// update "order" information when deleting a content of a XMLNode
1129
int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
1131
int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
1132
memmove(o+i, o+i+1, (n-i)*sizeof(int));
1134
if ((o[i]&3)==(int)t) o[i]-=4;
1135
// We should normally do:
1136
// d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
1137
// but we skip reallocation because it's too time consuming.
1138
// Anyway, at the end, it will be free'd completely at once.
1142
void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype)
1144
// in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
1145
// out: *_pos is the index inside p
1146
p=myRealloc(p,(nc+1),memoryIncrease,size);
1147
int n=d->nChild+d->nText+d->nClear;
1148
d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int));
1149
int pos=*_pos,*o=d->pOrder;
1151
if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1154
memmove(o+i+1, o+i, (n-i)*sizeof(int));
1156
while ((pos<n)&&((o[pos]&3)!=(int)xtype)) pos++;
1157
if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1160
for (i=pos+1;i<=n;i++) if ((o[i]&3)==(int)xtype) o[i]+=4;
1162
*_pos=pos=o[pos]>>2;
1163
memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size);
1168
// Add a child node to the given element.
1169
XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos)
1171
if (!lpszName) return emptyXMLNode;
1172
d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
1173
d->pChild[pos].d=NULL;
1174
d->pChild[pos]=XMLNode(d,lpszName,isDeclaration);
1176
return d->pChild[pos];
1179
// Add an attribute to an element.
1180
XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLSTR lpszName, XMLSTR lpszValuev)
1182
if (!lpszName) return &emptyXMLAttribute;
1183
if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; }
1184
int nc=d->nAttribute;
1185
d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute));
1186
XMLAttribute *pAttr=d->pAttribute+nc;
1187
pAttr->lpszName = lpszName;
1188
pAttr->lpszValue = lpszValuev;
1193
// Add text to the element.
1194
XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos)
1196
if (!lpszValue) return NULL;
1197
if (!d) { myFree(lpszValue); return NULL; }
1198
d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText);
1199
d->pText[pos]=lpszValue;
1204
// Add clear (unformatted) text to the element.
1205
XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
1207
if (!lpszValue) return &emptyXMLClear;
1208
if (!d) { myFree(lpszValue); return &emptyXMLClear; }
1209
d->pClear=(XMLClear *)addToOrder(memoryIncrease,&pos,d->nClear,d->pClear,sizeof(XMLClear),eNodeClear);
1210
XMLClear *pNewClear=d->pClear+pos;
1211
pNewClear->lpszValue = lpszValue;
1212
if (!lpszOpen) lpszOpen=XMLClearTags->lpszOpen;
1213
if (!lpszClose) lpszClose=XMLClearTags->lpszClose;
1214
pNewClear->lpszOpenTag = lpszOpen;
1215
pNewClear->lpszCloseTag = lpszClose;
1221
// Parse a clear (unformatted) type node.
1222
char XMLNode::parseClearTag(void *px, void *_pClear)
1224
XML *pXML=(XML *)px;
1225
ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear);
1227
XMLCSTR lpszTemp=NULL;
1228
XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
1229
static XMLCSTR docTypeEnd=_CXML("]>");
1231
// Find the closing tag
1232
// Seems the <!DOCTYPE need a better treatment so lets handle it
1233
if (pClear.lpszOpen==XMLClearTags[1].lpszOpen)
1238
if (*pCh==_CXML('<')) { pClear.lpszClose=docTypeEnd; lpszTemp=xstrstr(lpXML,docTypeEnd); break; }
1239
else if (*pCh==_CXML('>')) { lpszTemp=pCh; break; }
1243
pCh+=XML_ByteTable[(unsigned char)(*pCh)];
1246
} else lpszTemp=xstrstr(lpXML, pClear.lpszClose);
1250
// Cache the size and increment the index
1251
cbTemp = (int)(lpszTemp - lpXML);
1253
pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
1255
// Add the clear node to the current element
1256
addClear_priv(MEMORYINCREASE,cbTemp?stringDup(lpXML,cbTemp):NULL, pClear.lpszOpen, pClear.lpszClose,-1);
1260
// If we failed to find the end tag
1261
pXML->error = eXMLErrorUnmatchedEndClearTag;
1265
void XMLNode::exactMemory(XMLNodeData *d)
1267
if (d->pOrder) d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int));
1268
if (d->pChild) d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode));
1269
if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute));
1270
if (d->pText) d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR));
1271
if (d->pClear) d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear));
1274
char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
1276
XML *pXML=(XML *)pa;
1277
XMLCSTR lpszText=pXML->lpszText;
1278
if (!lpszText) return 0;
1279
if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
1280
int cbText = (int)(tokenPStr - lpszText);
1281
if (!cbText) { pXML->lpszText=NULL; return 0; }
1282
if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
1283
if (!cbText) { pXML->lpszText=NULL; return 0; }
1284
XMLSTR lpt=fromXMLString(lpszText,cbText,pXML);
1286
pXML->lpszText=NULL;
1287
if (removeCommentsInMiddleOfText && d->nText && d->nClear)
1289
// if the previous insertion was a comment (<!-- -->) AND
1290
// if the previous previous insertion was a text then, delete the comment and append the text
1291
int n=d->nChild+d->nText+d->nClear-1,*o=d->pOrder;
1292
if (((o[n]&3)==eNodeClear)&&((o[n-1]&3)==eNodeText))
1295
if (d->pClear[i].lpszOpenTag==XMLClearTags[2].lpszOpen)
1299
n=xstrlen(d->pText[i]);
1300
int n2=xstrlen(lpt)+1;
1301
d->pText[i]=(XMLSTR)realloc((void*)d->pText[i],(n+n2)*sizeof(XMLCHAR));
1302
if (!d->pText[i]) return 1;
1303
memcpy((void*)(d->pText[i]+n),lpt,n2*sizeof(XMLCHAR));
1309
addText_priv(MEMORYINCREASE,lpt,-1);
1313
// Recursively parse an XML element.
1314
int XMLNode::ParseXMLElement(void *pa)
1316
XML *pXML=(XML *)pa;
1318
enum XMLTokenTypeTag xtype;
1320
XMLCSTR lpszTemp=NULL;
1324
enum XMLStatus status; // inside or outside a tag
1325
enum Attrib attrib = eAttribName;
1329
// If this is the first call to the function
1332
// Assume we are outside of a tag definition
1333
pXML->nFirst = FALSE;
1334
status = eOutsideTag;
1337
// If this is not the first call then we should only be called when inside a tag.
1338
status = eInsideTag;
1341
// Iterate through the tokens in the document
1344
// Obtain the next token
1345
token = GetNextToken(pXML, &cbToken, &xtype);
1347
if (xtype != eTokenError)
1349
// Check the current status
1353
// If we are outside of a tag definition
1356
// Check what type of token we obtained
1359
// If we have found text or quoted text
1361
case eTokenCloseTag: /* '>' */
1362
case eTokenShortHandClose: /* '/>' */
1363
case eTokenQuotedText:
1367
// If we found a start tag '<' and declarations '<?'
1368
case eTokenTagStart:
1369
case eTokenDeclaration:
1371
// Cache whether this new element is a declaration or not
1372
nDeclaration = (xtype == eTokenDeclaration);
1374
// If we have node text then add this to the element
1375
if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1377
// Find the name of the tag
1378
token = GetNextToken(pXML, &cbToken, &xtype);
1380
// Return an error if we couldn't obtain the next token or
1382
if (xtype != eTokenText)
1384
pXML->error = eXMLErrorMissingTagName;
1388
// If we found a new element which is the same as this
1389
// element then we need to pass this back to the caller..
1391
#ifdef APPROXIMATE_PARSING
1393
myTagCompare(d->lpszName, token.pStr) == 0)
1395
// Indicate to the caller that it needs to create a
1397
pXML->lpNewElement = token.pStr;
1398
pXML->cbNewElement = cbToken;
1403
// If the name of the new element differs from the name of
1404
// the current element we need to add the new element to
1405
// the current one and recurse
1406
pNew = addChild_priv(MEMORYINCREASE,stringDup(token.pStr,cbToken), nDeclaration,-1);
1408
while (!pNew.isEmpty())
1410
// Callself to process the new node. If we return
1411
// FALSE this means we dont have any more
1412
// processing to do...
1414
if (!pNew.ParseXMLElement(pXML)) return FALSE;
1417
// If the call to recurse this function
1418
// evented in a end tag specified in XML then
1419
// we need to unwind the calls to this
1420
// function until we find the appropriate node
1421
// (the element name and end tag name must
1425
// If we are back at the root node then we
1426
// have an unmatched end tag
1429
pXML->error=eXMLErrorUnmatchedEndTag;
1433
// If the end tag matches the name of this
1434
// element then we only need to unwind
1437
if (myTagCompare(d->lpszName, pXML->lpEndTag)==0)
1444
if (pXML->cbNewElement)
1446
// If the call indicated a new element is to
1447
// be created on THIS element.
1449
// If the name of this element matches the
1450
// name of the element we need to create
1451
// then we need to return to the caller
1452
// and let it process the element.
1454
if (myTagCompare(d->lpszName, pXML->lpNewElement)==0)
1459
// Add the new element and recurse
1460
pNew = addChild_priv(MEMORYINCREASE,stringDup(pXML->lpNewElement,pXML->cbNewElement),0,-1);
1461
pXML->cbNewElement = 0;
1465
// If we didn't have a new element to create
1466
pNew = emptyXMLNode;
1474
// If we found an end tag
1477
// If we have node text then add this to the element
1478
if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1480
// Find the name of the end tag
1481
token = GetNextToken(pXML, &cbTemp, &xtype);
1483
// The end tag should be text
1484
if (xtype != eTokenText)
1486
pXML->error = eXMLErrorMissingEndTagName;
1489
lpszTemp = token.pStr;
1491
// After the end tag we should find a closing tag
1492
token = GetNextToken(pXML, &cbToken, &xtype);
1493
if (xtype != eTokenCloseTag)
1495
pXML->error = eXMLErrorMissingEndTagName;
1498
pXML->lpszText=pXML->lpXML+pXML->nIndex;
1500
// We need to return to the previous caller. If the name
1501
// of the tag cannot be found we need to keep returning to
1502
// caller until we find a match
1503
if (myTagCompare(d->lpszName, lpszTemp) != 0)
1504
#ifdef STRICT_PARSING
1506
pXML->error=eXMLErrorUnmatchedEndTag;
1507
pXML->nIndexMissigEndTag=pXML->nIndex;
1512
pXML->error=eXMLErrorMissingEndTag;
1513
pXML->nIndexMissigEndTag=pXML->nIndex;
1514
pXML->lpEndTag = lpszTemp;
1515
pXML->cbEndTag = cbTemp;
1519
// Return to the caller
1523
// If we found a clear (unformatted) token
1525
// If we have node text then add this to the element
1526
if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1527
if (parseClearTag(pXML, token.pClr)) return FALSE;
1528
pXML->lpszText=pXML->lpXML+pXML->nIndex;
1536
// If we are inside a tag definition we need to search for attributes
1539
// Check what part of the attribute (name, equals, value) we
1543
// If we are looking for a new attribute
1546
// Check what the current token type is
1549
// If the current type is text...
1552
// Cache the token then indicate that we are next to
1553
// look for the equals
1554
lpszTemp = token.pStr;
1556
attrib = eAttribEquals;
1559
// If we found a closing tag...
1561
case eTokenCloseTag:
1562
// We are now outside the tag
1563
status = eOutsideTag;
1564
pXML->lpszText=pXML->lpXML+pXML->nIndex;
1567
// If we found a short hand '/>' closing tag then we can
1568
// return to the caller
1569
case eTokenShortHandClose:
1571
pXML->lpszText=pXML->lpXML+pXML->nIndex;
1575
case eTokenQuotedText: /* '"SomeText"' */
1576
case eTokenTagStart: /* '<' */
1577
case eTokenTagEnd: /* '</' */
1578
case eTokenEquals: /* '=' */
1579
case eTokenDeclaration: /* '<?' */
1581
pXML->error = eXMLErrorUnexpectedToken;
1587
// If we are looking for an equals
1589
// Check what the current token type is
1592
// If the current type is text...
1593
// Eg. 'Attribute AnotherAttribute'
1595
// Add the unvalued attribute to the list
1596
addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1597
// Cache the token then indicate. We are next to
1598
// look for the equals attribute
1599
lpszTemp = token.pStr;
1603
// If we found a closing tag 'Attribute >' or a short hand
1604
// closing tag 'Attribute />'
1605
case eTokenShortHandClose:
1606
case eTokenCloseTag:
1607
// If we are a declaration element '<?' then we need
1608
// to remove extra closing '?' if it exists
1609
pXML->lpszText=pXML->lpXML+pXML->nIndex;
1611
if (d->isDeclaration &&
1612
(lpszTemp[cbTemp-1]) == _CXML('?'))
1615
if (d->pParent && d->pParent->pParent) xtype = eTokenShortHandClose;
1620
// Add the unvalued attribute to the list
1621
addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1624
// If this is the end of the tag then return to the caller
1625
if (xtype == eTokenShortHandClose)
1631
// We are now outside the tag
1632
status = eOutsideTag;
1635
// If we found the equals token...
1636
// Eg. 'Attribute ='
1638
// Indicate that we next need to search for the value
1639
// for the attribute
1640
attrib = eAttribValue;
1644
case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/
1645
case eTokenTagStart: /* 'Attribute <' */
1646
case eTokenTagEnd: /* 'Attribute </' */
1647
case eTokenDeclaration: /* 'Attribute <?' */
1649
pXML->error = eXMLErrorUnexpectedToken;
1655
// If we are looking for an attribute value
1657
// Check what the current token type is
1660
// If the current type is text or quoted text...
1661
// Eg. 'Attribute = "Value"' or 'Attribute = Value' or
1662
// 'Attribute = 'Value''.
1664
case eTokenQuotedText:
1665
// If we are a declaration element '<?' then we need
1666
// to remove extra closing '?' if it exists
1667
if (d->isDeclaration &&
1668
(token.pStr[cbToken-1]) == _CXML('?'))
1675
// Add the valued attribute to the list
1676
if (xtype==eTokenQuotedText) { token.pStr++; cbToken-=2; }
1677
XMLSTR attrVal=(XMLSTR)token.pStr;
1680
attrVal=fromXMLString(attrVal,cbToken,pXML);
1681
if (!attrVal) return FALSE;
1683
addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp),attrVal);
1686
// Indicate we are searching for a new attribute
1687
attrib = eAttribName;
1691
case eTokenTagStart: /* 'Attr = <' */
1692
case eTokenTagEnd: /* 'Attr = </' */
1693
case eTokenCloseTag: /* 'Attr = >' */
1694
case eTokenShortHandClose: /* "Attr = />" */
1695
case eTokenEquals: /* 'Attr = =' */
1696
case eTokenDeclaration: /* 'Attr = <?' */
1698
pXML->error = eXMLErrorUnexpectedToken;
1706
// If we failed to obtain the next token
1709
if ((!d->isDeclaration)&&(d->pParent))
1711
#ifdef STRICT_PARSING
1712
pXML->error=eXMLErrorUnmatchedEndTag;
1714
pXML->error=eXMLErrorMissingEndTag;
1716
pXML->nIndexMissigEndTag=pXML->nIndex;
1718
maybeAddTxT(pXML,pXML->lpXML+pXML->nIndex);
1724
// Count the number of lines and columns in an XML string.
1725
static void CountLinesAndColumns(XMLCSTR lpXML, int nUpto, XMLResults *pResults)
1731
struct XML xml={ lpXML,lpXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1733
pResults->nLine = 1;
1734
pResults->nColumn = 1;
1735
while (xml.nIndex<nUpto)
1737
ch = getNextChar(&xml);
1738
if (ch != _CXML('\n')) pResults->nColumn++;
1742
pResults->nColumn=1;
1747
// Parse XML and return the root element.
1748
XMLNode XMLNode::parseString(XMLCSTR lpszXML, XMLCSTR tag, XMLResults *pResults)
1754
pResults->error=eXMLErrorNoElements;
1756
pResults->nColumn=0;
1758
return emptyXMLNode;
1761
XMLNode xnode(NULL,NULL,FALSE);
1762
struct XML xml={ lpszXML, lpszXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1764
// Create header element
1765
xnode.ParseXMLElement(&xml);
1766
enum XMLError error = xml.error;
1767
if (!xnode.nChildNode()) error=eXMLErrorNoXMLTagFound;
1768
if ((xnode.nChildNode()==1)&&(xnode.nElement()==1)) xnode=xnode.getChildNode(); // skip the empty node
1770
// If no error occurred
1771
if ((error==eXMLErrorNone)||(error==eXMLErrorMissingEndTag)||(error==eXMLErrorNoXMLTagFound))
1773
XMLCSTR name=xnode.getName();
1774
if (tag&&(*tag)&&((!name)||(xstricmp(name,tag))))
1776
xnode=xnode.getChildNode(tag);
1777
if (xnode.isEmpty())
1781
pResults->error=eXMLErrorFirstTagNotFound;
1783
pResults->nColumn=0;
1785
return emptyXMLNode;
1790
// Cleanup: this will destroy all the nodes
1791
xnode = emptyXMLNode;
1795
// If we have been given somewhere to place results
1798
pResults->error = error;
1800
// If we have an error
1801
if (error!=eXMLErrorNone)
1803
if (error==eXMLErrorMissingEndTag) xml.nIndex=xml.nIndexMissigEndTag;
1804
// Find which line and column it starts on.
1805
CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
1811
XMLNode XMLNode::parseFile(XMLCSTR filename, XMLCSTR tag, XMLResults *pResults)
1813
if (pResults) { pResults->nLine=0; pResults->nColumn=0; }
1814
FILE *f=xfopen(filename,_CXML("rb"));
1815
if (f==NULL) { if (pResults) pResults->error=eXMLErrorFileNotFound; return emptyXMLNode; }
1816
fseek(f,0,SEEK_END);
1817
int l=(int)ftell(f),headerSz=0;
1818
if (!l) { if (pResults) pResults->error=eXMLErrorEmpty; fclose(f); return emptyXMLNode; }
1819
fseek(f,0,SEEK_SET);
1820
unsigned char *buf=(unsigned char*)malloc(l+4);
1821
l=(int)fread(buf,1,l,f);
1823
buf[l]=0;buf[l+1]=0;buf[l+2]=0;buf[l+3]=0;
1825
if (guessWideCharChars)
1827
if (!myIsTextWideChar(buf,l))
1829
XMLNode::XMLCharEncoding ce=XMLNode::char_encoding_legacy;
1830
if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) { headerSz=3; ce=XMLNode::char_encoding_UTF8; }
1831
XMLSTR b2=myMultiByteToWideChar((const char*)(buf+headerSz),ce);
1834
// todo: unable to convert
1836
free(buf); buf=(unsigned char*)b2; headerSz=0;
1839
if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1840
if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1844
if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1845
if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1846
if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1849
if (guessWideCharChars)
1851
if (myIsTextWideChar(buf,l))
1853
if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1854
if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1855
char *b2=myWideCharToMultiByte((const wchar_t*)(buf+headerSz));
1856
free(buf); buf=(unsigned char*)b2; headerSz=0;
1859
if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1863
if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1864
if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1865
if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1869
if (!buf) { if (pResults) pResults->error=eXMLErrorCharConversionError; return emptyXMLNode; }
1870
XMLNode x=parseString((XMLSTR)(buf+headerSz),tag,pResults);
1875
static inline void charmemset(XMLSTR dest,XMLCHAR c,int l) { while (l--) *(dest++)=c; }
1877
// Creates an user friendly XML string from a given element with
1878
// appropriate white space and carriage returns.
1880
// This recurses through all subnodes then adds contents of the nodes to the
1882
int XMLNode::CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat)
1885
int cb=nFormat<0?0:nFormat;
1887
int nChildFormat=-1;
1888
int nElementI=pEntry->nChild+pEntry->nText+pEntry->nClear;
1890
if ((nFormat>=0)&&(nElementI==1)&&(pEntry->nText==1)&&(!pEntry->isDeclaration)) nFormat=-2;
1894
#define LENSTR(lpsz) (lpsz ? xstrlen(lpsz) : 0)
1896
// If the element has no name then assume this is the head node.
1897
cbElement = (int)LENSTR(pEntry->lpszName);
1904
if (cb) charmemset(lpszMarker, INDENTCHAR, cb);
1906
lpszMarker[nResult++]=_CXML('<');
1907
if (pEntry->isDeclaration) lpszMarker[nResult++]=_CXML('?');
1908
xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
1910
lpszMarker[nResult++]=_CXML(' ');
1914
nResult+=cbElement+2+cb;
1915
if (pEntry->isDeclaration) nResult++;
1918
// Enumerate attributes and add them to the string
1919
XMLAttribute *pAttr=pEntry->pAttribute;
1920
for (i=0; i<pEntry->nAttribute; i++)
1923
cb = (int)LENSTR(pAttr->lpszName);
1926
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pAttr->lpszName);
1929
if (pAttr->lpszValue)
1931
cb=(int)ToXMLStringTool::lengthXMLString(pAttr->lpszValue);
1934
lpszMarker[nResult]=_CXML('=');
1935
lpszMarker[nResult+1]=_CXML('"');
1936
if (cb) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+2],pAttr->lpszValue);
1937
lpszMarker[nResult+cb+2]=_CXML('"');
1941
if (lpszMarker) lpszMarker[nResult] = _CXML(' ');
1947
if (pEntry->isDeclaration)
1951
lpszMarker[nResult-1]=_CXML('?');
1952
lpszMarker[nResult]=_CXML('>');
1957
if (lpszMarker) lpszMarker[nResult]=_CXML('\n');
1961
// If there are child nodes we need to terminate the start tag
1964
if (lpszMarker) lpszMarker[nResult-1]=_CXML('>');
1967
if (lpszMarker) lpszMarker[nResult]=_CXML('\n');
1973
// Calculate the child format for when we recurse. This is used to
1974
// determine the number of spaces used for prefixes.
1977
if (cbElement&&(!pEntry->isDeclaration)) nChildFormat=nFormat+1;
1978
else nChildFormat=nFormat;
1981
// Enumerate through remaining children
1982
for (i=0; i<nElementI; i++)
1984
j=pEntry->pOrder[i];
1985
switch((XMLElementType)(j&3))
1991
XMLCSTR pChild=pEntry->pText[j>>2];
1992
cb = (int)ToXMLStringTool::lengthXMLString(pChild);
1999
charmemset(&lpszMarker[nResult],INDENTCHAR,nFormat+1);
2000
ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+nFormat+1],pChild);
2001
lpszMarker[nResult+nFormat+1+cb]=_CXML('\n');
2003
nResult+=cb+nFormat+2;
2006
if (lpszMarker) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult], pChild);
2016
XMLClear *pChild=pEntry->pClear+(j>>2);
2018
cb = (int)LENSTR(pChild->lpszOpenTag);
2025
charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat+1);
2026
xstrcpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag);
2028
nResult+=cb+nFormat+1;
2032
if (lpszMarker)xstrcpy(&lpszMarker[nResult], pChild->lpszOpenTag);
2038
cb = (int)LENSTR(pChild->lpszValue);
2041
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszValue);
2045
// "OpenTag Value CloseTag"
2046
cb = (int)LENSTR(pChild->lpszCloseTag);
2049
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszCloseTag);
2055
if (lpszMarker) lpszMarker[nResult] = _CXML('\n');
2064
// Recursively add child nodes
2065
nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
2072
if ((cbElement)&&(!pEntry->isDeclaration))
2074
// If we have child entries we need to use long XML notation for
2075
// closing the element - "<elementname>blah blah blah</elementname>"
2078
// "</elementname>\0"
2083
charmemset(&lpszMarker[nResult], INDENTCHAR,nFormat);
2087
lpszMarker[nResult]=_CXML('<'); lpszMarker[nResult+1]=_CXML('/');
2089
xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
2090
nResult += cbElement;
2092
lpszMarker[nResult]=_CXML('>');
2093
if (nFormat == -1) nResult++;
2096
lpszMarker[nResult+1]=_CXML('\n');
2101
if (nFormat>=0) nResult+=cbElement+4+nFormat;
2102
else if (nFormat==-1) nResult+=cbElement+3;
2103
else nResult+=cbElement+4;
2107
// If there are no children we can use shorthand XML notation -
2112
lpszMarker[nResult]=_CXML('/'); lpszMarker[nResult+1]=_CXML('>');
2113
if (nFormat != -1) lpszMarker[nResult+2]=_CXML('\n');
2115
nResult += nFormat == -1 ? 2 : 3;
2124
// Create an XML string
2125
// @param int nFormat - 0 if no formatting is required
2126
// otherwise nonzero for formatted text
2127
// with carriage returns and indentation.
2128
// @param int *pnSize - [out] pointer to the size of the
2129
// returned string not including the
2131
// @return XMLSTR - Allocated XML string, you must free
2132
// this with free().
2133
XMLSTR XMLNode::createXMLString(int nFormat, int *pnSize) const
2135
if (!d) { if (pnSize) *pnSize=0; return NULL; }
2137
XMLSTR lpszResult = NULL;
2140
// Recursively Calculate the size of the XML string
2141
if (!dropWhiteSpace) nFormat=0;
2142
nFormat = nFormat ? 0 : -1;
2143
cbStr = CreateXMLStringR(d, 0, nFormat);
2144
// Alllocate memory for the XML string + the NULL terminator and
2145
// create the recursively XML string.
2146
lpszResult=(XMLSTR)malloc((cbStr+1)*sizeof(XMLCHAR));
2147
CreateXMLStringR(d, lpszResult, nFormat);
2148
lpszResult[cbStr]=_CXML('\0');
2149
if (pnSize) *pnSize = cbStr;
2153
int XMLNode::detachFromParent(XMLNodeData *d)
2155
XMLNode *pa=d->pParent->pChild;
2157
while (((void*)(pa[i].d))!=((void*)d)) i++;
2158
d->pParent->nChild--;
2159
if (d->pParent->nChild) memmove(pa+i,pa+i+1,(d->pParent->nChild-i)*sizeof(XMLNode));
2160
else { free(pa); d->pParent->pChild=NULL; }
2161
return removeOrderElement(d->pParent,eNodeChild,i);
2170
void XMLNode::deleteNodeContent()
2173
if (d->pParent) { detachFromParent(d); d->pParent=NULL; d->ref_count--; }
2176
void XMLNode::emptyTheNode(char force)
2178
XMLNodeData *dd=d; // warning: must stay this way!
2179
if ((dd->ref_count==0)||force)
2181
if (d->pParent) detachFromParent(d);
2184
for(i=0; i<dd->nChild; i++)
2187
pc->d->pParent=NULL;
2189
pc->emptyTheNode(force);
2192
for(i=0; i<dd->nText; i++) free((void*)dd->pText[i]);
2194
for(i=0; i<dd->nClear; i++) free((void*)dd->pClear[i].lpszValue);
2196
for(i=0; i<dd->nAttribute; i++)
2198
free((void*)dd->pAttribute[i].lpszName);
2199
if (dd->pAttribute[i].lpszValue) free((void*)dd->pAttribute[i].lpszValue);
2201
myFree(dd->pAttribute);
2203
myFree((void*)dd->lpszName);
2204
dd->nChild=0; dd->nText=0; dd->nClear=0; dd->nAttribute=0;
2205
dd->pChild=NULL; dd->pText=NULL; dd->pClear=NULL; dd->pAttribute=NULL;
2206
dd->pOrder=NULL; dd->lpszName=NULL; dd->pParent=NULL;
2208
if (dd->ref_count==0)
2215
XMLNode& XMLNode::operator=( const XMLNode& A )
2220
if (d) { d->ref_count--; emptyTheNode(0); }
2222
if (d) (d->ref_count) ++ ;
2227
XMLNode::XMLNode(const XMLNode &A)
2231
if (d) (d->ref_count)++ ;
2234
XMLNode XMLNode::deepCopy() const
2236
if (!d) return XMLNode::emptyXMLNode;
2237
XMLNode x(NULL,stringDup(d->lpszName),d->isDeclaration);
2239
int n=d->nAttribute;
2242
p->nAttribute=n; p->pAttribute=(XMLAttribute*)malloc(n*sizeof(XMLAttribute));
2245
p->pAttribute[n].lpszName=stringDup(d->pAttribute[n].lpszName);
2246
p->pAttribute[n].lpszValue=stringDup(d->pAttribute[n].lpszValue);
2251
n=(d->nChild+d->nText+d->nClear)*sizeof(int); p->pOrder=(int*)malloc(n); memcpy(p->pOrder,d->pOrder,n);
2256
p->nText=n; p->pText=(XMLCSTR*)malloc(n*sizeof(XMLCSTR));
2257
while(n--) p->pText[n]=stringDup(d->pText[n]);
2262
p->nClear=n; p->pClear=(XMLClear*)malloc(n*sizeof(XMLClear));
2265
p->pClear[n].lpszCloseTag=d->pClear[n].lpszCloseTag;
2266
p->pClear[n].lpszOpenTag=d->pClear[n].lpszOpenTag;
2267
p->pClear[n].lpszValue=stringDup(d->pClear[n].lpszValue);
2273
p->nChild=n; p->pChild=(XMLNode*)malloc(n*sizeof(XMLNode));
2276
p->pChild[n].d=NULL;
2277
p->pChild[n]=d->pChild[n].deepCopy();
2278
p->pChild[n].d->pParent=p;
2284
XMLNode XMLNode::addChild(XMLNode childNode, int pos)
2286
XMLNodeData *dc=childNode.d;
2287
if ((!dc)||(!d)) return childNode;
2290
// this is a root node: todo: correct fix
2294
addChild(dc->pChild[0],j);
2299
if (dc->pParent) { if ((detachFromParent(dc)<=pos)&&(dc->pParent==d)) pos--; } else dc->ref_count++;
2301
// int nc=d->nChild;
2302
// d->pChild=(XMLNode*)myRealloc(d->pChild,(nc+1),memoryIncrease,sizeof(XMLNode));
2303
d->pChild=(XMLNode*)addToOrder(0,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
2304
d->pChild[pos].d=dc;
2309
void XMLNode::deleteAttribute(int i)
2311
if ((!d)||(i<0)||(i>=d->nAttribute)) return;
2313
XMLAttribute *p=d->pAttribute+i;
2314
free((void*)p->lpszName);
2315
if (p->lpszValue) free((void*)p->lpszValue);
2316
if (d->nAttribute) memmove(p,p+1,(d->nAttribute-i)*sizeof(XMLAttribute)); else { free(p); d->pAttribute=NULL; }
2319
void XMLNode::deleteAttribute(XMLAttribute *a){ if (a) deleteAttribute(a->lpszName); }
2320
void XMLNode::deleteAttribute(XMLCSTR lpszName)
2323
getAttribute(lpszName,&j);
2324
if (j) deleteAttribute(j-1);
2327
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,int i)
2329
if (!d) { if (lpszNewValue) free(lpszNewValue); if (lpszNewName) free(lpszNewName); return NULL; }
2330
if (i>=d->nAttribute)
2332
if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2335
XMLAttribute *p=d->pAttribute+i;
2336
if (p->lpszValue&&p->lpszValue!=lpszNewValue) free((void*)p->lpszValue);
2337
p->lpszValue=lpszNewValue;
2338
if (lpszNewName&&p->lpszName!=lpszNewName) { free((void*)p->lpszName); p->lpszName=lpszNewName; };
2342
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2344
if (oldAttribute) return updateAttribute_WOSD((XMLSTR)newAttribute->lpszValue,(XMLSTR)newAttribute->lpszName,oldAttribute->lpszName);
2345
return addAttribute_WOSD((XMLSTR)newAttribute->lpszName,(XMLSTR)newAttribute->lpszValue);
2348
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName)
2351
getAttribute(lpszOldName,&j);
2352
if (j) return updateAttribute_WOSD(lpszNewValue,lpszNewName,j-1);
2355
if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2356
else return addAttribute_WOSD(stringDup(lpszOldName),lpszNewValue);
2360
int XMLNode::indexText(XMLCSTR lpszValue) const
2364
if (!lpszValue) { if (l) return 0; return -1; }
2365
XMLCSTR *p=d->pText;
2366
for (i=0; i<l; i++) if (lpszValue==p[i]) return i;
2370
void XMLNode::deleteText(int i)
2372
if ((!d)||(i<0)||(i>=d->nText)) return;
2374
XMLCSTR *p=d->pText+i;
2376
if (d->nText) memmove(p,p+1,(d->nText-i)*sizeof(XMLCSTR)); else { free(p); d->pText=NULL; }
2377
removeOrderElement(d,eNodeText,i);
2380
void XMLNode::deleteText(XMLCSTR lpszValue) { deleteText(indexText(lpszValue)); }
2382
XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, int i)
2384
if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
2385
if (i>=d->nText) return addText_WOSD(lpszNewValue);
2386
XMLCSTR *p=d->pText+i;
2387
if (*p!=lpszNewValue) { free((void*)*p); *p=lpszNewValue; }
2388
return lpszNewValue;
2391
XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue)
2393
if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
2394
int i=indexText(lpszOldValue);
2395
if (i>=0) return updateText_WOSD(lpszNewValue,i);
2396
return addText_WOSD(lpszNewValue);
2399
void XMLNode::deleteClear(int i)
2401
if ((!d)||(i<0)||(i>=d->nClear)) return;
2403
XMLClear *p=d->pClear+i;
2404
free((void*)p->lpszValue);
2405
if (d->nClear) memmove(p,p+1,(d->nClear-i)*sizeof(XMLClear)); else { free(p); d->pClear=NULL; }
2406
removeOrderElement(d,eNodeClear,i);
2409
int XMLNode::indexClear(XMLCSTR lpszValue) const
2413
if (!lpszValue) { if (l) return 0; return -1; }
2414
XMLClear *p=d->pClear;
2415
for (i=0; i<l; i++) if (lpszValue==p[i].lpszValue) return i;
2419
void XMLNode::deleteClear(XMLCSTR lpszValue) { deleteClear(indexClear(lpszValue)); }
2420
void XMLNode::deleteClear(XMLClear *a) { if (a) deleteClear(a->lpszValue); }
2422
XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, int i)
2424
if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
2425
if (i>=d->nClear) return addClear_WOSD(lpszNewContent);
2426
XMLClear *p=d->pClear+i;
2427
if (lpszNewContent!=p->lpszValue) { free((void*)p->lpszValue); p->lpszValue=lpszNewContent; }
2431
XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, XMLCSTR lpszOldValue)
2433
if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
2434
int i=indexClear(lpszOldValue);
2435
if (i>=0) return updateClear_WOSD(lpszNewContent,i);
2436
return addClear_WOSD(lpszNewContent);
2439
XMLClear *XMLNode::updateClear_WOSD(XMLClear *newP,XMLClear *oldP)
2441
if (oldP) return updateClear_WOSD((XMLSTR)newP->lpszValue,(XMLSTR)oldP->lpszValue);
2445
int XMLNode::nChildNode(XMLCSTR name) const
2448
int i,j=0,n=d->nChild;
2449
XMLNode *pc=d->pChild;
2452
if (xstricmp(pc->d->lpszName, name)==0) j++;
2458
XMLNode XMLNode::getChildNode(XMLCSTR name, int *j) const
2460
if (!d) return emptyXMLNode;
2461
int i=0,n=d->nChild;
2463
XMLNode *pc=d->pChild+i;
2466
if (!xstricmp(pc->d->lpszName, name))
2473
return emptyXMLNode;
2476
XMLNode XMLNode::getChildNode(XMLCSTR name, int j) const
2478
if (!d) return emptyXMLNode;
2482
while (j-->0) getChildNode(name,&i);
2483
return getChildNode(name,&i);
2486
while (i--) if (!xstricmp(name,d->pChild[i].d->lpszName)) break;
2487
if (i<0) return emptyXMLNode;
2488
return getChildNode(i);
2491
XMLNode XMLNode::getChildNodeByPath(XMLCSTR _path, char createMissing, XMLCHAR sep)
2493
XMLSTR path=stringDup(_path);
2494
XMLNode x=getChildNodeByPathNonConst(path,createMissing,sep);
2495
if (path) free(path);
2499
XMLNode XMLNode::getChildNodeByPathNonConst(XMLSTR path, char createIfMissing, XMLCHAR sep)
2501
if ((!path)||(!(*path))) return *this;
2502
XMLNode xn,xbase=*this;
2503
XMLCHAR *tend1,sepString[2]; sepString[0]=sep; sepString[1]=0;
2504
tend1=xstrstr(path,sepString);
2508
xn=xbase.getChildNode(path);
2511
if (createIfMissing) xn=xbase.addChild(path);
2512
else { *tend1=sep; return XMLNode::emptyXMLNode; }
2517
tend1=xstrstr(path,sepString);
2519
xn=xbase.getChildNode(path);
2520
if (xn.isEmpty()&&createIfMissing) xn=xbase.addChild(path);
2524
XMLElementPosition XMLNode::positionOfText (int i) const { if (i>=d->nText ) i=d->nText-1; return findPosition(d,i,eNodeText ); }
2525
XMLElementPosition XMLNode::positionOfClear (int i) const { if (i>=d->nClear) i=d->nClear-1; return findPosition(d,i,eNodeClear); }
2526
XMLElementPosition XMLNode::positionOfChildNode(int i) const { if (i>=d->nChild) i=d->nChild-1; return findPosition(d,i,eNodeChild); }
2527
XMLElementPosition XMLNode::positionOfText (XMLCSTR lpszValue) const { return positionOfText (indexText (lpszValue)); }
2528
XMLElementPosition XMLNode::positionOfClear(XMLCSTR lpszValue) const { return positionOfClear(indexClear(lpszValue)); }
2529
XMLElementPosition XMLNode::positionOfClear(XMLClear *a) const { if (a) return positionOfClear(a->lpszValue); return positionOfClear(); }
2530
XMLElementPosition XMLNode::positionOfChildNode(XMLNode x) const
2532
if ((!d)||(!x.d)) return -1;
2533
XMLNodeData *dd=x.d;
2534
XMLNode *pc=d->pChild;
2536
while (i--) if (pc[i].d==dd) return findPosition(d,i,eNodeChild);
2539
XMLElementPosition XMLNode::positionOfChildNode(XMLCSTR name, int count) const
2541
if (!name) return positionOfChildNode(count);
2543
do { getChildNode(name,&j); if (j<0) return -1; } while (count--);
2544
return findPosition(d,j-1,eNodeChild);
2547
XMLNode XMLNode::getChildNodeWithAttribute(XMLCSTR name,XMLCSTR attributeName,XMLCSTR attributeValue, int *k) const
2555
x=getChildNode(name,&i);
2563
t=x.getAttribute(attributeName,&j);
2564
if (t&&(xstricmp(attributeValue,t)==0)) { if (k) *k=i; return x; }
2568
if (x.isAttributeSet(attributeName)) { if (k) *k=i; return x; }
2571
} while (!x.isEmpty());
2572
return emptyXMLNode;
2575
// Find an attribute on an node.
2576
XMLCSTR XMLNode::getAttribute(XMLCSTR lpszAttrib, int *j) const
2578
if (!d) return NULL;
2579
int i=0,n=d->nAttribute;
2581
XMLAttribute *pAttr=d->pAttribute+i;
2584
if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2587
return pAttr->lpszValue;
2594
char XMLNode::isAttributeSet(XMLCSTR lpszAttrib) const
2596
if (!d) return FALSE;
2597
int i,n=d->nAttribute;
2598
XMLAttribute *pAttr=d->pAttribute;
2601
if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2610
XMLCSTR XMLNode::getAttribute(XMLCSTR name, int j) const
2612
if (!d) return NULL;
2614
while (j-->0) getAttribute(name,&i);
2615
return getAttribute(name,&i);
2618
XMLNodeContents XMLNode::enumContents(int i) const
2621
if (!d) { c.etype=eNodeNULL; return c; }
2622
if (i<d->nAttribute)
2624
c.etype=eNodeAttribute;
2625
c.attrib=d->pAttribute[i];
2629
c.etype=(XMLElementType)(d->pOrder[i]&3);
2630
i=(d->pOrder[i])>>2;
2633
case eNodeChild: c.child = d->pChild[i]; break;
2634
case eNodeText: c.text = d->pText[i]; break;
2635
case eNodeClear: c.clear = d->pClear[i]; break;
2641
XMLCSTR XMLNode::getName() const { if (!d) return NULL; return d->lpszName; }
2642
int XMLNode::nText() const { if (!d) return 0; return d->nText; }
2643
int XMLNode::nChildNode() const { if (!d) return 0; return d->nChild; }
2644
int XMLNode::nAttribute() const { if (!d) return 0; return d->nAttribute; }
2645
int XMLNode::nClear() const { if (!d) return 0; return d->nClear; }
2646
int XMLNode::nElement() const { if (!d) return 0; return d->nAttribute+d->nChild+d->nText+d->nClear; }
2647
XMLClear XMLNode::getClear (int i) const { if ((!d)||(i>=d->nClear )) return emptyXMLClear; return d->pClear[i]; }
2648
XMLAttribute XMLNode::getAttribute (int i) const { if ((!d)||(i>=d->nAttribute)) return emptyXMLAttribute; return d->pAttribute[i]; }
2649
XMLCSTR XMLNode::getAttributeName (int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszName; }
2650
XMLCSTR XMLNode::getAttributeValue(int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszValue; }
2651
XMLCSTR XMLNode::getText (int i) const { if ((!d)||(i>=d->nText )) return NULL; return d->pText[i]; }
2652
XMLNode XMLNode::getChildNode (int i) const { if ((!d)||(i>=d->nChild )) return emptyXMLNode; return d->pChild[i]; }
2653
XMLNode XMLNode::getParentNode ( ) const { if ((!d)||(!d->pParent )) return emptyXMLNode; return XMLNode(d->pParent); }
2654
char XMLNode::isDeclaration ( ) const { if (!d) return 0; return d->isDeclaration; }
2655
char XMLNode::isEmpty ( ) const { return (d==NULL); }
2656
XMLNode XMLNode::emptyNode ( ) { return XMLNode::emptyXMLNode; }
2658
XMLNode XMLNode::addChild(XMLCSTR lpszName, char isDeclaration, XMLElementPosition pos)
2659
{ return addChild_priv(0,stringDup(lpszName),isDeclaration,pos); }
2660
XMLNode XMLNode::addChild_WOSD(XMLSTR lpszName, char isDeclaration, XMLElementPosition pos)
2661
{ return addChild_priv(0,lpszName,isDeclaration,pos); }
2662
XMLAttribute *XMLNode::addAttribute(XMLCSTR lpszName, XMLCSTR lpszValue)
2663
{ return addAttribute_priv(0,stringDup(lpszName),stringDup(lpszValue)); }
2664
XMLAttribute *XMLNode::addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValuev)
2665
{ return addAttribute_priv(0,lpszName,lpszValuev); }
2666
XMLCSTR XMLNode::addText(XMLCSTR lpszValue, XMLElementPosition pos)
2667
{ return addText_priv(0,stringDup(lpszValue),pos); }
2668
XMLCSTR XMLNode::addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos)
2669
{ return addText_priv(0,lpszValue,pos); }
2670
XMLClear *XMLNode::addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
2671
{ return addClear_priv(0,stringDup(lpszValue),lpszOpen,lpszClose,pos); }
2672
XMLClear *XMLNode::addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
2673
{ return addClear_priv(0,lpszValue,lpszOpen,lpszClose,pos); }
2674
XMLCSTR XMLNode::updateName(XMLCSTR lpszName)
2675
{ return updateName_WOSD(stringDup(lpszName)); }
2676
XMLAttribute *XMLNode::updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2677
{ return updateAttribute_WOSD(stringDup(newAttribute->lpszValue),stringDup(newAttribute->lpszName),oldAttribute->lpszName); }
2678
XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,int i)
2679
{ return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),i); }
2680
XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName)
2681
{ return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),lpszOldName); }
2682
XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, int i)
2683
{ return updateText_WOSD(stringDup(lpszNewValue),i); }
2684
XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2685
{ return updateText_WOSD(stringDup(lpszNewValue),lpszOldValue); }
2686
XMLClear *XMLNode::updateClear(XMLCSTR lpszNewContent, int i)
2687
{ return updateClear_WOSD(stringDup(lpszNewContent),i); }
2688
XMLClear *XMLNode::updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2689
{ return updateClear_WOSD(stringDup(lpszNewValue),lpszOldValue); }
2690
XMLClear *XMLNode::updateClear(XMLClear *newP,XMLClear *oldP)
2691
{ return updateClear_WOSD(stringDup(newP->lpszValue),oldP->lpszValue); }
2693
char XMLNode::setGlobalOptions(XMLCharEncoding _characterEncoding, char _guessWideCharChars,
2694
char _dropWhiteSpace, char _removeCommentsInMiddleOfText)
2696
guessWideCharChars=_guessWideCharChars; dropWhiteSpace=_dropWhiteSpace; removeCommentsInMiddleOfText=_removeCommentsInMiddleOfText;
2698
if (_characterEncoding) characterEncoding=_characterEncoding;
2700
switch(_characterEncoding)
2702
case char_encoding_UTF8: characterEncoding=_characterEncoding; XML_ByteTable=XML_utf8ByteTable; break;
2703
case char_encoding_legacy: characterEncoding=_characterEncoding; XML_ByteTable=XML_legacyByteTable; break;
2704
case char_encoding_ShiftJIS: characterEncoding=_characterEncoding; XML_ByteTable=XML_sjisByteTable; break;
2705
case char_encoding_GB2312: characterEncoding=_characterEncoding; XML_ByteTable=XML_gb2312ByteTable; break;
2706
case char_encoding_Big5:
2707
case char_encoding_GBK: characterEncoding=_characterEncoding; XML_ByteTable=XML_gbk_big5_ByteTable; break;
2714
XMLNode::XMLCharEncoding XMLNode::guessCharEncoding(void *buf,int l, char useXMLEncodingAttribute)
2717
return (XMLCharEncoding)0;
2719
if (l<25) return (XMLCharEncoding)0;
2720
if (guessWideCharChars&&(myIsTextWideChar(buf,l))) return (XMLCharEncoding)0;
2721
unsigned char *b=(unsigned char*)buf;
2722
if ((b[0]==0xef)&&(b[1]==0xbb)&&(b[2]==0xbf)) return char_encoding_UTF8;
2724
// Match utf-8 model ?
2725
XMLCharEncoding bestGuess=char_encoding_UTF8;
2728
switch (XML_utf8ByteTable[b[i]])
2730
case 4: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2731
case 3: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2732
case 2: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2736
if (!useXMLEncodingAttribute) return bestGuess;
2737
// if encoding is specified and different from utf-8 than it's non-utf8
2738
// otherwise it's utf-8
2741
memcpy(bb,buf,l); // copy buf into bb to be able to do "bb[l]=0"
2743
b=(unsigned char*)strstr(bb,"encoding");
2744
if (!b) return bestGuess;
2745
b+=8; while XML_isSPACECHAR(*b) b++; if (*b!='=') return bestGuess;
2746
b++; while XML_isSPACECHAR(*b) b++; if ((*b!='\'')&&(*b!='"')) return bestGuess;
2747
b++; while XML_isSPACECHAR(*b) b++;
2749
if ((xstrnicmp((char*)b,"utf-8",5)==0)||
2750
(xstrnicmp((char*)b,"utf8",4)==0))
2752
if (bestGuess==char_encoding_legacy) return char_encoding_error;
2753
return char_encoding_UTF8;
2756
if ((xstrnicmp((char*)b,"shiftjis",8)==0)||
2757
(xstrnicmp((char*)b,"shift-jis",9)==0)||
2758
(xstrnicmp((char*)b,"sjis",4)==0)) return char_encoding_ShiftJIS;
2760
if (xstrnicmp((char*)b,"GB2312",6)==0) return char_encoding_GB2312;
2761
if (xstrnicmp((char*)b,"Big5",4)==0) return char_encoding_Big5;
2762
if (xstrnicmp((char*)b,"GBK",3)==0) return char_encoding_GBK;
2764
return char_encoding_legacy;
2767
#undef XML_isSPACECHAR
2769
//////////////////////////////////////////////////////////
2770
// Here starts the base64 conversion functions. //
2771
//////////////////////////////////////////////////////////
2773
static const char base64Fillchar = _CXML('='); // used to mark partial words at the end
2775
// this lookup table defines the base64 encoding
2776
XMLCSTR base64EncodeTable=_CXML("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2778
// Decode Table gives the index of any valid base64 character in the Base64 table]
2779
// 96: '=' - 97: space char - 98: illegal char - 99: end of string
2780
const unsigned char base64DecodeTable[] = {
2781
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
2782
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
2783
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
2784
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
2785
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
2786
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
2787
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
2788
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
2789
98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98 //240 -255
2792
XMLParserBase64Tool::~XMLParserBase64Tool(){ freeBuffer(); }
2794
void XMLParserBase64Tool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
2796
int XMLParserBase64Tool::encodeLength(int inlen, char formatted)
2798
unsigned int i=((inlen-1)/3*4+4+1);
2799
if (formatted) i+=inlen/54;
2803
XMLSTR XMLParserBase64Tool::encode(unsigned char *inbuf, unsigned int inlen, char formatted)
2805
int i=encodeLength(inlen,formatted),k=17,eLen=inlen/3,j;
2806
alloc(i*sizeof(XMLCHAR));
2807
XMLSTR curr=(XMLSTR)buf;
2810
// Copy next three bytes into lower 24 bits of int, paying attention to sign.
2811
j=(inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
2812
// Encode the int into four chars
2813
*(curr++)=base64EncodeTable[ j>>18 ];
2814
*(curr++)=base64EncodeTable[(j>>12)&0x3f];
2815
*(curr++)=base64EncodeTable[(j>> 6)&0x3f];
2816
*(curr++)=base64EncodeTable[(j )&0x3f];
2817
if (formatted) { if (!k) { *(curr++)=_CXML('\n'); k=18; } k--; }
2819
eLen=inlen-eLen*3; // 0 - 2.
2822
*(curr++)=base64EncodeTable[ inbuf[0]>>2 ];
2823
*(curr++)=base64EncodeTable[(inbuf[0]<<4)&0x3F];
2824
*(curr++)=base64Fillchar;
2825
*(curr++)=base64Fillchar;
2828
j=(inbuf[0]<<8)|inbuf[1];
2829
*(curr++)=base64EncodeTable[ j>>10 ];
2830
*(curr++)=base64EncodeTable[(j>> 4)&0x3f];
2831
*(curr++)=base64EncodeTable[(j<< 2)&0x3f];
2832
*(curr++)=base64Fillchar;
2838
unsigned int XMLParserBase64Tool::decodeSize(XMLCSTR data,XMLError *xe)
2840
if (!data) return 0;
2841
if (xe) *xe=eXMLErrorNone;
2844
//skip any extra characters (e.g. newlines or spaces)
2848
if (*data>255) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2850
c=base64DecodeTable[(unsigned char)(*data)];
2852
else if (c==98) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2855
if (xe&&(size%4!=0)) *xe=eXMLErrorBase64DataSizeIsNotMultipleOf4;
2856
if (size==0) return 0;
2857
do { data--; size--; } while(*data==base64Fillchar); size++;
2858
return (unsigned int)((size*3)/4);
2861
unsigned char XMLParserBase64Tool::decode(XMLCSTR data, unsigned char *buf, int len, XMLError *xe)
2863
if (!data) return 0;
2864
if (xe) *xe=eXMLErrorNone;
2871
#define BASE64DECODE_READ_NEXT_CHAR(c) \
2873
if (data[i]>255){ c=98; break; } \
2874
c=base64DecodeTable[(unsigned char)data[i++]]; \
2876
if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2878
#define BASE64DECODE_READ_NEXT_CHAR(c) \
2879
do { c=base64DecodeTable[(unsigned char)data[i++]]; }while (c==97); \
2880
if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2883
BASE64DECODE_READ_NEXT_CHAR(c)
2884
if (c==99) { return 2; }
2887
if (p==(int)len) return 2;
2888
if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;
2892
BASE64DECODE_READ_NEXT_CHAR(d)
2893
if ((d==99)||(d==96)) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2894
if (p==(int)len) { if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; return 0; }
2895
buf[p++]=(unsigned char)((c<<2)|((d>>4)&0x3));
2897
BASE64DECODE_READ_NEXT_CHAR(c)
2898
if (c==99) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2901
if (c==96) return 2;
2902
if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2905
if (c==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2906
buf[p++]=(unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf));
2908
BASE64DECODE_READ_NEXT_CHAR(d)
2909
if (d==99 ) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2912
if (d==96) return 2;
2913
if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2916
if (d==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2917
buf[p++]=(unsigned char)(((c<<6)&0xc0)|d);
2920
#undef BASE64DECODE_READ_NEXT_CHAR
2922
void XMLParserBase64Tool::alloc(int newsize)
2924
if ((!buf)&&(newsize)) { buf=malloc(newsize); buflen=newsize; return; }
2925
if (newsize>buflen) { buf=realloc(buf,newsize); buflen=newsize; }
2928
unsigned char *XMLParserBase64Tool::decode(XMLCSTR data, int *outlen, XMLError *xe)
2930
if (xe) *xe=eXMLErrorNone;
2931
if (!data) { *outlen=0; return (unsigned char*)""; }
2932
unsigned int len=decodeSize(data,xe);
2933
if (outlen) *outlen=len;
2934
if (!len) return NULL;
2936
if(!decode(data,(unsigned char*)buf,len,xe)){ return NULL; }
2937
return (unsigned char*)buf;