Debian Codegraph @

modules/lsp-runtime-lib/include/private/fmt/xpm/XPM1StreamParser.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 янв. 2026 г.
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 #ifndef PRIVATE_FMT_XPM_XPM1STREAMPARSER_H_
23 #define PRIVATE_FMT_XPM_XPM1STREAMPARSER_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/fmt/xpm/Parser.h>
27 #include <lsp-plug.in/stdlib/string.h>
28
29 #include <private/fmt/xpm/Tokenizer.h>
30 #include <private/fmt/xpm/utils.h>
31
32 namespace lsp
33 {
34 namespace xpm
35 {
36 /**
37 * Parser for XPM1 streaming format
38 */
39 class LSP_HIDDEN_MODIFIER XPM1StreamParser: public Parser
40 {
41 private:
42 enum state_t
43 {
44 ST_HEADER,
45 ST_COLORS_LOOKUP,
46 ST_COLORS,
47 ST_PIXELS_LOOKUP,
48 ST_PIXELS,
49 ST_EOF
50 };
51
52 static const char * const postfixes[];
53 static const size_t INVALID_VALUE = ~size_t(0);
54
55 private:
56 Tokenizer *pTokenizer;
57 char *sIconId;
58 header_t sHeader;
59 state_t enState;
60 size_t nColors;
61 size_t nRows;
62
63 private:
64 status_t do_close()
65 {
66 if (sIconId != NULL)
67 {
68 free(sIconId);
69 sIconId = NULL;
70 }
71
72 if (pTokenizer == NULL)
73 return STATUS_OK;
74
75 status_t res = pTokenizer->close();
76 delete pTokenizer;
77 pTokenizer = NULL;
78
79 return res;
80 }
81
82 status_t obtain_icon_id(const char *name)
83 {
84 const ssize_t len = strlen(name);
85
86 for (const char * const * postfix = postfixes; *postfix != NULL; ++postfix)
87 {
88 const ssize_t offset = len - strlen(*postfix);
89 if (offset < 0)
90 return STATUS_CORRUPTED_FILE;
91 if (strcmp(&name[offset], *postfix) == 0)
92 {
93 sIconId = static_cast<char *>(malloc(offset + 1));
94 if (sIconId == NULL)
95 return STATUS_NO_MEM;
96 memcpy(sIconId, name, offset);
97 sIconId[offset] = '\0';
98 return STATUS_OK;
99 }
100 }
101
102 return STATUS_CORRUPTED_FILE;
103 }
104
105 public:
106 explicit XPM1StreamParser(Tokenizer * tokenizer)
107 {
108 pTokenizer = tokenizer;
109 sIconId = NULL;
110
111 sHeader.version = VERSION_XPM1;
112 sHeader.width = INVALID_VALUE;
113 sHeader.height = INVALID_VALUE;
114 sHeader.num_colors = INVALID_VALUE;
115 sHeader.chars_per_pixel = INVALID_VALUE;
116 sHeader.x_hotspot = INVALID_VALUE;
117 sHeader.y_hotspot = INVALID_VALUE;
118 sHeader.has_extensions = false;
119
120 enState = ST_HEADER;
121 nColors = 0;
122 nRows = 0;
123 }
124
125 virtual ~XPM1StreamParser() override
126 {
127 do_close();
128 }
129
130 public:
131 virtual status_t close() override
132 {
133 return do_close();
134 }
135
136 virtual status_t read_header(header_t *dst) override
137 {
138 if (pTokenizer == NULL)
139 return STATUS_CLOSED;
140 if (dst == NULL)
141 return STATUS_BAD_ARGUMENTS;
142
143 // Header already parsed?
144 if (enState != ST_HEADER)
145 {
146 *dst = sHeader;
147 return STATUS_OK;
148 }
149
150 // Read header contents
151 status_t res;
152 token_type_t ttype = TOK_INVALID;
153 const char *tvalue = NULL;
154 size_t num_defines = 0;
155 size_t format = INVALID_VALUE;
156
157 while (true)
158 {
159 // Read '#define' token
160 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
161 return res;
162
163 // Check token type
164 if (ttype != TOK_DEFINE)
165 {
166 if (num_defines == 0)
167 {
168 pTokenizer->unread_token();
169 return STATUS_UNSUPPORTED_FORMAT;
170 }
171 break;
172 }
173
174 // Read macro name
175 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
176 return res;
177 if (ttype != TOK_IDENTIFIER)
178 return STATUS_CORRUPTED_FILE;
179
180 // Obtain icon name if not defined
181 if ((num_defines++) == 0)
182 {
183 if ((res = obtain_icon_id(tvalue)) != STATUS_OK)
184 return STATUS_CORRUPTED_FILE;
185 }
186
187 // Look at the header value
188 if ((tvalue = match_prefix(tvalue, sIconId)) == NULL)
189 return STATUS_CORRUPTED_FILE;
190
191 // Prepare to parse value
192 size_t *dst = NULL;
193 if (strcmp(tvalue, "_width") == 0)
194 dst = &sHeader.width;
195 else if (strcmp(tvalue, "_format") == 0)
196 dst = &format;
197 else if (strcmp(tvalue, "_height") == 0)
198 dst = &sHeader.height;
199 else if (strcmp(tvalue, "_ncolors") == 0)
200 dst = &sHeader.num_colors;
201 else if (strcmp(tvalue, "_x_hotspot") == 0)
202 dst = &sHeader.x_hotspot;
203 else if (strcmp(tvalue, "_y_hotspot") == 0)
204 dst = &sHeader.y_hotspot;
205 else if (strcmp(tvalue, "_chars_per_pixel") == 0)
206 dst = &sHeader.chars_per_pixel;
207 else
208 return STATUS_CORRUPTED_FILE;
209
210 // Check for duplicates
211 if (*dst != INVALID_VALUE)
212 return STATUS_CORRUPTED_FILE;
213
214 // Read the integer value
215 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
216 return res;
217 if (ttype != TOK_INTEGER)
218 return STATUS_CORRUPTED_FILE;
219
220 if ((tvalue = parse_int(dst, tvalue)) == NULL)
221 return STATUS_CORRUPTED_FILE;
222 if ((*tvalue) != '\0')
223 return STATUS_CORRUPTED_FILE;
224 }
225
226 // Check current token for "static" word
227 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "static") != 0))
228 return STATUS_CORRUPTED_FILE;
229
230 // Check that all mandatory values have been specified
231 if (format != 1)
232 return STATUS_CORRUPTED_FILE;
233
234 if ((sHeader.width == INVALID_VALUE) ||
235 (sHeader.height == INVALID_VALUE) ||
236 (sHeader.num_colors == INVALID_VALUE) ||
237 (sHeader.chars_per_pixel == INVALID_VALUE))
238 return STATUS_CORRUPTED_FILE;
239
240 // Set default values to 'extended' parameters
241 if (sHeader.x_hotspot == INVALID_VALUE)
242 sHeader.x_hotspot = 0;
243 if (sHeader.y_hotspot == INVALID_VALUE)
244 sHeader.y_hotspot = 0;
245
246 // Check that values are in valid ranges
247 if (!verify_xpm_header(sHeader))
248 return STATUS_CORRUPTED_FILE;
249
250 // Return header and update state
251 *dst = sHeader;
252 enState = ST_COLORS_LOOKUP;
253
254 return STATUS_OK;
255 }
256
257 virtual status_t read_color(Color *dst) override
258 {
259 if (pTokenizer == NULL)
260 return STATUS_CLOSED;
261 if (dst == NULL)
262 return STATUS_BAD_ARGUMENTS;
263
264 status_t res;
265 token_type_t ttype = TOK_INVALID;
266 const char *tvalue = NULL;
267
268 // Need to lookup for color block?
269 if (enState == ST_COLORS_LOOKUP)
270 {
271 // static [const] char * [const] <prefix>_colors[] = {
272
273 // Read 'char' identifier
274 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
275 return res;
276 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
277 {
278 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
279 return res;
280 }
281 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "char") != 0))
282 return STATUS_CORRUPTED_FILE;
283
284 // Read '*'
285 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
286 return res;
287 if (ttype != TOK_ASTERISK)
288 return STATUS_CORRUPTED_FILE;
289
290 // Read variable name
291 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
292 return res;
293 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
294 {
295 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
296 return res;
297 }
298 if (ttype != TOK_IDENTIFIER)
299 return STATUS_CORRUPTED_FILE;
300 if ((tvalue = match_prefix(tvalue, sIconId)) == NULL)
301 return STATUS_CORRUPTED_FILE;
302 if (strcmp(tvalue, "_colors") != 0)
303 return STATUS_CORRUPTED_FILE;
304
305 // Read '[]' braces
306 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
307 return res;
308 if (ttype != TOK_LQBRACKET)
309 return STATUS_CORRUPTED_FILE;
310 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
311 return res;
312 if (ttype != TOK_RQBRACKET)
313 return STATUS_CORRUPTED_FILE;
314
315 // Read assign ('=')
316 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
317 return res;
318 if (ttype != TOK_ASSIGN)
319 return STATUS_CORRUPTED_FILE;
320
321 // Read start of array ('{')
322 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
323 return res;
324 if (ttype != TOK_LBRACE)
325 return STATUS_CORRUPTED_FILE;
326
327 // OK, now is all successful
328 enState = ST_COLORS;
329 }
330
331 // Check that there are still colors available
332 if (enState != ST_COLORS)
333 return (nColors >= sHeader.num_colors) ? STATUS_NOT_FOUND : STATUS_BAD_STATE;
334 else if (nColors >= sHeader.num_colors)
335 return STATUS_NOT_FOUND;
336
337 Color tmp;
338
339 // Read color code
340 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
341 return res;
342 if (ttype == TOK_SEMICOLON)
343 {
344 if (nColors < sHeader.num_colors)
345 return STATUS_CORRUPTED_FILE;
346
347 }
348 if ((ttype != TOK_STRING) || (strlen(tvalue) != sHeader.chars_per_pixel))
349 return STATUS_CORRUPTED_FILE;
350 if (!tmp.set_code(tvalue))
351 return STATUS_NO_MEM;
352
353 // Read separator
354 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
355 return res;
356 if (ttype != TOK_COMMA)
357 return STATUS_CORRUPTED_FILE;
358
359 // Read color value
360 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
361 return res;
362 if (ttype != TOK_STRING)
363 return STATUS_CORRUPTED_FILE;
364
365 // Parse color
366 tvalue = parse_color_item(tmp.color_visual(), tvalue);
367 if ((tvalue == NULL) || (*tvalue != '\0'))
368 return STATUS_CORRUPTED_FILE;
369
370 ++nColors;
371
372 // Read separator
373 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
374 return res;
375
376 if (ttype == TOK_RBRACE)
377 {
378 if (nColors < sHeader.num_colors)
379 return STATUS_CORRUPTED_FILE;
380
381 // Semicolon ';' should be after the rbrace
382 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
383 return res;
384 if (ttype != TOK_SEMICOLON)
385 return STATUS_CORRUPTED_FILE;
386
387 // Update state
388 enState = ST_PIXELS_LOOKUP;
389 }
390 else if (ttype == TOK_COMMA)
391 {
392 if (nColors >= sHeader.num_colors)
393 {
394 // Require '}' rbrace
395 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
396 return res;
397 if (ttype != TOK_RBRACE)
398 return STATUS_CORRUPTED_FILE;
399
400 // Semicolon ';' should be after the rbrace
401 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
402 return res;
403 if (ttype != TOK_SEMICOLON)
404 return STATUS_CORRUPTED_FILE;
405
406 enState = ST_PIXELS_LOOKUP;
407 }
408 }
409 else
410 return STATUS_CORRUPTED_FILE;
411
412 // All seems to be OK
413 if (dst != NULL)
414 tmp.swap(dst);
415
416 return STATUS_OK;
417 }
418
419 virtual status_t read_line(char *dst) override
420 {
421 if (pTokenizer == NULL)
422 return STATUS_CLOSED;
423 if (dst == NULL)
424 return STATUS_BAD_ARGUMENTS;
425
426 status_t res;
427 token_type_t ttype = TOK_INVALID;
428 const char *tvalue = NULL;
429
430 // Need to lookup for color block?
431 if (enState == ST_PIXELS_LOOKUP)
432 {
433 // static [const] char * [const] <prefix>_pixels[] = {
434 // Read 'static' identifier
435 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
436 return res;
437 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "static") != 0))
438 return STATUS_CORRUPTED_FILE;
439
440 // Read 'char' identifier
441 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
442 return res;
443 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
444 {
445 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
446 return res;
447 }
448 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "char") != 0))
449 return STATUS_CORRUPTED_FILE;
450
451 // Read '*'
452 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
453 return res;
454 if (ttype != TOK_ASTERISK)
455 return STATUS_CORRUPTED_FILE;
456
457 // Read variable name
458 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
459 return res;
460 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
461 {
462 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
463 return res;
464 }
465 if (ttype != TOK_IDENTIFIER)
466 return STATUS_CORRUPTED_FILE;
467 if ((tvalue = match_prefix(tvalue, sIconId)) == NULL)
468 return STATUS_CORRUPTED_FILE;
469 if (strcmp(tvalue, "_pixels") != 0)
470 return STATUS_CORRUPTED_FILE;
471
472 // Read '[]' braces
473 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
474 return res;
475 if (ttype != TOK_LQBRACKET)
476 return STATUS_CORRUPTED_FILE;
477 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
478 return res;
479 if (ttype != TOK_RQBRACKET)
480 return STATUS_CORRUPTED_FILE;
481
482 // Read assign ('=')
483 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
484 return res;
485 if (ttype != TOK_ASSIGN)
486 return STATUS_CORRUPTED_FILE;
487
488 // Read start of array ('{')
489 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
490 return res;
491 if (ttype != TOK_LBRACE)
492 return STATUS_CORRUPTED_FILE;
493
494 // OK, now is all successful
495 enState = ST_PIXELS;
496 }
497
498 // Check that there are still pixels available
499 if (enState != ST_PIXELS)
500 return (nRows >= sHeader.height) ? STATUS_NOT_FOUND : STATUS_BAD_STATE;
501 else if (nRows >= sHeader.height)
502 return STATUS_NOT_FOUND;
503
504 // Read string
505 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
506 return res;
507 const size_t row_size = sHeader.width * sHeader.chars_per_pixel;
508 if ((ttype != TOK_STRING) || (strlen(tvalue) != row_size))
509 return STATUS_CORRUPTED_FILE;
510
511 ++nRows;
512 if (dst != NULL)
513 memcpy(dst, tvalue, row_size);
514
515 // Read separator
516 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
517 return res;
518
519 if (ttype == TOK_RBRACE)
520 {
521 if (nRows < sHeader.height)
522 return STATUS_CORRUPTED_FILE;
523
524 // Semicolon ';' should be after the rbrace
525 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
526 return res;
527 if (ttype != TOK_SEMICOLON)
528 return STATUS_CORRUPTED_FILE;
529
530 // Update state
531 enState = ST_EOF;
532 }
533 else if (ttype == TOK_COMMA)
534 {
535 if (nRows >= sHeader.height)
536 {
537 // Require '}' rbrace
538 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
539 return res;
540 if (ttype != TOK_RBRACE)
541 return STATUS_CORRUPTED_FILE;
542
543 // Semicolon ';' should be after the rbrace
544 if ((res = pTokenizer->read_token(ttype, tvalue)) != STATUS_OK)
545 return res;
546 if (ttype != TOK_SEMICOLON)
547 return STATUS_CORRUPTED_FILE;
548
549 enState = ST_EOF;
550 }
551 }
552 else
553 return STATUS_CORRUPTED_FILE;
554
555 return STATUS_OK;
556 }
557
558 virtual status_t read_ext(Extension *dst) override
559 {
560 // XPM1 has no extensions
561 return (pTokenizer == NULL) ? STATUS_CLOSED : STATUS_NOT_FOUND;
562 }
563
564 };
565
566 const char * const XPM1StreamParser::postfixes[] =
567 {
568 "_width",
569 "_format",
570 "_height",
571 "_ncolors",
572 "_x_hotspot",
573 "_y_hotspot",
574 "_chars_per_pixel",
575 NULL
576 };
577
578 } /* namespace xpm */
579 } /* namespace lsp */
580
581
582
583
584 #endif /* PRIVATE_FMT_XPM_XPM1STREAMPARSER_H_ */