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.

2976 lines
108 KiB

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