string_helpers.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STRING_HELPERS_H_
  3. #define _LINUX_STRING_HELPERS_H_
  4. #include <linux/ctype.h>
  5. #include <linux/types.h>
  6. struct file;
  7. struct task_struct;
  8. /* Descriptions of the types of units to
  9. * print in */
  10. enum string_size_units {
  11. STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
  12. STRING_UNITS_2, /* use binary powers of 2^10 */
  13. };
  14. void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
  15. char *buf, int len);
  16. #define UNESCAPE_SPACE 0x01
  17. #define UNESCAPE_OCTAL 0x02
  18. #define UNESCAPE_HEX 0x04
  19. #define UNESCAPE_SPECIAL 0x08
  20. #define UNESCAPE_ANY \
  21. (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
  22. int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
  23. static inline int string_unescape_inplace(char *buf, unsigned int flags)
  24. {
  25. return string_unescape(buf, buf, 0, flags);
  26. }
  27. static inline int string_unescape_any(char *src, char *dst, size_t size)
  28. {
  29. return string_unescape(src, dst, size, UNESCAPE_ANY);
  30. }
  31. static inline int string_unescape_any_inplace(char *buf)
  32. {
  33. return string_unescape_any(buf, buf, 0);
  34. }
  35. #define ESCAPE_SPACE 0x01
  36. #define ESCAPE_SPECIAL 0x02
  37. #define ESCAPE_NULL 0x04
  38. #define ESCAPE_OCTAL 0x08
  39. #define ESCAPE_ANY \
  40. (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
  41. #define ESCAPE_NP 0x10
  42. #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)
  43. #define ESCAPE_HEX 0x20
  44. int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
  45. unsigned int flags, const char *only);
  46. int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
  47. size_t osz);
  48. static inline int string_escape_mem_any_np(const char *src, size_t isz,
  49. char *dst, size_t osz, const char *only)
  50. {
  51. return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
  52. }
  53. static inline int string_escape_str(const char *src, char *dst, size_t sz,
  54. unsigned int flags, const char *only)
  55. {
  56. return string_escape_mem(src, strlen(src), dst, sz, flags, only);
  57. }
  58. static inline int string_escape_str_any_np(const char *src, char *dst,
  59. size_t sz, const char *only)
  60. {
  61. return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
  62. }
  63. static inline void string_upper(char *dst, const char *src)
  64. {
  65. do {
  66. *dst++ = toupper(*src);
  67. } while (*src++);
  68. }
  69. static inline void string_lower(char *dst, const char *src)
  70. {
  71. do {
  72. *dst++ = tolower(*src);
  73. } while (*src++);
  74. }
  75. char *kstrdup_quotable(const char *src, gfp_t gfp);
  76. char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
  77. char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
  78. void kfree_strarray(char **array, size_t n);
  79. #endif