string_helpers.c 9.9 KB

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