string_helpers.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. const char *units_10[] = { "B", "kB", "MB", "GB", "TB", "PB",
  28. "EB", "ZB", "YB", NULL};
  29. const char *units_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB",
  30. "EiB", "ZiB", "YiB", NULL };
  31. const char **units_str[] = {
  32. [STRING_UNITS_10] = units_10,
  33. [STRING_UNITS_2] = units_2,
  34. };
  35. const unsigned int divisor[] = {
  36. [STRING_UNITS_10] = 1000,
  37. [STRING_UNITS_2] = 1024,
  38. };
  39. int i, j;
  40. u64 remainder = 0, sf_cap;
  41. char tmp[8];
  42. tmp[0] = '\0';
  43. i = 0;
  44. if (size >= divisor[units]) {
  45. while (size >= divisor[units] && units_str[units][i]) {
  46. remainder = do_div(size, divisor[units]);
  47. i++;
  48. }
  49. sf_cap = size;
  50. for (j = 0; sf_cap*10 < 1000; j++)
  51. sf_cap *= 10;
  52. if (j) {
  53. remainder *= 1000;
  54. do_div(remainder, divisor[units]);
  55. snprintf(tmp, sizeof(tmp), ".%03lld",
  56. (unsigned long long)remainder);
  57. tmp[j+1] = '\0';
  58. }
  59. }
  60. snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
  61. tmp, units_str[units][i]);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(string_get_size);
  65. static bool unescape_space(char **src, char **dst)
  66. {
  67. char *p = *dst, *q = *src;
  68. switch (*q) {
  69. case 'n':
  70. *p = '\n';
  71. break;
  72. case 'r':
  73. *p = '\r';
  74. break;
  75. case 't':
  76. *p = '\t';
  77. break;
  78. case 'v':
  79. *p = '\v';
  80. break;
  81. case 'f':
  82. *p = '\f';
  83. break;
  84. default:
  85. return false;
  86. }
  87. *dst += 1;
  88. *src += 1;
  89. return true;
  90. }
  91. static bool unescape_octal(char **src, char **dst)
  92. {
  93. char *p = *dst, *q = *src;
  94. u8 num;
  95. if (isodigit(*q) == 0)
  96. return false;
  97. num = (*q++) & 7;
  98. while (num < 32 && isodigit(*q) && (q - *src < 3)) {
  99. num <<= 3;
  100. num += (*q++) & 7;
  101. }
  102. *p = num;
  103. *dst += 1;
  104. *src = q;
  105. return true;
  106. }
  107. static bool unescape_hex(char **src, char **dst)
  108. {
  109. char *p = *dst, *q = *src;
  110. int digit;
  111. u8 num;
  112. if (*q++ != 'x')
  113. return false;
  114. num = digit = hex_to_bin(*q++);
  115. if (digit < 0)
  116. return false;
  117. digit = hex_to_bin(*q);
  118. if (digit >= 0) {
  119. q++;
  120. num = (num << 4) | digit;
  121. }
  122. *p = num;
  123. *dst += 1;
  124. *src = q;
  125. return true;
  126. }
  127. static bool unescape_special(char **src, char **dst)
  128. {
  129. char *p = *dst, *q = *src;
  130. switch (*q) {
  131. case '\"':
  132. *p = '\"';
  133. break;
  134. case '\\':
  135. *p = '\\';
  136. break;
  137. case 'a':
  138. *p = '\a';
  139. break;
  140. case 'e':
  141. *p = '\e';
  142. break;
  143. default:
  144. return false;
  145. }
  146. *dst += 1;
  147. *src += 1;
  148. return true;
  149. }
  150. int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
  151. {
  152. char *out = dst;
  153. while (*src && --size) {
  154. if (src[0] == '\\' && src[1] != '\0' && size > 1) {
  155. src++;
  156. size--;
  157. if (flags & UNESCAPE_SPACE &&
  158. unescape_space(&src, &out))
  159. continue;
  160. if (flags & UNESCAPE_OCTAL &&
  161. unescape_octal(&src, &out))
  162. continue;
  163. if (flags & UNESCAPE_HEX &&
  164. unescape_hex(&src, &out))
  165. continue;
  166. if (flags & UNESCAPE_SPECIAL &&
  167. unescape_special(&src, &out))
  168. continue;
  169. *out++ = '\\';
  170. }
  171. *out++ = *src++;
  172. }
  173. *out = '\0';
  174. return out - dst;
  175. }
  176. EXPORT_SYMBOL(string_unescape);