string_helpers.c 9.9 KB

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