2 * Helpers for formatting and printing strings
4 * Copyright 31 August 2008 James Bottomley
5 * Copyright (C) 2013, Intel Corporation
8 #include <linux/kernel.h>
9 #include <linux/math64.h>
10 #include <linux/export.h>
11 #include <linux/ctype.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/string_helpers.h>
17 * string_get_size - get the size in the specified units
18 * @size: The size to be converted in blocks
19 * @blk_size: Size of the block (use 1 for size in bytes)
20 * @units: units to use (powers of 1000 or 1024)
21 * @buf: buffer to format to
22 * @len: length of buffer
24 * This function returns a string formatted to 3 significant figures
25 * giving the size in the required units. @buf should have room for
26 * at least 9 bytes and will always be zero terminated.
29 void string_get_size(u64 size
, u64 blk_size
, const enum string_size_units units
,
32 static const char *const units_10
[] = {
33 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
35 static const char *const units_2
[] = {
36 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
38 static const char *const *const units_str
[] = {
39 [STRING_UNITS_10
] = units_10
,
40 [STRING_UNITS_2
] = units_2
,
42 static const unsigned int divisor
[] = {
43 [STRING_UNITS_10
] = 1000,
44 [STRING_UNITS_2
] = 1024,
46 static const unsigned int rounding
[] = { 500, 50, 5 };
48 u32 remainder
= 0, sf_cap
;
59 /* This is Napier's algorithm. Reduce the original block size to
61 * coefficient * divisor[units]^i
63 * we do the reduction so both coefficients are just under 32 bits so
64 * that multiplying them together won't overflow 64 bits and we keep
65 * as much precision as possible in the numbers.
67 * Note: it's safe to throw away the remainders here because all the
68 * precision is in the coefficients.
70 while (blk_size
>> 32) {
71 do_div(blk_size
, divisor
[units
]);
76 do_div(size
, divisor
[units
]);
80 /* now perform the actual multiplication keeping i as the sum of the
84 /* and logarithmically reduce it until it's just under the divisor */
85 while (size
>= divisor
[units
]) {
86 remainder
= do_div(size
, divisor
[units
]);
90 /* work out in j how many digits of precision we need from the
93 for (j
= 0; sf_cap
*10 < 1000; j
++)
96 if (units
== STRING_UNITS_2
) {
97 /* express the remainder as a decimal. It's currently the
98 * numerator of a fraction whose denominator is
99 * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
104 /* add a 5 to the digit below what will be printed to ensure
105 * an arithmetical round up and carry it through to size */
106 remainder
+= rounding
[j
];
107 if (remainder
>= 1000) {
113 snprintf(tmp
, sizeof(tmp
), ".%03u", remainder
);
118 if (i
>= ARRAY_SIZE(units_2
))
121 unit
= units_str
[units
][i
];
123 snprintf(buf
, len
, "%u%s %s", (u32
)size
,
126 EXPORT_SYMBOL(string_get_size
);
128 static bool unescape_space(char **src
, char **dst
)
130 char *p
= *dst
, *q
= *src
;
156 static bool unescape_octal(char **src
, char **dst
)
158 char *p
= *dst
, *q
= *src
;
161 if (isodigit(*q
) == 0)
165 while (num
< 32 && isodigit(*q
) && (q
- *src
< 3)) {
175 static bool unescape_hex(char **src
, char **dst
)
177 char *p
= *dst
, *q
= *src
;
184 num
= digit
= hex_to_bin(*q
++);
188 digit
= hex_to_bin(*q
);
191 num
= (num
<< 4) | digit
;
199 static bool unescape_special(char **src
, char **dst
)
201 char *p
= *dst
, *q
= *src
;
225 * string_unescape - unquote characters in the given string
226 * @src: source buffer (escaped)
227 * @dst: destination buffer (unescaped)
228 * @size: size of the destination buffer (0 to unlimit)
229 * @flags: combination of the flags (bitwise OR):
233 * '\r' - carriage return
234 * '\t' - horizontal tab
235 * '\v' - vertical tab
237 * '\NNN' - byte with octal value NNN (1 to 3 digits)
239 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
241 * '\"' - double quote
246 * all previous together
249 * The function unquotes characters in the given string.
251 * Because the size of the output will be the same as or less than the size of
252 * the input, the transformation may be performed in place.
254 * Caller must provide valid source and destination pointers. Be aware that
255 * destination buffer will always be NULL-terminated. Source string must be
256 * NULL-terminated as well.
259 * The amount of the characters processed to the destination buffer excluding
260 * trailing '\0' is returned.
262 int string_unescape(char *src
, char *dst
, size_t size
, unsigned int flags
)
266 while (*src
&& --size
) {
267 if (src
[0] == '\\' && src
[1] != '\0' && size
> 1) {
271 if (flags
& UNESCAPE_SPACE
&&
272 unescape_space(&src
, &out
))
275 if (flags
& UNESCAPE_OCTAL
&&
276 unescape_octal(&src
, &out
))
279 if (flags
& UNESCAPE_HEX
&&
280 unescape_hex(&src
, &out
))
283 if (flags
& UNESCAPE_SPECIAL
&&
284 unescape_special(&src
, &out
))
295 EXPORT_SYMBOL(string_unescape
);
297 static bool escape_passthrough(unsigned char c
, char **dst
, char *end
)
307 static bool escape_space(unsigned char c
, char **dst
, char *end
)
343 static bool escape_special(unsigned char c
, char **dst
, char *end
)
373 static bool escape_null(unsigned char c
, char **dst
, char *end
)
391 static bool escape_octal(unsigned char c
, char **dst
, char *end
)
399 *out
= ((c
>> 6) & 0x07) + '0';
402 *out
= ((c
>> 3) & 0x07) + '0';
405 *out
= ((c
>> 0) & 0x07) + '0';
412 static bool escape_hex(unsigned char c
, char **dst
, char *end
)
423 *out
= hex_asc_hi(c
);
426 *out
= hex_asc_lo(c
);
434 * string_escape_mem - quote characters in the given memory buffer
435 * @src: source buffer (unescaped)
436 * @isz: source buffer size
437 * @dst: destination buffer (escaped)
438 * @osz: destination buffer size
439 * @flags: combination of the flags (bitwise OR):
440 * %ESCAPE_SPACE: (special white space, not space itself)
443 * '\r' - carriage return
444 * '\t' - horizontal tab
445 * '\v' - vertical tab
453 * '\NNN' - byte with octal value NNN (3 digits)
455 * all previous together
457 * escape only non-printable characters (checked by isprint)
459 * all previous together
461 * '\xHH' - byte with hexadecimal value HH (2 digits)
462 * @only: NULL-terminated string containing characters used to limit
463 * the selected escape class. If characters are included in @only
464 * that would not normally be escaped by the classes selected
465 * in @flags, they will be copied to @dst unescaped.
468 * The process of escaping byte buffer includes several parts. They are applied
469 * in the following sequence.
470 * 1. The character is matched to the printable class, if asked, and in
471 * case of match it passes through to the output.
472 * 2. The character is not matched to the one from @only string and thus
473 * must go as-is to the output.
474 * 3. The character is checked if it falls into the class given by @flags.
475 * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
476 * character. Note that they actually can't go together, otherwise
477 * %ESCAPE_HEX will be ignored.
479 * Caller must provide valid source and destination pointers. Be aware that
480 * destination buffer will not be NULL-terminated, thus caller have to append
484 * The total size of the escaped output that would be generated for
485 * the given input and flags. To check whether the output was
486 * truncated, compare the return value to osz. There is room left in
487 * dst for a '\0' terminator if and only if ret < osz.
489 int string_escape_mem(const char *src
, size_t isz
, char *dst
, size_t osz
,
490 unsigned int flags
, const char *only
)
494 bool is_dict
= only
&& *only
;
497 unsigned char c
= *src
++;
500 * Apply rules in the following sequence:
501 * - the character is printable, when @flags has
503 * - the @only string is supplied and does not contain a
504 * character under question
505 * - the character doesn't fall into a class of symbols
506 * defined by given @flags
507 * In these cases we just pass through a character to the
510 if ((flags
& ESCAPE_NP
&& isprint(c
)) ||
511 (is_dict
&& !strchr(only
, c
))) {
514 if (flags
& ESCAPE_SPACE
&& escape_space(c
, &p
, end
))
517 if (flags
& ESCAPE_SPECIAL
&& escape_special(c
, &p
, end
))
520 if (flags
& ESCAPE_NULL
&& escape_null(c
, &p
, end
))
523 /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
524 if (flags
& ESCAPE_OCTAL
&& escape_octal(c
, &p
, end
))
527 if (flags
& ESCAPE_HEX
&& escape_hex(c
, &p
, end
))
531 escape_passthrough(c
, &p
, end
);
536 EXPORT_SYMBOL(string_escape_mem
);