1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
// Note: swapping these two includes around causes MSVC to error out
// while in /permissive- mode. No, I don't know why.
// Tested on VS 2019, 18.{3, 4}.x
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_xmlwriter.hpp>
#include <cstdint>
#include <iomanip>
#include <type_traits>
namespace Catch {
namespace {
size_t trailingBytes(unsigned char c) {
if ((c & 0xE0) == 0xC0) {
return 2;
}
if ((c & 0xF0) == 0xE0) {
return 3;
}
if ((c & 0xF8) == 0xF0) {
return 4;
}
CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered");
}
uint32_t headerValue(unsigned char c) {
if ((c & 0xE0) == 0xC0) {
return c & 0x1F;
}
if ((c & 0xF0) == 0xE0) {
return c & 0x0F;
}
if ((c & 0xF8) == 0xF0) {
return c & 0x07;
}
CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered");
}
void hexEscapeChar(std::ostream& os, unsigned char c) {
std::ios_base::fmtflags f(os.flags());
os << "\\x"
<< std::uppercase << std::hex << std::setfill('0') << std::setw(2)
<< static_cast<int>(c);
os.flags(f);
}
bool shouldNewline(XmlFormatting fmt) {
return !!(static_cast<std::underlying_type_t<XmlFormatting>>(fmt & XmlFormatting::Newline));
}
bool shouldIndent(XmlFormatting fmt) {
return !!(static_cast<std::underlying_type_t<XmlFormatting>>(fmt & XmlFormatting::Indent));
}
} // anonymous namespace
XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs) {
return static_cast<XmlFormatting>(
static_cast<std::underlying_type_t<XmlFormatting>>(lhs) |
static_cast<std::underlying_type_t<XmlFormatting>>(rhs)
);
}
XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs) {
return static_cast<XmlFormatting>(
static_cast<std::underlying_type_t<XmlFormatting>>(lhs) &
static_cast<std::underlying_type_t<XmlFormatting>>(rhs)
);
}
XmlEncode::XmlEncode( StringRef str, ForWhat forWhat )
: m_str( str ),
m_forWhat( forWhat )
{}
void XmlEncode::encodeTo( std::ostream& os ) const {
// Apostrophe escaping not necessary if we always use " to write attributes
// (see: http://www.w3.org/TR/xml/#syntax)
for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) {
unsigned char c = static_cast<unsigned char>(m_str[idx]);
switch (c) {
case '<': os << "<"; break;
case '&': os << "&"; break;
case '>':
// See: http://www.w3.org/TR/xml/#syntax
if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']')
os << ">";
else
os << c;
break;
case '\"':
if (m_forWhat == ForAttributes)
os << """;
else
os << c;
break;
default:
// Check for control characters and invalid utf-8
// Escape control characters in standard ascii
// see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) {
hexEscapeChar(os, c);
break;
}
// Plain ASCII: Write it to stream
if (c < 0x7F) {
os << c;
break;
}
// UTF-8 territory
// Check if the encoding is valid and if it is not, hex escape bytes.
// Important: We do not check the exact decoded values for validity, only the encoding format
// First check that this bytes is a valid lead byte:
// This means that it is not encoded as 1111 1XXX
// Or as 10XX XXXX
if (c < 0xC0 ||
c >= 0xF8) {
hexEscapeChar(os, c);
break;
}
auto encBytes = trailingBytes(c);
// Are there enough bytes left to avoid accessing out-of-bounds memory?
if (idx + encBytes - 1 >= m_str.size()) {
hexEscapeChar(os, c);
break;
}
// The header is valid, check data
// The next encBytes bytes must together be a valid utf-8
// This means: bitpattern 10XX XXXX and the extracted value is sane (ish)
bool valid = true;
uint32_t value = headerValue(c);
for (std::size_t n = 1; n < encBytes; ++n) {
unsigned char nc = static_cast<unsigned char>(m_str[idx + n]);
valid &= ((nc & 0xC0) == 0x80);
value = (value << 6) | (nc & 0x3F);
}
if (
// Wrong bit pattern of following bytes
(!valid) ||
// Overlong encodings
(value < 0x80) ||
(0x80 <= value && value < 0x800 && encBytes > 2) ||
(0x800 < value && value < 0x10000 && encBytes > 3) ||
// Encoded value out of range
(value >= 0x110000)
) {
hexEscapeChar(os, c);
break;
}
// If we got here, this is in fact a valid(ish) utf-8 sequence
for (std::size_t n = 0; n < encBytes; ++n) {
os << m_str[idx + n];
}
idx += encBytes - 1;
break;
}
}
}
std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) {
xmlEncode.encodeTo( os );
return os;
}
XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer, XmlFormatting fmt )
: m_writer( writer ),
m_fmt(fmt)
{}
XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept
: m_writer( other.m_writer ),
m_fmt(other.m_fmt)
{
other.m_writer = nullptr;
other.m_fmt = XmlFormatting::None;
}
XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept {
if ( m_writer ) {
m_writer->endElement();
}
m_writer = other.m_writer;
other.m_writer = nullptr;
m_fmt = other.m_fmt;
other.m_fmt = XmlFormatting::None;
return *this;
}
XmlWriter::ScopedElement::~ScopedElement() {
if (m_writer) {
m_writer->endElement(m_fmt);
}
}
XmlWriter::ScopedElement&
XmlWriter::ScopedElement::writeText( StringRef text, XmlFormatting fmt ) {
m_writer->writeText( text, fmt );
return *this;
}
XmlWriter::ScopedElement&
XmlWriter::ScopedElement::writeAttribute( StringRef name,
StringRef attribute ) {
m_writer->writeAttribute( name, attribute );
return *this;
}
XmlWriter::XmlWriter( std::ostream& os ) : m_os( os )
{
writeDeclaration();
}
XmlWriter::~XmlWriter() {
while (!m_tags.empty()) {
endElement();
}
newlineIfNecessary();
}
XmlWriter& XmlWriter::startElement( std::string const& name, XmlFormatting fmt ) {
ensureTagClosed();
newlineIfNecessary();
if (shouldIndent(fmt)) {
m_os << m_indent;
m_indent += " ";
}
m_os << '<' << name;
m_tags.push_back( name );
m_tagIsOpen = true;
applyFormatting(fmt);
return *this;
}
XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name, XmlFormatting fmt ) {
ScopedElement scoped( this, fmt );
startElement( name, fmt );
return scoped;
}
XmlWriter& XmlWriter::endElement(XmlFormatting fmt) {
m_indent = m_indent.substr(0, m_indent.size() - 2);
if( m_tagIsOpen ) {
m_os << "/>";
m_tagIsOpen = false;
} else {
newlineIfNecessary();
if (shouldIndent(fmt)) {
m_os << m_indent;
}
m_os << "</" << m_tags.back() << '>';
}
m_os << std::flush;
applyFormatting(fmt);
m_tags.pop_back();
return *this;
}
XmlWriter& XmlWriter::writeAttribute( StringRef name,
StringRef attribute ) {
if( !name.empty() && !attribute.empty() )
m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"';
return *this;
}
XmlWriter& XmlWriter::writeAttribute( StringRef name, bool attribute ) {
writeAttribute(name, (attribute ? "true"_sr : "false"_sr));
return *this;
}
XmlWriter& XmlWriter::writeAttribute( StringRef name,
char const* attribute ) {
writeAttribute( name, StringRef( attribute ) );
return *this;
}
XmlWriter& XmlWriter::writeText( StringRef text, XmlFormatting fmt ) {
CATCH_ENFORCE(!m_tags.empty(), "Cannot write text as top level element");
if( !text.empty() ){
bool tagWasOpen = m_tagIsOpen;
ensureTagClosed();
if (tagWasOpen && shouldIndent(fmt)) {
m_os << m_indent;
}
m_os << XmlEncode( text, XmlEncode::ForTextNodes );
applyFormatting(fmt);
}
return *this;
}
XmlWriter& XmlWriter::writeComment( StringRef text, XmlFormatting fmt ) {
ensureTagClosed();
if (shouldIndent(fmt)) {
m_os << m_indent;
}
m_os << "<!-- " << text << " -->";
applyFormatting(fmt);
return *this;
}
void XmlWriter::writeStylesheetRef( StringRef url ) {
m_os << R"(<?xml-stylesheet type="text/xsl" href=")" << url << R"("?>)" << '\n';
}
void XmlWriter::ensureTagClosed() {
if( m_tagIsOpen ) {
m_os << '>' << std::flush;
newlineIfNecessary();
m_tagIsOpen = false;
}
}
void XmlWriter::applyFormatting(XmlFormatting fmt) {
m_needsNewline = shouldNewline(fmt);
}
void XmlWriter::writeDeclaration() {
m_os << R"(<?xml version="1.0" encoding="UTF-8"?>)" << '\n';
}
void XmlWriter::newlineIfNecessary() {
if( m_needsNewline ) {
m_os << '\n' << std::flush;
m_needsNewline = false;
}
}
}
|