string_helpers.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/string_helpers.h>
  12. /**
  13. * string_get_size - get the size in the specified units
  14. * @size: The size to be converted
  15. * @units: units to use (powers of 1000 or 1024)
  16. * @buf: buffer to format to
  17. * @len: length of buffer
  18. *
  19. * This function returns a string formatted to 3 significant figures
  20. * giving the size in the required units. Returns 0 on success or
  21. * error on failure. @buf is always zero terminated.
  22. *
  23. */
  24. int string_get_size(u64 size, const enum string_size_units units,
  25. char *buf, int len)
  26. {
  27. static const char *const units_10[] = {
  28. "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL
  29. };
  30. static const char *const units_2[] = {
  31. "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
  32. NULL
  33. };
  34. static const char *const *const units_str[] = {
  35. [STRING_UNITS_10] = units_10,
  36. [STRING_UNITS_2] = units_2,
  37. };
  38. static const unsigned int divisor[] = {
  39. [STRING_UNITS_10] = 1000,
  40. [STRING_UNITS_2] = 1024,
  41. };
  42. int i, j;
  43. u64 remainder = 0, sf_cap;
  44. char tmp[8];
  45. tmp[0] = '\0';
  46. i = 0;
  47. if (size >= divisor[units]) {
  48. while (size >= divisor[units] && units_str[units][i]) {
  49. remainder = do_div(size, divisor[units]);
  50. i++;
  51. }
  52. sf_cap = size;
  53. for (j = 0; sf_cap*10 < 1000; j++)
  54. sf_cap *= 10;
  55. if (j) {
  56. remainder *= 1000;
  57. do_div(remainder, divisor[units]);
  58. snprintf(tmp, sizeof(tmp), ".%03lld",
  59. (unsigned long long)remainder);
  60. tmp[j+1] = '\0';
  61. }
  62. }
  63. snprintf(buf, len, "%lld%s %s", (unsigned long long)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);