Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 29 янв. 2023 г.
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/common/debug.h>
23 #include <lsp-plug.in/fmt/sfz/PullParser.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25 #include <lsp-plug.in/io/InMemoryStream.h>
26 #include <lsp-plug.in/io/InStringSequence.h>
27 #include <lsp-plug.in/io/InSequence.h>
28 #include <lsp-plug.in/io/OutMemoryStream.h>
29
30 namespace lsp
31 {
32 namespace sfz
33 {
34
35 PullParser::PullParser()
36 {
37 pIn = NULL;
38 nWFlags = 0;
39 sCurrent.type = EVENT_NONE;
40 sSample.type = EVENT_NONE;
41 nUnget = 0;
42 }
43
44 PullParser::~PullParser()
45 {
46 close();
47 }
48
49 status_t PullParser::open(const char *path)
50 {
51 if (pIn != NULL)
52 return STATUS_OPENED;
53
54 io::InFileStream *ifs = new io::InFileStream();
55 if (ifs == NULL)
56 return STATUS_NO_MEM;
57
58 status_t res = ifs->open(path);
59 if (res == STATUS_OK)
60 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE);
61 if (res != STATUS_OK)
62 {
63 ifs->close();
64 delete ifs;
65 }
66
67 return res;
68 }
69
70 status_t PullParser::open(const LSPString *path)
71 {
72 if (pIn != NULL)
73 return STATUS_OPENED;
74
75 io::InFileStream *ifs = new io::InFileStream();
76 if (ifs == NULL)
77 return STATUS_NO_MEM;
78
79 status_t res = ifs->open(path);
80 if (res == STATUS_OK)
81 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE);
82 if (res != STATUS_OK)
83 {
84 ifs->close();
85 delete ifs;
86 }
87
88 return res;
89 }
90
91 status_t PullParser::open(const io::Path *path)
92 {
93 if (pIn != NULL)
94 return STATUS_OPENED;
95
96 io::InFileStream *ifs = new io::InFileStream();
97 if (ifs == NULL)
98 return STATUS_NO_MEM;
99
100 status_t res = ifs->open(path);
101 if (res == STATUS_OK)
102 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE);
103 if (res != STATUS_OK)
104 {
105 ifs->close();
106 delete ifs;
107 }
108
109 return res;
110 }
111
112 status_t PullParser::wrap(const char *str)
113 {
114 return wrap(str, strlen(str));
115 }
116
117 status_t PullParser::wrap(const void *buf, size_t len)
118 {
119 if (pIn != NULL)
120 return STATUS_OPENED;
121
122 io::InMemoryStream *ifs = new io::InMemoryStream(buf, len);
123 if (ifs == NULL)
124 return STATUS_NO_MEM;
125
126 status_t res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE);
127 if (res != STATUS_OK)
128 {
129 ifs->close();
130 delete ifs;
131 }
132
133 return res;
134 }
135
136 status_t PullParser::wrap(const LSPString *str)
137 {
138 if (pIn != NULL)
139 return STATUS_OPENED;
140
141 return wrap(str->get_utf8());
142 }
143
144 status_t PullParser::wrap(io::IInStream *is, size_t flags)
145 {
146 if (pIn != NULL)
147 return STATUS_OPENED;
148
149 pIn = is;
150 nWFlags = flags;
151 sCurrent.type = EVENT_NONE;
152 sUnget.truncate();
153 nUnget = 0;
154
155 return STATUS_OK;
156 }
157
158 status_t PullParser::close()
159 {
160 status_t res = STATUS_OK;
161 if (pIn == NULL)
162 return res;
163
164 if (nWFlags & WRAP_CLOSE)
165 res = pIn->close();
166 if (nWFlags & WRAP_DELETE)
167 delete pIn;
168
169 pIn = NULL;
170 sCurrent.type = EVENT_NONE;
171 sUnget.truncate();
172 nUnget = 0;
173
174 return res;
175 }
176
177 const event_t *PullParser::current() const
178 {
179 return &sCurrent;
180 }
181
182 status_t PullParser::next(event_t *event)
183 {
184 if (pIn == NULL)
185 return STATUS_CLOSED;
186
187 // Read the next event
188 status_t res = read_next_event(&sCurrent);
189 if (res != STATUS_OK)
190 return res;
191
192 // Return event data if operation was successful
193 if (event != NULL)
194 {
195 LSPString name, value;
196 if (!name.set(&sCurrent.name))
197 return STATUS_NO_MEM;
198 if (!value.set(&sCurrent.value))
199 return STATUS_NO_MEM;
200
201 // Commit changes
202 event->type = sCurrent.type;
203 event->name.swap(&name);
204 event->value.swap(&value);
205 event->blob.wrap(sCurrent.blob, 0);
206 }
207
208 return STATUS_OK;
209 }
210
211 bool PullParser::is_identifier(lsp_wchar_t ch, bool first)
212 {
213 if ((ch >= 'a') && (ch <= 'z'))
214 return true;
215 if ((ch >= 'A') && (ch <= 'Z'))
216 return true;
217 if (ch == '_')
218 return true;
219 if ((!first) && (ch >= '0') && (ch <= '9'))
220 return true;
221 return false;
222 }
223
224 bool PullParser::is_space(lsp_wchar_t ch)
225 {
226 switch (ch)
227 {
228 case '\n':
229 case '\r':
230 case '\t':
231 case '\v':
232 case ' ':
233 return true;
234 default:
235 break;
236 }
237 return false;
238 }
239
240 void PullParser::trim_right(LSPString *value)
241 {
242 ssize_t index = ssize_t(value->length()) - 1;
243 for (; index >= 0; --index)
244 {
245 if (!is_space(value->char_at(index)))
246 break;
247 }
248 value->set_length((index >= 0) ? index + 1 : 0);
249 }
250
251 lsp_swchar_t PullParser::get_char()
252 {
253 if (nUnget < sUnget.length())
254 {
255 lsp_wchar_t ch = sUnget.char_at(nUnget++);
256 if (nUnget >= sUnget.length())
257 {
258 sUnget.truncate();
259 nUnget = 0;
260 }
261 return ch;
262 }
263 return lsp_swchar_t(pIn->read_byte());
264 }
265
266 status_t PullParser::peek_pending_event(event_t *ev)
267 {
268 if (sSample.type != EVENT_NONE)
269 {
270 ev->type = sSample.type;
271 ev->name.take(&sSample.name);
272 ev->value.take(sSample.value);
273 ev->blob.take(sSample.blob);
274
275 sSample.type = EVENT_NONE;
276
277 return STATUS_OK;
278 }
279
280 return STATUS_EOF;
281 }
282
283 status_t PullParser::read_next_event(event_t *ev)
284 {
285 lsp_swchar_t ch;
286
287 while ((ch = get_char()) >= 0)
288 {
289 switch (ch)
290 {
291 case '<':
292 return set_error(read_header(ev));
293 case '/':
294 return set_error(read_comment(ev));
295 case '#':
296 return set_error(read_preprocessor(ev));
297 default:
298 {
299 if (is_space(ch))
300 break;
301 status_t res = read_opcode(ch, ev);
302 if (res == STATUS_SKIP) // Special case?
303 break;
304 return set_error(res);
305 }
306 }
307 }
308
309 // Check if there is pending event
310 if (ch == -STATUS_EOF)
311 return set_error(peek_pending_event(ev));
312
313 return set_error(-ch);
314 }
315
316 status_t PullParser::read_header(event_t *ev)
317 {
318 lsp_swchar_t ch;
319 LSPString header;
320
321 // If there is pending event, return it first
322 status_t res = peek_pending_event(ev);
323 if (res == STATUS_OK)
324 return (sUnget.append('<')) ? STATUS_OK : STATUS_NO_MEM;
325
326 while ((ch = get_char()) >= 0)
327 {
328 if (ch == '>')
329 {
330 ev->type = EVENT_HEADER;
331 ev->name.swap(&header);
332 ev->value.clear();
333 ev->blob.close();
334
335 // Special case: trigger pending event on '<sample>' header
336 if (ev->name.equals_ascii(HDR_SAMPLE))
337 sSample.type = EVENT_SAMPLE;
338
339 return STATUS_OK;
340 }
341 else if (!is_identifier(ch, header.is_empty()))
342 return STATUS_CORRUPTED;
343
344 if (!header.append(ch))
345 return STATUS_NO_MEM;
346 }
347
348 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
349 }
350
351 status_t PullParser::read_opcode_name(lsp_swchar_t ch, LSPString *name)
352 {
353 if (!is_identifier(ch, true))
354 return STATUS_CORRUPTED;
355 if (!name->append(ch))
356 return STATUS_NO_MEM;
357
358 while ((ch = get_char()) >= 0)
359 {
360 if (ch == '=')
361 return STATUS_OK;
362 else if ((ch != '$') && (!is_identifier(ch, name->is_empty())))
363 return STATUS_CORRUPTED;
364
365 if (!name->append(ch))
366 return STATUS_NO_MEM;
367 }
368
369 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
370 }
371
372 status_t PullParser::read_opcode_value(LSPString *value)
373 {
374 lsp_swchar_t ch;
375
376 while ((ch = get_char()) >= 0)
377 {
378 switch (ch)
379 {
380 case '<': // header?
381 case '/': // comment?
382 nUnget = 0;
383 sUnget.clear();
384 if (!sUnget.append(ch))
385 return STATUS_NO_MEM;
386 return STATUS_OK;
387
388 default:
389 if (is_space(ch))
390 return STATUS_OK;
391 if (!value->append(ch))
392 return STATUS_NO_MEM;
393 break;
394 }
395 }
396
397 return (ch == -STATUS_EOF) ? STATUS_OK : -ch;
398 }
399
400 status_t PullParser::read_string_opcode(LSPString *value)
401 {
402 lsp_swchar_t ch;
403
404 while ((ch = get_char()) >= 0)
405 {
406 if (ch == '=')
407 {
408 // Special case from SFZ specification:
409 // Long names and names with blank spaces and other special characters
410 // (excepting the = character) are allowed in the sample definition.
411 // sample=some file name.ext opcode2= <- we are here
412 if (!value->append(ch))
413 return STATUS_NO_MEM;
414
415 // Look for the first space character from the end
416 ssize_t index = ssize_t(value->length()) - 2;
417 for (; index >= 0; --index)
418 {
419 ch = value->char_at(index);
420 if (is_space(ch))
421 break;
422 }
423
424 // Fill the unget buffer
425 if (!sUnget.set(value, (index >= 0) ? index + 1 : 0))
426 return STATUS_NO_MEM;
427 nUnget = 0;
428
429 // Trim the value from the right
430 value->set_length((index >= 0) ? index : 0);
431 trim_right(value);
432
433 return STATUS_OK;
434 }
435 else if (ch == '\n')
436 {
437 trim_right(value);
438 return STATUS_OK;
439 }
440
441 if (!value->append(ch))
442 return STATUS_NO_MEM;
443 }
444
445 return (ch == -STATUS_EOF) ? STATUS_OK : -ch;
446 }
447
448 status_t PullParser::read_sample_data(io::IOutStream *os)
449 {
450 lsp_swchar_t ch;
451 status_t res;
452
453 // The decoding algorithm is as follows:
454 // Loop while there is an input byte b1
455 // if b1 is the end marker $24, stop reading
456 // if b1 is ‘\r’ ($0D) or ‘\n’ ($0A), discard it
457 // if b1 is the escape character ‘=’ ($3D),
458 // extract the next byte b2, and compute the next output byte as (b2+$C0)%256
459 // otherwise, compute the next output byte as (b1+$D6)%256
460
461 while ((ch = get_char()) >= 0)
462 {
463 switch (ch)
464 {
465 // If the character is the end marker 0x24, stop reading
466 case '$':
467 return STATUS_OK;
468
469 // If the character is ‘\r’ (0x0D) or ‘\n’ (0x0A), discard it
470 case '\n':
471 case '\r':
472 break;
473
474 // If the character is the escape character ‘=’
475 case '=':
476 // Extract the next byte and compute the next output byte as (value + 0xC0)&0xff
477 if ((ch = get_char()) < 0)
478 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
479 if ((res = os->write_byte((ch + 0xc0) & 0xff)) != STATUS_OK)
480 return res;
481 break;
482
483 // Otherwise, compute the next output byte as (value + 0xD6)&0xff
484 default:
485 if ((res = os->write_byte((ch + 0xd6) & 0xff)) != STATUS_OK)
486 return res;
487 break;
488 }
489 }
490
491 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
492 }
493
494 bool PullParser::is_string_opcode(const LSPString *name)
495 {
496 ssize_t first, last;
497
498 // sorted list of parameter names
499 static const char *names[] =
500 {
501 "default_path",
502 "delay_filter",
503 "filter_type",
504 "global_label",
505 "group_label",
506 "image",
507 "master_label",
508 "region_label",
509 "sample",
510 "script",
511 "static_filter",
512 "sw_label",
513 "vendor_specific"
514 };
515
516 // sorted list of parameter prefixes
517 static const char *prefixes[] =
518 {
519 "label_cc",
520 "label_key"
521 };
522
523 // Perform binary search by full match
524 first = 0;
525 last = sizeof(names) / sizeof(names[0]) - 1;
526 while (first <= last)
527 {
528 ssize_t mid = (first + last) / 2;
529 const char *text = names[mid];
530
531 ssize_t res = name->compare_to_ascii(text);
532 if (res == 0)
533 return true;
534 if (res < 0)
535 last = mid - 1;
536 else
537 first = mid + 1;
538 }
539
540 // Perform binary search by prefx match
541 first = 0;
542 last = sizeof(prefixes) / sizeof(prefixes[0]) - 1;
543 while (first <= last)
544 {
545 ssize_t mid = (first + last) / 2;
546 const char *text = prefixes[mid];
547
548 if (name->starts_with_ascii(text))
549 return true;
550 if (name->compare_to_ascii(text) < 0)
551 last = mid - 1;
552 else
553 first = mid + 1;
554 }
555
556 return false;
557 }
558
559 status_t PullParser::read_opcode(lsp_wchar_t ch, event_t *ev)
560 {
561 status_t res;
562 LSPString name, value;
563
564 if ((res = read_opcode_name(ch, &name)) != STATUS_OK)
565 return res;
566
567 if (is_string_opcode(&name))
568 {
569 if ((res = read_string_opcode(&value)) != STATUS_OK)
570 return res;
571 }
572 else if (sSample.type == EVENT_SAMPLE)
573 {
574 // Special case: the <sample> header is pending
575 if ((is_string_opcode(&name)) || (name.equals_ascii("name")))
576 {
577 if ((res = read_string_opcode(&value)) != STATUS_OK)
578 return res;
579 sSample.name.swap(&value);
580 return STATUS_SKIP;
581 }
582 else if (name.equals_ascii("data"))
583 {
584 io::OutMemoryStream os;
585 if ((res = read_sample_data(&os)) != STATUS_OK)
586 return res;
587
588 sSample.blob.take(&os);
589 return STATUS_SKIP;
590 }
591 else if ((res = read_opcode_value(&value)) != STATUS_OK)
592 return res;
593 }
594 else
595 {
596 if ((res = read_opcode_value(&value)) != STATUS_OK)
597 return res;
598 }
599
600 // Commit the result
601 ev->type = EVENT_OPCODE;
602 ev->name.swap(&name);
603 ev->value.swap(&value);
604 ev->blob.close();
605
606 return STATUS_OK;
607 }
608
609 status_t PullParser::read_comment(event_t *ev)
610 {
611 lsp_swchar_t ch;
612 if ((ch = get_char()) < 0)
613 return -ch;
614 if (ch != '/')
615 return STATUS_CORRUPTED;
616
617 // Read the entire string until end of line
618 LSPString text;
619 while ((ch = get_char()) >= 0)
620 {
621 if (ch == '\n')
622 {
623 // Patch for Windows line endings
624 if (text.last() == '\r')
625 text.remove_last();
626 break;
627 }
628
629 if (!text.append(ch))
630 return STATUS_NO_MEM;
631 }
632
633 ev->type = EVENT_COMMENT;
634 ev->name.clear();
635 ev->value.swap(&text);
636 ev->blob.close();
637
638 return STATUS_OK;
639 }
640
641 status_t PullParser::expect_string(const char *text)
642 {
643 // Read the expected string from the file
644 lsp_swchar_t ch;
645 for (const char *p=text; *p != '\0'; ++p)
646 {
647 if ((ch = get_char()) < 0)
648 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
649 if (ch != *p)
650 return STATUS_CORRUPTED;
651 }
652
653 return STATUS_OK;
654 }
655
656 status_t PullParser::expect_char(lsp_swchar_t expected)
657 {
658 lsp_swchar_t ch;
659 while ((ch = get_char()) >= 0)
660 {
661 if (ch == expected)
662 return STATUS_OK;
663 if (!is_space(ch))
664 return STATUS_CORRUPTED;
665 }
666
667 return (ch == -STATUS_EOF) ? STATUS_CORRUPTED : -ch;
668 }
669
670 status_t PullParser::read_variable_name(LSPString *value)
671 {
672 lsp_swchar_t ch;
673 if (!value->append('$'))
674 return STATUS_NO_MEM;
675
676 while ((ch = get_char()) >= 0)
677 {
678 if (is_space(ch))
679 break;
680 if (!is_identifier(ch, value->length() == 1))
681 return STATUS_CORRUPTED;
682 if (!value->append(ch))
683 return STATUS_NO_MEM;
684 }
685 if ((ch < 0) && (ch != -STATUS_EOF))
686 return -ch;
687
688 // The variable name should be of at least 1 character after '$' sign
689 return (value->length() >= 2) ? STATUS_OK : STATUS_CORRUPTED;
690 }
691
692 status_t PullParser::read_variable_value(LSPString *value)
693 {
694 lsp_swchar_t ch;
695
696 // Read first character
697 while ((ch = get_char()) >= 0)
698 {
699 if (!is_space(ch))
700 break;
701 }
702 if (ch < 0)
703 return (ch != -STATUS_EOF) ? -ch : STATUS_OK;
704
705 if (!value->append(ch))
706 return STATUS_NO_MEM;
707
708 // Read the last variable definition
709 size_t slashes = 0;
710
711 while ((ch = get_char()) >= 0)
712 {
713 switch (ch)
714 {
715 case '<': // header?
716 nUnget = 0;
717 sUnget.clear();
718 if (!sUnget.append(ch))
719 return STATUS_NO_MEM;
720 return STATUS_OK;
721
722 case '/': // possible comment?
723 if ((++slashes) >= 2)
724 {
725 nUnget = 0;
726 sUnget.clear();
727 if (!sUnget.append_ascii("//", 2))
728 return STATUS_NO_MEM;
729 return STATUS_OK;
730 }
731 if (!value->append(ch))
732 return STATUS_NO_MEM;
733 break;
734
735 default:
736 slashes = 0;
737 if (is_space(ch))
738 return STATUS_OK;
739 if (!value->append(ch))
740 return STATUS_NO_MEM;
741 break;
742 }
743 }
744
745 return (ch == -STATUS_EOF) ? STATUS_OK : -ch;
746 }
747
748 status_t PullParser::read_define(event_t *ev)
749 {
750 status_t res;
751 if ((res = expect_string("efine")) != STATUS_OK) // 'd' already was read
752 return res;
753 if ((res = expect_char('$')) != STATUS_OK)
754 return res;
755
756 // Read the variable name
757 LSPString name;
758 if ((res = read_variable_name(&name)) != STATUS_OK)
759 return res;
760
761 // Read the variable value
762 LSPString value;
763 if ((res = read_variable_value(&value)) != STATUS_OK)
764 return res;
765
766 // Commit the result
767 ev->type = EVENT_DEFINE;
768 ev->name.swap(&name);
769 ev->value.swap(&value);
770 ev->blob.close();
771
772 return STATUS_OK;
773 }
774
775 status_t PullParser::read_include(event_t *ev)
776 {
777 status_t res;
778 if ((res = expect_string("nclude")) != STATUS_OK) // 'i' already was read
779 return res;
780 if ((res = expect_char('\"')) != STATUS_OK)
781 return res;
782
783 // Read the entire string until the end quote occurs
784 lsp_swchar_t ch;
785 LSPString text;
786 while (true)
787 {
788 if ((ch = get_char()) < 0)
789 return (ch == -STATUS_EOF) ? -STATUS_CORRUPTED : -ch;
790
791 if (ch == '\"')
792 break;
793 if (!text.append(ch))
794 return STATUS_NO_MEM;
795 }
796
797 // Commit the result
798 ev->type = EVENT_INCLUDE;
799 ev->name.swap(&text);
800 ev->value.clear();
801 ev->blob.close();
802
803 return STATUS_OK;
804 }
805
806 status_t PullParser::read_preprocessor(event_t *ev)
807 {
808 lsp_swchar_t ch = get_char();
809 switch (ch)
810 {
811 case 'd': return read_define(ev);
812 case 'i': return read_include(ev);
813 default: break;
814 }
815
816 return ((ch < 0) && (ch != -STATUS_EOF)) ? -ch : STATUS_CORRUPTED;
817 }
818
819 status_t PullParser::set_error(status_t code)
820 {
821 if (code != STATUS_OK)
822 {
823 sCurrent.type = EVENT_NONE;
824 sCurrent.name.truncate();
825 sCurrent.value.truncate();
826 }
827
828 return code;
829 }
830
831 } /* namespace sfz */
832 } /* namespace lsp */
833
834
835