.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Helpers for formatting and printing strings |
---|
3 | 4 | * |
---|
.. | .. |
---|
230 | 231 | * @src: source buffer (escaped) |
---|
231 | 232 | * @dst: destination buffer (unescaped) |
---|
232 | 233 | * @size: size of the destination buffer (0 to unlimit) |
---|
233 | | - * @flags: combination of the flags (bitwise OR): |
---|
234 | | - * %UNESCAPE_SPACE: |
---|
235 | | - * '\f' - form feed |
---|
236 | | - * '\n' - new line |
---|
237 | | - * '\r' - carriage return |
---|
238 | | - * '\t' - horizontal tab |
---|
239 | | - * '\v' - vertical tab |
---|
240 | | - * %UNESCAPE_OCTAL: |
---|
241 | | - * '\NNN' - byte with octal value NNN (1 to 3 digits) |
---|
242 | | - * %UNESCAPE_HEX: |
---|
243 | | - * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) |
---|
244 | | - * %UNESCAPE_SPECIAL: |
---|
245 | | - * '\"' - double quote |
---|
246 | | - * '\\' - backslash |
---|
247 | | - * '\a' - alert (BEL) |
---|
248 | | - * '\e' - escape |
---|
249 | | - * %UNESCAPE_ANY: |
---|
250 | | - * all previous together |
---|
| 234 | + * @flags: combination of the flags. |
---|
251 | 235 | * |
---|
252 | 236 | * Description: |
---|
253 | 237 | * The function unquotes characters in the given string. |
---|
.. | .. |
---|
257 | 241 | * |
---|
258 | 242 | * Caller must provide valid source and destination pointers. Be aware that |
---|
259 | 243 | * destination buffer will always be NULL-terminated. Source string must be |
---|
260 | | - * NULL-terminated as well. |
---|
| 244 | + * NULL-terminated as well. The supported flags are:: |
---|
| 245 | + * |
---|
| 246 | + * UNESCAPE_SPACE: |
---|
| 247 | + * '\f' - form feed |
---|
| 248 | + * '\n' - new line |
---|
| 249 | + * '\r' - carriage return |
---|
| 250 | + * '\t' - horizontal tab |
---|
| 251 | + * '\v' - vertical tab |
---|
| 252 | + * UNESCAPE_OCTAL: |
---|
| 253 | + * '\NNN' - byte with octal value NNN (1 to 3 digits) |
---|
| 254 | + * UNESCAPE_HEX: |
---|
| 255 | + * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) |
---|
| 256 | + * UNESCAPE_SPECIAL: |
---|
| 257 | + * '\"' - double quote |
---|
| 258 | + * '\\' - backslash |
---|
| 259 | + * '\a' - alert (BEL) |
---|
| 260 | + * '\e' - escape |
---|
| 261 | + * UNESCAPE_ANY: |
---|
| 262 | + * all previous together |
---|
261 | 263 | * |
---|
262 | 264 | * Return: |
---|
263 | 265 | * The amount of the characters processed to the destination buffer excluding |
---|
.. | .. |
---|
440 | 442 | * @isz: source buffer size |
---|
441 | 443 | * @dst: destination buffer (escaped) |
---|
442 | 444 | * @osz: destination buffer size |
---|
443 | | - * @flags: combination of the flags (bitwise OR): |
---|
| 445 | + * @flags: combination of the flags |
---|
| 446 | + * @only: NULL-terminated string containing characters used to limit |
---|
| 447 | + * the selected escape class. If characters are included in @only |
---|
| 448 | + * that would not normally be escaped by the classes selected |
---|
| 449 | + * in @flags, they will be copied to @dst unescaped. |
---|
| 450 | + * |
---|
| 451 | + * Description: |
---|
| 452 | + * The process of escaping byte buffer includes several parts. They are applied |
---|
| 453 | + * in the following sequence. |
---|
| 454 | + * |
---|
| 455 | + * 1. The character is matched to the printable class, if asked, and in |
---|
| 456 | + * case of match it passes through to the output. |
---|
| 457 | + * 2. The character is not matched to the one from @only string and thus |
---|
| 458 | + * must go as-is to the output. |
---|
| 459 | + * 3. The character is checked if it falls into the class given by @flags. |
---|
| 460 | + * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any |
---|
| 461 | + * character. Note that they actually can't go together, otherwise |
---|
| 462 | + * %ESCAPE_HEX will be ignored. |
---|
| 463 | + * |
---|
| 464 | + * Caller must provide valid source and destination pointers. Be aware that |
---|
| 465 | + * destination buffer will not be NULL-terminated, thus caller have to append |
---|
| 466 | + * it if needs. The supported flags are:: |
---|
| 467 | + * |
---|
444 | 468 | * %ESCAPE_SPACE: (special white space, not space itself) |
---|
445 | 469 | * '\f' - form feed |
---|
446 | 470 | * '\n' - new line |
---|
.. | .. |
---|
463 | 487 | * all previous together |
---|
464 | 488 | * %ESCAPE_HEX: |
---|
465 | 489 | * '\xHH' - byte with hexadecimal value HH (2 digits) |
---|
466 | | - * @only: NULL-terminated string containing characters used to limit |
---|
467 | | - * the selected escape class. If characters are included in @only |
---|
468 | | - * that would not normally be escaped by the classes selected |
---|
469 | | - * in @flags, they will be copied to @dst unescaped. |
---|
470 | | - * |
---|
471 | | - * Description: |
---|
472 | | - * The process of escaping byte buffer includes several parts. They are applied |
---|
473 | | - * in the following sequence. |
---|
474 | | - * 1. The character is matched to the printable class, if asked, and in |
---|
475 | | - * case of match it passes through to the output. |
---|
476 | | - * 2. The character is not matched to the one from @only string and thus |
---|
477 | | - * must go as-is to the output. |
---|
478 | | - * 3. The character is checked if it falls into the class given by @flags. |
---|
479 | | - * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any |
---|
480 | | - * character. Note that they actually can't go together, otherwise |
---|
481 | | - * %ESCAPE_HEX will be ignored. |
---|
482 | | - * |
---|
483 | | - * Caller must provide valid source and destination pointers. Be aware that |
---|
484 | | - * destination buffer will not be NULL-terminated, thus caller have to append |
---|
485 | | - * it if needs. |
---|
486 | 490 | * |
---|
487 | 491 | * Return: |
---|
488 | 492 | * The total size of the escaped output that would be generated for |
---|
.. | .. |
---|
538 | 542 | return p - dst; |
---|
539 | 543 | } |
---|
540 | 544 | EXPORT_SYMBOL(string_escape_mem); |
---|
| 545 | + |
---|
| 546 | +int string_escape_mem_ascii(const char *src, size_t isz, char *dst, |
---|
| 547 | + size_t osz) |
---|
| 548 | +{ |
---|
| 549 | + char *p = dst; |
---|
| 550 | + char *end = p + osz; |
---|
| 551 | + |
---|
| 552 | + while (isz--) { |
---|
| 553 | + unsigned char c = *src++; |
---|
| 554 | + |
---|
| 555 | + if (!isprint(c) || !isascii(c) || c == '"' || c == '\\') |
---|
| 556 | + escape_hex(c, &p, end); |
---|
| 557 | + else |
---|
| 558 | + escape_passthrough(c, &p, end); |
---|
| 559 | + } |
---|
| 560 | + |
---|
| 561 | + return p - dst; |
---|
| 562 | +} |
---|
| 563 | +EXPORT_SYMBOL(string_escape_mem_ascii); |
---|
541 | 564 | |
---|
542 | 565 | /* |
---|
543 | 566 | * Return an allocated string that has been escaped of special characters |
---|
.. | .. |
---|
626 | 649 | return pathname; |
---|
627 | 650 | } |
---|
628 | 651 | EXPORT_SYMBOL_GPL(kstrdup_quotable_file); |
---|
| 652 | + |
---|
| 653 | +/** |
---|
| 654 | + * kfree_strarray - free a number of dynamically allocated strings contained |
---|
| 655 | + * in an array and the array itself |
---|
| 656 | + * |
---|
| 657 | + * @array: Dynamically allocated array of strings to free. |
---|
| 658 | + * @n: Number of strings (starting from the beginning of the array) to free. |
---|
| 659 | + * |
---|
| 660 | + * Passing a non-NULL @array and @n == 0 as well as NULL @array are valid |
---|
| 661 | + * use-cases. If @array is NULL, the function does nothing. |
---|
| 662 | + */ |
---|
| 663 | +void kfree_strarray(char **array, size_t n) |
---|
| 664 | +{ |
---|
| 665 | + unsigned int i; |
---|
| 666 | + |
---|
| 667 | + if (!array) |
---|
| 668 | + return; |
---|
| 669 | + |
---|
| 670 | + for (i = 0; i < n; i++) |
---|
| 671 | + kfree(array[i]); |
---|
| 672 | + kfree(array); |
---|
| 673 | +} |
---|
| 674 | +EXPORT_SYMBOL_GPL(kfree_strarray); |
---|