VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
457c899653991 (Thomas Gleixner     2019-05-19 13:08:55 +0100   1) // SPDX-License-Identifier: GPL-2.0-only
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   2) /*
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   3)  * Helpers for formatting and printing strings
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   4)  *
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   5)  * Copyright 31 August 2008 James Bottomley
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700   6)  * Copyright (C) 2013, Intel Corporation
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   7)  */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800   8) #include <linux/bug.h>
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500   9) #include <linux/kernel.h>
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  10) #include <linux/math64.h>
8bc3bcc93a2b4 (Paul Gortmaker      2011-11-16 21:29:17 -0500  11) #include <linux/export.h>
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700  12) #include <linux/ctype.h>
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700  13) #include <linux/errno.h>
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700  14) #include <linux/fs.h>
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700  15) #include <linux/limits.h>
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700  16) #include <linux/mm.h>
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700  17) #include <linux/slab.h>
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700  18) #include <linux/string.h>
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  19) #include <linux/string_helpers.h>
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  20) 
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  21) /**
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  22)  * string_get_size - get the size in the specified units
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  23)  * @size:	The size to be converted in blocks
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  24)  * @blk_size:	Size of the block (use 1 for size in bytes)
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  25)  * @units:	units to use (powers of 1000 or 1024)
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  26)  * @buf:	buffer to format to
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  27)  * @len:	length of buffer
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  28)  *
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  29)  * This function returns a string formatted to 3 significant figures
d1214c65c02d5 (Rasmus Villemoes    2015-02-12 15:01:50 -0800  30)  * giving the size in the required units.  @buf should have room for
d1214c65c02d5 (Rasmus Villemoes    2015-02-12 15:01:50 -0800  31)  * at least 9 bytes and will always be zero terminated.
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  32)  *
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  33)  */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  34) void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
d1214c65c02d5 (Rasmus Villemoes    2015-02-12 15:01:50 -0800  35) 		     char *buf, int len)
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  36) {
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  37) 	static const char *const units_10[] = {
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  38) 		"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  39) 	};
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  40) 	static const char *const units_2[] = {
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  41) 		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  42) 	};
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  43) 	static const char *const *const units_str[] = {
142cda5dbcb0d (Mathias Krause      2014-08-06 16:09:31 -0700  44) 		[STRING_UNITS_10] = units_10,
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  45) 		[STRING_UNITS_2] = units_2,
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  46) 	};
68aecfb97978f (Andrew Morton       2012-05-29 15:07:32 -0700  47) 	static const unsigned int divisor[] = {
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  48) 		[STRING_UNITS_10] = 1000,
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  49) 		[STRING_UNITS_2] = 1024,
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  50) 	};
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  51) 	static const unsigned int rounding[] = { 500, 50, 5 };
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  52) 	int i = 0, j;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  53) 	u32 remainder = 0, sf_cap;
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  54) 	char tmp[8];
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  55) 	const char *unit;
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  56) 
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  57) 	tmp[0] = '\0';
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  58) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  59) 	if (blk_size == 0)
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  60) 		size = 0;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  61) 	if (size == 0)
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  62) 		goto out;
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  63) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  64) 	/* This is Napier's algorithm.  Reduce the original block size to
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  65) 	 *
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  66) 	 * coefficient * divisor[units]^i
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  67) 	 *
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  68) 	 * we do the reduction so both coefficients are just under 32 bits so
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  69) 	 * that multiplying them together won't overflow 64 bits and we keep
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  70) 	 * as much precision as possible in the numbers.
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  71) 	 *
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  72) 	 * Note: it's safe to throw away the remainders here because all the
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  73) 	 * precision is in the coefficients.
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  74) 	 */
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  75) 	while (blk_size >> 32) {
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  76) 		do_div(blk_size, divisor[units]);
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  77) 		i++;
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  78) 	}
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  79) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  80) 	while (size >> 32) {
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  81) 		do_div(size, divisor[units]);
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  82) 		i++;
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  83) 	}
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  84) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  85) 	/* now perform the actual multiplication keeping i as the sum of the
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  86) 	 * two logarithms */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  87) 	size *= blk_size;
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  88) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  89) 	/* and logarithmically reduce it until it's just under the divisor */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  90) 	while (size >= divisor[units]) {
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  91) 		remainder = do_div(size, divisor[units]);
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  92) 		i++;
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  93) 	}
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500  94) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  95) 	/* work out in j how many digits of precision we need from the
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800  96) 	 * remainder */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  97) 	sf_cap = size;
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  98) 	for (j = 0; sf_cap*10 < 1000; j++)
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800  99) 		sf_cap *= 10;
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 100) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 101) 	if (units == STRING_UNITS_2) {
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 102) 		/* express the remainder as a decimal.  It's currently the
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 103) 		 * numerator of a fraction whose denominator is
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 104) 		 * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 105) 		remainder *= 1000;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 106) 		remainder >>= 10;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 107) 	}
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 108) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 109) 	/* add a 5 to the digit below what will be printed to ensure
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 110) 	 * an arithmetical round up and carry it through to size */
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 111) 	remainder += rounding[j];
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 112) 	if (remainder >= 1000) {
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 113) 		remainder -= 1000;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 114) 		size += 1;
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 115) 	}
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 116) 
564b026fbd0d2 (James Bottomley     2016-01-20 14:58:29 -0800 117) 	if (j) {
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 118) 		snprintf(tmp, sizeof(tmp), ".%03u", remainder);
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 119) 		tmp[j+1] = '\0';
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 120) 	}
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 121) 
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 122)  out:
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 123) 	if (i >= ARRAY_SIZE(units_2))
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 124) 		unit = "UNK";
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 125) 	else
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 126) 		unit = units_str[units][i];
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 127) 
84b9fbedf54a6 (Rasmus Villemoes    2015-02-12 15:01:48 -0800 128) 	snprintf(buf, len, "%u%s %s", (u32)size,
b9f28d863594c (James Bottomley     2015-03-05 18:47:01 -0800 129) 		 tmp, unit);
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500 130) }
3c9f3681d0b4a (James Bottomley     2008-08-31 10:13:54 -0500 131) EXPORT_SYMBOL(string_get_size);
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 132) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 133) static bool unescape_space(char **src, char **dst)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 134) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 135) 	char *p = *dst, *q = *src;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 136) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 137) 	switch (*q) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 138) 	case 'n':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 139) 		*p = '\n';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 140) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 141) 	case 'r':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 142) 		*p = '\r';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 143) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 144) 	case 't':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 145) 		*p = '\t';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 146) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 147) 	case 'v':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 148) 		*p = '\v';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 149) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 150) 	case 'f':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 151) 		*p = '\f';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 152) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 153) 	default:
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 154) 		return false;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 155) 	}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 156) 	*dst += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 157) 	*src += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 158) 	return true;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 159) }
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 160) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 161) static bool unescape_octal(char **src, char **dst)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 162) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 163) 	char *p = *dst, *q = *src;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 164) 	u8 num;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 165) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 166) 	if (isodigit(*q) == 0)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 167) 		return false;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 168) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 169) 	num = (*q++) & 7;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 170) 	while (num < 32 && isodigit(*q) && (q - *src < 3)) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 171) 		num <<= 3;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 172) 		num += (*q++) & 7;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 173) 	}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 174) 	*p = num;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 175) 	*dst += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 176) 	*src = q;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 177) 	return true;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 178) }
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 179) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 180) static bool unescape_hex(char **src, char **dst)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 181) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 182) 	char *p = *dst, *q = *src;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 183) 	int digit;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 184) 	u8 num;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 185) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 186) 	if (*q++ != 'x')
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 187) 		return false;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 188) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 189) 	num = digit = hex_to_bin(*q++);
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 190) 	if (digit < 0)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 191) 		return false;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 192) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 193) 	digit = hex_to_bin(*q);
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 194) 	if (digit >= 0) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 195) 		q++;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 196) 		num = (num << 4) | digit;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 197) 	}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 198) 	*p = num;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 199) 	*dst += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 200) 	*src = q;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 201) 	return true;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 202) }
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 203) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 204) static bool unescape_special(char **src, char **dst)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 205) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 206) 	char *p = *dst, *q = *src;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 207) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 208) 	switch (*q) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 209) 	case '\"':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 210) 		*p = '\"';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 211) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 212) 	case '\\':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 213) 		*p = '\\';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 214) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 215) 	case 'a':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 216) 		*p = '\a';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 217) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 218) 	case 'e':
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 219) 		*p = '\e';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 220) 		break;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 221) 	default:
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 222) 		return false;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 223) 	}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 224) 	*dst += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 225) 	*src += 1;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 226) 	return true;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 227) }
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 228) 
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 229) /**
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 230)  * string_unescape - unquote characters in the given string
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 231)  * @src:	source buffer (escaped)
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 232)  * @dst:	destination buffer (unescaped)
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 233)  * @size:	size of the destination buffer (0 to unlimit)
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 234)  * @flags:	combination of the flags.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 235)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 236)  * Description:
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 237)  * The function unquotes characters in the given string.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 238)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 239)  * Because the size of the output will be the same as or less than the size of
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 240)  * the input, the transformation may be performed in place.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 241)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 242)  * Caller must provide valid source and destination pointers. Be aware that
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 243)  * destination buffer will always be NULL-terminated. Source string must be
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 244)  * NULL-terminated as well.  The supported flags are::
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 245)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 246)  *	UNESCAPE_SPACE:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 247)  *		'\f' - form feed
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 248)  *		'\n' - new line
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 249)  *		'\r' - carriage return
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 250)  *		'\t' - horizontal tab
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 251)  *		'\v' - vertical tab
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 252)  *	UNESCAPE_OCTAL:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 253)  *		'\NNN' - byte with octal value NNN (1 to 3 digits)
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 254)  *	UNESCAPE_HEX:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 255)  *		'\xHH' - byte with hexadecimal value HH (1 to 2 digits)
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 256)  *	UNESCAPE_SPECIAL:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 257)  *		'\"' - double quote
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 258)  *		'\\' - backslash
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 259)  *		'\a' - alert (BEL)
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 260)  *		'\e' - escape
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 261)  *	UNESCAPE_ANY:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 262)  *		all previous together
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 263)  *
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 264)  * Return:
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 265)  * The amount of the characters processed to the destination buffer excluding
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 266)  * trailing '\0' is returned.
d295634e965ec (Andy Shevchenko     2014-10-13 15:55:11 -0700 267)  */
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 268) int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 269) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 270) 	char *out = dst;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 271) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 272) 	while (*src && --size) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 273) 		if (src[0] == '\\' && src[1] != '\0' && size > 1) {
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 274) 			src++;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 275) 			size--;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 276) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 277) 			if (flags & UNESCAPE_SPACE &&
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 278) 					unescape_space(&src, &out))
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 279) 				continue;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 280) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 281) 			if (flags & UNESCAPE_OCTAL &&
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 282) 					unescape_octal(&src, &out))
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 283) 				continue;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 284) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 285) 			if (flags & UNESCAPE_HEX &&
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 286) 					unescape_hex(&src, &out))
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 287) 				continue;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 288) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 289) 			if (flags & UNESCAPE_SPECIAL &&
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 290) 					unescape_special(&src, &out))
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 291) 				continue;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 292) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 293) 			*out++ = '\\';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 294) 		}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 295) 		*out++ = *src++;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 296) 	}
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 297) 	*out = '\0';
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 298) 
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 299) 	return out - dst;
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 300) }
16c7fa05829e8 (Andy Shevchenko     2013-04-30 15:27:30 -0700 301) EXPORT_SYMBOL(string_unescape);
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 302) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 303) static bool escape_passthrough(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 304) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 305) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 306) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 307) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 308) 		*out = c;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 309) 	*dst = out + 1;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 310) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 311) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 312) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 313) static bool escape_space(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 314) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 315) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 316) 	unsigned char to;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 317) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 318) 	switch (c) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 319) 	case '\n':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 320) 		to = 'n';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 321) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 322) 	case '\r':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 323) 		to = 'r';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 324) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 325) 	case '\t':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 326) 		to = 't';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 327) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 328) 	case '\v':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 329) 		to = 'v';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 330) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 331) 	case '\f':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 332) 		to = 'f';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 333) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 334) 	default:
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 335) 		return false;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 336) 	}
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 337) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 338) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 339) 		*out = '\\';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 340) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 341) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 342) 		*out = to;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 343) 	++out;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 344) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 345) 	*dst = out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 346) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 347) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 348) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 349) static bool escape_special(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 350) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 351) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 352) 	unsigned char to;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 353) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 354) 	switch (c) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 355) 	case '\\':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 356) 		to = '\\';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 357) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 358) 	case '\a':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 359) 		to = 'a';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 360) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 361) 	case '\e':
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 362) 		to = 'e';
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 363) 		break;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 364) 	default:
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 365) 		return false;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 366) 	}
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 367) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 368) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 369) 		*out = '\\';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 370) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 371) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 372) 		*out = to;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 373) 	++out;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 374) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 375) 	*dst = out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 376) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 377) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 378) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 379) static bool escape_null(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 380) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 381) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 382) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 383) 	if (c)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 384) 		return false;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 385) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 386) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 387) 		*out = '\\';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 388) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 389) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 390) 		*out = '0';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 391) 	++out;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 392) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 393) 	*dst = out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 394) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 395) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 396) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 397) static bool escape_octal(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 398) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 399) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 400) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 401) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 402) 		*out = '\\';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 403) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 404) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 405) 		*out = ((c >> 6) & 0x07) + '0';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 406) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 407) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 408) 		*out = ((c >> 3) & 0x07) + '0';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 409) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 410) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 411) 		*out = ((c >> 0) & 0x07) + '0';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 412) 	++out;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 413) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 414) 	*dst = out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 415) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 416) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 417) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 418) static bool escape_hex(unsigned char c, char **dst, char *end)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 419) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 420) 	char *out = *dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 421) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 422) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 423) 		*out = '\\';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 424) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 425) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 426) 		*out = 'x';
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 427) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 428) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 429) 		*out = hex_asc_hi(c);
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 430) 	++out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 431) 	if (out < end)
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 432) 		*out = hex_asc_lo(c);
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 433) 	++out;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 434) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 435) 	*dst = out;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 436) 	return true;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 437) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 438) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 439) /**
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 440)  * string_escape_mem - quote characters in the given memory buffer
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 441)  * @src:	source buffer (unescaped)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 442)  * @isz:	source buffer size
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 443)  * @dst:	destination buffer (escaped)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 444)  * @osz:	destination buffer size
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 445)  * @flags:	combination of the flags
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 446)  * @only:	NULL-terminated string containing characters used to limit
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 447)  *		the selected escape class. If characters are included in @only
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 448)  *		that would not normally be escaped by the classes selected
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 449)  *		in @flags, they will be copied to @dst unescaped.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 450)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 451)  * Description:
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 452)  * The process of escaping byte buffer includes several parts. They are applied
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 453)  * in the following sequence.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 454)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 455)  *	1. The character is matched to the printable class, if asked, and in
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 456)  *	   case of match it passes through to the output.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 457)  *	2. The character is not matched to the one from @only string and thus
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 458)  *	   must go as-is to the output.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 459)  *	3. The character is checked if it falls into the class given by @flags.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 460)  *	   %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 461)  *	   character. Note that they actually can't go together, otherwise
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 462)  *	   %ESCAPE_HEX will be ignored.
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 463)  *
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 464)  * Caller must provide valid source and destination pointers. Be aware that
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 465)  * destination buffer will not be NULL-terminated, thus caller have to append
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 466)  * it if needs.   The supported flags are::
b4658cdd8cab4 (Jonathan Corbet     2019-07-16 16:27:36 -0700 467)  *
d89a3f7335bb5 (Kees Cook           2015-09-09 15:37:14 -0700 468)  *	%ESCAPE_SPACE: (special white space, not space itself)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 469)  *		'\f' - form feed
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 470)  *		'\n' - new line
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 471)  *		'\r' - carriage return
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 472)  *		'\t' - horizontal tab
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 473)  *		'\v' - vertical tab
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 474)  *	%ESCAPE_SPECIAL:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 475)  *		'\\' - backslash
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 476)  *		'\a' - alert (BEL)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 477)  *		'\e' - escape
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 478)  *	%ESCAPE_NULL:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 479)  *		'\0' - null
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 480)  *	%ESCAPE_OCTAL:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 481)  *		'\NNN' - byte with octal value NNN (3 digits)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 482)  *	%ESCAPE_ANY:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 483)  *		all previous together
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 484)  *	%ESCAPE_NP:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 485)  *		escape only non-printable characters (checked by isprint)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 486)  *	%ESCAPE_ANY_NP:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 487)  *		all previous together
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 488)  *	%ESCAPE_HEX:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 489)  *		'\xHH' - byte with hexadecimal value HH (2 digits)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 490)  *
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 491)  * Return:
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 492)  * The total size of the escaped output that would be generated for
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 493)  * the given input and flags. To check whether the output was
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 494)  * truncated, compare the return value to osz. There is room left in
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 495)  * dst for a '\0' terminator if and only if ret < osz.
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 496)  */
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 497) int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
b40bdb7fb2b83 (Kees Cook           2015-09-09 15:37:16 -0700 498) 		      unsigned int flags, const char *only)
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 499) {
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 500) 	char *p = dst;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 501) 	char *end = p + osz;
b40bdb7fb2b83 (Kees Cook           2015-09-09 15:37:16 -0700 502) 	bool is_dict = only && *only;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 503) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 504) 	while (isz--) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 505) 		unsigned char c = *src++;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 506) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 507) 		/*
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 508) 		 * Apply rules in the following sequence:
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 509) 		 *	- the character is printable, when @flags has
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 510) 		 *	  %ESCAPE_NP bit set
b40bdb7fb2b83 (Kees Cook           2015-09-09 15:37:16 -0700 511) 		 *	- the @only string is supplied and does not contain a
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 512) 		 *	  character under question
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 513) 		 *	- the character doesn't fall into a class of symbols
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 514) 		 *	  defined by given @flags
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 515) 		 * In these cases we just pass through a character to the
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 516) 		 * output buffer.
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 517) 		 */
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 518) 		if ((flags & ESCAPE_NP && isprint(c)) ||
b40bdb7fb2b83 (Kees Cook           2015-09-09 15:37:16 -0700 519) 		    (is_dict && !strchr(only, c))) {
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 520) 			/* do nothing */
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 521) 		} else {
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 522) 			if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 523) 				continue;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 524) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 525) 			if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 526) 				continue;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 527) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 528) 			if (flags & ESCAPE_NULL && escape_null(c, &p, end))
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 529) 				continue;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 530) 
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 531) 			/* ESCAPE_OCTAL and ESCAPE_HEX always go last */
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 532) 			if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 533) 				continue;
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 534) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 535) 			if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 536) 				continue;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 537) 		}
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 538) 
3aeddc7d665e4 (Rasmus Villemoes    2015-04-15 16:17:25 -0700 539) 		escape_passthrough(c, &p, end);
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 540) 	}
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 541) 
41416f2330112 (Rasmus Villemoes    2015-04-15 16:17:28 -0700 542) 	return p - dst;
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 543) }
c8250381c8272 (Andy Shevchenko     2014-10-13 15:55:16 -0700 544) EXPORT_SYMBOL(string_escape_mem);
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 545) 
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 546) int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 547) 					size_t osz)
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 548) {
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 549) 	char *p = dst;
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 550) 	char *end = p + osz;
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 551) 
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 552) 	while (isz--) {
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 553) 		unsigned char c = *src++;
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 554) 
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 555) 		if (!isprint(c) || !isascii(c) || c == '"' || c == '\\')
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 556) 			escape_hex(c, &p, end);
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 557) 		else
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 558) 			escape_passthrough(c, &p, end);
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 559) 	}
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 560) 
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 561) 	return p - dst;
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 562) }
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 563) EXPORT_SYMBOL(string_escape_mem_ascii);
ea053e164cc81 (J. Bruce Fields     2019-06-19 12:30:13 -0400 564) 
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 565) /*
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 566)  * Return an allocated string that has been escaped of special characters
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 567)  * and double quotes, making it safe to log in quotes.
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 568)  */
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 569) char *kstrdup_quotable(const char *src, gfp_t gfp)
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 570) {
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 571) 	size_t slen, dlen;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 572) 	char *dst;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 573) 	const int flags = ESCAPE_HEX;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 574) 	const char esc[] = "\f\n\r\t\v\a\e\\\"";
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 575) 
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 576) 	if (!src)
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 577) 		return NULL;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 578) 	slen = strlen(src);
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 579) 
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 580) 	dlen = string_escape_mem(src, slen, NULL, 0, flags, esc);
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 581) 	dst = kmalloc(dlen + 1, gfp);
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 582) 	if (!dst)
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 583) 		return NULL;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 584) 
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 585) 	WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen);
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 586) 	dst[dlen] = '\0';
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 587) 
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 588) 	return dst;
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 589) }
b53f27e4fa0d0 (Kees Cook           2016-04-20 15:46:23 -0700 590) EXPORT_SYMBOL_GPL(kstrdup_quotable);
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 591) 
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 592) /*
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 593)  * Returns allocated NULL-terminated string containing process
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 594)  * command line, with inter-argument NULLs replaced with spaces,
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 595)  * and other special characters escaped.
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 596)  */
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 597) char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 598) {
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 599) 	char *buffer, *quoted;
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 600) 	int i, res;
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 601) 
0ee931c4e31a5 (Michal Hocko        2017-09-13 16:28:29 -0700 602) 	buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 603) 	if (!buffer)
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 604) 		return NULL;
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 605) 
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 606) 	res = get_cmdline(task, buffer, PAGE_SIZE - 1);
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 607) 	buffer[res] = '\0';
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 608) 
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 609) 	/* Collapse trailing NULLs, leave res pointing to last non-NULL. */
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 610) 	while (--res >= 0 && buffer[res] == '\0')
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 611) 		;
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 612) 
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 613) 	/* Replace inter-argument NULLs. */
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 614) 	for (i = 0; i <= res; i++)
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 615) 		if (buffer[i] == '\0')
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 616) 			buffer[i] = ' ';
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 617) 
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 618) 	/* Make sure result is printable. */
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 619) 	quoted = kstrdup_quotable(buffer, gfp);
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 620) 	kfree(buffer);
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 621) 	return quoted;
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 622) }
0d0443288f224 (Kees Cook           2016-04-20 15:46:24 -0700 623) EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 624) 
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 625) /*
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 626)  * Returns allocated NULL-terminated string containing pathname,
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 627)  * with special characters escaped, able to be safely logged. If
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 628)  * there is an error, the leading character will be "<".
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 629)  */
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 630) char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 631) {
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 632) 	char *temp, *pathname;
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 633) 
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 634) 	if (!file)
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 635) 		return kstrdup("<unknown>", gfp);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 636) 
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 637) 	/* We add 11 spaces for ' (deleted)' to be appended */
0ee931c4e31a5 (Michal Hocko        2017-09-13 16:28:29 -0700 638) 	temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 639) 	if (!temp)
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 640) 		return kstrdup("<no_memory>", gfp);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 641) 
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 642) 	pathname = file_path(file, temp, PATH_MAX + 11);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 643) 	if (IS_ERR(pathname))
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 644) 		pathname = kstrdup("<too_long>", gfp);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 645) 	else
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 646) 		pathname = kstrdup_quotable(pathname, gfp);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 647) 
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 648) 	kfree(temp);
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 649) 	return pathname;
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 650) }
21985319add60 (Kees Cook           2016-04-20 15:46:25 -0700 651) EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 652) 
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 653) /**
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 654)  * kfree_strarray - free a number of dynamically allocated strings contained
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 655)  *                  in an array and the array itself
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 656)  *
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 657)  * @array: Dynamically allocated array of strings to free.
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 658)  * @n: Number of strings (starting from the beginning of the array) to free.
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 659)  *
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 660)  * Passing a non-NULL @array and @n == 0 as well as NULL @array are valid
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 661)  * use-cases. If @array is NULL, the function does nothing.
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 662)  */
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 663) void kfree_strarray(char **array, size_t n)
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 664) {
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 665) 	unsigned int i;
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 666) 
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 667) 	if (!array)
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 668) 		return;
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 669) 
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 670) 	for (i = 0; i < n; i++)
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 671) 		kfree(array[i]);
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 672) 	kfree(array);
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 673) }
0fd16012adc0a (Bartosz Golaszewski 2020-09-29 12:09:55 +0200 674) EXPORT_SYMBOL_GPL(kfree_strarray);