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/errno.h>
12 #include <linux/string.h>
13 #include <linux/string_helpers.h>
16 * string_get_size - get the size in the specified units
17 * @size: The size to be converted
18 * @units: units to use (powers of 1000 or 1024)
19 * @buf: buffer to format to
20 * @len: length of buffer
22 * This function returns a string formatted to 3 significant figures
23 * giving the size in the required units. Returns 0 on success or
24 * error on failure. @buf is always zero terminated.
27 int string_get_size(u64 size, const enum string_size_units units,
30 static const char *const units_10[] = {
31 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL
33 static const char *const units_2[] = {
34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
37 static const char *const *const units_str[] = {
38 [STRING_UNITS_10] = units_10,
39 [STRING_UNITS_2] = units_2,
41 static const unsigned int divisor[] = {
42 [STRING_UNITS_10] = 1000,
43 [STRING_UNITS_2] = 1024,
46 u64 remainder = 0, sf_cap;
51 if (size >= divisor[units]) {
52 while (size >= divisor[units] && units_str[units][i]) {
53 remainder = do_div(size, divisor[units]);
58 for (j = 0; sf_cap*10 < 1000; j++)
63 do_div(remainder, divisor[units]);
64 snprintf(tmp, sizeof(tmp), ".%03lld",
65 (unsigned long long)remainder);
70 snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
71 tmp, units_str[units][i]);
75 EXPORT_SYMBOL(string_get_size);
77 static bool unescape_space(char **src, char **dst)
79 char *p = *dst, *q = *src;
105 static bool unescape_octal(char **src, char **dst)
107 char *p = *dst, *q = *src;
110 if (isodigit(*q) == 0)
114 while (num < 32 && isodigit(*q) && (q - *src < 3)) {
124 static bool unescape_hex(char **src, char **dst)
126 char *p = *dst, *q = *src;
133 num = digit = hex_to_bin(*q++);
137 digit = hex_to_bin(*q);
140 num = (num << 4) | digit;
148 static bool unescape_special(char **src, char **dst)
150 char *p = *dst, *q = *src;
174 * string_unescape - unquote characters in the given string
175 * @src: source buffer (escaped)
176 * @dst: destination buffer (unescaped)
177 * @size: size of the destination buffer (0 to unlimit)
178 * @flags: combination of the flags (bitwise OR):
182 * '\r' - carriage return
183 * '\t' - horizontal tab
184 * '\v' - vertical tab
186 * '\NNN' - byte with octal value NNN (1 to 3 digits)
188 * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
190 * '\"' - double quote
195 * all previous together
198 * The function unquotes characters in the given string.
200 * Because the size of the output will be the same as or less than the size of
201 * the input, the transformation may be performed in place.
203 * Caller must provide valid source and destination pointers. Be aware that
204 * destination buffer will always be NULL-terminated. Source string must be
205 * NULL-terminated as well.
208 * The amount of the characters processed to the destination buffer excluding
209 * trailing '\0' is returned.
211 int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
215 while (*src && --size) {
216 if (src[0] == '\\' && src[1] != '\0' && size > 1) {
220 if (flags & UNESCAPE_SPACE &&
221 unescape_space(&src, &out))
224 if (flags & UNESCAPE_OCTAL &&
225 unescape_octal(&src, &out))
228 if (flags & UNESCAPE_HEX &&
229 unescape_hex(&src, &out))
232 if (flags & UNESCAPE_SPECIAL &&
233 unescape_special(&src, &out))
244 EXPORT_SYMBOL(string_unescape);
246 static int escape_passthrough(unsigned char c, char **dst, size_t *osz)
261 static int escape_space(unsigned char c, char **dst, size_t *osz)
298 static int escape_special(unsigned char c, char **dst, size_t *osz)
329 static int escape_null(unsigned char c, char **dst, size_t *osz)
348 static int escape_octal(unsigned char c, char **dst, size_t *osz)
356 *out++ = ((c >> 6) & 0x07) + '0';
357 *out++ = ((c >> 3) & 0x07) + '0';
358 *out++ = ((c >> 0) & 0x07) + '0';
366 static int escape_hex(unsigned char c, char **dst, size_t *osz)
375 *out++ = hex_asc_hi(c);
376 *out++ = hex_asc_lo(c);
385 * string_escape_mem - quote characters in the given memory buffer
386 * @src: source buffer (unescaped)
387 * @isz: source buffer size
388 * @dst: destination buffer (escaped)
389 * @osz: destination buffer size
390 * @flags: combination of the flags (bitwise OR):
394 * '\r' - carriage return
395 * '\t' - horizontal tab
396 * '\v' - vertical tab
404 * '\NNN' - byte with octal value NNN (3 digits)
406 * all previous together
408 * escape only non-printable characters (checked by isprint)
410 * all previous together
412 * '\xHH' - byte with hexadecimal value HH (2 digits)
413 * @esc: NULL-terminated string of characters any of which, if found in
414 * the source, has to be escaped
417 * The process of escaping byte buffer includes several parts. They are applied
418 * in the following sequence.
419 * 1. The character is matched to the printable class, if asked, and in
420 * case of match it passes through to the output.
421 * 2. The character is not matched to the one from @esc string and thus
422 * must go as is to the output.
423 * 3. The character is checked if it falls into the class given by @flags.
424 * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
425 * character. Note that they actually can't go together, otherwise
426 * %ESCAPE_HEX will be ignored.
428 * Caller must provide valid source and destination pointers. Be aware that
429 * destination buffer will not be NULL-terminated, thus caller have to append
433 * The amount of the characters processed to the destination buffer, or
434 * %-ENOMEM if the size of buffer is not enough to put an escaped character is
437 * Even in the case of error @dst pointer will be updated to point to the byte
438 * after the last processed character.
440 int string_escape_mem(const char *src, size_t isz, char **dst, size_t osz,
441 unsigned int flags, const char *esc)
443 char *out = *dst, *p = out;
444 bool is_dict = esc && *esc;
448 unsigned char c = *src++;
451 * Apply rules in the following sequence:
452 * - the character is printable, when @flags has
454 * - the @esc string is supplied and does not contain a
455 * character under question
456 * - the character doesn't fall into a class of symbols
457 * defined by given @flags
458 * In these cases we just pass through a character to the
461 if ((flags & ESCAPE_NP && isprint(c)) ||
462 (is_dict && !strchr(esc, c))) {
465 if (flags & ESCAPE_SPACE) {
466 ret = escape_space(c, &p, &osz);
473 if (flags & ESCAPE_SPECIAL) {
474 ret = escape_special(c, &p, &osz);
481 if (flags & ESCAPE_NULL) {
482 ret = escape_null(c, &p, &osz);
489 /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
490 if (flags & ESCAPE_OCTAL) {
491 ret = escape_octal(c, &p, &osz);
496 if (flags & ESCAPE_HEX) {
497 ret = escape_hex(c, &p, &osz);
504 ret = escape_passthrough(c, &p, &osz);
516 EXPORT_SYMBOL(string_escape_mem);