7#include "core/fxcrt/xml/cfx_xmlparser.h"
16#include "core/fxcrt/autorestorer.h"
17#include "core/fxcrt/cfx_seekablestreamproxy.h"
18#include "core/fxcrt/data_vector.h"
19#include "core/fxcrt/fx_codepage.h"
20#include "core/fxcrt/fx_extension.h"
21#include "core/fxcrt/fx_safe_types.h"
22#include "core/fxcrt/xml/cfx_xmlchardata.h"
23#include "core/fxcrt/xml/cfx_xmldocument.h"
24#include "core/fxcrt/xml/cfx_xmlelement.h"
25#include "core/fxcrt/xml/cfx_xmlinstruction.h"
26#include "core/fxcrt/xml/cfx_xmlnode.h"
27#include "core/fxcrt/xml/cfx_xmltext.h"
28#include "third_party/base/check.h"
29#include "third_party/base/notreached.h"
33constexpr size_t kCurrentTextReserve = 128;
34constexpr uint32_t kMaxCharRange = 0x10ffff;
36bool IsXMLWhiteSpace(
wchar_t ch) {
37 return ch == L' ' || ch == 0x0A || ch == 0x0D || ch == 0x09;
40struct FX_XMLNAMECHAR {
46constexpr FX_XMLNAMECHAR kXMLNameChars[] = {
47 {L'-', L'.',
false}, {L'0', L'9',
false}, {L':', L':',
false},
48 {L'A', L'Z',
true}, {L'_', L'_',
true}, {L'a', L'z',
true},
49 {0xB7, 0xB7,
false}, {0xC0, 0xD6,
true}, {0xD8, 0xF6,
true},
50 {0xF8, 0x02FF,
true}, {0x0300, 0x036F,
false}, {0x0370, 0x037D,
true},
51 {0x037F, 0x1FFF,
true}, {0x200C, 0x200D,
true}, {0x203F, 0x2040,
false},
52 {0x2070, 0x218F,
true}, {0x2C00, 0x2FEF,
true}, {0x3001, 0xD7FF,
true},
53 {0xF900, 0xFDCF,
true}, {0xFDF0, 0xFFFD,
true},
60 auto* it =
std::lower_bound(
61 std::begin(kXMLNameChars),
std::end(kXMLNameChars), ch,
62 [](
const FX_XMLNAMECHAR& arg,
wchar_t ch) {
return arg.wEnd < ch; });
63 return it !=
std::end(kXMLNameChars) && ch >= it->wStart &&
64 (!bFirstChar || it->bStartChar);
70 auto proxy = pdfium::MakeRetain<CFX_SeekableStreamProxy>(pStream);
78 xml_plane_size_ = std::min(
79 xml_plane_size_, pdfium::base::checked_cast<size_t>(stream_->GetSize()));
81 current_text_.reserve(kCurrentTextReserve);
89 current_node_ = doc->GetRoot();
90 return DoSyntaxParse(doc.get()) ?
std::move(doc) :
nullptr;
94 if (xml_plane_size_ <= 0)
97 FX_SAFE_SIZE_T alloc_size_safe = xml_plane_size_;
99 if (!alloc_size_safe.IsValid())
102 size_t current_buffer_idx = 0;
103 size_t buffer_size = 0;
105 DataVector<
wchar_t> buffer;
106 buffer.resize(alloc_size_safe.ValueOrDie());
108 std::stack<
wchar_t> character_to_skip_too_stack;
109 std::stack<CFX_XMLNode::Type> node_type_stack;
110 WideString current_attribute_name;
111 FDE_XmlSyntaxState current_parser_state = FDE_XmlSyntaxState::Text;
112 wchar_t current_quote_character = 0;
113 wchar_t current_character_to_skip_to = 0;
116 if (current_buffer_idx >= buffer_size) {
117 if (stream_->IsEOF())
120 size_t buffer_chars = stream_->ReadBlock(buffer.data(), xml_plane_size_);
121 if (buffer_chars == 0)
124 current_buffer_idx = 0;
125 buffer_size = buffer_chars;
128 while (current_buffer_idx < buffer_size) {
129 wchar_t ch = buffer[current_buffer_idx];
130 switch (current_parser_state) {
131 case FDE_XmlSyntaxState::Text:
133 if (!current_text_.empty()) {
134 current_node_->AppendLastChild(
135 doc->CreateNode<CFX_XMLText>(GetTextData()));
137 current_buffer_idx++;
138 current_parser_state = FDE_XmlSyntaxState::Node;
146 current_buffer_idx++;
149 case FDE_XmlSyntaxState::Node:
151 current_buffer_idx++;
152 current_parser_state = FDE_XmlSyntaxState::SkipCommentOrDecl;
153 }
else if (ch == L'/') {
154 current_buffer_idx++;
155 current_parser_state = FDE_XmlSyntaxState::CloseElement;
156 }
else if (ch == L'?') {
158 current_buffer_idx++;
159 current_parser_state = FDE_XmlSyntaxState::Target;
162 current_parser_state = FDE_XmlSyntaxState::Tag;
165 case FDE_XmlSyntaxState::Target:
166 if (!IsXMLNameChar(ch, current_text_.empty())) {
167 if (current_text_.empty())
170 current_parser_state = FDE_XmlSyntaxState::TargetData;
172 WideString target_name = GetTextData();
175 auto* node = doc->CreateNode<CFX_XMLInstruction>(target_name);
176 current_node_->AppendLastChild(node);
177 current_node_ = node;
180 current_text_.push_back(ch);
181 current_buffer_idx++;
184 case FDE_XmlSyntaxState::Tag:
185 if (!IsXMLNameChar(ch, current_text_.empty())) {
186 if (current_text_.empty())
189 current_parser_state = FDE_XmlSyntaxState::AttriName;
191 auto* child = doc->CreateNode<CFX_XMLElement>(GetTextData());
192 current_node_->AppendLastChild(child);
193 current_node_ = child;
195 current_text_.push_back(ch);
196 current_buffer_idx++;
199 case FDE_XmlSyntaxState::AttriName:
200 if (current_text_.empty() && IsXMLWhiteSpace(ch)) {
201 current_buffer_idx++;
204 if (!IsXMLNameChar(ch, current_text_.empty())) {
205 if (current_text_.empty()) {
207 if (ch == L'>' || ch == L'/') {
208 current_parser_state = FDE_XmlSyntaxState::BreakElement;
211 }
else if (node_type_stack.top() ==
214 current_parser_state = FDE_XmlSyntaxState::CloseInstruction;
215 current_buffer_idx++;
217 current_parser_state = FDE_XmlSyntaxState::TargetData;
224 if (ch !=
'=' && !IsXMLWhiteSpace(ch)) {
225 current_parser_state = FDE_XmlSyntaxState::TargetData;
229 current_parser_state = FDE_XmlSyntaxState::AttriEqualSign;
230 current_attribute_name
= GetTextData();
233 current_text_.push_back(ch);
234 current_buffer_idx++;
237 case FDE_XmlSyntaxState::AttriEqualSign:
238 if (IsXMLWhiteSpace(ch)) {
239 current_buffer_idx++;
244 current_parser_state = FDE_XmlSyntaxState::TargetData;
249 current_parser_state = FDE_XmlSyntaxState::AttriQuotation;
250 current_buffer_idx++;
253 case FDE_XmlSyntaxState::AttriQuotation:
254 if (IsXMLWhiteSpace(ch)) {
255 current_buffer_idx++;
258 if (ch != L'\"' && ch != L'\'') {
262 current_quote_character = ch;
263 current_parser_state = FDE_XmlSyntaxState::AttriValue;
264 current_buffer_idx++;
266 case FDE_XmlSyntaxState::AttriValue:
267 if (ch == current_quote_character) {
268 if (entity_start_.has_value())
271 current_quote_character = 0;
272 current_buffer_idx++;
273 current_parser_state = FDE_XmlSyntaxState::AttriName;
275 CFX_XMLElement* elem = ToXMLElement(current_node_);
282 current_buffer_idx++;
285 case FDE_XmlSyntaxState::CloseInstruction:
287 current_text_.push_back(ch);
288 current_parser_state = FDE_XmlSyntaxState::TargetData;
289 }
else if (!current_text_.empty()) {
292 current_buffer_idx++;
293 if (node_type_stack.empty())
296 node_type_stack.pop();
297 current_parser_state = FDE_XmlSyntaxState::Text;
300 current_node_->GetType() == CFX_XMLNode::Type::kInstruction)
301 current_node_ = current_node_->GetParent();
304 case FDE_XmlSyntaxState::BreakElement:
306 current_parser_state = FDE_XmlSyntaxState::Text;
307 }
else if (ch == L'/') {
308 current_parser_state = FDE_XmlSyntaxState::CloseElement;
312 current_buffer_idx++;
314 case FDE_XmlSyntaxState::CloseElement:
315 if (!IsXMLNameChar(ch, current_text_.empty())) {
317 if (node_type_stack.empty())
320 node_type_stack.pop();
321 current_parser_state = FDE_XmlSyntaxState::Text;
323 CFX_XMLElement* element = ToXMLElement(current_node_);
327 WideString element_name = GetTextData();
328 if (element_name.GetLength() > 0 &&
333 current_node_ = current_node_->GetParent();
334 }
else if (!IsXMLWhiteSpace(ch)) {
338 current_text_.push_back(ch);
340 current_buffer_idx++;
342 case FDE_XmlSyntaxState::SkipCommentOrDecl: {
344 pdfium::make_span(buffer).subspan(current_buffer_idx);
345 if (FXSYS_wcsnicmp(current_span.data(), L"--", 2) == 0) {
346 current_buffer_idx += 2;
347 current_parser_state = FDE_XmlSyntaxState::SkipComment;
348 }
else if (FXSYS_wcsnicmp(current_span.data(), L"[CDATA[", 7) == 0) {
349 current_buffer_idx += 7;
350 current_parser_state = FDE_XmlSyntaxState::SkipCData;
352 current_parser_state = FDE_XmlSyntaxState::SkipDeclNode;
353 current_character_to_skip_to = L'>';
354 character_to_skip_too_stack.push(L'>');
358 case FDE_XmlSyntaxState::SkipCData: {
360 pdfium::make_span(buffer).subspan(current_buffer_idx);
361 if (FXSYS_wcsnicmp(current_span.data(), L"]]>", 3) == 0) {
362 current_buffer_idx += 3;
363 current_parser_state = FDE_XmlSyntaxState::Text;
364 current_node_->AppendLastChild(
365 doc->CreateNode<CFX_XMLCharData>(GetTextData()));
367 current_text_.push_back(ch);
368 current_buffer_idx++;
372 case FDE_XmlSyntaxState::SkipDeclNode:
373 if (current_character_to_skip_to == L'\'' ||
374 current_character_to_skip_to == L'\"') {
375 current_buffer_idx++;
376 if (ch != current_character_to_skip_to)
379 character_to_skip_too_stack.pop();
380 if (character_to_skip_too_stack.empty())
381 current_parser_state = FDE_XmlSyntaxState::Text;
383 current_character_to_skip_to = character_to_skip_too_stack.top();
387 current_character_to_skip_to = L'>';
388 character_to_skip_too_stack.push(L'>');
391 current_character_to_skip_to = L']';
392 character_to_skip_too_stack.push(L']');
395 current_character_to_skip_to = L')';
396 character_to_skip_too_stack.push(L')');
399 current_character_to_skip_to = L'\'';
400 character_to_skip_too_stack.push(L'\'');
403 current_character_to_skip_to = L'\"';
404 character_to_skip_too_stack.push(L'\"');
407 if (ch == current_character_to_skip_to) {
408 character_to_skip_too_stack.pop();
409 if (character_to_skip_too_stack.empty()) {
410 current_parser_state = FDE_XmlSyntaxState::Text;
412 current_character_to_skip_to =
413 character_to_skip_too_stack.top();
418 current_buffer_idx++;
421 case FDE_XmlSyntaxState::SkipComment: {
423 pdfium::make_span(buffer).subspan(current_buffer_idx);
424 if (FXSYS_wcsnicmp(current_span.data(), L"-->", 3) == 0) {
425 current_buffer_idx += 2;
426 current_parser_state = FDE_XmlSyntaxState::Text;
428 current_buffer_idx++;
431 case FDE_XmlSyntaxState::TargetData:
432 if (IsXMLWhiteSpace(ch)) {
433 if (current_text_.empty()) {
434 current_buffer_idx++;
437 if (current_quote_character == 0) {
438 current_buffer_idx++;
444 current_parser_state = FDE_XmlSyntaxState::CloseInstruction;
445 current_buffer_idx++;
446 }
else if (ch ==
'\"') {
447 if (current_quote_character == 0) {
448 current_quote_character = ch;
449 current_buffer_idx++;
450 }
else if (ch == current_quote_character) {
451 current_quote_character = 0;
452 current_buffer_idx++;
458 current_text_.push_back(ch);
459 current_buffer_idx++;
466 NOTREACHED_NORETURN();
469void CFX_XMLParser::ProcessTextChar(
wchar_t character) {
470 current_text_.push_back(character);
472 if (entity_start_.has_value() && character == L';') {
476 WideString csEntity(current_text_.data() + entity_start_.value() + 1,
477 current_text_.size() - entity_start_.value() - 2);
478 current_text_.erase(current_text_.begin() + entity_start_.value(),
479 current_text_.end());
481 size_t iLen = csEntity.GetLength();
483 if (csEntity
[0
] == L'#') {
485 if (iLen > 1 && csEntity
[1
] == L'x') {
486 for (size_t i = 2; i < iLen; i++) {
487 if (!FXSYS_IsHexDigit(csEntity[i]))
489 ch = (ch << 4) + FXSYS_HexCharToInt(csEntity[i]);
492 for (size_t i = 1; i < iLen; i++) {
493 if (!FXSYS_IsDecimalDigit(csEntity[i]))
495 ch = ch * 10 + FXSYS_DecimalCharToInt(csEntity[i]);
498 if (ch > kMaxCharRange)
501 character =
static_cast<
wchar_t>(ch);
503 current_text_.push_back(character);
505 if (csEntity
== L"amp") {
506 current_text_.push_back(L'&');
507 }
else if (csEntity
== L"lt") {
508 current_text_.push_back(L'<');
509 }
else if (csEntity
== L"gt") {
510 current_text_.push_back(L'>');
511 }
else if (csEntity
== L"apos") {
512 current_text_.push_back(L'\'');
513 }
else if (csEntity
== L"quot") {
514 current_text_.push_back(L'"');
518 entity_start_ = absl::nullopt;
519 }
else if (!entity_start_.has_value() && character == L'&') {
520 entity_start_ = current_text_.size() - 1;
524void CFX_XMLParser::ProcessTargetData() {
525 WideString target_data = GetTextData();
529 CFX_XMLInstruction* instruction = ToXMLInstruction(current_node_);
534WideString CFX_XMLParser::GetTextData() {
535 WideString ret(current_text_.data(), current_text_.size());
536 entity_start_ = absl::nullopt;
537 current_text_.clear();
538 current_text_.reserve(kCurrentTextReserve);
const WideString & GetName() const
void SetAttribute(const WideString &name, const WideString &value)
void AppendData(const WideString &wsData)
CFX_XMLParser(const RetainPtr< IFX_SeekableReadStream > &pStream)
static bool IsXMLNameChar(wchar_t ch, bool bFirstChar)
std::unique_ptr< CFX_XMLDocument > Parse()
WideString & operator=(WideString &&that) noexcept
bool operator==(const wchar_t *ptr) const
CharType operator[](const size_t index) const
bool operator!=(const WideString &other) const
bool EqualsASCII(ByteStringView that) const
bool FXSYS_iswspace(wchar_t c)