]>
Commit | Line | Data |
---|---|---|
3c9f3681 JB |
1 | /* |
2 | * Helpers for formatting and printing strings | |
3 | * | |
4 | * Copyright 31 August 2008 James Bottomley | |
16c7fa05 | 5 | * Copyright (C) 2013, Intel Corporation |
3c9f3681 | 6 | */ |
b9f28d86 | 7 | #include <linux/bug.h> |
3c9f3681 JB |
8 | #include <linux/kernel.h> |
9 | #include <linux/math64.h> | |
8bc3bcc9 | 10 | #include <linux/export.h> |
16c7fa05 | 11 | #include <linux/ctype.h> |
c8250381 | 12 | #include <linux/errno.h> |
b53f27e4 | 13 | #include <linux/slab.h> |
c8250381 | 14 | #include <linux/string.h> |
3c9f3681 JB |
15 | #include <linux/string_helpers.h> |
16 | ||
17 | /** | |
18 | * string_get_size - get the size in the specified units | |
b9f28d86 JB |
19 | * @size: The size to be converted in blocks |
20 | * @blk_size: Size of the block (use 1 for size in bytes) | |
3c9f3681 JB |
21 | * @units: units to use (powers of 1000 or 1024) |
22 | * @buf: buffer to format to | |
23 | * @len: length of buffer | |
24 | * | |
25 | * This function returns a string formatted to 3 significant figures | |
d1214c65 RV |
26 | * giving the size in the required units. @buf should have room for |
27 | * at least 9 bytes and will always be zero terminated. | |
3c9f3681 JB |
28 | * |
29 | */ | |
b9f28d86 | 30 | void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, |
d1214c65 | 31 | char *buf, int len) |
3c9f3681 | 32 | { |
142cda5d | 33 | static const char *const units_10[] = { |
b9f28d86 | 34 | "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" |
142cda5d MK |
35 | }; |
36 | static const char *const units_2[] = { | |
b9f28d86 | 37 | "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" |
142cda5d MK |
38 | }; |
39 | static const char *const *const units_str[] = { | |
40 | [STRING_UNITS_10] = units_10, | |
3c9f3681 JB |
41 | [STRING_UNITS_2] = units_2, |
42 | }; | |
68aecfb9 | 43 | static const unsigned int divisor[] = { |
3c9f3681 JB |
44 | [STRING_UNITS_10] = 1000, |
45 | [STRING_UNITS_2] = 1024, | |
46 | }; | |
564b026f JB |
47 | static const unsigned int rounding[] = { 500, 50, 5 }; |
48 | int i = 0, j; | |
49 | u32 remainder = 0, sf_cap; | |
3c9f3681 | 50 | char tmp[8]; |
b9f28d86 | 51 | const char *unit; |
3c9f3681 JB |
52 | |
53 | tmp[0] = '\0'; | |
564b026f JB |
54 | |
55 | if (blk_size == 0) | |
56 | size = 0; | |
57 | if (size == 0) | |
b9f28d86 | 58 | goto out; |
3c9f3681 | 59 | |
564b026f JB |
60 | /* This is Napier's algorithm. Reduce the original block size to |
61 | * | |
62 | * coefficient * divisor[units]^i | |
63 | * | |
64 | * we do the reduction so both coefficients are just under 32 bits so | |
65 | * that multiplying them together won't overflow 64 bits and we keep | |
66 | * as much precision as possible in the numbers. | |
67 | * | |
68 | * Note: it's safe to throw away the remainders here because all the | |
69 | * precision is in the coefficients. | |
70 | */ | |
71 | while (blk_size >> 32) { | |
72 | do_div(blk_size, divisor[units]); | |
b9f28d86 JB |
73 | i++; |
74 | } | |
75 | ||
564b026f JB |
76 | while (size >> 32) { |
77 | do_div(size, divisor[units]); | |
b9f28d86 | 78 | i++; |
b9f28d86 JB |
79 | } |
80 | ||
564b026f JB |
81 | /* now perform the actual multiplication keeping i as the sum of the |
82 | * two logarithms */ | |
b9f28d86 | 83 | size *= blk_size; |
3c9f3681 | 84 | |
564b026f | 85 | /* and logarithmically reduce it until it's just under the divisor */ |
b9f28d86 JB |
86 | while (size >= divisor[units]) { |
87 | remainder = do_div(size, divisor[units]); | |
88 | i++; | |
3c9f3681 JB |
89 | } |
90 | ||
564b026f JB |
91 | /* work out in j how many digits of precision we need from the |
92 | * remainder */ | |
b9f28d86 JB |
93 | sf_cap = size; |
94 | for (j = 0; sf_cap*10 < 1000; j++) | |
95 | sf_cap *= 10; | |
96 | ||
564b026f JB |
97 | if (units == STRING_UNITS_2) { |
98 | /* express the remainder as a decimal. It's currently the | |
99 | * numerator of a fraction whose denominator is | |
100 | * divisor[units], which is 1 << 10 for STRING_UNITS_2 */ | |
b9f28d86 | 101 | remainder *= 1000; |
564b026f JB |
102 | remainder >>= 10; |
103 | } | |
104 | ||
105 | /* add a 5 to the digit below what will be printed to ensure | |
106 | * an arithmetical round up and carry it through to size */ | |
107 | remainder += rounding[j]; | |
108 | if (remainder >= 1000) { | |
109 | remainder -= 1000; | |
110 | size += 1; | |
111 | } | |
112 | ||
113 | if (j) { | |
b9f28d86 JB |
114 | snprintf(tmp, sizeof(tmp), ".%03u", remainder); |
115 | tmp[j+1] = '\0'; | |
116 | } | |
117 | ||
118 | out: | |
119 | if (i >= ARRAY_SIZE(units_2)) | |
120 | unit = "UNK"; | |
121 | else | |
122 | unit = units_str[units][i]; | |
123 | ||
84b9fbed | 124 | snprintf(buf, len, "%u%s %s", (u32)size, |
b9f28d86 | 125 | tmp, unit); |
3c9f3681 JB |
126 | } |
127 | EXPORT_SYMBOL(string_get_size); | |
16c7fa05 AS |
128 | |
129 | static bool unescape_space(char **src, char **dst) | |
130 | { | |
131 | char *p = *dst, *q = *src; | |
132 | ||
133 | switch (*q) { | |
134 | case 'n': | |
135 | *p = '\n'; | |
136 | break; | |
137 | case 'r': | |
138 | *p = '\r'; | |
139 | break; | |
140 | case 't': | |
141 | *p = '\t'; | |
142 | break; | |
143 | case 'v': | |
144 | *p = '\v'; | |
145 | break; | |
146 | case 'f': | |
147 | *p = '\f'; | |
148 | break; | |
149 | default: | |
150 | return false; | |
151 | } | |
152 | *dst += 1; | |
153 | *src += 1; | |
154 | return true; | |
155 | } | |
156 | ||
157 | static bool unescape_octal(char **src, char **dst) | |
158 | { | |
159 | char *p = *dst, *q = *src; | |
160 | u8 num; | |
161 | ||
162 | if (isodigit(*q) == 0) | |
163 | return false; | |
164 | ||
165 | num = (*q++) & 7; | |
166 | while (num < 32 && isodigit(*q) && (q - *src < 3)) { | |
167 | num <<= 3; | |
168 | num += (*q++) & 7; | |
169 | } | |
170 | *p = num; | |
171 | *dst += 1; | |
172 | *src = q; | |
173 | return true; | |
174 | } | |
175 | ||
176 | static bool unescape_hex(char **src, char **dst) | |
177 | { | |
178 | char *p = *dst, *q = *src; | |
179 | int digit; | |
180 | u8 num; | |
181 | ||
182 | if (*q++ != 'x') | |
183 | return false; | |
184 | ||
185 | num = digit = hex_to_bin(*q++); | |
186 | if (digit < 0) | |
187 | return false; | |
188 | ||
189 | digit = hex_to_bin(*q); | |
190 | if (digit >= 0) { | |
191 | q++; | |
192 | num = (num << 4) | digit; | |
193 | } | |
194 | *p = num; | |
195 | *dst += 1; | |
196 | *src = q; | |
197 | return true; | |
198 | } | |
199 | ||
200 | static bool unescape_special(char **src, char **dst) | |
201 | { | |
202 | char *p = *dst, *q = *src; | |
203 | ||
204 | switch (*q) { | |
205 | case '\"': | |
206 | *p = '\"'; | |
207 | break; | |
208 | case '\\': | |
209 | *p = '\\'; | |
210 | break; | |
211 | case 'a': | |
212 | *p = '\a'; | |
213 | break; | |
214 | case 'e': | |
215 | *p = '\e'; | |
216 | break; | |
217 | default: | |
218 | return false; | |
219 | } | |
220 | *dst += 1; | |
221 | *src += 1; | |
222 | return true; | |
223 | } | |
224 | ||
d295634e AS |
225 | /** |
226 | * string_unescape - unquote characters in the given string | |
227 | * @src: source buffer (escaped) | |
228 | * @dst: destination buffer (unescaped) | |
229 | * @size: size of the destination buffer (0 to unlimit) | |
230 | * @flags: combination of the flags (bitwise OR): | |
231 | * %UNESCAPE_SPACE: | |
232 | * '\f' - form feed | |
233 | * '\n' - new line | |
234 | * '\r' - carriage return | |
235 | * '\t' - horizontal tab | |
236 | * '\v' - vertical tab | |
237 | * %UNESCAPE_OCTAL: | |
238 | * '\NNN' - byte with octal value NNN (1 to 3 digits) | |
239 | * %UNESCAPE_HEX: | |
240 | * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) | |
241 | * %UNESCAPE_SPECIAL: | |
242 | * '\"' - double quote | |
243 | * '\\' - backslash | |
244 | * '\a' - alert (BEL) | |
245 | * '\e' - escape | |
246 | * %UNESCAPE_ANY: | |
247 | * all previous together | |
248 | * | |
249 | * Description: | |
250 | * The function unquotes characters in the given string. | |
251 | * | |
252 | * Because the size of the output will be the same as or less than the size of | |
253 | * the input, the transformation may be performed in place. | |
254 | * | |
255 | * Caller must provide valid source and destination pointers. Be aware that | |
256 | * destination buffer will always be NULL-terminated. Source string must be | |
257 | * NULL-terminated as well. | |
258 | * | |
259 | * Return: | |
260 | * The amount of the characters processed to the destination buffer excluding | |
261 | * trailing '\0' is returned. | |
262 | */ | |
16c7fa05 AS |
263 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags) |
264 | { | |
265 | char *out = dst; | |
266 | ||
267 | while (*src && --size) { | |
268 | if (src[0] == '\\' && src[1] != '\0' && size > 1) { | |
269 | src++; | |
270 | size--; | |
271 | ||
272 | if (flags & UNESCAPE_SPACE && | |
273 | unescape_space(&src, &out)) | |
274 | continue; | |
275 | ||
276 | if (flags & UNESCAPE_OCTAL && | |
277 | unescape_octal(&src, &out)) | |
278 | continue; | |
279 | ||
280 | if (flags & UNESCAPE_HEX && | |
281 | unescape_hex(&src, &out)) | |
282 | continue; | |
283 | ||
284 | if (flags & UNESCAPE_SPECIAL && | |
285 | unescape_special(&src, &out)) | |
286 | continue; | |
287 | ||
288 | *out++ = '\\'; | |
289 | } | |
290 | *out++ = *src++; | |
291 | } | |
292 | *out = '\0'; | |
293 | ||
294 | return out - dst; | |
295 | } | |
296 | EXPORT_SYMBOL(string_unescape); | |
c8250381 | 297 | |
3aeddc7d | 298 | static bool escape_passthrough(unsigned char c, char **dst, char *end) |
c8250381 AS |
299 | { |
300 | char *out = *dst; | |
301 | ||
3aeddc7d RV |
302 | if (out < end) |
303 | *out = c; | |
304 | *dst = out + 1; | |
305 | return true; | |
c8250381 AS |
306 | } |
307 | ||
3aeddc7d | 308 | static bool escape_space(unsigned char c, char **dst, char *end) |
c8250381 AS |
309 | { |
310 | char *out = *dst; | |
311 | unsigned char to; | |
312 | ||
c8250381 AS |
313 | switch (c) { |
314 | case '\n': | |
315 | to = 'n'; | |
316 | break; | |
317 | case '\r': | |
318 | to = 'r'; | |
319 | break; | |
320 | case '\t': | |
321 | to = 't'; | |
322 | break; | |
323 | case '\v': | |
324 | to = 'v'; | |
325 | break; | |
326 | case '\f': | |
327 | to = 'f'; | |
328 | break; | |
329 | default: | |
3aeddc7d | 330 | return false; |
c8250381 AS |
331 | } |
332 | ||
3aeddc7d RV |
333 | if (out < end) |
334 | *out = '\\'; | |
335 | ++out; | |
336 | if (out < end) | |
337 | *out = to; | |
338 | ++out; | |
c8250381 | 339 | |
3aeddc7d RV |
340 | *dst = out; |
341 | return true; | |
c8250381 AS |
342 | } |
343 | ||
3aeddc7d | 344 | static bool escape_special(unsigned char c, char **dst, char *end) |
c8250381 AS |
345 | { |
346 | char *out = *dst; | |
347 | unsigned char to; | |
348 | ||
c8250381 AS |
349 | switch (c) { |
350 | case '\\': | |
351 | to = '\\'; | |
352 | break; | |
353 | case '\a': | |
354 | to = 'a'; | |
355 | break; | |
356 | case '\e': | |
357 | to = 'e'; | |
358 | break; | |
359 | default: | |
3aeddc7d | 360 | return false; |
c8250381 AS |
361 | } |
362 | ||
3aeddc7d RV |
363 | if (out < end) |
364 | *out = '\\'; | |
365 | ++out; | |
366 | if (out < end) | |
367 | *out = to; | |
368 | ++out; | |
c8250381 | 369 | |
3aeddc7d RV |
370 | *dst = out; |
371 | return true; | |
c8250381 AS |
372 | } |
373 | ||
3aeddc7d | 374 | static bool escape_null(unsigned char c, char **dst, char *end) |
c8250381 AS |
375 | { |
376 | char *out = *dst; | |
377 | ||
c8250381 | 378 | if (c) |
3aeddc7d | 379 | return false; |
c8250381 | 380 | |
3aeddc7d RV |
381 | if (out < end) |
382 | *out = '\\'; | |
383 | ++out; | |
384 | if (out < end) | |
385 | *out = '0'; | |
386 | ++out; | |
c8250381 | 387 | |
3aeddc7d RV |
388 | *dst = out; |
389 | return true; | |
c8250381 AS |
390 | } |
391 | ||
3aeddc7d | 392 | static bool escape_octal(unsigned char c, char **dst, char *end) |
c8250381 AS |
393 | { |
394 | char *out = *dst; | |
395 | ||
3aeddc7d RV |
396 | if (out < end) |
397 | *out = '\\'; | |
398 | ++out; | |
399 | if (out < end) | |
400 | *out = ((c >> 6) & 0x07) + '0'; | |
401 | ++out; | |
402 | if (out < end) | |
403 | *out = ((c >> 3) & 0x07) + '0'; | |
404 | ++out; | |
405 | if (out < end) | |
406 | *out = ((c >> 0) & 0x07) + '0'; | |
407 | ++out; | |
c8250381 AS |
408 | |
409 | *dst = out; | |
3aeddc7d | 410 | return true; |
c8250381 AS |
411 | } |
412 | ||
3aeddc7d | 413 | static bool escape_hex(unsigned char c, char **dst, char *end) |
c8250381 AS |
414 | { |
415 | char *out = *dst; | |
416 | ||
3aeddc7d RV |
417 | if (out < end) |
418 | *out = '\\'; | |
419 | ++out; | |
420 | if (out < end) | |
421 | *out = 'x'; | |
422 | ++out; | |
423 | if (out < end) | |
424 | *out = hex_asc_hi(c); | |
425 | ++out; | |
426 | if (out < end) | |
427 | *out = hex_asc_lo(c); | |
428 | ++out; | |
c8250381 AS |
429 | |
430 | *dst = out; | |
3aeddc7d | 431 | return true; |
c8250381 AS |
432 | } |
433 | ||
434 | /** | |
435 | * string_escape_mem - quote characters in the given memory buffer | |
436 | * @src: source buffer (unescaped) | |
437 | * @isz: source buffer size | |
438 | * @dst: destination buffer (escaped) | |
439 | * @osz: destination buffer size | |
440 | * @flags: combination of the flags (bitwise OR): | |
d89a3f73 | 441 | * %ESCAPE_SPACE: (special white space, not space itself) |
c8250381 AS |
442 | * '\f' - form feed |
443 | * '\n' - new line | |
444 | * '\r' - carriage return | |
445 | * '\t' - horizontal tab | |
446 | * '\v' - vertical tab | |
447 | * %ESCAPE_SPECIAL: | |
448 | * '\\' - backslash | |
449 | * '\a' - alert (BEL) | |
450 | * '\e' - escape | |
451 | * %ESCAPE_NULL: | |
452 | * '\0' - null | |
453 | * %ESCAPE_OCTAL: | |
454 | * '\NNN' - byte with octal value NNN (3 digits) | |
455 | * %ESCAPE_ANY: | |
456 | * all previous together | |
457 | * %ESCAPE_NP: | |
458 | * escape only non-printable characters (checked by isprint) | |
459 | * %ESCAPE_ANY_NP: | |
460 | * all previous together | |
461 | * %ESCAPE_HEX: | |
462 | * '\xHH' - byte with hexadecimal value HH (2 digits) | |
b40bdb7f KC |
463 | * @only: NULL-terminated string containing characters used to limit |
464 | * the selected escape class. If characters are included in @only | |
d89a3f73 KC |
465 | * that would not normally be escaped by the classes selected |
466 | * in @flags, they will be copied to @dst unescaped. | |
c8250381 AS |
467 | * |
468 | * Description: | |
469 | * The process of escaping byte buffer includes several parts. They are applied | |
470 | * in the following sequence. | |
471 | * 1. The character is matched to the printable class, if asked, and in | |
472 | * case of match it passes through to the output. | |
b40bdb7f | 473 | * 2. The character is not matched to the one from @only string and thus |
d89a3f73 | 474 | * must go as-is to the output. |
c8250381 AS |
475 | * 3. The character is checked if it falls into the class given by @flags. |
476 | * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any | |
477 | * character. Note that they actually can't go together, otherwise | |
478 | * %ESCAPE_HEX will be ignored. | |
479 | * | |
480 | * Caller must provide valid source and destination pointers. Be aware that | |
481 | * destination buffer will not be NULL-terminated, thus caller have to append | |
482 | * it if needs. | |
483 | * | |
484 | * Return: | |
41416f23 RV |
485 | * The total size of the escaped output that would be generated for |
486 | * the given input and flags. To check whether the output was | |
487 | * truncated, compare the return value to osz. There is room left in | |
488 | * dst for a '\0' terminator if and only if ret < osz. | |
c8250381 | 489 | */ |
41416f23 | 490 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
b40bdb7f | 491 | unsigned int flags, const char *only) |
c8250381 | 492 | { |
41416f23 | 493 | char *p = dst; |
3aeddc7d | 494 | char *end = p + osz; |
b40bdb7f | 495 | bool is_dict = only && *only; |
c8250381 AS |
496 | |
497 | while (isz--) { | |
498 | unsigned char c = *src++; | |
499 | ||
500 | /* | |
501 | * Apply rules in the following sequence: | |
502 | * - the character is printable, when @flags has | |
503 | * %ESCAPE_NP bit set | |
b40bdb7f | 504 | * - the @only string is supplied and does not contain a |
c8250381 AS |
505 | * character under question |
506 | * - the character doesn't fall into a class of symbols | |
507 | * defined by given @flags | |
508 | * In these cases we just pass through a character to the | |
509 | * output buffer. | |
510 | */ | |
511 | if ((flags & ESCAPE_NP && isprint(c)) || | |
b40bdb7f | 512 | (is_dict && !strchr(only, c))) { |
c8250381 AS |
513 | /* do nothing */ |
514 | } else { | |
3aeddc7d RV |
515 | if (flags & ESCAPE_SPACE && escape_space(c, &p, end)) |
516 | continue; | |
517 | ||
518 | if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end)) | |
519 | continue; | |
520 | ||
521 | if (flags & ESCAPE_NULL && escape_null(c, &p, end)) | |
522 | continue; | |
c8250381 AS |
523 | |
524 | /* ESCAPE_OCTAL and ESCAPE_HEX always go last */ | |
3aeddc7d | 525 | if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end)) |
c8250381 | 526 | continue; |
3aeddc7d RV |
527 | |
528 | if (flags & ESCAPE_HEX && escape_hex(c, &p, end)) | |
c8250381 | 529 | continue; |
c8250381 AS |
530 | } |
531 | ||
3aeddc7d | 532 | escape_passthrough(c, &p, end); |
c8250381 AS |
533 | } |
534 | ||
41416f23 | 535 | return p - dst; |
c8250381 AS |
536 | } |
537 | EXPORT_SYMBOL(string_escape_mem); | |
b53f27e4 KC |
538 | |
539 | /* | |
540 | * Return an allocated string that has been escaped of special characters | |
541 | * and double quotes, making it safe to log in quotes. | |
542 | */ | |
543 | char *kstrdup_quotable(const char *src, gfp_t gfp) | |
544 | { | |
545 | size_t slen, dlen; | |
546 | char *dst; | |
547 | const int flags = ESCAPE_HEX; | |
548 | const char esc[] = "\f\n\r\t\v\a\e\\\""; | |
549 | ||
550 | if (!src) | |
551 | return NULL; | |
552 | slen = strlen(src); | |
553 | ||
554 | dlen = string_escape_mem(src, slen, NULL, 0, flags, esc); | |
555 | dst = kmalloc(dlen + 1, gfp); | |
556 | if (!dst) | |
557 | return NULL; | |
558 | ||
559 | WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen); | |
560 | dst[dlen] = '\0'; | |
561 | ||
562 | return dst; | |
563 | } | |
564 | EXPORT_SYMBOL_GPL(kstrdup_quotable); |