string_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 bool escape_passthrough(unsigned char c, char **dst, char *end)
  241. {
  242. char *out = *dst;
  243. if (out < end)
  244. *out = c;
  245. *dst = out + 1;
  246. return true;
  247. }
  248. static bool escape_space(unsigned char c, char **dst, char *end)
  249. {
  250. char *out = *dst;
  251. unsigned char to;
  252. switch (c) {
  253. case '\n':
  254. to = 'n';
  255. break;
  256. case '\r':
  257. to = 'r';
  258. break;
  259. case '\t':
  260. to = 't';
  261. break;
  262. case '\v':
  263. to = 'v';
  264. break;
  265. case '\f':
  266. to = 'f';
  267. break;
  268. default:
  269. return false;
  270. }
  271. if (out < end)
  272. *out = '\\';
  273. ++out;
  274. if (out < end)
  275. *out = to;
  276. ++out;
  277. *dst = out;
  278. return true;
  279. }
  280. static bool escape_special(unsigned char c, char **dst, char *end)
  281. {
  282. char *out = *dst;
  283. unsigned char to;
  284. switch (c) {
  285. case '\\':
  286. to = '\\';
  287. break;
  288. case '\a':
  289. to = 'a';
  290. break;
  291. case '\e':
  292. to = 'e';
  293. break;
  294. default:
  295. return false;
  296. }
  297. if (out < end)
  298. *out = '\\';
  299. ++out;
  300. if (out < end)
  301. *out = to;
  302. ++out;
  303. *dst = out;
  304. return true;
  305. }
  306. static bool escape_null(unsigned char c, char **dst, char *end)
  307. {
  308. char *out = *dst;
  309. if (c)
  310. return false;
  311. if (out < end)
  312. *out = '\\';
  313. ++out;
  314. if (out < end)
  315. *out = '0';
  316. ++out;
  317. *dst = out;
  318. return true;
  319. }
  320. static bool escape_octal(unsigned char c, char **dst, char *end)
  321. {
  322. char *out = *dst;
  323. if (out < end)
  324. *out = '\\';
  325. ++out;
  326. if (out < end)
  327. *out = ((c >> 6) & 0x07) + '0';
  328. ++out;
  329. if (out < end)
  330. *out = ((c >> 3) & 0x07) + '0';
  331. ++out;
  332. if (out < end)
  333. *out = ((c >> 0) & 0x07) + '0';
  334. ++out;
  335. *dst = out;
  336. return true;
  337. }
  338. static bool escape_hex(unsigned char c, char **dst, char *end)
  339. {
  340. char *out = *dst;
  341. if (out < end)
  342. *out = '\\';
  343. ++out;
  344. if (out < end)
  345. *out = 'x';
  346. ++out;
  347. if (out < end)
  348. *out = hex_asc_hi(c);
  349. ++out;
  350. if (out < end)
  351. *out = hex_asc_lo(c);
  352. ++out;
  353. *dst = out;
  354. return true;
  355. }
  356. /**
  357. * string_escape_mem - quote characters in the given memory buffer
  358. * @src: source buffer (unescaped)
  359. * @isz: source buffer size
  360. * @dst: destination buffer (escaped)
  361. * @osz: destination buffer size
  362. * @flags: combination of the flags (bitwise OR):
  363. * %ESCAPE_SPACE: (special white space, not space itself)
  364. * '\f' - form feed
  365. * '\n' - new line
  366. * '\r' - carriage return
  367. * '\t' - horizontal tab
  368. * '\v' - vertical tab
  369. * %ESCAPE_SPECIAL:
  370. * '\\' - backslash
  371. * '\a' - alert (BEL)
  372. * '\e' - escape
  373. * %ESCAPE_NULL:
  374. * '\0' - null
  375. * %ESCAPE_OCTAL:
  376. * '\NNN' - byte with octal value NNN (3 digits)
  377. * %ESCAPE_ANY:
  378. * all previous together
  379. * %ESCAPE_NP:
  380. * escape only non-printable characters (checked by isprint)
  381. * %ESCAPE_ANY_NP:
  382. * all previous together
  383. * %ESCAPE_HEX:
  384. * '\xHH' - byte with hexadecimal value HH (2 digits)
  385. * @esc: NULL-terminated string containing characters used to limit
  386. * the selected escape class. If characters are included in @esc
  387. * that would not normally be escaped by the classes selected
  388. * in @flags, they will be copied to @dst unescaped.
  389. *
  390. * Description:
  391. * The process of escaping byte buffer includes several parts. They are applied
  392. * in the following sequence.
  393. * 1. The character is matched to the printable class, if asked, and in
  394. * case of match it passes through to the output.
  395. * 2. The character is not matched to the one from @esc string and thus
  396. * must go as-is to the output.
  397. * 3. The character is checked if it falls into the class given by @flags.
  398. * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
  399. * character. Note that they actually can't go together, otherwise
  400. * %ESCAPE_HEX will be ignored.
  401. *
  402. * Caller must provide valid source and destination pointers. Be aware that
  403. * destination buffer will not be NULL-terminated, thus caller have to append
  404. * it if needs.
  405. *
  406. * Return:
  407. * The total size of the escaped output that would be generated for
  408. * the given input and flags. To check whether the output was
  409. * truncated, compare the return value to osz. There is room left in
  410. * dst for a '\0' terminator if and only if ret < osz.
  411. */
  412. int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
  413. unsigned int flags, const char *esc)
  414. {
  415. char *p = dst;
  416. char *end = p + osz;
  417. bool is_dict = esc && *esc;
  418. while (isz--) {
  419. unsigned char c = *src++;
  420. /*
  421. * Apply rules in the following sequence:
  422. * - the character is printable, when @flags has
  423. * %ESCAPE_NP bit set
  424. * - the @esc string is supplied and does not contain a
  425. * character under question
  426. * - the character doesn't fall into a class of symbols
  427. * defined by given @flags
  428. * In these cases we just pass through a character to the
  429. * output buffer.
  430. */
  431. if ((flags & ESCAPE_NP && isprint(c)) ||
  432. (is_dict && !strchr(esc, c))) {
  433. /* do nothing */
  434. } else {
  435. if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
  436. continue;
  437. if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
  438. continue;
  439. if (flags & ESCAPE_NULL && escape_null(c, &p, end))
  440. continue;
  441. /* ESCAPE_OCTAL and ESCAPE_HEX always go last */
  442. if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
  443. continue;
  444. if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
  445. continue;
  446. }
  447. escape_passthrough(c, &p, end);
  448. }
  449. return p - dst;
  450. }
  451. EXPORT_SYMBOL(string_escape_mem);