| 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 мая 2019 г. |
| 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/alloc.h> |
| 23 |
#include <lsp-plug.in/protocol/osc.h> |
| 24 |
#include <lsp-plug.in/stdlib/string.h> |
| 25 |
|
| 26 |
namespace lsp |
| 27 |
{ |
| 28 |
namespace osc |
| 29 |
{ |
| 30 |
static size_t pattern_calc_segments(const char *address, size_t len) |
| 31 |
{ |
| 32 |
// Compute number of parts |
| 33 |
if ((len <= 0) || (*(address++) != '/')) |
| 34 |
return 0; |
| 35 |
|
| 36 |
// Estimate number of parts in the address |
| 37 |
const char *s; |
| 38 |
size_t parts = 1; |
| 39 |
|
| 40 |
while (true) |
| 41 |
{ |
| 42 |
if ((s = reinterpret_cast<const char *>(::memchr(address, '/', len))) == NULL) |
| 43 |
break; |
| 44 |
++parts; |
| 45 |
++s; |
| 46 |
|
| 47 |
len -= (s - address); |
| 48 |
address = s; |
| 49 |
} |
| 50 |
|
| 51 |
return parts; |
| 52 |
} |
| 53 |
|
| 54 |
static void pattern_split_segments(char **dst, const char *format, size_t len) |
| 55 |
{ |
| 56 |
const char *s; |
| 57 |
size_t idx = 0; |
| 58 |
|
| 59 |
while (true) |
| 60 |
{ |
| 61 |
dst[idx++] = const_cast<char *>(format); |
| 62 |
if ((s = reinterpret_cast<const char *>(::memchr(format, '/', len))) == NULL) |
| 63 |
break; |
| 64 |
++s; |
| 65 |
|
| 66 |
len -= (s - format); |
| 67 |
format = s; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Non-valid characters for OSC: ' ', '#, '*', ',', '/', '?', '[', ']', '{', '}' |
| 72 |
static inline bool pattern_is_invalid(int ch) |
| 73 |
{ |
| 74 |
switch (ch) |
| 75 |
{ |
| 76 |
// End of part? |
| 77 |
case ' ': case '#': case '*': case ',': |
| 78 |
case '/': case '?': case '[': case ']': |
| 79 |
case '{': case '}': case '\0': |
| 80 |
return true; |
| 81 |
|
| 82 |
default: |
| 83 |
return ((ch < 0x20) || (ch >= 0x7f)); |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
static inline bool pattern_is_printable(int ch) |
| 89 |
{ |
| 90 |
return ((ch >= 0x20) && (ch < 0x7f)); |
| 91 |
} |
| 92 |
|
| 93 |
static const char *pattern_validate_list(const char *part) |
| 94 |
{ |
| 95 |
while (true) |
| 96 |
{ |
| 97 |
uint8_t ch = uint8_t(*(part++)); |
| 98 |
|
| 99 |
// Check validity of character |
| 100 |
if (pattern_is_invalid(ch)) |
| 101 |
{ |
| 102 |
if (ch == '}') // End of list? |
| 103 |
return part; |
| 104 |
else if (ch != ',') // Separator? |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static const char *pattern_validate_range(const char *part) |
| 111 |
{ |
| 112 |
// Negotation mark? |
| 113 |
if (*part == '!') |
| 114 |
++part; |
| 115 |
|
| 116 |
size_t count = 0; |
| 117 |
while (true) |
| 118 |
{ |
| 119 |
uint8_t ch = uint8_t(*(part++)); |
| 120 |
|
| 121 |
// Check validity of character |
| 122 |
if (pattern_is_invalid(ch)) |
| 123 |
return ((ch == ']') && (count > 0)) ? part : NULL; |
| 124 |
|
| 125 |
// Range character occurred? |
| 126 |
++count; |
| 127 |
if (*part == '-') |
| 128 |
{ |
| 129 |
++part; |
| 130 |
if (pattern_is_invalid(*part)) |
| 131 |
return NULL; |
| 132 |
++part; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static bool pattern_validate_part(const char *part) |
| 138 |
{ |
| 139 |
// Do not allow empty elements |
| 140 |
uint8_t ch = uint8_t(*(part++)); |
| 141 |
if ((ch == '\0') || (ch == '/')) |
| 142 |
return false; |
| 143 |
|
| 144 |
while (true) |
| 145 |
{ |
| 146 |
switch (ch) |
| 147 |
{ |
| 148 |
// Special cases? |
| 149 |
case '{': |
| 150 |
if (!(part = pattern_validate_list(part))) |
| 151 |
return false; |
| 152 |
break; |
| 153 |
case '[': |
| 154 |
if (!(part = pattern_validate_range(part))) |
| 155 |
return false; |
| 156 |
break; |
| 157 |
|
| 158 |
case '*': |
| 159 |
case '?': |
| 160 |
break; |
| 161 |
|
| 162 |
// End of part? |
| 163 |
case '/': |
| 164 |
case '\0': |
| 165 |
return true; |
| 166 |
|
| 167 |
// Not allowed characters? |
| 168 |
case ' ': |
| 169 |
case '#': |
| 170 |
case ']': |
| 171 |
case '}': |
| 172 |
case ',': |
| 173 |
return false; |
| 174 |
|
| 175 |
// Each OSC Method and each OSC Container other than the root of the tree has a symbolic name, |
| 176 |
// an ASCII string consisting of printable characters other than... |
| 177 |
default: |
| 178 |
if (!pattern_is_printable(ch)) |
| 179 |
return false; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
ch = uint8_t(*(part++)); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
status_t pattern_create(pattern_t *pattern, const char *format) |
| 188 |
{ |
| 189 |
// Check arguments |
| 190 |
if ((pattern == NULL) || (format == NULL)) |
| 191 |
return STATUS_BAD_ARGUMENTS; |
| 192 |
|
| 193 |
// Estimate number of parts |
| 194 |
size_t len = strlen(format); |
| 195 |
size_t parts = pattern_calc_segments(format, len); |
| 196 |
if (parts <= 0) |
| 197 |
return STATUS_BAD_FORMAT; |
| 198 |
|
| 199 |
// Now allocate the memory |
| 200 |
size_t fmt_size = align_size(len + 1, DEFAULT_ALIGN); |
| 201 |
size_t arg_size = align_size(sizeof(char *) * parts, DEFAULT_ALIGN); |
| 202 |
|
| 203 |
// Try to allocate the memory |
| 204 |
char *xfmt = reinterpret_cast<char *>(::malloc(fmt_size + arg_size)); |
| 205 |
if (xfmt == NULL) |
| 206 |
return STATUS_NO_MEM; |
| 207 |
|
| 208 |
::memcpy(xfmt, format, len); // Copy string contents |
| 209 |
xfmt[len] = '\0'; |
| 210 |
char **xargs = reinterpret_cast<char **>(&xfmt[fmt_size]); |
| 211 |
|
| 212 |
// Split segments of pattern |
| 213 |
pattern_split_segments(xargs, &xfmt[1], len); |
| 214 |
|
| 215 |
// Validate pattern segments |
| 216 |
for (size_t i=0; i<parts; ++i) |
| 217 |
if (!pattern_validate_part(xargs[i])) |
| 218 |
{ |
| 219 |
::free(xfmt); |
| 220 |
return STATUS_BAD_FORMAT; |
| 221 |
} |
| 222 |
|
| 223 |
// Store pattern data |
| 224 |
pattern->nparts = parts; |
| 225 |
pattern->format = xfmt; |
| 226 |
pattern->parts = xargs; |
| 227 |
|
| 228 |
return STATUS_OK; |
| 229 |
} |
| 230 |
|
| 231 |
status_t pattern_destroy(pattern_t *pattern) |
| 232 |
{ |
| 233 |
if (pattern == NULL) |
| 234 |
return STATUS_BAD_ARGUMENTS; |
| 235 |
if ((pattern->format == NULL) || (pattern->parts == NULL)) |
| 236 |
return STATUS_OK; |
| 237 |
|
| 238 |
::free(pattern->format); |
| 239 |
pattern->nparts = 0; |
| 240 |
pattern->parts = NULL; |
| 241 |
pattern->format = NULL; |
| 242 |
|
| 243 |
return STATUS_OK; |
| 244 |
} |
| 245 |
|
| 246 |
static const char *pattern_match_range(const char **xpart, const char *address, const char *end) |
| 247 |
{ |
| 248 |
const char *part = *xpart; |
| 249 |
bool invert = false; |
| 250 |
|
| 251 |
// Negotation mark? |
| 252 |
if (*part == '!') |
| 253 |
{ |
| 254 |
++part; |
| 255 |
invert = true; |
| 256 |
} |
| 257 |
|
| 258 |
// Immediate end of range? |
| 259 |
if ((*part == ']') || (address >= end)) |
| 260 |
return (invert) ? NULL : address; |
| 261 |
|
| 262 |
// Get character for check and validate |
| 263 |
uint8_t ch = uint8_t(*(address++)); |
| 264 |
if (pattern_is_invalid(ch)) |
| 265 |
return NULL; |
| 266 |
|
| 267 |
bool matched = false; |
| 268 |
while (true) |
| 269 |
{ |
| 270 |
// Get control character |
| 271 |
uint8_t sch = uint8_t(*(part++)); |
| 272 |
if (sch == ']') |
| 273 |
{ |
| 274 |
*xpart = part; |
| 275 |
return (matched ^ invert) ? address : NULL; |
| 276 |
} |
| 277 |
else if (matched) |
| 278 |
continue; |
| 279 |
|
| 280 |
// Range check ? |
| 281 |
if (*part == '-') |
| 282 |
{ |
| 283 |
++part; |
| 284 |
uint8_t dch = uint8_t(*(part++)); |
| 285 |
if (sch <= dch) |
| 286 |
matched = ((ch >= sch) && (ch <= dch)); |
| 287 |
else |
| 288 |
matched = ((ch >= dch) && (ch <= sch)); |
| 289 |
} |
| 290 |
else |
| 291 |
matched = (ch == sch); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static const char *pattern_match_list(const char **xpart, const char *address, const char *end) |
| 296 |
{ |
| 297 |
ssize_t len = 0; |
| 298 |
bool matched = false; |
| 299 |
const char *part = *xpart, *item = *xpart; |
| 300 |
|
| 301 |
while (true) |
| 302 |
{ |
| 303 |
uint8_t ch = uint8_t(*(part++)); |
| 304 |
|
| 305 |
switch (ch) |
| 306 |
{ |
| 307 |
// End of list? |
| 308 |
case '}': |
| 309 |
if ((!matched) && ((end - address) >= len)) |
| 310 |
{ |
| 311 |
if ((matched = (::memcmp(item, address, len) == 0))) |
| 312 |
address = &address[len]; |
| 313 |
} |
| 314 |
*xpart = part; |
| 315 |
return (matched) ? address : NULL; |
| 316 |
|
| 317 |
// Next token? |
| 318 |
case ',': |
| 319 |
if (matched) |
| 320 |
break; |
| 321 |
if ((end - address) >= len) |
| 322 |
{ |
| 323 |
if ((matched = (::memcmp(item, address, len) == 0))) |
| 324 |
address = &address[len]; |
| 325 |
} |
| 326 |
|
| 327 |
// Update list item parameters |
| 328 |
item = part; |
| 329 |
len = 0; |
| 330 |
break; |
| 331 |
|
| 332 |
// Another chatacter? |
| 333 |
default: |
| 334 |
++len; |
| 335 |
break; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
static const char *pattern_match_part(const char *part, const char *address, const char *end) |
| 341 |
{ |
| 342 |
while (true) |
| 343 |
{ |
| 344 |
uint8_t pch = uint8_t(*(part++)); |
| 345 |
switch (pch) |
| 346 |
{ |
| 347 |
// End of segment? |
| 348 |
case '\0': |
| 349 |
case '/': |
| 350 |
pch = uint8_t(*address); |
| 351 |
return ((pch == '\0') || (pch == '/')) ? address : NULL; |
| 352 |
|
| 353 |
// Any character? |
| 354 |
case '?': |
| 355 |
if (pattern_is_invalid(*(address++))) |
| 356 |
return NULL; |
| 357 |
break; |
| 358 |
|
| 359 |
// Wildcard? |
| 360 |
case '*': |
| 361 |
{ |
| 362 |
do |
| 363 |
{ |
| 364 |
// Do forward lookup |
| 365 |
const char *new_address = pattern_match_part(part, address, end); |
| 366 |
if (new_address) |
| 367 |
return new_address; |
| 368 |
pch = uint8_t(*(address++)); |
| 369 |
} while ((pch != '\0') && (pch != '/')); |
| 370 |
|
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
// Start of character range? |
| 375 |
case '[': |
| 376 |
if ((address = pattern_match_range(&part, address, end))) |
| 377 |
break; |
| 378 |
return address; |
| 379 |
|
| 380 |
// Start of string list? |
| 381 |
case '{': |
| 382 |
if ((address = pattern_match_list(&part, address, end))) |
| 383 |
break; |
| 384 |
return address; |
| 385 |
|
| 386 |
// Simple character match? |
| 387 |
default: |
| 388 |
if (*(address++) != pch) |
| 389 |
return NULL; |
| 390 |
break; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
bool pattern_match(const pattern_t *pattern, const char *address) |
| 396 |
{ |
| 397 |
if ((pattern == NULL) || (address == NULL)) |
| 398 |
return false; |
| 399 |
|
| 400 |
uint8_t ch = uint8_t(*(address++)); |
| 401 |
if (ch != '/') |
| 402 |
return false; |
| 403 |
const char *end = address + strlen(address); |
| 404 |
|
| 405 |
for (size_t parts=0; parts < pattern->nparts; ) |
| 406 |
{ |
| 407 |
const char *part = pattern->parts[parts++]; |
| 408 |
|
| 409 |
// The substring in address should be not empty |
| 410 |
ch = *address; |
| 411 |
if ((ch == '\0') || (ch == '/')) |
| 412 |
return false; |
| 413 |
|
| 414 |
// Match address string with pattern |
| 415 |
if (!(address = pattern_match_part(part, address, end))) |
| 416 |
return false; |
| 417 |
|
| 418 |
switch (*address) |
| 419 |
{ |
| 420 |
case '\0': |
| 421 |
return parts == pattern->nparts; |
| 422 |
case '/': |
| 423 |
++address; |
| 424 |
break; |
| 425 |
default: |
| 426 |
return false; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
status_t pattern_get_format(pattern_t *pattern, const char **format) |
| 434 |
{ |
| 435 |
if ((pattern == NULL) || (format == NULL)) |
| 436 |
return STATUS_BAD_ARGUMENTS; |
| 437 |
*format = pattern->format; |
| 438 |
return STATUS_OK; |
| 439 |
} |
| 440 |
} /* namespace osc */ |
| 441 |
} /* namespace lsp */ |
| 442 |
|
| 443 |
|