string_helpers.c 10 KB

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