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 | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/string.h> |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 14 | #include <linux/string_helpers.h> |
| 15 | |
| 16 | /** |
| 17 | * string_get_size - get the size in the specified units |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 18 | * @size: The size to be converted in blocks |
| 19 | * @blk_size: Size of the block (use 1 for size in bytes) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 20 | * @units: units to use (powers of 1000 or 1024) |
| 21 | * @buf: buffer to format to |
| 22 | * @len: length of buffer |
| 23 | * |
| 24 | * This function returns a string formatted to 3 significant figures |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 25 | * giving the size in the required units. @buf should have room for |
| 26 | * at least 9 bytes and will always be zero terminated. |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 27 | * |
| 28 | */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 29 | 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] | 30 | char *buf, int len) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 31 | { |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 32 | static const char *const units_10[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 33 | "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 34 | }; |
| 35 | static const char *const units_2[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 36 | "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 37 | }; |
| 38 | static const char *const *const units_str[] = { |
| 39 | [STRING_UNITS_10] = units_10, |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 40 | [STRING_UNITS_2] = units_2, |
| 41 | }; |
Andrew Morton | 68aecfb | 2012-05-29 15:07:32 -0700 | [diff] [blame] | 42 | static const unsigned int divisor[] = { |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 43 | [STRING_UNITS_10] = 1000, |
| 44 | [STRING_UNITS_2] = 1024, |
| 45 | }; |
| 46 | int i, j; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 47 | u32 remainder = 0, sf_cap, exp; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 48 | char tmp[8]; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 49 | const char *unit; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 50 | |
| 51 | tmp[0] = '\0'; |
H. Peter Anvin | a865959 | 2008-10-14 11:34:21 -0700 | [diff] [blame] | 52 | i = 0; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 53 | if (!size) |
| 54 | goto out; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 55 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 56 | while (blk_size >= divisor[units]) { |
| 57 | remainder = do_div(blk_size, divisor[units]); |
| 58 | i++; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 59 | } |
| 60 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 61 | exp = divisor[units] / (u32)blk_size; |
Vitaly Kuznetsov | 62bef58 | 2015-09-17 16:01:51 -0700 | [diff] [blame] | 62 | /* |
| 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. |
| 65 | */ |
| 66 | if (size > exp) { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 67 | remainder = do_div(size, divisor[units]); |
| 68 | remainder *= blk_size; |
| 69 | i++; |
| 70 | } else { |
| 71 | remainder *= size; |
| 72 | } |
| 73 | |
| 74 | size *= blk_size; |
| 75 | size += remainder / divisor[units]; |
| 76 | remainder %= divisor[units]; |
| 77 | |
| 78 | while (size >= divisor[units]) { |
| 79 | remainder = do_div(size, divisor[units]); |
| 80 | i++; |
| 81 | } |
| 82 | |
| 83 | sf_cap = size; |
| 84 | for (j = 0; sf_cap*10 < 1000; j++) |
| 85 | sf_cap *= 10; |
| 86 | |
| 87 | if (j) { |
| 88 | remainder *= 1000; |
| 89 | remainder /= divisor[units]; |
| 90 | snprintf(tmp, sizeof(tmp), ".%03u", remainder); |
| 91 | tmp[j+1] = '\0'; |
| 92 | } |
| 93 | |
| 94 | out: |
| 95 | if (i >= ARRAY_SIZE(units_2)) |
| 96 | unit = "UNK"; |
| 97 | else |
| 98 | unit = units_str[units][i]; |
| 99 | |
Rasmus Villemoes | 84b9fbe | 2015-02-12 15:01:48 -0800 | [diff] [blame] | 100 | snprintf(buf, len, "%u%s %s", (u32)size, |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 101 | tmp, unit); |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 102 | } |
| 103 | EXPORT_SYMBOL(string_get_size); |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 104 | |
| 105 | static bool unescape_space(char **src, char **dst) |
| 106 | { |
| 107 | char *p = *dst, *q = *src; |
| 108 | |
| 109 | switch (*q) { |
| 110 | case 'n': |
| 111 | *p = '\n'; |
| 112 | break; |
| 113 | case 'r': |
| 114 | *p = '\r'; |
| 115 | break; |
| 116 | case 't': |
| 117 | *p = '\t'; |
| 118 | break; |
| 119 | case 'v': |
| 120 | *p = '\v'; |
| 121 | break; |
| 122 | case 'f': |
| 123 | *p = '\f'; |
| 124 | break; |
| 125 | default: |
| 126 | return false; |
| 127 | } |
| 128 | *dst += 1; |
| 129 | *src += 1; |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | static bool unescape_octal(char **src, char **dst) |
| 134 | { |
| 135 | char *p = *dst, *q = *src; |
| 136 | u8 num; |
| 137 | |
| 138 | if (isodigit(*q) == 0) |
| 139 | return false; |
| 140 | |
| 141 | num = (*q++) & 7; |
| 142 | while (num < 32 && isodigit(*q) && (q - *src < 3)) { |
| 143 | num <<= 3; |
| 144 | num += (*q++) & 7; |
| 145 | } |
| 146 | *p = num; |
| 147 | *dst += 1; |
| 148 | *src = q; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static bool unescape_hex(char **src, char **dst) |
| 153 | { |
| 154 | char *p = *dst, *q = *src; |
| 155 | int digit; |
| 156 | u8 num; |
| 157 | |
| 158 | if (*q++ != 'x') |
| 159 | return false; |
| 160 | |
| 161 | num = digit = hex_to_bin(*q++); |
| 162 | if (digit < 0) |
| 163 | return false; |
| 164 | |
| 165 | digit = hex_to_bin(*q); |
| 166 | if (digit >= 0) { |
| 167 | q++; |
| 168 | num = (num << 4) | digit; |
| 169 | } |
| 170 | *p = num; |
| 171 | *dst += 1; |
| 172 | *src = q; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | static bool unescape_special(char **src, char **dst) |
| 177 | { |
| 178 | char *p = *dst, *q = *src; |
| 179 | |
| 180 | switch (*q) { |
| 181 | case '\"': |
| 182 | *p = '\"'; |
| 183 | break; |
| 184 | case '\\': |
| 185 | *p = '\\'; |
| 186 | break; |
| 187 | case 'a': |
| 188 | *p = '\a'; |
| 189 | break; |
| 190 | case 'e': |
| 191 | *p = '\e'; |
| 192 | break; |
| 193 | default: |
| 194 | return false; |
| 195 | } |
| 196 | *dst += 1; |
| 197 | *src += 1; |
| 198 | return true; |
| 199 | } |
| 200 | |
Andy Shevchenko | d295634 | 2014-10-13 15:55:11 -0700 | [diff] [blame] | 201 | /** |
| 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): |
| 207 | * %UNESCAPE_SPACE: |
| 208 | * '\f' - form feed |
| 209 | * '\n' - new line |
| 210 | * '\r' - carriage return |
| 211 | * '\t' - horizontal tab |
| 212 | * '\v' - vertical tab |
| 213 | * %UNESCAPE_OCTAL: |
| 214 | * '\NNN' - byte with octal value NNN (1 to 3 digits) |
| 215 | * %UNESCAPE_HEX: |
| 216 | * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) |
| 217 | * %UNESCAPE_SPECIAL: |
| 218 | * '\"' - double quote |
| 219 | * '\\' - backslash |
| 220 | * '\a' - alert (BEL) |
| 221 | * '\e' - escape |
| 222 | * %UNESCAPE_ANY: |
| 223 | * all previous together |
| 224 | * |
| 225 | * Description: |
| 226 | * The function unquotes characters in the given string. |
| 227 | * |
| 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. |
| 230 | * |
| 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. |
| 234 | * |
| 235 | * Return: |
| 236 | * The amount of the characters processed to the destination buffer excluding |
| 237 | * trailing '\0' is returned. |
| 238 | */ |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 239 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags) |
| 240 | { |
| 241 | char *out = dst; |
| 242 | |
| 243 | while (*src && --size) { |
| 244 | if (src[0] == '\\' && src[1] != '\0' && size > 1) { |
| 245 | src++; |
| 246 | size--; |
| 247 | |
| 248 | if (flags & UNESCAPE_SPACE && |
| 249 | unescape_space(&src, &out)) |
| 250 | continue; |
| 251 | |
| 252 | if (flags & UNESCAPE_OCTAL && |
| 253 | unescape_octal(&src, &out)) |
| 254 | continue; |
| 255 | |
| 256 | if (flags & UNESCAPE_HEX && |
| 257 | unescape_hex(&src, &out)) |
| 258 | continue; |
| 259 | |
| 260 | if (flags & UNESCAPE_SPECIAL && |
| 261 | unescape_special(&src, &out)) |
| 262 | continue; |
| 263 | |
| 264 | *out++ = '\\'; |
| 265 | } |
| 266 | *out++ = *src++; |
| 267 | } |
| 268 | *out = '\0'; |
| 269 | |
| 270 | return out - dst; |
| 271 | } |
| 272 | EXPORT_SYMBOL(string_unescape); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 273 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 274 | static bool escape_passthrough(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 275 | { |
| 276 | char *out = *dst; |
| 277 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 278 | if (out < end) |
| 279 | *out = c; |
| 280 | *dst = out + 1; |
| 281 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 284 | static bool escape_space(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 285 | { |
| 286 | char *out = *dst; |
| 287 | unsigned char to; |
| 288 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 289 | switch (c) { |
| 290 | case '\n': |
| 291 | to = 'n'; |
| 292 | break; |
| 293 | case '\r': |
| 294 | to = 'r'; |
| 295 | break; |
| 296 | case '\t': |
| 297 | to = 't'; |
| 298 | break; |
| 299 | case '\v': |
| 300 | to = 'v'; |
| 301 | break; |
| 302 | case '\f': |
| 303 | to = 'f'; |
| 304 | break; |
| 305 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 306 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 309 | if (out < end) |
| 310 | *out = '\\'; |
| 311 | ++out; |
| 312 | if (out < end) |
| 313 | *out = to; |
| 314 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 315 | |
| 316 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 317 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 320 | static bool escape_special(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 321 | { |
| 322 | char *out = *dst; |
| 323 | unsigned char to; |
| 324 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 325 | switch (c) { |
| 326 | case '\\': |
| 327 | to = '\\'; |
| 328 | break; |
| 329 | case '\a': |
| 330 | to = 'a'; |
| 331 | break; |
| 332 | case '\e': |
| 333 | to = 'e'; |
| 334 | break; |
| 335 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 336 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 339 | if (out < end) |
| 340 | *out = '\\'; |
| 341 | ++out; |
| 342 | if (out < end) |
| 343 | *out = to; |
| 344 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 345 | |
| 346 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 347 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 350 | static bool escape_null(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 351 | { |
| 352 | char *out = *dst; |
| 353 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 354 | if (c) |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 355 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 356 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 357 | if (out < end) |
| 358 | *out = '\\'; |
| 359 | ++out; |
| 360 | if (out < end) |
| 361 | *out = '0'; |
| 362 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 363 | |
| 364 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 365 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 368 | static bool escape_octal(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 369 | { |
| 370 | char *out = *dst; |
| 371 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 372 | if (out < end) |
| 373 | *out = '\\'; |
| 374 | ++out; |
| 375 | if (out < end) |
| 376 | *out = ((c >> 6) & 0x07) + '0'; |
| 377 | ++out; |
| 378 | if (out < end) |
| 379 | *out = ((c >> 3) & 0x07) + '0'; |
| 380 | ++out; |
| 381 | if (out < end) |
| 382 | *out = ((c >> 0) & 0x07) + '0'; |
| 383 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 384 | |
| 385 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 386 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 389 | static bool escape_hex(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 390 | { |
| 391 | char *out = *dst; |
| 392 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 393 | if (out < end) |
| 394 | *out = '\\'; |
| 395 | ++out; |
| 396 | if (out < end) |
| 397 | *out = 'x'; |
| 398 | ++out; |
| 399 | if (out < end) |
| 400 | *out = hex_asc_hi(c); |
| 401 | ++out; |
| 402 | if (out < end) |
| 403 | *out = hex_asc_lo(c); |
| 404 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 405 | |
| 406 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 407 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 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): |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 417 | * %ESCAPE_SPACE: (special white space, not space itself) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 418 | * '\f' - form feed |
| 419 | * '\n' - new line |
| 420 | * '\r' - carriage return |
| 421 | * '\t' - horizontal tab |
| 422 | * '\v' - vertical tab |
| 423 | * %ESCAPE_SPECIAL: |
| 424 | * '\\' - backslash |
| 425 | * '\a' - alert (BEL) |
| 426 | * '\e' - escape |
| 427 | * %ESCAPE_NULL: |
| 428 | * '\0' - null |
| 429 | * %ESCAPE_OCTAL: |
| 430 | * '\NNN' - byte with octal value NNN (3 digits) |
| 431 | * %ESCAPE_ANY: |
| 432 | * all previous together |
| 433 | * %ESCAPE_NP: |
| 434 | * escape only non-printable characters (checked by isprint) |
| 435 | * %ESCAPE_ANY_NP: |
| 436 | * all previous together |
| 437 | * %ESCAPE_HEX: |
| 438 | * '\xHH' - byte with hexadecimal value HH (2 digits) |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 439 | * @only: NULL-terminated string containing characters used to limit |
| 440 | * the selected escape class. If characters are included in @only |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 441 | * that would not normally be escaped by the classes selected |
| 442 | * in @flags, they will be copied to @dst unescaped. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 443 | * |
| 444 | * Description: |
| 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. |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 449 | * 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] | 450 | * must go as-is to the output. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 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. |
| 455 | * |
| 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 |
| 458 | * it if needs. |
| 459 | * |
| 460 | * Return: |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 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. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 465 | */ |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 466 | 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] | 467 | unsigned int flags, const char *only) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 468 | { |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 469 | char *p = dst; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 470 | char *end = p + osz; |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 471 | bool is_dict = only && *only; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 472 | |
| 473 | while (isz--) { |
| 474 | unsigned char c = *src++; |
| 475 | |
| 476 | /* |
| 477 | * Apply rules in the following sequence: |
| 478 | * - the character is printable, when @flags has |
| 479 | * %ESCAPE_NP bit set |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 480 | * - the @only string is supplied and does not contain a |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 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 |
| 485 | * output buffer. |
| 486 | */ |
| 487 | if ((flags & ESCAPE_NP && isprint(c)) || |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 488 | (is_dict && !strchr(only, c))) { |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 489 | /* do nothing */ |
| 490 | } else { |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 491 | if (flags & ESCAPE_SPACE && escape_space(c, &p, end)) |
| 492 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 493 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 494 | if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end)) |
| 495 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 496 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 497 | if (flags & ESCAPE_NULL && escape_null(c, &p, end)) |
| 498 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 499 | |
| 500 | /* ESCAPE_OCTAL and ESCAPE_HEX always go last */ |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 501 | if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end)) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 502 | continue; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 503 | |
| 504 | if (flags & ESCAPE_HEX && escape_hex(c, &p, end)) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 505 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 508 | escape_passthrough(c, &p, end); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 511 | return p - dst; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 512 | } |
| 513 | EXPORT_SYMBOL(string_escape_mem); |