]>
git.proxmox.com Git - mirror_ubuntu-artful-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
7 #include <linux/kernel.h>
8 #include <linux/math64.h>
9 #include <linux/export.h>
10 #include <linux/ctype.h>
11 #include <linux/string_helpers.h>
14 * string_get_size - get the size in the specified units
15 * @size: The size to be converted
16 * @units: units to use (powers of 1000 or 1024)
17 * @buf: buffer to format to
18 * @len: length of buffer
20 * This function returns a string formatted to 3 significant figures
21 * giving the size in the required units. Returns 0 on success or
22 * error on failure. @buf is always zero terminated.
25 int string_get_size(u64 size
, const enum string_size_units units
,
28 static const char *const units_10
[] = {
29 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL
31 static const char *const units_2
[] = {
32 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
35 static const char *const *const units_str
[] = {
36 [STRING_UNITS_10
] = units_10
,
37 [STRING_UNITS_2
] = units_2
,
39 static const unsigned int divisor
[] = {
40 [STRING_UNITS_10
] = 1000,
41 [STRING_UNITS_2
] = 1024,
44 u64 remainder
= 0, sf_cap
;
49 if (size
>= divisor
[units
]) {
50 while (size
>= divisor
[units
] && units_str
[units
][i
]) {
51 remainder
= do_div(size
, divisor
[units
]);
56 for (j
= 0; sf_cap
*10 < 1000; j
++)
61 do_div(remainder
, divisor
[units
]);
62 snprintf(tmp
, sizeof(tmp
), ".%03lld",
63 (unsigned long long)remainder
);
68 snprintf(buf
, len
, "%lld%s %s", (unsigned long long)size
,
69 tmp
, units_str
[units
][i
]);
73 EXPORT_SYMBOL(string_get_size
);
75 static bool unescape_space(char **src
, char **dst
)
77 char *p
= *dst
, *q
= *src
;
103 static bool unescape_octal(char **src
, char **dst
)
105 char *p
= *dst
, *q
= *src
;
108 if (isodigit(*q
) == 0)
112 while (num
< 32 && isodigit(*q
) && (q
- *src
< 3)) {
122 static bool unescape_hex(char **src
, char **dst
)
124 char *p
= *dst
, *q
= *src
;
131 num
= digit
= hex_to_bin(*q
++);
135 digit
= hex_to_bin(*q
);
138 num
= (num
<< 4) | digit
;
146 static bool unescape_special(char **src
, char **dst
)
148 char *p
= *dst
, *q
= *src
;
172 * string_unescape - unquote characters in the given string
173 * @src: source buffer (escaped)
174 * @dst: destination buffer (unescaped)
175 * @size: size of the destination buffer (0 to unlimit)
176 * @flags: combination of the flags (bitwise OR):
180 * '\r' - carriage return
181 * '\t' - horizontal tab
182 * '\v' - vertical tab
184 * '\NNN' - byte with octal value NNN (1 to 3 digits)
186 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
188 * '\"' - double quote
193 * all previous together
196 * The function unquotes characters in the given string.
198 * Because the size of the output will be the same as or less than the size of
199 * the input, the transformation may be performed in place.
201 * Caller must provide valid source and destination pointers. Be aware that
202 * destination buffer will always be NULL-terminated. Source string must be
203 * NULL-terminated as well.
206 * The amount of the characters processed to the destination buffer excluding
207 * trailing '\0' is returned.
209 int string_unescape(char *src
, char *dst
, size_t size
, unsigned int flags
)
213 while (*src
&& --size
) {
214 if (src
[0] == '\\' && src
[1] != '\0' && size
> 1) {
218 if (flags
& UNESCAPE_SPACE
&&
219 unescape_space(&src
, &out
))
222 if (flags
& UNESCAPE_OCTAL
&&
223 unescape_octal(&src
, &out
))
226 if (flags
& UNESCAPE_HEX
&&
227 unescape_hex(&src
, &out
))
230 if (flags
& UNESCAPE_SPECIAL
&&
231 unescape_special(&src
, &out
))
242 EXPORT_SYMBOL(string_unescape
);