Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/xml/PullParser.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 24 окт. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/io/InStringSequence.h>
23 #include <lsp-plug.in/io/InSequence.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25 #include <lsp-plug.in/fmt/xml/PullParser.h>
26 #include <ctype.h>
27 #include <wctype.h>
28
29 namespace lsp
30 {
31 namespace xml
32 {
33
34 PullParser::PullParser()
35 {
36 pIn = NULL;
37 nWFlags = 0;
38 nToken = -STATUS_NO_DATA;
39 nState = PS_READ_MISC;
40 enVersion = XML_VERSION_1_0;
41 nFlags = 0;
42
43 for (size_t i=0; i<4; ++i)
44 vUngetch[i] = 0;
45 nUngetch = 0;
46 for (size_t i=0; i<4; ++i)
47 vStates[i] = PS_END_DOCUMENT;
48 nStates = 0;
49 }
50
51 PullParser::~PullParser()
52 {
53 close();
54 }
55
56 status_t PullParser::open(const char *path, const char *charset)
57 {
58 if (pIn != NULL)
59 return STATUS_BAD_STATE;
60 else if (path == NULL)
61 return STATUS_BAD_ARGUMENTS;
62
63 io::InFileStream *ifs = new io::InFileStream();
64 if (ifs == NULL)
65 return STATUS_NO_MEM;
66 status_t res = ifs->open(path);
67 if (res == STATUS_OK)
68 {
69 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
70 if (res == STATUS_OK)
71 return res;
72 ifs->close();
73 }
74 delete ifs;
75
76 return res;
77 }
78
79 status_t PullParser::open(const LSPString *path, const char *charset)
80 {
81 if (pIn != NULL)
82 return STATUS_BAD_STATE;
83 else if (path == NULL)
84 return STATUS_BAD_ARGUMENTS;
85
86 io::InFileStream *ifs = new io::InFileStream();
87 if (ifs == NULL)
88 return STATUS_NO_MEM;
89 status_t res = ifs->open(path);
90 if (res == STATUS_OK)
91 {
92 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
93 if (res == STATUS_OK)
94 return res;
95 ifs->close();
96 }
97 delete ifs;
98
99 return res;
100 }
101
102 status_t PullParser::open(const io::Path *path, const char *charset)
103 {
104 if (pIn != NULL)
105 return STATUS_BAD_STATE;
106 else if (path == NULL)
107 return STATUS_BAD_ARGUMENTS;
108
109 io::InFileStream *ifs = new io::InFileStream();
110 if (ifs == NULL)
111 return STATUS_NO_MEM;
112 status_t res = ifs->open(path);
113 if (res == STATUS_OK)
114 {
115 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
116 if (res == STATUS_OK)
117 return res;
118 ifs->close();
119 }
120 delete ifs;
121
122 return res;
123 }
124
125 status_t PullParser::wrap(const char *str, const char *charset)
126 {
127 if (pIn != NULL)
128 return STATUS_BAD_STATE;
129 else if (str == NULL)
130 return STATUS_BAD_ARGUMENTS;
131
132 io::InStringSequence *seq = new io::InStringSequence();
133 if (seq == NULL)
134 return STATUS_NO_MEM;
135
136 status_t res = seq->wrap(str, charset);
137 if (res == STATUS_OK)
138 {
139 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
140 return res;
141 seq->close();
142 }
143
144 delete seq;
145 return res;
146 }
147
148 status_t PullParser::wrap(const LSPString *str)
149 {
150 if (pIn != NULL)
151 return STATUS_BAD_STATE;
152 else if (str == NULL)
153 return STATUS_BAD_ARGUMENTS;
154
155 io::InStringSequence *seq = new io::InStringSequence();
156 if (seq == NULL)
157 return STATUS_NO_MEM;
158
159 status_t res = seq->wrap(str);
160 if (res == STATUS_OK)
161 {
162 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
163 return res;
164 seq->close();
165 }
166
167 delete seq;
168 return res;
169 }
170
171 status_t PullParser::wrap(io::IInStream *is, size_t flags, const char *charset)
172 {
173 if (pIn != NULL)
174 return STATUS_BAD_STATE;
175 else if (is == NULL)
176 return STATUS_BAD_ARGUMENTS;
177
178 io::InSequence *seq = new io::InSequence();
179 if (seq == NULL)
180 return STATUS_NO_MEM;
181
182 status_t res = seq->wrap(is, flags, charset);
183 if (res == STATUS_OK)
184 {
185 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
186 return res;
187 seq->close();
188 }
189
190 delete seq;
191 return res;
192 }
193
194 status_t PullParser::wrap(io::IInSequence *seq, size_t flags)
195 {
196 if (pIn != NULL)
197 return STATUS_BAD_STATE;
198 else if (seq == NULL)
199 return STATUS_BAD_ARGUMENTS;
200
201 pIn = seq;
202 nWFlags = flags;
203 nToken = -STATUS_NO_DATA;
204 nState = PS_READ_MISC;
205 nStates = 0;
206 enVersion = XML_VERSION_1_0;
207 sVersion.truncate();
208 sEncoding.truncate();
209 sDoctype.truncate();
210 sPublic.truncate();
211 sSystem.truncate();
212 nFlags = 0;
213 nUngetch = 0;
214
215 return STATUS_OK;
216 }
217
218 status_t PullParser::close()
219 {
220 status_t res = STATUS_OK;
221
222 // Drop unnecessary resources
223 nUngetch = 0;
224 sVersion.truncate();
225 sEncoding.truncate();
226 sName.truncate();
227 sValue.truncate();
228 sDoctype.truncate();
229 sPublic.truncate();
230 sSystem.truncate();
231 nFlags = 0;
232
233 // Remove all tag hierarchy
234 drop_list(&vTags);
235 drop_list(&vAtts);
236
237 // Release input sequence
238 if (pIn != NULL)
239 {
240 if (nWFlags & WRAP_CLOSE)
241 res = pIn->close();
242
243 if (nWFlags & WRAP_DELETE)
244 delete pIn;
245
246 pIn = NULL;
247 }
248
249 return res;
250 }
251
252 lsp_swchar_t PullParser::getch()
253 {
254 return (nUngetch > 0) ? vUngetch[--nUngetch] : pIn->read();
255 }
256
257 void PullParser::ungetch(lsp_swchar_t ch)
258 {
259 vUngetch[nUngetch++] = ch;
260 }
261
262 void PullParser::push_state(parse_state_t override)
263 {
264 vStates[nStates++] = nState;
265 nState = override;
266 }
267
268 void PullParser::pop_state()
269 {
270 nState = vStates[--nStates];
271 }
272
273 void PullParser::drop_list(lltl::parray<LSPString> *list)
274 {
275 for (size_t i=0, n=list->size(); i<n; ++i)
276 {
277 LSPString *s = list->uget(i);
278 if (s != NULL)
279 delete s;
280 }
281 list->flush();
282 }
283
284 status_t PullParser::check_duplicate_attribute()
285 {
286 // Is item present in list?
287 for (size_t i=0, n=vAtts.size(); i<n; ++i)
288 {
289 LSPString *s = vAtts.uget(i);
290 if ((s != NULL) && (s->equals(&sName)))
291 return STATUS_CORRUPTED;
292 }
293
294 // Add to list
295 LSPString *copy = sName.clone();
296 if (copy == NULL)
297 return STATUS_NO_MEM;
298 if (!vAtts.add(copy))
299 {
300 delete copy;
301 return STATUS_NO_MEM;
302 }
303
304 return STATUS_OK;
305 }
306
307 bool PullParser::skip_spaces()
308 {
309 bool skipped = false;
310
311 while (true)
312 {
313 // Read next character
314 lsp_swchar_t c = getch();
315 if (!is_whitespace(c))
316 {
317 ungetch(c);
318 break;
319 }
320 skipped = true;
321 }
322
323 return skipped;
324 }
325
326 status_t PullParser::read_text(const char *text)
327 {
328 lsp_swchar_t c;
329 for ( ; *text != '\0'; ++text)
330 {
331 if ((c = getch()) != *text)
332 return (c < 0) ? -c : STATUS_CORRUPTED;
333 }
334 return STATUS_OK;
335 }
336
337 status_t PullParser::read_name(LSPString *name)
338 {
339 // Get first character
340 lsp_swchar_t c = getch();
341 if (!(is_name_first(c)))
342 return (c < 0) ? -c : STATUS_CORRUPTED;
343
344 // Read name
345 name->clear();
346 do
347 {
348 // Append current character
349 if (!name->append(c))
350 return STATUS_NO_MEM;
351
352 // Get next character
353 c = getch();
354 } while (is_name_next(c));
355
356 // Return back last character and return OK status
357 ungetch(c);
358 return STATUS_OK;
359 }
360
361 status_t PullParser::read_attribute_value(lsp_swchar_t qc)
362 {
363 lsp_swchar_t c;
364 status_t res;
365
366 while (true)
367 {
368 // Read character
369 if ((c = getch()) < 0)
370 {
371 pop_state();
372 return -c;
373 }
374 else if (c == qc)
375 break;
376
377 // Reference?
378 if (c == '&')
379 {
380 // Read and append reference (if possible) to the string value
381 if ((res = read_entity_reference(&sValue)) != STATUS_OK)
382 {
383 pop_state();
384 return res;
385 }
386
387 // Need to query reference?
388 if (nState != PS_READ_REFERENCE)
389 continue;
390 return STATUS_OK; // Query for reference, do not need to pop_state()
391 }
392
393 // Append current character
394 if (!sValue.append(c))
395 {
396 pop_state();
397 return STATUS_NO_MEM;
398 }
399 }
400
401 pop_state();
402 nToken = XT_ATTRIBUTE;
403 return STATUS_OK;
404 }
405
406 status_t PullParser::read_version()
407 {
408 // Get quote character
409 lsp_swchar_t qc = getch();
410 if ((qc != '\'') && (qc != '\"'))
411 return (qc < 0) ? -qc : STATUS_CORRUPTED;
412
413 // Version should be '1.x'
414 lsp_swchar_t c;
415 if ((c = getch()) != '1')
416 return (c < 0) ? -c : STATUS_CORRUPTED;
417 if ((c = getch()) != '.')
418 return (c < 0) ? -c : STATUS_CORRUPTED;
419
420 // Read integer value
421 size_t v=0, k=0;
422 while ((c = getch()) != qc)
423 {
424 if (v >= 0x1000000) // Prevent from integer overflow
425 return STATUS_CORRUPTED;
426
427 if ((c >= '0') && (c <= '9'))
428 v = v * 10 + (c - '0');
429 else
430 return (c < 0) ? -c : STATUS_CORRUPTED;
431 ++k;
432 }
433
434 // Validate number of digits
435 if (k <= 0)
436 return STATUS_CORRUPTED;
437
438 // Update version text
439 if (!sVersion.fmt_ascii("1.%d", int(v)))
440 return STATUS_NO_MEM;
441
442 enVersion = (v >= 1) ? XML_VERSION_1_1 : XML_VERSION_1_0;
443 nFlags |= XF_VERSION;
444
445 return STATUS_OK;
446 }
447
448 status_t PullParser::read_encoding()
449 {
450 sEncoding.clear();
451
452 // Get quote character
453 lsp_swchar_t qc = getch();
454 if ((qc != '\'') && (qc != '\"'))
455 return (qc < 0) ? -qc : STATUS_CORRUPTED;
456
457 // Read encoding char
458 lsp_swchar_t c = getch();
459 if (!is_encoding_first(c))
460 return STATUS_BAD_FORMAT;
461 if (!sEncoding.append(c))
462 return STATUS_NO_MEM;
463
464 // Check the remained characters
465 while ((c = getch()) != qc)
466 {
467 if (!is_encoding_next(c))
468 return (c < 0) ? -c : STATUS_CORRUPTED;
469 if (!sEncoding.append(c))
470 return STATUS_NO_MEM;
471 }
472
473 nFlags |= XF_ENCODING;
474
475 return STATUS_OK;
476 }
477
478 status_t PullParser::read_standalone()
479 {
480 LSPString tmp;
481
482 // Get quote character
483 lsp_swchar_t qc = getch();
484 if ((qc != '\'') && (qc != '\"'))
485 return (qc < 0) ? -qc : STATUS_CORRUPTED;
486
487 // Read quoted string
488 lsp_swchar_t c;
489 while ((c = getch()) != qc)
490 {
491 if (tmp.length() >= 3)
492 return STATUS_CORRUPTED;
493 if (!tmp.append(c))
494 return STATUS_NO_MEM;
495 }
496
497 // Compare string with possible value
498 if (tmp.equals_ascii("yes"))
499 nFlags |= XF_STANDALONE;
500 else if (tmp.equals_ascii("no"))
501 nFlags &= ~XF_STANDALONE;
502 else
503 return STATUS_CORRUPTED;
504
505 return STATUS_OK;
506 }
507
508 status_t PullParser::read_header()
509 {
510 status_t res;
511 lsp_swchar_t c;
512
513 // Fetch optional attributes
514 enum flags_t
515 {
516 F_VERSION = 1 << 0,
517 F_ENCODING = 1 << 1,
518 F_STANDALONE = 1 << 2
519 };
520
521 size_t flags = 0;
522 LSPString name, value;
523
524 while (true)
525 {
526 // Skip spaces and read next character
527 bool skipped = skip_spaces();
528 if ((c = getch()) < 0)
529 return -c;
530
531 if (c == '?') // end of header?
532 {
533 // Read next character
534 if ((c = getch()) != '>')
535 return (c < 0) ? -c : STATUS_CORRUPTED;
536 return (flags & F_VERSION) ? read_start_document() : STATUS_CORRUPTED;
537 }
538
539 // At least one space is mandatory
540 if (!skipped)
541 return STATUS_CORRUPTED;
542
543 // Read attribute name
544 ungetch(c);
545 if ((res = read_name(&name)) != STATUS_OK)
546 return res;
547
548 // Required '=' sign
549 skip_spaces();
550 if ((c = getch()) != '=')
551 return (c < 0) ? -c : STATUS_CORRUPTED;
552
553 // Check attribute type
554 size_t flag = 0;
555 if (name.equals_ascii("version"))
556 {
557 flag = F_VERSION;
558 if ((res = read_version()) != STATUS_OK)
559 return res;
560 }
561 else if (name.equals_ascii("encoding"))
562 {
563 flag = F_ENCODING;
564 if ((res = read_encoding()) != STATUS_OK)
565 return res;
566 }
567 else if (name.equals_ascii("standalone"))
568 {
569 flag = F_STANDALONE;
570 if ((res = read_standalone()) != STATUS_OK)
571 return res;
572 }
573
574 // Check that attribute is at proper place
575 if (flag <= flags)
576 return STATUS_CORRUPTED;
577 flags |= flag;
578 }
579 }
580
581 status_t PullParser::read_comment()
582 {
583 lsp_swchar_t c, xc;
584 sValue.clear();
585
586 while (true)
587 {
588 // Fetch new character
589 if ((c = getch()) < 0)
590 return -c;
591
592 // Going to end of comment?
593 if (c == '-')
594 {
595 // End of comment?
596 if ((xc = getch()) == '-')
597 {
598 // Next character should be '>'
599 if ((xc = getch()) != '>')
600 return (xc < 0) ? -xc : STATUS_CORRUPTED;
601
602 nToken = XT_COMMENT;
603 return STATUS_OK;
604 }
605
606 // Return character back
607 ungetch(xc);
608 }
609
610 if (!sValue.append(c))
611 return STATUS_NO_MEM;
612 }
613
614 return STATUS_OK;
615 }
616
617 status_t PullParser::read_processing_instruction()
618 {
619 status_t res;
620
621 // Read processing instruction name
622 if ((res = read_name(&sName)) != STATUS_OK)
623 return res;
624
625 if (sName.equals_ascii_nocase("xml"))
626 {
627 if (nFlags & XF_HEADER)
628 return STATUS_CORRUPTED; // XML processing instruction is prohibited
629 return read_header();
630 }
631
632 // Read processing instruction value
633 lsp_swchar_t c;
634 skip_spaces(); // Skip spaces
635
636 sValue.clear();
637 while (true)
638 {
639 // Fetch new character
640 if ((c = getch()) < 0)
641 return -c;
642
643 // PI end?
644 if (c == '>')
645 {
646 ssize_t pos = sValue.length() - 1;
647 if ((pos >= 0) && (sValue.char_at(pos) == '?'))
648 break;
649 }
650
651 // No, simple character
652 if (!sValue.append(c))
653 return STATUS_NO_MEM;
654 }
655
656 // Remove last character which is '?'
657 sValue.set_length(sValue.length() - 1);
658
659 nToken = XT_PROCESSING_INSTRUCTION;
660 return STATUS_OK;
661 }
662
663 status_t PullParser::read_system_literal(LSPString *dst)
664 {
665 LSPString tmp;
666
667 // Get quote character
668 lsp_swchar_t qc = getch();
669 if ((qc != '\'') && (qc != '\"'))
670 return (qc < 0) ? -qc : STATUS_CORRUPTED;
671
672 // Read quoted string
673 lsp_swchar_t c;
674 while ((c = getch()) != qc)
675 {
676 if (!tmp.append(c))
677 return STATUS_NO_MEM;
678 }
679
680 dst->swap(&tmp);
681 return STATUS_OK;
682 }
683
684 status_t PullParser::read_pubid_literal(LSPString *dst)
685 {
686 LSPString tmp;
687
688 // Get quote character
689 lsp_swchar_t qc = getch();
690 if ((qc != '\'') && (qc != '\"'))
691 return (qc < 0) ? -qc : STATUS_CORRUPTED;
692
693 // Read quoted string
694 lsp_swchar_t c;
695 while ((c = getch()) != qc)
696 {
697 if ((!is_pubid_char(c)) || (c == qc))
698 return STATUS_CORRUPTED;
699 if (!tmp.append(c))
700 return STATUS_NO_MEM;
701 }
702
703 dst->swap(&tmp);
704 return STATUS_OK;
705 }
706
707 status_t PullParser::read_doctype()
708 {
709 status_t res;
710 lsp_swchar_t c;
711 LSPString x;
712
713 // Duplicate DOCTYPE?
714 if (nFlags & XF_DOCTYPE)
715 return STATUS_CORRUPTED;
716
717 // Space is required
718 if (!skip_spaces())
719 return STATUS_CORRUPTED;
720 if ((res = read_name(&sDoctype)) != STATUS_OK)
721 return res;
722
723 // Watch next token
724 nFlags |= XF_DOCTYPE;
725 bool skip = skip_spaces();
726 if ((c = getch()) < 0)
727 return -c;
728
729 // ExternalID is present?
730 if (c == 'P')
731 {
732 if (!skip)
733 return STATUS_CORRUPTED;
734 if ((res = read_text("UBLIC")) != STATUS_OK)
735 return res;
736 if (!skip_spaces())
737 return STATUS_CORRUPTED;
738 if ((res = read_pubid_literal(&sPublic)) != STATUS_OK)
739 return res;
740 nFlags |= XF_DOCTYPE_PUB;
741 if (!skip_spaces())
742 return STATUS_CORRUPTED;
743 if ((res = read_system_literal(&sSystem)) != STATUS_OK)
744 return res;
745 nFlags |= XF_DOCTYPE_SYS;
746
747 // Skip spaces and get next token
748 skip_spaces();
749 if ((c = getch()) < 0)
750 return -c;
751 }
752 else if (c == 'S')
753 {
754 if (!skip)
755 return STATUS_CORRUPTED;
756 if ((res = read_text("YSTEM")) != STATUS_OK)
757 return res;
758 if (!skip_spaces())
759 return STATUS_CORRUPTED;
760 if ((res = read_system_literal(&sSystem)) != STATUS_OK)
761 return res;
762 nFlags |= XF_DOCTYPE_SYS;
763
764 // Skip spaces and get next token
765 skip_spaces();
766 if ((c = getch()) < 0)
767 return -c;
768 }
769
770 // intSubset?
771 if (c == '[')
772 {
773 // TODO: currently we don't support DOCTYPE definition with built-in doctypes
774 return STATUS_NOT_IMPLEMENTED;
775 }
776
777 // End of Doctype?
778 nToken = XT_DTD;
779 return (c == '>') ? STATUS_OK : STATUS_CORRUPTED;
780 }
781
782 status_t PullParser::read_start_document()
783 {
784 nToken = XT_START_DOCUMENT;
785 nFlags |= XF_HEADER;
786 return STATUS_OK;
787 }
788
789 status_t PullParser::read_end_document()
790 {
791 nToken = XT_END_DOCUMENT;
792 nState = PS_END_DOCUMENT;
793 return STATUS_OK;
794 }
795
796 status_t PullParser::read_misc()
797 {
798 status_t res;
799 lsp_swchar_t c;
800
801 // Skip whitespace
802 if (!(nFlags & XF_HEADER))
803 {
804 if (skip_spaces())
805 return read_start_document();
806 }
807 else
808 skip_spaces();
809
810 // Next character should be '<'
811 if ((c = getch()) != '<')
812 {
813 if (c == -STATUS_EOF)
814 return (nFlags & XF_HEADER) ? read_end_document() : read_start_document();
815 return (c < 0) ? -c : STATUS_CORRUPTED;
816 }
817
818 // Get the following character
819 if ((c = getch()) < 0)
820 return -c;
821
822 // Processing instruction?
823 if (c == '?')
824 return read_processing_instruction();
825 else if (!(nFlags & XF_HEADER))
826 {
827 ungetch(c);
828 ungetch('<');
829 return read_start_document();
830 }
831
832 // Comment or Doctype?
833 if (c == '!')
834 {
835 // Get next character
836 if ((c = getch()) < 0)
837 return -c;
838
839 if (c == '-') // Comment?
840 {
841 // '<!--' should be parsed
842 if ((c = getch()) != '-')
843 return (c < 0) ? -c : STATUS_CORRUPTED;
844 return read_comment();
845 }
846
847 if (c == 'D') // Doctype?
848 {
849 // 'DOCTYPE' should be parsed
850 if ((res = read_text("OCTYPE")) != STATUS_OK)
851 return res;
852 return read_doctype();
853 }
854
855 return STATUS_CORRUPTED;
856 }
857
858 // We already have root tag?
859 if (nFlags & XF_ROOT)
860 return STATUS_CORRUPTED;
861 nFlags |= XF_ROOT; // Now we already have root tag defined
862
863 // Return character and read root tag name
864 ungetch(c);
865 return read_tag_open();
866 }
867
868 status_t PullParser::read_cdata()
869 {
870 lsp_swchar_t c;
871
872 sValue.clear();
873
874 while (true)
875 {
876 // Get next character
877 if ((c = getch()) < 0)
878 return -c;
879
880 // CDATA end?
881 if (c == '>')
882 {
883 ssize_t pos = sValue.length() - 2;
884 if (
885 (pos >= 0) &&
886 (sValue.char_at(pos) == ']') &&
887 (sValue.char_at(pos+1) == ']')
888 )
889 break;
890 }
891
892 // No, simple character
893 if (!sValue.append(c))
894 return STATUS_NO_MEM;
895 }
896
897 // Remove last two characters which are ']]'
898 sValue.set_length(sValue.length() - 2);
899
900 nToken = XT_CDATA;
901 return STATUS_OK;
902 }
903
904 status_t PullParser::read_tag_open()
905 {
906 status_t res;
907 if ((res = read_name(&sName)) != STATUS_OK)
908 return res;
909
910 // Add tag to stack
911 LSPString *tag = sName.clone();
912 if (tag == NULL)
913 return STATUS_NO_MEM;
914 else if (!vTags.push(tag))
915 {
916 delete tag;
917 return STATUS_NO_MEM;
918 }
919
920 // Change state
921 drop_list(&vAtts);
922 nToken = XT_START_ELEMENT;
923 nState = PS_READ_ATTRIBUTES;
924 return STATUS_OK;
925 }
926
927 status_t PullParser::read_tag_close(bool copy)
928 {
929 // Get last tag name
930 LSPString *name = NULL;
931 if (!vTags.pop(&name))
932 return STATUS_CORRUPTED;
933
934 if (copy)
935 sName.swap(name);
936 else if (!sName.equals(name))
937 {
938 delete name;
939 return STATUS_CORRUPTED;
940 }
941 delete name;
942
943 // Update state
944 drop_list(&vAtts);
945 nToken = XT_END_ELEMENT;
946 nState = (vTags.size() > 0) ? PS_READ_ELEMENT_DATA : PS_READ_MISC;
947 return STATUS_OK;
948 }
949
950 status_t PullParser::read_entity_reference(LSPString *cdata)
951 {
952 lsp_swchar_t c, code = 0;
953 status_t res;
954
955 // Get character
956 if ((c = getch()) < 0)
957 return -c;
958
959 // Entity reference ?
960 if (c != '#')
961 {
962 ungetch(c);
963
964 // Read entity name
965 if ((res = read_name(&sRefName)) != STATUS_OK)
966 return res;
967
968 if (sRefName.equals_ascii("amp"))
969 code = '&';
970 else if (sRefName.equals_ascii("gt"))
971 code = '>';
972 else if (sRefName.equals_ascii("lt"))
973 code = '<';
974 else if (sRefName.equals_ascii("apos"))
975 code = '\'';
976 else if (sRefName.equals_ascii("quot"))
977 code = '\"';
978
979 // Get next character which should be ';'
980 if ((c = getch()) < 0)
981 return -c;
982 }
983 else
984 {
985 // Get next character
986 if ((c = getch()) < 0)
987 return -c;
988
989 // Hexadecimal character?
990 if (c == 'x')
991 {
992 // Read hex digit
993 while ((c = getch()) >= 0)
994 {
995 // Protect from integer overflow
996 if (code >= 0x1000000)
997 return STATUS_CORRUPTED;
998
999 // Decode hex character
1000 if ((c >= '0') && (c <= '9'))
1001 code = (code << 4) | (c - '0');
1002 else if ((c >= 'a') && (c <= 'f'))
1003 code = (code << 4) | (c - 'a' + 10);
1004 else if ((c >= 'A') && (c <= 'F'))
1005 code = (code << 4) | (c - 'A' + 10);
1006 else
1007 break;
1008 }
1009 }
1010 else // Decimal character?
1011 {
1012 do
1013 {
1014 // Protect from integer overflow
1015 if (code >= 0x1000000)
1016 return STATUS_CORRUPTED;
1017
1018 // Decode decimal character
1019 if ((c >= '0') && (c <= '9'))
1020 code = (code * 10) + (c - '0');
1021 else
1022 break;
1023 } while ((c = getch()) >= 0);
1024 }
1025
1026 // Validate character
1027 if (!is_valid_char(code, enVersion))
1028 return STATUS_CORRUPTED;
1029 }
1030
1031 // Current character should be ';'
1032 if (c != ';')
1033 return STATUS_CORRUPTED;
1034 else if (code == 0)
1035 {
1036 push_state(PS_READ_REFERENCE);
1037 nToken = XT_ENTITY_RESOLVE;
1038 return STATUS_OK;
1039 }
1040
1041 // Append fetched character to the character data and exit
1042 return (cdata->append(code)) ? STATUS_OK : STATUS_NO_MEM;
1043 }
1044
1045 status_t PullParser::read_characters()
1046 {
1047 lsp_swchar_t c;
1048 status_t res;
1049
1050 while (true)
1051 {
1052 // Get next character
1053 if ((c = getch()) < 0)
1054 {
1055 pop_state();
1056 return -c;
1057 }
1058
1059 // Start of tag?
1060 if (c == '<')
1061 {
1062 ungetch(c);
1063 break;
1064 }
1065
1066 // Reference?
1067 if (c == '&')
1068 {
1069 // Read and append reference (if possible) to the string value
1070 if ((res = read_entity_reference(&sValue)) != STATUS_OK)
1071 {
1072 pop_state();
1073 return res;
1074 }
1075
1076 // Need to query reference?
1077 if (nState != PS_READ_REFERENCE)
1078 continue;
1079 return STATUS_OK; // Query for reference, do not need to pop_state()
1080 }
1081
1082 // CDATA end?
1083 if (c == '>')
1084 {
1085 ssize_t pos = sValue.length() - 2;
1086 if (
1087 (pos >= 0) &&
1088 (sValue.char_at(pos) == ']') &&
1089 (sValue.char_at(pos+1) == ']')
1090 )
1091 {
1092 pop_state();
1093 return STATUS_CORRUPTED;
1094 }
1095 }
1096
1097 // No, simple character
1098 if (!sValue.append(c))
1099 {
1100 pop_state();
1101 return STATUS_NO_MEM;
1102 }
1103 }
1104
1105 // Ensure that there is character data
1106 pop_state();
1107
1108 if (sValue.length() <= 0)
1109 return STATUS_CORRUPTED;
1110
1111 nToken = XT_CHARACTERS;
1112 return STATUS_OK;
1113 }
1114
1115 status_t PullParser::read_tag_content()
1116 {
1117 lsp_swchar_t c;
1118 status_t res;
1119
1120 // Read character
1121 if ((c = getch()) < 0)
1122 return -c;
1123
1124 // Tag? Processing instruction? End of tag? Comment? CDATA?
1125 if (c != '<')
1126 {
1127 ungetch(c);
1128 sValue.clear();
1129 push_state(PS_READ_CHARACTERS);
1130 return read_characters();
1131 }
1132
1133 // Get next character
1134 if ((c = getch()) < 0)
1135 return -c;
1136
1137 // Read tag name
1138 if (c == '/') // End of tag ?
1139 {
1140 // Read tag name
1141 if ((res = read_name(&sName)) != STATUS_OK)
1142 return res;
1143
1144 // '>' is required
1145 skip_spaces();
1146 if ((c = getch()) != '>')
1147 return (c < 0) ? -c : STATUS_CORRUPTED;
1148
1149 return read_tag_close(false);
1150 }
1151 else if (c == '?') // Processing instruction ?
1152 return read_processing_instruction();
1153 else if (c == '!') // Comment? CDATA?
1154 {
1155 // Get next character
1156 if ((c = getch()) < 0)
1157 return -c;
1158
1159 // CDATA?
1160 if (c == '[')
1161 {
1162 // Lookup CDATA start
1163 if ((res = read_text("CDATA[")) != STATUS_OK)
1164 return res;
1165 return read_cdata();
1166 }
1167
1168 // Comment?
1169 if (c == '-')
1170 {
1171 // Next character is required to be '-'
1172 if ((c = getch()) != '-')
1173 return (c < 0) ? -c : STATUS_CORRUPTED;
1174 return read_comment();
1175 }
1176
1177 // No match
1178 return STATUS_CORRUPTED;
1179 }
1180
1181 // Just open tag name?
1182 ungetch(c);
1183 return read_tag_open();
1184 }
1185
1186 status_t PullParser::read_tag_attribute()
1187 {
1188 lsp_swchar_t c;
1189 status_t res;
1190
1191 // Ignore set of spaces if they are present
1192 bool skipped = skip_spaces();
1193 if ((c = getch()) < 0)
1194 return -c;
1195
1196 // End of tag header?
1197 if (c == '>')
1198 {
1199 nState = PS_READ_ELEMENT_DATA;
1200 return read_tag_content();
1201 }
1202
1203 // End of tag?
1204 if (c == '/')
1205 {
1206 // Required character
1207 if ((c = getch()) != '>')
1208 return (c < 0) ? -c : STATUS_CORRUPTED;
1209
1210 return read_tag_close(true);
1211 }
1212
1213 // Try to read attribute and preprocess it's value
1214 if (!skipped) // At least one space is mandatory
1215 return STATUS_CORRUPTED;
1216
1217 // Read attribute name
1218 ungetch(c);
1219 if ((res = read_name(&sName)) != STATUS_OK)
1220 return res;
1221 else if (check_duplicate_attribute())
1222 return STATUS_CORRUPTED;
1223
1224 skip_spaces(); // Spaces are optional
1225 if ((c = getch()) != '=')
1226 return STATUS_CORRUPTED;
1227
1228 skip_spaces(); // Spaces are optional
1229 c = getch(); // Get quote character
1230 if ((c != '\'') && (c != '\"'))
1231 return (c < 0) ? -c : STATUS_CORRUPTED;
1232
1233 // Read quoted value
1234 sValue.clear();
1235 push_state((c == '\'') ? PS_READ_SQ_ATTRIBUTE : PS_READ_DQ_ATTRIBUTE);
1236 return read_attribute_value(c);
1237 }
1238
1239 status_t PullParser::read_token()
1240 {
1241 if (pIn == NULL)
1242 return STATUS_BAD_STATE;
1243
1244 switch (nState)
1245 {
1246 case PS_END_DOCUMENT:
1247 nToken = XT_END_DOCUMENT;
1248 return STATUS_EOF;
1249
1250 case PS_READ_MISC:
1251 return read_misc();
1252
1253 case PS_READ_ATTRIBUTES:
1254 return read_tag_attribute();
1255
1256 case PS_READ_ELEMENT_DATA:
1257 return read_tag_content();
1258
1259 case PS_READ_REFERENCE:
1260 nToken = XT_ENTITY_RESOLVE;
1261 return STATUS_OK;
1262
1263 case PS_READ_CHARACTERS:
1264 return read_characters();
1265
1266 case PS_READ_SQ_ATTRIBUTE:
1267 return read_attribute_value('\'');
1268
1269 case PS_READ_DQ_ATTRIBUTE:
1270 return read_attribute_value('\"');
1271
1272 default:
1273 break;
1274 }
1275 return STATUS_CORRUPTED;
1276 }
1277
1278 status_t PullParser::set_value(const LSPString *value)
1279 {
1280 if (pIn == NULL)
1281 return STATUS_BAD_STATE;
1282 else if (value == NULL)
1283 return STATUS_BAD_ARGUMENTS;
1284
1285 if (nState != PS_READ_REFERENCE)
1286 return STATUS_BAD_STATE;
1287
1288 // Append value with entity content
1289 if (!sValue.append(value))
1290 return STATUS_NO_MEM;
1291
1292 pop_state();
1293 return STATUS_OK;
1294 }
1295
1296 status_t PullParser::set_value(const char *value, const char *charset)
1297 {
1298 LSPString tmp;
1299 if (!tmp.set_native(value, charset))
1300 return STATUS_NO_MEM;
1301 return set_value(value);
1302 }
1303
1304 status_t PullParser::read_next()
1305 {
1306 status_t res = read_token();
1307 return (res == STATUS_OK) ? nToken : -res;
1308 }
1309
1310 status_t PullParser::get_current()
1311 {
1312 return nToken;
1313 }
1314
1315 const LSPString *PullParser::name() const
1316 {
1317 if (pIn == NULL)
1318 return NULL;
1319
1320 switch (nToken)
1321 {
1322 case XT_ATTRIBUTE:
1323 case XT_PROCESSING_INSTRUCTION:
1324 case XT_START_ELEMENT:
1325 case XT_END_ELEMENT:
1326 return &sName;
1327 case XT_ENTITY_RESOLVE:
1328 return &sRefName;
1329 default:
1330 break;
1331 }
1332 return NULL;
1333 }
1334
1335 const LSPString *PullParser::value() const
1336 {
1337 if (pIn == NULL)
1338 return NULL;
1339
1340 switch (nToken)
1341 {
1342 case XT_ATTRIBUTE:
1343 case XT_CDATA:
1344 case XT_CHARACTERS:
1345 case XT_COMMENT:
1346 case XT_PROCESSING_INSTRUCTION:
1347 return &sValue;
1348 default:
1349 break;
1350 }
1351 return NULL;
1352 }
1353
1354 } /* namespace xml */
1355 } /* namespace lsp */