James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Helpers for formatting and printing strings |
| 3 | * |
| 4 | * Copyright 31 August 2008 James Bottomley |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 5 | * Copyright (C) 2013, Intel Corporation |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 6 | */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 7 | #include <linux/bug.h> |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/math64.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 10 | #include <linux/export.h> |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 11 | #include <linux/ctype.h> |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame^] | 13 | #include <linux/slab.h> |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 14 | #include <linux/string.h> |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 15 | #include <linux/string_helpers.h> |
| 16 | |
| 17 | /** |
| 18 | * string_get_size - get the size in the specified units |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 19 | * @size: The size to be converted in blocks |
| 20 | * @blk_size: Size of the block (use 1 for size in bytes) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 21 | * @units: units to use (powers of 1000 or 1024) |
| 22 | * @buf: buffer to format to |
| 23 | * @len: length of buffer |
| 24 | * |
| 25 | * This function returns a string formatted to 3 significant figures |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 26 | * giving the size in the required units. @buf should have room for |
| 27 | * at least 9 bytes and will always be zero terminated. |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 28 | * |
| 29 | */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 30 | void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 31 | char *buf, int len) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 32 | { |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 33 | static const char *const units_10[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 34 | "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 35 | }; |
| 36 | static const char *const units_2[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 37 | "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 38 | }; |
| 39 | static const char *const *const units_str[] = { |
| 40 | [STRING_UNITS_10] = units_10, |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 41 | [STRING_UNITS_2] = units_2, |
| 42 | }; |
Andrew Morton | 68aecfb | 2012-05-29 15:07:32 -0700 | [diff] [blame] | 43 | static const unsigned int divisor[] = { |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 44 | [STRING_UNITS_10] = 1000, |
| 45 | [STRING_UNITS_2] = 1024, |
| 46 | }; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 47 | static const unsigned int rounding[] = { 500, 50, 5 }; |
| 48 | int i = 0, j; |
| 49 | u32 remainder = 0, sf_cap; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 50 | char tmp[8]; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 51 | const char *unit; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 52 | |
| 53 | tmp[0] = '\0'; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 54 | |
| 55 | if (blk_size == 0) |
| 56 | size = 0; |
| 57 | if (size == 0) |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 58 | goto out; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 59 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 60 | /* This is Napier's algorithm. Reduce the original block size to |
| 61 | * |
| 62 | * coefficient * divisor[units]^i |
| 63 | * |
| 64 | * we do the reduction so both coefficients are just under 32 bits so |
| 65 | * that multiplying them together won't overflow 64 bits and we keep |
| 66 | * as much precision as possible in the numbers. |
| 67 | * |
| 68 | * Note: it's safe to throw away the remainders here because all the |
| 69 | * precision is in the coefficients. |
Vitaly Kuznetsov | 62bef58 | 2015-09-17 16:01:51 -0700 | [diff] [blame] | 70 | */ |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 71 | while (blk_size >> 32) { |
| 72 | do_div(blk_size, divisor[units]); |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 73 | i++; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 74 | } |
| 75 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 76 | while (size >> 32) { |
| 77 | do_div(size, divisor[units]); |
| 78 | i++; |
| 79 | } |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 80 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 81 | /* now perform the actual multiplication keeping i as the sum of the |
| 82 | * two logarithms */ |
| 83 | size *= blk_size; |
| 84 | |
| 85 | /* and logarithmically reduce it until it's just under the divisor */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 86 | while (size >= divisor[units]) { |
| 87 | remainder = do_div(size, divisor[units]); |
| 88 | i++; |
| 89 | } |
| 90 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 91 | /* work out in j how many digits of precision we need from the |
| 92 | * remainder */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 93 | sf_cap = size; |
| 94 | for (j = 0; sf_cap*10 < 1000; j++) |
| 95 | sf_cap *= 10; |
| 96 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 97 | if (units == STRING_UNITS_2) { |
| 98 | /* express the remainder as a decimal. It's currently the |
| 99 | * numerator of a fraction whose denominator is |
| 100 | * divisor[units], which is 1 << 10 for STRING_UNITS_2 */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 101 | remainder *= 1000; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 102 | remainder >>= 10; |
| 103 | } |
| 104 | |
| 105 | /* add a 5 to the digit below what will be printed to ensure |
| 106 | * an arithmetical round up and carry it through to size */ |
| 107 | remainder += rounding[j]; |
| 108 | if (remainder >= 1000) { |
| 109 | remainder -= 1000; |
| 110 | size += 1; |
| 111 | } |
| 112 | |
| 113 | if (j) { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 114 | snprintf(tmp, sizeof(tmp), ".%03u", remainder); |
| 115 | tmp[j+1] = '\0'; |
| 116 | } |
| 117 | |
| 118 | out: |
| 119 | if (i >= ARRAY_SIZE(units_2)) |
| 120 | unit = "UNK"; |
| 121 | else |
| 122 | unit = units_str[units][i]; |
| 123 | |
Rasmus Villemoes | 84b9fbe | 2015-02-12 15:01:48 -0800 | [diff] [blame] | 124 | snprintf(buf, len, "%u%s %s", (u32)size, |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 125 | tmp, unit); |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL(string_get_size); |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 128 | |
| 129 | static bool unescape_space(char **src, char **dst) |
| 130 | { |
| 131 | char *p = *dst, *q = *src; |
| 132 | |
| 133 | switch (*q) { |
| 134 | case 'n': |
| 135 | *p = '\n'; |
| 136 | break; |
| 137 | case 'r': |
| 138 | *p = '\r'; |
| 139 | break; |
| 140 | case 't': |
| 141 | *p = '\t'; |
| 142 | break; |
| 143 | case 'v': |
| 144 | *p = '\v'; |
| 145 | break; |
| 146 | case 'f': |
| 147 | *p = '\f'; |
| 148 | break; |
| 149 | default: |
| 150 | return false; |
| 151 | } |
| 152 | *dst += 1; |
| 153 | *src += 1; |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | static bool unescape_octal(char **src, char **dst) |
| 158 | { |
| 159 | char *p = *dst, *q = *src; |
| 160 | u8 num; |
| 161 | |
| 162 | if (isodigit(*q) == 0) |
| 163 | return false; |
| 164 | |
| 165 | num = (*q++) & 7; |
| 166 | while (num < 32 && isodigit(*q) && (q - *src < 3)) { |
| 167 | num <<= 3; |
| 168 | num += (*q++) & 7; |
| 169 | } |
| 170 | *p = num; |
| 171 | *dst += 1; |
| 172 | *src = q; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | static bool unescape_hex(char **src, char **dst) |
| 177 | { |
| 178 | char *p = *dst, *q = *src; |
| 179 | int digit; |
| 180 | u8 num; |
| 181 | |
| 182 | if (*q++ != 'x') |
| 183 | return false; |
| 184 | |
| 185 | num = digit = hex_to_bin(*q++); |
| 186 | if (digit < 0) |
| 187 | return false; |
| 188 | |
| 189 | digit = hex_to_bin(*q); |
| 190 | if (digit >= 0) { |
| 191 | q++; |
| 192 | num = (num << 4) | digit; |
| 193 | } |
| 194 | *p = num; |
| 195 | *dst += 1; |
| 196 | *src = q; |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | static bool unescape_special(char **src, char **dst) |
| 201 | { |
| 202 | char *p = *dst, *q = *src; |
| 203 | |
| 204 | switch (*q) { |
| 205 | case '\"': |
| 206 | *p = '\"'; |
| 207 | break; |
| 208 | case '\\': |
| 209 | *p = '\\'; |
| 210 | break; |
| 211 | case 'a': |
| 212 | *p = '\a'; |
| 213 | break; |
| 214 | case 'e': |
| 215 | *p = '\e'; |
| 216 | break; |
| 217 | default: |
| 218 | return false; |
| 219 | } |
| 220 | *dst += 1; |
| 221 | *src += 1; |
| 222 | return true; |
| 223 | } |
| 224 | |
Andy Shevchenko | d295634 | 2014-10-13 15:55:11 -0700 | [diff] [blame] | 225 | /** |
| 226 | * string_unescape - unquote characters in the given string |
| 227 | * @src: source buffer (escaped) |
| 228 | * @dst: destination buffer (unescaped) |
| 229 | * @size: size of the destination buffer (0 to unlimit) |
| 230 | * @flags: combination of the flags (bitwise OR): |
| 231 | * %UNESCAPE_SPACE: |
| 232 | * '\f' - form feed |
| 233 | * '\n' - new line |
| 234 | * '\r' - carriage return |
| 235 | * '\t' - horizontal tab |
| 236 | * '\v' - vertical tab |
| 237 | * %UNESCAPE_OCTAL: |
| 238 | * '\NNN' - byte with octal value NNN (1 to 3 digits) |
| 239 | * %UNESCAPE_HEX: |
| 240 | * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) |
| 241 | * %UNESCAPE_SPECIAL: |
| 242 | * '\"' - double quote |
| 243 | * '\\' - backslash |
| 244 | * '\a' - alert (BEL) |
| 245 | * '\e' - escape |
| 246 | * %UNESCAPE_ANY: |
| 247 | * all previous together |
| 248 | * |
| 249 | * Description: |
| 250 | * The function unquotes characters in the given string. |
| 251 | * |
| 252 | * Because the size of the output will be the same as or less than the size of |
| 253 | * the input, the transformation may be performed in place. |
| 254 | * |
| 255 | * Caller must provide valid source and destination pointers. Be aware that |
| 256 | * destination buffer will always be NULL-terminated. Source string must be |
| 257 | * NULL-terminated as well. |
| 258 | * |
| 259 | * Return: |
| 260 | * The amount of the characters processed to the destination buffer excluding |
| 261 | * trailing '\0' is returned. |
| 262 | */ |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 263 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags) |
| 264 | { |
| 265 | char *out = dst; |
| 266 | |
| 267 | while (*src && --size) { |
| 268 | if (src[0] == '\\' && src[1] != '\0' && size > 1) { |
| 269 | src++; |
| 270 | size--; |
| 271 | |
| 272 | if (flags & UNESCAPE_SPACE && |
| 273 | unescape_space(&src, &out)) |
| 274 | continue; |
| 275 | |
| 276 | if (flags & UNESCAPE_OCTAL && |
| 277 | unescape_octal(&src, &out)) |
| 278 | continue; |
| 279 | |
| 280 | if (flags & UNESCAPE_HEX && |
| 281 | unescape_hex(&src, &out)) |
| 282 | continue; |
| 283 | |
| 284 | if (flags & UNESCAPE_SPECIAL && |
| 285 | unescape_special(&src, &out)) |
| 286 | continue; |
| 287 | |
| 288 | *out++ = '\\'; |
| 289 | } |
| 290 | *out++ = *src++; |
| 291 | } |
| 292 | *out = '\0'; |
| 293 | |
| 294 | return out - dst; |
| 295 | } |
| 296 | EXPORT_SYMBOL(string_unescape); |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 297 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 298 | static bool escape_passthrough(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 299 | { |
| 300 | char *out = *dst; |
| 301 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 302 | if (out < end) |
| 303 | *out = c; |
| 304 | *dst = out + 1; |
| 305 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 308 | static bool escape_space(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 309 | { |
| 310 | char *out = *dst; |
| 311 | unsigned char to; |
| 312 | |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 313 | switch (c) { |
| 314 | case '\n': |
| 315 | to = 'n'; |
| 316 | break; |
| 317 | case '\r': |
| 318 | to = 'r'; |
| 319 | break; |
| 320 | case '\t': |
| 321 | to = 't'; |
| 322 | break; |
| 323 | case '\v': |
| 324 | to = 'v'; |
| 325 | break; |
| 326 | case '\f': |
| 327 | to = 'f'; |
| 328 | break; |
| 329 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 330 | return false; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 333 | if (out < end) |
| 334 | *out = '\\'; |
| 335 | ++out; |
| 336 | if (out < end) |
| 337 | *out = to; |
| 338 | ++out; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 339 | |
| 340 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 341 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 344 | static bool escape_special(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 345 | { |
| 346 | char *out = *dst; |
| 347 | unsigned char to; |
| 348 | |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 349 | switch (c) { |
| 350 | case '\\': |
| 351 | to = '\\'; |
| 352 | break; |
| 353 | case '\a': |
| 354 | to = 'a'; |
| 355 | break; |
| 356 | case '\e': |
| 357 | to = 'e'; |
| 358 | break; |
| 359 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 360 | return false; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 363 | if (out < end) |
| 364 | *out = '\\'; |
| 365 | ++out; |
| 366 | if (out < end) |
| 367 | *out = to; |
| 368 | ++out; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 369 | |
| 370 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 371 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 374 | static bool escape_null(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 375 | { |
| 376 | char *out = *dst; |
| 377 | |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 378 | if (c) |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 379 | return false; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 380 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 381 | if (out < end) |
| 382 | *out = '\\'; |
| 383 | ++out; |
| 384 | if (out < end) |
| 385 | *out = '0'; |
| 386 | ++out; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 387 | |
| 388 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 389 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 392 | static bool escape_octal(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 393 | { |
| 394 | char *out = *dst; |
| 395 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 396 | if (out < end) |
| 397 | *out = '\\'; |
| 398 | ++out; |
| 399 | if (out < end) |
| 400 | *out = ((c >> 6) & 0x07) + '0'; |
| 401 | ++out; |
| 402 | if (out < end) |
| 403 | *out = ((c >> 3) & 0x07) + '0'; |
| 404 | ++out; |
| 405 | if (out < end) |
| 406 | *out = ((c >> 0) & 0x07) + '0'; |
| 407 | ++out; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 408 | |
| 409 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 410 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 413 | static bool escape_hex(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 414 | { |
| 415 | char *out = *dst; |
| 416 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 417 | if (out < end) |
| 418 | *out = '\\'; |
| 419 | ++out; |
| 420 | if (out < end) |
| 421 | *out = 'x'; |
| 422 | ++out; |
| 423 | if (out < end) |
| 424 | *out = hex_asc_hi(c); |
| 425 | ++out; |
| 426 | if (out < end) |
| 427 | *out = hex_asc_lo(c); |
| 428 | ++out; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 429 | |
| 430 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 431 | return true; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * string_escape_mem - quote characters in the given memory buffer |
| 436 | * @src: source buffer (unescaped) |
| 437 | * @isz: source buffer size |
| 438 | * @dst: destination buffer (escaped) |
| 439 | * @osz: destination buffer size |
| 440 | * @flags: combination of the flags (bitwise OR): |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 441 | * %ESCAPE_SPACE: (special white space, not space itself) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 442 | * '\f' - form feed |
| 443 | * '\n' - new line |
| 444 | * '\r' - carriage return |
| 445 | * '\t' - horizontal tab |
| 446 | * '\v' - vertical tab |
| 447 | * %ESCAPE_SPECIAL: |
| 448 | * '\\' - backslash |
| 449 | * '\a' - alert (BEL) |
| 450 | * '\e' - escape |
| 451 | * %ESCAPE_NULL: |
| 452 | * '\0' - null |
| 453 | * %ESCAPE_OCTAL: |
| 454 | * '\NNN' - byte with octal value NNN (3 digits) |
| 455 | * %ESCAPE_ANY: |
| 456 | * all previous together |
| 457 | * %ESCAPE_NP: |
| 458 | * escape only non-printable characters (checked by isprint) |
| 459 | * %ESCAPE_ANY_NP: |
| 460 | * all previous together |
| 461 | * %ESCAPE_HEX: |
| 462 | * '\xHH' - byte with hexadecimal value HH (2 digits) |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 463 | * @only: NULL-terminated string containing characters used to limit |
| 464 | * the selected escape class. If characters are included in @only |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 465 | * that would not normally be escaped by the classes selected |
| 466 | * in @flags, they will be copied to @dst unescaped. |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 467 | * |
| 468 | * Description: |
| 469 | * The process of escaping byte buffer includes several parts. They are applied |
| 470 | * in the following sequence. |
| 471 | * 1. The character is matched to the printable class, if asked, and in |
| 472 | * case of match it passes through to the output. |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 473 | * 2. The character is not matched to the one from @only string and thus |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 474 | * must go as-is to the output. |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 475 | * 3. The character is checked if it falls into the class given by @flags. |
| 476 | * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any |
| 477 | * character. Note that they actually can't go together, otherwise |
| 478 | * %ESCAPE_HEX will be ignored. |
| 479 | * |
| 480 | * Caller must provide valid source and destination pointers. Be aware that |
| 481 | * destination buffer will not be NULL-terminated, thus caller have to append |
| 482 | * it if needs. |
| 483 | * |
| 484 | * Return: |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 485 | * The total size of the escaped output that would be generated for |
| 486 | * the given input and flags. To check whether the output was |
| 487 | * truncated, compare the return value to osz. There is room left in |
| 488 | * dst for a '\0' terminator if and only if ret < osz. |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 489 | */ |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 490 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 491 | unsigned int flags, const char *only) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 492 | { |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 493 | char *p = dst; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 494 | char *end = p + osz; |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 495 | bool is_dict = only && *only; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 496 | |
| 497 | while (isz--) { |
| 498 | unsigned char c = *src++; |
| 499 | |
| 500 | /* |
| 501 | * Apply rules in the following sequence: |
| 502 | * - the character is printable, when @flags has |
| 503 | * %ESCAPE_NP bit set |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 504 | * - the @only string is supplied and does not contain a |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 505 | * character under question |
| 506 | * - the character doesn't fall into a class of symbols |
| 507 | * defined by given @flags |
| 508 | * In these cases we just pass through a character to the |
| 509 | * output buffer. |
| 510 | */ |
| 511 | if ((flags & ESCAPE_NP && isprint(c)) || |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 512 | (is_dict && !strchr(only, c))) { |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 513 | /* do nothing */ |
| 514 | } else { |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 515 | if (flags & ESCAPE_SPACE && escape_space(c, &p, end)) |
| 516 | continue; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 517 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 518 | if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end)) |
| 519 | continue; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 520 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 521 | if (flags & ESCAPE_NULL && escape_null(c, &p, end)) |
| 522 | continue; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 523 | |
| 524 | /* ESCAPE_OCTAL and ESCAPE_HEX always go last */ |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 525 | if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end)) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 526 | continue; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 527 | |
| 528 | if (flags & ESCAPE_HEX && escape_hex(c, &p, end)) |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 529 | continue; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 532 | escape_passthrough(c, &p, end); |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 535 | return p - dst; |
Andy Shevchenko | c8250381c | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL(string_escape_mem); |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame^] | 538 | |
| 539 | /* |
| 540 | * Return an allocated string that has been escaped of special characters |
| 541 | * and double quotes, making it safe to log in quotes. |
| 542 | */ |
| 543 | char *kstrdup_quotable(const char *src, gfp_t gfp) |
| 544 | { |
| 545 | size_t slen, dlen; |
| 546 | char *dst; |
| 547 | const int flags = ESCAPE_HEX; |
| 548 | const char esc[] = "\f\n\r\t\v\a\e\\\""; |
| 549 | |
| 550 | if (!src) |
| 551 | return NULL; |
| 552 | slen = strlen(src); |
| 553 | |
| 554 | dlen = string_escape_mem(src, slen, NULL, 0, flags, esc); |
| 555 | dst = kmalloc(dlen + 1, gfp); |
| 556 | if (!dst) |
| 557 | return NULL; |
| 558 | |
| 559 | WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen); |
| 560 | dst[dlen] = '\0'; |
| 561 | |
| 562 | return dst; |
| 563 | } |
| 564 | EXPORT_SYMBOL_GPL(kstrdup_quotable); |