Coverage Report

Created: 2025-07-23 06:41

/src/xmlProtoConverter.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2019 Google Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include "xmlProtoConverter.h"
18
19
#include <algorithm>
20
21
using namespace std;
22
using namespace xmlProtoFuzzer;
23
24
string ProtoConverter::removeNonAscii(string const& _utf8)
25
1.10M
{
26
1.10M
  string asciiStr{_utf8};
27
20.1M
  asciiStr.erase(remove_if(asciiStr.begin(), asciiStr.end(), [=](char c) -> bool {
28
20.1M
                        return !(std::isalpha(c) || std::isdigit(c));
29
20.1M
                }), asciiStr.end());
30
1.10M
  return asciiStr.empty() ? "fuzz" : asciiStr;
31
1.10M
}
32
33
34
void ProtoConverter::visit(Misc const& _x)
35
4.89k
{
36
4.89k
  switch (_x.misc_oneof_case())
37
4.89k
  {
38
1.73k
  case Misc::kComment:
39
1.73k
    m_output << "<!--" << _x.comment() << "-->\n";
40
1.73k
    break;
41
2.01k
  case Misc::kInst:
42
2.01k
    visit(_x.inst());
43
2.01k
    break;
44
1.13k
  case Misc::MISC_ONEOF_NOT_SET:
45
1.13k
    break;
46
4.89k
  }
47
4.89k
}
48
49
void ProtoConverter::visit(Prolog const& _x)
50
8.36k
{
51
8.36k
  visit(_x.decl());
52
8.36k
  visit(_x.doctype());
53
8.36k
  for (auto const& misc: _x.misc())
54
2.47k
    visit(misc);
55
8.36k
}
56
57
void ProtoConverter::visit(KeyValue const& _x)
58
21.3k
{
59
21.3k
  if (!KeyValue::XmlNamespace_IsValid(_x.type()))
60
167
    return;
61
62
21.2k
  switch (_x.type())
63
21.2k
  {
64
5.59k
  case KeyValue::ATTRIBUTES:
65
5.59k
    m_output << "xml:attributes=\"" << removeNonAscii(_x.value()) << "\" ";
66
5.59k
    break;
67
1.30k
  case KeyValue::BASE:
68
1.30k
    m_output << "xml:base=\"" << removeNonAscii(_x.value()) << "\" ";
69
1.30k
    break;
70
1.12k
  case KeyValue::CATALOG:
71
1.12k
    m_output << "xml:catalog=\"" << removeNonAscii(_x.value()) << "\" ";
72
1.12k
    break;
73
1.86k
  case KeyValue::ID:
74
1.86k
    m_output << "xml:id=\"" << removeNonAscii(_x.value()) << "\" ";
75
1.86k
    break;
76
582
  case KeyValue::LANG:
77
582
    m_output << "xml:lang=\"" << removeNonAscii(_x.value()) << "\" ";
78
582
    break;
79
686
  case KeyValue::LINK:
80
686
    m_output << "xml:link=\"" << removeNonAscii(_x.value()) << "\" ";
81
686
    break;
82
578
  case KeyValue::SPACE:
83
578
    m_output << "xml:space=\"" << removeNonAscii(_x.value()) << "\" ";
84
578
    break;
85
633
  case KeyValue::SPECIAL:
86
633
    m_output << "xml:special=\"" << removeNonAscii(_x.value()) << "\" ";
87
633
    break;
88
1.18k
  case KeyValue::TEST:
89
1.18k
    m_output << "xml:test=\"" << removeNonAscii(_x.value()) << "\" ";
90
1.18k
    break;
91
7.66k
  case KeyValue::FUZZ:
92
7.66k
    if (_x.ByteSizeLong() % 2)
93
2.62k
      m_output << "xmlns:" << removeNonAscii(_x.key()) << "=\"" << removeNonAscii(_x.value()) << "\" ";
94
5.04k
    else
95
5.04k
      m_output << removeNonAscii(_x.key()) << "=\"" << removeNonAscii(_x.value()) << "\" ";
96
7.66k
    break;
97
0
  case KeyValue_XmlNamespace_KeyValue_XmlNamespace_INT_MIN_SENTINEL_DO_NOT_USE_:
98
0
  case KeyValue_XmlNamespace_KeyValue_XmlNamespace_INT_MAX_SENTINEL_DO_NOT_USE_:
99
0
    break;
100
21.2k
  }
101
21.2k
}
102
103
void ProtoConverter::visit(ProcessingInstruction const& _x)
104
2.01k
{
105
2.01k
  m_output << "<?" << removeNonAscii(_x.name()) << " ";
106
2.01k
  for (auto const& prop: _x.kv())
107
1.62k
    visit(prop);
108
2.01k
  m_output << "?>\n";
109
2.01k
}
110
111
void ProtoConverter::visit(Content const& _x)
112
216k
{
113
216k
  switch (_x.content_oneof_case())
114
216k
  {
115
1.97k
  case Content::kStr:
116
1.97k
    m_output << _x.str() << "\n";
117
1.97k
    break;
118
3.37k
  case Content::kE:
119
3.37k
    visit(_x.e());
120
3.37k
    m_output << "\n";
121
3.37k
    break;
122
2.18k
  case Content::kC:
123
2.18k
    visit(_x.c());
124
2.18k
    m_output << "\n";
125
2.18k
    break;
126
208k
  case Content::CONTENT_ONEOF_NOT_SET:
127
208k
    break;
128
216k
  }
129
216k
}
130
131
void ProtoConverter::visit(ElementDecl const& _x)
132
6.01k
{
133
6.01k
  if (!ElementDecl::ContentSpec_IsValid(_x.spec()))
134
42
    return;
135
136
5.97k
  m_output << "<!ELEMENT " << _x.name() << " ";
137
5.97k
  switch (_x.spec())
138
5.97k
  {
139
2.56k
  case ElementDecl::EMPTY:
140
2.56k
    m_output << "EMPTY>";
141
2.56k
    break;
142
297
  case ElementDecl::ANY:
143
297
    m_output << "ANY>";
144
297
    break;
145
231
  case ElementDecl::FUZZ:
146
231
    m_output << "FUZZ>";
147
231
    break;
148
1.61k
  case ElementDecl::MIXED:
149
1.61k
    m_output << "(#PCDATA";
150
1.61k
    for (auto const& pcdata: _x.cdata())
151
2.90k
      m_output << "|" << pcdata;
152
1.61k
    m_output << ")";
153
1.61k
    if (_x.cdata_size() > 0)
154
1.08k
      m_output << "*";
155
1.61k
    m_output << ">";
156
1.61k
    break;
157
1.26k
  case ElementDecl::CHILDREN:
158
1.26k
  {
159
1.26k
    m_output << "(";
160
1.26k
    string delim = "";
161
3.74k
    for (auto const& str: _x.cdata()) {
162
3.74k
      m_output << delim << removeNonAscii(str);
163
3.74k
      delim = ", ";
164
3.74k
    }
165
1.26k
    m_output << ")>";
166
1.26k
    break;
167
0
  }
168
0
  case ElementDecl_ContentSpec_ElementDecl_ContentSpec_INT_MIN_SENTINEL_DO_NOT_USE_:
169
0
  case ElementDecl_ContentSpec_ElementDecl_ContentSpec_INT_MAX_SENTINEL_DO_NOT_USE_:
170
0
    break;
171
5.97k
  }
172
5.97k
}
173
174
void ProtoConverter::visit(AttValue const& _x)
175
4.75k
{
176
4.75k
  if (!isValid(_x))
177
52
    return;
178
179
4.70k
  m_output << "\"";
180
4.70k
  string prefix;
181
4.70k
  switch (_x.type())
182
4.70k
  {
183
2.60k
  case AttValue::ENTITY:
184
2.60k
    prefix = "&";
185
2.60k
    break;
186
1.74k
  case AttValue::CHAR:
187
1.74k
    if (_x.ByteSizeLong() % 2)
188
338
      prefix = "&#";
189
1.40k
    else
190
      // TODO: Value that follows this must be a
191
      // sequence of hex digits.
192
1.40k
      prefix = "&#x";
193
1.74k
    break;
194
356
  case AttValue::FUZZ:
195
356
    prefix = "fuzz";
196
356
    break;
197
0
  case AttValue_Type_AttValue_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
198
0
  case AttValue_Type_AttValue_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
199
0
    break;
200
4.70k
  }
201
4.70k
  for (auto const& name: _x.value())
202
5.20k
    m_output << prefix << removeNonAscii(name) << ";";
203
4.70k
  m_output << "\"";
204
4.70k
}
205
206
void ProtoConverter::visit(DefaultDecl const& _x)
207
18.3k
{
208
18.3k
  if (!isValid(_x))
209
99
    return;
210
211
18.2k
  switch (_x.type())
212
18.2k
  {
213
12.3k
  case DefaultDecl::REQUIRED:
214
12.3k
    m_output << "#REQUIRED";
215
12.3k
    break;
216
642
  case DefaultDecl::IMPLIED:
217
642
    m_output << "#IMPLIED";
218
642
    break;
219
4.75k
  case DefaultDecl::FIXED:
220
4.75k
    m_output << "#FIXED ";
221
4.75k
    visit(_x.att());
222
4.75k
    break;
223
497
  case DefaultDecl::FUZZ:
224
497
    m_output << "#FUZZ";
225
497
    break;
226
0
  case DefaultDecl_Type_DefaultDecl_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
227
0
  case DefaultDecl_Type_DefaultDecl_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
228
0
    break;
229
18.2k
  }
230
18.2k
}
231
232
void ProtoConverter::visit(AttDef const& _x)
233
18.4k
{
234
18.4k
  if (!isValid(_x))
235
66
    return;
236
237
18.3k
  m_output << " " << removeNonAscii(_x.name()) << " ";
238
18.3k
  switch (_x.type())
239
18.3k
  {
240
12.0k
  case AttDef::CDATA:
241
12.0k
    m_output << "CDATA ";
242
12.0k
    break;
243
1.26k
  case AttDef::ID:
244
1.26k
    m_output << "ID ";
245
1.26k
    break;
246
1.90k
  case AttDef::IDREF:
247
1.90k
    m_output << "IDREF ";
248
1.90k
    break;
249
601
  case AttDef::IDREFS:
250
601
    m_output << "IDREFS ";
251
601
    break;
252
694
  case AttDef::ENTITY:
253
694
    m_output << "ENTITY ";
254
694
    break;
255
821
  case AttDef::ENTITIES:
256
821
    m_output << "ENTITIES ";
257
821
    break;
258
343
  case AttDef::NMTOKEN:
259
343
    m_output << "NMTOKEN ";
260
343
    break;
261
591
  case AttDef::NMTOKENS:
262
591
    m_output << "NMTOKENS ";
263
591
    break;
264
151
  case AttDef::FUZZ:
265
151
    m_output << "FUZZ ";
266
151
    break;
267
0
  case AttDef_Type_AttDef_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
268
0
  case AttDef_Type_AttDef_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
269
0
    break;
270
18.3k
  }
271
18.3k
  visit(_x.def());
272
18.3k
}
273
274
void ProtoConverter::visit(AttListDecl const& _x)
275
4.17k
{
276
4.17k
  m_output << "<!ATTLIST " << removeNonAscii(_x.name());
277
4.17k
  for (auto const& att: _x.attdefs())
278
18.4k
    visit(att);
279
4.17k
  m_output << ">";
280
4.17k
}
281
282
void ProtoConverter::visit(NotationDecl const& _x)
283
5.28k
{
284
5.28k
  m_output << "<!NOTATION " << removeNonAscii(_x.name()) << " ";
285
5.28k
  switch (_x.notation_oneof_case())
286
5.28k
  {
287
1.61k
  case NotationDecl::kExt:
288
1.61k
    visit(_x.ext());
289
1.61k
    break;
290
2.48k
  case NotationDecl::kPub:
291
2.48k
    m_output << "PUBLIC " << _x.pub();
292
2.48k
    break;
293
643
  case NotationDecl::kFuzz:
294
643
    m_output << "FUZZ " << _x.fuzz();
295
643
    break;
296
544
  case NotationDecl::NOTATION_ONEOF_NOT_SET:
297
544
    break;
298
5.28k
  }
299
5.28k
  m_output << ">";
300
5.28k
}
301
302
void ProtoConverter::visit(NDataDecl const& _x)
303
2.10k
{
304
2.10k
  m_output << " NDATA " << _x.name();
305
2.10k
}
306
307
void ProtoConverter::visit(EntityDef const& _x)
308
7.93k
{
309
7.93k
  switch (_x.entity_oneof_case())
310
7.93k
  {
311
3.24k
  case EntityDef::kExt:
312
3.24k
    visit(_x.ext());
313
3.24k
    if (_x.ByteSizeLong() % 2)
314
2.10k
      visit(_x.ndata());
315
3.24k
    break;
316
2.41k
  case EntityDef::kVal:
317
2.41k
    visit(_x.val());
318
2.41k
    break;
319
2.27k
  case EntityDef::ENTITY_ONEOF_NOT_SET:
320
2.27k
    break;
321
7.93k
  }
322
7.93k
}
323
324
void ProtoConverter::visit(PEDef const& _x)
325
2.34k
{
326
2.34k
  switch (_x.pedef_oneof_case())
327
2.34k
  {
328
960
  case PEDef::kVal:
329
960
    visit(_x.val());
330
960
    break;
331
739
  case PEDef::kId:
332
739
    visit(_x.id());
333
739
    break;
334
642
  case PEDef::PEDEF_ONEOF_NOT_SET:
335
642
    break;
336
2.34k
  }
337
2.34k
}
338
339
void ProtoConverter::visit(EntityValue const& _x)
340
3.37k
{
341
3.37k
  if (!isValid(_x))
342
18
    return;
343
344
3.35k
  m_output << "\"";
345
3.35k
  string prefix;
346
3.35k
  switch (_x.type())
347
3.35k
  {
348
1.57k
  case EntityValue::ENTITY:
349
1.57k
    prefix = "&";
350
1.57k
    break;
351
1.25k
  case EntityValue::CHAR:
352
1.25k
    if (_x.ByteSizeLong() % 2)
353
304
      prefix = "&#";
354
954
    else
355
954
      prefix = "&#x";
356
1.25k
    break;
357
231
  case EntityValue::PEREF:
358
231
    prefix = "%";
359
231
    break;
360
287
  case EntityValue::FUZZ:
361
287
    prefix = "fuzz";
362
287
    break;
363
0
  case EntityValue_Type_EntityValue_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
364
0
  case EntityValue_Type_EntityValue_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
365
0
    break;
366
3.35k
  }
367
3.35k
  for (auto const& ref: _x.name())
368
7.04k
    m_output << prefix << ref << ";";
369
3.35k
  m_output << "\"";
370
3.35k
}
371
372
void ProtoConverter::visit(EntityDecl const& _x)
373
10.3k
{
374
10.3k
  if (!isValid(_x))
375
81
    return;
376
377
10.2k
  m_output << "<!ENTITY ";
378
10.2k
  switch (_x.type())
379
10.2k
  {
380
7.93k
  case EntityDecl::GEDECL:
381
7.93k
    m_output << _x.name() << " ";
382
7.93k
    visit(_x.ent());
383
7.93k
    break;
384
2.34k
  case EntityDecl::PEDECL:
385
2.34k
    m_output << "% " << _x.name() << " ";
386
2.34k
    visit(_x.pedef());
387
2.34k
    break;
388
0
  case EntityDecl_Type_EntityDecl_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
389
0
  case EntityDecl_Type_EntityDecl_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
390
0
    break;
391
10.2k
  }
392
10.2k
  m_output << ">";
393
10.2k
}
394
395
void ProtoConverter::visit(ConditionalSect const& _x)
396
9.93k
{
397
9.93k
  if (!isValid(_x))
398
197
    return;
399
400
9.73k
  switch (_x.type())
401
9.73k
  {
402
6.33k
  case ConditionalSect::INCLUDE:
403
6.33k
    m_output << "<![ INCLUDE [";
404
6.33k
    visit(_x.ext());
405
6.33k
    m_output << "]]>";
406
6.33k
    break;
407
1.18k
  case ConditionalSect::IGNORE:
408
1.18k
    m_output << "<![ IGNORE [";
409
1.18k
    for (auto const& str: _x.ignores())
410
1.63k
      m_output << "<![" << removeNonAscii(str) << "]]>";
411
1.18k
    m_output << "]]>";
412
1.18k
    break;
413
2.21k
  case ConditionalSect::FUZZ:
414
2.21k
    m_output << "<![ FUZZ [";
415
2.21k
    visit(_x.ext());
416
2.21k
    m_output << "]]>";
417
2.21k
    break;
418
0
  case ConditionalSect_Type_ConditionalSect_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
419
0
  case ConditionalSect_Type_ConditionalSect_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
420
0
    break;
421
9.73k
  }
422
9.73k
}
423
424
425
void ProtoConverter::visit(OneExtSubsetDecl const& _x)
426
42.0k
{
427
42.0k
  switch (_x.extsubset_oneof_case())
428
42.0k
  {
429
24.2k
  case OneExtSubsetDecl::kM:
430
24.2k
    visit(_x.m());
431
24.2k
    break;
432
9.93k
  case OneExtSubsetDecl::kC:
433
9.93k
    visit(_x.c());
434
9.93k
    break;
435
7.88k
  case OneExtSubsetDecl::EXTSUBSET_ONEOF_NOT_SET:
436
7.88k
    break;
437
42.0k
  }
438
42.0k
}
439
440
441
void ProtoConverter::visit(ExtSubsetDecl const& _x)
442
15.2k
{
443
15.2k
  for (auto const& decl: _x.decls())
444
42.0k
    visit(decl);
445
15.2k
}
446
447
void ProtoConverter::visit(CData const& _x)
448
2.18k
{
449
2.18k
  m_output << "<![CDATA[" << removeNonAscii(_x.data()) << "]]>";
450
2.18k
}
451
452
void ProtoConverter::visit(MarkupDecl const& _x)
453
40.6k
{
454
40.6k
  switch (_x.markup_oneof_case())
455
40.6k
  {
456
6.01k
  case MarkupDecl::kE:
457
6.01k
    visit(_x.e());
458
6.01k
    break;
459
4.17k
  case MarkupDecl::kA:
460
4.17k
    visit(_x.a());
461
4.17k
    break;
462
5.28k
  case MarkupDecl::kN:
463
5.28k
    visit(_x.n());
464
5.28k
    break;
465
2.42k
  case MarkupDecl::kM:
466
2.42k
    visit(_x.m());
467
2.42k
    break;
468
10.3k
  case MarkupDecl::kEntity:
469
10.3k
    visit(_x.entity());
470
10.3k
    break;
471
6.69k
  case MarkupDecl::kExt:
472
6.69k
    visit(_x.ext());
473
6.69k
    break;
474
5.69k
  case MarkupDecl::MARKUP_ONEOF_NOT_SET:
475
5.69k
    break;
476
40.6k
  }
477
40.6k
}
478
479
/// Returns predefined element from an Element_Id enum
480
/// @param _x is an enum that holds the desired type of predefined value
481
/// @param _prop is a string that holds the value of the desired type
482
/// @return string holding the predefined value of the form
483
/// name attribute=\"value\"
484
string ProtoConverter::getPredefined(Element_Id _x, string const& _prop)
485
201k
{
486
201k
  string output{};
487
201k
  switch (_x)
488
201k
  {
489
188k
  case Element::XIINCLUDE:
490
190k
  case Element::XIFALLBACK:
491
194k
  case Element::XIHREF:
492
194k
    output = "xi:include href=\"fuzz.xml\"";
493
195k
  case Element::XIPARSE:
494
195k
    output = "xi:include parse=\"xml\"";
495
196k
  case Element::XIXPOINTER:
496
196k
    output = "xi:include xpointer=\"" + removeNonAscii(_prop) + "\"";
497
198k
  case Element::XIENCODING:
498
198k
    output = "xi:include encoding=\"" + removeNonAscii(_prop) + "\"";
499
200k
  case Element::XIACCEPT:
500
200k
    output = "xi:include accept=\"" + removeNonAscii(_prop) + "\"";
501
201k
  case Element::XIACCEPTLANG:
502
201k
    output = "xi:include accept-language=\"" + removeNonAscii(_prop) + "\"";
503
201k
  case Element_Id_Element_Id_INT_MIN_SENTINEL_DO_NOT_USE_:
504
201k
  case Element_Id_Element_Id_INT_MAX_SENTINEL_DO_NOT_USE_:
505
201k
    output = "xi:fuzz xifuzz=\"fuzz\"";
506
201k
  }
507
201k
  return output;
508
201k
}
509
510
/// Returns uri string for a given Element_Id type
511
string ProtoConverter::getUri(Element_Id _x)
512
201k
{
513
201k
  if (!Element::Id_IsValid(_x))
514
347
    return s_XInclude;
515
516
201k
  switch (_x)
517
201k
  {
518
188k
  case Element::XIINCLUDE:
519
190k
  case Element::XIFALLBACK:
520
194k
  case Element::XIHREF:
521
195k
  case Element::XIPARSE:
522
196k
  case Element::XIXPOINTER:
523
198k
  case Element::XIENCODING:
524
200k
  case Element::XIACCEPT:
525
201k
  case Element::XIACCEPTLANG:
526
201k
  case Element_Id_Element_Id_INT_MIN_SENTINEL_DO_NOT_USE_:
527
201k
  case Element_Id_Element_Id_INT_MAX_SENTINEL_DO_NOT_USE_:
528
201k
    return s_XInclude;
529
201k
  }
530
201k
}
531
532
void ProtoConverter::visit(Element const& _x)
533
216k
{
534
216k
  if (!isValid(_x))
535
33
    return;
536
537
  // Predefined child node
538
216k
  string child = {};
539
  // Predefined uri for child node
540
216k
  string pUri = {};
541
  // Element name
542
216k
  string name = removeNonAscii(_x.name());
543
544
216k
  switch (_x.type())
545
216k
  {
546
201k
  case Element::PREDEFINED:
547
201k
    child = getPredefined(_x.id(), _x.childprop());
548
201k
    pUri = getUri(_x.id());
549
201k
    break;
550
14.6k
  case Element::FUZZ:
551
14.6k
  case Element_Type_Element_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
552
14.6k
  case Element_Type_Element_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
553
14.6k
    break;
554
216k
  }
555
556
  // <name k1=v1 k2=v2 k3=v3>
557
  // <content>
558
  // </name>
559
560
  // Start name tag: Must be Ascii?
561
216k
  m_output << "<" << name << " ";
562
563
  // Add uri to name tag
564
216k
  if (!pUri.empty())
565
201k
    m_output << pUri << " ";
566
216k
  for (auto const& prop: _x.kv())
567
19.7k
    visit(prop);
568
216k
  m_output << ">\n";
569
570
  // Add attribute
571
216k
  if (!child.empty())
572
201k
    m_output << "<" << child << "/>\n";
573
574
  // Add content
575
216k
  visit(_x.content());
576
577
  // Close name tag
578
216k
  m_output << "</" << name << ">\n";
579
216k
}
580
581
void ProtoConverter::visit(ExternalId const& _x)
582
13.9k
{
583
13.9k
  if (!isValid(_x))
584
959
    return;
585
586
13.0k
  switch (_x.type())
587
13.0k
  {
588
10.8k
  case ExternalId::SYSTEM:
589
10.8k
    m_output << "SYSTEM " << "\"" << removeNonAscii(_x.system()) << "\"";
590
10.8k
    break;
591
1.25k
  case ExternalId::PUBLIC:
592
1.25k
    m_output << "PUBLIC " << "\"" << removeNonAscii(_x.pub()) << "\""
593
1.25k
      << " " << "\"" << removeNonAscii(_x.system()) << "\"";
594
1.25k
    break;
595
930
  case ExternalId::FUZZ:
596
930
    m_output << "FUZZ " << "\"" << removeNonAscii(_x.pub()) << "\"";
597
930
    break;
598
0
  case ExternalId_Type_ExternalId_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
599
0
  case ExternalId_Type_ExternalId_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
600
0
    break;
601
13.0k
  }
602
13.0k
}
603
604
void ProtoConverter::visit(DocTypeDecl const& _x)
605
8.36k
{
606
8.36k
  m_output << "<!DOCTYPE " << removeNonAscii(_x.name()) << " ";
607
8.36k
  visit(_x.ext());
608
8.36k
  m_output << "[";
609
8.36k
  for (auto const& m: _x.mdecl())
610
16.3k
    visit(m);
611
8.36k
  m_output << "]";
612
8.36k
  m_output << ">\n";
613
8.36k
}
614
615
void ProtoConverter::visit(VersionNum const& _x)
616
8.36k
{
617
8.36k
  if (!isValid(_x))
618
3
    return;
619
620
8.36k
  switch (_x.type())
621
8.36k
  {
622
8.20k
  case VersionNum::STANDARD:
623
8.20k
    m_output << "\"1.0\"";
624
8.20k
    break;
625
154
  case VersionNum::FUZZ:
626
154
  case VersionNum_Type_VersionNum_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
627
154
  case VersionNum_Type_VersionNum_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
628
154
    m_output << "\"" << _x.major() << "." << _x.minor() << "\"";
629
154
    break;
630
8.36k
  }
631
8.36k
}
632
633
void ProtoConverter::visit(Encodings const& _x)
634
8.36k
{
635
8.36k
  if (!Encodings::Enc_IsValid(_x.name()))
636
623
    return;
637
638
7.74k
  m_output << " encoding=\"";
639
7.74k
  switch (_x.name())
640
7.74k
  {
641
6.80k
  case Encodings::BIG5:
642
6.80k
    m_output << "BIG5";
643
6.80k
    break;
644
10
  case Encodings::EUCJP:
645
10
    m_output << "EUC-JP";
646
10
    break;
647
38
  case Encodings::EUCKR:
648
38
    m_output << "EUC-KR";
649
38
    break;
650
11
  case Encodings::GB18030:
651
11
    m_output << "GB18030";
652
11
    break;
653
1
  case Encodings::ISO2022JP:
654
1
    m_output << "ISO-2022-JP";
655
1
    break;
656
127
  case Encodings::ISO2022KR:
657
127
    m_output << "ISO-2022-KR";
658
127
    break;
659
73
  case Encodings::ISO88591:
660
73
    m_output << "ISO-8859-1";
661
73
    break;
662
4
  case Encodings::ISO88592:
663
4
    m_output << "ISO-8859-2";
664
4
    break;
665
14
  case Encodings::ISO88593:
666
14
    m_output << "ISO-8859-3";
667
14
    break;
668
15
  case Encodings::ISO88594:
669
15
    m_output << "ISO-8859-4";
670
15
    break;
671
10
  case Encodings::ISO88595:
672
10
    m_output << "ISO-8859-5";
673
10
    break;
674
3
  case Encodings::ISO88596:
675
3
    m_output << "ISO-8859-6";
676
3
    break;
677
7
  case Encodings::ISO88597:
678
7
    m_output << "ISO-8859-7";
679
7
    break;
680
5
  case Encodings::ISO88598:
681
5
    m_output << "ISO-8859-8";
682
5
    break;
683
14
  case Encodings::ISO88599:
684
14
    m_output << "ISO-8859-9";
685
14
    break;
686
90
  case Encodings::SHIFTJIS:
687
90
    m_output << "SHIFT_JIS";
688
90
    break;
689
22
  case Encodings::TIS620:
690
22
    m_output << "TIS-620";
691
22
    break;
692
83
  case Encodings::USASCII:
693
83
    m_output << "US-ASCII";
694
83
    break;
695
82
  case Encodings::UTF8:
696
82
    m_output << "UTF-8";
697
82
    break;
698
59
  case Encodings::UTF16:
699
59
    m_output << "UTF-16";
700
59
    break;
701
16
  case Encodings::UTF16BE:
702
16
    m_output << "UTF-16BE";
703
16
    break;
704
15
  case Encodings::UTF16LE:
705
15
    m_output << "UTF-16LE";
706
15
    break;
707
62
  case Encodings::WINDOWS31J:
708
62
    m_output << "WINDOWS-31J";
709
62
    break;
710
4
  case Encodings::WINDOWS1255:
711
4
    m_output << "WINDOWS-1255";
712
4
    break;
713
10
  case Encodings::WINDOWS1256:
714
10
    m_output << "WINDOWS-1256";
715
10
    break;
716
164
  case Encodings::FUZZ:
717
164
    m_output << removeNonAscii(_x.fuzz());
718
164
    break;
719
0
  case Encodings_Enc_Encodings_Enc_INT_MIN_SENTINEL_DO_NOT_USE_:
720
0
  case Encodings_Enc_Encodings_Enc_INT_MAX_SENTINEL_DO_NOT_USE_:
721
0
    break;
722
7.74k
  }
723
7.74k
  m_output << "\"";
724
7.74k
}
725
726
void ProtoConverter::visit(XmlDeclaration const& _x)
727
8.36k
{
728
8.36k
  m_output << R"(<?xml version=)";
729
8.36k
  visit(_x.ver());
730
8.36k
  visit(_x.enc());
731
8.36k
  switch (_x.standalone())
732
8.36k
  {
733
8.04k
  case XmlDeclaration::YES:
734
8.04k
    m_output << " standalone=\'yes\'";
735
8.04k
    break;
736
268
  case XmlDeclaration::NO:
737
268
    m_output << " standalone=\'no\'";
738
268
    break;
739
0
  case XmlDeclaration_Standalone_XmlDeclaration_Standalone_INT_MIN_SENTINEL_DO_NOT_USE_:
740
0
  case XmlDeclaration_Standalone_XmlDeclaration_Standalone_INT_MAX_SENTINEL_DO_NOT_USE_:
741
54
  default:
742
54
    break;
743
8.36k
  }
744
8.36k
  m_output << "?>\n";
745
8.36k
}
746
747
void ProtoConverter::visit(XmlDocument const& _x)
748
8.36k
{
749
8.36k
  visit(_x.p());
750
8.36k
  for (auto const& element: _x.e())
751
212k
    visit(element);
752
8.36k
}
753
754
string ProtoConverter::protoToString(XmlDocument const& _x)
755
8.36k
{
756
8.36k
  visit(_x);
757
8.36k
  return m_output.str();
758
8.36k
}