hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/lib/string_helpers.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Helpers for formatting and printing strings
34 *
....@@ -230,24 +231,7 @@
230231 * @src: source buffer (escaped)
231232 * @dst: destination buffer (unescaped)
232233 * @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.
251235 *
252236 * Description:
253237 * The function unquotes characters in the given string.
....@@ -257,7 +241,25 @@
257241 *
258242 * Caller must provide valid source and destination pointers. Be aware that
259243 * 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
261263 *
262264 * Return:
263265 * The amount of the characters processed to the destination buffer excluding
....@@ -440,7 +442,29 @@
440442 * @isz: source buffer size
441443 * @dst: destination buffer (escaped)
442444 * @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
+ *
444468 * %ESCAPE_SPACE: (special white space, not space itself)
445469 * '\f' - form feed
446470 * '\n' - new line
....@@ -463,26 +487,6 @@
463487 * all previous together
464488 * %ESCAPE_HEX:
465489 * '\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.
486490 *
487491 * Return:
488492 * The total size of the escaped output that would be generated for
....@@ -538,6 +542,25 @@
538542 return p - dst;
539543 }
540544 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);
541564
542565 /*
543566 * Return an allocated string that has been escaped of special characters
....@@ -626,3 +649,26 @@
626649 return pathname;
627650 }
628651 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);