string_helpers.h 2.2 KB

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