You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2954 lines
107 KiB

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