Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qxmlstream_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
5//
6// W A R N I N G
7// -------------
8//
9// This file is not part of the Qt API. It exists for the convenience
10// of other Qt classes. This header file may change from version to
11// version without notice, or even be removed.
12//
13// We mean it.
14//
15
16#include <QtCore/private/qglobal_p.h>
17#include <qstringconverter.h>
18#include <qxmlstream.h>
20#include <QtCore/qhash.h>
21#include <QCoreApplication> // Q_DECLARE_TR_FUNCTIONS
22#include <QtCore/private/qtools_p.h>
23
24
25#include <memory>
26#include <optional>
27
28#ifndef QXMLSTREAM_P_H
29#define QXMLSTREAM_P_H
30
31QT_BEGIN_NAMESPACE
32
33class QByteArrayView;
34
35namespace QtPrivate {
36
38{
39public:
40 const QString *m_string = nullptr;
43
44 constexpr XmlStringRef() = default;
45 constexpr inline XmlStringRef(const QString *string, qsizetype pos, qsizetype length)
47 {
48 }
49 XmlStringRef(const QString *string)
50 : XmlStringRef(string, 0, string->size())
51 {
52 }
53
55 if (!m_string)
56 return QXmlString();
58 d.setBegin(d.data() + m_pos);
59 d.size = m_size;
60 return QXmlString(std::move(d));
61 }
62
63 void clear() { m_string = nullptr; m_pos = 0; m_size= 0; }
65 bool isEmpty() const { return m_size == 0; }
66 bool isNull() const { return !m_string; }
67 QString toString() const { return view().toString(); }
68
70 {
71 verify(pos, 0);
73 }
75 {
76 verify(pos, n);
77 return XmlStringRef(m_string, m_pos + pos, n);
78 }
79
91
92#define MAKE_MEMBER(name)
93 auto name () const noexcept { return view(). name (); }
94 MAKE_MEMBER(data)
95 MAKE_MEMBER(size)
96 MAKE_MEMBER(empty)
97 MAKE_MEMBER(begin)
98 MAKE_MEMBER(end)
99 MAKE_MEMBER(cbegin)
100 MAKE_MEMBER(cend)
101 MAKE_MEMBER(rbegin)
102 MAKE_MEMBER(rend)
103 MAKE_MEMBER(crbegin)
104 MAKE_MEMBER(crend)
105#undef MAKE_MEMBER
106
107#define MAKE_OP(op)
108 friend auto operator op(const XmlStringRef &lhs, const XmlStringRef &rhs) noexcept { return lhs.view() op rhs.view(); }
109 /*end*/
110 MAKE_OP(==)
111 MAKE_OP(!=)
112 MAKE_OP(<=)
113 MAKE_OP(>=)
114 MAKE_OP(<)
115 MAKE_OP(>)
116#undef MAKE_OP
117#define MAKE_OP(op)
118 friend auto operator op(const XmlStringRef &lhs, QStringView rhs) noexcept { return lhs.view() op rhs; }
119 friend auto operator op(QStringView lhs, const XmlStringRef &rhs) noexcept { return lhs op rhs.view(); }
120 /*end*/
121 MAKE_OP(==)
122 MAKE_OP(!=)
123 MAKE_OP(<=)
124 MAKE_OP(>=)
125 MAKE_OP(<)
126 MAKE_OP(>)
127#undef MAKE_OP
128
129private:
130 Q_ALWAYS_INLINE
131 void verify([[maybe_unused]] qsizetype pos = 0,
132 [[maybe_unused]] qsizetype n = 1) const
133 {
134 Q_PRE(pos >= 0);
135 Q_PRE(pos <= size());
136 Q_PRE(n >= 0);
137 Q_PRE(n <= size() - pos);
138 }
139};
140
141}
142
143template <typename T> class QXmlStreamSimpleStack
144{
146 static_assert(sizeof(T) > 1, "some operations may overflow with such small types");
147
148 T *data;
149 qsizetype tos, cap; // class invariants: cap ≥ 0, tos < cap
150public:
152 : data(nullptr), tos(-1), cap(0)
153 {}
155 {
156 if (data) {
157 std::destroy_n(data, size());
158 free(data);
159 }
160 }
161
162 inline void reserve(qsizetype extraCapacity)
163 {
164 auto needed = extraCapacity;
165 if (add_overflow(needed, tos + 1, &needed)) // tos + 1 is <= cap, so doesn't overflow!
166 qBadAlloc();
167 if (needed > cap) {
168 // The following multiplication cannot overflow or we would have
169 // allocated at least half the address space successfully before
170 // (for sizeof(T) > 1, which we assert):
171 needed = (std::max)(needed, cap * 2);
172 void *ptr = qt_reallocarray(static_cast<void *>(data), needed, sizeof(T));
173 Q_CHECK_PTR(ptr);
174 data = reinterpret_cast<T *>(ptr);
175 cap = needed;
176 }
177 }
178
179 inline T &push() { reserve(1); return rawPush(); }
180 inline T &rawPush() { return *new (data + (++tos)) T; }
181 inline const T &top() const { return data[tos]; }
182 inline T &top() { return data[tos]; }
183 inline T pop() { T t = std::move(data[tos]); std::destroy_at(data + tos); --tos; return t; }
184 inline T &operator[](qsizetype index) { return data[index]; }
185 inline const T &at(qsizetype index) const { return data[index]; }
186 inline qsizetype size() const { return tos + 1; }
187 inline void resize(qsizetype s) { tos = s - 1; }
188 inline bool isEmpty() const { return tos < 0; }
189 inline void clear() { tos = -1; }
190
191 using const_iterator = const T*;
192 using iterator = T*;
193 T *begin() { return data; }
194 const T *begin() const { return data; }
195 const T *cbegin() const { return begin(); }
196 T *end() { return data + size(); }
197 const T *end() const { return data + size(); }
198 const T *cend() const { return end(); }
199};
200
205
209public:
215
224
225
232
234 {
235 qsizetype pos = tagStackStringStorageSize;
236 if (pos != tagStackStringStorage.size())
237 tagStackStringStorage.resize(pos);
238 s.visit([&](auto s) { tagStackStringStorage.append(s); });
239 qsizetype sz = (tagStackStringStorage.size() - pos);
240 tagStackStringStorageSize += sz;
241 return XmlStringRef(&tagStackStringStorage, pos, sz);
242 }
243
245
246
247 inline Tag tagStack_pop() {
248 Tag tag = tagStack.pop();
249 tagStackStringStorageSize = tag.tagStackStringStorageSize;
250 namespaceDeclarations.resize(tag.namespaceDeclarationsSize);
251 tagsDone = tagStack.isEmpty();
252 return tag;
253 }
254 inline Tag &tagStack_push() {
255 Tag &tag = tagStack.push();
256 tag.tagStackStringStorageSize = tagStackStringStorageSize;
257 tag.namespaceDeclarationsSize = namespaceDeclarations.size();
258 return tag;
259 }
260};
261
262#if QT_CONFIG(xmlstreamreader)
263class QXmlStreamEntityResolver;
264class QXmlStreamReaderPrivate : public QXmlStreamGrammar, public QXmlStreamPrivateTagStack
265{
266 QXmlStreamReader *q_ptr;
267 Q_DECLARE_PUBLIC(QXmlStreamReader)
268 using XmlStringRef = QtPrivate::XmlStringRef;
269public:
270 QXmlStreamReaderPrivate(QXmlStreamReader *q);
271 ~QXmlStreamReaderPrivate();
272 void init();
273
274 struct BufferAndEncoding
275 {
276 QByteArray buffer;
277 // System means that we didn't specify the encoding
278 QStringDecoder::Encoding encoding;
279
280 BufferAndEncoding()
281 : buffer(), encoding(QStringDecoder::System)
282 {}
283 BufferAndEncoding(const QByteArray &b, QStringDecoder::Encoding e)
284 : buffer(b), encoding(e)
285 {}
286 };
287 QList<BufferAndEncoding> dataInfo;
288 QStringDecoder chunkDecoder;
289 void appendDataWithEncoding(const QByteArray &data, QStringDecoder::Encoding enc);
290 bool tryAppendViewWithEncoding(QByteArrayView data, QStringDecoder::Encoding enc);
291 void appendViewWithEncoding(QByteArrayView data, QStringDecoder::Encoding enc);
292
293 QByteArray rawReadBuffer;
294 qint64 nbytesread;
295 QString readBuffer;
296 qsizetype readBufferPos;
297 QXmlStreamSimpleStack<uint> putStack;
298 struct Entity {
299 Entity() = default;
300 Entity(const QString &name, const QString &value)
301 : name(name), value(value), external(false), unparsed(false), literal(false),
302 hasBeenParsed(false), isCurrentlyReferenced(false){}
303 static inline Entity createLiteral(QLatin1StringView name, QLatin1StringView value)
304 { Entity result(name, value); result.literal = result.hasBeenParsed = true; return result; }
305 QString name, value;
306 uint external : 1;
307 uint unparsed : 1;
308 uint literal : 1;
309 uint hasBeenParsed : 1;
310 uint isCurrentlyReferenced : 1;
311 };
312 // these hash tables use a QStringView as a key to avoid creating QStrings
313 // just for lookup. The keys are usually views into Entity::name and thus
314 // are guaranteed to have the same lifetime as the referenced data:
315 QHash<QStringView, Entity> entityHash;
316 QHash<QStringView, Entity> parameterEntityHash;
317 struct QEntityReference
318 {
319 QHash<QStringView, Entity> *hash;
320 QStringView name;
321 };
322 QXmlStreamSimpleStack<QEntityReference> entityReferenceStack;
323 int entityExpansionLimit = 4096;
324 qsizetype entityLength = 0;
325 inline bool referenceEntity(QHash<QStringView, Entity> *hash, Entity &entity)
326 {
327 Q_ASSERT(hash);
328 if (entity.isCurrentlyReferenced) {
329 raiseWellFormedError(QXmlStream::tr("Self-referencing entity detected."));
330 return false;
331 }
332 const auto valLen = entity.value.size();
333 const auto refLen = entity.name.size() + 2; // 2 for `&` and `;`
334 // don't count the top-level reference (could turn the entityLength negative):
335 const auto effectiveRefLen = entityReferenceStack.isEmpty() ? 0 : refLen;
336 entityLength += valLen - effectiveRefLen;
337 if (entityLength > entityExpansionLimit) {
338 raiseWellFormedError(QXmlStream::tr("Entity expands to more characters than the entity expansion limit."));
339 return false;
340 }
341 entity.isCurrentlyReferenced = true;
342 entityReferenceStack.push() = { hash, entity.name };
343 injectToken(ENTITY_DONE);
344 return true;
345 }
346
347
348 QIODevice *device;
349 bool deleteDevice;
350 QStringDecoder decoder;
351 bool atEnd;
352
353 enum class XmlContext
354 {
355 Prolog,
356 Body,
357 };
358
359 XmlContext currentContext = XmlContext::Prolog;
360 bool foundDTD = false;
361 bool isValidToken(QXmlStreamReader::TokenType type);
362 void checkToken();
363
364 /*!
365 \sa setType()
366 */
367 QXmlStreamReader::TokenType type;
368 QXmlStreamReader::Error error;
369 QString errorString;
370 QString unresolvedEntity;
371
372 qint64 lineNumber, lastLineStart, characterOffset;
373
374
375 void write(const QString &);
376 void write(const char *);
377
378
379 QXmlStreamAttributes attributes;
380 XmlStringRef namespaceForPrefix(QStringView prefix);
381 void resolveTag();
382 void resolvePublicNamespaces();
383 void resolveDtd();
384 uint resolveCharRef(int symbolIndex);
385 bool checkStartDocument();
386 void startDocument();
387 void parseError();
388 void checkPublicLiteral(QStringView publicId);
389
390 // internal DTD subset support (subset of text() between '[' and ']'; empty if there is none)
391 QStringView dtdInternalSubset() const { return dtdInternalSubsetValue; }
392 static QXmlStreamReaderPrivate *get(QXmlStreamReader *reader) { return reader->d_func(); }
393
394 bool scanDtd;
395 XmlStringRef lastAttributeValue;
396 bool lastAttributeIsCData;
397 struct DtdAttribute {
398 XmlStringRef tagName;
399 XmlStringRef attributeQualifiedName;
400 XmlStringRef attributePrefix;
401 XmlStringRef attributeName;
402 XmlStringRef defaultValue;
403 bool isCDATA;
404 bool isNamespaceAttribute;
405 };
406 QXmlStreamSimpleStack<DtdAttribute> dtdAttributes;
407 struct NotationDeclaration {
408 XmlStringRef name;
409 XmlStringRef publicId;
410 XmlStringRef systemId;
411 };
412 QXmlStreamSimpleStack<NotationDeclaration> notationDeclarations;
413 QXmlStreamNotationDeclarations publicNotationDeclarations;
414 QXmlStreamNamespaceDeclarations publicNamespaceDeclarations;
415
416 struct EntityDeclaration {
417 XmlStringRef name;
418 XmlStringRef notationName;
419 XmlStringRef publicId;
420 XmlStringRef systemId;
421 XmlStringRef value;
422 bool parameter;
423 bool external;
424 inline void clear() {
425 name.clear();
426 notationName.clear();
427 publicId.clear();
428 systemId.clear();
429 value.clear();
430 parameter = external = false;
431 }
432 };
433 QXmlStreamSimpleStack<EntityDeclaration> entityDeclarations;
434 QXmlStreamEntityDeclarations publicEntityDeclarations;
435
436 XmlStringRef text;
437
438 XmlStringRef prefix, namespaceUri, qualifiedName, name;
439 XmlStringRef processingInstructionTarget, processingInstructionData;
440 XmlStringRef dtdName, dtdPublicId, dtdSystemId;
441 XmlStringRef dtdInternalSubsetValue;
442 XmlStringRef documentVersion, documentEncoding;
443 uint isEmptyElement : 1;
444 uint isWhitespace : 1;
445 uint isCDATA : 1;
446 uint standalone : 1;
447 uint hasCheckedStartDocument : 1;
448 uint normalizeLiterals : 1;
449 uint hasSeenTag : 1;
450 uint inParseEntity : 1;
451 uint referenceToUnparsedEntityDetected : 1;
452 uint referenceToParameterEntityDetected : 1;
453 uint hasExternalDtdSubset : 1;
454 uint lockEncoding : 1;
455 uint namespaceProcessing : 1;
456 uint hasStandalone : 1; // TODO: expose in public API
457
458 int resumeReduction;
459 void resume(int rule);
460
461 inline bool entitiesMustBeDeclared() const {
462 return (!inParseEntity
463 && (standalone
464 || (!referenceToUnparsedEntityDetected
465 && !referenceToParameterEntityDetected // Errata 13 as of 2006-04-25
466 && !hasExternalDtdSubset)));
467 }
468
469 // qlalr parser
470 qsizetype tos;
471 qsizetype stack_size;
472 struct Value {
473 qsizetype pos; // offset into textBuffer
474 qsizetype len; // length incl. prefix (if any)
475 qint16 prefix; // prefix of a name (as in "prefix:name") limited to 4k in fastScanName()
476 ushort c;
477 };
478
479 Value *sym_stack;
480 int *state_stack;
481 inline void reallocateStack();
482 inline Value &sym(int index) const
483 { return sym_stack[tos + index - 1]; }
484 QString textBuffer;
485 inline void clearTextBuffer() {
486 if (!scanDtd) {
487 textBuffer.resize(0);
488 textBuffer.reserve(256);
489 }
490 }
491 struct Attribute {
492 Value key;
493 Value value;
494 };
495 QXmlStreamSimpleStack<Attribute> attributeStack;
496
497 inline XmlStringRef symString(int index) {
498 return symString(sym(index));
499 }
500 QStringView symView(int index) const
501 {
502 const Value &symbol = sym(index);
503 return QStringView(textBuffer.data() + symbol.pos, symbol.len).mid(symbol.prefix);
504 }
505 inline XmlStringRef symName(int index) {
506 return symName(sym(index));
507 }
508 inline XmlStringRef symPrefix(int index) {
509 return symPrefix(sym(index));
510 }
511 auto symQName(int index) {
512 struct R {
513 XmlStringRef m_data;
514 qsizetype m_prefix;
515
516 XmlStringRef all() const { return m_data; }
517 XmlStringRef qname() const { return m_data; }
518 XmlStringRef prefix() const
519 { return m_prefix ? m_data.sliced(0, m_prefix - 1) : XmlStringRef(); }
520 XmlStringRef localPart() const { return m_data.sliced(m_prefix); }
521
522 void resetBackingStorage(XmlStringRef newData) { m_data = newData; } // m_prefix stays!
523 };
524 const Value &symbol = sym(index);
525 return R{{&textBuffer, symbol.pos, symbol.len}, symbol.prefix};
526 }
527 inline XmlStringRef symString(const Value &symbol) {
528 return XmlStringRef(&textBuffer, symbol.pos + symbol.prefix, symbol.len - symbol.prefix);
529 }
530 inline XmlStringRef symName(const Value &symbol) {
531 return XmlStringRef(&textBuffer, symbol.pos, symbol.len);
532 }
533 inline XmlStringRef symPrefix(const Value &symbol) {
534 if (symbol.prefix)
535 return XmlStringRef(&textBuffer, symbol.pos, symbol.prefix - 1);
536 return XmlStringRef();
537 }
538
539 inline void clearSym() { Value &val = sym(1); val.pos = textBuffer.size(); val.len = 0; }
540
541
542 short token;
543 uint token_char;
544
545 uint filterCarriageReturn();
546 inline uint getChar();
547 inline uint peekChar();
548 inline void putChar(uint c) { putStack.push() = c; }
549 inline void putChar(QChar c) { putStack.push() = c.unicode(); }
550 void putString(QStringView s, qsizetype from = 0);
551 void putStringLiteral(QStringView s);
552 void putReplacement(QStringView s);
553 void putReplacementInAttributeValue(QStringView s);
554 uint getChar_helper();
555
556 bool scanUntil(const char *str, short tokenToInject = -1);
557 bool scanString(const char *str, short tokenToInject, bool requireSpace = true);
558 inline void injectToken(ushort tokenToInject) {
559 putChar(int(tokenToInject) << 16);
560 }
561
562 QString resolveUndeclaredEntity(const QString &name);
563 void parseEntity(const QString &value);
564 std::unique_ptr<QXmlStreamReaderPrivate> entityParser;
565
566 bool scanAfterLangleBang();
567 bool scanPublicOrSystem();
568 bool scanNData();
569 bool scanAfterDefaultDecl();
570 bool scanAttType();
571
572
573 // scan optimization functions. Not strictly necessary but LALR is
574 // not very well suited for scanning fast
575 qsizetype fastScanLiteralContent();
576 qsizetype fastScanSpace();
577 qsizetype fastScanContentCharList();
578 std::optional<qsizetype> fastScanName(Value *val = nullptr);
579 inline qsizetype fastScanNMTOKEN();
580
581
582 bool parse();
583 inline void consumeRule(int);
584
585 void raiseError(QXmlStreamReader::Error error, const QString& message = QString());
586 void raiseWellFormedError(const QString &message);
587 void raiseNamePrefixTooLongError();
588
589 QXmlStreamEntityResolver *entityResolver;
590
591private:
592 /*! \internal
593 Never assign to variable type directly. Instead use this function.
594
595 This prevents errors from being ignored.
596 */
597 inline void setType(const QXmlStreamReader::TokenType t)
598 {
599 if (type != QXmlStreamReader::Invalid)
600 type = t;
601 }
602};
603#endif // feature xmlstreamreader
604
605QT_END_NAMESPACE
606
607#endif // QXMLSTREAM_P_H
qsizetype initialTagStackStringStorageSize
XmlStringRef addToStringStorage(QAnyStringView s)
QXmlStreamSimpleStack< NamespaceDeclaration > namespaceDeclarations
QXmlStreamSimpleStack< Tag > tagStack
void resize(qsizetype s)
const T * cbegin() const
const T & top() const
void reserve(qsizetype extraCapacity)
const T * cend() const
const T * begin() const
const T & at(qsizetype index) const
T & operator[](qsizetype index)
qsizetype size() const
const T * end() const
XmlStringRef(const QString *string)
constexpr XmlStringRef()=default
const QString * m_string
constexpr XmlStringRef(const QString *string, qsizetype pos, qsizetype length)
#define MAKE_MEMBER(name)
#define MAKE_OP(op)
NamespaceDeclaration namespaceDeclaration