Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 29 апр. 2020 г.
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/fmt/config/PullParser.h>
23 #include <lsp-plug.in/io/InFileStream.h>
24 #include <lsp-plug.in/io/InStringSequence.h>
25 #include <lsp-plug.in/io/InSequence.h>
26 #include <lsp-plug.in/stdlib/locale.h>
27 #include <lsp-plug.in/stdlib/math.h>
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <limits.h>
32
33 namespace lsp
34 {
35 namespace config
36 {
37 typedef struct type_t
38 {
39 const char *prefix;
40 size_t flags;
41 } type_t;
42
43 static const type_t all_types[] =
44 {
45 { "i32:", SF_TYPE_I32 },
46 { "u32:", SF_TYPE_U32 },
47 { "f32:", SF_TYPE_F32 },
48 { "i64:", SF_TYPE_I64 },
49 { "u64:", SF_TYPE_U64 },
50 { "f64:", SF_TYPE_F64 },
51 { "bool:", SF_TYPE_BOOL },
52 { "str:", SF_TYPE_STR },
53 { "blob:", SF_TYPE_BLOB },
54 { NULL, 0 }
55 };
56
57 PullParser::PullParser()
58 {
59 pIn = NULL;
60 nWFlags = 0;
61 }
62
63 PullParser::~PullParser()
64 {
65 do_close();
66 }
67
68 status_t PullParser::open(const char *path, const char *charset)
69 {
70 if (pIn != NULL)
71 return STATUS_BAD_STATE;
72 else if (path == NULL)
73 return STATUS_BAD_ARGUMENTS;
74
75 io::InFileStream *ifs = new io::InFileStream();
76 if (ifs == NULL)
77 return STATUS_NO_MEM;
78 status_t res = ifs->open(path);
79 if (res == STATUS_OK)
80 {
81 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
82 if (res == STATUS_OK)
83 return res;
84 ifs->close();
85 }
86 delete ifs;
87
88 return res;
89 }
90
91 status_t PullParser::open(const LSPString *path, const char *charset)
92 {
93 if (pIn != NULL)
94 return STATUS_BAD_STATE;
95 else if (path == NULL)
96 return STATUS_BAD_ARGUMENTS;
97
98 io::InFileStream *ifs = new io::InFileStream();
99 if (ifs == NULL)
100 return STATUS_NO_MEM;
101 status_t res = ifs->open(path);
102 if (res == STATUS_OK)
103 {
104 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
105 if (res == STATUS_OK)
106 return res;
107 ifs->close();
108 }
109 delete ifs;
110
111 return res;
112 }
113
114 status_t PullParser::open(const io::Path *path, const char *charset)
115 {
116 if (pIn != NULL)
117 return STATUS_BAD_STATE;
118 else if (path == NULL)
119 return STATUS_BAD_ARGUMENTS;
120
121 io::InFileStream *ifs = new io::InFileStream();
122 if (ifs == NULL)
123 return STATUS_NO_MEM;
124 status_t res = ifs->open(path);
125 if (res == STATUS_OK)
126 {
127 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
128 if (res == STATUS_OK)
129 return res;
130 ifs->close();
131 }
132 delete ifs;
133
134 return res;
135 }
136
137 status_t PullParser::wrap(const char *str, const char *charset)
138 {
139 if (pIn != NULL)
140 return STATUS_BAD_STATE;
141 else if (str == NULL)
142 return STATUS_BAD_ARGUMENTS;
143
144 io::InStringSequence *seq = new io::InStringSequence();
145 if (seq == NULL)
146 return STATUS_NO_MEM;
147
148 status_t res = seq->wrap(str, charset);
149 if (res == STATUS_OK)
150 {
151 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
152 return res;
153 seq->close();
154 }
155
156 delete seq;
157 return res;
158 }
159
160 status_t PullParser::wrap(const LSPString *str)
161 {
162 if (pIn != NULL)
163 return STATUS_BAD_STATE;
164 else if (str == NULL)
165 return STATUS_BAD_ARGUMENTS;
166
167 io::InStringSequence *seq = new io::InStringSequence();
168 if (seq == NULL)
169 return STATUS_NO_MEM;
170
171 status_t res = seq->wrap(str);
172 if (res == STATUS_OK)
173 {
174 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
175 return res;
176 seq->close();
177 }
178
179 delete seq;
180 return res;
181 }
182
183 status_t PullParser::wrap(io::IInStream *is, size_t flags, const char *charset)
184 {
185 if (pIn != NULL)
186 return STATUS_BAD_STATE;
187 else if (is == NULL)
188 return STATUS_BAD_ARGUMENTS;
189
190 io::InSequence *seq = new io::InSequence();
191 if (seq == NULL)
192 return STATUS_NO_MEM;
193
194 status_t res = seq->wrap(is, flags, charset);
195 if (res == STATUS_OK)
196 {
197 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
198 return res;
199 seq->close();
200 }
201
202 delete seq;
203 return res;
204 }
205
206 status_t PullParser::wrap(io::IInSequence *seq, size_t flags)
207 {
208 if (pIn != NULL)
209 return STATUS_BAD_STATE;
210 else if (seq == NULL)
211 return STATUS_BAD_ARGUMENTS;
212
213 pIn = seq;
214 nWFlags = flags;
215
216 return STATUS_OK;
217 }
218
219 status_t PullParser::do_close()
220 {
221 // Release input sequence
222 if (pIn == NULL)
223 return STATUS_OK;
224
225 status_t res = STATUS_OK;
226 if (nWFlags & WRAP_CLOSE)
227 res = update_status(res, pIn->close());
228
229 if (nWFlags & WRAP_DELETE)
230 delete pIn;
231
232 pIn = NULL;
233
234 return res;
235 }
236
237 status_t PullParser::close()
238 {
239 return do_close();
240 }
241
242 status_t PullParser::next(param_t *ev)
243 {
244 if (pIn == NULL)
245 return STATUS_CLOSED;
246
247 status_t result = STATUS_OK;
248
249 while (true)
250 {
251 // Clear buffer
252 sLine.clear();
253 sKey.clear();
254 sValue.clear();
255
256 // Read line from file
257 result = pIn->read_line(&sLine, true);
258 if (result != STATUS_OK)
259 break;
260
261 // Parse the line
262 size_t flags = 0;
263 result = parse_line(flags);
264 if (result == STATUS_OK)
265 {
266 result = commit_param(&sKey, &sValue, flags);
267 if ((result == STATUS_OK) && (ev != NULL))
268 result = (ev->copy(&sParam)) ? STATUS_OK : STATUS_NO_MEM;
269 break;
270 }
271 else if (result != STATUS_SKIP)
272 break;
273 }
274
275 return result;
276 }
277
278 const param_t *PullParser::current() const
279 {
280 return ((sParam.flags & SF_TYPE_MASK) != SF_NONE) ? &sParam : NULL;
281 }
282
283 status_t PullParser::current(param_t *ev) const
284 {
285 if (pIn == NULL)
286 return STATUS_CLOSED;
287
288 if ((sParam.flags & SF_TYPE_MASK) == SF_NONE)
289 return STATUS_NO_DATA;
290
291 if ((ev != NULL) && (!ev->copy(&sParam)))
292 return STATUS_NO_MEM;
293
294 return STATUS_OK;
295 }
296
297 bool PullParser::skip_spaces(size_t &off)
298 {
299 size_t len = sLine.length();
300
301 while (off < len)
302 {
303 lsp_wchar_t ch = sLine.at(off);
304 if ((ch != ' ') && (ch != '\t') && (ch != '\r'))
305 break;
306 off ++;
307 }
308
309 return ((off >= len) || (sLine.at(off) == '#')); // Return true if line is void
310 }
311
312 status_t PullParser::read_key(size_t &off)
313 {
314 size_t len = sLine.length();
315
316 while (off < len)
317 {
318 lsp_wchar_t ch = sLine.at(off++);
319
320 if (((ch >= 'a') && (ch <= 'z')) ||
321 ((ch >= 'A') && (ch <= 'Z')) ||
322 (ch == '_') || (ch == '/'))
323 {
324 if (!sKey.append(ch))
325 return STATUS_NO_MEM;
326 }
327 else if ((ch >= '0') && (ch <= '9'))
328 {
329 if (sKey.is_empty())
330 return STATUS_BAD_FORMAT;
331 if (!sKey.append(ch))
332 return STATUS_NO_MEM;
333 }
334 else
335 {
336 --off;
337 break;
338 }
339 }
340
341 // Validate that key should start with '/' or not contain any '/' character
342 if ((sKey.is_empty()) || (sKey.index_of('/') > 0))
343 return STATUS_BAD_FORMAT;
344
345 return STATUS_OK;
346 }
347
348 status_t PullParser::read_type(size_t &off, size_t &flags)
349 {
350 // Check type prefix
351 for (const type_t *p = all_types; p->prefix != NULL; ++p)
352 {
353 if (sLine.contains_at_ascii(off, p->prefix))
354 {
355 flags |= p->flags | SF_TYPE_SET;
356 off += ::strlen(p->prefix);
357 return STATUS_OK;
358 }
359 }
360
361 return STATUS_OK;
362 }
363
364 status_t PullParser::read_value(size_t &off, size_t &flags)
365 {
366 size_t len = sLine.length();
367 ssize_t trim = -1;
368
369 // Check that value is quoted
370 if (sLine.char_at(off) == '"')
371 {
372 flags |= SF_QUOTED;
373 ++off;
374 }
375
376 // Check that type is present
377 while (off < len)
378 {
379 lsp_wchar_t ch = sLine.at(off++);
380
381 switch (ch)
382 {
383 // Escape sequence
384 case '\\':
385 {
386 if (off >= len)
387 return (sLine.append(ch)) ? STATUS_OK : STATUS_NO_MEM;
388
389 ch = sLine.at(off++);
390
391 switch (ch)
392 {
393 case 'n': ch = '\n'; break;
394 case 'r': ch = '\r'; break;
395 case 't': ch = '\t'; break;
396 case '\\':
397 case ' ':
398 case '\"':
399 case '#':
400 break;
401 default:
402 if (!sValue.append('\\'))
403 return STATUS_NO_MEM;
404 break;
405 }
406 if (!sValue.append(ch))
407 return STATUS_NO_MEM;
408 break;
409 }
410
411 // Comment
412 case '#':
413 if (flags & SF_QUOTED)
414 {
415 if (!sValue.append(ch))
416 return STATUS_NO_MEM;
417 trim = -1;
418 }
419 else
420 off = len; // Move to end of line
421
422 break;
423
424 // Spaces
425 case ' ':
426 case '\t':
427 if ((!(flags & SF_QUOTED)) && (trim < 0))
428 trim = sValue.length();
429
430 if (!sValue.append(ch))
431 return STATUS_NO_MEM;
432 break;
433
434 // Quotes
435 case '\"':
436 if (!(flags & SF_QUOTED))
437 return STATUS_BAD_FORMAT;
438 return (skip_spaces(off)) ? STATUS_OK : STATUS_BAD_FORMAT;
439
440 // End of line
441 case '\n':
442 if (flags & SF_QUOTED)
443 return STATUS_BAD_FORMAT;
444 return STATUS_OK;
445
446 // Other characters
447 default:
448 if (!sValue.append(ch))
449 return STATUS_NO_MEM;
450 trim = -1;
451 break;
452 }
453 }
454
455 if (flags & SF_QUOTED)
456 return STATUS_BAD_FORMAT;
457 else if (trim >= 0)
458 sValue.set_length(trim);
459
460 return STATUS_OK;
461 }
462
463 status_t PullParser::parse_line(size_t &flags)
464 {
465 sKey.clear();
466 sValue.clear();
467 flags = 0;
468
469 // Empty line?
470 size_t off=0;
471 if (skip_spaces(off))
472 return STATUS_SKIP;
473
474 // Fetch the key value
475 status_t res = read_key(off);
476 if (res != STATUS_OK) // Error while parsing line
477 return res;
478 if (skip_spaces(off))
479 return STATUS_BAD_FORMAT;
480
481 // Analyze character
482 lsp_wchar_t ch = sLine.at(off++);
483 if ((sKey.is_empty()) || (ch != '='))
484 return STATUS_BAD_FORMAT;
485
486 // Fetch the value type
487 if (skip_spaces(off))
488 return STATUS_OK;
489 res = read_type(off, flags);
490 if (res != STATUS_OK)
491 return res;
492
493 // Fetch the value's value
494 if (skip_spaces(off))
495 return STATUS_OK;
496 res = read_value(off, flags);
497 if (res != STATUS_OK)
498 return res;
499
500 // Ensure that line ends correctly
501 return (skip_spaces(off)) ? STATUS_OK : STATUS_BAD_FORMAT;
502 }
503
504 status_t PullParser::commit_param(const LSPString *key, const LSPString *value, size_t flags)
505 {
506 param_t tmp;
507 status_t res = STATUS_OK;
508
509 // Copy parameter name
510 if (!tmp.name.set(key))
511 return STATUS_NO_MEM;
512 tmp.comment.clear();
513
514 // If type is explicitly set
515 if (flags & SF_TYPE_SET)
516 {
517 switch (flags & SF_TYPE_MASK)
518 {
519 case SF_TYPE_I32: res = parse_int32(value, &tmp.v.i32); break;
520 case SF_TYPE_U32: res = parse_uint32(value, &tmp.v.u32); break;
521 case SF_TYPE_F32: res = parse_float(value, &tmp.v.f32, &flags); break;
522 case SF_TYPE_I64: res = parse_int64(value, &tmp.v.i64); break;
523 case SF_TYPE_U64: res = parse_uint64(value, &tmp.v.u64); break;
524 case SF_TYPE_F64: res = parse_double(value, &tmp.v.f64, &flags); break;
525 case SF_TYPE_BOOL: res = parse_bool(value, &tmp.v.bval); break;
526 case SF_TYPE_STR:
527 if ((tmp.v.str = value->clone_utf8()) == NULL)
528 res = STATUS_NO_MEM;
529 break;
530 case SF_TYPE_BLOB:
531 tmp.v.blob.ctype = NULL;
532 tmp.v.blob.data = NULL;
533 res = parse_blob(value, &tmp.v.blob);
534 break;
535 default:
536 return STATUS_UNKNOWN_ERR;
537 }
538 tmp.flags = flags;
539 if (res == STATUS_OK)
540 sParam.swap(&tmp);
541 return res;
542 }
543
544 // Type is not explicitly set
545 if (!(flags & SF_QUOTED))
546 {
547 if (value->index_of('.') < 0)
548 {
549 // Try to parse as boolean
550 if ((res = parse_bool(value, &tmp.v.bval)) == STATUS_OK)
551 {
552 tmp.flags = flags | SF_TYPE_BOOL;
553 sParam.swap(&tmp);
554 return STATUS_OK;
555 }
556
557 // Try to parse as integer
558 if ((res = parse_int32(value, &tmp.v.i32)) == STATUS_OK)
559 {
560 tmp.flags = flags | SF_TYPE_I32;
561 sParam.swap(&tmp);
562 return STATUS_OK;
563 }
564 }
565
566 // Try to parse as float
567 if ((res = parse_float(value, &tmp.v.f32, &flags)) == STATUS_OK)
568 {
569 tmp.flags = flags | SF_TYPE_F32;
570 sParam.swap(&tmp);
571 return STATUS_OK;
572 }
573 }
574
575 // Return as a string
576 if ((tmp.v.str = value->clone_utf8()) == NULL)
577 res = STATUS_NO_MEM;
578 tmp.flags = flags | SF_TYPE_STR;
579
580 sParam.swap(&tmp);
581 return STATUS_OK;
582 }
583
584 status_t PullParser::parse_int32(const LSPString *str, int32_t *dst)
585 {
586 const char *s = str->get_utf8();
587 if (s == NULL)
588 return STATUS_NO_MEM;
589 else if (*s == '\0')
590 return STATUS_BAD_FORMAT;
591
592 errno = 0;
593 char *end = NULL;
594 long value = ::strtol(s, &end, 10);
595
596 if ((errno != 0) || (*end != '\0'))
597 return STATUS_BAD_FORMAT;
598
599 *dst = int32_t(value);
600 return STATUS_OK;
601 }
602
603 status_t PullParser::parse_uint32(const LSPString *str, uint32_t *dst)
604 {
605 const char *s = str->get_utf8();
606 if (s == NULL)
607 return STATUS_NO_MEM;
608 else if (*s == '\0')
609 return STATUS_BAD_FORMAT;
610
611 errno = 0;
612 char *end = NULL;
613 unsigned long value = ::strtoul(s, &end, 10);
614
615 if ((errno != 0) || (*end != '\0'))
616 return STATUS_BAD_FORMAT;
617
618 *dst = uint32_t(value);
619 return STATUS_OK;
620 }
621
622 status_t PullParser::parse_int64(const LSPString *str, int64_t *dst)
623 {
624 const char *s = str->get_utf8();
625 if (s == NULL)
626 return STATUS_NO_MEM;
627 else if (*s == '\0')
628 return STATUS_BAD_FORMAT;
629
630 errno = 0;
631 char *end = NULL;
632 #ifdef PLATFORM_BSD
633 long value = ::strtol(s, &end, 10);
634 #else
635 long long value = ::strtoll(s, &end, 10);
636 #endif /* PLATFORM_BSD */
637
638 if ((errno != 0) || (*end != '\0'))
639 return STATUS_BAD_FORMAT;
640
641 *dst = value;
642 return STATUS_OK;
643 }
644
645 status_t PullParser::parse_uint64(const LSPString *str, uint64_t *dst)
646 {
647 const char *s = str->get_utf8();
648 if (s == NULL)
649 return STATUS_NO_MEM;
650 else if (*s == '\0')
651 return STATUS_BAD_FORMAT;
652
653 errno = 0;
654 char *end = NULL;
655 #ifdef PLATFORM_BSD
656 unsigned long value = ::strtoul(s, &end, 10);
657 #else
658 unsigned long long value = ::strtoull(s, &end, 10);
659 #endif /* PLATFORM_BSD */
660
661 if ((errno != 0) || (*end != '\0'))
662 return STATUS_BAD_FORMAT;
663
664 *dst = value;
665 return STATUS_OK;
666 }
667
668 status_t PullParser::parse_float(const LSPString *str, float *dst, size_t *flags)
669 {
670 const char *s = str->get_utf8();
671 if (s == NULL)
672 return STATUS_NO_MEM;
673 else if (*s == '\0')
674 return STATUS_BAD_FORMAT;
675
676 // Save and update locale
677 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
678
679 // Parse float
680 errno = 0;
681 char *end = NULL;
682 float value = ::strtof(s, &end);
683 size_t xf = 0;
684
685 bool success = (errno == 0);
686 if ((success) && (end != NULL))
687 {
688 // Skip spaces
689 while (((*end) == ' ') || ((*end) == '\t'))
690 ++ end;
691 if (((end[0] == 'd') || (end[0] == 'D')) &&
692 ((end[1] == 'b') || (end[1] == 'B')))
693 {
694 xf |= SF_DECIBELS;
695 end += 2;
696 }
697 // Skip spaces
698 while (((*end) == ' ') || ((*end) == '\t'))
699 ++ end;
700 if (*end != '\0')
701 success = false;
702 }
703
704 // Return result
705 if (!success)
706 return STATUS_BAD_FORMAT;
707
708 *dst = value;
709 *flags |= xf;
710 return STATUS_OK;
711 }
712
713 status_t PullParser::parse_double(const LSPString *str, double *dst, size_t *flags)
714 {
715 const char *s = str->get_utf8();
716 if (s == NULL)
717 return STATUS_NO_MEM;
718 else if (*s == '\0')
719 return STATUS_BAD_FORMAT;
720
721 // Save and update locale
722 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
723
724 // Parse float
725 errno = 0;
726 char *end = NULL;
727 double value = ::strtod(s, &end);
728 size_t xf = 0;
729
730 bool success = (errno == 0);
731 if ((success) && (end != NULL))
732 {
733 // Skip spaces
734 while (((*end) == ' ') || ((*end) == '\t'))
735 ++ end;
736 if (((end[0] == 'd') || (end[0] == 'D')) &&
737 ((end[1] == 'b') || (end[1] == 'B')))
738 {
739 xf |= SF_DECIBELS;
740 end += 2;
741 }
742 // Skip spaces
743 while (((*end) == ' ') || ((*end) == '\t'))
744 ++ end;
745 if (*end != '\0')
746 success = false;
747 }
748
749 // Return result
750 if (!success)
751 return STATUS_BAD_FORMAT;
752
753 *dst = value;
754 *flags |= xf;
755 return STATUS_OK;
756 }
757
758 status_t PullParser::parse_bool(const LSPString *str, bool *dst)
759 {
760 if (str->length() <= 0)
761 return STATUS_BAD_FORMAT;
762
763 // Parse boolean value
764 if (str->equals_ascii_nocase("true"))
765 {
766 *dst = true;
767 return STATUS_OK;
768 }
769 else if (str->equals_ascii_nocase("false"))
770 {
771 *dst = false;
772 return STATUS_OK;
773 }
774
775 return STATUS_BAD_FORMAT;
776 }
777
778 status_t PullParser::parse_blob(const LSPString *str, blob_t *dst)
779 {
780 ssize_t pos, idx = str->index_of(':');
781 if (idx < 0)
782 return STATUS_BAD_FORMAT;
783
784 // Content type is present
785 if (idx > 0)
786 {
787 dst->ctype = str->clone_utf8(ssize_t(0), idx);
788 if (dst->ctype == NULL)
789 return STATUS_NO_MEM;
790 }
791 pos = idx + 1;
792
793 // Require length field and parse
794 idx = str->index_of(pos, ':');
795 if (idx <= pos)
796 return STATUS_BAD_FORMAT;
797
798 LSPString length;
799 if (!length.set(str, pos, idx))
800 return STATUS_NO_MEM;
801 uint64_t size;
802 status_t res = parse_uint64(&length, &size);
803 if (res != STATUS_OK)
804 return res;
805 dst->length = size_t(size);
806
807 // Copy data
808 dst->data = str->clone_utf8(idx + 1);
809 if (dst->data == NULL)
810 return STATUS_NO_MEM;
811
812 return STATUS_OK;
813 }
814
815 } /* namespace config */
816 } /* namespace lsp */