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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fxcrt/xml/cfx_xmlparser.h"
#include <algorithm>
#include <cwctype>
#include <iterator>
#include <stack>
#include <utility>
#include "core/fxcrt/cfx_seekablestreamproxy.h"
#include "core/fxcrt/fx_codepage.h"
#include "core/fxcrt/fx_extension.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/xml/cfx_xmlchardata.h"
#include "core/fxcrt/xml/cfx_xmldocument.h"
#include "core/fxcrt/xml/cfx_xmlelement.h"
#include "core/fxcrt/xml/cfx_xmlinstruction.h"
#include "core/fxcrt/xml/cfx_xmlnode.h"
#include "core/fxcrt/xml/cfx_xmltext.h"
#include "third_party/base/ptr_util.h"
namespace {
constexpr size_t kCurrentTextReserve = 128;
constexpr uint32_t kMaxCharRange = 0x10ffff;
bool IsXMLWhiteSpace(wchar_t ch) {
return ch == L' ' || ch == 0x0A || ch == 0x0D || ch == 0x09;
}
struct FX_XMLNAMECHAR {
uint16_t wStart;
uint16_t wEnd;
bool bStartChar;
};
const FX_XMLNAMECHAR g_XMLNameChars[] = {
{L'-', L'.', false}, {L'0', L'9', false}, {L':', L':', false},
{L'A', L'Z', true}, {L'_', L'_', true}, {L'a', L'z', true},
{0xB7, 0xB7, false}, {0xC0, 0xD6, true}, {0xD8, 0xF6, true},
{0xF8, 0x02FF, true}, {0x0300, 0x036F, false}, {0x0370, 0x037D, true},
{0x037F, 0x1FFF, true}, {0x200C, 0x200D, true}, {0x203F, 0x2040, false},
{0x2070, 0x218F, true}, {0x2C00, 0x2FEF, true}, {0x3001, 0xD7FF, true},
{0xF900, 0xFDCF, true}, {0xFDF0, 0xFFFD, true},
};
} // namespace
// static
bool CFX_XMLParser::IsXMLNameChar(wchar_t ch, bool bFirstChar) {
auto* it = std::lower_bound(
std::begin(g_XMLNameChars), std::end(g_XMLNameChars), ch,
[](const FX_XMLNAMECHAR& arg, wchar_t ch) { return arg.wEnd < ch; });
return it != std::end(g_XMLNameChars) && ch >= it->wStart &&
(!bFirstChar || it->bStartChar);
}
CFX_XMLParser::CFX_XMLParser(const RetainPtr<IFX_SeekableReadStream>& pStream) {
ASSERT(pStream);
auto proxy = pdfium::MakeRetain<CFX_SeekableStreamProxy>(pStream);
uint16_t wCodePage = proxy->GetCodePage();
if (wCodePage != FX_CODEPAGE_UTF16LE && wCodePage != FX_CODEPAGE_UTF16BE &&
wCodePage != FX_CODEPAGE_UTF8) {
proxy->SetCodePage(FX_CODEPAGE_UTF8);
}
stream_ = proxy;
xml_plane_size_ = std::min(
xml_plane_size_, pdfium::base::checked_cast<size_t>(stream_->GetSize()));
current_text_.reserve(kCurrentTextReserve);
}
CFX_XMLParser::~CFX_XMLParser() = default;
std::unique_ptr<CFX_XMLDocument> CFX_XMLParser::Parse() {
auto doc = pdfium::MakeUnique<CFX_XMLDocument>();
current_node_ = doc->GetRoot();
return DoSyntaxParse(doc.get()) ? std::move(doc) : nullptr;
}
bool CFX_XMLParser::DoSyntaxParse(CFX_XMLDocument* doc) {
FX_FILESIZE current_buffer_idx = 0;
FX_FILESIZE buffer_size = 0;
FX_SAFE_SIZE_T alloc_size_safe = xml_plane_size_;
alloc_size_safe += 1; // For NUL.
if (!alloc_size_safe.IsValid() || alloc_size_safe.ValueOrDie() <= 0 ||
xml_plane_size_ <= 0)
return false;
std::vector<wchar_t> buffer;
buffer.resize(pdfium::base::ValueOrDieForType<size_t>(alloc_size_safe));
std::stack<wchar_t> character_to_skip_too_stack;
std::stack<FX_XMLNODETYPE> node_type_stack;
WideString current_attribute_name;
FDE_XmlSyntaxState current_parser_state = FDE_XmlSyntaxState::Text;
int32_t iCount = 0;
wchar_t current_quote_character = 0;
wchar_t current_character_to_skip_to = 0;
while (true) {
if (current_buffer_idx >= buffer_size) {
if (stream_->IsEOF())
return true;
size_t buffer_chars = stream_->ReadBlock(buffer.data(), xml_plane_size_);
if (buffer_chars == 0)
return true;
current_buffer_idx = 0;
buffer_size = buffer_chars;
}
while (current_buffer_idx < buffer_size) {
wchar_t ch = buffer[current_buffer_idx];
switch (current_parser_state) {
case FDE_XmlSyntaxState::Text:
if (ch == L'<') {
if (!current_text_.empty()) {
current_node_->AppendChild(
doc->CreateNode<CFX_XMLText>(GetTextData()));
} else {
current_buffer_idx++;
current_parser_state = FDE_XmlSyntaxState::Node;
}
} else {
// Fail if there is text outside of the root element, ignore
// whitespace/null.
if (node_type_stack.empty() && ch && !FXSYS_iswspace(ch))
return false;
ProcessTextChar(ch);
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::Node:
if (ch == L'!') {
current_buffer_idx++;
current_parser_state = FDE_XmlSyntaxState::SkipCommentOrDecl;
} else if (ch == L'/') {
current_buffer_idx++;
current_parser_state = FDE_XmlSyntaxState::CloseElement;
} else if (ch == L'?') {
node_type_stack.push(FX_XMLNODE_Instruction);
current_buffer_idx++;
current_parser_state = FDE_XmlSyntaxState::Target;
} else {
node_type_stack.push(FX_XMLNODE_Element);
current_parser_state = FDE_XmlSyntaxState::Tag;
}
break;
case FDE_XmlSyntaxState::Target:
if (!IsXMLNameChar(ch, current_text_.empty())) {
if (current_text_.empty())
return false;
current_parser_state = FDE_XmlSyntaxState::TargetData;
WideString target_name = GetTextData();
if (target_name == L"originalXFAVersion" ||
target_name == L"acrobat") {
auto* node = doc->CreateNode<CFX_XMLInstruction>(target_name);
current_node_->AppendChild(node);
current_node_ = node;
}
} else {
current_text_.push_back(ch);
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::Tag:
if (!IsXMLNameChar(ch, current_text_.empty())) {
if (current_text_.empty())
return false;
current_parser_state = FDE_XmlSyntaxState::AttriName;
auto* child = doc->CreateNode<CFX_XMLElement>(GetTextData());
current_node_->AppendChild(child);
current_node_ = child;
} else {
current_text_.push_back(ch);
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::AttriName:
if (current_text_.empty() && IsXMLWhiteSpace(ch)) {
current_buffer_idx++;
break;
}
if (!IsXMLNameChar(ch, current_text_.empty())) {
if (current_text_.empty()) {
if (node_type_stack.top() == FX_XMLNODE_Element) {
if (ch == L'>' || ch == L'/') {
current_parser_state = FDE_XmlSyntaxState::BreakElement;
break;
}
} else if (node_type_stack.top() == FX_XMLNODE_Instruction) {
if (ch == L'?') {
current_parser_state = FDE_XmlSyntaxState::CloseInstruction;
current_buffer_idx++;
} else {
current_parser_state = FDE_XmlSyntaxState::TargetData;
}
break;
}
return false;
} else {
if (node_type_stack.top() == FX_XMLNODE_Instruction) {
if (ch != '=' && !IsXMLWhiteSpace(ch)) {
current_parser_state = FDE_XmlSyntaxState::TargetData;
break;
}
}
current_parser_state = FDE_XmlSyntaxState::AttriEqualSign;
current_attribute_name = GetTextData();
}
} else {
current_text_.push_back(ch);
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::AttriEqualSign:
if (IsXMLWhiteSpace(ch)) {
current_buffer_idx++;
break;
}
if (ch != L'=') {
if (node_type_stack.top() == FX_XMLNODE_Instruction) {
current_parser_state = FDE_XmlSyntaxState::TargetData;
break;
}
return false;
} else {
current_parser_state = FDE_XmlSyntaxState::AttriQuotation;
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::AttriQuotation:
if (IsXMLWhiteSpace(ch)) {
current_buffer_idx++;
break;
}
if (ch != L'\"' && ch != L'\'') {
return false;
}
current_quote_character = ch;
current_parser_state = FDE_XmlSyntaxState::AttriValue;
current_buffer_idx++;
break;
case FDE_XmlSyntaxState::AttriValue:
if (ch == current_quote_character) {
if (entity_start_ > -1)
return false;
current_quote_character = 0;
current_buffer_idx++;
current_parser_state = FDE_XmlSyntaxState::AttriName;
CFX_XMLElement* elem = ToXMLElement(current_node_);
if (elem)
elem->SetAttribute(current_attribute_name, GetTextData());
current_attribute_name.clear();
} else {
ProcessTextChar(ch);
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::CloseInstruction:
if (ch != L'>') {
current_text_.push_back(ch);
current_parser_state = FDE_XmlSyntaxState::TargetData;
} else if (!current_text_.empty()) {
ProcessTargetData();
} else {
current_buffer_idx++;
if (node_type_stack.empty())
return false;
node_type_stack.pop();
current_parser_state = FDE_XmlSyntaxState::Text;
if (current_node_ &&
current_node_->GetType() == FX_XMLNODE_Instruction)
current_node_ = current_node_->GetParent();
}
break;
case FDE_XmlSyntaxState::BreakElement:
if (ch == L'>') {
current_parser_state = FDE_XmlSyntaxState::Text;
} else if (ch == L'/') {
current_parser_state = FDE_XmlSyntaxState::CloseElement;
} else {
return false;
}
current_buffer_idx++;
break;
case FDE_XmlSyntaxState::CloseElement:
if (!IsXMLNameChar(ch, current_text_.empty())) {
if (ch == L'>') {
if (node_type_stack.empty())
return false;
node_type_stack.pop();
current_parser_state = FDE_XmlSyntaxState::Text;
CFX_XMLElement* element = ToXMLElement(current_node_);
if (!element)
return false;
WideString element_name = GetTextData();
if (element_name.GetLength() > 0 &&
element_name != element->GetName()) {
return false;
}
current_node_ = current_node_->GetParent();
iCount++;
} else if (!IsXMLWhiteSpace(ch)) {
return false;
}
} else {
current_text_.push_back(ch);
}
current_buffer_idx++;
break;
case FDE_XmlSyntaxState::SkipCommentOrDecl:
if (FXSYS_wcsnicmp(buffer.data() + current_buffer_idx, L"--", 2) ==
0) {
current_buffer_idx += 2;
current_parser_state = FDE_XmlSyntaxState::SkipComment;
} else if (FXSYS_wcsnicmp(buffer.data() + current_buffer_idx,
L"[CDATA[", 7) == 0) {
current_buffer_idx += 7;
current_parser_state = FDE_XmlSyntaxState::SkipCData;
} else {
current_parser_state = FDE_XmlSyntaxState::SkipDeclNode;
current_character_to_skip_to = L'>';
character_to_skip_too_stack.push(L'>');
}
break;
case FDE_XmlSyntaxState::SkipCData: {
if (FXSYS_wcsnicmp(buffer.data() + current_buffer_idx, L"]]>", 3) ==
0) {
current_buffer_idx += 3;
current_parser_state = FDE_XmlSyntaxState::Text;
current_node_->AppendChild(
doc->CreateNode<CFX_XMLCharData>(GetTextData()));
} else {
current_text_.push_back(ch);
current_buffer_idx++;
}
break;
}
case FDE_XmlSyntaxState::SkipDeclNode:
if (current_character_to_skip_to == L'\'' ||
current_character_to_skip_to == L'\"') {
current_buffer_idx++;
if (ch != current_character_to_skip_to)
break;
character_to_skip_too_stack.pop();
if (character_to_skip_too_stack.empty())
current_parser_state = FDE_XmlSyntaxState::Text;
else
current_character_to_skip_to = character_to_skip_too_stack.top();
} else {
switch (ch) {
case L'<':
current_character_to_skip_to = L'>';
character_to_skip_too_stack.push(L'>');
break;
case L'[':
current_character_to_skip_to = L']';
character_to_skip_too_stack.push(L']');
break;
case L'(':
current_character_to_skip_to = L')';
character_to_skip_too_stack.push(L')');
break;
case L'\'':
current_character_to_skip_to = L'\'';
character_to_skip_too_stack.push(L'\'');
break;
case L'\"':
current_character_to_skip_to = L'\"';
character_to_skip_too_stack.push(L'\"');
break;
default:
if (ch == current_character_to_skip_to) {
character_to_skip_too_stack.pop();
if (character_to_skip_too_stack.empty()) {
current_parser_state = FDE_XmlSyntaxState::Text;
} else {
current_character_to_skip_to =
character_to_skip_too_stack.top();
}
}
break;
}
current_buffer_idx++;
}
break;
case FDE_XmlSyntaxState::SkipComment:
if (FXSYS_wcsnicmp(buffer.data() + current_buffer_idx, L"-->", 3) ==
0) {
current_buffer_idx += 2;
current_parser_state = FDE_XmlSyntaxState::Text;
}
current_buffer_idx++;
break;
case FDE_XmlSyntaxState::TargetData:
if (IsXMLWhiteSpace(ch)) {
if (current_text_.empty()) {
current_buffer_idx++;
break;
}
if (current_quote_character == 0) {
current_buffer_idx++;
ProcessTargetData();
break;
}
}
if (ch == '?') {
current_parser_state = FDE_XmlSyntaxState::CloseInstruction;
current_buffer_idx++;
} else if (ch == '\"') {
if (current_quote_character == 0) {
current_quote_character = ch;
current_buffer_idx++;
} else if (ch == current_quote_character) {
current_quote_character = 0;
current_buffer_idx++;
ProcessTargetData();
} else {
return false;
}
} else {
current_text_.push_back(ch);
current_buffer_idx++;
}
break;
default:
break;
}
}
}
current_node_->AppendChild(doc->CreateNode<CFX_XMLText>(GetTextData()));
return true;
}
void CFX_XMLParser::ProcessTextChar(wchar_t character) {
current_text_.push_back(character);
if (entity_start_ > -1 && character == L';') {
// Copy the entity out into a string and remove from the vector. When we
// copy the entity we don't want to copy out the & or the ; so we start
// shifted by one and want to copy 2 less characters in total.
WideString csEntity(current_text_.data() + entity_start_ + 1,
current_text_.size() - entity_start_ - 2);
current_text_.erase(current_text_.begin() + entity_start_,
current_text_.end());
int32_t iLen = csEntity.GetLength();
if (iLen > 0) {
if (csEntity[0] == L'#') {
uint32_t ch = 0;
if (iLen > 1 && csEntity[1] == L'x') {
for (int32_t i = 2; i < iLen; i++) {
if (!FXSYS_isHexDigit(csEntity[i]))
break;
ch = (ch << 4) + FXSYS_HexCharToInt(csEntity[i]);
}
} else {
for (int32_t i = 1; i < iLen; i++) {
if (!FXSYS_isDecimalDigit(csEntity[i]))
break;
ch = ch * 10 + FXSYS_DecimalCharToInt(csEntity[i]);
}
}
if (ch > kMaxCharRange)
ch = ' ';
character = static_cast<wchar_t>(ch);
if (character != 0)
current_text_.push_back(character);
} else {
if (csEntity.Compare(L"amp") == 0) {
current_text_.push_back(L'&');
} else if (csEntity.Compare(L"lt") == 0) {
current_text_.push_back(L'<');
} else if (csEntity.Compare(L"gt") == 0) {
current_text_.push_back(L'>');
} else if (csEntity.Compare(L"apos") == 0) {
current_text_.push_back(L'\'');
} else if (csEntity.Compare(L"quot") == 0) {
current_text_.push_back(L'"');
}
}
}
entity_start_ = -1;
} else if (entity_start_ < 0 && character == L'&') {
entity_start_ = current_text_.size() - 1;
}
}
void CFX_XMLParser::ProcessTargetData() {
WideString target_data = GetTextData();
if (target_data.IsEmpty())
return;
CFX_XMLInstruction* instruction = ToXMLInstruction(current_node_);
if (instruction)
instruction->AppendData(target_data);
}
WideString CFX_XMLParser::GetTextData() {
WideString ret(current_text_.data(), current_text_.size());
entity_start_ = -1;
current_text_.clear();
current_text_.reserve(kCurrentTextReserve);
return ret;
}
|