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,
47 u32 remainder
= 0, sf_cap
, exp
;
56 while (blk_size
>= divisor
[units
]) {
57 remainder
= do_div(blk_size
, divisor
[units
]);
61 exp
= divisor
[units
] / (u32
)blk_size
;
63 remainder
= do_div(size
, divisor
[units
]);
64 remainder
*= blk_size
;
71 size
+= remainder
/ divisor
[units
];
72 remainder
%= divisor
[units
];
74 while (size
>= divisor
[units
]) {
75 remainder
= do_div(size
, divisor
[units
]);
80 for (j
= 0; sf_cap
*10 < 1000; j
++)
85 remainder
/= divisor
[units
];
86 snprintf(tmp
, sizeof(tmp
), ".%03u", remainder
);
91 if (i
>= ARRAY_SIZE(units_2
))
94 unit
= units_str
[units
][i
];
96 snprintf(buf
, len
, "%u%s %s", (u32
)size
,
99 EXPORT_SYMBOL(string_get_size
);
101 static bool unescape_space(char **src
, char **dst
)
103 char *p
= *dst
, *q
= *src
;
129 static bool unescape_octal(char **src
, char **dst
)
131 char *p
= *dst
, *q
= *src
;
134 if (isodigit(*q
) == 0)
138 while (num
< 32 && isodigit(*q
) && (q
- *src
< 3)) {
148 static bool unescape_hex(char **src
, char **dst
)
150 char *p
= *dst
, *q
= *src
;
157 num
= digit
= hex_to_bin(*q
++);
161 digit
= hex_to_bin(*q
);
164 num
= (num
<< 4) | digit
;
172 static bool unescape_special(char **src
, char **dst
)
174 char *p
= *dst
, *q
= *src
;
198 * string_unescape - unquote characters in the given string
199 * @src: source buffer (escaped)
200 * @dst: destination buffer (unescaped)
201 * @size: size of the destination buffer (0 to unlimit)
202 * @flags: combination of the flags (bitwise OR):
206 * '\r' - carriage return
207 * '\t' - horizontal tab
208 * '\v' - vertical tab
210 * '\NNN' - byte with octal value NNN (1 to 3 digits)
212 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
214 * '\"' - double quote
219 * all previous together
222 * The function unquotes characters in the given string.
224 * Because the size of the output will be the same as or less than the size of
225 * the input, the transformation may be performed in place.
227 * Caller must provide valid source and destination pointers. Be aware that
228 * destination buffer will always be NULL-terminated. Source string must be
229 * NULL-terminated as well.
232 * The amount of the characters processed to the destination buffer excluding
233 * trailing '\0' is returned.
235 int string_unescape(char *src
, char *dst
, size_t size
, unsigned int flags
)
239 while (*src
&& --size
) {
240 if (src
[0] == '\\' && src
[1] != '\0' && size
> 1) {
244 if (flags
& UNESCAPE_SPACE
&&
245 unescape_space(&src
, &out
))
248 if (flags
& UNESCAPE_OCTAL
&&
249 unescape_octal(&src
, &out
))
252 if (flags
& UNESCAPE_HEX
&&
253 unescape_hex(&src
, &out
))
256 if (flags
& UNESCAPE_SPECIAL
&&
257 unescape_special(&src
, &out
))
268 EXPORT_SYMBOL(string_unescape
);
270 static bool escape_passthrough(unsigned char c
, char **dst
, char *end
)
280 static bool escape_space(unsigned char c
, char **dst
, char *end
)
316 static bool escape_special(unsigned char c
, char **dst
, char *end
)
346 static bool escape_null(unsigned char c
, char **dst
, char *end
)
364 static bool escape_octal(unsigned char c
, char **dst
, char *end
)
372 *out
= ((c
>> 6) & 0x07) + '0';
375 *out
= ((c
>> 3) & 0x07) + '0';
378 *out
= ((c
>> 0) & 0x07) + '0';
385 static bool escape_hex(unsigned char c
, char **dst
, char *end
)
396 *out
= hex_asc_hi(c
);
399 *out
= hex_asc_lo(c
);
407 * string_escape_mem - quote characters in the given memory buffer
408 * @src: source buffer (unescaped)
409 * @isz: source buffer size
410 * @dst: destination buffer (escaped)
411 * @osz: destination buffer size
412 * @flags: combination of the flags (bitwise OR):
416 * '\r' - carriage return
417 * '\t' - horizontal tab
418 * '\v' - vertical tab
426 * '\NNN' - byte with octal value NNN (3 digits)
428 * all previous together
430 * escape only non-printable characters (checked by isprint)
432 * all previous together
434 * '\xHH' - byte with hexadecimal value HH (2 digits)
435 * @esc: NULL-terminated string of characters any of which, if found in
436 * the source, has to be escaped
439 * The process of escaping byte buffer includes several parts. They are applied
440 * in the following sequence.
441 * 1. The character is matched to the printable class, if asked, and in
442 * case of match it passes through to the output.
443 * 2. The character is not matched to the one from @esc string and thus
444 * must go as is to the output.
445 * 3. The character is checked if it falls into the class given by @flags.
446 * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
447 * character. Note that they actually can't go together, otherwise
448 * %ESCAPE_HEX will be ignored.
450 * Caller must provide valid source and destination pointers. Be aware that
451 * destination buffer will not be NULL-terminated, thus caller have to append
455 * The total size of the escaped output that would be generated for
456 * the given input and flags. To check whether the output was
457 * truncated, compare the return value to osz. There is room left in
458 * dst for a '\0' terminator if and only if ret < osz.
460 int string_escape_mem(const char *src
, size_t isz
, char *dst
, size_t osz
,
461 unsigned int flags
, const char *esc
)
465 bool is_dict
= esc
&& *esc
;
468 unsigned char c
= *src
++;
471 * Apply rules in the following sequence:
472 * - the character is printable, when @flags has
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
481 if ((flags
& ESCAPE_NP
&& isprint(c
)) ||
482 (is_dict
&& !strchr(esc
, c
))) {
485 if (flags
& ESCAPE_SPACE
&& escape_space(c
, &p
, end
))
488 if (flags
& ESCAPE_SPECIAL
&& escape_special(c
, &p
, end
))
491 if (flags
& ESCAPE_NULL
&& escape_null(c
, &p
, end
))
494 /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
495 if (flags
& ESCAPE_OCTAL
&& escape_octal(c
, &p
, end
))
498 if (flags
& ESCAPE_HEX
&& escape_hex(c
, &p
, end
))
502 escape_passthrough(c
, &p
, end
);
507 EXPORT_SYMBOL(string_escape_mem
);