string_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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/string.h>
  14. #include <linux/string_helpers.h>
  15. /**
  16. * string_get_size - get the size in the specified units
  17. * @size: The size to be converted in blocks
  18. * @blk_size: Size of the block (use 1 for size in bytes)
  19. * @units: units to use (powers of 1000 or 1024)
  20. * @buf: buffer to format to
  21. * @len: length of buffer
  22. *
  23. * This function returns a string formatted to 3 significant figures
  24. * giving the size in the required units. @buf should have room for
  25. * at least 9 bytes and will always be zero terminated.
  26. *
  27. */
  28. void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
  29. char *buf, int len)
  30. {
  31. static const char *const units_10[] = {
  32. "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
  33. };
  34. static const char *const units_2[] = {
  35. "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
  36. };
  37. static const char *const *const units_str[] = {
  38. [STRING_UNITS_10] = units_10,
  39. [STRING_UNITS_2] = units_2,
  40. };
  41. static const unsigned int divisor[] = {
  42. [STRING_UNITS_10] = 1000,
  43. [STRING_UNITS_2] = 1024,
  44. };
  45. int i, j;
  46. u32 remainder = 0, sf_cap, exp;
  47. char tmp[8];
  48. const char *unit;
  49. tmp[0] = '\0';
  50. i = 0;
  51. if (!size)
  52. goto out;
  53. while (blk_size >= divisor[units]) {
  54. remainder = do_div(blk_size, divisor[units]);
  55. i++;
  56. }
  57. exp = divisor[units] / (u32)blk_size;
  58. if (size >= exp) {
  59. remainder = do_div(size, divisor[units]);
  60. remainder *= blk_size;
  61. i++;
  62. } else {
  63. remainder *= size;
  64. }
  65. size *= blk_size;
  66. size += remainder / divisor[units];
  67. remainder %= divisor[units];
  68. while (size >= divisor[units]) {
  69. remainder = do_div(size, divisor[units]);
  70. i++;
  71. }
  72. sf_cap = size;
  73. for (j = 0; sf_cap*10 < 1000; j++)
  74. sf_cap *= 10;
  75. if (j) {
  76. remainder *= 1000;
  77. remainder /= divisor[units];
  78. snprintf(tmp, sizeof(tmp), ".%03u", remainder);
  79. tmp[j+1] = '\0';
  80. }
  81. out:
  82. if (i >= ARRAY_SIZE(units_2))
  83. unit = "UNK";
  84. else
  85. unit = units_str[units][i];
  86. snprintf(buf, len, "%u%s %s", (u32)size,
  87. tmp, unit);
  88. }
  89. EXPORT_SYMBOL(string_get_size);
  90. static bool unescape_space(char **src, char **dst)
  91. {
  92. char *p = *dst, *q = *src;
  93. switch (*q) {
  94. case 'n':
  95. *p = '\n';
  96. break;
  97. case 'r':
  98. *p = '\r';
  99. break;
  100. case 't':
  101. *p = '\t';
  102. break;
  103. case 'v':
  104. *p = '\v';
  105. break;
  106. case 'f':
  107. *p = '\f';
  108. break;
  109. default:
  110. return false;
  111. }
  112. *dst += 1;
  113. *src += 1;
  114. return true;
  115. }
  116. static bool unescape_octal(char **src, char **dst)
  117. {
  118. char *p = *dst, *q = *src;
  119. u8 num;
  120. if (isodigit(*q) == 0)
  121. return false;
  122. num = (*q++) & 7;
  123. while (num < 32 && isodigit(*q) && (q - *src < 3)) {
  124. num <<= 3;
  125. num += (*q++) & 7;
  126. }
  127. *p = num;
  128. *dst += 1;
  129. *src = q;
  130. return true;
  131. }
  132. static bool unescape_hex(char **src, char **dst)
  133. {
  134. char *p = *dst, *q = *src;
  135. int digit;
  136. u8 num;
  137. if (*q++ != 'x')
  138. return false;
  139. num = digit = hex_to_bin(*q++);
  140. if (digit < 0)
  141. return false;
  142. digit = hex_to_bin(*q);
  143. if (digit >= 0) {
  144. q++;
  145. num = (num << 4) | digit;
  146. }
  147. *p = num;
  148. *dst += 1;
  149. *src = q;
  150. return true;
  151. }
  152. static bool unescape_special(char **src, char **dst)
  153. {
  154. char *p = *dst, *q = *src;
  155. switch (*q) {
  156. case '\"':
  157. *p = '\"';
  158. break;
  159. case '\\':
  160. *p = '\\';
  161. break;
  162. case 'a':
  163. *p = '\a';
  164. break;
  165. case 'e':
  166. *p = '\e';
  167. break;
  168. default:
  169. return false;
  170. }
  171. *dst += 1;
  172. *src += 1;
  173. return true;
  174. }
  175. /**
  176. * string_unescape - unquote characters in the given string
  177. * @src: source buffer (escaped)
  178. * @dst: destination buffer (unescaped)
  179. * @size: size of the destination buffer (0 to unlimit)
  180. * @flags: combination of the flags (bitwise OR):
  181. * %UNESCAPE_SPACE:
  182. * '\f' - form feed
  183. * '\n' - new line
  184. * '\r' - carriage return
  185. * '\t' - horizontal tab
  186. * '\v' - vertical tab
  187. * %UNESCAPE_OCTAL:
  188. * '\NNN' - byte with octal value NNN (1 to 3 digits)
  189. * %UNESCAPE_HEX:
  190. * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
  191. * %UNESCAPE_SPECIAL:
  192. * '\"' - double quote
  193. * '\\' - backslash
  194. * '\a' - alert (BEL)
  195. * '\e' - escape
  196. * %UNESCAPE_ANY:
  197. * all previous together
  198. *
  199. * Description:
  200. * The function unquotes characters in the given string.
  201. *
  202. * Because the size of the output will be the same as or less than the size of
  203. * the input, the transformation may be performed in place.
  204. *
  205. * Caller must provide valid source and destination pointers. Be aware that
  206. * destination buffer will always be NULL-terminated. Source string must be
  207. * NULL-terminated as well.
  208. *
  209. * Return:
  210. * The amount of the characters processed to the destination buffer excluding
  211. * trailing '\0' is returned.
  212. */
  213. int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
  214. {
  215. char *out = dst;
  216. while (*src && --size) {
  217. if (src[0] == '\\' && src[1] != '\0' && size > 1) {
  218. src++;
  219. size--;
  220. if (flags & UNESCAPE_SPACE &&
  221. unescape_space(&src, &out))
  222. continue;
  223. if (flags & UNESCAPE_OCTAL &&
  224. unescape_octal(&src, &out))
  225. continue;
  226. if (flags & UNESCAPE_HEX &&
  227. unescape_hex(&src, &out))
  228. continue;
  229. if (flags & UNESCAPE_SPECIAL &&
  230. unescape_special(&src, &out))
  231. continue;
  232. *out++ = '\\';
  233. }
  234. *out++ = *src++;
  235. }
  236. *out = '\0';
  237. return out - dst;
  238. }
  239. EXPORT_SYMBOL(string_unescape);
  240. static int escape_passthrough(unsigned char c, char **dst, size_t *osz)
  241. {
  242. char *out = *dst;
  243. if (*osz < 1)
  244. return -ENOMEM;
  245. *out++ = c;
  246. *dst = out;
  247. *osz -= 1;
  248. return 1;
  249. }
  250. static int escape_space(unsigned char c, char **dst, size_t *osz)
  251. {
  252. char *out = *dst;
  253. unsigned char to;
  254. if (*osz < 2)
  255. return -ENOMEM;
  256. switch (c) {
  257. case '\n':
  258. to = 'n';
  259. break;
  260. case '\r':
  261. to = 'r';
  262. break;
  263. case '\t':
  264. to = 't';
  265. break;
  266. case '\v':
  267. to = 'v';
  268. break;
  269. case '\f':
  270. to = 'f';
  271. break;
  272. default:
  273. return 0;
  274. }
  275. *out++ = '\\';
  276. *out++ = to;
  277. *dst = out;
  278. *osz -= 2;
  279. return 1;
  280. }
  281. static int escape_special(unsigned char c, char **dst, size_t *osz)
  282. {
  283. char *out = *dst;
  284. unsigned char to;
  285. if (*osz < 2)
  286. return -ENOMEM;
  287. switch (c) {
  288. case '\\':
  289. to = '\\';
  290. break;
  291. case '\a':
  292. to = 'a';
  293. break;
  294. case '\e':
  295. to = 'e';
  296. break;
  297. default:
  298. return 0;
  299. }
  300. *out++ = '\\';
  301. *out++ = to;
  302. *dst = out;
  303. *osz -= 2;
  304. return 1;
  305. }
  306. static int escape_null(unsigned char c, char **dst, size_t *osz)
  307. {
  308. char *out = *dst;
  309. if (*osz < 2)
  310. return -ENOMEM;
  311. if (c)
  312. return 0;
  313. *out++ = '\\';
  314. *out++ = '0';
  315. *dst = out;
  316. *osz -= 2;
  317. return 1;
  318. }
  319. static int escape_octal(unsigned char c, char **dst, size_t *osz)
  320. {
  321. char *out = *dst;
  322. if (*osz < 4)
  323. return -ENOMEM;
  324. *out++ = '\\';
  325. *out++ = ((c >> 6) & 0x07) + '0';
  326. *out++ = ((c >> 3) & 0x07) + '0';
  327. *out++ = ((c >> 0) & 0x07) + '0';
  328. *dst = out;
  329. *osz -= 4;
  330. return 1;
  331. }
  332. static int escape_hex(unsigned char c, char **dst, size_t *osz)
  333. {
  334. char *out = *dst;
  335. if (*osz < 4)
  336. return -ENOMEM;
  337. *out++ = '\\';
  338. *out++ = 'x';
  339. *out++ = hex_asc_hi(c);
  340. *out++ = hex_asc_lo(c);
  341. *dst = out;
  342. *osz -= 4;
  343. return 1;
  344. }
  345. /**
  346. * string_escape_mem - quote characters in the given memory buffer
  347. * @src: source buffer (unescaped)
  348. * @isz: source buffer size
  349. * @dst: destination buffer (escaped)
  350. * @osz: destination buffer size
  351. * @flags: combination of the flags (bitwise OR):
  352. * %ESCAPE_SPACE:
  353. * '\f' - form feed
  354. * '\n' - new line
  355. * '\r' - carriage return
  356. * '\t' - horizontal tab
  357. * '\v' - vertical tab
  358. * %ESCAPE_SPECIAL:
  359. * '\\' - backslash
  360. * '\a' - alert (BEL)
  361. * '\e' - escape
  362. * %ESCAPE_NULL:
  363. * '\0' - null
  364. * %ESCAPE_OCTAL:
  365. * '\NNN' - byte with octal value NNN (3 digits)
  366. * %ESCAPE_ANY:
  367. * all previous together
  368. * %ESCAPE_NP:
  369. * escape only non-printable characters (checked by isprint)
  370. * %ESCAPE_ANY_NP:
  371. * all previous together
  372. * %ESCAPE_HEX:
  373. * '\xHH' - byte with hexadecimal value HH (2 digits)
  374. * @esc: NULL-terminated string of characters any of which, if found in
  375. * the source, has to be escaped
  376. *
  377. * Description:
  378. * The process of escaping byte buffer includes several parts. They are applied
  379. * in the following sequence.
  380. * 1. The character is matched to the printable class, if asked, and in
  381. * case of match it passes through to the output.
  382. * 2. The character is not matched to the one from @esc string and thus
  383. * must go as is to the output.
  384. * 3. The character is checked if it falls into the class given by @flags.
  385. * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
  386. * character. Note that they actually can't go together, otherwise
  387. * %ESCAPE_HEX will be ignored.
  388. *
  389. * Caller must provide valid source and destination pointers. Be aware that
  390. * destination buffer will not be NULL-terminated, thus caller have to append
  391. * it if needs.
  392. *
  393. * Return:
  394. * The amount of the characters processed to the destination buffer, or
  395. * %-ENOMEM if the size of buffer is not enough to put an escaped character is
  396. * returned.
  397. *
  398. * Even in the case of error @dst pointer will be updated to point to the byte
  399. * after the last processed character.
  400. */
  401. int string_escape_mem(const char *src, size_t isz, char **dst, size_t osz,
  402. unsigned int flags, const char *esc)
  403. {
  404. char *out = *dst, *p = out;
  405. bool is_dict = esc && *esc;
  406. int ret = 0;
  407. while (isz--) {
  408. unsigned char c = *src++;
  409. /*
  410. * Apply rules in the following sequence:
  411. * - the character is printable, when @flags has
  412. * %ESCAPE_NP bit set
  413. * - the @esc string is supplied and does not contain a
  414. * character under question
  415. * - the character doesn't fall into a class of symbols
  416. * defined by given @flags
  417. * In these cases we just pass through a character to the
  418. * output buffer.
  419. */
  420. if ((flags & ESCAPE_NP && isprint(c)) ||
  421. (is_dict && !strchr(esc, c))) {
  422. /* do nothing */
  423. } else {
  424. if (flags & ESCAPE_SPACE) {
  425. ret = escape_space(c, &p, &osz);
  426. if (ret < 0)
  427. break;
  428. if (ret > 0)
  429. continue;
  430. }
  431. if (flags & ESCAPE_SPECIAL) {
  432. ret = escape_special(c, &p, &osz);
  433. if (ret < 0)
  434. break;
  435. if (ret > 0)
  436. continue;
  437. }
  438. if (flags & ESCAPE_NULL) {
  439. ret = escape_null(c, &p, &osz);
  440. if (ret < 0)
  441. break;
  442. if (ret > 0)
  443. continue;
  444. }
  445. /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
  446. if (flags & ESCAPE_OCTAL) {
  447. ret = escape_octal(c, &p, &osz);
  448. if (ret < 0)
  449. break;
  450. continue;
  451. }
  452. if (flags & ESCAPE_HEX) {
  453. ret = escape_hex(c, &p, &osz);
  454. if (ret < 0)
  455. break;
  456. continue;
  457. }
  458. }
  459. ret = escape_passthrough(c, &p, &osz);
  460. if (ret < 0)
  461. break;
  462. }
  463. *dst = p;
  464. if (ret < 0)
  465. return ret;
  466. return p - out;
  467. }
  468. EXPORT_SYMBOL(string_escape_mem);