blob: 9c48ddad0f0d5a5a03eb2914fdd9ecebfa1b5f9e [file] [log] [blame]
James Bottomley3c9f3682008-08-31 10:13:54 -05001/*
2 * Helpers for formatting and printing strings
3 *
4 * Copyright 31 August 2008 James Bottomley
Andy Shevchenko16c7fa02013-04-30 15:27:30 -07005 * Copyright (C) 2013, Intel Corporation
James Bottomley3c9f3682008-08-31 10:13:54 -05006 */
7#include <linux/kernel.h>
8#include <linux/math64.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Andy Shevchenko16c7fa02013-04-30 15:27:30 -070010#include <linux/ctype.h>
Andy Shevchenkoc8250382014-10-13 15:55:16 -070011#include <linux/errno.h>
12#include <linux/string.h>
James Bottomley3c9f3682008-08-31 10:13:54 -050013#include <linux/string_helpers.h>
14
15/**
16 * string_get_size - get the size in the specified units
17 * @size: The size to be converted
18 * @units: units to use (powers of 1000 or 1024)
19 * @buf: buffer to format to
20 * @len: length of buffer
21 *
22 * This function returns a string formatted to 3 significant figures
Rasmus Villemoesd1214c62015-02-12 15:01:50 -080023 * giving the size in the required units. @buf should have room for
24 * at least 9 bytes and will always be zero terminated.
James Bottomley3c9f3682008-08-31 10:13:54 -050025 *
26 */
Rasmus Villemoesd1214c62015-02-12 15:01:50 -080027void string_get_size(u64 size, const enum string_size_units units,
28 char *buf, int len)
James Bottomley3c9f3682008-08-31 10:13:54 -050029{
Mathias Krause142cda52014-08-06 16:09:31 -070030 static const char *const units_10[] = {
Rasmus Villemoes7eed8fd2015-02-12 15:01:45 -080031 "B", "kB", "MB", "GB", "TB", "PB", "EB"
Mathias Krause142cda52014-08-06 16:09:31 -070032 };
33 static const char *const units_2[] = {
Rasmus Villemoes7eed8fd2015-02-12 15:01:45 -080034 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
Mathias Krause142cda52014-08-06 16:09:31 -070035 };
36 static const char *const *const units_str[] = {
37 [STRING_UNITS_10] = units_10,
James Bottomley3c9f3682008-08-31 10:13:54 -050038 [STRING_UNITS_2] = units_2,
39 };
Andrew Morton68aecfb2012-05-29 15:07:32 -070040 static const unsigned int divisor[] = {
James Bottomley3c9f3682008-08-31 10:13:54 -050041 [STRING_UNITS_10] = 1000,
42 [STRING_UNITS_2] = 1024,
43 };
44 int i, j;
Rasmus Villemoes84b9fbe2015-02-12 15:01:48 -080045 u32 remainder = 0, sf_cap;
James Bottomley3c9f3682008-08-31 10:13:54 -050046 char tmp[8];
47
48 tmp[0] = '\0';
H. Peter Anvina8659592008-10-14 11:34:21 -070049 i = 0;
50 if (size >= divisor[units]) {
Rasmus Villemoes7eed8fd2015-02-12 15:01:45 -080051 while (size >= divisor[units]) {
H. Peter Anvina8659592008-10-14 11:34:21 -070052 remainder = do_div(size, divisor[units]);
53 i++;
54 }
James Bottomley3c9f3682008-08-31 10:13:54 -050055
H. Peter Anvina8659592008-10-14 11:34:21 -070056 sf_cap = size;
57 for (j = 0; sf_cap*10 < 1000; j++)
58 sf_cap *= 10;
James Bottomley3c9f3682008-08-31 10:13:54 -050059
H. Peter Anvina8659592008-10-14 11:34:21 -070060 if (j) {
61 remainder *= 1000;
Rasmus Villemoes84b9fbe2015-02-12 15:01:48 -080062 remainder /= divisor[units];
63 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
H. Peter Anvina8659592008-10-14 11:34:21 -070064 tmp[j+1] = '\0';
65 }
James Bottomley3c9f3682008-08-31 10:13:54 -050066 }
67
Rasmus Villemoes84b9fbe2015-02-12 15:01:48 -080068 snprintf(buf, len, "%u%s %s", (u32)size,
James Bottomley3c9f3682008-08-31 10:13:54 -050069 tmp, units_str[units][i]);
James Bottomley3c9f3682008-08-31 10:13:54 -050070}
71EXPORT_SYMBOL(string_get_size);
Andy Shevchenko16c7fa02013-04-30 15:27:30 -070072
73static bool unescape_space(char **src, char **dst)
74{
75 char *p = *dst, *q = *src;
76
77 switch (*q) {
78 case 'n':
79 *p = '\n';
80 break;
81 case 'r':
82 *p = '\r';
83 break;
84 case 't':
85 *p = '\t';
86 break;
87 case 'v':
88 *p = '\v';
89 break;
90 case 'f':
91 *p = '\f';
92 break;
93 default:
94 return false;
95 }
96 *dst += 1;
97 *src += 1;
98 return true;
99}
100
101static bool unescape_octal(char **src, char **dst)
102{
103 char *p = *dst, *q = *src;
104 u8 num;
105
106 if (isodigit(*q) == 0)
107 return false;
108
109 num = (*q++) & 7;
110 while (num < 32 && isodigit(*q) && (q - *src < 3)) {
111 num <<= 3;
112 num += (*q++) & 7;
113 }
114 *p = num;
115 *dst += 1;
116 *src = q;
117 return true;
118}
119
120static bool unescape_hex(char **src, char **dst)
121{
122 char *p = *dst, *q = *src;
123 int digit;
124 u8 num;
125
126 if (*q++ != 'x')
127 return false;
128
129 num = digit = hex_to_bin(*q++);
130 if (digit < 0)
131 return false;
132
133 digit = hex_to_bin(*q);
134 if (digit >= 0) {
135 q++;
136 num = (num << 4) | digit;
137 }
138 *p = num;
139 *dst += 1;
140 *src = q;
141 return true;
142}
143
144static bool unescape_special(char **src, char **dst)
145{
146 char *p = *dst, *q = *src;
147
148 switch (*q) {
149 case '\"':
150 *p = '\"';
151 break;
152 case '\\':
153 *p = '\\';
154 break;
155 case 'a':
156 *p = '\a';
157 break;
158 case 'e':
159 *p = '\e';
160 break;
161 default:
162 return false;
163 }
164 *dst += 1;
165 *src += 1;
166 return true;
167}
168
Andy Shevchenkod2956342014-10-13 15:55:11 -0700169/**
170 * string_unescape - unquote characters in the given string
171 * @src: source buffer (escaped)
172 * @dst: destination buffer (unescaped)
173 * @size: size of the destination buffer (0 to unlimit)
174 * @flags: combination of the flags (bitwise OR):
175 * %UNESCAPE_SPACE:
176 * '\f' - form feed
177 * '\n' - new line
178 * '\r' - carriage return
179 * '\t' - horizontal tab
180 * '\v' - vertical tab
181 * %UNESCAPE_OCTAL:
182 * '\NNN' - byte with octal value NNN (1 to 3 digits)
183 * %UNESCAPE_HEX:
184 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
185 * %UNESCAPE_SPECIAL:
186 * '\"' - double quote
187 * '\\' - backslash
188 * '\a' - alert (BEL)
189 * '\e' - escape
190 * %UNESCAPE_ANY:
191 * all previous together
192 *
193 * Description:
194 * The function unquotes characters in the given string.
195 *
196 * Because the size of the output will be the same as or less than the size of
197 * the input, the transformation may be performed in place.
198 *
199 * Caller must provide valid source and destination pointers. Be aware that
200 * destination buffer will always be NULL-terminated. Source string must be
201 * NULL-terminated as well.
202 *
203 * Return:
204 * The amount of the characters processed to the destination buffer excluding
205 * trailing '\0' is returned.
206 */
Andy Shevchenko16c7fa02013-04-30 15:27:30 -0700207int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
208{
209 char *out = dst;
210
211 while (*src && --size) {
212 if (src[0] == '\\' && src[1] != '\0' && size > 1) {
213 src++;
214 size--;
215
216 if (flags & UNESCAPE_SPACE &&
217 unescape_space(&src, &out))
218 continue;
219
220 if (flags & UNESCAPE_OCTAL &&
221 unescape_octal(&src, &out))
222 continue;
223
224 if (flags & UNESCAPE_HEX &&
225 unescape_hex(&src, &out))
226 continue;
227
228 if (flags & UNESCAPE_SPECIAL &&
229 unescape_special(&src, &out))
230 continue;
231
232 *out++ = '\\';
233 }
234 *out++ = *src++;
235 }
236 *out = '\0';
237
238 return out - dst;
239}
240EXPORT_SYMBOL(string_unescape);
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700241
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700242static bool escape_passthrough(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700243{
244 char *out = *dst;
245
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700246 if (out < end)
247 *out = c;
248 *dst = out + 1;
249 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700250}
251
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700252static bool escape_space(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700253{
254 char *out = *dst;
255 unsigned char to;
256
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700257 switch (c) {
258 case '\n':
259 to = 'n';
260 break;
261 case '\r':
262 to = 'r';
263 break;
264 case '\t':
265 to = 't';
266 break;
267 case '\v':
268 to = 'v';
269 break;
270 case '\f':
271 to = 'f';
272 break;
273 default:
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700274 return false;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700275 }
276
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700277 if (out + 2 > end) {
278 *dst = out + 2;
279 return true;
280 }
281
282 if (out < end)
283 *out = '\\';
284 ++out;
285 if (out < end)
286 *out = to;
287 ++out;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700288
289 *dst = out;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700290 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700291}
292
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700293static bool escape_special(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700294{
295 char *out = *dst;
296 unsigned char to;
297
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700298 switch (c) {
299 case '\\':
300 to = '\\';
301 break;
302 case '\a':
303 to = 'a';
304 break;
305 case '\e':
306 to = 'e';
307 break;
308 default:
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700309 return false;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700310 }
311
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700312 if (out + 2 > end) {
313 *dst = out + 2;
314 return true;
315 }
316
317 if (out < end)
318 *out = '\\';
319 ++out;
320 if (out < end)
321 *out = to;
322 ++out;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700323
324 *dst = out;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700325 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700326}
327
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700328static bool escape_null(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700329{
330 char *out = *dst;
331
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700332 if (c)
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700333 return false;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700334
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700335 if (out + 2 > end) {
336 *dst = out + 2;
337 return true;
338 }
339
340 if (out < end)
341 *out = '\\';
342 ++out;
343 if (out < end)
344 *out = '0';
345 ++out;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700346
347 *dst = out;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700348 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700349}
350
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700351static bool escape_octal(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700352{
353 char *out = *dst;
354
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700355 if (out + 4 > end) {
356 *dst = out + 4;
357 return true;
358 }
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700359
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700360 if (out < end)
361 *out = '\\';
362 ++out;
363 if (out < end)
364 *out = ((c >> 6) & 0x07) + '0';
365 ++out;
366 if (out < end)
367 *out = ((c >> 3) & 0x07) + '0';
368 ++out;
369 if (out < end)
370 *out = ((c >> 0) & 0x07) + '0';
371 ++out;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700372
373 *dst = out;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700374 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700375}
376
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700377static bool escape_hex(unsigned char c, char **dst, char *end)
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700378{
379 char *out = *dst;
380
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700381 if (out + 4 > end) {
382 *dst = out + 4;
383 return true;
384 }
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700385
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700386 if (out < end)
387 *out = '\\';
388 ++out;
389 if (out < end)
390 *out = 'x';
391 ++out;
392 if (out < end)
393 *out = hex_asc_hi(c);
394 ++out;
395 if (out < end)
396 *out = hex_asc_lo(c);
397 ++out;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700398
399 *dst = out;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700400 return true;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700401}
402
403/**
404 * string_escape_mem - quote characters in the given memory buffer
405 * @src: source buffer (unescaped)
406 * @isz: source buffer size
407 * @dst: destination buffer (escaped)
408 * @osz: destination buffer size
409 * @flags: combination of the flags (bitwise OR):
410 * %ESCAPE_SPACE:
411 * '\f' - form feed
412 * '\n' - new line
413 * '\r' - carriage return
414 * '\t' - horizontal tab
415 * '\v' - vertical tab
416 * %ESCAPE_SPECIAL:
417 * '\\' - backslash
418 * '\a' - alert (BEL)
419 * '\e' - escape
420 * %ESCAPE_NULL:
421 * '\0' - null
422 * %ESCAPE_OCTAL:
423 * '\NNN' - byte with octal value NNN (3 digits)
424 * %ESCAPE_ANY:
425 * all previous together
426 * %ESCAPE_NP:
427 * escape only non-printable characters (checked by isprint)
428 * %ESCAPE_ANY_NP:
429 * all previous together
430 * %ESCAPE_HEX:
431 * '\xHH' - byte with hexadecimal value HH (2 digits)
432 * @esc: NULL-terminated string of characters any of which, if found in
433 * the source, has to be escaped
434 *
435 * Description:
436 * The process of escaping byte buffer includes several parts. They are applied
437 * in the following sequence.
438 * 1. The character is matched to the printable class, if asked, and in
439 * case of match it passes through to the output.
440 * 2. The character is not matched to the one from @esc string and thus
441 * must go as is to the output.
442 * 3. The character is checked if it falls into the class given by @flags.
443 * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
444 * character. Note that they actually can't go together, otherwise
445 * %ESCAPE_HEX will be ignored.
446 *
447 * Caller must provide valid source and destination pointers. Be aware that
448 * destination buffer will not be NULL-terminated, thus caller have to append
449 * it if needs.
450 *
451 * Return:
452 * The amount of the characters processed to the destination buffer, or
453 * %-ENOMEM if the size of buffer is not enough to put an escaped character is
454 * returned.
455 *
456 * Even in the case of error @dst pointer will be updated to point to the byte
457 * after the last processed character.
458 */
459int string_escape_mem(const char *src, size_t isz, char **dst, size_t osz,
460 unsigned int flags, const char *esc)
461{
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700462 char *p = *dst;
463 char *end = p + osz;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700464 bool is_dict = esc && *esc;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700465 int ret;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700466
467 while (isz--) {
468 unsigned char c = *src++;
469
470 /*
471 * Apply rules in the following sequence:
472 * - the character is printable, when @flags has
473 * %ESCAPE_NP bit set
474 * - the @esc string is supplied and does not contain a
475 * character under question
476 * - the character doesn't fall into a class of symbols
477 * defined by given @flags
478 * In these cases we just pass through a character to the
479 * output buffer.
480 */
481 if ((flags & ESCAPE_NP && isprint(c)) ||
482 (is_dict && !strchr(esc, c))) {
483 /* do nothing */
484 } else {
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700485 if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
486 continue;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700487
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700488 if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
489 continue;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700490
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700491 if (flags & ESCAPE_NULL && escape_null(c, &p, end))
492 continue;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700493
494 /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700495 if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700496 continue;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700497
498 if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700499 continue;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700500 }
501
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700502 escape_passthrough(c, &p, end);
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700503 }
504
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700505 if (p > end) {
506 *dst = end;
507 return -ENOMEM;
508 }
509
510 ret = p - *dst;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700511 *dst = p;
Rasmus Villemoes3aeddc72015-04-15 16:17:25 -0700512 return ret;
Andy Shevchenkoc8250382014-10-13 15:55:16 -0700513}
514EXPORT_SYMBOL(string_escape_mem);