mirror of
https://github.com/leethomason/tinyxml2.git
synced 2026-07-22 03:33:00 +04:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8224e427b6 | |||
| 935ddecc91 | |||
| 88d678c39e | |||
| 999a21fa86 | |||
| a737ecb2cb | |||
| 9901919829 | |||
| 63428c8852 | |||
| beb16fbca0 | |||
| 26ab0bd591 | |||
| 8d8472fe1a | |||
| 3dcad8e3c3 | |||
| 418229dc5c | |||
| 98137e6936 | |||
| 111f462dc4 | |||
| aba13c50bf | |||
| e48a1310fe | |||
| 3324d04d58 | |||
| c4e29afeaf | |||
| 5c9a452c73 | |||
| 2722500313 | |||
| b5e767a6b9 | |||
| e186491558 | |||
| 36ff404c34 | |||
| 302f4661a7 | |||
| e6caeae857 | |||
| b765a210b3 | |||
| 6e246aec4d | |||
| 30dee1efa0 | |||
| a93163d2f5 | |||
| f87d0e3077 | |||
| 487f42aa69 | |||
| afff7bf328 |
@@ -6,15 +6,15 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ windows-2019, macos-latest, ubuntu-20.04 ]
|
||||
os: [ windows-latest, macos-latest, ubuntu-latest ]
|
||||
cmake: [ 3.15, 3.x ]
|
||||
include:
|
||||
- os: windows-2019
|
||||
- os: windows-latest
|
||||
static_postfix: _static
|
||||
tree: tree /F
|
||||
CXX: cl
|
||||
|
||||
- os: ubuntu-20.04
|
||||
- os: ubuntu-latest
|
||||
tree: tree
|
||||
|
||||
- os: macos-latest
|
||||
@@ -33,7 +33,7 @@ jobs:
|
||||
# System set-up
|
||||
- uses: actions/checkout@v4
|
||||
- uses: ilammy/msvc-dev-cmd@v1
|
||||
- uses: seanmiddleditch/gha-setup-ninja@master
|
||||
- uses: seanmiddleditch/gha-setup-ninja@v4
|
||||
- uses: jwlawson/actions-setup-cmake@v2
|
||||
with:
|
||||
cmake-version: ${{ matrix.cmake }}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
TinyXML-2
|
||||
=========
|
||||
# TinyXML-2
|
||||
|
||||
[](https://github.com/leethomason/tinyxml2/actions/workflows/test.yml)
|
||||
|
||||
TinyXML-2 is a simple, small, efficient, C++ XML parser that can be
|
||||
easily integrated into other programs.
|
||||
|
||||
The master is hosted on github:
|
||||
The master is hosted on GitHub:
|
||||
https://github.com/leethomason/tinyxml2
|
||||
|
||||
The online HTML version of these docs:
|
||||
@@ -14,8 +13,7 @@ http://leethomason.github.io/tinyxml2/
|
||||
|
||||
Examples are in the "related pages" tab of the HTML docs.
|
||||
|
||||
What it does.
|
||||
-------------
|
||||
## What it does.
|
||||
|
||||
In brief, TinyXML-2 parses an XML document, and builds from that a
|
||||
Document Object Model (DOM) that can be read, modified, and saved.
|
||||
@@ -37,7 +35,7 @@ code without creating a document first.
|
||||
|
||||
TinyXML-2 is designed to be easy and fast to learn. It is one header and
|
||||
one cpp file. Simply add these to your project and off you go.
|
||||
There is an example file - xmltest.cpp - to get you started.
|
||||
There is an example file - `xmltest.cpp` - to get you started.
|
||||
|
||||
TinyXML-2 is released under the ZLib license,
|
||||
so you can use it in open source or commercial code. The details
|
||||
@@ -45,10 +43,10 @@ of the license are at the top of every source file.
|
||||
|
||||
TinyXML-2 attempts to be a flexible parser, but with truly correct and
|
||||
compliant XML output. TinyXML-2 should compile on any reasonably C++
|
||||
compliant system. It does not rely on exceptions, RTTI, or the STL.
|
||||
compliant system. It does not rely on exceptions, run-time type information,
|
||||
or the C++ Standard Library.
|
||||
|
||||
What it doesn't do.
|
||||
-------------------
|
||||
## What it doesn't do.
|
||||
|
||||
TinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs
|
||||
(eXtensible Stylesheet Language.) There are other parsers out there
|
||||
@@ -56,10 +54,9 @@ that are much more fully featured. But they are generally bigger and
|
||||
more difficult to use. If you are working with
|
||||
browsers or have more complete XML needs, TinyXML-2 is not the parser for you.
|
||||
|
||||
TinyXML-1 vs. TinyXML-2
|
||||
-----------------------
|
||||
## TinyXML-1 vs. TinyXML-2
|
||||
|
||||
TinyXML-2 long been the focus of all development. It is well tested
|
||||
TinyXML-2 has long been the focus of all development. It is well tested
|
||||
and should be used instead of TinyXML-1.
|
||||
|
||||
TinyXML-2 uses a similar API to TinyXML-1 and the same
|
||||
@@ -67,10 +64,10 @@ rich test cases. But the implementation of the parser is completely re-written
|
||||
to make it more appropriate for use in a game. It uses less memory, is faster,
|
||||
and uses far fewer memory allocations.
|
||||
|
||||
TinyXML-2 has no requirement or support for STL.
|
||||
TinyXML-2 has no dependency on the C++ Standard Library, and does not adapt or use any
|
||||
of its collection types either.
|
||||
|
||||
Features
|
||||
--------
|
||||
## Features
|
||||
|
||||
### Code Page
|
||||
|
||||
@@ -81,17 +78,17 @@ Filenames for loading / saving are passed unchanged to the underlying OS.
|
||||
|
||||
### Memory Model
|
||||
|
||||
An XMLDocument is a C++ object like any other, that can be on the stack, or
|
||||
new'd and deleted on the heap.
|
||||
An `XMLDocument` is a C++ object like any other, that can be on the stack, or
|
||||
`new`'d and `delete`d on the heap.
|
||||
|
||||
However, any sub-node of the Document, XMLElement, XMLText, etc, can only
|
||||
be created by calling the appropriate XMLDocument::NewElement, NewText, etc.
|
||||
However, any sub-node of the `XMLDocument`, `XMLElement`, `XMLText`, etc, can only
|
||||
be created by calling the appropriate `XMLDocument::NewElement`, `XMLDocument::NewText`, etc.
|
||||
method. Although you have pointers to these objects, they are still owned
|
||||
by the Document. When the Document is deleted, so are all the nodes it contains.
|
||||
by the `XMLDocument`. When the `XMLDocument` is `delete`d, so are all the nodes it contains.
|
||||
|
||||
### White Space
|
||||
|
||||
#### Whitespace Preservation (default, PRESERVE_WHITESPACE)
|
||||
#### Whitespace Preservation (default, `PRESERVE_WHITESPACE`)
|
||||
|
||||
Microsoft has an excellent article on white space: http://msdn.microsoft.com/en-us/library/ms256097.aspx
|
||||
|
||||
@@ -103,51 +100,57 @@ line-feed character, as required by the XML spec.
|
||||
|
||||
White space in text is preserved. For example:
|
||||
|
||||
<element> Hello, World</element>
|
||||
```xml
|
||||
<element> Hello, World</element>
|
||||
```
|
||||
|
||||
The leading space before the "Hello" and the double space after the comma are
|
||||
The leading space before the `"Hello"` and the double space after the comma are
|
||||
preserved. Line-feeds are preserved, as in this example:
|
||||
|
||||
<element> Hello again,
|
||||
World</element>
|
||||
```xml
|
||||
<element> Hello again,
|
||||
World</element>
|
||||
```
|
||||
|
||||
However, white space between elements is **not** preserved. Although not strictly
|
||||
compliant, tracking and reporting inter-element space is awkward, and not normally
|
||||
valuable. TinyXML-2 sees these as the same XML:
|
||||
|
||||
<document>
|
||||
<data>1</data>
|
||||
<data>2</data>
|
||||
<data>3</data>
|
||||
</document>
|
||||
```xml
|
||||
<document>
|
||||
<data>1</data>
|
||||
<data>2</data>
|
||||
<data>3</data>
|
||||
</document>
|
||||
|
||||
<document><data>1</data><data>2</data><data>3</data></document>
|
||||
<document><data>1</data><data>2</data><data>3</data></document>
|
||||
```
|
||||
|
||||
#### Whitespace Collapse (COLLAPSE_WHITESPACE)
|
||||
#### Whitespace Collapse (`COLLAPSE_WHITESPACE`)
|
||||
|
||||
For some applications, it is preferable to collapse whitespace. Collapsing
|
||||
whitespace gives you "HTML-like" behavior, which is sometimes more suitable
|
||||
for hand typed documents.
|
||||
|
||||
TinyXML-2 supports this with the 'whitespace' parameter to the XMLDocument constructor.
|
||||
TinyXML-2 supports this with the `whitespace` parameter to the `XMLDocument` constructor.
|
||||
(The default is to preserve whitespace, as described above.)
|
||||
|
||||
However, you may also use COLLAPSE_WHITESPACE, which will:
|
||||
However, you may also use `COLLAPSE_WHITESPACE`, which will:
|
||||
|
||||
* Remove leading and trailing whitespace
|
||||
* Convert newlines and line-feeds into a space character
|
||||
* Collapse a run of any number of space characters into a single space character
|
||||
|
||||
Note that (currently) there is a performance impact for using COLLAPSE_WHITESPACE.
|
||||
Note that (currently) there is a performance impact for using `COLLAPSE_WHITESPACE`.
|
||||
It essentially causes the XML to be parsed twice.
|
||||
|
||||
#### Pedantic Whitespace (PEDANTIC_WHITESPACE)
|
||||
#### Pedantic Whitespace (`PEDANTIC_WHITESPACE`)
|
||||
|
||||
For applications that need to know about text nodes that are composed entirely of
|
||||
whitespace, PEDANTIC_WHITESPACE is available. PEDANTIC_WHITESPACE maintains all the
|
||||
whilespace between elements.
|
||||
whitespace, `PEDANTIC_WHITESPACE` is available. `PEDANTIC_WHITESPACE` maintains all the
|
||||
whitespace between elements.
|
||||
|
||||
PEDANTIC_WHITESPACE is a new mode and not as tested as the other whitespace modes.
|
||||
`PEDANTIC_WHITESPACE` is a new mode and not as tested as the other whitespace modes.
|
||||
|
||||
### Error Reporting
|
||||
|
||||
@@ -163,22 +166,26 @@ line number information for error messages.
|
||||
TinyXML-2 recognizes the pre-defined "character entities", meaning special
|
||||
characters. Namely:
|
||||
|
||||
& &
|
||||
< <
|
||||
> >
|
||||
" "
|
||||
' '
|
||||
```html
|
||||
& &
|
||||
< <
|
||||
> >
|
||||
" "
|
||||
' '
|
||||
```
|
||||
|
||||
These are recognized when the XML document is read, and translated to their
|
||||
UTF-8 equivalents. For instance, text with the XML of:
|
||||
|
||||
Far & Away
|
||||
```xml
|
||||
Far & Away
|
||||
```
|
||||
|
||||
will have the Value() of "Far & Away" when queried from the XMLText object,
|
||||
will have the `Value()` of `"Far & Away"` when queried from the `XMLText` object,
|
||||
and will be written back to the XML stream/file as an ampersand.
|
||||
|
||||
Additionally, any character can be specified by its Unicode code point:
|
||||
The syntax ` ` or ` ` are both to the non-breaking space character.
|
||||
The syntax ` ` or ` ` both refer to the non-breaking space character.
|
||||
This is called a 'numeric character reference'. Any numeric character reference
|
||||
that isn't one of the special entities above, will be read, but written as a
|
||||
regular code point. The output is correct, but the entity syntax isn't preserved.
|
||||
@@ -188,21 +195,27 @@ regular code point. The output is correct, but the entity syntax isn't preserved
|
||||
#### Print to file
|
||||
You can directly use the convenience function:
|
||||
|
||||
XMLDocument doc;
|
||||
...
|
||||
doc.SaveFile( "foo.xml" );
|
||||
```cpp
|
||||
XMLDocument doc;
|
||||
// ...
|
||||
doc.SaveFile("foo.xml");
|
||||
```
|
||||
|
||||
Or the XMLPrinter class:
|
||||
Or the `XMLPrinter` class:
|
||||
|
||||
XMLPrinter printer( fp );
|
||||
doc.Print( &printer );
|
||||
```cpp
|
||||
XMLPrinter printer(fp);
|
||||
doc.Print(&printer);
|
||||
```
|
||||
|
||||
#### Print to memory
|
||||
Printing to memory is supported by the XMLPrinter.
|
||||
Printing to memory is supported by the `XMLPrinter`.
|
||||
|
||||
XMLPrinter printer;
|
||||
doc.Print( &printer );
|
||||
// printer.CStr() has a const char* to the XML
|
||||
```cpp
|
||||
XMLPrinter printer;
|
||||
doc.Print(&printer);
|
||||
// printer.CStr() has a const char* to the XML
|
||||
```
|
||||
|
||||
#### Print without an XMLDocument
|
||||
|
||||
@@ -210,92 +223,95 @@ When loading, an XML parser is very useful. However, sometimes
|
||||
when saving, it just gets in the way. The code is often set up
|
||||
for streaming, and constructing the DOM is just overhead.
|
||||
|
||||
The Printer supports the streaming case. The following code
|
||||
The `XMLPrinter` supports the streaming case. The following code
|
||||
prints out a trivially simple XML file without ever creating
|
||||
an XML document.
|
||||
|
||||
XMLPrinter printer( fp );
|
||||
printer.OpenElement( "foo" );
|
||||
printer.PushAttribute( "foo", "bar" );
|
||||
printer.CloseElement();
|
||||
```cpp
|
||||
XMLPrinter printer(fp);
|
||||
printer.OpenElement("foo");
|
||||
printer.PushAttribute("foo", "bar");
|
||||
printer.CloseElement();
|
||||
```
|
||||
|
||||
Examples
|
||||
--------
|
||||
### Examples
|
||||
|
||||
#### Load and parse an XML file.
|
||||
|
||||
/* ------ Example 1: Load and parse an XML file. ---- */
|
||||
{
|
||||
XMLDocument doc;
|
||||
doc.LoadFile( "dream.xml" );
|
||||
}
|
||||
```cpp
|
||||
/* ------ Example 1: Load and parse an XML file. ---- */
|
||||
{
|
||||
XMLDocument doc;
|
||||
doc.LoadFile("dream.xml");
|
||||
}
|
||||
```
|
||||
|
||||
#### Lookup information.
|
||||
|
||||
/* ------ Example 2: Lookup information. ---- */
|
||||
{
|
||||
XMLDocument doc;
|
||||
doc.LoadFile( "dream.xml" );
|
||||
```cpp
|
||||
/* ------ Example 2: Lookup information. ---- */
|
||||
{
|
||||
XMLDocument doc;
|
||||
doc.LoadFile("dream.xml");
|
||||
|
||||
// Structure of the XML file:
|
||||
// - Element "PLAY" the root Element, which is the
|
||||
// FirstChildElement of the Document
|
||||
// - - Element "TITLE" child of the root PLAY Element
|
||||
// - - - Text child of the TITLE Element
|
||||
// Structure of the XML file:
|
||||
// - Element "PLAY" the root Element, which is the
|
||||
// FirstChildElement of the Document
|
||||
// - - Element "TITLE" child of the root PLAY Element
|
||||
// - - - Text child of the TITLE Element
|
||||
|
||||
// Navigate to the title, using the convenience function,
|
||||
// with a dangerous lack of error checking.
|
||||
const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
|
||||
printf( "Name of play (1): %s\n", title );
|
||||
// Navigate to the title, using the convenience function,
|
||||
// with a dangerous lack of error checking.
|
||||
const char* title = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->GetText();
|
||||
printf("Name of play (1): %s\n", title);
|
||||
|
||||
// Text is just another Node to TinyXML-2. The more
|
||||
// general way to get to the XMLText:
|
||||
XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
|
||||
title = textNode->Value();
|
||||
printf( "Name of play (2): %s\n", title );
|
||||
}
|
||||
// Text is just another Node to TinyXML-2. The more
|
||||
// general way to get to the XMLText:
|
||||
XMLText* textNode = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->FirstChild()->ToText();
|
||||
title = textNode->Value();
|
||||
printf("Name of play (2): %s\n", title);
|
||||
}
|
||||
```
|
||||
|
||||
Using and Installing
|
||||
--------------------
|
||||
## Using and Installing
|
||||
|
||||
There are 2 files in TinyXML-2:
|
||||
* tinyxml2.cpp
|
||||
* tinyxml2.h
|
||||
* `tinyxml2.cpp`
|
||||
* `tinyxml2.h`
|
||||
|
||||
And additionally a test file:
|
||||
* xmltest.cpp
|
||||
* `xmltest.cpp`
|
||||
|
||||
Generally speaking, the intent is that you simply include the tinyxml2.cpp and
|
||||
tinyxml2.h files in your project and build with your other source code.
|
||||
Generally speaking, the intent is that you simply include the `tinyxml2.cpp` and
|
||||
`tinyxml2.h` files in your project and build with your other source code.
|
||||
|
||||
There is also a CMake build included. CMake is the general build for TinyXML-2.
|
||||
|
||||
(Additional build systems are costly to maintain, and tend to bit-rot. They are
|
||||
(Additional build systems are costly to maintain, and tend to become outdated. They are
|
||||
being removed over time.)
|
||||
|
||||
Building TinyXML-2 - Using vcpkg
|
||||
--------------------------------
|
||||
### Building TinyXML-2 - Using vcpkg
|
||||
|
||||
You can download and install TinyXML-2 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
|
||||
|
||||
git clone https://github.com/Microsoft/vcpkg.git
|
||||
cd vcpkg
|
||||
./bootstrap-vcpkg.sh
|
||||
./vcpkg integrate install
|
||||
./vcpkg install tinyxml2
|
||||
```sh
|
||||
git clone https://github.com/Microsoft/vcpkg.git
|
||||
cd vcpkg
|
||||
./bootstrap-vcpkg.sh
|
||||
./vcpkg integrate install
|
||||
./vcpkg install tinyxml2
|
||||
```
|
||||
|
||||
The TinyXML-2 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
|
||||
|
||||
Versioning
|
||||
----------
|
||||
## Versioning
|
||||
|
||||
TinyXML-2 uses semantic versioning. http://semver.org/ Releases are now tagged in github.
|
||||
TinyXML-2 uses semantic versioning. http://semver.org/ Releases are now tagged in GitHub.
|
||||
|
||||
Note that the major version will (probably) change fairly rapidly. API changes are fairly
|
||||
common.
|
||||
|
||||
License
|
||||
-------
|
||||
## License
|
||||
|
||||
TinyXML-2 is released under the zlib license:
|
||||
|
||||
@@ -316,8 +332,7 @@ must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
|
||||
Contributors
|
||||
------------
|
||||
## Contributors
|
||||
|
||||
Thanks very much to everyone who sends suggestions, bugs, ideas, and
|
||||
encouragement. It all helps, and makes this project fun.
|
||||
|
||||
+68
-58
@@ -24,7 +24,7 @@ distribution.
|
||||
#include "tinyxml2.h"
|
||||
|
||||
#include <new> // yes, this one new style header, is in the Android SDK.
|
||||
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) || defined(__CC_ARM)
|
||||
#if defined(ANDROID_NDK)
|
||||
# include <stddef.h>
|
||||
# include <stdarg.h>
|
||||
#else
|
||||
@@ -32,15 +32,27 @@ distribution.
|
||||
# include <cstdarg>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
|
||||
// Microsoft Visual Studio, version 2005 and higher. Not WinCE.
|
||||
/*int _snprintf_s(
|
||||
char *buffer,
|
||||
size_t sizeOfBuffer,
|
||||
size_t count,
|
||||
const char *format [,
|
||||
argument] ...
|
||||
);*/
|
||||
// Handle fallthrough attribute for different compilers
|
||||
#ifndef __has_attribute
|
||||
# define __has_attribute(x) 0
|
||||
#endif
|
||||
#ifndef __has_cpp_attribute
|
||||
# define __has_cpp_attribute(x) 0
|
||||
#endif
|
||||
|
||||
#if (__cplusplus >= 201703L && __has_cpp_attribute(fallthrough))
|
||||
# define TIXML_FALLTHROUGH [[fallthrough]]
|
||||
#elif __has_cpp_attribute(clang::fallthrough)
|
||||
# define TIXML_FALLTHROUGH [[clang::fallthrough]]
|
||||
#elif __has_attribute(fallthrough)
|
||||
# define TIXML_FALLTHROUGH __attribute__((fallthrough))
|
||||
#else
|
||||
# define TIXML_FALLTHROUGH (void(0))
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||
// Microsoft Visual Studio, version 2005 and higher.
|
||||
static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
|
||||
{
|
||||
va_list va;
|
||||
@@ -59,33 +71,11 @@ distribution.
|
||||
#define TIXML_VSCPRINTF _vscprintf
|
||||
#define TIXML_SSCANF sscanf_s
|
||||
#elif defined _MSC_VER
|
||||
// Microsoft Visual Studio 2003 and earlier or WinCE
|
||||
// Microsoft Visual Studio 2003 and earlier.
|
||||
#define TIXML_SNPRINTF _snprintf
|
||||
#define TIXML_VSNPRINTF _vsnprintf
|
||||
#define TIXML_SSCANF sscanf
|
||||
#if (_MSC_VER < 1400 ) && (!defined WINCE)
|
||||
// Microsoft Visual Studio 2003 and not WinCE.
|
||||
#define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have.
|
||||
#else
|
||||
// Microsoft Visual Studio 2003 and earlier or WinCE.
|
||||
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
||||
{
|
||||
int len = 512;
|
||||
for (;;) {
|
||||
len = len*2;
|
||||
char* str = new char[len]();
|
||||
const int required = _vsnprintf(str, len, format, va);
|
||||
delete[] str;
|
||||
if ( required != -1 ) {
|
||||
TIXMLASSERT( required >= 0 );
|
||||
len = required;
|
||||
break;
|
||||
}
|
||||
}
|
||||
TIXMLASSERT( len >= 0 );
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
#define TIXML_VSCPRINTF _vscprintf
|
||||
#else
|
||||
// GCC version 3 and higher
|
||||
//#warning( "Using sn* functions." )
|
||||
@@ -93,6 +83,9 @@ distribution.
|
||||
#define TIXML_VSNPRINTF vsnprintf
|
||||
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
||||
{
|
||||
if (!format) {
|
||||
return 0;
|
||||
}
|
||||
int len = vsnprintf( 0, 0, format, va );
|
||||
TIXMLASSERT( len >= 0 );
|
||||
return len;
|
||||
@@ -100,7 +93,7 @@ distribution.
|
||||
#define TIXML_SSCANF sscanf
|
||||
#endif
|
||||
|
||||
#if defined(_WIN64)
|
||||
#if defined(_MSC_VER)
|
||||
#define TIXML_FSEEK _fseeki64
|
||||
#define TIXML_FTELL _ftelli64
|
||||
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
|
||||
@@ -446,17 +439,17 @@ void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length
|
||||
--output;
|
||||
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
|
||||
input >>= 6;
|
||||
//fall through
|
||||
TIXML_FALLTHROUGH;
|
||||
case 3:
|
||||
--output;
|
||||
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
|
||||
input >>= 6;
|
||||
//fall through
|
||||
TIXML_FALLTHROUGH;
|
||||
case 2:
|
||||
--output;
|
||||
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
|
||||
input >>= 6;
|
||||
//fall through
|
||||
TIXML_FALLTHROUGH;
|
||||
case 1:
|
||||
--output;
|
||||
*output = static_cast<char>(input | FIRST_BYTE_MARK[*length]);
|
||||
@@ -528,12 +521,15 @@ const char* XMLUtil::GetCharacterRef(const char* p, char* value, int* length)
|
||||
TIXMLASSERT(digit < radix);
|
||||
|
||||
const unsigned int digitScaled = mult * digit;
|
||||
// Reject before adding: if digitScaled alone exceeds MAX_CODE_POINT,
|
||||
// or if adding it to ucs would exceed it (checked without overflow by
|
||||
// testing ucs > MAX_CODE_POINT - digitScaled, safe since digitScaled
|
||||
// <= MAX_CODE_POINT at this point).
|
||||
if (digitScaled > MAX_CODE_POINT || ucs > MAX_CODE_POINT - digitScaled) {
|
||||
return 0;
|
||||
}
|
||||
ucs += digitScaled;
|
||||
mult *= radix;
|
||||
|
||||
// Security check: could a value exist that is out of range?
|
||||
// Easily; limit to the MAX_CODE_POINT, which also allows for a
|
||||
// bunch of leading zeroes.
|
||||
mult *= radix;
|
||||
if (mult > MAX_CODE_POINT) {
|
||||
mult = MAX_CODE_POINT;
|
||||
}
|
||||
@@ -545,11 +541,11 @@ const char* XMLUtil::GetCharacterRef(const char* p, char* value, int* length)
|
||||
}
|
||||
// convert the UCS to UTF-8
|
||||
ConvertUTF32ToUTF8(ucs, value, length);
|
||||
if (length == 0) {
|
||||
// If length is 0, there was an error. (Security? Bad input?)
|
||||
if (*length == 0) {
|
||||
// If *length is 0, ConvertUTF32ToUTF8 rejected the code point.
|
||||
// Fail safely.
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return p + delta + 1;
|
||||
}
|
||||
return p + 1;
|
||||
@@ -810,7 +806,17 @@ XMLNode::XMLNode( XMLDocument* doc ) :
|
||||
|
||||
XMLNode::~XMLNode()
|
||||
{
|
||||
DeleteChildren();
|
||||
// Fast path: this node is dying, so maintaining _firstChild/_lastChild and
|
||||
// sibling _prev/_next links is unnecessary. Only _parent must be zeroed to
|
||||
// satisfy the MarkInUse assertion inside DeleteNode.
|
||||
XMLNode *currentChild = _firstChild;
|
||||
while (currentChild != NULL) {
|
||||
XMLNode *next = currentChild->_next;
|
||||
currentChild->_parent = 0;
|
||||
DeleteNode(currentChild);
|
||||
currentChild = next;
|
||||
}
|
||||
|
||||
if ( _parent ) {
|
||||
_parent->Unlink( this );
|
||||
}
|
||||
@@ -2236,7 +2242,7 @@ void XMLDocument::Clear()
|
||||
delete [] _charBuffer;
|
||||
_charBuffer = 0;
|
||||
_parsingDepth = 0;
|
||||
|
||||
|
||||
#if 0
|
||||
_textPool.Trace( "text" );
|
||||
_elementPool.Trace( "element" );
|
||||
@@ -2311,7 +2317,7 @@ static FILE* callfopen( const char* filepath, const char* mode )
|
||||
{
|
||||
TIXMLASSERT( filepath );
|
||||
TIXMLASSERT( mode );
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||
FILE* fp = 0;
|
||||
const errno_t err = fopen_s( &fp, filepath, mode );
|
||||
if ( err ) {
|
||||
@@ -2323,9 +2329,11 @@ static FILE* callfopen( const char* filepath, const char* mode )
|
||||
return fp;
|
||||
}
|
||||
|
||||
void XMLDocument::DeleteNode( XMLNode* node ) {
|
||||
TIXMLASSERT( node );
|
||||
TIXMLASSERT(node->_document == this );
|
||||
void XMLDocument::DeleteNode( XMLNode* node ) {
|
||||
if(node == 0) {
|
||||
return; // check for null pointer
|
||||
}
|
||||
TIXMLASSERT(node->_document == this);
|
||||
if (node->_parent) {
|
||||
node->_parent->DeleteChild( node );
|
||||
}
|
||||
@@ -2577,7 +2585,7 @@ void XMLDocument::PopDepth()
|
||||
--_parsingDepth;
|
||||
}
|
||||
|
||||
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
|
||||
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth, EscapeAposCharsInAttributes aposInAttributes ) :
|
||||
_elementJustOpened( false ),
|
||||
_stack(),
|
||||
_firstElement( true ),
|
||||
@@ -2594,9 +2602,11 @@ XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
|
||||
}
|
||||
for( int i=0; i<NUM_ENTITIES; ++i ) {
|
||||
const char entityValue = entities[i].value;
|
||||
const unsigned char flagIndex = static_cast<unsigned char>(entityValue);
|
||||
TIXMLASSERT( flagIndex < ENTITY_RANGE );
|
||||
_entityFlag[flagIndex] = true;
|
||||
if ((aposInAttributes == ESCAPE_APOS_CHARS_IN_ATTRIBUTES) || (entityValue != SINGLE_QUOTE)) {
|
||||
const unsigned char flagIndex = static_cast<unsigned char>(entityValue);
|
||||
TIXMLASSERT( flagIndex < ENTITY_RANGE );
|
||||
_entityFlag[flagIndex] = true;
|
||||
}
|
||||
}
|
||||
_restrictedEntityFlag[static_cast<unsigned char>('&')] = true;
|
||||
_restrictedEntityFlag[static_cast<unsigned char>('<')] = true;
|
||||
@@ -2633,7 +2643,7 @@ void XMLPrinter::Write( const char* data, size_t size )
|
||||
fwrite ( data , sizeof(char), size, _fp);
|
||||
}
|
||||
else {
|
||||
char* p = _buffer.PushArr( static_cast<int>(size) ) - 1; // back up over the null terminator.
|
||||
char* p = _buffer.PushArr( size ) - 1; // back up over the null terminator.
|
||||
memcpy( p, data, size );
|
||||
p[size] = 0;
|
||||
}
|
||||
|
||||
+19
-18
@@ -24,15 +24,12 @@ distribution.
|
||||
#ifndef TINYXML2_INCLUDED
|
||||
#define TINYXML2_INCLUDED
|
||||
|
||||
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
|
||||
#if defined(ANDROID_NDK)
|
||||
# include <ctype.h>
|
||||
# include <limits.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# if defined(__PS3__)
|
||||
# include <stddef.h>
|
||||
# endif
|
||||
#else
|
||||
# include <cctype>
|
||||
# include <climits>
|
||||
@@ -42,14 +39,6 @@ distribution.
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
gcc:
|
||||
g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
|
||||
|
||||
Formatting, Artistic Style:
|
||||
AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
|
||||
*/
|
||||
|
||||
#if defined( _DEBUG ) || defined (__DEBUG__)
|
||||
# ifndef TINYXML2_DEBUG
|
||||
# define TINYXML2_DEBUG
|
||||
@@ -93,12 +82,20 @@ distribution.
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus) && __cplusplus >= 201703L
|
||||
#define TINYXML2_CONSTANT inline constexpr
|
||||
#elif defined(__cplusplus) && __cplusplus >= 201103L
|
||||
#define TINYXML2_CONSTANT static constexpr
|
||||
#else
|
||||
#define TINYXML2_CONSTANT static const
|
||||
#endif
|
||||
|
||||
/* Versioning, past 1.0.14:
|
||||
http://semver.org/
|
||||
*/
|
||||
static const int TIXML2_MAJOR_VERSION = 11;
|
||||
static const int TIXML2_MINOR_VERSION = 0;
|
||||
static const int TIXML2_PATCH_VERSION = 0;
|
||||
TINYXML2_CONSTANT int TIXML2_MAJOR_VERSION = 11;
|
||||
TINYXML2_CONSTANT int TIXML2_MINOR_VERSION = 0;
|
||||
TINYXML2_CONSTANT int TIXML2_PATCH_VERSION = 0;
|
||||
|
||||
#define TINYXML2_MAJOR_VERSION 11
|
||||
#define TINYXML2_MINOR_VERSION 0
|
||||
@@ -109,7 +106,7 @@ static const int TIXML2_PATCH_VERSION = 0;
|
||||
// system, and the capacity of the stack. On the other hand, it's a trivial
|
||||
// attack that can result from ill, malicious, or even correctly formed XML,
|
||||
// so there needs to be a limit in place.
|
||||
static const int TINYXML2_MAX_ELEMENT_DEPTH = 500;
|
||||
TINYXML2_CONSTANT int TINYXML2_MAX_ELEMENT_DEPTH = 500;
|
||||
|
||||
namespace tinyxml2
|
||||
{
|
||||
@@ -266,7 +263,6 @@ public:
|
||||
}
|
||||
|
||||
size_t Size() const {
|
||||
TIXMLASSERT( _size >= 0 );
|
||||
return _size;
|
||||
}
|
||||
|
||||
@@ -2240,13 +2236,18 @@ private:
|
||||
class TINYXML2_LIB XMLPrinter : public XMLVisitor
|
||||
{
|
||||
public:
|
||||
enum EscapeAposCharsInAttributes {
|
||||
ESCAPE_APOS_CHARS_IN_ATTRIBUTES,
|
||||
DONT_ESCAPE_APOS_CHARS_IN_ATTRIBUTES
|
||||
};
|
||||
|
||||
/** Construct the printer. If the FILE* is specified,
|
||||
this will print to the FILE. Else it will print
|
||||
to memory, and the result is available in CStr().
|
||||
If 'compact' is set to true, then output is created
|
||||
with only required whitespace and newlines.
|
||||
*/
|
||||
XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
|
||||
XMLPrinter( FILE* file=0, bool compact = false, int depth = 0, EscapeAposCharsInAttributes aposInAttributes = ESCAPE_APOS_CHARS_IN_ATTRIBUTES );
|
||||
virtual ~XMLPrinter() {}
|
||||
|
||||
/** If streaming, write the BOM and declaration. */
|
||||
|
||||
+121
-28
@@ -45,13 +45,15 @@ bool XMLTest (const char* testString, const char* expected, const char* found, b
|
||||
printf (" %s\n", testString);
|
||||
}
|
||||
else {
|
||||
const char* expectedStr = expected ? expected : "(null)";
|
||||
const char* foundStr = found ? found : "(null)";
|
||||
if ( extraNL ) {
|
||||
printf( " %s\n", testString );
|
||||
printf( "%s\n", expected );
|
||||
printf( "%s\n", found );
|
||||
printf( "%s\n", expectedStr );
|
||||
printf( "%s\n", foundStr );
|
||||
}
|
||||
else {
|
||||
printf (" %s [%s][%s]\n", testString, expected, found);
|
||||
printf (" %s [%s][%s]\n", testString, expectedStr, foundStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,9 +326,9 @@ int main( int argc, const char ** argv )
|
||||
|
||||
printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
|
||||
if ( !errorID ) {
|
||||
printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
|
||||
printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
|
||||
printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
|
||||
printf( "Load time=%u\n", static_cast<unsigned>(loadTime - startTime) );
|
||||
printf( "Delete time=%u\n", static_cast<unsigned>(deleteTime - loadTime) );
|
||||
printf( "Total time=%u\n", static_cast<unsigned>(deleteTime - startTime) );
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
@@ -595,8 +597,8 @@ int main( int argc, const char ** argv )
|
||||
|
||||
result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
||||
XMLTest( "Query attribute: int as double", XML_SUCCESS, result);
|
||||
XMLTest( "Query attribute: int as double", 1, (int)dVal );
|
||||
XMLTest( "Query attribute: int as double", 1, (int)ele->DoubleAttribute("attr0"));
|
||||
XMLTest( "Query attribute: int as double", 1, static_cast<int>(dVal) );
|
||||
XMLTest( "Query attribute: int as double", 1, static_cast<int>(ele->DoubleAttribute("attr0")));
|
||||
|
||||
result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
||||
XMLTest( "Query attribute: double as double", XML_SUCCESS, result);
|
||||
@@ -648,17 +650,17 @@ int main( int argc, const char ** argv )
|
||||
|
||||
{
|
||||
XMLError queryResult = ele->QueryAttribute( "int", &iVal2 );
|
||||
XMLTest( "Query int attribute generic", (int)XML_SUCCESS, queryResult);
|
||||
XMLTest( "Query int attribute generic", static_cast<int>(XML_SUCCESS), queryResult);
|
||||
}
|
||||
{
|
||||
XMLError queryResult = ele->QueryAttribute( "double", &dVal2 );
|
||||
XMLTest( "Query double attribute generic", (int)XML_SUCCESS, queryResult);
|
||||
XMLTest( "Query double attribute generic", static_cast<int>(XML_SUCCESS), queryResult);
|
||||
}
|
||||
|
||||
XMLTest( "Attribute match test", "strValue", ele->Attribute( "str", "strValue" ) );
|
||||
XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
||||
XMLTest( "Attribute round trip. int.", 1, iVal );
|
||||
XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
||||
XMLTest( "Attribute round trip. double.", -1, static_cast<int>(dVal) );
|
||||
XMLTest( "Alternate query", true, iVal == iVal2 );
|
||||
XMLTest( "Alternate query", true, dVal == dVal2 );
|
||||
XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") );
|
||||
@@ -675,7 +677,7 @@ int main( int argc, const char ** argv )
|
||||
const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
||||
0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
||||
|
||||
XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
||||
XMLTest( "UTF-8: Russian value.", reinterpret_cast<const char*>(correctValue), element->Attribute( "value" ) );
|
||||
|
||||
const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
||||
0xd1U, 0x81U, 0xd1U, 0x81U,
|
||||
@@ -683,7 +685,7 @@ int main( int argc, const char ** argv )
|
||||
0xd0U, 0xb9U, 0 };
|
||||
const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
||||
|
||||
XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
||||
XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( reinterpret_cast<const char*>(russianElementName) )->FirstChild()->ToText();
|
||||
XMLTest( "UTF-8: Browsing russian element name.",
|
||||
russianText,
|
||||
text->Value() );
|
||||
@@ -824,7 +826,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
int v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: int", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: int", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: int", -100, v, true);
|
||||
}
|
||||
XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true);
|
||||
@@ -840,7 +842,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
unsigned v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: unsigned", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: unsigned", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: unsigned", unsigned(100), v, true);
|
||||
}
|
||||
{
|
||||
@@ -864,7 +866,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
int64_t v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: int64_t", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: int64_t", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: int64_t", BIG, v, true);
|
||||
}
|
||||
XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true);
|
||||
@@ -880,7 +882,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
uint64_t v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: uint64_t", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: uint64_t", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: uint64_t", BIG_POS, v, true);
|
||||
}
|
||||
XMLTest("Attribute: uint64_t", BIG_POS, element->Unsigned64Attribute("attrib"), true);
|
||||
@@ -896,7 +898,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
bool v = false;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: bool", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: bool", true, v, true);
|
||||
}
|
||||
XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
|
||||
@@ -924,7 +926,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
double v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: bool", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: double", 100.0, v, true);
|
||||
}
|
||||
XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true);
|
||||
@@ -940,7 +942,7 @@ int main( int argc, const char ** argv )
|
||||
{
|
||||
float v = 0;
|
||||
XMLError queryResult = element->QueryAttribute("attrib", &v);
|
||||
XMLTest("Attribute: float", (int)XML_SUCCESS, queryResult, true);
|
||||
XMLTest("Attribute: float", static_cast<int>(XML_SUCCESS), queryResult, true);
|
||||
XMLTest("Attribute: float", 100.0f, v, true);
|
||||
}
|
||||
XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true);
|
||||
@@ -1068,15 +1070,15 @@ int main( int argc, const char ** argv )
|
||||
|
||||
unsigned unsigned_value;
|
||||
unsigned_value = root->FirstChildElement("unsigned")->UnsignedText();
|
||||
XMLTest("PushText( unsigned value ) test", (unsigned)12, unsigned_value);
|
||||
XMLTest("PushText( unsigned value ) test", static_cast<unsigned>(12), unsigned_value);
|
||||
|
||||
int64_t int64_t_value;
|
||||
int64_t_value = root->FirstChildElement("int64_t")->Int64Text();
|
||||
XMLTest("PushText( int64_t value ) test", (int64_t) 13, int64_t_value);
|
||||
XMLTest("PushText( int64_t value ) test", static_cast<int64_t>(13), int64_t_value);
|
||||
|
||||
uint64_t uint64_t_value;
|
||||
uint64_t_value = root->FirstChildElement("uint64_t")->Unsigned64Text();
|
||||
XMLTest("PushText( uint64_t value ) test", (uint64_t) 14, uint64_t_value);
|
||||
XMLTest("PushText( uint64_t value ) test", static_cast<uint64_t>(14), uint64_t_value);
|
||||
|
||||
float float_value;
|
||||
float_value = root->FirstChildElement("float")->FloatText();
|
||||
@@ -1397,7 +1399,7 @@ int main( int argc, const char ** argv )
|
||||
buf[61] = 0;
|
||||
|
||||
XMLDocument doc;
|
||||
doc.Parse( (const char*)buf);
|
||||
doc.Parse( reinterpret_cast<const char*>(buf) );
|
||||
XMLTest( "Broken CDATA", true, doc.Error() );
|
||||
}
|
||||
|
||||
@@ -1753,7 +1755,7 @@ int main( int argc, const char ** argv )
|
||||
unsigned unsignedValue = 0;
|
||||
XMLError queryResult = pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
|
||||
XMLTest( "QueryUnsignedText result", XML_SUCCESS, queryResult, false );
|
||||
XMLTest( "QueryUnsignedText", (unsigned)1, unsignedValue, false );
|
||||
XMLTest( "QueryUnsignedText", static_cast<unsigned>(1), unsignedValue, false );
|
||||
}
|
||||
|
||||
{
|
||||
@@ -2025,7 +2027,13 @@ int main( int argc, const char ** argv )
|
||||
XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
|
||||
XMLTest("Pedantic whitespace", true, 0 == doc.RootElement()->FirstChildElement()->GetText());
|
||||
}
|
||||
|
||||
//Check the robustness of the DeleteNode function in handling null pointers.
|
||||
{
|
||||
XMLDocument doc;
|
||||
doc.DeleteNode(nullptr);
|
||||
XMLTest("DeleteNode with null pointer", true, doc.Error() == XML_SUCCESS);
|
||||
}
|
||||
|
||||
// Check sample xml can be parsed with pedantic mode
|
||||
{
|
||||
XMLDocument doc(true, PEDANTIC_WHITESPACE);
|
||||
@@ -2695,6 +2703,91 @@ int main( int argc, const char ** argv )
|
||||
XMLTest("Test attribute encode with a Hex value", value5, "!"); // hex value in unicode value
|
||||
}
|
||||
|
||||
// ---------- Security: numeric character reference bounds ----------
|
||||
{
|
||||
// Regression: U+10FFFF is the last valid Unicode code point and must
|
||||
// parse correctly. The in-loop overflow guard must not reject it.
|
||||
XMLDocument doc;
|
||||
doc.Parse( "<t v=''/>" );
|
||||
XMLTest( "Numeric ref U+10FFFF: no error", false, doc.Error() );
|
||||
const char* v = doc.FirstChildElement()->Attribute( "v" );
|
||||
// U+10FFFF encodes to the 4-byte UTF-8 sequence F4 8F BF BF.
|
||||
const char expected[] = {
|
||||
static_cast<char>(0xF4), static_cast<char>(0x8F),
|
||||
static_cast<char>(0xBF), static_cast<char>(0xBF), 0
|
||||
};
|
||||
XMLTest( "Numeric ref U+10FFFF: correct UTF-8 output", expected, v );
|
||||
}
|
||||
{
|
||||
// Boundary check: U+110000 is one above the maximum code point.
|
||||
// The in-loop overflow guard must catch this before ucs is written,
|
||||
// leaving the entity as a literal (starting with '&').
|
||||
XMLDocument doc;
|
||||
doc.Parse( "<t v='�'/>" );
|
||||
XMLTest( "Numeric ref U+110000: no parse error", false, doc.Error() );
|
||||
const char* v = doc.FirstChildElement()->Attribute( "v" );
|
||||
XMLTest( "Numeric ref U+110000: not resolved (left as literal)", true,
|
||||
v != nullptr && v[0] == '&' );
|
||||
}
|
||||
{
|
||||
// A hex entity with enough digits to overflow uint32_t must
|
||||
// be rejected by the in-loop guard before the accumulator wraps.
|
||||
// Before the fix, ucs could wrap around and pass the post-loop range
|
||||
// check, producing an attacker-chosen character in the parsed output.
|
||||
// Build "&#x" + 300 'F' digits + ";" -- far beyond what fits in uint32_t.
|
||||
const char prefix[] = "<t v='&#x";
|
||||
const char suffix[] = ";'/>";
|
||||
static const int NDIGITS = 300;
|
||||
char xml[sizeof(prefix) + NDIGITS + sizeof(suffix)];
|
||||
strcpy( xml, prefix );
|
||||
memset( xml + strlen(prefix), 'F', NDIGITS );
|
||||
strcpy( xml + strlen(prefix) + NDIGITS, suffix );
|
||||
|
||||
XMLDocument doc;
|
||||
doc.Parse( xml );
|
||||
XMLTest( "Overflow hex entity: no parse error", false, doc.Error() );
|
||||
const char* v = doc.FirstChildElement()->Attribute( "v" );
|
||||
// GetCharacterRef returns 0 for rejected refs; the caller then copies
|
||||
// the literal '&', so the attribute must start with '&', not a char.
|
||||
XMLTest( "Overflow hex entity: not resolved to a character", true,
|
||||
v != nullptr && v[0] == '&' );
|
||||
}
|
||||
|
||||
// ---------- XMLPrinter Apos Escaping ------
|
||||
{
|
||||
const char* testText = "text containing a ' character";
|
||||
XMLDocument doc;
|
||||
XMLElement* element = doc.NewElement( "element" );
|
||||
doc.InsertEndChild( element );
|
||||
element->SetAttribute( "attrib", testText );
|
||||
{
|
||||
XMLPrinter defaultPrinter;
|
||||
doc.Print( &defaultPrinter );
|
||||
const char* defaultOutput = defaultPrinter.CStr();
|
||||
const bool foundTextWithUnescapedApos = (strstr(defaultOutput, testText) != nullptr);
|
||||
XMLTest("Default XMLPrinter should escape ' characters", false, foundTextWithUnescapedApos);
|
||||
{
|
||||
XMLDocument parsingDoc;
|
||||
parsingDoc.Parse(defaultOutput);
|
||||
const XMLAttribute* attrib = parsingDoc.FirstChildElement("element")->FindAttribute("attrib");
|
||||
XMLTest("Default XMLPrinter should output parsable xml", testText, attrib->Value(), true);
|
||||
}
|
||||
}
|
||||
{
|
||||
XMLPrinter customPrinter(0, false, 0, XMLPrinter::DONT_ESCAPE_APOS_CHARS_IN_ATTRIBUTES);
|
||||
doc.Print( &customPrinter );
|
||||
const char* customOutput = customPrinter.CStr();
|
||||
const bool foundTextWithUnescapedApos = (strstr(customOutput, testText) != nullptr);
|
||||
XMLTest("Custom XMLPrinter should not escape ' characters", true, foundTextWithUnescapedApos);
|
||||
{
|
||||
XMLDocument parsingDoc;
|
||||
parsingDoc.Parse(customOutput);
|
||||
const XMLAttribute* attrib = parsingDoc.FirstChildElement("element")->FindAttribute("attrib");
|
||||
XMLTest("Custom XMLPrinter should output parsable xml", testText, attrib->Value(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------- Performance tracking --------------
|
||||
{
|
||||
#if defined( _MSC_VER )
|
||||
@@ -2744,9 +2837,9 @@ int main( int argc, const char ** argv )
|
||||
#endif
|
||||
|
||||
#if defined( _MSC_VER )
|
||||
const double duration = 1000.0 * (double)(end - start) / ((double)freq * (double)COUNT);
|
||||
const double duration = 1000.0 * static_cast<double>(end - start) / (static_cast<double>(freq) * static_cast<double>(COUNT));
|
||||
#else
|
||||
const double duration = (double)(cend - cstart) / (double)COUNT;
|
||||
const double duration = static_cast<double>(cend - cstart) / static_cast<double>(COUNT);
|
||||
#endif
|
||||
printf("\nParsing dream.xml (%s): %.3f milli-seconds\n", note, duration);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user