string_helpers.h 1.9 KB

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