Blame view

lib/string_helpers.c 14.5 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
3c9f3681d   James Bottomley   [SCSI] lib: add g...
2
3
4
5
  /*
   * Helpers for formatting and printing strings
   *
   * Copyright 31 August 2008 James Bottomley
16c7fa058   Andy Shevchenko   lib/string_helper...
6
   * Copyright (C) 2013, Intel Corporation
3c9f3681d   James Bottomley   [SCSI] lib: add g...
7
   */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
8
  #include <linux/bug.h>
3c9f3681d   James Bottomley   [SCSI] lib: add g...
9
10
  #include <linux/kernel.h>
  #include <linux/math64.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
11
  #include <linux/export.h>
16c7fa058   Andy Shevchenko   lib/string_helper...
12
  #include <linux/ctype.h>
c8250381c   Andy Shevchenko   lib / string_help...
13
  #include <linux/errno.h>
21985319a   Kees Cook   string_helpers: a...
14
15
  #include <linux/fs.h>
  #include <linux/limits.h>
0d0443288   Kees Cook   string_helpers: a...
16
  #include <linux/mm.h>
b53f27e4f   Kees Cook   string_helpers: a...
17
  #include <linux/slab.h>
c8250381c   Andy Shevchenko   lib / string_help...
18
  #include <linux/string.h>
3c9f3681d   James Bottomley   [SCSI] lib: add g...
19
20
21
22
  #include <linux/string_helpers.h>
  
  /**
   * string_get_size - get the size in the specified units
b9f28d863   James Bottomley   sd, mmc, virtio_b...
23
24
   * @size:	The size to be converted in blocks
   * @blk_size:	Size of the block (use 1 for size in bytes)
3c9f3681d   James Bottomley   [SCSI] lib: add g...
25
26
27
28
29
   * @units:	units to use (powers of 1000 or 1024)
   * @buf:	buffer to format to
   * @len:	length of buffer
   *
   * This function returns a string formatted to 3 significant figures
d1214c65c   Rasmus Villemoes   libstring_helpers...
30
31
   * giving the size in the required units.  @buf should have room for
   * at least 9 bytes and will always be zero terminated.
3c9f3681d   James Bottomley   [SCSI] lib: add g...
32
33
   *
   */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
34
  void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
d1214c65c   Rasmus Villemoes   libstring_helpers...
35
  		     char *buf, int len)
3c9f3681d   James Bottomley   [SCSI] lib: add g...
36
  {
142cda5db   Mathias Krause   lib/string_helper...
37
  	static const char *const units_10[] = {
b9f28d863   James Bottomley   sd, mmc, virtio_b...
38
  		"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
142cda5db   Mathias Krause   lib/string_helper...
39
40
  	};
  	static const char *const units_2[] = {
b9f28d863   James Bottomley   sd, mmc, virtio_b...
41
  		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
142cda5db   Mathias Krause   lib/string_helper...
42
43
44
  	};
  	static const char *const *const units_str[] = {
  		[STRING_UNITS_10] = units_10,
3c9f3681d   James Bottomley   [SCSI] lib: add g...
45
46
  		[STRING_UNITS_2] = units_2,
  	};
68aecfb97   Andrew Morton   lib/string_helper...
47
  	static const unsigned int divisor[] = {
3c9f3681d   James Bottomley   [SCSI] lib: add g...
48
49
50
  		[STRING_UNITS_10] = 1000,
  		[STRING_UNITS_2] = 1024,
  	};
564b026fb   James Bottomley   string_helpers: f...
51
52
53
  	static const unsigned int rounding[] = { 500, 50, 5 };
  	int i = 0, j;
  	u32 remainder = 0, sf_cap;
3c9f3681d   James Bottomley   [SCSI] lib: add g...
54
  	char tmp[8];
b9f28d863   James Bottomley   sd, mmc, virtio_b...
55
  	const char *unit;
3c9f3681d   James Bottomley   [SCSI] lib: add g...
56
57
  
  	tmp[0] = '\0';
564b026fb   James Bottomley   string_helpers: f...
58
59
60
61
  
  	if (blk_size == 0)
  		size = 0;
  	if (size == 0)
b9f28d863   James Bottomley   sd, mmc, virtio_b...
62
  		goto out;
3c9f3681d   James Bottomley   [SCSI] lib: add g...
63

564b026fb   James Bottomley   string_helpers: f...
64
65
66
67
68
69
70
71
72
73
74
75
76
  	/* This is Napier's algorithm.  Reduce the original block size to
  	 *
  	 * coefficient * divisor[units]^i
  	 *
  	 * we do the reduction so both coefficients are just under 32 bits so
  	 * that multiplying them together won't overflow 64 bits and we keep
  	 * as much precision as possible in the numbers.
  	 *
  	 * Note: it's safe to throw away the remainders here because all the
  	 * precision is in the coefficients.
  	 */
  	while (blk_size >> 32) {
  		do_div(blk_size, divisor[units]);
b9f28d863   James Bottomley   sd, mmc, virtio_b...
77
78
  		i++;
  	}
564b026fb   James Bottomley   string_helpers: f...
79
80
  	while (size >> 32) {
  		do_div(size, divisor[units]);
b9f28d863   James Bottomley   sd, mmc, virtio_b...
81
  		i++;
b9f28d863   James Bottomley   sd, mmc, virtio_b...
82
  	}
564b026fb   James Bottomley   string_helpers: f...
83
84
  	/* now perform the actual multiplication keeping i as the sum of the
  	 * two logarithms */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
85
  	size *= blk_size;
3c9f3681d   James Bottomley   [SCSI] lib: add g...
86

564b026fb   James Bottomley   string_helpers: f...
87
  	/* and logarithmically reduce it until it's just under the divisor */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
88
89
90
  	while (size >= divisor[units]) {
  		remainder = do_div(size, divisor[units]);
  		i++;
3c9f3681d   James Bottomley   [SCSI] lib: add g...
91
  	}
564b026fb   James Bottomley   string_helpers: f...
92
93
  	/* work out in j how many digits of precision we need from the
  	 * remainder */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
94
95
96
  	sf_cap = size;
  	for (j = 0; sf_cap*10 < 1000; j++)
  		sf_cap *= 10;
564b026fb   James Bottomley   string_helpers: f...
97
98
99
100
  	if (units == STRING_UNITS_2) {
  		/* express the remainder as a decimal.  It's currently the
  		 * numerator of a fraction whose denominator is
  		 * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
b9f28d863   James Bottomley   sd, mmc, virtio_b...
101
  		remainder *= 1000;
564b026fb   James Bottomley   string_helpers: f...
102
103
104
105
106
107
108
109
110
111
112
113
  		remainder >>= 10;
  	}
  
  	/* add a 5 to the digit below what will be printed to ensure
  	 * an arithmetical round up and carry it through to size */
  	remainder += rounding[j];
  	if (remainder >= 1000) {
  		remainder -= 1000;
  		size += 1;
  	}
  
  	if (j) {
b9f28d863   James Bottomley   sd, mmc, virtio_b...
114
115
116
117
118
119
120
121
122
  		snprintf(tmp, sizeof(tmp), ".%03u", remainder);
  		tmp[j+1] = '\0';
  	}
  
   out:
  	if (i >= ARRAY_SIZE(units_2))
  		unit = "UNK";
  	else
  		unit = units_str[units][i];
84b9fbedf   Rasmus Villemoes   lib/string_helper...
123
  	snprintf(buf, len, "%u%s %s", (u32)size,
b9f28d863   James Bottomley   sd, mmc, virtio_b...
124
  		 tmp, unit);
3c9f3681d   James Bottomley   [SCSI] lib: add g...
125
126
  }
  EXPORT_SYMBOL(string_get_size);
16c7fa058   Andy Shevchenko   lib/string_helper...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  
  static bool unescape_space(char **src, char **dst)
  {
  	char *p = *dst, *q = *src;
  
  	switch (*q) {
  	case 'n':
  		*p = '
  ';
  		break;
  	case 'r':
  		*p = '\r';
  		break;
  	case 't':
  		*p = '\t';
  		break;
  	case 'v':
  		*p = '\v';
  		break;
  	case 'f':
  		*p = '\f';
  		break;
  	default:
  		return false;
  	}
  	*dst += 1;
  	*src += 1;
  	return true;
  }
  
  static bool unescape_octal(char **src, char **dst)
  {
  	char *p = *dst, *q = *src;
  	u8 num;
  
  	if (isodigit(*q) == 0)
  		return false;
  
  	num = (*q++) & 7;
  	while (num < 32 && isodigit(*q) && (q - *src < 3)) {
  		num <<= 3;
  		num += (*q++) & 7;
  	}
  	*p = num;
  	*dst += 1;
  	*src = q;
  	return true;
  }
  
  static bool unescape_hex(char **src, char **dst)
  {
  	char *p = *dst, *q = *src;
  	int digit;
  	u8 num;
  
  	if (*q++ != 'x')
  		return false;
  
  	num = digit = hex_to_bin(*q++);
  	if (digit < 0)
  		return false;
  
  	digit = hex_to_bin(*q);
  	if (digit >= 0) {
  		q++;
  		num = (num << 4) | digit;
  	}
  	*p = num;
  	*dst += 1;
  	*src = q;
  	return true;
  }
  
  static bool unescape_special(char **src, char **dst)
  {
  	char *p = *dst, *q = *src;
  
  	switch (*q) {
  	case '\"':
  		*p = '\"';
  		break;
  	case '\\':
  		*p = '\\';
  		break;
  	case 'a':
  		*p = '\a';
  		break;
  	case 'e':
  		*p = '\e';
  		break;
  	default:
  		return false;
  	}
  	*dst += 1;
  	*src += 1;
  	return true;
  }
d295634e9   Andy Shevchenko   lib / string_help...
224
225
226
227
228
  /**
   * string_unescape - unquote characters in the given string
   * @src:	source buffer (escaped)
   * @dst:	destination buffer (unescaped)
   * @size:	size of the destination buffer (0 to unlimit)
b4658cdd8   Jonathan Corbet   lib/string_helper...
229
230
231
232
233
234
235
236
237
238
239
240
241
   * @flags:	combination of the flags.
   *
   * Description:
   * The function unquotes characters in the given string.
   *
   * Because the size of the output will be the same as or less than the size of
   * the input, the transformation may be performed in place.
   *
   * Caller must provide valid source and destination pointers. Be aware that
   * destination buffer will always be NULL-terminated. Source string must be
   * NULL-terminated as well.  The supported flags are::
   *
   *	UNESCAPE_SPACE:
d295634e9   Andy Shevchenko   lib / string_help...
242
243
244
245
246
247
   *		'\f' - form feed
   *		'
  ' - new line
   *		'\r' - carriage return
   *		'\t' - horizontal tab
   *		'\v' - vertical tab
b4658cdd8   Jonathan Corbet   lib/string_helper...
248
   *	UNESCAPE_OCTAL:
d295634e9   Andy Shevchenko   lib / string_help...
249
   *		'\NNN' - byte with octal value NNN (1 to 3 digits)
b4658cdd8   Jonathan Corbet   lib/string_helper...
250
   *	UNESCAPE_HEX:
d295634e9   Andy Shevchenko   lib / string_help...
251
   *		'\xHH' - byte with hexadecimal value HH (1 to 2 digits)
b4658cdd8   Jonathan Corbet   lib/string_helper...
252
   *	UNESCAPE_SPECIAL:
d295634e9   Andy Shevchenko   lib / string_help...
253
254
255
256
   *		'\"' - double quote
   *		'\\' - backslash
   *		'\a' - alert (BEL)
   *		'\e' - escape
b4658cdd8   Jonathan Corbet   lib/string_helper...
257
   *	UNESCAPE_ANY:
d295634e9   Andy Shevchenko   lib / string_help...
258
259
   *		all previous together
   *
d295634e9   Andy Shevchenko   lib / string_help...
260
261
262
263
   * Return:
   * The amount of the characters processed to the destination buffer excluding
   * trailing '\0' is returned.
   */
16c7fa058   Andy Shevchenko   lib/string_helper...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
  {
  	char *out = dst;
  
  	while (*src && --size) {
  		if (src[0] == '\\' && src[1] != '\0' && size > 1) {
  			src++;
  			size--;
  
  			if (flags & UNESCAPE_SPACE &&
  					unescape_space(&src, &out))
  				continue;
  
  			if (flags & UNESCAPE_OCTAL &&
  					unescape_octal(&src, &out))
  				continue;
  
  			if (flags & UNESCAPE_HEX &&
  					unescape_hex(&src, &out))
  				continue;
  
  			if (flags & UNESCAPE_SPECIAL &&
  					unescape_special(&src, &out))
  				continue;
  
  			*out++ = '\\';
  		}
  		*out++ = *src++;
  	}
  	*out = '\0';
  
  	return out - dst;
  }
  EXPORT_SYMBOL(string_unescape);
c8250381c   Andy Shevchenko   lib / string_help...
298

3aeddc7d6   Rasmus Villemoes   lib/string_helper...
299
  static bool escape_passthrough(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
300
301
  {
  	char *out = *dst;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
302
303
304
305
  	if (out < end)
  		*out = c;
  	*dst = out + 1;
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
306
  }
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
307
  static bool escape_space(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
308
309
310
  {
  	char *out = *dst;
  	unsigned char to;
c8250381c   Andy Shevchenko   lib / string_help...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  	switch (c) {
  	case '
  ':
  		to = 'n';
  		break;
  	case '\r':
  		to = 'r';
  		break;
  	case '\t':
  		to = 't';
  		break;
  	case '\v':
  		to = 'v';
  		break;
  	case '\f':
  		to = 'f';
  		break;
  	default:
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
329
  		return false;
c8250381c   Andy Shevchenko   lib / string_help...
330
  	}
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
331
332
333
334
335
336
  	if (out < end)
  		*out = '\\';
  	++out;
  	if (out < end)
  		*out = to;
  	++out;
c8250381c   Andy Shevchenko   lib / string_help...
337

3aeddc7d6   Rasmus Villemoes   lib/string_helper...
338
339
  	*dst = out;
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
340
  }
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
341
  static bool escape_special(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
342
343
344
  {
  	char *out = *dst;
  	unsigned char to;
c8250381c   Andy Shevchenko   lib / string_help...
345
346
347
348
349
350
351
352
353
354
355
  	switch (c) {
  	case '\\':
  		to = '\\';
  		break;
  	case '\a':
  		to = 'a';
  		break;
  	case '\e':
  		to = 'e';
  		break;
  	default:
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
356
  		return false;
c8250381c   Andy Shevchenko   lib / string_help...
357
  	}
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
358
359
360
361
362
363
  	if (out < end)
  		*out = '\\';
  	++out;
  	if (out < end)
  		*out = to;
  	++out;
c8250381c   Andy Shevchenko   lib / string_help...
364

3aeddc7d6   Rasmus Villemoes   lib/string_helper...
365
366
  	*dst = out;
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
367
  }
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
368
  static bool escape_null(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
369
370
  {
  	char *out = *dst;
c8250381c   Andy Shevchenko   lib / string_help...
371
  	if (c)
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
372
  		return false;
c8250381c   Andy Shevchenko   lib / string_help...
373

3aeddc7d6   Rasmus Villemoes   lib/string_helper...
374
375
376
377
378
379
  	if (out < end)
  		*out = '\\';
  	++out;
  	if (out < end)
  		*out = '0';
  	++out;
c8250381c   Andy Shevchenko   lib / string_help...
380

3aeddc7d6   Rasmus Villemoes   lib/string_helper...
381
382
  	*dst = out;
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
383
  }
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
384
  static bool escape_octal(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
385
386
  {
  	char *out = *dst;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
387
388
389
390
391
392
393
394
395
396
397
398
  	if (out < end)
  		*out = '\\';
  	++out;
  	if (out < end)
  		*out = ((c >> 6) & 0x07) + '0';
  	++out;
  	if (out < end)
  		*out = ((c >> 3) & 0x07) + '0';
  	++out;
  	if (out < end)
  		*out = ((c >> 0) & 0x07) + '0';
  	++out;
c8250381c   Andy Shevchenko   lib / string_help...
399
400
  
  	*dst = out;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
401
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
402
  }
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
403
  static bool escape_hex(unsigned char c, char **dst, char *end)
c8250381c   Andy Shevchenko   lib / string_help...
404
405
  {
  	char *out = *dst;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
406
407
408
409
410
411
412
413
414
415
416
417
  	if (out < end)
  		*out = '\\';
  	++out;
  	if (out < end)
  		*out = 'x';
  	++out;
  	if (out < end)
  		*out = hex_asc_hi(c);
  	++out;
  	if (out < end)
  		*out = hex_asc_lo(c);
  	++out;
c8250381c   Andy Shevchenko   lib / string_help...
418
419
  
  	*dst = out;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
420
  	return true;
c8250381c   Andy Shevchenko   lib / string_help...
421
422
423
424
425
426
427
428
  }
  
  /**
   * string_escape_mem - quote characters in the given memory buffer
   * @src:	source buffer (unescaped)
   * @isz:	source buffer size
   * @dst:	destination buffer (escaped)
   * @osz:	destination buffer size
b4658cdd8   Jonathan Corbet   lib/string_helper...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
   * @flags:	combination of the flags
   * @only:	NULL-terminated string containing characters used to limit
   *		the selected escape class. If characters are included in @only
   *		that would not normally be escaped by the classes selected
   *		in @flags, they will be copied to @dst unescaped.
   *
   * Description:
   * The process of escaping byte buffer includes several parts. They are applied
   * in the following sequence.
   *
   *	1. The character is matched to the printable class, if asked, and in
   *	   case of match it passes through to the output.
   *	2. The character is not matched to the one from @only string and thus
   *	   must go as-is to the output.
   *	3. The character is checked if it falls into the class given by @flags.
   *	   %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
   *	   character. Note that they actually can't go together, otherwise
   *	   %ESCAPE_HEX will be ignored.
   *
   * Caller must provide valid source and destination pointers. Be aware that
   * destination buffer will not be NULL-terminated, thus caller have to append
   * it if needs.   The supported flags are::
   *
d89a3f733   Kees Cook   lib/string_helper...
452
   *	%ESCAPE_SPACE: (special white space, not space itself)
c8250381c   Andy Shevchenko   lib / string_help...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
   *		'\f' - form feed
   *		'
  ' - new line
   *		'\r' - carriage return
   *		'\t' - horizontal tab
   *		'\v' - vertical tab
   *	%ESCAPE_SPECIAL:
   *		'\\' - backslash
   *		'\a' - alert (BEL)
   *		'\e' - escape
   *	%ESCAPE_NULL:
   *		'\0' - null
   *	%ESCAPE_OCTAL:
   *		'\NNN' - byte with octal value NNN (3 digits)
   *	%ESCAPE_ANY:
   *		all previous together
   *	%ESCAPE_NP:
   *		escape only non-printable characters (checked by isprint)
   *	%ESCAPE_ANY_NP:
   *		all previous together
   *	%ESCAPE_HEX:
   *		'\xHH' - byte with hexadecimal value HH (2 digits)
c8250381c   Andy Shevchenko   lib / string_help...
475
476
   *
   * Return:
41416f233   Rasmus Villemoes   lib/string_helper...
477
478
479
480
   * The total size of the escaped output that would be generated for
   * the given input and flags. To check whether the output was
   * truncated, compare the return value to osz. There is room left in
   * dst for a '\0' terminator if and only if ret < osz.
c8250381c   Andy Shevchenko   lib / string_help...
481
   */
41416f233   Rasmus Villemoes   lib/string_helper...
482
  int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
b40bdb7fb   Kees Cook   lib/string_helper...
483
  		      unsigned int flags, const char *only)
c8250381c   Andy Shevchenko   lib / string_help...
484
  {
41416f233   Rasmus Villemoes   lib/string_helper...
485
  	char *p = dst;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
486
  	char *end = p + osz;
b40bdb7fb   Kees Cook   lib/string_helper...
487
  	bool is_dict = only && *only;
c8250381c   Andy Shevchenko   lib / string_help...
488
489
490
491
492
493
494
495
  
  	while (isz--) {
  		unsigned char c = *src++;
  
  		/*
  		 * Apply rules in the following sequence:
  		 *	- the character is printable, when @flags has
  		 *	  %ESCAPE_NP bit set
b40bdb7fb   Kees Cook   lib/string_helper...
496
  		 *	- the @only string is supplied and does not contain a
c8250381c   Andy Shevchenko   lib / string_help...
497
498
499
500
501
502
503
  		 *	  character under question
  		 *	- the character doesn't fall into a class of symbols
  		 *	  defined by given @flags
  		 * In these cases we just pass through a character to the
  		 * output buffer.
  		 */
  		if ((flags & ESCAPE_NP && isprint(c)) ||
b40bdb7fb   Kees Cook   lib/string_helper...
504
  		    (is_dict && !strchr(only, c))) {
c8250381c   Andy Shevchenko   lib / string_help...
505
506
  			/* do nothing */
  		} else {
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
507
508
509
510
511
512
513
514
  			if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
  				continue;
  
  			if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
  				continue;
  
  			if (flags & ESCAPE_NULL && escape_null(c, &p, end))
  				continue;
c8250381c   Andy Shevchenko   lib / string_help...
515
516
  
  			/* ESCAPE_OCTAL and ESCAPE_HEX always go last */
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
517
  			if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
c8250381c   Andy Shevchenko   lib / string_help...
518
  				continue;
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
519
520
  
  			if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
c8250381c   Andy Shevchenko   lib / string_help...
521
  				continue;
c8250381c   Andy Shevchenko   lib / string_help...
522
  		}
3aeddc7d6   Rasmus Villemoes   lib/string_helper...
523
  		escape_passthrough(c, &p, end);
c8250381c   Andy Shevchenko   lib / string_help...
524
  	}
41416f233   Rasmus Villemoes   lib/string_helper...
525
  	return p - dst;
c8250381c   Andy Shevchenko   lib / string_help...
526
527
  }
  EXPORT_SYMBOL(string_escape_mem);
b53f27e4f   Kees Cook   string_helpers: a...
528

ea053e164   J. Bruce Fields   nfsd: escape high...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
  					size_t osz)
  {
  	char *p = dst;
  	char *end = p + osz;
  
  	while (isz--) {
  		unsigned char c = *src++;
  
  		if (!isprint(c) || !isascii(c) || c == '"' || c == '\\')
  			escape_hex(c, &p, end);
  		else
  			escape_passthrough(c, &p, end);
  	}
  
  	return p - dst;
  }
  EXPORT_SYMBOL(string_escape_mem_ascii);
b53f27e4f   Kees Cook   string_helpers: a...
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  /*
   * Return an allocated string that has been escaped of special characters
   * and double quotes, making it safe to log in quotes.
   */
  char *kstrdup_quotable(const char *src, gfp_t gfp)
  {
  	size_t slen, dlen;
  	char *dst;
  	const int flags = ESCAPE_HEX;
  	const char esc[] = "\f
  \r\t\v\a\e\\\"";
  
  	if (!src)
  		return NULL;
  	slen = strlen(src);
  
  	dlen = string_escape_mem(src, slen, NULL, 0, flags, esc);
  	dst = kmalloc(dlen + 1, gfp);
  	if (!dst)
  		return NULL;
  
  	WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen);
  	dst[dlen] = '\0';
  
  	return dst;
  }
  EXPORT_SYMBOL_GPL(kstrdup_quotable);
0d0443288   Kees Cook   string_helpers: a...
574
575
576
577
578
579
580
581
582
583
  
  /*
   * Returns allocated NULL-terminated string containing process
   * command line, with inter-argument NULLs replaced with spaces,
   * and other special characters escaped.
   */
  char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
  {
  	char *buffer, *quoted;
  	int i, res;
0ee931c4e   Michal Hocko   mm: treewide: rem...
584
  	buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
0d0443288   Kees Cook   string_helpers: a...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  	if (!buffer)
  		return NULL;
  
  	res = get_cmdline(task, buffer, PAGE_SIZE - 1);
  	buffer[res] = '\0';
  
  	/* Collapse trailing NULLs, leave res pointing to last non-NULL. */
  	while (--res >= 0 && buffer[res] == '\0')
  		;
  
  	/* Replace inter-argument NULLs. */
  	for (i = 0; i <= res; i++)
  		if (buffer[i] == '\0')
  			buffer[i] = ' ';
  
  	/* Make sure result is printable. */
  	quoted = kstrdup_quotable(buffer, gfp);
  	kfree(buffer);
  	return quoted;
  }
  EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);
21985319a   Kees Cook   string_helpers: a...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
  
  /*
   * Returns allocated NULL-terminated string containing pathname,
   * with special characters escaped, able to be safely logged. If
   * there is an error, the leading character will be "<".
   */
  char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
  {
  	char *temp, *pathname;
  
  	if (!file)
  		return kstrdup("<unknown>", gfp);
  
  	/* We add 11 spaces for ' (deleted)' to be appended */
0ee931c4e   Michal Hocko   mm: treewide: rem...
620
  	temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
21985319a   Kees Cook   string_helpers: a...
621
622
623
624
625
626
627
628
629
630
631
632
633
  	if (!temp)
  		return kstrdup("<no_memory>", gfp);
  
  	pathname = file_path(file, temp, PATH_MAX + 11);
  	if (IS_ERR(pathname))
  		pathname = kstrdup("<too_long>", gfp);
  	else
  		pathname = kstrdup_quotable(pathname, gfp);
  
  	kfree(temp);
  	return pathname;
  }
  EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
0fd16012a   Bartosz Golaszewski   lib: string_helpe...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  
  /**
   * kfree_strarray - free a number of dynamically allocated strings contained
   *                  in an array and the array itself
   *
   * @array: Dynamically allocated array of strings to free.
   * @n: Number of strings (starting from the beginning of the array) to free.
   *
   * Passing a non-NULL @array and @n == 0 as well as NULL @array are valid
   * use-cases. If @array is NULL, the function does nothing.
   */
  void kfree_strarray(char **array, size_t n)
  {
  	unsigned int i;
  
  	if (!array)
  		return;
  
  	for (i = 0; i < n; i++)
  		kfree(array[i]);
  	kfree(array);
  }
  EXPORT_SYMBOL_GPL(kfree_strarray);