]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/string_helpers.c
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 * size must be strictly greater than exp here to ensure that remainder
64 * is greater than divisor[units] coming out of the if below.
67 remainder
= do_div(size
, divisor
[units
]);
68 remainder
*= blk_size
;
75 size
+= remainder
/ divisor
[units
];
76 remainder
%= divisor
[units
];
78 while (size
>= divisor
[units
]) {
79 remainder
= do_div(size
, divisor
[units
]);
84 for (j
= 0; sf_cap
*10 < 1000; j
++)
89 remainder
/= divisor
[units
];
90 snprintf(tmp
, sizeof(tmp
), ".%03u", remainder
);
95 if (i
>= ARRAY_SIZE(units_2
))
98 unit
= units_str
[units
][i
];
100 snprintf(buf
, len
, "%u%s %s", (u32
)size
,
103 EXPORT_SYMBOL(string_get_size
);
105 static bool unescape_space(char **src
, char **dst
)
107 char *p
= *dst
, *q
= *src
;
133 static bool unescape_octal(char **src
, char **dst
)
135 char *p
= *dst
, *q
= *src
;
138 if (isodigit(*q
) == 0)
142 while (num
< 32 && isodigit(*q
) && (q
- *src
< 3)) {
152 static bool unescape_hex(char **src
, char **dst
)
154 char *p
= *dst
, *q
= *src
;
161 num
= digit
= hex_to_bin(*q
++);
165 digit
= hex_to_bin(*q
);
168 num
= (num
<< 4) | digit
;
176 static bool unescape_special(char **src
, char **dst
)
178 char *p
= *dst
, *q
= *src
;
202 * string_unescape - unquote characters in the given string
203 * @src: source buffer (escaped)
204 * @dst: destination buffer (unescaped)
205 * @size: size of the destination buffer (0 to unlimit)
206 * @flags: combination of the flags (bitwise OR):
210 * '\r' - carriage return
211 * '\t' - horizontal tab
212 * '\v' - vertical tab
214 * '\NNN' - byte with octal value NNN (1 to 3 digits)
216 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
218 * '\"' - double quote
223 * all previous together
226 * The function unquotes characters in the given string.
228 * Because the size of the output will be the same as or less than the size of
229 * the input, the transformation may be performed in place.
231 * Caller must provide valid source and destination pointers. Be aware that
232 * destination buffer will always be NULL-terminated. Source string must be
233 * NULL-terminated as well.
236 * The amount of the characters processed to the destination buffer excluding
237 * trailing '\0' is returned.
239 int string_unescape(char *src
, char *dst
, size_t size
, unsigned int flags
)
243 while (*src
&& --size
) {
244 if (src
[0] == '\\' && src
[1] != '\0' && size
> 1) {
248 if (flags
& UNESCAPE_SPACE
&&
249 unescape_space(&src
, &out
))
252 if (flags
& UNESCAPE_OCTAL
&&
253 unescape_octal(&src
, &out
))
256 if (flags
& UNESCAPE_HEX
&&
257 unescape_hex(&src
, &out
))
260 if (flags
& UNESCAPE_SPECIAL
&&
261 unescape_special(&src
, &out
))
272 EXPORT_SYMBOL(string_unescape
);
274 static bool escape_passthrough(unsigned char c
, char **dst
, char *end
)
284 static bool escape_space(unsigned char c
, char **dst
, char *end
)
320 static bool escape_special(unsigned char c
, char **dst
, char *end
)
350 static bool escape_null(unsigned char c
, char **dst
, char *end
)
368 static bool escape_octal(unsigned char c
, char **dst
, char *end
)
376 *out
= ((c
>> 6) & 0x07) + '0';
379 *out
= ((c
>> 3) & 0x07) + '0';
382 *out
= ((c
>> 0) & 0x07) + '0';
389 static bool escape_hex(unsigned char c
, char **dst
, char *end
)
400 *out
= hex_asc_hi(c
);
403 *out
= hex_asc_lo(c
);
411 * string_escape_mem - quote characters in the given memory buffer
412 * @src: source buffer (unescaped)
413 * @isz: source buffer size
414 * @dst: destination buffer (escaped)
415 * @osz: destination buffer size
416 * @flags: combination of the flags (bitwise OR):
417 * %ESCAPE_SPACE: (special white space, not space itself)
420 * '\r' - carriage return
421 * '\t' - horizontal tab
422 * '\v' - vertical tab
430 * '\NNN' - byte with octal value NNN (3 digits)
432 * all previous together
434 * escape only non-printable characters (checked by isprint)
436 * all previous together
438 * '\xHH' - byte with hexadecimal value HH (2 digits)
439 * @only: NULL-terminated string containing characters used to limit
440 * the selected escape class. If characters are included in @only
441 * that would not normally be escaped by the classes selected
442 * in @flags, they will be copied to @dst unescaped.
445 * The process of escaping byte buffer includes several parts. They are applied
446 * in the following sequence.
447 * 1. The character is matched to the printable class, if asked, and in
448 * case of match it passes through to the output.
449 * 2. The character is not matched to the one from @only string and thus
450 * must go as-is to the output.
451 * 3. The character is checked if it falls into the class given by @flags.
452 * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
453 * character. Note that they actually can't go together, otherwise
454 * %ESCAPE_HEX will be ignored.
456 * Caller must provide valid source and destination pointers. Be aware that
457 * destination buffer will not be NULL-terminated, thus caller have to append
461 * The total size of the escaped output that would be generated for
462 * the given input and flags. To check whether the output was
463 * truncated, compare the return value to osz. There is room left in
464 * dst for a '\0' terminator if and only if ret < osz.
466 int string_escape_mem(const char *src
, size_t isz
, char *dst
, size_t osz
,
467 unsigned int flags
, const char *only
)
471 bool is_dict
= only
&& *only
;
474 unsigned char c
= *src
++;
477 * Apply rules in the following sequence:
478 * - the character is printable, when @flags has
480 * - the @only string is supplied and does not contain a
481 * character under question
482 * - the character doesn't fall into a class of symbols
483 * defined by given @flags
484 * In these cases we just pass through a character to the
487 if ((flags
& ESCAPE_NP
&& isprint(c
)) ||
488 (is_dict
&& !strchr(only
, c
))) {
491 if (flags
& ESCAPE_SPACE
&& escape_space(c
, &p
, end
))
494 if (flags
& ESCAPE_SPECIAL
&& escape_special(c
, &p
, end
))
497 if (flags
& ESCAPE_NULL
&& escape_null(c
, &p
, end
))
500 /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
501 if (flags
& ESCAPE_OCTAL
&& escape_octal(c
, &p
, end
))
504 if (flags
& ESCAPE_HEX
&& escape_hex(c
, &p
, end
))
508 escape_passthrough(c
, &p
, end
);
513 EXPORT_SYMBOL(string_escape_mem
);