Debian Codegraph @

modules/lsp-runtime-lib/include/private/fmt/xpm/XPM3StreamParser.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: 23 янв. 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_XPM3STREAMPARSER_H_
23 #define PRIVATE_FMT_XPM_XPM3STREAMPARSER_H_
24
25
26 #include <lsp-plug.in/runtime/version.h>
27 #include <lsp-plug.in/fmt/xpm/Parser.h>
28 #include <lsp-plug.in/stdlib/string.h>
29
30 #include <private/fmt/xpm/Tokenizer.h>
31 #include <private/fmt/xpm/utils.h>
32
33 namespace lsp
34 {
35 namespace xpm
36 {
37 /**
38 * Parser for XPM3 streaming format
39 */
40 class LSP_HIDDEN_MODIFIER XPM3StreamParser: public Parser
41 {
42 private:
43 enum state_t
44 {
45 ST_HEADER,
46 ST_COLORS,
47 ST_PIXELS,
48 ST_EXTENSIONS,
49 ST_EOF
50 };
51
52 private:
53 Tokenizer *pTokenizer;
54 header_t sHeader;
55 state_t enState;
56 size_t nColors;
57 size_t nRows;
58
59 private:
60 status_t do_close()
61 {
62 if (pTokenizer == NULL)
63 return STATUS_OK;
64
65 status_t res = pTokenizer->close();
66 delete pTokenizer;
67 pTokenizer = NULL;
68
69 return res;
70 }
71
72 status_t read_token(token_type_t & type, const char * & value)
73 {
74 status_t res;
75 do {
76 if ((res = pTokenizer->read_token(type, value)) != STATUS_OK)
77 return res;
78 } while (type == TOK_COMMENT);
79 return res;
80 }
81
82 public:
83 XPM3StreamParser(Tokenizer * tokenizer)
84 {
85 pTokenizer = tokenizer;
86
87 sHeader.version = VERSION_XPM3;
88 sHeader.width = 0;
89 sHeader.height = 0;
90 sHeader.num_colors = 0;
91 sHeader.chars_per_pixel = 0;
92 sHeader.x_hotspot = 0;
93 sHeader.y_hotspot = 0;
94 sHeader.has_extensions = false;
95
96 enState = ST_HEADER;
97 nColors = 0;
98 nRows = 0;
99 }
100
101 virtual ~XPM3StreamParser()
102 {
103 do_close();
104 }
105
106 public:
107 virtual status_t close() override
108 {
109 return do_close();
110 }
111
112 virtual status_t read_header(header_t *dst) override
113 {
114 if (pTokenizer == NULL)
115 return STATUS_CLOSED;
116 if (dst == NULL)
117 return STATUS_BAD_ARGUMENTS;
118
119 // Header already parsed?
120 if (enState != ST_HEADER)
121 {
122 *dst = sHeader;
123 return STATUS_OK;
124 }
125
126 // Read the 'static [const] char * [const] <name>[] = {' signature
127 status_t res;
128 token_type_t ttype = TOK_INVALID;
129 const char *tvalue = NULL;
130
131 // Keyword 'static'
132 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
133 return STATUS_CORRUPTED_FILE;
134 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "static") != 0))
135 return STATUS_CORRUPTED_FILE;
136
137 // Read 'char' type
138 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
139 return STATUS_CORRUPTED_FILE;
140 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
141 {
142 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
143 return res;
144 }
145 if ((ttype != TOK_IDENTIFIER) || (strcmp(tvalue, "char") != 0))
146 return STATUS_CORRUPTED_FILE;
147
148 // Read '*'
149 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
150 return res;
151 if (ttype != TOK_ASTERISK)
152 return STATUS_CORRUPTED_FILE;
153
154 // Read variable name
155 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
156 return res;
157 if ((ttype == TOK_IDENTIFIER) && (strcmp(tvalue, "const") == 0))
158 {
159 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
160 return res;
161 }
162 if (ttype != TOK_IDENTIFIER)
163 return STATUS_CORRUPTED_FILE;
164
165 // Read '[]' braces
166 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
167 return res;
168 if (ttype != TOK_LQBRACKET)
169 return STATUS_CORRUPTED_FILE;
170 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
171 return res;
172 if (ttype != TOK_RQBRACKET)
173 return STATUS_CORRUPTED_FILE;
174
175 // Read assign ('=')
176 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
177 return res;
178 if (ttype != TOK_ASSIGN)
179 return STATUS_CORRUPTED_FILE;
180
181 // Read start of array ('{')
182 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
183 return res;
184 if (ttype != TOK_LBRACE)
185 return STATUS_CORRUPTED_FILE;
186
187 // Finally, read the first line in the array
188 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
189 return res;
190 if (ttype != TOK_STRING)
191 return STATUS_CORRUPTED_FILE;
192
193 // Parse XPM header
194 if ((tvalue = parse_xpm_header(sHeader, tvalue)) == NULL)
195 return STATUS_CORRUPTED_FILE;
196 if ((*tvalue) != '\0')
197 return STATUS_CORRUPTED_FILE;
198
199 // Check that values are in valid ranges
200 if (!verify_xpm_header(sHeader))
201 return STATUS_CORRUPTED_FILE;
202
203 // Return header and update state
204 *dst = sHeader;
205 enState = ST_COLORS;
206
207 return STATUS_OK;
208 }
209
210 virtual status_t read_color(Color *dst) override
211 {
212 if (pTokenizer == NULL)
213 return STATUS_CLOSED;
214 if (dst == NULL)
215 return STATUS_BAD_ARGUMENTS;
216
217 // Check that there are still colors available
218 if (enState != ST_COLORS)
219 return (nColors >= sHeader.num_colors) ? STATUS_NOT_FOUND : STATUS_BAD_STATE;
220 else if (nColors >= sHeader.num_colors)
221 {
222 enState = ST_PIXELS;
223 return STATUS_NOT_FOUND;
224 }
225
226 status_t res;
227 token_type_t ttype = TOK_INVALID;
228 const char *tvalue = NULL;
229 Color tmp;
230
231 // Require comma separator
232 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
233 return res;
234 if (ttype != TOK_COMMA)
235 return STATUS_CORRUPTED_FILE;
236
237 // Read color line
238 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
239 return res;
240 if (ttype != TOK_STRING)
241 return STATUS_CORRUPTED_FILE;
242
243 // Parse color
244 tvalue = parse_color(tmp, tvalue, sHeader.chars_per_pixel);
245 if ((tvalue == NULL) || (*tvalue != '\0'))
246 return STATUS_CORRUPTED_FILE;
247
248 if ((++nColors) >= sHeader.num_colors)
249 enState = ST_PIXELS;
250
251 // All seems to be OK
252 if (dst != NULL)
253 tmp.swap(dst);
254
255 return STATUS_OK;
256 }
257
258 virtual status_t read_line(char *dst) override
259 {
260 if (pTokenizer == NULL)
261 return STATUS_CLOSED;
262 if (dst == NULL)
263 return STATUS_BAD_ARGUMENTS;
264
265 // Check that there are still pixels available
266 if (enState != ST_PIXELS)
267 return (nRows >= sHeader.height) ? STATUS_NOT_FOUND : STATUS_BAD_STATE;
268 else if (nRows >= sHeader.height)
269 {
270 enState = ST_EXTENSIONS;
271 return STATUS_NOT_FOUND;
272 }
273
274 status_t res;
275 token_type_t ttype = TOK_INVALID;
276 const char *tvalue = NULL;
277 Color tmp;
278
279 // Require comma separator
280 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
281 return res;
282 if (ttype != TOK_COMMA)
283 return STATUS_CORRUPTED_FILE;
284
285 // Read pixel line
286 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
287 return res;
288 if (ttype != TOK_STRING)
289 return STATUS_CORRUPTED_FILE;
290
291 const size_t row_size = sHeader.width * sHeader.chars_per_pixel;
292 if (strlen(tvalue) != row_size)
293 return STATUS_CORRUPTED_FILE;
294
295 // Commit result and update state
296 if (dst != NULL)
297 memcpy(dst, tvalue, row_size);
298
299 if ((++nRows) >= sHeader.height)
300 enState = (sHeader.has_extensions) ? ST_EXTENSIONS : ST_EOF;
301
302 return STATUS_OK;
303 }
304
305 virtual status_t read_ext(Extension *dst) override
306 {
307 if (pTokenizer == NULL)
308 return STATUS_CLOSED;
309 if (dst == NULL)
310 return STATUS_BAD_ARGUMENTS;
311
312 // Check that there are still pixels available
313 if (enState < ST_EXTENSIONS)
314 return STATUS_BAD_STATE;
315 else if (enState > ST_EXTENSIONS)
316 return STATUS_NOT_FOUND;
317
318 // Read extension
319 status_t res;
320 token_type_t ttype = TOK_INVALID;
321 const char *tvalue = NULL;
322 Extension tmp;
323
324 // Require comma separator or closing right brace
325 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
326 return res;
327 if (ttype == TOK_RBRACE)
328 {
329 // Require semicolon
330 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
331 return res;
332 if (ttype != TOK_SEMICOLON)
333 return STATUS_CORRUPTED_FILE;
334
335 enState = ST_EOF;
336 return STATUS_NOT_FOUND;
337 }
338 else if (ttype != TOK_COMMA)
339 return STATUS_CORRUPTED_FILE;
340
341 // Read extension line
342 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
343 return res;
344 if (ttype != TOK_STRING)
345 return STATUS_CORRUPTED_FILE;
346
347 // XPMEXT token
348 if ((tvalue = match_prefix(tvalue, "XPMEXT")) == NULL)
349 return STATUS_CORRUPTED_FILE;
350 if (!is_blank(*tvalue++))
351 return STATUS_CORRUPTED_FILE;
352
353 // Extension name
354 const char *id = parse_identifier(tvalue);
355 if ((id == NULL) || (id <= tvalue))
356 return STATUS_CORRUPTED_FILE;
357 if (!tmp.set_name(tvalue, id - tvalue))
358 return STATUS_NO_MEM;
359
360 // Extension data
361 tvalue = id;
362 if (*tvalue == '\0')
363 {
364 // Multiline extension
365 size_t lines = 0;
366 while (true)
367 {
368 // Require comma
369 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
370 return res;
371 if (ttype != TOK_COMMA)
372 return STATUS_CORRUPTED_FILE;
373
374 // Read extension line
375 if ((res = read_token(ttype, tvalue)) != STATUS_OK)
376 return res;
377 if (ttype != TOK_STRING)
378 return STATUS_CORRUPTED_FILE;
379
380 // End of extension lines?
381 if (strcmp(tvalue, "XPMENDEXT") == 0)
382 {
383 if (lines <= 0)
384 return STATUS_CORRUPTED_FILE;
385 break;
386 }
387
388 if (!tmp.add_row(tvalue))
389 return STATUS_NO_MEM;
390 ++lines;
391 }
392 }
393 else if (is_blank(*tvalue))
394 {
395 // Single-line extension
396 ++tvalue;
397 if (!tmp.add_row(tvalue))
398 return STATUS_NO_MEM;
399 }
400 else
401 return STATUS_CORRUPTED_FILE;
402
403 // All seems to be OK
404 if (dst != NULL)
405 tmp.swap(dst);
406
407 return STATUS_OK;
408 }
409 };
410
411 } /* namespace xpm */
412 } /* namespace lsp */
413
414
415
416 #endif /* PRIVATE_FMT_XPM_XPM3STREAMPARSER_H_ */