| 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_XPM1BUILTINPARSER_H_ |
| 23 |
#define PRIVATE_FMT_XPM_XPM1BUILTINPARSER_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/utils.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace xpm |
| 34 |
{ |
| 35 |
/** |
| 36 |
* Parser for XPM1 built-in format |
| 37 |
*/ |
| 38 |
class LSP_HIDDEN_MODIFIER XPM1BuiltinParser: public Parser |
| 39 |
{ |
| 40 |
private: |
| 41 |
header_t sHeader; |
| 42 |
const char * const *vColors; |
| 43 |
const char * const *vPixels; |
| 44 |
size_t nColors; |
| 45 |
size_t nRows; |
| 46 |
|
| 47 |
public: |
| 48 |
XPM1BuiltinParser(const header_t & header, const char * const * colors, const char * const * pixels) |
| 49 |
{ |
| 50 |
sHeader = header; |
| 51 |
vColors = colors; |
| 52 |
vPixels = pixels; |
| 53 |
nColors = 0; |
| 54 |
nRows = 0; |
| 55 |
} |
| 56 |
|
| 57 |
virtual ~XPM1BuiltinParser() |
| 58 |
{ |
| 59 |
vColors = NULL; |
| 60 |
vPixels = NULL; |
| 61 |
} |
| 62 |
|
| 63 |
public: |
| 64 |
virtual status_t read_header(header_t *dst) override |
| 65 |
{ |
| 66 |
if (dst == NULL) |
| 67 |
return STATUS_BAD_ARGUMENTS; |
| 68 |
|
| 69 |
*dst = sHeader; |
| 70 |
return STATUS_OK; |
| 71 |
} |
| 72 |
|
| 73 |
virtual status_t read_color(Color *dst) override |
| 74 |
{ |
| 75 |
if (dst == NULL) |
| 76 |
return STATUS_BAD_ARGUMENTS; |
| 77 |
|
| 78 |
if (vColors == NULL) |
| 79 |
return STATUS_CLOSED; |
| 80 |
|
| 81 |
// Check that there are still colors available |
| 82 |
if (nColors >= sHeader.num_colors) |
| 83 |
return STATUS_NOT_FOUND; |
| 84 |
|
| 85 |
Color tmp; |
| 86 |
const size_t offset = nColors * 2; |
| 87 |
|
| 88 |
// Obtain color key |
| 89 |
const char *key = vColors[offset]; |
| 90 |
if (key == NULL) |
| 91 |
return STATUS_CORRUPTED_FILE; |
| 92 |
|
| 93 |
// Obtain color value |
| 94 |
const char *value = vColors[offset + 1]; |
| 95 |
if (value == NULL) |
| 96 |
return STATUS_CORRUPTED_FILE; |
| 97 |
|
| 98 |
// Validate key |
| 99 |
if (strlen(key) != sHeader.chars_per_pixel) |
| 100 |
return STATUS_CORRUPTED_FILE; |
| 101 |
if (!tmp.set_code(key)) |
| 102 |
return STATUS_NO_MEM; |
| 103 |
|
| 104 |
// Validate value |
| 105 |
const size_t vlen = strlen(value); |
| 106 |
if (vlen <= 0) |
| 107 |
return STATUS_CORRUPTED_FILE; |
| 108 |
if (value[0] != '#') |
| 109 |
return STATUS_CORRUPTED_FILE; |
| 110 |
|
| 111 |
// Parse color value |
| 112 |
value = parse_color_item(tmp.color_visual(), value); |
| 113 |
if ((value == NULL) || (*value != '\0')) |
| 114 |
return STATUS_CORRUPTED_FILE; |
| 115 |
|
| 116 |
// All seems to be OK |
| 117 |
++nColors; |
| 118 |
if (dst != NULL) |
| 119 |
tmp.swap(dst); |
| 120 |
|
| 121 |
return STATUS_OK; |
| 122 |
} |
| 123 |
|
| 124 |
virtual status_t read_line(char *dst) override |
| 125 |
{ |
| 126 |
if (dst == NULL) |
| 127 |
return STATUS_BAD_ARGUMENTS; |
| 128 |
|
| 129 |
if (vPixels == NULL) |
| 130 |
return STATUS_CLOSED; |
| 131 |
|
| 132 |
// Check that all colors have been read previously |
| 133 |
if (nColors < sHeader.num_colors) |
| 134 |
return STATUS_BAD_STATE; |
| 135 |
|
| 136 |
// Check that there are still rows available |
| 137 |
if (nRows >= sHeader.height) |
| 138 |
return STATUS_NOT_FOUND; |
| 139 |
|
| 140 |
// Get the row and validate |
| 141 |
const char * const row = vPixels[nRows]; |
| 142 |
const size_t bytes = strlen(row); |
| 143 |
if (bytes != sHeader.chars_per_pixel * sHeader.width) |
| 144 |
return STATUS_CORRUPTED_FILE; |
| 145 |
|
| 146 |
// Now we are ready to return the line |
| 147 |
if (dst != NULL) |
| 148 |
memcpy(dst, row, bytes); |
| 149 |
++nRows; |
| 150 |
|
| 151 |
return STATUS_OK; |
| 152 |
} |
| 153 |
|
| 154 |
virtual status_t read_ext(Extension *dst) override |
| 155 |
{ |
| 156 |
if (dst == NULL) |
| 157 |
return STATUS_BAD_ARGUMENTS; |
| 158 |
|
| 159 |
if ((vColors == NULL) || (vPixels == NULL)) |
| 160 |
return STATUS_CLOSED; |
| 161 |
|
| 162 |
// XPM1 has no extensions |
| 163 |
return STATUS_NOT_FOUND; |
| 164 |
} |
| 165 |
|
| 166 |
virtual status_t close() override |
| 167 |
{ |
| 168 |
vColors = NULL; |
| 169 |
vPixels = NULL; |
| 170 |
|
| 171 |
return STATUS_OK; |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
} /* namespace xpm */ |
| 176 |
} /* namespace lsp */ |
| 177 |
|
| 178 |
#endif /* PRIVATE_FMT_XPM_XPM1BUILTINPARSER_H_ */ |