string_helpers.c 13 KB

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