string_helpers.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STRING_HELPERS_H_
  3. #define _LINUX_STRING_HELPERS_H_
  4. #include <linux/bits.h>
  5. #include <linux/ctype.h>
  6. #include <linux/string.h>
  7. #include <linux/types.h>
  8. struct device;
  9. struct file;
  10. struct task_struct;
  11. /* Descriptions of the types of units to
  12. * print in */
  13. enum string_size_units {
  14. STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
  15. STRING_UNITS_2, /* use binary powers of 2^10 */
  16. };
  17. void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
  18. char *buf, int len);
  19. int parse_int_array_user(const char __user *from, size_t count, int **array);
  20. #define UNESCAPE_SPACE BIT(0)
  21. #define UNESCAPE_OCTAL BIT(1)
  22. #define UNESCAPE_HEX BIT(2)
  23. #define UNESCAPE_SPECIAL BIT(3)
  24. #define UNESCAPE_ANY \
  25. (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
  26. #define UNESCAPE_ALL_MASK GENMASK(3, 0)
  27. int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
  28. static inline int string_unescape_inplace(char *buf, unsigned int flags)
  29. {
  30. return string_unescape(buf, buf, 0, flags);
  31. }
  32. static inline int string_unescape_any(char *src, char *dst, size_t size)
  33. {
  34. return string_unescape(src, dst, size, UNESCAPE_ANY);
  35. }
  36. static inline int string_unescape_any_inplace(char *buf)
  37. {
  38. return string_unescape_any(buf, buf, 0);
  39. }
  40. #define ESCAPE_SPACE BIT(0)
  41. #define ESCAPE_SPECIAL BIT(1)
  42. #define ESCAPE_NULL BIT(2)
  43. #define ESCAPE_OCTAL BIT(3)
  44. #define ESCAPE_ANY \
  45. (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
  46. #define ESCAPE_NP BIT(4)
  47. #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)
  48. #define ESCAPE_HEX BIT(5)
  49. #define ESCAPE_NA BIT(6)
  50. #define ESCAPE_NAP BIT(7)
  51. #define ESCAPE_APPEND BIT(8)
  52. #define ESCAPE_ALL_MASK GENMASK(8, 0)
  53. int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
  54. unsigned int flags, const char *only);
  55. static inline int string_escape_mem_any_np(const char *src, size_t isz,
  56. char *dst, size_t osz, const char *only)
  57. {
  58. return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
  59. }
  60. static inline int string_escape_str(const char *src, char *dst, size_t sz,
  61. unsigned int flags, const char *only)
  62. {
  63. return string_escape_mem(src, strlen(src), dst, sz, flags, only);
  64. }
  65. static inline int string_escape_str_any_np(const char *src, char *dst,
  66. size_t sz, const char *only)
  67. {
  68. return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
  69. }
  70. static inline void string_upper(char *dst, const char *src)
  71. {
  72. do {
  73. *dst++ = toupper(*src);
  74. } while (*src++);
  75. }
  76. static inline void string_lower(char *dst, const char *src)
  77. {
  78. do {
  79. *dst++ = tolower(*src);
  80. } while (*src++);
  81. }
  82. char *kstrdup_quotable(const char *src, gfp_t gfp);
  83. char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
  84. char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
  85. char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
  86. void kfree_strarray(char **array, size_t n);
  87. char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);
  88. static inline const char *str_yes_no(bool v)
  89. {
  90. return v ? "yes" : "no";
  91. }
  92. static inline const char *str_on_off(bool v)
  93. {
  94. return v ? "on" : "off";
  95. }
  96. static inline const char *str_enable_disable(bool v)
  97. {
  98. return v ? "enable" : "disable";
  99. }
  100. static inline const char *str_enabled_disabled(bool v)
  101. {
  102. return v ? "enabled" : "disabled";
  103. }
  104. static inline const char *str_read_write(bool v)
  105. {
  106. return v ? "read" : "write";
  107. }
  108. #endif